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 | |
Prabir Pradhan | d2c9e8e | 2021-05-24 15:00:12 -0700 | [diff] [blame] | 22 | #include <InputFlingerProperties.sysprop.h> |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 23 | #include <android-base/chrono_utils.h> |
Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 24 | #include <android-base/properties.h> |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 25 | #include <android-base/stringprintf.h> |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 26 | #include <android/os/IInputConstants.h> |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 27 | #include <binder/Binder.h> |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 28 | #include <binder/IServiceManager.h> |
| 29 | #include <com/android/internal/compat/IPlatformCompatNative.h> |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 30 | #include <ftl/enum.h> |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 31 | #include <gui/SurfaceComposerClient.h> |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 32 | #include <input/InputDevice.h> |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 33 | #include <log/log.h> |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 34 | #include <log/log_event_list.h> |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 35 | #include <powermanager/PowerManager.h> |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 36 | #include <unistd.h> |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 37 | #include <utils/Trace.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 38 | |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 39 | #include <cerrno> |
| 40 | #include <cinttypes> |
| 41 | #include <climits> |
| 42 | #include <cstddef> |
| 43 | #include <ctime> |
| 44 | #include <queue> |
| 45 | #include <sstream> |
| 46 | |
| 47 | #include "Connection.h" |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 48 | #include "InputDispatcher.h" |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 49 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 50 | #define INDENT " " |
| 51 | #define INDENT2 " " |
| 52 | #define INDENT3 " " |
| 53 | #define INDENT4 " " |
| 54 | |
Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 55 | using android::base::HwTimeoutMultiplier; |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 56 | using android::base::Result; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 57 | using android::base::StringPrintf; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 58 | using android::gui::DisplayInfo; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 59 | using android::gui::FocusRequest; |
| 60 | using android::gui::TouchOcclusionMode; |
| 61 | using android::gui::WindowInfo; |
| 62 | using android::gui::WindowInfoHandle; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 63 | using android::os::BlockUntrustedTouchesMode; |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 64 | using android::os::IInputConstants; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 65 | using android::os::InputEventInjectionResult; |
| 66 | using android::os::InputEventInjectionSync; |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 67 | using com::android::internal::compat::IPlatformCompatNative; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 68 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 69 | namespace android::inputdispatcher { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 70 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 71 | namespace { |
| 72 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 73 | // Log detailed debug messages about each inbound event notification to the dispatcher. |
| 74 | constexpr bool DEBUG_INBOUND_EVENT_DETAILS = false; |
| 75 | |
| 76 | // Log detailed debug messages about each outbound event processed by the dispatcher. |
| 77 | constexpr bool DEBUG_OUTBOUND_EVENT_DETAILS = false; |
| 78 | |
| 79 | // Log debug messages about the dispatch cycle. |
| 80 | constexpr bool DEBUG_DISPATCH_CYCLE = false; |
| 81 | |
| 82 | // Log debug messages about channel creation |
| 83 | constexpr bool DEBUG_CHANNEL_CREATION = false; |
| 84 | |
| 85 | // Log debug messages about input event injection. |
| 86 | constexpr bool DEBUG_INJECTION = false; |
| 87 | |
| 88 | // Log debug messages about input focus tracking. |
| 89 | constexpr bool DEBUG_FOCUS = false; |
| 90 | |
| 91 | // Log debug messages about touch occlusion |
| 92 | // STOPSHIP(b/169067926): Set to false |
| 93 | constexpr bool DEBUG_TOUCH_OCCLUSION = true; |
| 94 | |
| 95 | // Log debug messages about the app switch latency optimization. |
| 96 | constexpr bool DEBUG_APP_SWITCH = false; |
| 97 | |
| 98 | // Log debug messages about hover events. |
| 99 | constexpr bool DEBUG_HOVER = false; |
| 100 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 101 | // Temporarily releases a held mutex for the lifetime of the instance. |
| 102 | // Named to match std::scoped_lock |
| 103 | class scoped_unlock { |
| 104 | public: |
| 105 | explicit scoped_unlock(std::mutex& mutex) : mMutex(mutex) { mMutex.unlock(); } |
| 106 | ~scoped_unlock() { mMutex.lock(); } |
| 107 | |
| 108 | private: |
| 109 | std::mutex& mMutex; |
| 110 | }; |
| 111 | |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 112 | // When per-window-input-rotation is enabled, InputFlinger works in the un-rotated display |
| 113 | // coordinates and SurfaceFlinger includes the display rotation in the input window transforms. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 114 | bool isPerWindowInputRotationEnabled() { |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 115 | static const bool PER_WINDOW_INPUT_ROTATION = |
Vadim Tryshev | 7719c7d | 2021-08-27 17:28:43 +0000 | [diff] [blame] | 116 | sysprop::InputFlingerProperties::per_window_input_rotation().value_or(false); |
Prabir Pradhan | d2c9e8e | 2021-05-24 15:00:12 -0700 | [diff] [blame] | 117 | |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 118 | return PER_WINDOW_INPUT_ROTATION; |
| 119 | } |
| 120 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 121 | // Default input dispatching timeout if there is no focused application or paused window |
| 122 | // from which to determine an appropriate dispatching timeout. |
Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 123 | const std::chrono::duration DEFAULT_INPUT_DISPATCHING_TIMEOUT = std::chrono::milliseconds( |
| 124 | android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS * |
| 125 | HwTimeoutMultiplier()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 126 | |
| 127 | // Amount of time to allow for all pending events to be processed when an app switch |
| 128 | // key is on the way. This is used to preempt input dispatch and drop input events |
| 129 | // 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] | 130 | constexpr nsecs_t APP_SWITCH_TIMEOUT = 500 * 1000000LL; // 0.5sec |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 131 | |
| 132 | // Amount of time to allow for an event to be dispatched (measured since its eventTime) |
| 133 | // before considering it stale and dropping it. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 134 | constexpr nsecs_t STALE_EVENT_TIMEOUT = 10000 * 1000000LL; // 10sec |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 135 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 136 | // 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] | 137 | constexpr nsecs_t SLOW_EVENT_PROCESSING_WARNING_TIMEOUT = 2000 * 1000000LL; // 2sec |
| 138 | |
| 139 | // Log a warning when an interception call takes longer than this to process. |
| 140 | constexpr std::chrono::milliseconds SLOW_INTERCEPTION_THRESHOLD = 50ms; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 141 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 142 | // Additional key latency in case a connection is still processing some motion events. |
| 143 | // This will help with the case when a user touched a button that opens a new window, |
| 144 | // and gives us the chance to dispatch the key to this new window. |
| 145 | constexpr std::chrono::nanoseconds KEY_WAITING_FOR_EVENTS_TIMEOUT = 500ms; |
| 146 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 147 | // Number of recent events to keep for debugging purposes. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 148 | constexpr size_t RECENT_QUEUE_MAX_SIZE = 10; |
| 149 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 150 | // Event log tags. See EventLogTags.logtags for reference |
| 151 | constexpr int LOGTAG_INPUT_INTERACTION = 62000; |
| 152 | constexpr int LOGTAG_INPUT_FOCUS = 62001; |
| 153 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 154 | inline nsecs_t now() { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 155 | return systemTime(SYSTEM_TIME_MONOTONIC); |
| 156 | } |
| 157 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 158 | inline const char* toString(bool value) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 159 | return value ? "true" : "false"; |
| 160 | } |
| 161 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 162 | inline const std::string toString(const sp<IBinder>& binder) { |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 163 | if (binder == nullptr) { |
| 164 | return "<null>"; |
| 165 | } |
| 166 | return StringPrintf("%p", binder.get()); |
| 167 | } |
| 168 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 169 | inline int32_t getMotionEventActionPointerIndex(int32_t action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 170 | return (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> |
| 171 | AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 172 | } |
| 173 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 174 | bool isValidKeyAction(int32_t action) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 175 | switch (action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 176 | case AKEY_EVENT_ACTION_DOWN: |
| 177 | case AKEY_EVENT_ACTION_UP: |
| 178 | return true; |
| 179 | default: |
| 180 | return false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 184 | bool validateKeyEvent(int32_t action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 185 | if (!isValidKeyAction(action)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 186 | ALOGE("Key event has invalid action code 0x%x", action); |
| 187 | return false; |
| 188 | } |
| 189 | return true; |
| 190 | } |
| 191 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 192 | bool isValidMotionAction(int32_t action, int32_t actionButton, int32_t pointerCount) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 193 | switch (action & AMOTION_EVENT_ACTION_MASK) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 194 | case AMOTION_EVENT_ACTION_DOWN: |
| 195 | case AMOTION_EVENT_ACTION_UP: |
| 196 | case AMOTION_EVENT_ACTION_CANCEL: |
| 197 | case AMOTION_EVENT_ACTION_MOVE: |
| 198 | case AMOTION_EVENT_ACTION_OUTSIDE: |
| 199 | case AMOTION_EVENT_ACTION_HOVER_ENTER: |
| 200 | case AMOTION_EVENT_ACTION_HOVER_MOVE: |
| 201 | case AMOTION_EVENT_ACTION_HOVER_EXIT: |
| 202 | case AMOTION_EVENT_ACTION_SCROLL: |
| 203 | return true; |
| 204 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 205 | case AMOTION_EVENT_ACTION_POINTER_UP: { |
| 206 | int32_t index = getMotionEventActionPointerIndex(action); |
| 207 | return index >= 0 && index < pointerCount; |
| 208 | } |
| 209 | case AMOTION_EVENT_ACTION_BUTTON_PRESS: |
| 210 | case AMOTION_EVENT_ACTION_BUTTON_RELEASE: |
| 211 | return actionButton != 0; |
| 212 | default: |
| 213 | return false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 217 | int64_t millis(std::chrono::nanoseconds t) { |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 218 | return std::chrono::duration_cast<std::chrono::milliseconds>(t).count(); |
| 219 | } |
| 220 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 221 | bool validateMotionEvent(int32_t action, int32_t actionButton, size_t pointerCount, |
| 222 | const PointerProperties* pointerProperties) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 223 | if (!isValidMotionAction(action, actionButton, pointerCount)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 224 | ALOGE("Motion event has invalid action code 0x%x", action); |
| 225 | return false; |
| 226 | } |
| 227 | if (pointerCount < 1 || pointerCount > MAX_POINTERS) { |
Narayan Kamath | 37764c7 | 2014-03-27 14:21:09 +0000 | [diff] [blame] | 228 | 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] | 229 | pointerCount, MAX_POINTERS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 230 | return false; |
| 231 | } |
| 232 | BitSet32 pointerIdBits; |
| 233 | for (size_t i = 0; i < pointerCount; i++) { |
| 234 | int32_t id = pointerProperties[i].id; |
| 235 | if (id < 0 || id > MAX_POINTER_ID) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 236 | ALOGE("Motion event has invalid pointer id %d; value must be between 0 and %d", id, |
| 237 | MAX_POINTER_ID); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 238 | return false; |
| 239 | } |
| 240 | if (pointerIdBits.hasBit(id)) { |
| 241 | ALOGE("Motion event has duplicate pointer id %d", id); |
| 242 | return false; |
| 243 | } |
| 244 | pointerIdBits.markBit(id); |
| 245 | } |
| 246 | return true; |
| 247 | } |
| 248 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 249 | std::string dumpRegion(const Region& region) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 250 | if (region.isEmpty()) { |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 251 | return "<empty>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 252 | } |
| 253 | |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 254 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 255 | bool first = true; |
| 256 | Region::const_iterator cur = region.begin(); |
| 257 | Region::const_iterator const tail = region.end(); |
| 258 | while (cur != tail) { |
| 259 | if (first) { |
| 260 | first = false; |
| 261 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 262 | dump += "|"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 263 | } |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 264 | 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] | 265 | cur++; |
| 266 | } |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 267 | return dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 268 | } |
| 269 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 270 | std::string dumpQueue(const std::deque<DispatchEntry*>& queue, nsecs_t currentTime) { |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 271 | constexpr size_t maxEntries = 50; // max events to print |
| 272 | constexpr size_t skipBegin = maxEntries / 2; |
| 273 | const size_t skipEnd = queue.size() - maxEntries / 2; |
| 274 | // skip from maxEntries / 2 ... size() - maxEntries/2 |
| 275 | // only print from 0 .. skipBegin and then from skipEnd .. size() |
| 276 | |
| 277 | std::string dump; |
| 278 | for (size_t i = 0; i < queue.size(); i++) { |
| 279 | const DispatchEntry& entry = *queue[i]; |
| 280 | if (i >= skipBegin && i < skipEnd) { |
| 281 | dump += StringPrintf(INDENT4 "<skipped %zu entries>\n", skipEnd - skipBegin); |
| 282 | i = skipEnd - 1; // it will be incremented to "skipEnd" by 'continue' |
| 283 | continue; |
| 284 | } |
| 285 | dump.append(INDENT4); |
| 286 | dump += entry.eventEntry->getDescription(); |
| 287 | dump += StringPrintf(", seq=%" PRIu32 |
| 288 | ", targetFlags=0x%08x, resolvedAction=%d, age=%" PRId64 "ms", |
| 289 | entry.seq, entry.targetFlags, entry.resolvedAction, |
| 290 | ns2ms(currentTime - entry.eventEntry->eventTime)); |
| 291 | if (entry.deliveryTime != 0) { |
| 292 | // This entry was delivered, so add information on how long we've been waiting |
| 293 | dump += StringPrintf(", wait=%" PRId64 "ms", ns2ms(currentTime - entry.deliveryTime)); |
| 294 | } |
| 295 | dump.append("\n"); |
| 296 | } |
| 297 | return dump; |
| 298 | } |
| 299 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 300 | /** |
| 301 | * Find the entry in std::unordered_map by key, and return it. |
| 302 | * If the entry is not found, return a default constructed entry. |
| 303 | * |
| 304 | * Useful when the entries are vectors, since an empty vector will be returned |
| 305 | * if the entry is not found. |
| 306 | * Also useful when the entries are sp<>. If an entry is not found, nullptr is returned. |
| 307 | */ |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 308 | template <typename K, typename V> |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 309 | V getValueByKey(const std::unordered_map<K, V>& map, K key) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 310 | auto it = map.find(key); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 311 | return it != map.end() ? it->second : V{}; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 312 | } |
| 313 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 314 | bool haveSameToken(const sp<WindowInfoHandle>& first, const sp<WindowInfoHandle>& second) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 315 | if (first == second) { |
| 316 | return true; |
| 317 | } |
| 318 | |
| 319 | if (first == nullptr || second == nullptr) { |
| 320 | return false; |
| 321 | } |
| 322 | |
| 323 | return first->getToken() == second->getToken(); |
| 324 | } |
| 325 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 326 | bool haveSameApplicationToken(const WindowInfo* first, const WindowInfo* second) { |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 327 | if (first == nullptr || second == nullptr) { |
| 328 | return false; |
| 329 | } |
| 330 | return first->applicationInfo.token != nullptr && |
| 331 | first->applicationInfo.token == second->applicationInfo.token; |
| 332 | } |
| 333 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 334 | bool isStaleEvent(nsecs_t currentTime, const EventEntry& entry) { |
Siarhei Vishniakou | adfd4fa | 2019-12-20 11:02:58 -0800 | [diff] [blame] | 335 | return currentTime - entry.eventTime >= STALE_EVENT_TIMEOUT; |
| 336 | } |
| 337 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 338 | std::unique_ptr<DispatchEntry> createDispatchEntry(const InputTarget& inputTarget, |
| 339 | std::shared_ptr<EventEntry> eventEntry, |
| 340 | int32_t inputTargetFlags) { |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 341 | if (eventEntry->type == EventEntry::Type::MOTION) { |
| 342 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); |
Prabir Pradhan | 664834b | 2021-05-20 16:00:42 -0700 | [diff] [blame] | 343 | if ((motionEntry.source & AINPUT_SOURCE_CLASS_JOYSTICK) || |
| 344 | (motionEntry.source & AINPUT_SOURCE_CLASS_POSITION)) { |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 345 | const ui::Transform identityTransform; |
Prabir Pradhan | 664834b | 2021-05-20 16:00:42 -0700 | [diff] [blame] | 346 | // Use identity transform for joystick and position-based (touchpad) events because they |
| 347 | // don't depend on the window transform. |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 348 | return std::make_unique<DispatchEntry>(eventEntry, inputTargetFlags, identityTransform, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 349 | identityTransform, 1.0f /*globalScaleFactor*/); |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 353 | if (inputTarget.useDefaultPointerTransform()) { |
| 354 | const ui::Transform& transform = inputTarget.getDefaultPointerTransform(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 355 | return std::make_unique<DispatchEntry>(eventEntry, inputTargetFlags, transform, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 356 | inputTarget.displayTransform, |
| 357 | inputTarget.globalScaleFactor); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | ALOG_ASSERT(eventEntry->type == EventEntry::Type::MOTION); |
| 361 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); |
| 362 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 363 | std::vector<PointerCoords> pointerCoords; |
| 364 | pointerCoords.resize(motionEntry.pointerCount); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 365 | |
| 366 | // Use the first pointer information to normalize all other pointers. This could be any pointer |
| 367 | // 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] | 368 | // uses the transform for the normalized pointer. |
| 369 | const ui::Transform& firstPointerTransform = |
| 370 | inputTarget.pointerTransforms[inputTarget.pointerIds.firstMarkedBit()]; |
| 371 | ui::Transform inverseFirstTransform = firstPointerTransform.inverse(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 372 | |
| 373 | // Iterate through all pointers in the event to normalize against the first. |
| 374 | for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.pointerCount; pointerIndex++) { |
| 375 | const PointerProperties& pointerProperties = motionEntry.pointerProperties[pointerIndex]; |
| 376 | uint32_t pointerId = uint32_t(pointerProperties.id); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 377 | const ui::Transform& currTransform = inputTarget.pointerTransforms[pointerId]; |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 378 | |
| 379 | pointerCoords[pointerIndex].copyFrom(motionEntry.pointerCoords[pointerIndex]); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 380 | // First, apply the current pointer's transform to update the coordinates into |
| 381 | // window space. |
| 382 | pointerCoords[pointerIndex].transform(currTransform); |
| 383 | // Next, apply the inverse transform of the normalized coordinates so the |
| 384 | // current coordinates are transformed into the normalized coordinate space. |
| 385 | pointerCoords[pointerIndex].transform(inverseFirstTransform); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 386 | } |
| 387 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 388 | std::unique_ptr<MotionEntry> combinedMotionEntry = |
| 389 | std::make_unique<MotionEntry>(motionEntry.id, motionEntry.eventTime, |
| 390 | motionEntry.deviceId, motionEntry.source, |
| 391 | motionEntry.displayId, motionEntry.policyFlags, |
| 392 | motionEntry.action, motionEntry.actionButton, |
| 393 | motionEntry.flags, motionEntry.metaState, |
| 394 | motionEntry.buttonState, motionEntry.classification, |
| 395 | motionEntry.edgeFlags, motionEntry.xPrecision, |
| 396 | motionEntry.yPrecision, motionEntry.xCursorPosition, |
| 397 | motionEntry.yCursorPosition, motionEntry.downTime, |
| 398 | motionEntry.pointerCount, motionEntry.pointerProperties, |
| 399 | pointerCoords.data(), 0 /* xOffset */, 0 /* yOffset */); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 400 | |
| 401 | if (motionEntry.injectionState) { |
| 402 | combinedMotionEntry->injectionState = motionEntry.injectionState; |
| 403 | combinedMotionEntry->injectionState->refCount += 1; |
| 404 | } |
| 405 | |
| 406 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 407 | std::make_unique<DispatchEntry>(std::move(combinedMotionEntry), inputTargetFlags, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 408 | firstPointerTransform, inputTarget.displayTransform, |
| 409 | inputTarget.globalScaleFactor); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 410 | return dispatchEntry; |
| 411 | } |
| 412 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 413 | status_t openInputChannelPair(const std::string& name, std::shared_ptr<InputChannel>& serverChannel, |
| 414 | std::unique_ptr<InputChannel>& clientChannel) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 415 | std::unique_ptr<InputChannel> uniqueServerChannel; |
| 416 | status_t result = InputChannel::openInputChannelPair(name, uniqueServerChannel, clientChannel); |
| 417 | |
| 418 | serverChannel = std::move(uniqueServerChannel); |
| 419 | return result; |
| 420 | } |
| 421 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 422 | template <typename T> |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 423 | bool sharedPointersEqual(const std::shared_ptr<T>& lhs, const std::shared_ptr<T>& rhs) { |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 424 | if (lhs == nullptr && rhs == nullptr) { |
| 425 | return true; |
| 426 | } |
| 427 | if (lhs == nullptr || rhs == nullptr) { |
| 428 | return false; |
| 429 | } |
| 430 | return *lhs == *rhs; |
| 431 | } |
| 432 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 433 | sp<IPlatformCompatNative> getCompatService() { |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 434 | sp<IBinder> service(defaultServiceManager()->getService(String16("platform_compat_native"))); |
| 435 | if (service == nullptr) { |
| 436 | ALOGE("Failed to link to compat service"); |
| 437 | return nullptr; |
| 438 | } |
| 439 | return interface_cast<IPlatformCompatNative>(service); |
| 440 | } |
| 441 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 442 | KeyEvent createKeyEvent(const KeyEntry& entry) { |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 443 | KeyEvent event; |
| 444 | event.initialize(entry.id, entry.deviceId, entry.source, entry.displayId, INVALID_HMAC, |
| 445 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, |
| 446 | entry.repeatCount, entry.downTime, entry.eventTime); |
| 447 | return event; |
| 448 | } |
| 449 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 450 | std::optional<int32_t> findMonitorPidByToken( |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 451 | const std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay, |
| 452 | const sp<IBinder>& token) { |
| 453 | for (const auto& it : monitorsByDisplay) { |
| 454 | const std::vector<Monitor>& monitors = it.second; |
| 455 | for (const Monitor& monitor : monitors) { |
| 456 | if (monitor.inputChannel->getConnectionToken() == token) { |
| 457 | return monitor.pid; |
| 458 | } |
| 459 | } |
| 460 | } |
| 461 | return std::nullopt; |
| 462 | } |
| 463 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 464 | bool shouldReportMetricsForConnection(const Connection& connection) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 465 | // Do not keep track of gesture monitors. They receive every event and would disproportionately |
| 466 | // affect the statistics. |
| 467 | if (connection.monitor) { |
| 468 | return false; |
| 469 | } |
| 470 | // If the connection is experiencing ANR, let's skip it. We have separate ANR metrics |
| 471 | if (!connection.responsive) { |
| 472 | return false; |
| 473 | } |
| 474 | return true; |
| 475 | } |
| 476 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 477 | bool shouldReportFinishedEvent(const DispatchEntry& dispatchEntry, const Connection& connection) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 478 | const EventEntry& eventEntry = *dispatchEntry.eventEntry; |
| 479 | const int32_t& inputEventId = eventEntry.id; |
| 480 | if (inputEventId != dispatchEntry.resolvedEventId) { |
| 481 | // Event was transmuted |
| 482 | return false; |
| 483 | } |
| 484 | if (inputEventId == android::os::IInputConstants::INVALID_INPUT_EVENT_ID) { |
| 485 | return false; |
| 486 | } |
| 487 | // Only track latency for events that originated from hardware |
| 488 | if (eventEntry.isSynthesized()) { |
| 489 | return false; |
| 490 | } |
| 491 | const EventEntry::Type& inputEventEntryType = eventEntry.type; |
| 492 | if (inputEventEntryType == EventEntry::Type::KEY) { |
| 493 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 494 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
| 495 | return false; |
| 496 | } |
| 497 | } else if (inputEventEntryType == EventEntry::Type::MOTION) { |
| 498 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 499 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL || |
| 500 | motionEntry.action == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 501 | return false; |
| 502 | } |
| 503 | } else { |
| 504 | // Not a key or a motion |
| 505 | return false; |
| 506 | } |
| 507 | if (!shouldReportMetricsForConnection(connection)) { |
| 508 | return false; |
| 509 | } |
| 510 | return true; |
| 511 | } |
| 512 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 513 | /** |
| 514 | * Connection is responsive if it has no events in the waitQueue that are older than the |
| 515 | * current time. |
| 516 | */ |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 517 | bool isConnectionResponsive(const Connection& connection) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 518 | const nsecs_t currentTime = now(); |
| 519 | for (const DispatchEntry* entry : connection.waitQueue) { |
| 520 | if (entry->timeoutTime < currentTime) { |
| 521 | return false; |
| 522 | } |
| 523 | } |
| 524 | return true; |
| 525 | } |
| 526 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 527 | bool isFromSource(uint32_t source, uint32_t test) { |
| 528 | return (source & test) == test; |
| 529 | } |
| 530 | |
| 531 | vec2 transformWithoutTranslation(const ui::Transform& transform, float x, float y) { |
| 532 | const vec2 transformedXy = transform.transform(x, y); |
| 533 | const vec2 transformedOrigin = transform.transform(0, 0); |
| 534 | return transformedXy - transformedOrigin; |
| 535 | } |
| 536 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 537 | } // namespace |
| 538 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 539 | // --- InputDispatcher --- |
| 540 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 541 | InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy) |
| 542 | : mPolicy(policy), |
| 543 | mPendingEvent(nullptr), |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 544 | mLastDropReason(DropReason::NOT_DROPPED), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 545 | mIdGenerator(IdGenerator::Source::INPUT_DISPATCHER), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 546 | mAppSwitchSawKeyDown(false), |
| 547 | mAppSwitchDueTime(LONG_LONG_MAX), |
| 548 | mNextUnblockedEvent(nullptr), |
| 549 | mDispatchEnabled(false), |
| 550 | mDispatchFrozen(false), |
| 551 | mInputFilterEnabled(false), |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 552 | // mInTouchMode will be initialized by the WindowManager to the default device config. |
| 553 | // To avoid leaking stack in case that call never comes, and for tests, |
| 554 | // initialize it here anyways. |
| 555 | mInTouchMode(true), |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 556 | mMaximumObscuringOpacityForTouch(1.0f), |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 557 | mFocusedDisplayId(ADISPLAY_ID_DEFAULT), |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 558 | mWindowTokenWithPointerCapture(nullptr), |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 559 | mLatencyAggregator(), |
| 560 | mLatencyTracker(&mLatencyAggregator), |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 561 | mCompatService(getCompatService()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 562 | mLooper = new Looper(false); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 563 | mReporter = createInputReporter(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 564 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 565 | mWindowInfoListener = new DispatcherWindowListener(*this); |
| 566 | SurfaceComposerClient::getDefault()->addWindowInfosListener(mWindowInfoListener); |
| 567 | |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 568 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 569 | |
| 570 | policy->getDispatcherConfiguration(&mConfig); |
| 571 | } |
| 572 | |
| 573 | InputDispatcher::~InputDispatcher() { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 574 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 575 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 576 | resetKeyRepeatLocked(); |
| 577 | releasePendingEventLocked(); |
| 578 | drainInboundQueueLocked(); |
| 579 | mCommandQueue.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 580 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 581 | while (!mConnectionsByToken.empty()) { |
| 582 | sp<Connection> connection = mConnectionsByToken.begin()->second; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 583 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), |
| 584 | false /* notify */); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 585 | } |
| 586 | } |
| 587 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 588 | status_t InputDispatcher::start() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 589 | if (mThread) { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 590 | return ALREADY_EXISTS; |
| 591 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 592 | mThread = std::make_unique<InputThread>( |
| 593 | "InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); }); |
| 594 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | status_t InputDispatcher::stop() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 598 | if (mThread && mThread->isCallingThread()) { |
| 599 | ALOGE("InputDispatcher cannot be stopped from its own thread!"); |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 600 | return INVALID_OPERATION; |
| 601 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 602 | mThread.reset(); |
| 603 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 604 | } |
| 605 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 606 | void InputDispatcher::dispatchOnce() { |
| 607 | nsecs_t nextWakeupTime = LONG_LONG_MAX; |
| 608 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 609 | std::scoped_lock _l(mLock); |
| 610 | mDispatcherIsAlive.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 611 | |
| 612 | // Run a dispatch loop if there are no pending commands. |
| 613 | // The dispatch loop might enqueue commands to run afterwards. |
| 614 | if (!haveCommandsLocked()) { |
| 615 | dispatchOnceInnerLocked(&nextWakeupTime); |
| 616 | } |
| 617 | |
| 618 | // Run all pending commands if there are any. |
| 619 | // If any commands were run then force the next poll to wake up immediately. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 620 | if (runCommandsLockedInterruptable()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 621 | nextWakeupTime = LONG_LONG_MIN; |
| 622 | } |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 623 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 624 | // If we are still waiting for ack on some events, |
| 625 | // we might have to wake up earlier to check if an app is anr'ing. |
| 626 | const nsecs_t nextAnrCheck = processAnrsLocked(); |
| 627 | nextWakeupTime = std::min(nextWakeupTime, nextAnrCheck); |
| 628 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 629 | // We are about to enter an infinitely long sleep, because we have no commands or |
| 630 | // pending or queued events |
| 631 | if (nextWakeupTime == LONG_LONG_MAX) { |
| 632 | mDispatcherEnteredIdle.notify_all(); |
| 633 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 634 | } // release lock |
| 635 | |
| 636 | // Wait for callback or timeout or wake. (make sure we round up, not down) |
| 637 | nsecs_t currentTime = now(); |
| 638 | int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime); |
| 639 | mLooper->pollOnce(timeoutMillis); |
| 640 | } |
| 641 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 642 | /** |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 643 | * Raise ANR if there is no focused window. |
| 644 | * Before the ANR is raised, do a final state check: |
| 645 | * 1. The currently focused application must be the same one we are waiting for. |
| 646 | * 2. Ensure we still don't have a focused window. |
| 647 | */ |
| 648 | void InputDispatcher::processNoFocusedWindowAnrLocked() { |
| 649 | // Check if the application that we are waiting for is still focused. |
| 650 | std::shared_ptr<InputApplicationHandle> focusedApplication = |
| 651 | getValueByKey(mFocusedApplicationHandlesByDisplay, mAwaitedApplicationDisplayId); |
| 652 | if (focusedApplication == nullptr || |
| 653 | focusedApplication->getApplicationToken() != |
| 654 | mAwaitedFocusedApplication->getApplicationToken()) { |
| 655 | // Unexpected because we should have reset the ANR timer when focused application changed |
| 656 | ALOGE("Waited for a focused window, but focused application has already changed to %s", |
| 657 | focusedApplication->getName().c_str()); |
| 658 | return; // The focused application has changed. |
| 659 | } |
| 660 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 661 | const sp<WindowInfoHandle>& focusedWindowHandle = |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 662 | getFocusedWindowHandleLocked(mAwaitedApplicationDisplayId); |
| 663 | if (focusedWindowHandle != nullptr) { |
| 664 | return; // We now have a focused window. No need for ANR. |
| 665 | } |
| 666 | onAnrLocked(mAwaitedFocusedApplication); |
| 667 | } |
| 668 | |
| 669 | /** |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 670 | * Check if any of the connections' wait queues have events that are too old. |
| 671 | * If we waited for events to be ack'ed for more than the window timeout, raise an ANR. |
| 672 | * Return the time at which we should wake up next. |
| 673 | */ |
| 674 | nsecs_t InputDispatcher::processAnrsLocked() { |
| 675 | const nsecs_t currentTime = now(); |
| 676 | nsecs_t nextAnrCheck = LONG_LONG_MAX; |
| 677 | // Check if we are waiting for a focused window to appear. Raise ANR if waited too long |
| 678 | if (mNoFocusedWindowTimeoutTime.has_value() && mAwaitedFocusedApplication != nullptr) { |
| 679 | if (currentTime >= *mNoFocusedWindowTimeoutTime) { |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 680 | processNoFocusedWindowAnrLocked(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 681 | mAwaitedFocusedApplication.reset(); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 682 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 683 | return LONG_LONG_MIN; |
| 684 | } else { |
Siarhei Vishniakou | 38a6d27 | 2020-10-20 20:29:33 -0500 | [diff] [blame] | 685 | // Keep waiting. We will drop the event when mNoFocusedWindowTimeoutTime comes. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 686 | nextAnrCheck = *mNoFocusedWindowTimeoutTime; |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | // Check if any connection ANRs are due |
| 691 | nextAnrCheck = std::min(nextAnrCheck, mAnrTracker.firstTimeout()); |
| 692 | if (currentTime < nextAnrCheck) { // most likely scenario |
| 693 | return nextAnrCheck; // everything is normal. Let's check again at nextAnrCheck |
| 694 | } |
| 695 | |
| 696 | // If we reached here, we have an unresponsive connection. |
| 697 | sp<Connection> connection = getConnectionLocked(mAnrTracker.firstToken()); |
| 698 | if (connection == nullptr) { |
| 699 | ALOGE("Could not find connection for entry %" PRId64, mAnrTracker.firstTimeout()); |
| 700 | return nextAnrCheck; |
| 701 | } |
| 702 | connection->responsive = false; |
| 703 | // Stop waking up for this unresponsive connection |
| 704 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 705 | onAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 706 | return LONG_LONG_MIN; |
| 707 | } |
| 708 | |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 709 | std::chrono::nanoseconds InputDispatcher::getDispatchingTimeoutLocked(const sp<IBinder>& token) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 710 | sp<WindowInfoHandle> window = getWindowHandleLocked(token); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 711 | if (window != nullptr) { |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 712 | return window->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 713 | } |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 714 | return DEFAULT_INPUT_DISPATCHING_TIMEOUT; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 715 | } |
| 716 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 717 | void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) { |
| 718 | nsecs_t currentTime = now(); |
| 719 | |
Jeff Brown | dc5992e | 2014-04-11 01:27:26 -0700 | [diff] [blame] | 720 | // Reset the key repeat timer whenever normal dispatch is suspended while the |
| 721 | // device is in a non-interactive state. This is to ensure that we abort a key |
| 722 | // repeat if the device is just coming out of sleep. |
| 723 | if (!mDispatchEnabled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 724 | resetKeyRepeatLocked(); |
| 725 | } |
| 726 | |
| 727 | // If dispatching is frozen, do not process timeouts or try to deliver any new events. |
| 728 | if (mDispatchFrozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 729 | if (DEBUG_FOCUS) { |
| 730 | ALOGD("Dispatch frozen. Waiting some more."); |
| 731 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 732 | return; |
| 733 | } |
| 734 | |
| 735 | // Optimize latency of app switches. |
| 736 | // Essentially we start a short timeout when an app switch key (HOME / ENDCALL) has |
| 737 | // been pressed. When it expires, we preempt dispatch and drop all other pending events. |
| 738 | bool isAppSwitchDue = mAppSwitchDueTime <= currentTime; |
| 739 | if (mAppSwitchDueTime < *nextWakeupTime) { |
| 740 | *nextWakeupTime = mAppSwitchDueTime; |
| 741 | } |
| 742 | |
| 743 | // Ready to start a new event. |
| 744 | // If we don't already have a pending event, go grab one. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 745 | if (!mPendingEvent) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 746 | if (mInboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 747 | if (isAppSwitchDue) { |
| 748 | // The inbound queue is empty so the app switch key we were waiting |
| 749 | // for will never arrive. Stop waiting for it. |
| 750 | resetPendingAppSwitchLocked(false); |
| 751 | isAppSwitchDue = false; |
| 752 | } |
| 753 | |
| 754 | // Synthesize a key repeat if appropriate. |
| 755 | if (mKeyRepeatState.lastKeyEntry) { |
| 756 | if (currentTime >= mKeyRepeatState.nextRepeatTime) { |
| 757 | mPendingEvent = synthesizeKeyRepeatLocked(currentTime); |
| 758 | } else { |
| 759 | if (mKeyRepeatState.nextRepeatTime < *nextWakeupTime) { |
| 760 | *nextWakeupTime = mKeyRepeatState.nextRepeatTime; |
| 761 | } |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | // Nothing to do if there is no pending event. |
| 766 | if (!mPendingEvent) { |
| 767 | return; |
| 768 | } |
| 769 | } else { |
| 770 | // Inbound queue has at least one entry. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 771 | mPendingEvent = mInboundQueue.front(); |
| 772 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 773 | traceInboundQueueLengthLocked(); |
| 774 | } |
| 775 | |
| 776 | // Poke user activity for this event. |
| 777 | if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 778 | pokeUserActivityLocked(*mPendingEvent); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 779 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | // Now we have an event to dispatch. |
| 783 | // 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] | 784 | ALOG_ASSERT(mPendingEvent != nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 785 | bool done = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 786 | DropReason dropReason = DropReason::NOT_DROPPED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 787 | if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 788 | dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 789 | } else if (!mDispatchEnabled) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 790 | dropReason = DropReason::DISABLED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | if (mNextUnblockedEvent == mPendingEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 794 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | switch (mPendingEvent->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 798 | case EventEntry::Type::CONFIGURATION_CHANGED: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 799 | const ConfigurationChangedEntry& typedEntry = |
| 800 | static_cast<const ConfigurationChangedEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 801 | done = dispatchConfigurationChangedLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 802 | dropReason = DropReason::NOT_DROPPED; // configuration changes are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 803 | break; |
| 804 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 805 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 806 | case EventEntry::Type::DEVICE_RESET: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 807 | const DeviceResetEntry& typedEntry = |
| 808 | static_cast<const DeviceResetEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 809 | done = dispatchDeviceResetLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 810 | dropReason = DropReason::NOT_DROPPED; // device resets are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 811 | break; |
| 812 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 813 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 814 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 815 | std::shared_ptr<FocusEntry> typedEntry = |
| 816 | std::static_pointer_cast<FocusEntry>(mPendingEvent); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 817 | dispatchFocusLocked(currentTime, typedEntry); |
| 818 | done = true; |
| 819 | dropReason = DropReason::NOT_DROPPED; // focus events are never dropped |
| 820 | break; |
| 821 | } |
| 822 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 823 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 824 | const auto typedEntry = std::static_pointer_cast<TouchModeEntry>(mPendingEvent); |
| 825 | dispatchTouchModeChangeLocked(currentTime, typedEntry); |
| 826 | done = true; |
| 827 | dropReason = DropReason::NOT_DROPPED; // touch mode events are never dropped |
| 828 | break; |
| 829 | } |
| 830 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 831 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 832 | const auto typedEntry = |
| 833 | std::static_pointer_cast<PointerCaptureChangedEntry>(mPendingEvent); |
| 834 | dispatchPointerCaptureChangedLocked(currentTime, typedEntry, dropReason); |
| 835 | done = true; |
| 836 | break; |
| 837 | } |
| 838 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 839 | case EventEntry::Type::DRAG: { |
| 840 | std::shared_ptr<DragEntry> typedEntry = |
| 841 | std::static_pointer_cast<DragEntry>(mPendingEvent); |
| 842 | dispatchDragLocked(currentTime, typedEntry); |
| 843 | done = true; |
| 844 | break; |
| 845 | } |
| 846 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 847 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 848 | std::shared_ptr<KeyEntry> keyEntry = std::static_pointer_cast<KeyEntry>(mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 849 | if (isAppSwitchDue) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 850 | if (isAppSwitchKeyEvent(*keyEntry)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 851 | resetPendingAppSwitchLocked(true); |
| 852 | isAppSwitchDue = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 853 | } else if (dropReason == DropReason::NOT_DROPPED) { |
| 854 | dropReason = DropReason::APP_SWITCH; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 855 | } |
| 856 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 857 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *keyEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 858 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 859 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 860 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 861 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 862 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 863 | done = dispatchKeyLocked(currentTime, keyEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 864 | break; |
| 865 | } |
| 866 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 867 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 868 | std::shared_ptr<MotionEntry> motionEntry = |
| 869 | std::static_pointer_cast<MotionEntry>(mPendingEvent); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 870 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 871 | dropReason = DropReason::APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 872 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 873 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *motionEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 874 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 875 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 876 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 877 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 878 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 879 | done = dispatchMotionLocked(currentTime, motionEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 880 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 881 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 882 | |
| 883 | case EventEntry::Type::SENSOR: { |
| 884 | std::shared_ptr<SensorEntry> sensorEntry = |
| 885 | std::static_pointer_cast<SensorEntry>(mPendingEvent); |
| 886 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 887 | dropReason = DropReason::APP_SWITCH; |
| 888 | } |
| 889 | // Sensor timestamps use SYSTEM_TIME_BOOTTIME time base, so we can't use |
| 890 | // 'currentTime' here, get SYSTEM_TIME_BOOTTIME instead. |
| 891 | nsecs_t bootTime = systemTime(SYSTEM_TIME_BOOTTIME); |
| 892 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(bootTime, *sensorEntry)) { |
| 893 | dropReason = DropReason::STALE; |
| 894 | } |
| 895 | dispatchSensorLocked(currentTime, sensorEntry, &dropReason, nextWakeupTime); |
| 896 | done = true; |
| 897 | break; |
| 898 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | if (done) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 902 | if (dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 903 | dropInboundEventLocked(*mPendingEvent, dropReason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 904 | } |
Michael Wright | 3a98172 | 2015-06-10 15:26:13 +0100 | [diff] [blame] | 905 | mLastDropReason = dropReason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 906 | |
| 907 | releasePendingEventLocked(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 908 | *nextWakeupTime = LONG_LONG_MIN; // force next poll to wake up immediately |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 909 | } |
| 910 | } |
| 911 | |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 912 | /** |
| 913 | * Return true if the events preceding this incoming motion event should be dropped |
| 914 | * Return false otherwise (the default behaviour) |
| 915 | */ |
| 916 | bool InputDispatcher::shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 917 | const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN && |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 918 | (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 919 | |
| 920 | // Optimize case where the current application is unresponsive and the user |
| 921 | // decides to touch a window in a different application. |
| 922 | // If the application takes too long to catch up then we drop all events preceding |
| 923 | // the touch into the other window. |
| 924 | if (isPointerDownEvent && mAwaitedFocusedApplication != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 925 | int32_t displayId = motionEntry.displayId; |
| 926 | int32_t x = static_cast<int32_t>( |
| 927 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 928 | int32_t y = static_cast<int32_t>( |
| 929 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 930 | sp<WindowInfoHandle> touchedWindowHandle = |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 931 | findTouchedWindowAtLocked(displayId, x, y, nullptr); |
| 932 | if (touchedWindowHandle != nullptr && |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 933 | touchedWindowHandle->getApplicationToken() != |
| 934 | mAwaitedFocusedApplication->getApplicationToken()) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 935 | // User touched a different application than the one we are waiting on. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 936 | ALOGI("Pruning input queue because user touched a different application while waiting " |
| 937 | "for %s", |
| 938 | mAwaitedFocusedApplication->getName().c_str()); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 939 | return true; |
| 940 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 941 | |
| 942 | // Alternatively, maybe there's a gesture monitor that could handle this event |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 943 | for (const auto& monitor : getValueByKey(mGestureMonitorsByDisplay, displayId)) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 944 | sp<Connection> connection = |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 945 | getConnectionLocked(monitor.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 34ed4d4 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 946 | if (connection != nullptr && connection->responsive) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 947 | // This monitor could take more input. Drop all events preceding this |
| 948 | // event, so that gesture monitor could get a chance to receive the stream |
| 949 | ALOGW("Pruning the input queue because %s is unresponsive, but we have a " |
| 950 | "responsive gesture monitor that may handle the event", |
| 951 | mAwaitedFocusedApplication->getName().c_str()); |
| 952 | return true; |
| 953 | } |
| 954 | } |
| 955 | } |
| 956 | |
| 957 | // Prevent getting stuck: if we have a pending key event, and some motion events that have not |
| 958 | // yet been processed by some connections, the dispatcher will wait for these motion |
| 959 | // events to be processed before dispatching the key event. This is because these motion events |
| 960 | // may cause a new window to be launched, which the user might expect to receive focus. |
| 961 | // To prevent waiting forever for such events, just send the key to the currently focused window |
| 962 | if (isPointerDownEvent && mKeyIsWaitingForEventsTimeout) { |
| 963 | ALOGD("Received a new pointer down event, stop waiting for events to process and " |
| 964 | "just send the pending key event to the focused window."); |
| 965 | mKeyIsWaitingForEventsTimeout = now(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 966 | } |
| 967 | return false; |
| 968 | } |
| 969 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 970 | bool InputDispatcher::enqueueInboundEventLocked(std::unique_ptr<EventEntry> newEntry) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 971 | bool needWake = mInboundQueue.empty(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 972 | mInboundQueue.push_back(std::move(newEntry)); |
| 973 | EventEntry& entry = *(mInboundQueue.back()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 974 | traceInboundQueueLengthLocked(); |
| 975 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 976 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 977 | case EventEntry::Type::KEY: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 978 | // Optimize app switch latency. |
| 979 | // If the application takes too long to catch up then we drop all events preceding |
| 980 | // the app switch key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 981 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 982 | if (isAppSwitchKeyEvent(keyEntry)) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 983 | if (keyEntry.action == AKEY_EVENT_ACTION_DOWN) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 984 | mAppSwitchSawKeyDown = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 985 | } else if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 986 | if (mAppSwitchSawKeyDown) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 987 | if (DEBUG_APP_SWITCH) { |
| 988 | ALOGD("App switch is pending!"); |
| 989 | } |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 990 | mAppSwitchDueTime = keyEntry.eventTime + APP_SWITCH_TIMEOUT; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 991 | mAppSwitchSawKeyDown = false; |
| 992 | needWake = true; |
| 993 | } |
| 994 | } |
| 995 | } |
| 996 | break; |
| 997 | } |
| 998 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 999 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1000 | if (shouldPruneInboundQueueLocked(static_cast<MotionEntry&>(entry))) { |
| 1001 | mNextUnblockedEvent = mInboundQueue.back(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1002 | needWake = true; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1003 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1004 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1005 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1006 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1007 | LOG_ALWAYS_FATAL("Focus events should be inserted using enqueueFocusEventLocked"); |
| 1008 | break; |
| 1009 | } |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1010 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1011 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1012 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1013 | case EventEntry::Type::SENSOR: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1014 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1015 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1016 | // nothing to do |
| 1017 | break; |
| 1018 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | return needWake; |
| 1022 | } |
| 1023 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1024 | void InputDispatcher::addRecentEventLocked(std::shared_ptr<EventEntry> entry) { |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1025 | // Do not store sensor event in recent queue to avoid flooding the queue. |
| 1026 | if (entry->type != EventEntry::Type::SENSOR) { |
| 1027 | mRecentQueue.push_back(entry); |
| 1028 | } |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1029 | if (mRecentQueue.size() > RECENT_QUEUE_MAX_SIZE) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1030 | mRecentQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1031 | } |
| 1032 | } |
| 1033 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1034 | sp<WindowInfoHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, int32_t x, |
| 1035 | int32_t y, TouchState* touchState, |
| 1036 | bool addOutsideTargets, |
| 1037 | bool ignoreDragWindow) { |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 1038 | if (addOutsideTargets && touchState == nullptr) { |
| 1039 | LOG_ALWAYS_FATAL("Must provide a valid touch state if adding outside targets"); |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1040 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1041 | // Traverse windows from front to back to find touched window. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1042 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 1043 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 1044 | if (ignoreDragWindow && haveSameToken(windowHandle, mDragState->dragWindow)) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1045 | continue; |
| 1046 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1047 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1048 | if (windowInfo->displayId == displayId) { |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1049 | auto flags = windowInfo->flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1050 | |
| 1051 | if (windowInfo->visible) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1052 | if (!flags.test(WindowInfo::Flag::NOT_TOUCHABLE)) { |
| 1053 | bool isTouchModal = !flags.test(WindowInfo::Flag::NOT_FOCUSABLE) && |
| 1054 | !flags.test(WindowInfo::Flag::NOT_TOUCH_MODAL); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1055 | if (isTouchModal || windowInfo->touchableRegionContainsPoint(x, y)) { |
| 1056 | // Found window. |
| 1057 | return windowHandle; |
| 1058 | } |
| 1059 | } |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1060 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1061 | if (addOutsideTargets && flags.test(WindowInfo::Flag::WATCH_OUTSIDE_TOUCH)) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1062 | touchState->addOrUpdateWindow(windowHandle, |
| 1063 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE, |
| 1064 | BitSet32(0)); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1065 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1066 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1067 | } |
| 1068 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1069 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1070 | } |
| 1071 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1072 | void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1073 | const char* reason; |
| 1074 | switch (dropReason) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1075 | case DropReason::POLICY: |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1076 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 1077 | ALOGD("Dropped event because policy consumed it."); |
| 1078 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1079 | reason = "inbound event was dropped because the policy consumed it"; |
| 1080 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1081 | case DropReason::DISABLED: |
| 1082 | if (mLastDropReason != DropReason::DISABLED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1083 | ALOGI("Dropped event because input dispatch is disabled."); |
| 1084 | } |
| 1085 | reason = "inbound event was dropped because input dispatch is disabled"; |
| 1086 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1087 | case DropReason::APP_SWITCH: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1088 | ALOGI("Dropped event because of pending overdue app switch."); |
| 1089 | reason = "inbound event was dropped because of pending overdue app switch"; |
| 1090 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1091 | case DropReason::BLOCKED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1092 | ALOGI("Dropped event because the current application is not responding and the user " |
| 1093 | "has started interacting with a different application."); |
| 1094 | reason = "inbound event was dropped because the current application is not responding " |
| 1095 | "and the user has started interacting with a different application"; |
| 1096 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1097 | case DropReason::STALE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1098 | ALOGI("Dropped event because it is stale."); |
| 1099 | reason = "inbound event was dropped because it is stale"; |
| 1100 | break; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1101 | case DropReason::NO_POINTER_CAPTURE: |
| 1102 | ALOGI("Dropped event because there is no window with Pointer Capture."); |
| 1103 | reason = "inbound event was dropped because there is no window with Pointer Capture"; |
| 1104 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1105 | case DropReason::NOT_DROPPED: { |
| 1106 | LOG_ALWAYS_FATAL("Should not be dropping a NOT_DROPPED event"); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1107 | return; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1108 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1109 | } |
| 1110 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1111 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1112 | case EventEntry::Type::KEY: { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1113 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 1114 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1115 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1116 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1117 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1118 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1119 | if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1120 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, reason); |
| 1121 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1122 | } else { |
| 1123 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 1124 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1125 | } |
| 1126 | break; |
| 1127 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1128 | case EventEntry::Type::SENSOR: { |
| 1129 | break; |
| 1130 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1131 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1132 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1133 | break; |
| 1134 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1135 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1136 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1137 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 1138 | case EventEntry::Type::DEVICE_RESET: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1139 | LOG_ALWAYS_FATAL("Should not drop %s events", ftl::enum_string(entry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1140 | break; |
| 1141 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1142 | } |
| 1143 | } |
| 1144 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1145 | static bool isAppSwitchKeyCode(int32_t keyCode) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1146 | return keyCode == AKEYCODE_HOME || keyCode == AKEYCODE_ENDCALL || |
| 1147 | keyCode == AKEYCODE_APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1148 | } |
| 1149 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1150 | bool InputDispatcher::isAppSwitchKeyEvent(const KeyEntry& keyEntry) { |
| 1151 | return !(keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) && isAppSwitchKeyCode(keyEntry.keyCode) && |
| 1152 | (keyEntry.policyFlags & POLICY_FLAG_TRUSTED) && |
| 1153 | (keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1154 | } |
| 1155 | |
| 1156 | bool InputDispatcher::isAppSwitchPendingLocked() { |
| 1157 | return mAppSwitchDueTime != LONG_LONG_MAX; |
| 1158 | } |
| 1159 | |
| 1160 | void InputDispatcher::resetPendingAppSwitchLocked(bool handled) { |
| 1161 | mAppSwitchDueTime = LONG_LONG_MAX; |
| 1162 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1163 | if (DEBUG_APP_SWITCH) { |
| 1164 | if (handled) { |
| 1165 | ALOGD("App switch has arrived."); |
| 1166 | } else { |
| 1167 | ALOGD("App switch was abandoned."); |
| 1168 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1169 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1170 | } |
| 1171 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1172 | bool InputDispatcher::haveCommandsLocked() const { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1173 | return !mCommandQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1174 | } |
| 1175 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1176 | bool InputDispatcher::runCommandsLockedInterruptable() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1177 | if (mCommandQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1178 | return false; |
| 1179 | } |
| 1180 | |
| 1181 | do { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1182 | auto command = std::move(mCommandQueue.front()); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1183 | mCommandQueue.pop_front(); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1184 | // Commands are run with the lock held, but may release and re-acquire the lock from within. |
| 1185 | command(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1186 | } while (!mCommandQueue.empty()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1187 | return true; |
| 1188 | } |
| 1189 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1190 | void InputDispatcher::postCommandLocked(Command&& command) { |
| 1191 | mCommandQueue.push_back(command); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | void InputDispatcher::drainInboundQueueLocked() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1195 | while (!mInboundQueue.empty()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1196 | std::shared_ptr<EventEntry> entry = mInboundQueue.front(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1197 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1198 | releaseInboundEventLocked(entry); |
| 1199 | } |
| 1200 | traceInboundQueueLengthLocked(); |
| 1201 | } |
| 1202 | |
| 1203 | void InputDispatcher::releasePendingEventLocked() { |
| 1204 | if (mPendingEvent) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1205 | releaseInboundEventLocked(mPendingEvent); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1206 | mPendingEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1207 | } |
| 1208 | } |
| 1209 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1210 | void InputDispatcher::releaseInboundEventLocked(std::shared_ptr<EventEntry> entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1211 | InjectionState* injectionState = entry->injectionState; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1212 | if (injectionState && injectionState->injectionResult == InputEventInjectionResult::PENDING) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1213 | if (DEBUG_DISPATCH_CYCLE) { |
| 1214 | ALOGD("Injected inbound event was dropped."); |
| 1215 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1216 | setInjectionResult(*entry, InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1217 | } |
| 1218 | if (entry == mNextUnblockedEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1219 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1220 | } |
| 1221 | addRecentEventLocked(entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1222 | } |
| 1223 | |
| 1224 | void InputDispatcher::resetKeyRepeatLocked() { |
| 1225 | if (mKeyRepeatState.lastKeyEntry) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1226 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1227 | } |
| 1228 | } |
| 1229 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1230 | std::shared_ptr<KeyEntry> InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) { |
| 1231 | std::shared_ptr<KeyEntry> entry = mKeyRepeatState.lastKeyEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1232 | |
Michael Wright | 2e73295 | 2014-09-24 13:26:59 -0700 | [diff] [blame] | 1233 | uint32_t policyFlags = entry->policyFlags & |
| 1234 | (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1235 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1236 | std::shared_ptr<KeyEntry> newEntry = |
| 1237 | std::make_unique<KeyEntry>(mIdGenerator.nextId(), currentTime, entry->deviceId, |
| 1238 | entry->source, entry->displayId, policyFlags, entry->action, |
| 1239 | entry->flags, entry->keyCode, entry->scanCode, |
| 1240 | entry->metaState, entry->repeatCount + 1, entry->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1241 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1242 | newEntry->syntheticRepeat = true; |
| 1243 | mKeyRepeatState.lastKeyEntry = newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1244 | mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1245 | return newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1246 | } |
| 1247 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1248 | bool InputDispatcher::dispatchConfigurationChangedLocked(nsecs_t currentTime, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1249 | const ConfigurationChangedEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1250 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1251 | ALOGD("dispatchConfigurationChanged - eventTime=%" PRId64, entry.eventTime); |
| 1252 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1253 | |
| 1254 | // Reset key repeating in case a keyboard device was added or removed or something. |
| 1255 | resetKeyRepeatLocked(); |
| 1256 | |
| 1257 | // Enqueue a command to run outside the lock to tell the policy that the configuration changed. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1258 | auto command = [this, eventTime = entry.eventTime]() REQUIRES(mLock) { |
| 1259 | scoped_unlock unlock(mLock); |
| 1260 | mPolicy->notifyConfigurationChanged(eventTime); |
| 1261 | }; |
| 1262 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1263 | return true; |
| 1264 | } |
| 1265 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1266 | bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime, |
| 1267 | const DeviceResetEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1268 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1269 | ALOGD("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry.eventTime, |
| 1270 | entry.deviceId); |
| 1271 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1272 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 1273 | // Reset key repeating in case a keyboard device was disabled or enabled. |
| 1274 | if (mKeyRepeatState.lastKeyEntry && mKeyRepeatState.lastKeyEntry->deviceId == entry.deviceId) { |
| 1275 | resetKeyRepeatLocked(); |
| 1276 | } |
| 1277 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1278 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, "device was reset"); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1279 | options.deviceId = entry.deviceId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1280 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1281 | return true; |
| 1282 | } |
| 1283 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1284 | void InputDispatcher::enqueueFocusEventLocked(const sp<IBinder>& windowToken, bool hasFocus, |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1285 | const std::string& reason) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1286 | if (mPendingEvent != nullptr) { |
| 1287 | // Move the pending event to the front of the queue. This will give the chance |
| 1288 | // for the pending event to get dispatched to the newly focused window |
| 1289 | mInboundQueue.push_front(mPendingEvent); |
| 1290 | mPendingEvent = nullptr; |
| 1291 | } |
| 1292 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1293 | std::unique_ptr<FocusEntry> focusEntry = |
| 1294 | std::make_unique<FocusEntry>(mIdGenerator.nextId(), now(), windowToken, hasFocus, |
| 1295 | reason); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1296 | |
| 1297 | // This event should go to the front of the queue, but behind all other focus events |
| 1298 | // Find the last focus event, and insert right after it |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1299 | std::deque<std::shared_ptr<EventEntry>>::reverse_iterator it = |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1300 | std::find_if(mInboundQueue.rbegin(), mInboundQueue.rend(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1301 | [](const std::shared_ptr<EventEntry>& event) { |
| 1302 | return event->type == EventEntry::Type::FOCUS; |
| 1303 | }); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1304 | |
| 1305 | // 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] | 1306 | mInboundQueue.insert(it.base(), std::move(focusEntry)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1307 | } |
| 1308 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1309 | void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime, std::shared_ptr<FocusEntry> entry) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 1310 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1311 | if (channel == nullptr) { |
| 1312 | return; // Window has gone away |
| 1313 | } |
| 1314 | InputTarget target; |
| 1315 | target.inputChannel = channel; |
| 1316 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1317 | entry->dispatchInProgress = true; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1318 | std::string message = std::string("Focus ") + (entry->hasFocus ? "entering " : "leaving ") + |
| 1319 | channel->getName(); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 1320 | std::string reason = std::string("reason=").append(entry->reason); |
| 1321 | android_log_event_list(LOGTAG_INPUT_FOCUS) << message << reason << LOG_ID_EVENTS; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1322 | dispatchEventLocked(currentTime, entry, {target}); |
| 1323 | } |
| 1324 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1325 | void InputDispatcher::dispatchPointerCaptureChangedLocked( |
| 1326 | nsecs_t currentTime, const std::shared_ptr<PointerCaptureChangedEntry>& entry, |
| 1327 | DropReason& dropReason) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1328 | dropReason = DropReason::NOT_DROPPED; |
| 1329 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1330 | const bool haveWindowWithPointerCapture = mWindowTokenWithPointerCapture != nullptr; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1331 | sp<IBinder> token; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1332 | |
| 1333 | if (entry->pointerCaptureRequest.enable) { |
| 1334 | // Enable Pointer Capture. |
| 1335 | if (haveWindowWithPointerCapture && |
| 1336 | (entry->pointerCaptureRequest == mCurrentPointerCaptureRequest)) { |
| 1337 | LOG_ALWAYS_FATAL("This request to enable Pointer Capture has already been dispatched " |
| 1338 | "to the window."); |
| 1339 | } |
| 1340 | if (!mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1341 | // This can happen if a window requests capture and immediately releases capture. |
| 1342 | ALOGW("No window requested Pointer Capture."); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1343 | dropReason = DropReason::NO_POINTER_CAPTURE; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1344 | return; |
| 1345 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1346 | if (entry->pointerCaptureRequest.seq != mCurrentPointerCaptureRequest.seq) { |
| 1347 | ALOGI("Skipping dispatch of Pointer Capture being enabled: sequence number mismatch."); |
| 1348 | return; |
| 1349 | } |
| 1350 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1351 | token = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1352 | LOG_ALWAYS_FATAL_IF(!token, "Cannot find focused window for Pointer Capture."); |
| 1353 | mWindowTokenWithPointerCapture = token; |
| 1354 | } else { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1355 | // Disable Pointer Capture. |
| 1356 | // We do not check if the sequence number matches for requests to disable Pointer Capture |
| 1357 | // for two reasons: |
| 1358 | // 1. Pointer Capture can be disabled by a focus change, which means we can get two entries |
| 1359 | // to disable capture with the same sequence number: one generated by |
| 1360 | // disablePointerCaptureForcedLocked() and another as an acknowledgement of Pointer |
| 1361 | // Capture being disabled in InputReader. |
| 1362 | // 2. We respect any request to disable Pointer Capture generated by InputReader, since the |
| 1363 | // actual Pointer Capture state that affects events being generated by input devices is |
| 1364 | // in InputReader. |
| 1365 | if (!haveWindowWithPointerCapture) { |
| 1366 | // Pointer capture was already forcefully disabled because of focus change. |
| 1367 | dropReason = DropReason::NOT_DROPPED; |
| 1368 | return; |
| 1369 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1370 | token = mWindowTokenWithPointerCapture; |
| 1371 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1372 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1373 | setPointerCaptureLocked(false); |
| 1374 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1375 | } |
| 1376 | |
| 1377 | auto channel = getInputChannelLocked(token); |
| 1378 | if (channel == nullptr) { |
| 1379 | // Window has gone away, clean up Pointer Capture state. |
| 1380 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1381 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1382 | setPointerCaptureLocked(false); |
| 1383 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1384 | return; |
| 1385 | } |
| 1386 | InputTarget target; |
| 1387 | target.inputChannel = channel; |
| 1388 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1389 | entry->dispatchInProgress = true; |
| 1390 | dispatchEventLocked(currentTime, entry, {target}); |
| 1391 | |
| 1392 | dropReason = DropReason::NOT_DROPPED; |
| 1393 | } |
| 1394 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1395 | void InputDispatcher::dispatchTouchModeChangeLocked(nsecs_t currentTime, |
| 1396 | const std::shared_ptr<TouchModeEntry>& entry) { |
| 1397 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
| 1398 | getWindowHandlesLocked(mFocusedDisplayId); |
| 1399 | if (windowHandles.empty()) { |
| 1400 | return; |
| 1401 | } |
| 1402 | const std::vector<InputTarget> inputTargets = |
| 1403 | getInputTargetsFromWindowHandlesLocked(windowHandles); |
| 1404 | if (inputTargets.empty()) { |
| 1405 | return; |
| 1406 | } |
| 1407 | entry->dispatchInProgress = true; |
| 1408 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1409 | } |
| 1410 | |
| 1411 | std::vector<InputTarget> InputDispatcher::getInputTargetsFromWindowHandlesLocked( |
| 1412 | const std::vector<sp<WindowInfoHandle>>& windowHandles) const { |
| 1413 | std::vector<InputTarget> inputTargets; |
| 1414 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
| 1415 | // TODO(b/193718270): Due to performance concerns, consider notifying visible windows only. |
| 1416 | const sp<IBinder>& token = handle->getToken(); |
| 1417 | if (token == nullptr) { |
| 1418 | continue; |
| 1419 | } |
| 1420 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(token); |
| 1421 | if (channel == nullptr) { |
| 1422 | continue; // Window has gone away |
| 1423 | } |
| 1424 | InputTarget target; |
| 1425 | target.inputChannel = channel; |
| 1426 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1427 | inputTargets.push_back(target); |
| 1428 | } |
| 1429 | return inputTargets; |
| 1430 | } |
| 1431 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1432 | bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, std::shared_ptr<KeyEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1433 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1434 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1435 | if (!entry->dispatchInProgress) { |
| 1436 | if (entry->repeatCount == 0 && entry->action == AKEY_EVENT_ACTION_DOWN && |
| 1437 | (entry->policyFlags & POLICY_FLAG_TRUSTED) && |
| 1438 | (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { |
| 1439 | if (mKeyRepeatState.lastKeyEntry && |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1440 | mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode && |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1441 | // We have seen two identical key downs in a row which indicates that the device |
| 1442 | // driver is automatically generating key repeats itself. We take note of the |
| 1443 | // repeat here, but we disable our own next key repeat timer since it is clear that |
| 1444 | // we will not need to synthesize key repeats ourselves. |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1445 | mKeyRepeatState.lastKeyEntry->deviceId == entry->deviceId) { |
| 1446 | // Make sure we don't get key down from a different device. If a different |
| 1447 | // device Id has same key pressed down, the new device Id will replace the |
| 1448 | // current one to hold the key repeat with repeat count reset. |
| 1449 | // In the future when got a KEY_UP on the device id, drop it and do not |
| 1450 | // stop the key repeat on current device. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1451 | entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; |
| 1452 | resetKeyRepeatLocked(); |
| 1453 | mKeyRepeatState.nextRepeatTime = LONG_LONG_MAX; // don't generate repeats ourselves |
| 1454 | } else { |
| 1455 | // Not a repeat. Save key down state in case we do see a repeat later. |
| 1456 | resetKeyRepeatLocked(); |
| 1457 | mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; |
| 1458 | } |
| 1459 | mKeyRepeatState.lastKeyEntry = entry; |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1460 | } else if (entry->action == AKEY_EVENT_ACTION_UP && mKeyRepeatState.lastKeyEntry && |
| 1461 | mKeyRepeatState.lastKeyEntry->deviceId != entry->deviceId) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1462 | // The key on device 'deviceId' is still down, do not stop key repeat |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1463 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 1464 | ALOGD("deviceId=%d got KEY_UP as stale", entry->deviceId); |
| 1465 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1466 | } else if (!entry->syntheticRepeat) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1467 | resetKeyRepeatLocked(); |
| 1468 | } |
| 1469 | |
| 1470 | if (entry->repeatCount == 1) { |
| 1471 | entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; |
| 1472 | } else { |
| 1473 | entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; |
| 1474 | } |
| 1475 | |
| 1476 | entry->dispatchInProgress = true; |
| 1477 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1478 | logOutboundKeyDetails("dispatchKey - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1479 | } |
| 1480 | |
| 1481 | // Handle case where the policy asked us to try again later last time. |
| 1482 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER) { |
| 1483 | if (currentTime < entry->interceptKeyWakeupTime) { |
| 1484 | if (entry->interceptKeyWakeupTime < *nextWakeupTime) { |
| 1485 | *nextWakeupTime = entry->interceptKeyWakeupTime; |
| 1486 | } |
| 1487 | return false; // wait until next wakeup |
| 1488 | } |
| 1489 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; |
| 1490 | entry->interceptKeyWakeupTime = 0; |
| 1491 | } |
| 1492 | |
| 1493 | // Give the policy a chance to intercept the key. |
| 1494 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN) { |
| 1495 | if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1496 | sp<IBinder> focusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1497 | mFocusResolver.getFocusedWindowToken(getTargetDisplayId(*entry)); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1498 | |
| 1499 | auto command = [this, focusedWindowToken, entry]() REQUIRES(mLock) { |
| 1500 | doInterceptKeyBeforeDispatchingCommand(focusedWindowToken, *entry); |
| 1501 | }; |
| 1502 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1503 | return false; // wait for the command to run |
| 1504 | } else { |
| 1505 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
| 1506 | } |
| 1507 | } else if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_SKIP) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1508 | if (*dropReason == DropReason::NOT_DROPPED) { |
| 1509 | *dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1510 | } |
| 1511 | } |
| 1512 | |
| 1513 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1514 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1515 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1516 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1517 | : InputEventInjectionResult::FAILED); |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1518 | mReporter->reportDroppedKey(entry->id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1519 | return true; |
| 1520 | } |
| 1521 | |
| 1522 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1523 | std::vector<InputTarget> inputTargets; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1524 | InputEventInjectionResult injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1525 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1526 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1527 | return false; |
| 1528 | } |
| 1529 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1530 | setInjectionResult(*entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1531 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1532 | return true; |
| 1533 | } |
| 1534 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1535 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1536 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1537 | |
| 1538 | // Dispatch the key. |
| 1539 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1540 | return true; |
| 1541 | } |
| 1542 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1543 | void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1544 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1545 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 ", " |
| 1546 | "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, " |
| 1547 | "metaState=0x%x, repeatCount=%d, downTime=%" PRId64, |
| 1548 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, |
| 1549 | entry.policyFlags, entry.action, entry.flags, entry.keyCode, entry.scanCode, |
| 1550 | entry.metaState, entry.repeatCount, entry.downTime); |
| 1551 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1552 | } |
| 1553 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1554 | void InputDispatcher::dispatchSensorLocked(nsecs_t currentTime, |
| 1555 | const std::shared_ptr<SensorEntry>& entry, |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1556 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1557 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1558 | ALOGD("notifySensorEvent eventTime=%" PRId64 ", hwTimestamp=%" PRId64 ", deviceId=%d, " |
| 1559 | "source=0x%x, sensorType=%s", |
| 1560 | entry->eventTime, entry->hwTimestamp, entry->deviceId, entry->source, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1561 | ftl::enum_string(entry->sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1562 | } |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1563 | auto command = [this, entry]() REQUIRES(mLock) { |
| 1564 | scoped_unlock unlock(mLock); |
| 1565 | |
| 1566 | if (entry->accuracyChanged) { |
| 1567 | mPolicy->notifySensorAccuracy(entry->deviceId, entry->sensorType, entry->accuracy); |
| 1568 | } |
| 1569 | mPolicy->notifySensorEvent(entry->deviceId, entry->sensorType, entry->accuracy, |
| 1570 | entry->hwTimestamp, entry->values); |
| 1571 | }; |
| 1572 | postCommandLocked(std::move(command)); |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1573 | } |
| 1574 | |
| 1575 | bool InputDispatcher::flushSensor(int deviceId, InputDeviceSensorType sensorType) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1576 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1577 | ALOGD("flushSensor deviceId=%d, sensorType=%s", deviceId, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1578 | ftl::enum_string(sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1579 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1580 | { // acquire lock |
| 1581 | std::scoped_lock _l(mLock); |
| 1582 | |
| 1583 | for (auto it = mInboundQueue.begin(); it != mInboundQueue.end(); it++) { |
| 1584 | std::shared_ptr<EventEntry> entry = *it; |
| 1585 | if (entry->type == EventEntry::Type::SENSOR) { |
| 1586 | it = mInboundQueue.erase(it); |
| 1587 | releaseInboundEventLocked(entry); |
| 1588 | } |
| 1589 | } |
| 1590 | } |
| 1591 | return true; |
| 1592 | } |
| 1593 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1594 | bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, std::shared_ptr<MotionEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1595 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1596 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1597 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1598 | if (!entry->dispatchInProgress) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1599 | entry->dispatchInProgress = true; |
| 1600 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1601 | logOutboundMotionDetails("dispatchMotion - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1602 | } |
| 1603 | |
| 1604 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1605 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1606 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1607 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1608 | : InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1609 | return true; |
| 1610 | } |
| 1611 | |
| 1612 | bool isPointerEvent = entry->source & AINPUT_SOURCE_CLASS_POINTER; |
| 1613 | |
| 1614 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1615 | std::vector<InputTarget> inputTargets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1616 | |
| 1617 | bool conflictingPointerActions = false; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1618 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1619 | if (isPointerEvent) { |
| 1620 | // Pointer event. (eg. touchscreen) |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1621 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1622 | findTouchedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1623 | &conflictingPointerActions); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1624 | } else { |
| 1625 | // Non touch event. (eg. trackball) |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1626 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1627 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1628 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1629 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1630 | return false; |
| 1631 | } |
| 1632 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1633 | setInjectionResult(*entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1634 | if (injectionResult == InputEventInjectionResult::PERMISSION_DENIED) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1635 | ALOGW("Permission denied, dropping the motion (isPointer=%s)", toString(isPointerEvent)); |
| 1636 | return true; |
| 1637 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1638 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1639 | CancelationOptions::Mode mode(isPointerEvent |
| 1640 | ? CancelationOptions::CANCEL_POINTER_EVENTS |
| 1641 | : CancelationOptions::CANCEL_NON_POINTER_EVENTS); |
| 1642 | CancelationOptions options(mode, "input event injection failed"); |
| 1643 | synthesizeCancelationEventsForMonitorsLocked(options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1644 | return true; |
| 1645 | } |
| 1646 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1647 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1648 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1649 | |
| 1650 | // Dispatch the motion. |
| 1651 | if (conflictingPointerActions) { |
| 1652 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1653 | "conflicting pointer actions"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1654 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1655 | } |
| 1656 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1657 | return true; |
| 1658 | } |
| 1659 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1660 | void InputDispatcher::enqueueDragEventLocked(const sp<WindowInfoHandle>& windowHandle, |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1661 | bool isExiting, const MotionEntry& motionEntry) { |
| 1662 | // If the window needs enqueue a drag event, the pointerCount should be 1 and the action should |
| 1663 | // be AMOTION_EVENT_ACTION_MOVE, that could guarantee the first pointer is always valid. |
| 1664 | LOG_ALWAYS_FATAL_IF(motionEntry.pointerCount != 1); |
| 1665 | PointerCoords pointerCoords; |
| 1666 | pointerCoords.copyFrom(motionEntry.pointerCoords[0]); |
| 1667 | pointerCoords.transform(windowHandle->getInfo()->transform); |
| 1668 | |
| 1669 | std::unique_ptr<DragEntry> dragEntry = |
| 1670 | std::make_unique<DragEntry>(mIdGenerator.nextId(), motionEntry.eventTime, |
| 1671 | windowHandle->getToken(), isExiting, pointerCoords.getX(), |
| 1672 | pointerCoords.getY()); |
| 1673 | |
| 1674 | enqueueInboundEventLocked(std::move(dragEntry)); |
| 1675 | } |
| 1676 | |
| 1677 | void InputDispatcher::dispatchDragLocked(nsecs_t currentTime, std::shared_ptr<DragEntry> entry) { |
| 1678 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
| 1679 | if (channel == nullptr) { |
| 1680 | return; // Window has gone away |
| 1681 | } |
| 1682 | InputTarget target; |
| 1683 | target.inputChannel = channel; |
| 1684 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1685 | entry->dispatchInProgress = true; |
| 1686 | dispatchEventLocked(currentTime, entry, {target}); |
| 1687 | } |
| 1688 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1689 | void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1690 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1691 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
| 1692 | ", policyFlags=0x%x, " |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1693 | "action=%s, actionButton=0x%x, flags=0x%x, " |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1694 | "metaState=0x%x, buttonState=0x%x," |
| 1695 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64, |
| 1696 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1697 | entry.policyFlags, MotionEvent::actionToString(entry.action).c_str(), |
| 1698 | entry.actionButton, entry.flags, entry.metaState, entry.buttonState, entry.edgeFlags, |
| 1699 | entry.xPrecision, entry.yPrecision, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1700 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1701 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
| 1702 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
| 1703 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 1704 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 1705 | "orientation=%f", |
| 1706 | i, entry.pointerProperties[i].id, entry.pointerProperties[i].toolType, |
| 1707 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 1708 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 1709 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 1710 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 1711 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 1712 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 1713 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 1714 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 1715 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| 1716 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1717 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1718 | } |
| 1719 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1720 | void InputDispatcher::dispatchEventLocked(nsecs_t currentTime, |
| 1721 | std::shared_ptr<EventEntry> eventEntry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1722 | const std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1723 | ATRACE_CALL(); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1724 | if (DEBUG_DISPATCH_CYCLE) { |
| 1725 | ALOGD("dispatchEventToCurrentInputTargets"); |
| 1726 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1727 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1728 | updateInteractionTokensLocked(*eventEntry, inputTargets); |
| 1729 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1730 | ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true |
| 1731 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1732 | pokeUserActivityLocked(*eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1733 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1734 | for (const InputTarget& inputTarget : inputTargets) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 1735 | sp<Connection> connection = |
| 1736 | getConnectionLocked(inputTarget.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 1737 | if (connection != nullptr) { |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 1738 | prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1739 | } else { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1740 | if (DEBUG_FOCUS) { |
| 1741 | ALOGD("Dropping event delivery to target with channel '%s' because it " |
| 1742 | "is no longer registered with the input dispatcher.", |
| 1743 | inputTarget.inputChannel->getName().c_str()); |
| 1744 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1745 | } |
| 1746 | } |
| 1747 | } |
| 1748 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1749 | void InputDispatcher::cancelEventsForAnrLocked(const sp<Connection>& connection) { |
| 1750 | // We will not be breaking any connections here, even if the policy wants us to abort dispatch. |
| 1751 | // If the policy decides to close the app, we will get a channel removal event via |
| 1752 | // unregisterInputChannel, and will clean up the connection that way. We are already not |
| 1753 | // sending new pointers to the connection when it blocked, but focused events will continue to |
| 1754 | // pile up. |
| 1755 | ALOGW("Canceling events for %s because it is unresponsive", |
| 1756 | connection->inputChannel->getName().c_str()); |
| 1757 | if (connection->status == Connection::STATUS_NORMAL) { |
| 1758 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, |
| 1759 | "application not responding"); |
| 1760 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1761 | } |
| 1762 | } |
| 1763 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1764 | void InputDispatcher::resetNoFocusedWindowTimeoutLocked() { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1765 | if (DEBUG_FOCUS) { |
| 1766 | ALOGD("Resetting ANR timeouts."); |
| 1767 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1768 | |
| 1769 | // Reset input target wait timeout. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1770 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1771 | mAwaitedFocusedApplication.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1772 | } |
| 1773 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1774 | /** |
| 1775 | * Get the display id that the given event should go to. If this event specifies a valid display id, |
| 1776 | * then it should be dispatched to that display. Otherwise, the event goes to the focused display. |
| 1777 | * Focused display is the display that the user most recently interacted with. |
| 1778 | */ |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1779 | int32_t InputDispatcher::getTargetDisplayId(const EventEntry& entry) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1780 | int32_t displayId; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1781 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1782 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1783 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 1784 | displayId = keyEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1785 | break; |
| 1786 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1787 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1788 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1789 | displayId = motionEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1790 | break; |
| 1791 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1792 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1793 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1794 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1795 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1796 | case EventEntry::Type::DEVICE_RESET: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1797 | case EventEntry::Type::SENSOR: |
| 1798 | case EventEntry::Type::DRAG: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1799 | ALOGE("%s events do not have a target display", ftl::enum_string(entry.type).c_str()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1800 | return ADISPLAY_ID_NONE; |
| 1801 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1802 | } |
| 1803 | return displayId == ADISPLAY_ID_NONE ? mFocusedDisplayId : displayId; |
| 1804 | } |
| 1805 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1806 | bool InputDispatcher::shouldWaitToSendKeyLocked(nsecs_t currentTime, |
| 1807 | const char* focusedWindowName) { |
| 1808 | if (mAnrTracker.empty()) { |
| 1809 | // already processed all events that we waited for |
| 1810 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1811 | return false; |
| 1812 | } |
| 1813 | |
| 1814 | if (!mKeyIsWaitingForEventsTimeout.has_value()) { |
| 1815 | // Start the timer |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 1816 | // 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] | 1817 | mKeyIsWaitingForEventsTimeout = currentTime + |
| 1818 | std::chrono::duration_cast<std::chrono::nanoseconds>(KEY_WAITING_FOR_EVENTS_TIMEOUT) |
| 1819 | .count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1820 | return true; |
| 1821 | } |
| 1822 | |
| 1823 | // We still have pending events, and already started the timer |
| 1824 | if (currentTime < *mKeyIsWaitingForEventsTimeout) { |
| 1825 | return true; // Still waiting |
| 1826 | } |
| 1827 | |
| 1828 | // Waited too long, and some connection still hasn't processed all motions |
| 1829 | // Just send the key to the focused window |
| 1830 | ALOGW("Dispatching key to %s even though there are other unprocessed events", |
| 1831 | focusedWindowName); |
| 1832 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1833 | return false; |
| 1834 | } |
| 1835 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1836 | InputEventInjectionResult InputDispatcher::findFocusedWindowTargetsLocked( |
| 1837 | nsecs_t currentTime, const EventEntry& entry, std::vector<InputTarget>& inputTargets, |
| 1838 | nsecs_t* nextWakeupTime) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1839 | std::string reason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1840 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1841 | int32_t displayId = getTargetDisplayId(entry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1842 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1843 | std::shared_ptr<InputApplicationHandle> focusedApplicationHandle = |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1844 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 1845 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1846 | // If there is no currently focused window and no focused application |
| 1847 | // then drop the event. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1848 | if (focusedWindowHandle == nullptr && focusedApplicationHandle == nullptr) { |
| 1849 | ALOGI("Dropping %s event because there is no focused window or focused application in " |
| 1850 | "display %" PRId32 ".", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1851 | ftl::enum_string(entry.type).c_str(), displayId); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1852 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1853 | } |
| 1854 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 1855 | // Drop key events if requested by input feature |
| 1856 | if (focusedWindowHandle != nullptr && shouldDropInput(entry, focusedWindowHandle)) { |
| 1857 | return InputEventInjectionResult::FAILED; |
| 1858 | } |
| 1859 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1860 | // Compatibility behavior: raise ANR if there is a focused application, but no focused window. |
| 1861 | // Only start counting when we have a focused event to dispatch. The ANR is canceled if we |
| 1862 | // start interacting with another application via touch (app switch). This code can be removed |
| 1863 | // if the "no focused window ANR" is moved to the policy. Input doesn't know whether |
| 1864 | // an app is expected to have a focused window. |
| 1865 | if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) { |
| 1866 | if (!mNoFocusedWindowTimeoutTime.has_value()) { |
| 1867 | // 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] | 1868 | std::chrono::nanoseconds timeout = focusedApplicationHandle->getDispatchingTimeout( |
| 1869 | DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
| 1870 | mNoFocusedWindowTimeoutTime = currentTime + timeout.count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1871 | mAwaitedFocusedApplication = focusedApplicationHandle; |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 1872 | mAwaitedApplicationDisplayId = displayId; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1873 | ALOGW("Waiting because no window has focus but %s may eventually add a " |
| 1874 | "window when it finishes starting up. Will wait for %" PRId64 "ms", |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1875 | mAwaitedFocusedApplication->getName().c_str(), millis(timeout)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1876 | *nextWakeupTime = *mNoFocusedWindowTimeoutTime; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1877 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1878 | } else if (currentTime > *mNoFocusedWindowTimeoutTime) { |
| 1879 | // Already raised ANR. Drop the event |
| 1880 | ALOGE("Dropping %s event because there is no focused window", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1881 | ftl::enum_string(entry.type).c_str()); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1882 | return InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1883 | } else { |
| 1884 | // Still waiting for the focused window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1885 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1886 | } |
| 1887 | } |
| 1888 | |
| 1889 | // we have a valid, non-null focused window |
| 1890 | resetNoFocusedWindowTimeoutLocked(); |
| 1891 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1892 | // Check permissions. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1893 | if (!checkInjectionPermission(focusedWindowHandle, entry.injectionState)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1894 | return InputEventInjectionResult::PERMISSION_DENIED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1895 | } |
| 1896 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1897 | if (focusedWindowHandle->getInfo()->paused) { |
| 1898 | ALOGI("Waiting because %s is paused", focusedWindowHandle->getName().c_str()); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1899 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1900 | } |
| 1901 | |
| 1902 | // If the event is a key event, then we must wait for all previous events to |
| 1903 | // complete before delivering it because previous events may have the |
| 1904 | // side-effect of transferring focus to a different window and we want to |
| 1905 | // ensure that the following keys are sent to the new window. |
| 1906 | // |
| 1907 | // Suppose the user touches a button in a window then immediately presses "A". |
| 1908 | // If the button causes a pop-up window to appear then we want to ensure that |
| 1909 | // the "A" key is delivered to the new pop-up window. This is because users |
| 1910 | // often anticipate pending UI changes when typing on a keyboard. |
| 1911 | // To obtain this behavior, we must serialize key events with respect to all |
| 1912 | // prior input events. |
| 1913 | if (entry.type == EventEntry::Type::KEY) { |
| 1914 | if (shouldWaitToSendKeyLocked(currentTime, focusedWindowHandle->getName().c_str())) { |
| 1915 | *nextWakeupTime = *mKeyIsWaitingForEventsTimeout; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1916 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1917 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1918 | } |
| 1919 | |
| 1920 | // Success! Output targets. |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1921 | addWindowTargetLocked(focusedWindowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1922 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS, |
| 1923 | BitSet32(0), inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1924 | |
| 1925 | // Done. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1926 | return InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1927 | } |
| 1928 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1929 | /** |
| 1930 | * Given a list of monitors, remove the ones we cannot find a connection for, and the ones |
| 1931 | * that are currently unresponsive. |
| 1932 | */ |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 1933 | std::vector<Monitor> InputDispatcher::selectResponsiveMonitorsLocked( |
| 1934 | const std::vector<Monitor>& monitors) const { |
| 1935 | std::vector<Monitor> responsiveMonitors; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1936 | std::copy_if(monitors.begin(), monitors.end(), std::back_inserter(responsiveMonitors), |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 1937 | [this](const Monitor& monitor) REQUIRES(mLock) { |
| 1938 | sp<Connection> connection = |
| 1939 | getConnectionLocked(monitor.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1940 | if (connection == nullptr) { |
| 1941 | ALOGE("Could not find connection for monitor %s", |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 1942 | monitor.inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1943 | return false; |
| 1944 | } |
| 1945 | if (!connection->responsive) { |
| 1946 | ALOGW("Unresponsive monitor %s will not get the new gesture", |
| 1947 | connection->inputChannel->getName().c_str()); |
| 1948 | return false; |
| 1949 | } |
| 1950 | return true; |
| 1951 | }); |
| 1952 | return responsiveMonitors; |
| 1953 | } |
| 1954 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1955 | InputEventInjectionResult InputDispatcher::findTouchedWindowTargetsLocked( |
| 1956 | nsecs_t currentTime, const MotionEntry& entry, std::vector<InputTarget>& inputTargets, |
| 1957 | nsecs_t* nextWakeupTime, bool* outConflictingPointerActions) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1958 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1959 | enum InjectionPermission { |
| 1960 | INJECTION_PERMISSION_UNKNOWN, |
| 1961 | INJECTION_PERMISSION_GRANTED, |
| 1962 | INJECTION_PERMISSION_DENIED |
| 1963 | }; |
| 1964 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1965 | // For security reasons, we defer updating the touch state until we are sure that |
| 1966 | // event injection will be allowed. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1967 | int32_t displayId = entry.displayId; |
| 1968 | int32_t action = entry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1969 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
| 1970 | |
| 1971 | // 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] | 1972 | InputEventInjectionResult injectionResult = InputEventInjectionResult::PENDING; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1973 | InjectionPermission injectionPermission = INJECTION_PERMISSION_UNKNOWN; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1974 | sp<WindowInfoHandle> newHoverWindowHandle(mLastHoverWindowHandle); |
| 1975 | sp<WindowInfoHandle> newTouchedWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1976 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1977 | // Copy current touch state into tempTouchState. |
| 1978 | // This state will be used to update mTouchStatesByDisplay at the end of this function. |
| 1979 | // 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] | 1980 | const TouchState* oldState = nullptr; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1981 | TouchState tempTouchState; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 1982 | std::unordered_map<int32_t, TouchState>::iterator oldStateIt = |
| 1983 | mTouchStatesByDisplay.find(displayId); |
| 1984 | if (oldStateIt != mTouchStatesByDisplay.end()) { |
| 1985 | oldState = &(oldStateIt->second); |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1986 | tempTouchState.copyFrom(*oldState); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1987 | } |
| 1988 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1989 | bool isSplit = tempTouchState.split; |
| 1990 | bool switchedDevice = tempTouchState.deviceId >= 0 && tempTouchState.displayId >= 0 && |
| 1991 | (tempTouchState.deviceId != entry.deviceId || tempTouchState.source != entry.source || |
| 1992 | tempTouchState.displayId != displayId); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1993 | bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE || |
| 1994 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 1995 | maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); |
| 1996 | bool newGesture = (maskedAction == AMOTION_EVENT_ACTION_DOWN || |
| 1997 | maskedAction == AMOTION_EVENT_ACTION_SCROLL || isHoverAction); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1998 | const bool isFromMouse = entry.source == AINPUT_SOURCE_MOUSE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1999 | bool wrongDevice = false; |
| 2000 | if (newGesture) { |
| 2001 | bool down = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2002 | if (switchedDevice && tempTouchState.down && !down && !isHoverAction) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2003 | ALOGI("Dropping event because a pointer for a different device is already down " |
| 2004 | "in display %" PRId32, |
| 2005 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2006 | // TODO: test multiple simultaneous input streams. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2007 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2008 | switchedDevice = false; |
| 2009 | wrongDevice = true; |
| 2010 | goto Failed; |
| 2011 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2012 | tempTouchState.reset(); |
| 2013 | tempTouchState.down = down; |
| 2014 | tempTouchState.deviceId = entry.deviceId; |
| 2015 | tempTouchState.source = entry.source; |
| 2016 | tempTouchState.displayId = displayId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2017 | isSplit = false; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2018 | } else if (switchedDevice && maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2019 | ALOGI("Dropping move event because a pointer for a different device is already active " |
| 2020 | "in display %" PRId32, |
| 2021 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2022 | // TODO: test multiple simultaneous input streams. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2023 | injectionResult = InputEventInjectionResult::PERMISSION_DENIED; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2024 | switchedDevice = false; |
| 2025 | wrongDevice = true; |
| 2026 | goto Failed; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2027 | } |
| 2028 | |
| 2029 | if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { |
| 2030 | /* Case 1: New splittable pointer going down, or need target for hover or scroll. */ |
| 2031 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2032 | int32_t x; |
| 2033 | int32_t y; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2034 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2035 | // Always dispatch mouse events to cursor position. |
| 2036 | if (isFromMouse) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2037 | x = int32_t(entry.xCursorPosition); |
| 2038 | y = int32_t(entry.yCursorPosition); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2039 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2040 | x = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2041 | y = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2042 | } |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2043 | const bool isDown = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 2044 | newTouchedWindowHandle = findTouchedWindowAtLocked(displayId, x, y, &tempTouchState, |
| 2045 | isDown /*addOutsideTargets*/); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2046 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2047 | // Figure out whether splitting will be allowed for this window. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2048 | if (newTouchedWindowHandle != nullptr && |
| 2049 | newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
Garfield Tan | addb02b | 2019-06-25 16:36:13 -0700 | [diff] [blame] | 2050 | // New window supports splitting, but we should never split mouse events. |
| 2051 | isSplit = !isFromMouse; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2052 | } else if (isSplit) { |
| 2053 | // New window does not support splitting but we have already split events. |
| 2054 | // Ignore the new window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2055 | newTouchedWindowHandle = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2056 | } |
| 2057 | |
| 2058 | // Handle the case where we did not find a window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2059 | if (newTouchedWindowHandle == nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2060 | // 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] | 2061 | newTouchedWindowHandle = tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2062 | } |
| 2063 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2064 | if (newTouchedWindowHandle != nullptr && newTouchedWindowHandle->getInfo()->paused) { |
| 2065 | ALOGI("Not sending touch event to %s because it is paused", |
| 2066 | newTouchedWindowHandle->getName().c_str()); |
| 2067 | newTouchedWindowHandle = nullptr; |
| 2068 | } |
| 2069 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 2070 | // Ensure the window has a connection and the connection is responsive |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2071 | if (newTouchedWindowHandle != nullptr) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 2072 | const bool isResponsive = hasResponsiveConnectionLocked(*newTouchedWindowHandle); |
| 2073 | if (!isResponsive) { |
| 2074 | ALOGW("%s will not receive the new gesture at %" PRIu64, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2075 | newTouchedWindowHandle->getName().c_str(), entry.eventTime); |
| 2076 | newTouchedWindowHandle = nullptr; |
| 2077 | } |
| 2078 | } |
| 2079 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2080 | // Drop events that can't be trusted due to occlusion |
| 2081 | if (newTouchedWindowHandle != nullptr && |
| 2082 | mBlockUntrustedTouchesMode != BlockUntrustedTouchesMode::DISABLED) { |
| 2083 | TouchOcclusionInfo occlusionInfo = |
| 2084 | computeTouchOcclusionInfoLocked(newTouchedWindowHandle, x, y); |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 2085 | if (!isTouchTrustedLocked(occlusionInfo)) { |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2086 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2087 | ALOGD("Stack of obscuring windows during untrusted touch (%d, %d):", x, y); |
| 2088 | for (const auto& log : occlusionInfo.debugInfo) { |
| 2089 | ALOGD("%s", log.c_str()); |
| 2090 | } |
| 2091 | } |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2092 | sendUntrustedTouchCommandLocked(occlusionInfo.obscuringPackage); |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 2093 | if (mBlockUntrustedTouchesMode == BlockUntrustedTouchesMode::BLOCK) { |
| 2094 | ALOGW("Dropping untrusted touch event due to %s/%d", |
| 2095 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid); |
| 2096 | newTouchedWindowHandle = nullptr; |
| 2097 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2098 | } |
| 2099 | } |
| 2100 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2101 | // Drop touch events if requested by input feature |
| 2102 | if (newTouchedWindowHandle != nullptr && shouldDropInput(entry, newTouchedWindowHandle)) { |
| 2103 | newTouchedWindowHandle = nullptr; |
| 2104 | } |
| 2105 | |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2106 | const std::vector<Monitor> newGestureMonitors = isDown |
| 2107 | ? selectResponsiveMonitorsLocked( |
| 2108 | getValueByKey(mGestureMonitorsByDisplay, displayId)) |
| 2109 | : std::vector<Monitor>{}; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2110 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2111 | if (newTouchedWindowHandle == nullptr && newGestureMonitors.empty()) { |
| 2112 | 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] | 2113 | "(%d, %d) in display %" PRId32 ".", |
| 2114 | x, y, displayId); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2115 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2116 | goto Failed; |
| 2117 | } |
| 2118 | |
| 2119 | if (newTouchedWindowHandle != nullptr) { |
| 2120 | // Set target flags. |
| 2121 | int32_t targetFlags = InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS; |
| 2122 | if (isSplit) { |
| 2123 | targetFlags |= InputTarget::FLAG_SPLIT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2124 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2125 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
| 2126 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
| 2127 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
| 2128 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 2129 | } |
| 2130 | |
| 2131 | // Update hover state. |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2132 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 2133 | newHoverWindowHandle = nullptr; |
| 2134 | } else if (isHoverAction) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2135 | newHoverWindowHandle = newTouchedWindowHandle; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2136 | } |
| 2137 | |
| 2138 | // Update the temporary touch state. |
| 2139 | BitSet32 pointerIds; |
| 2140 | if (isSplit) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2141 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2142 | pointerIds.markBit(pointerId); |
| 2143 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2144 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2145 | } |
| 2146 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2147 | tempTouchState.addGestureMonitors(newGestureMonitors); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2148 | } else { |
| 2149 | /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ |
| 2150 | |
| 2151 | // If the pointer is not currently down, then ignore the event. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2152 | if (!tempTouchState.down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2153 | if (DEBUG_FOCUS) { |
| 2154 | ALOGD("Dropping event because the pointer is not down or we previously " |
| 2155 | "dropped the pointer down event in display %" PRId32, |
| 2156 | displayId); |
| 2157 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2158 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2159 | goto Failed; |
| 2160 | } |
| 2161 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2162 | addDragEventLocked(entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2163 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2164 | // Check whether touches should slip outside of the current foreground window. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2165 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.pointerCount == 1 && |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2166 | tempTouchState.isSlippery()) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2167 | int32_t x = int32_t(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2168 | 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] | 2169 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2170 | sp<WindowInfoHandle> oldTouchedWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2171 | tempTouchState.getFirstForegroundWindowHandle(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2172 | newTouchedWindowHandle = findTouchedWindowAtLocked(displayId, x, y, &tempTouchState); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2173 | |
| 2174 | // Drop touch events if requested by input feature |
| 2175 | if (newTouchedWindowHandle != nullptr && |
| 2176 | shouldDropInput(entry, newTouchedWindowHandle)) { |
| 2177 | newTouchedWindowHandle = nullptr; |
| 2178 | } |
| 2179 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2180 | if (oldTouchedWindowHandle != newTouchedWindowHandle && |
| 2181 | oldTouchedWindowHandle != nullptr && newTouchedWindowHandle != nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2182 | if (DEBUG_FOCUS) { |
| 2183 | ALOGD("Touch is slipping out of window %s into window %s in display %" PRId32, |
| 2184 | oldTouchedWindowHandle->getName().c_str(), |
| 2185 | newTouchedWindowHandle->getName().c_str(), displayId); |
| 2186 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2187 | // Make a slippery exit from the old window. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2188 | tempTouchState.addOrUpdateWindow(oldTouchedWindowHandle, |
| 2189 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT, |
| 2190 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2191 | |
| 2192 | // Make a slippery entrance into the new window. |
| 2193 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
| 2194 | isSplit = true; |
| 2195 | } |
| 2196 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2197 | int32_t targetFlags = |
| 2198 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2199 | if (isSplit) { |
| 2200 | targetFlags |= InputTarget::FLAG_SPLIT; |
| 2201 | } |
| 2202 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
| 2203 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2204 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
| 2205 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2206 | } |
| 2207 | |
| 2208 | BitSet32 pointerIds; |
| 2209 | if (isSplit) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2210 | pointerIds.markBit(entry.pointerProperties[0].id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2211 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2212 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2213 | } |
| 2214 | } |
| 2215 | } |
| 2216 | |
| 2217 | if (newHoverWindowHandle != mLastHoverWindowHandle) { |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2218 | // Let the previous window know that the hover sequence is over, unless we already did it |
| 2219 | // when dispatching it as is to newTouchedWindowHandle. |
| 2220 | if (mLastHoverWindowHandle != nullptr && |
| 2221 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT || |
| 2222 | mLastHoverWindowHandle != newTouchedWindowHandle)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2223 | if (DEBUG_HOVER) { |
| 2224 | ALOGD("Sending hover exit event to window %s.", |
| 2225 | mLastHoverWindowHandle->getName().c_str()); |
| 2226 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2227 | tempTouchState.addOrUpdateWindow(mLastHoverWindowHandle, |
| 2228 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT, BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2229 | } |
| 2230 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2231 | // Let the new window know that the hover sequence is starting, unless we already did it |
| 2232 | // when dispatching it as is to newTouchedWindowHandle. |
| 2233 | if (newHoverWindowHandle != nullptr && |
| 2234 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2235 | newHoverWindowHandle != newTouchedWindowHandle)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2236 | if (DEBUG_HOVER) { |
| 2237 | ALOGD("Sending hover enter event to window %s.", |
| 2238 | newHoverWindowHandle->getName().c_str()); |
| 2239 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2240 | tempTouchState.addOrUpdateWindow(newHoverWindowHandle, |
| 2241 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER, |
| 2242 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2243 | } |
| 2244 | } |
| 2245 | |
| 2246 | // Check permission to inject into all touched foreground windows and ensure there |
| 2247 | // is at least one touched foreground window. |
| 2248 | { |
| 2249 | bool haveForegroundWindow = false; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2250 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2251 | if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) { |
| 2252 | haveForegroundWindow = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2253 | if (!checkInjectionPermission(touchedWindow.windowHandle, entry.injectionState)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2254 | injectionResult = InputEventInjectionResult::PERMISSION_DENIED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2255 | injectionPermission = INJECTION_PERMISSION_DENIED; |
| 2256 | goto Failed; |
| 2257 | } |
| 2258 | } |
| 2259 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2260 | bool hasGestureMonitor = !tempTouchState.gestureMonitors.empty(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2261 | if (!haveForegroundWindow && !hasGestureMonitor) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2262 | ALOGI("Dropping event because there is no touched foreground window in display " |
| 2263 | "%" PRId32 " or gesture monitor to receive it.", |
| 2264 | displayId); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2265 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2266 | goto Failed; |
| 2267 | } |
| 2268 | |
| 2269 | // Permission granted to injection into all touched foreground windows. |
| 2270 | injectionPermission = INJECTION_PERMISSION_GRANTED; |
| 2271 | } |
| 2272 | |
| 2273 | // Check whether windows listening for outside touches are owned by the same UID. If it is |
| 2274 | // set the policy flag that we will not reveal coordinate information to this window. |
| 2275 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2276 | sp<WindowInfoHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2277 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2278 | if (foregroundWindowHandle) { |
| 2279 | const int32_t foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2280 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2281 | if (touchedWindow.targetFlags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2282 | sp<WindowInfoHandle> windowInfoHandle = touchedWindow.windowHandle; |
| 2283 | if (windowInfoHandle->getInfo()->ownerUid != foregroundWindowUid) { |
| 2284 | tempTouchState.addOrUpdateWindow(windowInfoHandle, |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2285 | InputTarget::FLAG_ZERO_COORDS, |
| 2286 | BitSet32(0)); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2287 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2288 | } |
| 2289 | } |
| 2290 | } |
| 2291 | } |
| 2292 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2293 | // If this is the first pointer going down and the touched window has a wallpaper |
| 2294 | // then also add the touched wallpaper windows so they are locked in for the duration |
| 2295 | // of the touch gesture. |
| 2296 | // We do not collect wallpapers during HOVER_MOVE or SCROLL because the wallpaper |
| 2297 | // engine only supports touch events. We would need to add a mechanism similar |
| 2298 | // to View.onGenericMotionEvent to enable wallpapers to handle these events. |
| 2299 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2300 | sp<WindowInfoHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2301 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2302 | if (foregroundWindowHandle && foregroundWindowHandle->getInfo()->hasWallpaper) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2303 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2304 | getWindowHandlesLocked(displayId); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2305 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 2306 | const WindowInfo* info = windowHandle->getInfo(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2307 | if (info->displayId == displayId && |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2308 | windowHandle->getInfo()->type == WindowInfo::Type::WALLPAPER) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2309 | tempTouchState |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2310 | .addOrUpdateWindow(windowHandle, |
| 2311 | InputTarget::FLAG_WINDOW_IS_OBSCURED | |
| 2312 | InputTarget:: |
| 2313 | FLAG_WINDOW_IS_PARTIALLY_OBSCURED | |
| 2314 | InputTarget::FLAG_DISPATCH_AS_IS, |
| 2315 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2316 | } |
| 2317 | } |
| 2318 | } |
| 2319 | } |
| 2320 | |
| 2321 | // Success! Output targets. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2322 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2323 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2324 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2325 | addWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2326 | touchedWindow.pointerIds, inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2327 | } |
| 2328 | |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2329 | for (const auto& monitor : tempTouchState.gestureMonitors) { |
| 2330 | addMonitoringTargetLocked(monitor, displayId, inputTargets); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2331 | } |
| 2332 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2333 | // Drop the outside or hover touch windows since we will not care about them |
| 2334 | // in the next iteration. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2335 | tempTouchState.filterNonAsIsTouchWindows(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2336 | |
| 2337 | Failed: |
| 2338 | // Check injection permission once and for all. |
| 2339 | if (injectionPermission == INJECTION_PERMISSION_UNKNOWN) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2340 | if (checkInjectionPermission(nullptr, entry.injectionState)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2341 | injectionPermission = INJECTION_PERMISSION_GRANTED; |
| 2342 | } else { |
| 2343 | injectionPermission = INJECTION_PERMISSION_DENIED; |
| 2344 | } |
| 2345 | } |
| 2346 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2347 | if (injectionPermission != INJECTION_PERMISSION_GRANTED) { |
| 2348 | return injectionResult; |
| 2349 | } |
| 2350 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2351 | // Update final pieces of touch state if the injector had permission. |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2352 | if (!wrongDevice) { |
| 2353 | if (switchedDevice) { |
| 2354 | if (DEBUG_FOCUS) { |
| 2355 | ALOGD("Conflicting pointer actions: Switched to a different device."); |
| 2356 | } |
| 2357 | *outConflictingPointerActions = true; |
| 2358 | } |
| 2359 | |
| 2360 | if (isHoverAction) { |
| 2361 | // Started hovering, therefore no longer down. |
| 2362 | if (oldState && oldState->down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2363 | if (DEBUG_FOCUS) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2364 | ALOGD("Conflicting pointer actions: Hover received while pointer was " |
| 2365 | "down."); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2366 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2367 | *outConflictingPointerActions = true; |
| 2368 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2369 | tempTouchState.reset(); |
| 2370 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2371 | maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) { |
| 2372 | tempTouchState.deviceId = entry.deviceId; |
| 2373 | tempTouchState.source = entry.source; |
| 2374 | tempTouchState.displayId = displayId; |
| 2375 | } |
| 2376 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP || |
| 2377 | maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
| 2378 | // All pointers up or canceled. |
| 2379 | tempTouchState.reset(); |
| 2380 | } else if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 2381 | // First pointer went down. |
| 2382 | if (oldState && oldState->down) { |
| 2383 | if (DEBUG_FOCUS) { |
| 2384 | ALOGD("Conflicting pointer actions: Down received while already down."); |
| 2385 | } |
| 2386 | *outConflictingPointerActions = true; |
| 2387 | } |
| 2388 | } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 2389 | // One pointer went up. |
| 2390 | if (isSplit) { |
| 2391 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
| 2392 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2393 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2394 | for (size_t i = 0; i < tempTouchState.windows.size();) { |
| 2395 | TouchedWindow& touchedWindow = tempTouchState.windows[i]; |
| 2396 | if (touchedWindow.targetFlags & InputTarget::FLAG_SPLIT) { |
| 2397 | touchedWindow.pointerIds.clearBit(pointerId); |
| 2398 | if (touchedWindow.pointerIds.isEmpty()) { |
| 2399 | tempTouchState.windows.erase(tempTouchState.windows.begin() + i); |
| 2400 | continue; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2401 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2402 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2403 | i += 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2404 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2405 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2406 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2407 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2408 | // Save changes unless the action was scroll in which case the temporary touch |
| 2409 | // state was only valid for this one action. |
| 2410 | if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) { |
| 2411 | if (tempTouchState.displayId >= 0) { |
| 2412 | mTouchStatesByDisplay[displayId] = tempTouchState; |
| 2413 | } else { |
| 2414 | mTouchStatesByDisplay.erase(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2415 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2416 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2417 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2418 | // Update hover state. |
| 2419 | mLastHoverWindowHandle = newHoverWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2420 | } |
| 2421 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2422 | return injectionResult; |
| 2423 | } |
| 2424 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2425 | void InputDispatcher::finishDragAndDrop(int32_t displayId, float x, float y) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2426 | const sp<WindowInfoHandle> dropWindow = |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2427 | findTouchedWindowAtLocked(displayId, x, y, nullptr /*touchState*/, |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 2428 | false /*addOutsideTargets*/, true /*ignoreDragWindow*/); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2429 | if (dropWindow) { |
| 2430 | vec2 local = dropWindow->getInfo()->transform.transform(x, y); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2431 | sendDropWindowCommandLocked(dropWindow->getToken(), local.x, local.y); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 2432 | } else { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2433 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2434 | } |
| 2435 | mDragState.reset(); |
| 2436 | } |
| 2437 | |
| 2438 | void InputDispatcher::addDragEventLocked(const MotionEntry& entry) { |
| 2439 | if (entry.pointerCount != 1 || !mDragState) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2440 | return; |
| 2441 | } |
| 2442 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2443 | if (!mDragState->isStartDrag) { |
| 2444 | mDragState->isStartDrag = true; |
| 2445 | mDragState->isStylusButtonDownAtStart = |
| 2446 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2447 | } |
| 2448 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2449 | int32_t maskedAction = entry.action & AMOTION_EVENT_ACTION_MASK; |
| 2450 | int32_t x = static_cast<int32_t>(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2451 | int32_t y = static_cast<int32_t>(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
| 2452 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2453 | // Handle the special case : stylus button no longer pressed. |
| 2454 | bool isStylusButtonDown = (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2455 | if (mDragState->isStylusButtonDownAtStart && !isStylusButtonDown) { |
| 2456 | finishDragAndDrop(entry.displayId, x, y); |
| 2457 | return; |
| 2458 | } |
| 2459 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2460 | const sp<WindowInfoHandle> hoverWindowHandle = |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2461 | findTouchedWindowAtLocked(entry.displayId, x, y, nullptr /*touchState*/, |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 2462 | false /*addOutsideTargets*/, true /*ignoreDragWindow*/); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2463 | // enqueue drag exit if needed. |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2464 | if (hoverWindowHandle != mDragState->dragHoverWindowHandle && |
| 2465 | !haveSameToken(hoverWindowHandle, mDragState->dragHoverWindowHandle)) { |
| 2466 | if (mDragState->dragHoverWindowHandle != nullptr) { |
| 2467 | enqueueDragEventLocked(mDragState->dragHoverWindowHandle, true /*isExiting*/, |
| 2468 | entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2469 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2470 | mDragState->dragHoverWindowHandle = hoverWindowHandle; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2471 | } |
| 2472 | // enqueue drag location if needed. |
| 2473 | if (hoverWindowHandle != nullptr) { |
| 2474 | enqueueDragEventLocked(hoverWindowHandle, false /*isExiting*/, entry); |
| 2475 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2476 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP) { |
| 2477 | finishDragAndDrop(entry.displayId, x, y); |
| 2478 | } else if (maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2479 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2480 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2481 | } |
| 2482 | } |
| 2483 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2484 | void InputDispatcher::addWindowTargetLocked(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2485 | int32_t targetFlags, BitSet32 pointerIds, |
| 2486 | std::vector<InputTarget>& inputTargets) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2487 | std::vector<InputTarget>::iterator it = |
| 2488 | std::find_if(inputTargets.begin(), inputTargets.end(), |
| 2489 | [&windowHandle](const InputTarget& inputTarget) { |
| 2490 | return inputTarget.inputChannel->getConnectionToken() == |
| 2491 | windowHandle->getToken(); |
| 2492 | }); |
Chavi Weingarten | 97b8eec | 2020-01-09 18:09:08 +0000 | [diff] [blame] | 2493 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2494 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2495 | |
| 2496 | if (it == inputTargets.end()) { |
| 2497 | InputTarget inputTarget; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 2498 | std::shared_ptr<InputChannel> inputChannel = |
| 2499 | getInputChannelLocked(windowHandle->getToken()); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2500 | if (inputChannel == nullptr) { |
| 2501 | ALOGW("Window %s already unregistered input channel", windowHandle->getName().c_str()); |
| 2502 | return; |
| 2503 | } |
| 2504 | inputTarget.inputChannel = inputChannel; |
| 2505 | inputTarget.flags = targetFlags; |
| 2506 | inputTarget.globalScaleFactor = windowInfo->globalScaleFactor; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2507 | const auto& displayInfoIt = mDisplayInfos.find(windowInfo->displayId); |
| 2508 | if (displayInfoIt != mDisplayInfos.end()) { |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 2509 | inputTarget.displayTransform = displayInfoIt->second.transform; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2510 | } else { |
| 2511 | ALOGI_IF(isPerWindowInputRotationEnabled(), |
| 2512 | "DisplayInfo not found for window on display: %d", windowInfo->displayId); |
| 2513 | } |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2514 | inputTargets.push_back(inputTarget); |
| 2515 | it = inputTargets.end() - 1; |
| 2516 | } |
| 2517 | |
| 2518 | ALOG_ASSERT(it->flags == targetFlags); |
| 2519 | ALOG_ASSERT(it->globalScaleFactor == windowInfo->globalScaleFactor); |
| 2520 | |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2521 | it->addPointers(pointerIds, windowInfo->transform); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2522 | } |
| 2523 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2524 | void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2525 | int32_t displayId) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2526 | std::unordered_map<int32_t, std::vector<Monitor>>::const_iterator it = |
| 2527 | mGlobalMonitorsByDisplay.find(displayId); |
| 2528 | |
| 2529 | if (it != mGlobalMonitorsByDisplay.end()) { |
| 2530 | const std::vector<Monitor>& monitors = it->second; |
| 2531 | for (const Monitor& monitor : monitors) { |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2532 | addMonitoringTargetLocked(monitor, displayId, inputTargets); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2533 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2534 | } |
| 2535 | } |
| 2536 | |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2537 | void InputDispatcher::addMonitoringTargetLocked(const Monitor& monitor, int32_t displayId, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2538 | std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2539 | InputTarget target; |
| 2540 | target.inputChannel = monitor.inputChannel; |
| 2541 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2542 | ui::Transform t; |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 2543 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
| 2544 | // Input monitors always get un-rotated display coordinates. We undo the display |
| 2545 | // rotation that is present in the display transform so that display rotation is not |
| 2546 | // applied to these input targets. |
| 2547 | const auto& displayInfo = it->second; |
| 2548 | int32_t width = displayInfo.logicalWidth; |
| 2549 | int32_t height = displayInfo.logicalHeight; |
| 2550 | const auto orientation = displayInfo.transform.getOrientation(); |
| 2551 | uint32_t inverseOrientation = orientation; |
| 2552 | if (orientation == ui::Transform::ROT_90) { |
| 2553 | inverseOrientation = ui::Transform::ROT_270; |
| 2554 | std::swap(width, height); |
| 2555 | } else if (orientation == ui::Transform::ROT_270) { |
| 2556 | inverseOrientation = ui::Transform::ROT_90; |
| 2557 | std::swap(width, height); |
| 2558 | } |
| 2559 | target.displayTransform = |
| 2560 | ui::Transform(inverseOrientation, width, height) * displayInfo.transform; |
| 2561 | t = t * target.displayTransform; |
| 2562 | } |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2563 | target.setDefaultPointerTransform(t); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2564 | inputTargets.push_back(target); |
| 2565 | } |
| 2566 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2567 | bool InputDispatcher::checkInjectionPermission(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2568 | const InjectionState* injectionState) { |
| 2569 | if (injectionState && |
| 2570 | (windowHandle == nullptr || |
| 2571 | windowHandle->getInfo()->ownerUid != injectionState->injectorUid) && |
| 2572 | !hasInjectionPermission(injectionState->injectorPid, injectionState->injectorUid)) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2573 | if (windowHandle != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2574 | 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] | 2575 | "owned by uid %d", |
| 2576 | injectionState->injectorPid, injectionState->injectorUid, |
| 2577 | windowHandle->getName().c_str(), windowHandle->getInfo()->ownerUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2578 | } else { |
| 2579 | ALOGW("Permission denied: injecting event from pid %d uid %d", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2580 | injectionState->injectorPid, injectionState->injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2581 | } |
| 2582 | return false; |
| 2583 | } |
| 2584 | return true; |
| 2585 | } |
| 2586 | |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2587 | /** |
| 2588 | * Indicate whether one window handle should be considered as obscuring |
| 2589 | * another window handle. We only check a few preconditions. Actually |
| 2590 | * checking the bounds is left to the caller. |
| 2591 | */ |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2592 | static bool canBeObscuredBy(const sp<WindowInfoHandle>& windowHandle, |
| 2593 | const sp<WindowInfoHandle>& otherHandle) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2594 | // Compare by token so cloned layers aren't counted |
| 2595 | if (haveSameToken(windowHandle, otherHandle)) { |
| 2596 | return false; |
| 2597 | } |
| 2598 | auto info = windowHandle->getInfo(); |
| 2599 | auto otherInfo = otherHandle->getInfo(); |
| 2600 | if (!otherInfo->visible) { |
| 2601 | return false; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2602 | } else if (otherInfo->alpha == 0 && otherInfo->flags.test(WindowInfo::Flag::NOT_TOUCHABLE)) { |
Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame] | 2603 | // Those act as if they were invisible, so we don't need to flag them. |
| 2604 | // We do want to potentially flag touchable windows even if they have 0 |
| 2605 | // opacity, since they can consume touches and alter the effects of the |
| 2606 | // user interaction (eg. apps that rely on |
| 2607 | // FLAG_WINDOW_IS_PARTIALLY_OBSCURED should still be told about those |
| 2608 | // windows), hence we also check for FLAG_NOT_TOUCHABLE. |
| 2609 | return false; |
Bernardo Rufino | 8007daf | 2020-09-22 09:40:01 +0000 | [diff] [blame] | 2610 | } else if (info->ownerUid == otherInfo->ownerUid) { |
| 2611 | // If ownerUid is the same we don't generate occlusion events as there |
| 2612 | // is no security boundary within an uid. |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2613 | return false; |
Chris Ye | fcdff3e | 2020-05-10 15:16:04 -0700 | [diff] [blame] | 2614 | } else if (otherInfo->trustedOverlay) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2615 | return false; |
| 2616 | } else if (otherInfo->displayId != info->displayId) { |
| 2617 | return false; |
| 2618 | } |
| 2619 | return true; |
| 2620 | } |
| 2621 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2622 | /** |
| 2623 | * Returns touch occlusion information in the form of TouchOcclusionInfo. To check if the touch is |
| 2624 | * untrusted, one should check: |
| 2625 | * |
| 2626 | * 1. If result.hasBlockingOcclusion is true. |
| 2627 | * If it's, it means the touch should be blocked due to a window with occlusion mode of |
| 2628 | * BLOCK_UNTRUSTED. |
| 2629 | * |
| 2630 | * 2. If result.obscuringOpacity > mMaximumObscuringOpacityForTouch. |
| 2631 | * If it is (and 1 is false), then the touch should be blocked because a stack of windows |
| 2632 | * (possibly only one) with occlusion mode of USE_OPACITY from one UID resulted in a composed |
| 2633 | * obscuring opacity above the threshold. Note that if there was no window of occlusion mode |
| 2634 | * USE_OPACITY, result.obscuringOpacity would've been 0 and since |
| 2635 | * mMaximumObscuringOpacityForTouch >= 0, the condition above would never be true. |
| 2636 | * |
| 2637 | * If neither of those is true, then it means the touch can be allowed. |
| 2638 | */ |
| 2639 | InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2640 | const sp<WindowInfoHandle>& windowHandle, int32_t x, int32_t y) const { |
| 2641 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2642 | int32_t displayId = windowInfo->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2643 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2644 | TouchOcclusionInfo info; |
| 2645 | info.hasBlockingOcclusion = false; |
| 2646 | info.obscuringOpacity = 0; |
| 2647 | info.obscuringUid = -1; |
| 2648 | std::map<int32_t, float> opacityByUid; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2649 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2650 | if (windowHandle == otherHandle) { |
| 2651 | break; // All future windows are below us. Exit early. |
| 2652 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2653 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 2654 | if (canBeObscuredBy(windowHandle, otherHandle) && otherInfo->frameContainsPoint(x, y) && |
| 2655 | !haveSameApplicationToken(windowInfo, otherInfo)) { |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2656 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2657 | info.debugInfo.push_back( |
| 2658 | dumpWindowForTouchOcclusion(otherInfo, /* isTouchedWindow */ false)); |
| 2659 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2660 | // canBeObscuredBy() has returned true above, which means this window is untrusted, so |
| 2661 | // we perform the checks below to see if the touch can be propagated or not based on the |
| 2662 | // window's touch occlusion mode |
| 2663 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::BLOCK_UNTRUSTED) { |
| 2664 | info.hasBlockingOcclusion = true; |
| 2665 | info.obscuringUid = otherInfo->ownerUid; |
| 2666 | info.obscuringPackage = otherInfo->packageName; |
| 2667 | break; |
| 2668 | } |
| 2669 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::USE_OPACITY) { |
| 2670 | uint32_t uid = otherInfo->ownerUid; |
| 2671 | float opacity = |
| 2672 | (opacityByUid.find(uid) == opacityByUid.end()) ? 0 : opacityByUid[uid]; |
| 2673 | // Given windows A and B: |
| 2674 | // opacity(A, B) = 1 - [1 - opacity(A)] * [1 - opacity(B)] |
| 2675 | opacity = 1 - (1 - opacity) * (1 - otherInfo->alpha); |
| 2676 | opacityByUid[uid] = opacity; |
| 2677 | if (opacity > info.obscuringOpacity) { |
| 2678 | info.obscuringOpacity = opacity; |
| 2679 | info.obscuringUid = uid; |
| 2680 | info.obscuringPackage = otherInfo->packageName; |
| 2681 | } |
| 2682 | } |
| 2683 | } |
| 2684 | } |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2685 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2686 | info.debugInfo.push_back( |
| 2687 | dumpWindowForTouchOcclusion(windowInfo, /* isTouchedWindow */ true)); |
| 2688 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2689 | return info; |
| 2690 | } |
| 2691 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2692 | std::string InputDispatcher::dumpWindowForTouchOcclusion(const WindowInfo* info, |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2693 | bool isTouchedWindow) const { |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 2694 | return StringPrintf(INDENT2 |
| 2695 | "* %stype=%s, package=%s/%" PRId32 ", id=%" PRId32 ", mode=%s, alpha=%.2f, " |
| 2696 | "frame=[%" PRId32 ",%" PRId32 "][%" PRId32 ",%" PRId32 |
| 2697 | "], touchableRegion=%s, window={%s}, flags={%s}, inputFeatures={%s}, " |
| 2698 | "hasToken=%s, applicationInfo.name=%s, applicationInfo.token=%s\n", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2699 | isTouchedWindow ? "[TOUCHED] " : "", ftl::enum_string(info->type).c_str(), |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 2700 | info->packageName.c_str(), info->ownerUid, info->id, |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 2701 | toString(info->touchOcclusionMode).c_str(), info->alpha, info->frameLeft, |
| 2702 | info->frameTop, info->frameRight, info->frameBottom, |
| 2703 | dumpRegion(info->touchableRegion).c_str(), info->name.c_str(), |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 2704 | info->flags.string().c_str(), info->inputFeatures.string().c_str(), |
| 2705 | toString(info->token != nullptr), info->applicationInfo.name.c_str(), |
| 2706 | toString(info->applicationInfo.token).c_str()); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2707 | } |
| 2708 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2709 | bool InputDispatcher::isTouchTrustedLocked(const TouchOcclusionInfo& occlusionInfo) const { |
| 2710 | if (occlusionInfo.hasBlockingOcclusion) { |
| 2711 | ALOGW("Untrusted touch due to occlusion by %s/%d", occlusionInfo.obscuringPackage.c_str(), |
| 2712 | occlusionInfo.obscuringUid); |
| 2713 | return false; |
| 2714 | } |
| 2715 | if (occlusionInfo.obscuringOpacity > mMaximumObscuringOpacityForTouch) { |
| 2716 | ALOGW("Untrusted touch due to occlusion by %s/%d (obscuring opacity = " |
| 2717 | "%.2f, maximum allowed = %.2f)", |
| 2718 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid, |
| 2719 | occlusionInfo.obscuringOpacity, mMaximumObscuringOpacityForTouch); |
| 2720 | return false; |
| 2721 | } |
| 2722 | return true; |
| 2723 | } |
| 2724 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2725 | bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2726 | int32_t x, int32_t y) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2727 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2728 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 2729 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2730 | if (windowHandle == otherHandle) { |
| 2731 | break; // All future windows are below us. Exit early. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2732 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2733 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2734 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2735 | otherInfo->frameContainsPoint(x, y)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2736 | return true; |
| 2737 | } |
| 2738 | } |
| 2739 | return false; |
| 2740 | } |
| 2741 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2742 | bool InputDispatcher::isWindowObscuredLocked(const sp<WindowInfoHandle>& windowHandle) const { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2743 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2744 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 2745 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 2746 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2747 | if (windowHandle == otherHandle) { |
| 2748 | break; // All future windows are below us. Exit early. |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2749 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2750 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2751 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2752 | otherInfo->overlaps(windowInfo)) { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2753 | return true; |
| 2754 | } |
| 2755 | } |
| 2756 | return false; |
| 2757 | } |
| 2758 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2759 | std::string InputDispatcher::getApplicationWindowLabel( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2760 | const InputApplicationHandle* applicationHandle, const sp<WindowInfoHandle>& windowHandle) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2761 | if (applicationHandle != nullptr) { |
| 2762 | if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2763 | return applicationHandle->getName() + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2764 | } else { |
| 2765 | return applicationHandle->getName(); |
| 2766 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2767 | } else if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2768 | return windowHandle->getInfo()->applicationInfo.name + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2769 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2770 | return "<unknown application or window>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2771 | } |
| 2772 | } |
| 2773 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2774 | void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 2775 | if (eventEntry.type == EventEntry::Type::FOCUS || |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2776 | eventEntry.type == EventEntry::Type::POINTER_CAPTURE_CHANGED || |
| 2777 | eventEntry.type == EventEntry::Type::DRAG) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 2778 | // Focus or pointer capture changed events are passed to apps, but do not represent user |
| 2779 | // activity. |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2780 | return; |
| 2781 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2782 | int32_t displayId = getTargetDisplayId(eventEntry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2783 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2784 | if (focusedWindowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2785 | const WindowInfo* info = focusedWindowHandle->getInfo(); |
| 2786 | if (info->inputFeatures.test(WindowInfo::Feature::DISABLE_USER_ACTIVITY)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2787 | if (DEBUG_DISPATCH_CYCLE) { |
| 2788 | ALOGD("Not poking user activity: disabled by window '%s'.", info->name.c_str()); |
| 2789 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2790 | return; |
| 2791 | } |
| 2792 | } |
| 2793 | |
| 2794 | int32_t eventType = USER_ACTIVITY_EVENT_OTHER; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2795 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2796 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2797 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 2798 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2799 | return; |
| 2800 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2801 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2802 | if (MotionEvent::isTouchEvent(motionEntry.source, motionEntry.action)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2803 | eventType = USER_ACTIVITY_EVENT_TOUCH; |
| 2804 | } |
| 2805 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2806 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2807 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2808 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 2809 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2810 | return; |
| 2811 | } |
| 2812 | eventType = USER_ACTIVITY_EVENT_BUTTON; |
| 2813 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2814 | } |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 2815 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 2816 | break; |
| 2817 | } |
| 2818 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2819 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2820 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 2821 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2822 | case EventEntry::Type::SENSOR: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2823 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 2824 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2825 | LOG_ALWAYS_FATAL("%s events are not user activity", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2826 | ftl::enum_string(eventEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2827 | break; |
| 2828 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2829 | } |
| 2830 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2831 | auto command = [this, eventTime = eventEntry.eventTime, eventType, displayId]() |
| 2832 | REQUIRES(mLock) { |
| 2833 | scoped_unlock unlock(mLock); |
| 2834 | mPolicy->pokeUserActivity(eventTime, eventType, displayId); |
| 2835 | }; |
| 2836 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2837 | } |
| 2838 | |
| 2839 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2840 | const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2841 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2842 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2843 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2844 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2845 | StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2846 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2847 | ATRACE_NAME(message.c_str()); |
| 2848 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2849 | if (DEBUG_DISPATCH_CYCLE) { |
| 2850 | ALOGD("channel '%s' ~ prepareDispatchCycle - flags=0x%08x, " |
| 2851 | "globalScaleFactor=%f, pointerIds=0x%x %s", |
| 2852 | connection->getInputChannelName().c_str(), inputTarget.flags, |
| 2853 | inputTarget.globalScaleFactor, inputTarget.pointerIds.value, |
| 2854 | inputTarget.getPointerInfoString().c_str()); |
| 2855 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2856 | |
| 2857 | // Skip this event if the connection status is not normal. |
| 2858 | // We don't want to enqueue additional outbound events if the connection is broken. |
| 2859 | if (connection->status != Connection::STATUS_NORMAL) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2860 | if (DEBUG_DISPATCH_CYCLE) { |
| 2861 | ALOGD("channel '%s' ~ Dropping event because the channel status is %s", |
| 2862 | connection->getInputChannelName().c_str(), connection->getStatusLabel()); |
| 2863 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2864 | return; |
| 2865 | } |
| 2866 | |
| 2867 | // Split a motion event if needed. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2868 | if (inputTarget.flags & InputTarget::FLAG_SPLIT) { |
| 2869 | LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION, |
| 2870 | "Entry type %s should not have FLAG_SPLIT", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2871 | ftl::enum_string(eventEntry->type).c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2872 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2873 | const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry); |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2874 | if (inputTarget.pointerIds.count() != originalMotionEntry.pointerCount) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2875 | std::unique_ptr<MotionEntry> splitMotionEntry = |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2876 | splitMotionEvent(originalMotionEntry, inputTarget.pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2877 | if (!splitMotionEntry) { |
| 2878 | return; // split event was dropped |
| 2879 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2880 | if (DEBUG_FOCUS) { |
| 2881 | ALOGD("channel '%s' ~ Split motion event.", |
| 2882 | connection->getInputChannelName().c_str()); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2883 | logOutboundMotionDetails(" ", *splitMotionEntry); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2884 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2885 | enqueueDispatchEntriesLocked(currentTime, connection, std::move(splitMotionEntry), |
| 2886 | inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2887 | return; |
| 2888 | } |
| 2889 | } |
| 2890 | |
| 2891 | // Not splitting. Enqueue dispatch entries for the event as is. |
| 2892 | enqueueDispatchEntriesLocked(currentTime, connection, eventEntry, inputTarget); |
| 2893 | } |
| 2894 | |
| 2895 | void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2896 | const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2897 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2898 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2899 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2900 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2901 | StringPrintf("enqueueDispatchEntriesLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2902 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2903 | ATRACE_NAME(message.c_str()); |
| 2904 | } |
| 2905 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2906 | bool wasEmpty = connection->outboundQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2907 | |
| 2908 | // Enqueue dispatch entries for the requested modes. |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2909 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2910 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2911 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2912 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2913 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2914 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2915 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2916 | InputTarget::FLAG_DISPATCH_AS_IS); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2917 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2918 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2919 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2920 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2921 | |
| 2922 | // If the outbound queue was previously empty, start the dispatch cycle going. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2923 | if (wasEmpty && !connection->outboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2924 | startDispatchCycleLocked(currentTime, connection); |
| 2925 | } |
| 2926 | } |
| 2927 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2928 | void InputDispatcher::enqueueDispatchEntryLocked(const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2929 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2930 | const InputTarget& inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2931 | int32_t dispatchMode) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2932 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2933 | std::string message = StringPrintf("enqueueDispatchEntry(inputChannel=%s, dispatchMode=%s)", |
| 2934 | connection->getInputChannelName().c_str(), |
| 2935 | dispatchModeToString(dispatchMode).c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2936 | ATRACE_NAME(message.c_str()); |
| 2937 | } |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2938 | int32_t inputTargetFlags = inputTarget.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2939 | if (!(inputTargetFlags & dispatchMode)) { |
| 2940 | return; |
| 2941 | } |
| 2942 | inputTargetFlags = (inputTargetFlags & ~InputTarget::FLAG_DISPATCH_MASK) | dispatchMode; |
| 2943 | |
| 2944 | // This is a new event. |
| 2945 | // Enqueue a new dispatch entry onto the outbound queue for this connection. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2946 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2947 | createDispatchEntry(inputTarget, eventEntry, inputTargetFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2948 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2949 | // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a |
| 2950 | // different EventEntry than what was passed in. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2951 | EventEntry& newEntry = *(dispatchEntry->eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2952 | // Apply target flags and update the connection's input state. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2953 | switch (newEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2954 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2955 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2956 | dispatchEntry->resolvedEventId = keyEntry.id; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2957 | dispatchEntry->resolvedAction = keyEntry.action; |
| 2958 | dispatchEntry->resolvedFlags = keyEntry.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2959 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2960 | if (!connection->inputState.trackKey(keyEntry, dispatchEntry->resolvedAction, |
| 2961 | dispatchEntry->resolvedFlags)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2962 | if (DEBUG_DISPATCH_CYCLE) { |
| 2963 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent key " |
| 2964 | "event", |
| 2965 | connection->getInputChannelName().c_str()); |
| 2966 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2967 | return; // skip the inconsistent event |
| 2968 | } |
| 2969 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2970 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2971 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2972 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2973 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2974 | // Assign a default value to dispatchEntry that will never be generated by InputReader, |
| 2975 | // and assign a InputDispatcher value if it doesn't change in the if-else chain below. |
| 2976 | constexpr int32_t DEFAULT_RESOLVED_EVENT_ID = |
| 2977 | static_cast<int32_t>(IdGenerator::Source::OTHER); |
| 2978 | dispatchEntry->resolvedEventId = DEFAULT_RESOLVED_EVENT_ID; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2979 | if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 2980 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; |
| 2981 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT) { |
| 2982 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; |
| 2983 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER) { |
| 2984 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 2985 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT) { |
| 2986 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_CANCEL; |
| 2987 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER) { |
| 2988 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_DOWN; |
| 2989 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2990 | dispatchEntry->resolvedAction = motionEntry.action; |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2991 | dispatchEntry->resolvedEventId = motionEntry.id; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2992 | } |
| 2993 | if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE && |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2994 | !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source, |
| 2995 | motionEntry.displayId)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2996 | if (DEBUG_DISPATCH_CYCLE) { |
| 2997 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: filling in missing hover " |
| 2998 | "enter event", |
| 2999 | connection->getInputChannelName().c_str()); |
| 3000 | } |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3001 | // We keep the 'resolvedEventId' here equal to the original 'motionEntry.id' because |
| 3002 | // this is a one-to-one event conversion. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3003 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 3004 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3005 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3006 | dispatchEntry->resolvedFlags = motionEntry.flags; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3007 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_OBSCURED) { |
| 3008 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; |
| 3009 | } |
| 3010 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED) { |
| 3011 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 3012 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3013 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3014 | if (!connection->inputState.trackMotion(motionEntry, dispatchEntry->resolvedAction, |
| 3015 | dispatchEntry->resolvedFlags)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3016 | if (DEBUG_DISPATCH_CYCLE) { |
| 3017 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent motion " |
| 3018 | "event", |
| 3019 | connection->getInputChannelName().c_str()); |
| 3020 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3021 | return; // skip the inconsistent event |
| 3022 | } |
| 3023 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3024 | dispatchEntry->resolvedEventId = |
| 3025 | dispatchEntry->resolvedEventId == DEFAULT_RESOLVED_EVENT_ID |
| 3026 | ? mIdGenerator.nextId() |
| 3027 | : motionEntry.id; |
| 3028 | if (ATRACE_ENABLED() && dispatchEntry->resolvedEventId != motionEntry.id) { |
| 3029 | std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32 |
| 3030 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3031 | motionEntry.id, dispatchEntry->resolvedEventId); |
| 3032 | ATRACE_NAME(message.c_str()); |
| 3033 | } |
| 3034 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 3035 | if ((motionEntry.flags & AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) && |
| 3036 | (motionEntry.policyFlags & POLICY_FLAG_TRUSTED)) { |
| 3037 | // Skip reporting pointer down outside focus to the policy. |
| 3038 | break; |
| 3039 | } |
| 3040 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3041 | dispatchPointerDownOutsideFocus(motionEntry.source, dispatchEntry->resolvedAction, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3042 | inputTarget.inputChannel->getConnectionToken()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3043 | |
| 3044 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3045 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3046 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3047 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3048 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3049 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3050 | break; |
| 3051 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3052 | case EventEntry::Type::SENSOR: { |
| 3053 | LOG_ALWAYS_FATAL("SENSOR events should not go to apps via input channel"); |
| 3054 | break; |
| 3055 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3056 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 3057 | case EventEntry::Type::DEVICE_RESET: { |
| 3058 | LOG_ALWAYS_FATAL("%s events should not go to apps", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3059 | ftl::enum_string(newEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3060 | break; |
| 3061 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3062 | } |
| 3063 | |
| 3064 | // Remember that we are waiting for this dispatch to complete. |
| 3065 | if (dispatchEntry->hasForegroundTarget()) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3066 | incrementPendingForegroundDispatches(newEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3067 | } |
| 3068 | |
| 3069 | // Enqueue the dispatch entry. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3070 | connection->outboundQueue.push_back(dispatchEntry.release()); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3071 | traceOutboundQueueLength(*connection); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3072 | } |
| 3073 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3074 | /** |
| 3075 | * This function is purely for debugging. It helps us understand where the user interaction |
| 3076 | * was taking place. For example, if user is touching launcher, we will see a log that user |
| 3077 | * started interacting with launcher. In that example, the event would go to the wallpaper as well. |
| 3078 | * We will see both launcher and wallpaper in that list. |
| 3079 | * Once the interaction with a particular set of connections starts, no new logs will be printed |
| 3080 | * until the set of interacted connections changes. |
| 3081 | * |
| 3082 | * The following items are skipped, to reduce the logspam: |
| 3083 | * ACTION_OUTSIDE: any windows that are receiving ACTION_OUTSIDE are not logged |
| 3084 | * ACTION_UP: any windows that receive ACTION_UP are not logged (for both keys and motions). |
| 3085 | * This includes situations like the soft BACK button key. When the user releases (lifts up the |
| 3086 | * finger) the back button, then navigation bar will inject KEYCODE_BACK with ACTION_UP. |
| 3087 | * Both of those ACTION_UP events would not be logged |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3088 | */ |
| 3089 | void InputDispatcher::updateInteractionTokensLocked(const EventEntry& entry, |
| 3090 | const std::vector<InputTarget>& targets) { |
| 3091 | // Skip ACTION_UP events, and all events other than keys and motions |
| 3092 | if (entry.type == EventEntry::Type::KEY) { |
| 3093 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 3094 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
| 3095 | return; |
| 3096 | } |
| 3097 | } else if (entry.type == EventEntry::Type::MOTION) { |
| 3098 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 3099 | if (motionEntry.action == AMOTION_EVENT_ACTION_UP || |
| 3100 | motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
| 3101 | return; |
| 3102 | } |
| 3103 | } else { |
| 3104 | return; // Not a key or a motion |
| 3105 | } |
| 3106 | |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 3107 | std::unordered_set<sp<IBinder>, StrongPointerHash<IBinder>> newConnectionTokens; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3108 | std::vector<sp<Connection>> newConnections; |
| 3109 | for (const InputTarget& target : targets) { |
| 3110 | if ((target.flags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) == |
| 3111 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 3112 | continue; // Skip windows that receive ACTION_OUTSIDE |
| 3113 | } |
| 3114 | |
| 3115 | sp<IBinder> token = target.inputChannel->getConnectionToken(); |
| 3116 | sp<Connection> connection = getConnectionLocked(token); |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3117 | if (connection == nullptr) { |
| 3118 | continue; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3119 | } |
| 3120 | newConnectionTokens.insert(std::move(token)); |
| 3121 | newConnections.emplace_back(connection); |
| 3122 | } |
| 3123 | if (newConnectionTokens == mInteractionConnectionTokens) { |
| 3124 | return; // no change |
| 3125 | } |
| 3126 | mInteractionConnectionTokens = newConnectionTokens; |
| 3127 | |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3128 | std::string targetList; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3129 | for (const sp<Connection>& connection : newConnections) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3130 | targetList += connection->getWindowName() + ", "; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3131 | } |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3132 | std::string message = "Interaction with: " + targetList; |
| 3133 | if (targetList.empty()) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3134 | message += "<none>"; |
| 3135 | } |
| 3136 | android_log_event_list(LOGTAG_INPUT_INTERACTION) << message << LOG_ID_EVENTS; |
| 3137 | } |
| 3138 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3139 | void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3140 | const sp<IBinder>& token) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3141 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3142 | uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK; |
| 3143 | if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3144 | return; |
| 3145 | } |
| 3146 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 3147 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3148 | if (focusedToken == token) { |
| 3149 | // ignore since token is focused |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3150 | return; |
| 3151 | } |
| 3152 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3153 | auto command = [this, token]() REQUIRES(mLock) { |
| 3154 | scoped_unlock unlock(mLock); |
| 3155 | mPolicy->onPointerDownOutsideFocus(token); |
| 3156 | }; |
| 3157 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3158 | } |
| 3159 | |
| 3160 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3161 | const sp<Connection>& connection) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3162 | if (ATRACE_ENABLED()) { |
| 3163 | std::string message = StringPrintf("startDispatchCycleLocked(inputChannel=%s)", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3164 | connection->getInputChannelName().c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3165 | ATRACE_NAME(message.c_str()); |
| 3166 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3167 | if (DEBUG_DISPATCH_CYCLE) { |
| 3168 | ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str()); |
| 3169 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3170 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3171 | while (connection->status == Connection::STATUS_NORMAL && !connection->outboundQueue.empty()) { |
| 3172 | DispatchEntry* dispatchEntry = connection->outboundQueue.front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3173 | dispatchEntry->deliveryTime = currentTime; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3174 | const std::chrono::nanoseconds timeout = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3175 | getDispatchingTimeoutLocked(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3176 | dispatchEntry->timeoutTime = currentTime + timeout.count(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3177 | |
| 3178 | // Publish the event. |
| 3179 | status_t status; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3180 | const EventEntry& eventEntry = *(dispatchEntry->eventEntry); |
| 3181 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3182 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3183 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 3184 | std::array<uint8_t, 32> hmac = getSignature(keyEntry, *dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3185 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3186 | // Publish the key event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3187 | status = connection->inputPublisher |
| 3188 | .publishKeyEvent(dispatchEntry->seq, |
| 3189 | dispatchEntry->resolvedEventId, keyEntry.deviceId, |
| 3190 | keyEntry.source, keyEntry.displayId, |
| 3191 | std::move(hmac), dispatchEntry->resolvedAction, |
| 3192 | dispatchEntry->resolvedFlags, keyEntry.keyCode, |
| 3193 | keyEntry.scanCode, keyEntry.metaState, |
| 3194 | keyEntry.repeatCount, keyEntry.downTime, |
| 3195 | keyEntry.eventTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3196 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3197 | } |
| 3198 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3199 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3200 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3201 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3202 | PointerCoords scaledCoords[MAX_POINTERS]; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3203 | const PointerCoords* usingCoords = motionEntry.pointerCoords; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3204 | |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3205 | // 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] | 3206 | if ((motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) && |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3207 | !(dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS)) { |
| 3208 | float globalScaleFactor = dispatchEntry->globalScaleFactor; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3209 | if (globalScaleFactor != 1.0f) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3210 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
| 3211 | scaledCoords[i] = motionEntry.pointerCoords[i]; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3212 | // Don't apply window scale here since we don't want scale to affect raw |
| 3213 | // coordinates. The scale will be sent back to the client and applied |
| 3214 | // later when requesting relative coordinates. |
| 3215 | scaledCoords[i].scale(globalScaleFactor, 1 /* windowXScale */, |
| 3216 | 1 /* windowYScale */); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3217 | } |
| 3218 | usingCoords = scaledCoords; |
| 3219 | } |
| 3220 | } else { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3221 | // We don't want the dispatch target to know. |
| 3222 | if (dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3223 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3224 | scaledCoords[i].clear(); |
| 3225 | } |
| 3226 | usingCoords = scaledCoords; |
| 3227 | } |
| 3228 | } |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3229 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3230 | std::array<uint8_t, 32> hmac = getSignature(motionEntry, *dispatchEntry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3231 | |
| 3232 | // Publish the motion event. |
| 3233 | status = connection->inputPublisher |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3234 | .publishMotionEvent(dispatchEntry->seq, |
| 3235 | dispatchEntry->resolvedEventId, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3236 | motionEntry.deviceId, motionEntry.source, |
| 3237 | motionEntry.displayId, std::move(hmac), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3238 | dispatchEntry->resolvedAction, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3239 | motionEntry.actionButton, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3240 | dispatchEntry->resolvedFlags, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3241 | motionEntry.edgeFlags, motionEntry.metaState, |
| 3242 | motionEntry.buttonState, |
| 3243 | motionEntry.classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3244 | dispatchEntry->transform, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3245 | motionEntry.xPrecision, motionEntry.yPrecision, |
| 3246 | motionEntry.xCursorPosition, |
| 3247 | motionEntry.yCursorPosition, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 3248 | dispatchEntry->rawTransform, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3249 | motionEntry.downTime, motionEntry.eventTime, |
| 3250 | motionEntry.pointerCount, |
| 3251 | motionEntry.pointerProperties, usingCoords); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3252 | break; |
| 3253 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3254 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3255 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3256 | const FocusEntry& focusEntry = static_cast<const FocusEntry&>(eventEntry); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3257 | status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3258 | focusEntry.id, |
| 3259 | focusEntry.hasFocus, |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3260 | mInTouchMode); |
| 3261 | break; |
| 3262 | } |
| 3263 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3264 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 3265 | const TouchModeEntry& touchModeEntry = |
| 3266 | static_cast<const TouchModeEntry&>(eventEntry); |
| 3267 | status = connection->inputPublisher |
| 3268 | .publishTouchModeEvent(dispatchEntry->seq, touchModeEntry.id, |
| 3269 | touchModeEntry.inTouchMode); |
| 3270 | |
| 3271 | break; |
| 3272 | } |
| 3273 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3274 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 3275 | const auto& captureEntry = |
| 3276 | static_cast<const PointerCaptureChangedEntry&>(eventEntry); |
| 3277 | status = connection->inputPublisher |
| 3278 | .publishCaptureEvent(dispatchEntry->seq, captureEntry.id, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 3279 | captureEntry.pointerCaptureRequest.enable); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3280 | break; |
| 3281 | } |
| 3282 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3283 | case EventEntry::Type::DRAG: { |
| 3284 | const DragEntry& dragEntry = static_cast<const DragEntry&>(eventEntry); |
| 3285 | status = connection->inputPublisher.publishDragEvent(dispatchEntry->seq, |
| 3286 | dragEntry.id, dragEntry.x, |
| 3287 | dragEntry.y, |
| 3288 | dragEntry.isExiting); |
| 3289 | break; |
| 3290 | } |
| 3291 | |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3292 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3293 | case EventEntry::Type::DEVICE_RESET: |
| 3294 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3295 | LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3296 | ftl::enum_string(eventEntry.type).c_str()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3297 | return; |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3298 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3299 | } |
| 3300 | |
| 3301 | // Check the result. |
| 3302 | if (status) { |
| 3303 | if (status == WOULD_BLOCK) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3304 | if (connection->waitQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3305 | ALOGE("channel '%s' ~ Could not publish event because the pipe is full. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3306 | "This is unexpected because the wait queue is empty, so the pipe " |
| 3307 | "should be empty and we shouldn't have any problems writing an " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3308 | "event to it, status=%s(%d)", |
| 3309 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3310 | status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3311 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 3312 | } else { |
| 3313 | // Pipe is full and we are waiting for the app to finish process some events |
| 3314 | // before sending more events to it. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3315 | if (DEBUG_DISPATCH_CYCLE) { |
| 3316 | ALOGD("channel '%s' ~ Could not publish event because the pipe is full, " |
| 3317 | "waiting for the application to catch up", |
| 3318 | connection->getInputChannelName().c_str()); |
| 3319 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3320 | } |
| 3321 | } else { |
| 3322 | ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3323 | "status=%s(%d)", |
| 3324 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3325 | status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3326 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 3327 | } |
| 3328 | return; |
| 3329 | } |
| 3330 | |
| 3331 | // Re-enqueue the event on the wait queue. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3332 | connection->outboundQueue.erase(std::remove(connection->outboundQueue.begin(), |
| 3333 | connection->outboundQueue.end(), |
| 3334 | dispatchEntry)); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3335 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3336 | connection->waitQueue.push_back(dispatchEntry); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3337 | if (connection->responsive) { |
| 3338 | mAnrTracker.insert(dispatchEntry->timeoutTime, |
| 3339 | connection->inputChannel->getConnectionToken()); |
| 3340 | } |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3341 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3342 | } |
| 3343 | } |
| 3344 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3345 | std::array<uint8_t, 32> InputDispatcher::sign(const VerifiedInputEvent& event) const { |
| 3346 | size_t size; |
| 3347 | switch (event.type) { |
| 3348 | case VerifiedInputEvent::Type::KEY: { |
| 3349 | size = sizeof(VerifiedKeyEvent); |
| 3350 | break; |
| 3351 | } |
| 3352 | case VerifiedInputEvent::Type::MOTION: { |
| 3353 | size = sizeof(VerifiedMotionEvent); |
| 3354 | break; |
| 3355 | } |
| 3356 | } |
| 3357 | const uint8_t* start = reinterpret_cast<const uint8_t*>(&event); |
| 3358 | return mHmacKeyManager.sign(start, size); |
| 3359 | } |
| 3360 | |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3361 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3362 | const MotionEntry& motionEntry, const DispatchEntry& dispatchEntry) const { |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame^] | 3363 | const int32_t actionMasked = dispatchEntry.resolvedAction & AMOTION_EVENT_ACTION_MASK; |
| 3364 | if (actionMasked != AMOTION_EVENT_ACTION_UP && actionMasked != AMOTION_EVENT_ACTION_DOWN) { |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3365 | // Only sign events up and down events as the purely move events |
| 3366 | // are tied to their up/down counterparts so signing would be redundant. |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame^] | 3367 | return INVALID_HMAC; |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3368 | } |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame^] | 3369 | |
| 3370 | VerifiedMotionEvent verifiedEvent = |
| 3371 | verifiedMotionEventFromMotionEntry(motionEntry, dispatchEntry.rawTransform); |
| 3372 | verifiedEvent.actionMasked = actionMasked; |
| 3373 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS; |
| 3374 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3375 | } |
| 3376 | |
| 3377 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3378 | const KeyEntry& keyEntry, const DispatchEntry& dispatchEntry) const { |
| 3379 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(keyEntry); |
| 3380 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_KEY_EVENT_FLAGS; |
| 3381 | verifiedEvent.action = dispatchEntry.resolvedAction; |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3382 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3383 | } |
| 3384 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3385 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3386 | const sp<Connection>& connection, uint32_t seq, |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 3387 | bool handled, nsecs_t consumeTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3388 | if (DEBUG_DISPATCH_CYCLE) { |
| 3389 | ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s", |
| 3390 | connection->getInputChannelName().c_str(), seq, toString(handled)); |
| 3391 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3392 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3393 | if (connection->status == Connection::STATUS_BROKEN || |
| 3394 | connection->status == Connection::STATUS_ZOMBIE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3395 | return; |
| 3396 | } |
| 3397 | |
| 3398 | // Notify other system components and prepare to start the next dispatch cycle. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3399 | auto command = [this, currentTime, connection, seq, handled, consumeTime]() REQUIRES(mLock) { |
| 3400 | doDispatchCycleFinishedCommand(currentTime, connection, seq, handled, consumeTime); |
| 3401 | }; |
| 3402 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3403 | } |
| 3404 | |
| 3405 | void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3406 | const sp<Connection>& connection, |
| 3407 | bool notify) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3408 | if (DEBUG_DISPATCH_CYCLE) { |
| 3409 | ALOGD("channel '%s' ~ abortBrokenDispatchCycle - notify=%s", |
| 3410 | connection->getInputChannelName().c_str(), toString(notify)); |
| 3411 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3412 | |
| 3413 | // Clear the dispatch queues. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3414 | drainDispatchQueue(connection->outboundQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3415 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3416 | drainDispatchQueue(connection->waitQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3417 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3418 | |
| 3419 | // The connection appears to be unrecoverably broken. |
| 3420 | // Ignore already broken or zombie connections. |
| 3421 | if (connection->status == Connection::STATUS_NORMAL) { |
| 3422 | connection->status = Connection::STATUS_BROKEN; |
| 3423 | |
| 3424 | if (notify) { |
| 3425 | // Notify other system components. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3426 | ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", |
| 3427 | connection->getInputChannelName().c_str()); |
| 3428 | |
| 3429 | auto command = [this, connection]() REQUIRES(mLock) { |
| 3430 | if (connection->status == Connection::STATUS_ZOMBIE) return; |
| 3431 | scoped_unlock unlock(mLock); |
| 3432 | mPolicy->notifyInputChannelBroken(connection->inputChannel->getConnectionToken()); |
| 3433 | }; |
| 3434 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3435 | } |
| 3436 | } |
| 3437 | } |
| 3438 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3439 | void InputDispatcher::drainDispatchQueue(std::deque<DispatchEntry*>& queue) { |
| 3440 | while (!queue.empty()) { |
| 3441 | DispatchEntry* dispatchEntry = queue.front(); |
| 3442 | queue.pop_front(); |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3443 | releaseDispatchEntry(dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3444 | } |
| 3445 | } |
| 3446 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3447 | void InputDispatcher::releaseDispatchEntry(DispatchEntry* dispatchEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3448 | if (dispatchEntry->hasForegroundTarget()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3449 | decrementPendingForegroundDispatches(*(dispatchEntry->eventEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3450 | } |
| 3451 | delete dispatchEntry; |
| 3452 | } |
| 3453 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3454 | int InputDispatcher::handleReceiveCallback(int events, sp<IBinder> connectionToken) { |
| 3455 | std::scoped_lock _l(mLock); |
| 3456 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 3457 | if (connection == nullptr) { |
| 3458 | ALOGW("Received looper callback for unknown input channel token %p. events=0x%x", |
| 3459 | connectionToken.get(), events); |
| 3460 | return 0; // remove the callback |
| 3461 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3462 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3463 | bool notify; |
| 3464 | if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { |
| 3465 | if (!(events & ALOOPER_EVENT_INPUT)) { |
| 3466 | ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. " |
| 3467 | "events=0x%x", |
| 3468 | connection->getInputChannelName().c_str(), events); |
| 3469 | return 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3470 | } |
| 3471 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3472 | nsecs_t currentTime = now(); |
| 3473 | bool gotOne = false; |
| 3474 | status_t status = OK; |
| 3475 | for (;;) { |
| 3476 | Result<InputPublisher::ConsumerResponse> result = |
| 3477 | connection->inputPublisher.receiveConsumerResponse(); |
| 3478 | if (!result.ok()) { |
| 3479 | status = result.error().code(); |
| 3480 | break; |
| 3481 | } |
| 3482 | |
| 3483 | if (std::holds_alternative<InputPublisher::Finished>(*result)) { |
| 3484 | const InputPublisher::Finished& finish = |
| 3485 | std::get<InputPublisher::Finished>(*result); |
| 3486 | finishDispatchCycleLocked(currentTime, connection, finish.seq, finish.handled, |
| 3487 | finish.consumeTime); |
| 3488 | } else if (std::holds_alternative<InputPublisher::Timeline>(*result)) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3489 | if (shouldReportMetricsForConnection(*connection)) { |
| 3490 | const InputPublisher::Timeline& timeline = |
| 3491 | std::get<InputPublisher::Timeline>(*result); |
| 3492 | mLatencyTracker |
| 3493 | .trackGraphicsLatency(timeline.inputEventId, |
| 3494 | connection->inputChannel->getConnectionToken(), |
| 3495 | std::move(timeline.graphicsTimeline)); |
| 3496 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3497 | } |
| 3498 | gotOne = true; |
| 3499 | } |
| 3500 | if (gotOne) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3501 | runCommandsLockedInterruptable(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3502 | if (status == WOULD_BLOCK) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3503 | return 1; |
| 3504 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3505 | } |
| 3506 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3507 | notify = status != DEAD_OBJECT || !connection->monitor; |
| 3508 | if (notify) { |
| 3509 | ALOGE("channel '%s' ~ Failed to receive finished signal. status=%s(%d)", |
| 3510 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3511 | status); |
| 3512 | } |
| 3513 | } else { |
| 3514 | // Monitor channels are never explicitly unregistered. |
| 3515 | // We do it automatically when the remote endpoint is closed so don't warn about them. |
| 3516 | const bool stillHaveWindowHandle = |
| 3517 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()) != nullptr; |
| 3518 | notify = !connection->monitor && stillHaveWindowHandle; |
| 3519 | if (notify) { |
| 3520 | ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred. events=0x%x", |
| 3521 | connection->getInputChannelName().c_str(), events); |
| 3522 | } |
| 3523 | } |
| 3524 | |
| 3525 | // Remove the channel. |
| 3526 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), notify); |
| 3527 | return 0; // remove the callback |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3528 | } |
| 3529 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3530 | void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3531 | const CancelationOptions& options) { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3532 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 3533 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3534 | } |
| 3535 | } |
| 3536 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3537 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3538 | const CancelationOptions& options) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3539 | synthesizeCancelationEventsForMonitorsLocked(options, mGlobalMonitorsByDisplay); |
| 3540 | synthesizeCancelationEventsForMonitorsLocked(options, mGestureMonitorsByDisplay); |
| 3541 | } |
| 3542 | |
| 3543 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
| 3544 | const CancelationOptions& options, |
| 3545 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { |
| 3546 | for (const auto& it : monitorsByDisplay) { |
| 3547 | const std::vector<Monitor>& monitors = it.second; |
| 3548 | for (const Monitor& monitor : monitors) { |
| 3549 | synthesizeCancelationEventsForInputChannelLocked(monitor.inputChannel, options); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3550 | } |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3551 | } |
| 3552 | } |
| 3553 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3554 | void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked( |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 3555 | const std::shared_ptr<InputChannel>& channel, const CancelationOptions& options) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 3556 | sp<Connection> connection = getConnectionLocked(channel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3557 | if (connection == nullptr) { |
| 3558 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3559 | } |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3560 | |
| 3561 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3562 | } |
| 3563 | |
| 3564 | void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( |
| 3565 | const sp<Connection>& connection, const CancelationOptions& options) { |
| 3566 | if (connection->status == Connection::STATUS_BROKEN) { |
| 3567 | return; |
| 3568 | } |
| 3569 | |
| 3570 | nsecs_t currentTime = now(); |
| 3571 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3572 | std::vector<std::unique_ptr<EventEntry>> cancelationEvents = |
Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 3573 | connection->inputState.synthesizeCancelationEvents(currentTime, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3574 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3575 | if (cancelationEvents.empty()) { |
| 3576 | return; |
| 3577 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3578 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 3579 | ALOGD("channel '%s' ~ Synthesized %zu cancelation events to bring channel back in sync " |
| 3580 | "with reality: %s, mode=%d.", |
| 3581 | connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason, |
| 3582 | options.mode); |
| 3583 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3584 | |
| 3585 | InputTarget target; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3586 | sp<WindowInfoHandle> windowHandle = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3587 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3588 | if (windowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3589 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3590 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3591 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3592 | } |
| 3593 | target.inputChannel = connection->inputChannel; |
| 3594 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 3595 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3596 | for (size_t i = 0; i < cancelationEvents.size(); i++) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3597 | std::unique_ptr<EventEntry> cancelationEventEntry = std::move(cancelationEvents[i]); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3598 | switch (cancelationEventEntry->type) { |
| 3599 | case EventEntry::Type::KEY: { |
| 3600 | logOutboundKeyDetails("cancel - ", |
| 3601 | static_cast<const KeyEntry&>(*cancelationEventEntry)); |
| 3602 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3603 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3604 | case EventEntry::Type::MOTION: { |
| 3605 | logOutboundMotionDetails("cancel - ", |
| 3606 | static_cast<const MotionEntry&>(*cancelationEventEntry)); |
| 3607 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3608 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3609 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3610 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3611 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3612 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3613 | LOG_ALWAYS_FATAL("Canceling %s events is not supported", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3614 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3615 | break; |
| 3616 | } |
| 3617 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3618 | case EventEntry::Type::DEVICE_RESET: |
| 3619 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3620 | LOG_ALWAYS_FATAL("%s event should not be found inside Connections's queue", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3621 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3622 | break; |
| 3623 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3624 | } |
| 3625 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3626 | enqueueDispatchEntryLocked(connection, std::move(cancelationEventEntry), target, |
| 3627 | InputTarget::FLAG_DISPATCH_AS_IS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3628 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3629 | |
| 3630 | startDispatchCycleLocked(currentTime, connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3631 | } |
| 3632 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3633 | void InputDispatcher::synthesizePointerDownEventsForConnectionLocked( |
| 3634 | const sp<Connection>& connection) { |
| 3635 | if (connection->status == Connection::STATUS_BROKEN) { |
| 3636 | return; |
| 3637 | } |
| 3638 | |
| 3639 | nsecs_t currentTime = now(); |
| 3640 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3641 | std::vector<std::unique_ptr<EventEntry>> downEvents = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3642 | connection->inputState.synthesizePointerDownEvents(currentTime); |
| 3643 | |
| 3644 | if (downEvents.empty()) { |
| 3645 | return; |
| 3646 | } |
| 3647 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3648 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3649 | ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.", |
| 3650 | connection->getInputChannelName().c_str(), downEvents.size()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3651 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3652 | |
| 3653 | InputTarget target; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3654 | sp<WindowInfoHandle> windowHandle = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3655 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3656 | if (windowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3657 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3658 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3659 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3660 | } |
| 3661 | target.inputChannel = connection->inputChannel; |
| 3662 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 3663 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3664 | for (std::unique_ptr<EventEntry>& downEventEntry : downEvents) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3665 | switch (downEventEntry->type) { |
| 3666 | case EventEntry::Type::MOTION: { |
| 3667 | logOutboundMotionDetails("down - ", |
| 3668 | static_cast<const MotionEntry&>(*downEventEntry)); |
| 3669 | break; |
| 3670 | } |
| 3671 | |
| 3672 | case EventEntry::Type::KEY: |
| 3673 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3674 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3675 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3676 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3677 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3678 | case EventEntry::Type::SENSOR: |
| 3679 | case EventEntry::Type::DRAG: { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3680 | LOG_ALWAYS_FATAL("%s event should not be found inside Connections's queue", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3681 | ftl::enum_string(downEventEntry->type).c_str()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3682 | break; |
| 3683 | } |
| 3684 | } |
| 3685 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3686 | enqueueDispatchEntryLocked(connection, std::move(downEventEntry), target, |
| 3687 | InputTarget::FLAG_DISPATCH_AS_IS); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3688 | } |
| 3689 | |
| 3690 | startDispatchCycleLocked(currentTime, connection); |
| 3691 | } |
| 3692 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3693 | std::unique_ptr<MotionEntry> InputDispatcher::splitMotionEvent( |
| 3694 | const MotionEntry& originalMotionEntry, BitSet32 pointerIds) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3695 | ALOG_ASSERT(pointerIds.value != 0); |
| 3696 | |
| 3697 | uint32_t splitPointerIndexMap[MAX_POINTERS]; |
| 3698 | PointerProperties splitPointerProperties[MAX_POINTERS]; |
| 3699 | PointerCoords splitPointerCoords[MAX_POINTERS]; |
| 3700 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3701 | uint32_t originalPointerCount = originalMotionEntry.pointerCount; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3702 | uint32_t splitPointerCount = 0; |
| 3703 | |
| 3704 | for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3705 | originalPointerIndex++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3706 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3707 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3708 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3709 | if (pointerIds.hasBit(pointerId)) { |
| 3710 | splitPointerIndexMap[splitPointerCount] = originalPointerIndex; |
| 3711 | splitPointerProperties[splitPointerCount].copyFrom(pointerProperties); |
| 3712 | splitPointerCoords[splitPointerCount].copyFrom( |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3713 | originalMotionEntry.pointerCoords[originalPointerIndex]); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3714 | splitPointerCount += 1; |
| 3715 | } |
| 3716 | } |
| 3717 | |
| 3718 | if (splitPointerCount != pointerIds.count()) { |
| 3719 | // This is bad. We are missing some of the pointers that we expected to deliver. |
| 3720 | // Most likely this indicates that we received an ACTION_MOVE events that has |
| 3721 | // different pointer ids than we expected based on the previous ACTION_DOWN |
| 3722 | // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers |
| 3723 | // in this way. |
| 3724 | ALOGW("Dropping split motion event because the pointer count is %d but " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3725 | "we expected there to be %d pointers. This probably means we received " |
| 3726 | "a broken sequence of pointer ids from the input device.", |
| 3727 | splitPointerCount, pointerIds.count()); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3728 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3729 | } |
| 3730 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3731 | int32_t action = originalMotionEntry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3732 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3733 | if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN || |
| 3734 | maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3735 | int32_t originalPointerIndex = getMotionEventActionPointerIndex(action); |
| 3736 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3737 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3738 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3739 | if (pointerIds.hasBit(pointerId)) { |
| 3740 | if (pointerIds.count() == 1) { |
| 3741 | // The first/last pointer went down/up. |
| 3742 | action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3743 | ? AMOTION_EVENT_ACTION_DOWN |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3744 | : (originalMotionEntry.flags & AMOTION_EVENT_FLAG_CANCELED) != 0 |
| 3745 | ? AMOTION_EVENT_ACTION_CANCEL |
| 3746 | : AMOTION_EVENT_ACTION_UP; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3747 | } else { |
| 3748 | // A secondary pointer went down/up. |
| 3749 | uint32_t splitPointerIndex = 0; |
| 3750 | while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { |
| 3751 | splitPointerIndex += 1; |
| 3752 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3753 | action = maskedAction | |
| 3754 | (splitPointerIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3755 | } |
| 3756 | } else { |
| 3757 | // An unrelated pointer changed. |
| 3758 | action = AMOTION_EVENT_ACTION_MOVE; |
| 3759 | } |
| 3760 | } |
| 3761 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3762 | int32_t newId = mIdGenerator.nextId(); |
| 3763 | if (ATRACE_ENABLED()) { |
| 3764 | std::string message = StringPrintf("Split MotionEvent(id=0x%" PRIx32 |
| 3765 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3766 | originalMotionEntry.id, newId); |
| 3767 | ATRACE_NAME(message.c_str()); |
| 3768 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3769 | std::unique_ptr<MotionEntry> splitMotionEntry = |
| 3770 | std::make_unique<MotionEntry>(newId, originalMotionEntry.eventTime, |
| 3771 | originalMotionEntry.deviceId, originalMotionEntry.source, |
| 3772 | originalMotionEntry.displayId, |
| 3773 | originalMotionEntry.policyFlags, action, |
| 3774 | originalMotionEntry.actionButton, |
| 3775 | originalMotionEntry.flags, originalMotionEntry.metaState, |
| 3776 | originalMotionEntry.buttonState, |
| 3777 | originalMotionEntry.classification, |
| 3778 | originalMotionEntry.edgeFlags, |
| 3779 | originalMotionEntry.xPrecision, |
| 3780 | originalMotionEntry.yPrecision, |
| 3781 | originalMotionEntry.xCursorPosition, |
| 3782 | originalMotionEntry.yCursorPosition, |
| 3783 | originalMotionEntry.downTime, splitPointerCount, |
| 3784 | splitPointerProperties, splitPointerCoords, 0, 0); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3785 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3786 | if (originalMotionEntry.injectionState) { |
| 3787 | splitMotionEntry->injectionState = originalMotionEntry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3788 | splitMotionEntry->injectionState->refCount += 1; |
| 3789 | } |
| 3790 | |
| 3791 | return splitMotionEntry; |
| 3792 | } |
| 3793 | |
| 3794 | void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3795 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 3796 | ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args->eventTime); |
| 3797 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3798 | |
| 3799 | bool needWake; |
| 3800 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3801 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3802 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3803 | std::unique_ptr<ConfigurationChangedEntry> newEntry = |
| 3804 | std::make_unique<ConfigurationChangedEntry>(args->id, args->eventTime); |
| 3805 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3806 | } // release lock |
| 3807 | |
| 3808 | if (needWake) { |
| 3809 | mLooper->wake(); |
| 3810 | } |
| 3811 | } |
| 3812 | |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3813 | /** |
| 3814 | * If one of the meta shortcuts is detected, process them here: |
| 3815 | * Meta + Backspace -> generate BACK |
| 3816 | * Meta + Enter -> generate HOME |
| 3817 | * This will potentially overwrite keyCode and metaState. |
| 3818 | */ |
| 3819 | void InputDispatcher::accelerateMetaShortcuts(const int32_t deviceId, const int32_t action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3820 | int32_t& keyCode, int32_t& metaState) { |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3821 | if (metaState & AMETA_META_ON && action == AKEY_EVENT_ACTION_DOWN) { |
| 3822 | int32_t newKeyCode = AKEYCODE_UNKNOWN; |
| 3823 | if (keyCode == AKEYCODE_DEL) { |
| 3824 | newKeyCode = AKEYCODE_BACK; |
| 3825 | } else if (keyCode == AKEYCODE_ENTER) { |
| 3826 | newKeyCode = AKEYCODE_HOME; |
| 3827 | } |
| 3828 | if (newKeyCode != AKEYCODE_UNKNOWN) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3829 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3830 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3831 | mReplacedKeys[replacement] = newKeyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3832 | keyCode = newKeyCode; |
| 3833 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3834 | } |
| 3835 | } else if (action == AKEY_EVENT_ACTION_UP) { |
| 3836 | // In order to maintain a consistent stream of up and down events, check to see if the key |
| 3837 | // going up is one we've replaced in a down event and haven't yet replaced in an up event, |
| 3838 | // 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] | 3839 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3840 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3841 | auto replacementIt = mReplacedKeys.find(replacement); |
| 3842 | if (replacementIt != mReplacedKeys.end()) { |
| 3843 | keyCode = replacementIt->second; |
| 3844 | mReplacedKeys.erase(replacementIt); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3845 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3846 | } |
| 3847 | } |
| 3848 | } |
| 3849 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3850 | void InputDispatcher::notifyKey(const NotifyKeyArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3851 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 3852 | ALOGD("notifyKey - eventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
| 3853 | "policyFlags=0x%x, action=0x%x, " |
| 3854 | "flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, downTime=%" PRId64, |
| 3855 | args->eventTime, args->deviceId, args->source, args->displayId, args->policyFlags, |
| 3856 | args->action, args->flags, args->keyCode, args->scanCode, args->metaState, |
| 3857 | args->downTime); |
| 3858 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3859 | if (!validateKeyEvent(args->action)) { |
| 3860 | return; |
| 3861 | } |
| 3862 | |
| 3863 | uint32_t policyFlags = args->policyFlags; |
| 3864 | int32_t flags = args->flags; |
| 3865 | int32_t metaState = args->metaState; |
Siarhei Vishniakou | 622bd32 | 2018-10-29 18:02:27 -0700 | [diff] [blame] | 3866 | // InputDispatcher tracks and generates key repeats on behalf of |
| 3867 | // whatever notifies it, so repeatCount should always be set to 0 |
| 3868 | constexpr int32_t repeatCount = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3869 | if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { |
| 3870 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 3871 | flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
| 3872 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3873 | if (policyFlags & POLICY_FLAG_FUNCTION) { |
| 3874 | metaState |= AMETA_FUNCTION_ON; |
| 3875 | } |
| 3876 | |
| 3877 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 3878 | |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3879 | int32_t keyCode = args->keyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3880 | accelerateMetaShortcuts(args->deviceId, args->action, keyCode, metaState); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3881 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3882 | KeyEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3883 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3884 | args->action, flags, keyCode, args->scanCode, metaState, repeatCount, |
| 3885 | args->downTime, args->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3886 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3887 | android::base::Timer t; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3888 | mPolicy->interceptKeyBeforeQueueing(&event, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3889 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3890 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3891 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3892 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3893 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3894 | bool needWake; |
| 3895 | { // acquire lock |
| 3896 | mLock.lock(); |
| 3897 | |
| 3898 | if (shouldSendKeyToInputFilterLocked(args)) { |
| 3899 | mLock.unlock(); |
| 3900 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 3901 | policyFlags |= POLICY_FLAG_FILTERED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3902 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 3903 | return; // event was consumed by the filter |
| 3904 | } |
| 3905 | |
| 3906 | mLock.lock(); |
| 3907 | } |
| 3908 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3909 | std::unique_ptr<KeyEntry> newEntry = |
| 3910 | std::make_unique<KeyEntry>(args->id, args->eventTime, args->deviceId, args->source, |
| 3911 | args->displayId, policyFlags, args->action, flags, |
| 3912 | keyCode, args->scanCode, metaState, repeatCount, |
| 3913 | args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3914 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3915 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3916 | mLock.unlock(); |
| 3917 | } // release lock |
| 3918 | |
| 3919 | if (needWake) { |
| 3920 | mLooper->wake(); |
| 3921 | } |
| 3922 | } |
| 3923 | |
| 3924 | bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) { |
| 3925 | return mInputFilterEnabled; |
| 3926 | } |
| 3927 | |
| 3928 | void InputDispatcher::notifyMotion(const NotifyMotionArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3929 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 3930 | ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 3931 | "displayId=%" PRId32 ", policyFlags=0x%x, " |
| 3932 | "action=0x%x, actionButton=0x%x, flags=0x%x, metaState=0x%x, buttonState=0x%x, " |
| 3933 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, xCursorPosition=%f, " |
| 3934 | "yCursorPosition=%f, downTime=%" PRId64, |
| 3935 | args->id, args->eventTime, args->deviceId, args->source, args->displayId, |
| 3936 | args->policyFlags, args->action, args->actionButton, args->flags, args->metaState, |
| 3937 | args->buttonState, args->edgeFlags, args->xPrecision, args->yPrecision, |
| 3938 | args->xCursorPosition, args->yCursorPosition, args->downTime); |
| 3939 | for (uint32_t i = 0; i < args->pointerCount; i++) { |
| 3940 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
| 3941 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 3942 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 3943 | "orientation=%f", |
| 3944 | i, args->pointerProperties[i].id, args->pointerProperties[i].toolType, |
| 3945 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 3946 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 3947 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 3948 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 3949 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 3950 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 3951 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 3952 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 3953 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| 3954 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3955 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3956 | if (!validateMotionEvent(args->action, args->actionButton, args->pointerCount, |
| 3957 | args->pointerProperties)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3958 | return; |
| 3959 | } |
| 3960 | |
| 3961 | uint32_t policyFlags = args->policyFlags; |
| 3962 | policyFlags |= POLICY_FLAG_TRUSTED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3963 | |
| 3964 | android::base::Timer t; |
Charles Chen | 3611f1f | 2019-01-29 17:26:18 +0800 | [diff] [blame] | 3965 | mPolicy->interceptMotionBeforeQueueing(args->displayId, args->eventTime, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3966 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3967 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3968 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3969 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3970 | |
| 3971 | bool needWake; |
| 3972 | { // acquire lock |
| 3973 | mLock.lock(); |
| 3974 | |
| 3975 | if (shouldSendMotionToInputFilterLocked(args)) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 3976 | ui::Transform displayTransform; |
| 3977 | if (const auto it = mDisplayInfos.find(args->displayId); it != mDisplayInfos.end()) { |
| 3978 | displayTransform = it->second.transform; |
| 3979 | } |
| 3980 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3981 | mLock.unlock(); |
| 3982 | |
| 3983 | MotionEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3984 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
| 3985 | args->action, args->actionButton, args->flags, args->edgeFlags, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 3986 | args->metaState, args->buttonState, args->classification, |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 3987 | displayTransform, args->xPrecision, args->yPrecision, |
| 3988 | args->xCursorPosition, args->yCursorPosition, displayTransform, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 3989 | args->downTime, args->eventTime, args->pointerCount, |
| 3990 | args->pointerProperties, args->pointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3991 | |
| 3992 | policyFlags |= POLICY_FLAG_FILTERED; |
| 3993 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 3994 | return; // event was consumed by the filter |
| 3995 | } |
| 3996 | |
| 3997 | mLock.lock(); |
| 3998 | } |
| 3999 | |
| 4000 | // Just enqueue a new motion event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4001 | std::unique_ptr<MotionEntry> newEntry = |
| 4002 | std::make_unique<MotionEntry>(args->id, args->eventTime, args->deviceId, |
| 4003 | args->source, args->displayId, policyFlags, |
| 4004 | args->action, args->actionButton, args->flags, |
| 4005 | args->metaState, args->buttonState, |
| 4006 | args->classification, args->edgeFlags, |
| 4007 | args->xPrecision, args->yPrecision, |
| 4008 | args->xCursorPosition, args->yCursorPosition, |
| 4009 | args->downTime, args->pointerCount, |
| 4010 | args->pointerProperties, args->pointerCoords, 0, 0); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4011 | |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 4012 | if (args->id != android::os::IInputConstants::INVALID_INPUT_EVENT_ID && |
| 4013 | IdGenerator::getSource(args->id) == IdGenerator::Source::INPUT_READER && |
| 4014 | !mInputFilterEnabled) { |
| 4015 | const bool isDown = args->action == AMOTION_EVENT_ACTION_DOWN; |
| 4016 | mLatencyTracker.trackListener(args->id, isDown, args->eventTime, args->readTime); |
| 4017 | } |
| 4018 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4019 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4020 | mLock.unlock(); |
| 4021 | } // release lock |
| 4022 | |
| 4023 | if (needWake) { |
| 4024 | mLooper->wake(); |
| 4025 | } |
| 4026 | } |
| 4027 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4028 | void InputDispatcher::notifySensor(const NotifySensorArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4029 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4030 | ALOGD("notifySensor - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 4031 | " sensorType=%s", |
| 4032 | args->id, args->eventTime, args->deviceId, args->source, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 4033 | ftl::enum_string(args->sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4034 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4035 | |
| 4036 | bool needWake; |
| 4037 | { // acquire lock |
| 4038 | mLock.lock(); |
| 4039 | |
| 4040 | // Just enqueue a new sensor event. |
| 4041 | std::unique_ptr<SensorEntry> newEntry = |
| 4042 | std::make_unique<SensorEntry>(args->id, args->eventTime, args->deviceId, |
| 4043 | args->source, 0 /* policyFlags*/, args->hwTimestamp, |
| 4044 | args->sensorType, args->accuracy, |
| 4045 | args->accuracyChanged, args->values); |
| 4046 | |
| 4047 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
| 4048 | mLock.unlock(); |
| 4049 | } // release lock |
| 4050 | |
| 4051 | if (needWake) { |
| 4052 | mLooper->wake(); |
| 4053 | } |
| 4054 | } |
| 4055 | |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 4056 | void InputDispatcher::notifyVibratorState(const NotifyVibratorStateArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4057 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4058 | ALOGD("notifyVibratorState - eventTime=%" PRId64 ", device=%d, isOn=%d", args->eventTime, |
| 4059 | args->deviceId, args->isOn); |
| 4060 | } |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 4061 | mPolicy->notifyVibratorState(args->deviceId, args->isOn); |
| 4062 | } |
| 4063 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4064 | bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4065 | return mInputFilterEnabled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4066 | } |
| 4067 | |
| 4068 | void InputDispatcher::notifySwitch(const NotifySwitchArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4069 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4070 | ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, " |
| 4071 | "switchMask=0x%08x", |
| 4072 | args->eventTime, args->policyFlags, args->switchValues, args->switchMask); |
| 4073 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4074 | |
| 4075 | uint32_t policyFlags = args->policyFlags; |
| 4076 | policyFlags |= POLICY_FLAG_TRUSTED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4077 | mPolicy->notifySwitch(args->eventTime, args->switchValues, args->switchMask, policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4078 | } |
| 4079 | |
| 4080 | void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4081 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4082 | ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args->eventTime, |
| 4083 | args->deviceId); |
| 4084 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4085 | |
| 4086 | bool needWake; |
| 4087 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4088 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4089 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4090 | std::unique_ptr<DeviceResetEntry> newEntry = |
| 4091 | std::make_unique<DeviceResetEntry>(args->id, args->eventTime, args->deviceId); |
| 4092 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4093 | } // release lock |
| 4094 | |
| 4095 | if (needWake) { |
| 4096 | mLooper->wake(); |
| 4097 | } |
| 4098 | } |
| 4099 | |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4100 | void InputDispatcher::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4101 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4102 | ALOGD("notifyPointerCaptureChanged - eventTime=%" PRId64 ", enabled=%s", args->eventTime, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4103 | args->request.enable ? "true" : "false"); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4104 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4105 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4106 | bool needWake; |
| 4107 | { // acquire lock |
| 4108 | std::scoped_lock _l(mLock); |
| 4109 | auto entry = std::make_unique<PointerCaptureChangedEntry>(args->id, args->eventTime, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4110 | args->request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4111 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 4112 | } // release lock |
| 4113 | |
| 4114 | if (needWake) { |
| 4115 | mLooper->wake(); |
| 4116 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4117 | } |
| 4118 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4119 | InputEventInjectionResult InputDispatcher::injectInputEvent( |
| 4120 | const InputEvent* event, int32_t injectorPid, int32_t injectorUid, |
| 4121 | InputEventInjectionSync syncMode, std::chrono::milliseconds timeout, uint32_t policyFlags) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4122 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4123 | ALOGD("injectInputEvent - eventType=%d, injectorPid=%d, injectorUid=%d, " |
| 4124 | "syncMode=%d, timeout=%lld, policyFlags=0x%08x", |
| 4125 | event->getType(), injectorPid, injectorUid, syncMode, timeout.count(), policyFlags); |
| 4126 | } |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 4127 | 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] | 4128 | |
| 4129 | policyFlags |= POLICY_FLAG_INJECTED; |
| 4130 | if (hasInjectionPermission(injectorPid, injectorUid)) { |
| 4131 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 4132 | } |
| 4133 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4134 | // 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] | 4135 | // that have gone through the InputFilter. If the event passed through the InputFilter, assign |
| 4136 | // the provided device id. If the InputFilter is accessibility, and it modifies or synthesizes |
| 4137 | // the injected event, it is responsible for setting POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY. |
| 4138 | // For those events, we will set FLAG_IS_ACCESSIBILITY_EVENT to allow apps to distinguish them |
| 4139 | // from events that originate from actual hardware. |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4140 | int32_t resolvedDeviceId = VIRTUAL_KEYBOARD_ID; |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4141 | if (policyFlags & POLICY_FLAG_FILTERED) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4142 | resolvedDeviceId = event->getDeviceId(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4143 | } |
| 4144 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4145 | std::queue<std::unique_ptr<EventEntry>> injectedEntries; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4146 | switch (event->getType()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4147 | case AINPUT_EVENT_TYPE_KEY: { |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4148 | const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event); |
| 4149 | int32_t action = incomingKey.getAction(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4150 | if (!validateKeyEvent(action)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4151 | return InputEventInjectionResult::FAILED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4152 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4153 | |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4154 | int32_t flags = incomingKey.getFlags(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4155 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4156 | flags |= AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4157 | } |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4158 | int32_t keyCode = incomingKey.getKeyCode(); |
| 4159 | int32_t metaState = incomingKey.getMetaState(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4160 | accelerateMetaShortcuts(resolvedDeviceId, action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4161 | /*byref*/ keyCode, /*byref*/ metaState); |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4162 | KeyEvent keyEvent; |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4163 | keyEvent.initialize(incomingKey.getId(), resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4164 | incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode, |
| 4165 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), |
| 4166 | incomingKey.getDownTime(), incomingKey.getEventTime()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4167 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4168 | if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) { |
| 4169 | policyFlags |= POLICY_FLAG_VIRTUAL; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4170 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4171 | |
| 4172 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 4173 | android::base::Timer t; |
| 4174 | mPolicy->interceptKeyBeforeQueueing(&keyEvent, /*byref*/ policyFlags); |
| 4175 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4176 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
| 4177 | std::to_string(t.duration().count()).c_str()); |
| 4178 | } |
| 4179 | } |
| 4180 | |
| 4181 | mLock.lock(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4182 | std::unique_ptr<KeyEntry> injectedEntry = |
| 4183 | std::make_unique<KeyEntry>(incomingKey.getId(), incomingKey.getEventTime(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4184 | resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4185 | incomingKey.getDisplayId(), policyFlags, action, |
| 4186 | flags, keyCode, incomingKey.getScanCode(), metaState, |
| 4187 | incomingKey.getRepeatCount(), |
| 4188 | incomingKey.getDownTime()); |
| 4189 | injectedEntries.push(std::move(injectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4190 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4191 | } |
| 4192 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4193 | case AINPUT_EVENT_TYPE_MOTION: { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4194 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
| 4195 | int32_t action = motionEvent.getAction(); |
| 4196 | size_t pointerCount = motionEvent.getPointerCount(); |
| 4197 | const PointerProperties* pointerProperties = motionEvent.getPointerProperties(); |
| 4198 | int32_t actionButton = motionEvent.getActionButton(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4199 | int32_t flags = motionEvent.getFlags(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4200 | int32_t displayId = motionEvent.getDisplayId(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4201 | if (!validateMotionEvent(action, actionButton, pointerCount, pointerProperties)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4202 | return InputEventInjectionResult::FAILED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4203 | } |
| 4204 | |
| 4205 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4206 | nsecs_t eventTime = motionEvent.getEventTime(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4207 | android::base::Timer t; |
| 4208 | mPolicy->interceptMotionBeforeQueueing(displayId, eventTime, /*byref*/ policyFlags); |
| 4209 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4210 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
| 4211 | std::to_string(t.duration().count()).c_str()); |
| 4212 | } |
| 4213 | } |
| 4214 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4215 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4216 | flags |= AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4217 | } |
| 4218 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4219 | mLock.lock(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4220 | const nsecs_t* sampleEventTimes = motionEvent.getSampleEventTimes(); |
| 4221 | const PointerCoords* samplePointerCoords = motionEvent.getSamplePointerCoords(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4222 | std::unique_ptr<MotionEntry> injectedEntry = |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4223 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, |
| 4224 | resolvedDeviceId, motionEvent.getSource(), |
| 4225 | motionEvent.getDisplayId(), policyFlags, action, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4226 | actionButton, flags, motionEvent.getMetaState(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4227 | motionEvent.getButtonState(), |
| 4228 | motionEvent.getClassification(), |
| 4229 | motionEvent.getEdgeFlags(), |
| 4230 | motionEvent.getXPrecision(), |
| 4231 | motionEvent.getYPrecision(), |
| 4232 | motionEvent.getRawXCursorPosition(), |
| 4233 | motionEvent.getRawYCursorPosition(), |
| 4234 | motionEvent.getDownTime(), uint32_t(pointerCount), |
| 4235 | pointerProperties, samplePointerCoords, |
| 4236 | motionEvent.getXOffset(), |
| 4237 | motionEvent.getYOffset()); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4238 | transformMotionEntryForInjectionLocked(*injectedEntry); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4239 | injectedEntries.push(std::move(injectedEntry)); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4240 | for (size_t i = motionEvent.getHistorySize(); i > 0; i--) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4241 | sampleEventTimes += 1; |
| 4242 | samplePointerCoords += pointerCount; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4243 | std::unique_ptr<MotionEntry> nextInjectedEntry = |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4244 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, |
| 4245 | resolvedDeviceId, motionEvent.getSource(), |
| 4246 | motionEvent.getDisplayId(), policyFlags, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4247 | action, actionButton, flags, |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4248 | motionEvent.getMetaState(), |
| 4249 | motionEvent.getButtonState(), |
| 4250 | motionEvent.getClassification(), |
| 4251 | motionEvent.getEdgeFlags(), |
| 4252 | motionEvent.getXPrecision(), |
| 4253 | motionEvent.getYPrecision(), |
| 4254 | motionEvent.getRawXCursorPosition(), |
| 4255 | motionEvent.getRawYCursorPosition(), |
| 4256 | motionEvent.getDownTime(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4257 | uint32_t(pointerCount), pointerProperties, |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4258 | samplePointerCoords, motionEvent.getXOffset(), |
| 4259 | motionEvent.getYOffset()); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4260 | transformMotionEntryForInjectionLocked(*nextInjectedEntry); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4261 | injectedEntries.push(std::move(nextInjectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4262 | } |
| 4263 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4264 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4265 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4266 | default: |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 4267 | ALOGW("Cannot inject %s events", inputEventTypeToString(event->getType())); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4268 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4269 | } |
| 4270 | |
| 4271 | InjectionState* injectionState = new InjectionState(injectorPid, injectorUid); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4272 | if (syncMode == InputEventInjectionSync::NONE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4273 | injectionState->injectionIsAsync = true; |
| 4274 | } |
| 4275 | |
| 4276 | injectionState->refCount += 1; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4277 | injectedEntries.back()->injectionState = injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4278 | |
| 4279 | bool needWake = false; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4280 | while (!injectedEntries.empty()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4281 | needWake |= enqueueInboundEventLocked(std::move(injectedEntries.front())); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4282 | injectedEntries.pop(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4283 | } |
| 4284 | |
| 4285 | mLock.unlock(); |
| 4286 | |
| 4287 | if (needWake) { |
| 4288 | mLooper->wake(); |
| 4289 | } |
| 4290 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4291 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4292 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4293 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4294 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4295 | if (syncMode == InputEventInjectionSync::NONE) { |
| 4296 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4297 | } else { |
| 4298 | for (;;) { |
| 4299 | injectionResult = injectionState->injectionResult; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4300 | if (injectionResult != InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4301 | break; |
| 4302 | } |
| 4303 | |
| 4304 | nsecs_t remainingTimeout = endTime - now(); |
| 4305 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4306 | if (DEBUG_INJECTION) { |
| 4307 | ALOGD("injectInputEvent - Timed out waiting for injection result " |
| 4308 | "to become available."); |
| 4309 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4310 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4311 | break; |
| 4312 | } |
| 4313 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4314 | mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4315 | } |
| 4316 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4317 | if (injectionResult == InputEventInjectionResult::SUCCEEDED && |
| 4318 | syncMode == InputEventInjectionSync::WAIT_FOR_FINISHED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4319 | while (injectionState->pendingForegroundDispatches != 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4320 | if (DEBUG_INJECTION) { |
| 4321 | ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", |
| 4322 | injectionState->pendingForegroundDispatches); |
| 4323 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4324 | nsecs_t remainingTimeout = endTime - now(); |
| 4325 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4326 | if (DEBUG_INJECTION) { |
| 4327 | ALOGD("injectInputEvent - Timed out waiting for pending foreground " |
| 4328 | "dispatches to finish."); |
| 4329 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4330 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4331 | break; |
| 4332 | } |
| 4333 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4334 | mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4335 | } |
| 4336 | } |
| 4337 | } |
| 4338 | |
| 4339 | injectionState->release(); |
| 4340 | } // release lock |
| 4341 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4342 | if (DEBUG_INJECTION) { |
| 4343 | ALOGD("injectInputEvent - Finished with result %d. injectorPid=%d, injectorUid=%d", |
| 4344 | injectionResult, injectorPid, injectorUid); |
| 4345 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4346 | |
| 4347 | return injectionResult; |
| 4348 | } |
| 4349 | |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4350 | std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4351 | std::array<uint8_t, 32> calculatedHmac; |
| 4352 | std::unique_ptr<VerifiedInputEvent> result; |
| 4353 | switch (event.getType()) { |
| 4354 | case AINPUT_EVENT_TYPE_KEY: { |
| 4355 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event); |
| 4356 | VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent); |
| 4357 | result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4358 | calculatedHmac = sign(verifiedKeyEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4359 | break; |
| 4360 | } |
| 4361 | case AINPUT_EVENT_TYPE_MOTION: { |
| 4362 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event); |
| 4363 | VerifiedMotionEvent verifiedMotionEvent = |
| 4364 | verifiedMotionEventFromMotionEvent(motionEvent); |
| 4365 | result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4366 | calculatedHmac = sign(verifiedMotionEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4367 | break; |
| 4368 | } |
| 4369 | default: { |
| 4370 | ALOGE("Cannot verify events of type %" PRId32, event.getType()); |
| 4371 | return nullptr; |
| 4372 | } |
| 4373 | } |
| 4374 | if (calculatedHmac == INVALID_HMAC) { |
| 4375 | return nullptr; |
| 4376 | } |
| 4377 | if (calculatedHmac != event.getHmac()) { |
| 4378 | return nullptr; |
| 4379 | } |
| 4380 | return result; |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4381 | } |
| 4382 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4383 | bool InputDispatcher::hasInjectionPermission(int32_t injectorPid, int32_t injectorUid) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4384 | return injectorUid == 0 || |
| 4385 | mPolicy->checkInjectEventsPermissionNonReentrant(injectorPid, injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4386 | } |
| 4387 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4388 | void InputDispatcher::setInjectionResult(EventEntry& entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4389 | InputEventInjectionResult injectionResult) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4390 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4391 | if (injectionState) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4392 | if (DEBUG_INJECTION) { |
| 4393 | ALOGD("Setting input event injection result to %d. " |
| 4394 | "injectorPid=%d, injectorUid=%d", |
| 4395 | injectionResult, injectionState->injectorPid, injectionState->injectorUid); |
| 4396 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4397 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4398 | if (injectionState->injectionIsAsync && !(entry.policyFlags & POLICY_FLAG_FILTERED)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4399 | // Log the outcome since the injector did not wait for the injection result. |
| 4400 | switch (injectionResult) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4401 | case InputEventInjectionResult::SUCCEEDED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4402 | ALOGV("Asynchronous input event injection succeeded."); |
| 4403 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4404 | case InputEventInjectionResult::FAILED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4405 | ALOGW("Asynchronous input event injection failed."); |
| 4406 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4407 | case InputEventInjectionResult::PERMISSION_DENIED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4408 | ALOGW("Asynchronous input event injection permission denied."); |
| 4409 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4410 | case InputEventInjectionResult::TIMED_OUT: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4411 | ALOGW("Asynchronous input event injection timed out."); |
| 4412 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4413 | case InputEventInjectionResult::PENDING: |
| 4414 | ALOGE("Setting result to 'PENDING' for asynchronous injection"); |
| 4415 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4416 | } |
| 4417 | } |
| 4418 | |
| 4419 | injectionState->injectionResult = injectionResult; |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4420 | mInjectionResultAvailable.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4421 | } |
| 4422 | } |
| 4423 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4424 | void InputDispatcher::transformMotionEntryForInjectionLocked(MotionEntry& entry) const { |
| 4425 | const bool isRelativeMouseEvent = isFromSource(entry.source, AINPUT_SOURCE_MOUSE_RELATIVE); |
| 4426 | if (!isRelativeMouseEvent && !isFromSource(entry.source, AINPUT_SOURCE_CLASS_POINTER)) { |
| 4427 | return; |
| 4428 | } |
| 4429 | |
| 4430 | // Input injection works in the logical display coordinate space, but the input pipeline works |
| 4431 | // display space, so we need to transform the injected events accordingly. |
| 4432 | const auto it = mDisplayInfos.find(entry.displayId); |
| 4433 | if (it == mDisplayInfos.end()) return; |
| 4434 | const auto& transformToDisplay = it->second.transform.inverse(); |
| 4435 | |
| 4436 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
| 4437 | PointerCoords& pc = entry.pointerCoords[i]; |
| 4438 | const auto xy = isRelativeMouseEvent |
| 4439 | ? transformWithoutTranslation(transformToDisplay, pc.getX(), pc.getY()) |
| 4440 | : transformToDisplay.transform(pc.getXYValue()); |
| 4441 | pc.setAxisValue(AMOTION_EVENT_AXIS_X, xy.x); |
| 4442 | pc.setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y); |
| 4443 | |
| 4444 | // Axes with relative values never represent points on a screen, so they should never have |
| 4445 | // translation applied. If a device does not report relative values, these values are always |
| 4446 | // 0, and will remain unaffected by the following operation. |
| 4447 | const auto rel = |
| 4448 | transformWithoutTranslation(transformToDisplay, |
| 4449 | pc.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X), |
| 4450 | pc.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)); |
| 4451 | pc.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, rel.x); |
| 4452 | pc.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, rel.y); |
| 4453 | } |
| 4454 | } |
| 4455 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4456 | void InputDispatcher::incrementPendingForegroundDispatches(EventEntry& entry) { |
| 4457 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4458 | if (injectionState) { |
| 4459 | injectionState->pendingForegroundDispatches += 1; |
| 4460 | } |
| 4461 | } |
| 4462 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4463 | void InputDispatcher::decrementPendingForegroundDispatches(EventEntry& entry) { |
| 4464 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4465 | if (injectionState) { |
| 4466 | injectionState->pendingForegroundDispatches -= 1; |
| 4467 | |
| 4468 | if (injectionState->pendingForegroundDispatches == 0) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4469 | mInjectionSyncFinished.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4470 | } |
| 4471 | } |
| 4472 | } |
| 4473 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4474 | const std::vector<sp<WindowInfoHandle>>& InputDispatcher::getWindowHandlesLocked( |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4475 | int32_t displayId) const { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4476 | static const std::vector<sp<WindowInfoHandle>> EMPTY_WINDOW_HANDLES; |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4477 | auto it = mWindowHandlesByDisplay.find(displayId); |
| 4478 | return it != mWindowHandlesByDisplay.end() ? it->second : EMPTY_WINDOW_HANDLES; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4479 | } |
| 4480 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4481 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4482 | const sp<IBinder>& windowHandleToken) const { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4483 | if (windowHandleToken == nullptr) { |
| 4484 | return nullptr; |
| 4485 | } |
| 4486 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4487 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4488 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 4489 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4490 | if (windowHandle->getToken() == windowHandleToken) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4491 | return windowHandle; |
| 4492 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4493 | } |
| 4494 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4495 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4496 | } |
| 4497 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4498 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked(const sp<IBinder>& windowHandleToken, |
| 4499 | int displayId) const { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4500 | if (windowHandleToken == nullptr) { |
| 4501 | return nullptr; |
| 4502 | } |
| 4503 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4504 | for (const sp<WindowInfoHandle>& windowHandle : getWindowHandlesLocked(displayId)) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4505 | if (windowHandle->getToken() == windowHandleToken) { |
| 4506 | return windowHandle; |
| 4507 | } |
| 4508 | } |
| 4509 | return nullptr; |
| 4510 | } |
| 4511 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4512 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
| 4513 | const sp<WindowInfoHandle>& windowHandle) const { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4514 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4515 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 4516 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4517 | if (handle->getId() == windowHandle->getId() && |
| 4518 | handle->getToken() == windowHandle->getToken()) { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4519 | if (windowHandle->getInfo()->displayId != it.first) { |
| 4520 | ALOGE("Found window %s in display %" PRId32 |
| 4521 | ", but it should belong to display %" PRId32, |
| 4522 | windowHandle->getName().c_str(), it.first, |
| 4523 | windowHandle->getInfo()->displayId); |
| 4524 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4525 | return handle; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4526 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4527 | } |
| 4528 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4529 | return nullptr; |
| 4530 | } |
| 4531 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4532 | sp<WindowInfoHandle> InputDispatcher::getFocusedWindowHandleLocked(int displayId) const { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4533 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(displayId); |
| 4534 | return getWindowHandleLocked(focusedToken, displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4535 | } |
| 4536 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4537 | bool InputDispatcher::hasResponsiveConnectionLocked(WindowInfoHandle& windowHandle) const { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4538 | sp<Connection> connection = getConnectionLocked(windowHandle.getToken()); |
| 4539 | const bool noInputChannel = |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4540 | windowHandle.getInfo()->inputFeatures.test(WindowInfo::Feature::NO_INPUT_CHANNEL); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4541 | if (connection != nullptr && noInputChannel) { |
| 4542 | ALOGW("%s has feature NO_INPUT_CHANNEL, but it matched to connection %s", |
| 4543 | windowHandle.getName().c_str(), connection->inputChannel->getName().c_str()); |
| 4544 | return false; |
| 4545 | } |
| 4546 | |
| 4547 | if (connection == nullptr) { |
| 4548 | if (!noInputChannel) { |
| 4549 | ALOGI("Could not find connection for %s", windowHandle.getName().c_str()); |
| 4550 | } |
| 4551 | return false; |
| 4552 | } |
| 4553 | if (!connection->responsive) { |
| 4554 | ALOGW("Window %s is not responsive", windowHandle.getName().c_str()); |
| 4555 | return false; |
| 4556 | } |
| 4557 | return true; |
| 4558 | } |
| 4559 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4560 | std::shared_ptr<InputChannel> InputDispatcher::getInputChannelLocked( |
| 4561 | const sp<IBinder>& token) const { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4562 | auto connectionIt = mConnectionsByToken.find(token); |
| 4563 | if (connectionIt == mConnectionsByToken.end()) { |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4564 | return nullptr; |
| 4565 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4566 | return connectionIt->second->inputChannel; |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4567 | } |
| 4568 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4569 | void InputDispatcher::updateWindowHandlesForDisplayLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4570 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
| 4571 | if (windowInfoHandles.empty()) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4572 | // Remove all handles on a display if there are no windows left. |
| 4573 | mWindowHandlesByDisplay.erase(displayId); |
| 4574 | return; |
| 4575 | } |
| 4576 | |
| 4577 | // Since we compare the pointer of input window handles across window updates, we need |
| 4578 | // to make sure the handle object for the same window stays unchanged across updates. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4579 | const std::vector<sp<WindowInfoHandle>>& oldHandles = getWindowHandlesLocked(displayId); |
| 4580 | std::unordered_map<int32_t /*id*/, sp<WindowInfoHandle>> oldHandlesById; |
| 4581 | for (const sp<WindowInfoHandle>& handle : oldHandles) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4582 | oldHandlesById[handle->getId()] = handle; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4583 | } |
| 4584 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4585 | std::vector<sp<WindowInfoHandle>> newHandles; |
| 4586 | for (const sp<WindowInfoHandle>& handle : windowInfoHandles) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4587 | const WindowInfo* info = handle->getInfo(); |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 4588 | if (getInputChannelLocked(handle->getToken()) == nullptr) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4589 | const bool noInputChannel = |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4590 | info->inputFeatures.test(WindowInfo::Feature::NO_INPUT_CHANNEL); |
| 4591 | const bool canReceiveInput = !info->flags.test(WindowInfo::Flag::NOT_TOUCHABLE) || |
| 4592 | !info->flags.test(WindowInfo::Flag::NOT_FOCUSABLE); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4593 | if (canReceiveInput && !noInputChannel) { |
John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 4594 | ALOGV("Window handle %s has no registered input channel", |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4595 | handle->getName().c_str()); |
Robert Carr | 2984b7a | 2020-04-13 17:06:45 -0700 | [diff] [blame] | 4596 | continue; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4597 | } |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4598 | } |
| 4599 | |
| 4600 | if (info->displayId != displayId) { |
| 4601 | ALOGE("Window %s updated by wrong display %d, should belong to display %d", |
| 4602 | handle->getName().c_str(), displayId, info->displayId); |
| 4603 | continue; |
| 4604 | } |
| 4605 | |
Robert Carr | edd1360 | 2020-04-13 17:24:34 -0700 | [diff] [blame] | 4606 | if ((oldHandlesById.find(handle->getId()) != oldHandlesById.end()) && |
| 4607 | (oldHandlesById.at(handle->getId())->getToken() == handle->getToken())) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4608 | const sp<WindowInfoHandle>& oldHandle = oldHandlesById.at(handle->getId()); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4609 | oldHandle->updateFrom(handle); |
| 4610 | newHandles.push_back(oldHandle); |
| 4611 | } else { |
| 4612 | newHandles.push_back(handle); |
| 4613 | } |
| 4614 | } |
| 4615 | |
| 4616 | // Insert or replace |
| 4617 | mWindowHandlesByDisplay[displayId] = newHandles; |
| 4618 | } |
| 4619 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4620 | void InputDispatcher::setInputWindows( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4621 | const std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>>& handlesPerDisplay) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 4622 | // TODO(b/198444055): Remove setInputWindows from InputDispatcher. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4623 | { // acquire lock |
| 4624 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 4625 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 4626 | setInputWindowsLocked(handles, displayId); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4627 | } |
| 4628 | } |
| 4629 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4630 | mLooper->wake(); |
| 4631 | } |
| 4632 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4633 | /** |
| 4634 | * Called from InputManagerService, update window handle list by displayId that can receive input. |
| 4635 | * A window handle contains information about InputChannel, Touch Region, Types, Focused,... |
| 4636 | * If set an empty list, remove all handles from the specific display. |
| 4637 | * For focused handle, check if need to change and send a cancel event to previous one. |
| 4638 | * For removed handle, check if need to send a cancel event if already in touch. |
| 4639 | */ |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4640 | void InputDispatcher::setInputWindowsLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4641 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4642 | if (DEBUG_FOCUS) { |
| 4643 | std::string windowList; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4644 | for (const sp<WindowInfoHandle>& iwh : windowInfoHandles) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4645 | windowList += iwh->getName() + " "; |
| 4646 | } |
| 4647 | ALOGD("setInputWindows displayId=%" PRId32 " %s", displayId, windowList.c_str()); |
| 4648 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4649 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4650 | // Ensure all tokens are null if the window has feature NO_INPUT_CHANNEL |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4651 | for (const sp<WindowInfoHandle>& window : windowInfoHandles) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4652 | const bool noInputWindow = |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4653 | window->getInfo()->inputFeatures.test(WindowInfo::Feature::NO_INPUT_CHANNEL); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4654 | if (noInputWindow && window->getToken() != nullptr) { |
| 4655 | ALOGE("%s has feature NO_INPUT_WINDOW, but a non-null token. Clearing", |
| 4656 | window->getName().c_str()); |
| 4657 | window->releaseChannel(); |
| 4658 | } |
| 4659 | } |
| 4660 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4661 | // Copy old handles for release if they are no longer present. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4662 | const std::vector<sp<WindowInfoHandle>> oldWindowHandles = getWindowHandlesLocked(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4663 | |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4664 | // Save the old windows' orientation by ID before it gets updated. |
| 4665 | std::unordered_map<int32_t, uint32_t> oldWindowOrientations; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4666 | for (const sp<WindowInfoHandle>& handle : oldWindowHandles) { |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4667 | oldWindowOrientations.emplace(handle->getId(), |
| 4668 | handle->getInfo()->transform.getOrientation()); |
| 4669 | } |
| 4670 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4671 | updateWindowHandlesForDisplayLocked(windowInfoHandles, displayId); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4672 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4673 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4674 | if (mLastHoverWindowHandle && |
| 4675 | std::find(windowHandles.begin(), windowHandles.end(), mLastHoverWindowHandle) == |
| 4676 | windowHandles.end()) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4677 | mLastHoverWindowHandle = nullptr; |
| 4678 | } |
| 4679 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4680 | std::optional<FocusResolver::FocusChanges> changes = |
| 4681 | mFocusResolver.setInputWindows(displayId, windowHandles); |
| 4682 | if (changes) { |
| 4683 | onFocusChangedLocked(*changes); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4684 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4685 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4686 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 4687 | mTouchStatesByDisplay.find(displayId); |
| 4688 | if (stateIt != mTouchStatesByDisplay.end()) { |
| 4689 | TouchState& state = stateIt->second; |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4690 | for (size_t i = 0; i < state.windows.size();) { |
| 4691 | TouchedWindow& touchedWindow = state.windows[i]; |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4692 | if (getWindowHandleLocked(touchedWindow.windowHandle) == nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4693 | if (DEBUG_FOCUS) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4694 | ALOGD("Touched window was removed: %s in display %" PRId32, |
| 4695 | touchedWindow.windowHandle->getName().c_str(), displayId); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4696 | } |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4697 | std::shared_ptr<InputChannel> touchedInputChannel = |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4698 | getInputChannelLocked(touchedWindow.windowHandle->getToken()); |
| 4699 | if (touchedInputChannel != nullptr) { |
| 4700 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4701 | "touched window was removed"); |
| 4702 | synthesizeCancelationEventsForInputChannelLocked(touchedInputChannel, options); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4703 | // Since we are about to drop the touch, cancel the events for the wallpaper as |
| 4704 | // well. |
| 4705 | if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND && |
| 4706 | touchedWindow.windowHandle->getInfo()->hasWallpaper) { |
| 4707 | sp<WindowInfoHandle> wallpaper = state.getWallpaperWindow(); |
| 4708 | if (wallpaper != nullptr) { |
| 4709 | sp<Connection> wallpaperConnection = |
| 4710 | getConnectionLocked(wallpaper->getToken()); |
| 4711 | synthesizeCancelationEventsForConnectionLocked(wallpaperConnection, |
| 4712 | options); |
| 4713 | } |
| 4714 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4715 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4716 | state.windows.erase(state.windows.begin() + i); |
| 4717 | } else { |
| 4718 | ++i; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4719 | } |
| 4720 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4721 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4722 | // 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] | 4723 | // could just clear the state here. |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4724 | if (mDragState && |
| 4725 | std::find(windowHandles.begin(), windowHandles.end(), mDragState->dragWindow) == |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4726 | windowHandles.end()) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4727 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4728 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4729 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4730 | |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4731 | if (isPerWindowInputRotationEnabled()) { |
| 4732 | // Determine if the orientation of any of the input windows have changed, and cancel all |
| 4733 | // pointer events if necessary. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4734 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
| 4735 | const sp<WindowInfoHandle> newWindowHandle = getWindowHandleLocked(oldWindowHandle); |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4736 | if (newWindowHandle != nullptr && |
| 4737 | newWindowHandle->getInfo()->transform.getOrientation() != |
| 4738 | oldWindowOrientations[oldWindowHandle->getId()]) { |
| 4739 | std::shared_ptr<InputChannel> inputChannel = |
| 4740 | getInputChannelLocked(newWindowHandle->getToken()); |
| 4741 | if (inputChannel != nullptr) { |
| 4742 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4743 | "touched window's orientation changed"); |
| 4744 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
| 4745 | } |
| 4746 | } |
| 4747 | } |
| 4748 | } |
| 4749 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4750 | // Release information for windows that are no longer present. |
| 4751 | // This ensures that unused input channels are released promptly. |
| 4752 | // Otherwise, they might stick around until the window handle is destroyed |
| 4753 | // which might not happen until the next GC. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4754 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4755 | if (getWindowHandleLocked(oldWindowHandle) == nullptr) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4756 | if (DEBUG_FOCUS) { |
| 4757 | ALOGD("Window went away: %s", oldWindowHandle->getName().c_str()); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4758 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4759 | oldWindowHandle->releaseChannel(); |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 4760 | // To avoid making too many calls into the compat framework, only |
| 4761 | // check for window flags when windows are going away. |
| 4762 | // TODO(b/157929241) : delete this. This is only needed temporarily |
| 4763 | // in order to gather some data about the flag usage |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4764 | if (oldWindowHandle->getInfo()->flags.test(WindowInfo::Flag::SLIPPERY)) { |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 4765 | ALOGW("%s has FLAG_SLIPPERY. Please report this in b/157929241", |
| 4766 | oldWindowHandle->getName().c_str()); |
| 4767 | if (mCompatService != nullptr) { |
| 4768 | mCompatService->reportChangeByUid(IInputConstants::BLOCK_FLAG_SLIPPERY, |
| 4769 | oldWindowHandle->getInfo()->ownerUid); |
| 4770 | } |
| 4771 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4772 | } |
chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 4773 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4774 | } |
| 4775 | |
| 4776 | void InputDispatcher::setFocusedApplication( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4777 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4778 | if (DEBUG_FOCUS) { |
| 4779 | ALOGD("setFocusedApplication displayId=%" PRId32 " %s", displayId, |
| 4780 | inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>"); |
| 4781 | } |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4782 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4783 | std::scoped_lock _l(mLock); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 4784 | setFocusedApplicationLocked(displayId, inputApplicationHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4785 | } // release lock |
| 4786 | |
| 4787 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4788 | mLooper->wake(); |
| 4789 | } |
| 4790 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 4791 | void InputDispatcher::setFocusedApplicationLocked( |
| 4792 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
| 4793 | std::shared_ptr<InputApplicationHandle> oldFocusedApplicationHandle = |
| 4794 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 4795 | |
| 4796 | if (sharedPointersEqual(oldFocusedApplicationHandle, inputApplicationHandle)) { |
| 4797 | return; // This application is already focused. No need to wake up or change anything. |
| 4798 | } |
| 4799 | |
| 4800 | // Set the new application handle. |
| 4801 | if (inputApplicationHandle != nullptr) { |
| 4802 | mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle; |
| 4803 | } else { |
| 4804 | mFocusedApplicationHandlesByDisplay.erase(displayId); |
| 4805 | } |
| 4806 | |
| 4807 | // No matter what the old focused application was, stop waiting on it because it is |
| 4808 | // no longer focused. |
| 4809 | resetNoFocusedWindowTimeoutLocked(); |
| 4810 | } |
| 4811 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4812 | /** |
| 4813 | * Sets the focused display, which is responsible for receiving focus-dispatched input events where |
| 4814 | * the display not specified. |
| 4815 | * |
| 4816 | * We track any unreleased events for each window. If a window loses the ability to receive the |
| 4817 | * released event, we will send a cancel event to it. So when the focused display is changed, we |
| 4818 | * cancel all the unreleased display-unspecified events for the focused window on the old focused |
| 4819 | * display. The display-specified events won't be affected. |
| 4820 | */ |
| 4821 | void InputDispatcher::setFocusedDisplay(int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4822 | if (DEBUG_FOCUS) { |
| 4823 | ALOGD("setFocusedDisplay displayId=%" PRId32, displayId); |
| 4824 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4825 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4826 | std::scoped_lock _l(mLock); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4827 | |
| 4828 | if (mFocusedDisplayId != displayId) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4829 | sp<IBinder> oldFocusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4830 | mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4831 | if (oldFocusedWindowToken != nullptr) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4832 | std::shared_ptr<InputChannel> inputChannel = |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4833 | getInputChannelLocked(oldFocusedWindowToken); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4834 | if (inputChannel != nullptr) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4835 | CancelationOptions |
| 4836 | options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 4837 | "The display which contains this window no longer has focus."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4838 | options.displayId = ADISPLAY_ID_NONE; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4839 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
| 4840 | } |
| 4841 | } |
| 4842 | mFocusedDisplayId = displayId; |
| 4843 | |
Chris Ye | 3c2d6f5 | 2020-08-09 10:39:48 -0700 | [diff] [blame] | 4844 | // Find new focused window and validate |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4845 | sp<IBinder> newFocusedWindowToken = mFocusResolver.getFocusedWindowToken(displayId); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 4846 | sendFocusChangedCommandLocked(oldFocusedWindowToken, newFocusedWindowToken); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4847 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4848 | if (newFocusedWindowToken == nullptr) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4849 | ALOGW("Focused display #%" PRId32 " does not have a focused window.", displayId); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4850 | if (mFocusResolver.hasFocusedWindowTokens()) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4851 | ALOGE("But another display has a focused window\n%s", |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4852 | mFocusResolver.dumpFocusedWindows().c_str()); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4853 | } |
| 4854 | } |
| 4855 | } |
| 4856 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4857 | if (DEBUG_FOCUS) { |
| 4858 | logDispatchStateLocked(); |
| 4859 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4860 | } // release lock |
| 4861 | |
| 4862 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4863 | mLooper->wake(); |
| 4864 | } |
| 4865 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4866 | void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4867 | if (DEBUG_FOCUS) { |
| 4868 | ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); |
| 4869 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4870 | |
| 4871 | bool changed; |
| 4872 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4873 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4874 | |
| 4875 | if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { |
| 4876 | if (mDispatchFrozen && !frozen) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4877 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4878 | } |
| 4879 | |
| 4880 | if (mDispatchEnabled && !enabled) { |
| 4881 | resetAndDropEverythingLocked("dispatcher is being disabled"); |
| 4882 | } |
| 4883 | |
| 4884 | mDispatchEnabled = enabled; |
| 4885 | mDispatchFrozen = frozen; |
| 4886 | changed = true; |
| 4887 | } else { |
| 4888 | changed = false; |
| 4889 | } |
| 4890 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4891 | if (DEBUG_FOCUS) { |
| 4892 | logDispatchStateLocked(); |
| 4893 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4894 | } // release lock |
| 4895 | |
| 4896 | if (changed) { |
| 4897 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4898 | mLooper->wake(); |
| 4899 | } |
| 4900 | } |
| 4901 | |
| 4902 | void InputDispatcher::setInputFilterEnabled(bool enabled) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4903 | if (DEBUG_FOCUS) { |
| 4904 | ALOGD("setInputFilterEnabled: enabled=%d", enabled); |
| 4905 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4906 | |
| 4907 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4908 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4909 | |
| 4910 | if (mInputFilterEnabled == enabled) { |
| 4911 | return; |
| 4912 | } |
| 4913 | |
| 4914 | mInputFilterEnabled = enabled; |
| 4915 | resetAndDropEverythingLocked("input filter is being enabled or disabled"); |
| 4916 | } // release lock |
| 4917 | |
| 4918 | // Wake up poll loop since there might be work to do to drop everything. |
| 4919 | mLooper->wake(); |
| 4920 | } |
| 4921 | |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 4922 | void InputDispatcher::setInTouchMode(bool inTouchMode) { |
| 4923 | std::scoped_lock lock(mLock); |
| 4924 | mInTouchMode = inTouchMode; |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 4925 | // TODO(b/193718270): Fire TouchModeEvent here. |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 4926 | } |
| 4927 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 4928 | void InputDispatcher::setMaximumObscuringOpacityForTouch(float opacity) { |
| 4929 | if (opacity < 0 || opacity > 1) { |
| 4930 | LOG_ALWAYS_FATAL("Maximum obscuring opacity for touch should be >= 0 and <= 1"); |
| 4931 | return; |
| 4932 | } |
| 4933 | |
| 4934 | std::scoped_lock lock(mLock); |
| 4935 | mMaximumObscuringOpacityForTouch = opacity; |
| 4936 | } |
| 4937 | |
| 4938 | void InputDispatcher::setBlockUntrustedTouchesMode(BlockUntrustedTouchesMode mode) { |
| 4939 | std::scoped_lock lock(mLock); |
| 4940 | mBlockUntrustedTouchesMode = mode; |
| 4941 | } |
| 4942 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4943 | std::pair<TouchState*, TouchedWindow*> InputDispatcher::findTouchStateAndWindowLocked( |
| 4944 | const sp<IBinder>& token) { |
| 4945 | for (auto& [displayId, state] : mTouchStatesByDisplay) { |
| 4946 | for (TouchedWindow& w : state.windows) { |
| 4947 | if (w.windowHandle->getToken() == token) { |
| 4948 | return std::make_pair(&state, &w); |
| 4949 | } |
| 4950 | } |
| 4951 | } |
| 4952 | return std::make_pair(nullptr, nullptr); |
| 4953 | } |
| 4954 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4955 | bool InputDispatcher::transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken, |
| 4956 | bool isDragDrop) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4957 | if (fromToken == toToken) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4958 | if (DEBUG_FOCUS) { |
| 4959 | ALOGD("Trivial transfer to same window."); |
| 4960 | } |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4961 | return true; |
| 4962 | } |
| 4963 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4964 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4965 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4966 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4967 | // Find the target touch state and touched window by fromToken. |
| 4968 | auto [state, touchedWindow] = findTouchStateAndWindowLocked(fromToken); |
| 4969 | if (state == nullptr || touchedWindow == nullptr) { |
| 4970 | ALOGD("Focus transfer failed because from window is not being touched."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4971 | return false; |
| 4972 | } |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4973 | |
| 4974 | const int32_t displayId = state->displayId; |
| 4975 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(toToken, displayId); |
| 4976 | if (toWindowHandle == nullptr) { |
| 4977 | ALOGW("Cannot transfer focus because to window not found."); |
| 4978 | return false; |
| 4979 | } |
| 4980 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4981 | if (DEBUG_FOCUS) { |
| 4982 | ALOGD("transferTouchFocus: fromWindowHandle=%s, toWindowHandle=%s", |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4983 | touchedWindow->windowHandle->getName().c_str(), |
| 4984 | toWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4985 | } |
| 4986 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4987 | // Erase old window. |
| 4988 | int32_t oldTargetFlags = touchedWindow->targetFlags; |
| 4989 | BitSet32 pointerIds = touchedWindow->pointerIds; |
| 4990 | state->removeWindowByToken(fromToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4991 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4992 | // Add new window. |
| 4993 | int32_t newTargetFlags = oldTargetFlags & |
| 4994 | (InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_SPLIT | |
| 4995 | InputTarget::FLAG_DISPATCH_AS_IS); |
| 4996 | state->addOrUpdateWindow(toWindowHandle, newTargetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4997 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4998 | // Store the dragging window. |
| 4999 | if (isDragDrop) { |
| 5000 | mDragState = std::make_unique<DragState>(toWindowHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5001 | } |
| 5002 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5003 | // Synthesize cancel for old window and down for new window. |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 5004 | sp<Connection> fromConnection = getConnectionLocked(fromToken); |
| 5005 | sp<Connection> toConnection = getConnectionLocked(toToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5006 | if (fromConnection != nullptr && toConnection != nullptr) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5007 | fromConnection->inputState.mergePointerStateTo(toConnection->inputState); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5008 | CancelationOptions |
| 5009 | options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 5010 | "transferring touch focus from this window to another window"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5011 | synthesizeCancelationEventsForConnectionLocked(fromConnection, options); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5012 | synthesizePointerDownEventsForConnectionLocked(toConnection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5013 | } |
| 5014 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5015 | if (DEBUG_FOCUS) { |
| 5016 | logDispatchStateLocked(); |
| 5017 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5018 | } // release lock |
| 5019 | |
| 5020 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5021 | mLooper->wake(); |
| 5022 | return true; |
| 5023 | } |
| 5024 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5025 | // Binder call |
| 5026 | bool InputDispatcher::transferTouch(const sp<IBinder>& destChannelToken) { |
| 5027 | sp<IBinder> fromToken; |
| 5028 | { // acquire lock |
| 5029 | std::scoped_lock _l(mLock); |
| 5030 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5031 | auto it = std::find_if(mTouchStatesByDisplay.begin(), mTouchStatesByDisplay.end(), |
| 5032 | [](const auto& pair) { return pair.second.windows.size() == 1; }); |
| 5033 | if (it == mTouchStatesByDisplay.end()) { |
| 5034 | ALOGW("Cannot transfer touch state because there is no exact window being touched"); |
| 5035 | return false; |
| 5036 | } |
| 5037 | const int32_t displayId = it->first; |
| 5038 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(destChannelToken, displayId); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5039 | if (toWindowHandle == nullptr) { |
| 5040 | ALOGW("Could not find window associated with token=%p", destChannelToken.get()); |
| 5041 | return false; |
| 5042 | } |
| 5043 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5044 | TouchState& state = it->second; |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5045 | const TouchedWindow& touchedWindow = state.windows[0]; |
| 5046 | fromToken = touchedWindow.windowHandle->getToken(); |
| 5047 | } // release lock |
| 5048 | |
| 5049 | return transferTouchFocus(fromToken, destChannelToken); |
| 5050 | } |
| 5051 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5052 | void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5053 | if (DEBUG_FOCUS) { |
| 5054 | ALOGD("Resetting and dropping all events (%s).", reason); |
| 5055 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5056 | |
| 5057 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, reason); |
| 5058 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 5059 | |
| 5060 | resetKeyRepeatLocked(); |
| 5061 | releasePendingEventLocked(); |
| 5062 | drainInboundQueueLocked(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5063 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5064 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5065 | mAnrTracker.clear(); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5066 | mTouchStatesByDisplay.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5067 | mLastHoverWindowHandle.clear(); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5068 | mReplacedKeys.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5069 | } |
| 5070 | |
| 5071 | void InputDispatcher::logDispatchStateLocked() { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5072 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5073 | dumpDispatchStateLocked(dump); |
| 5074 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5075 | std::istringstream stream(dump); |
| 5076 | std::string line; |
| 5077 | |
| 5078 | while (std::getline(stream, line, '\n')) { |
| 5079 | ALOGD("%s", line.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5080 | } |
| 5081 | } |
| 5082 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5083 | std::string InputDispatcher::dumpPointerCaptureStateLocked() { |
| 5084 | std::string dump; |
| 5085 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5086 | dump += StringPrintf(INDENT "Pointer Capture Requested: %s\n", |
| 5087 | toString(mCurrentPointerCaptureRequest.enable)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5088 | |
| 5089 | std::string windowName = "None"; |
| 5090 | if (mWindowTokenWithPointerCapture) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5091 | const sp<WindowInfoHandle> captureWindowHandle = |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5092 | getWindowHandleLocked(mWindowTokenWithPointerCapture); |
| 5093 | windowName = captureWindowHandle ? captureWindowHandle->getName().c_str() |
| 5094 | : "token has capture without window"; |
| 5095 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5096 | dump += StringPrintf(INDENT "Current Window with Pointer Capture: %s\n", windowName.c_str()); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5097 | |
| 5098 | return dump; |
| 5099 | } |
| 5100 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5101 | void InputDispatcher::dumpDispatchStateLocked(std::string& dump) { |
Siarhei Vishniakou | 043a3ec | 2019-05-01 11:30:46 -0700 | [diff] [blame] | 5102 | dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled)); |
| 5103 | dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen)); |
| 5104 | dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5105 | dump += StringPrintf(INDENT "FocusedDisplayId: %" PRId32 "\n", mFocusedDisplayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5106 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5107 | if (!mFocusedApplicationHandlesByDisplay.empty()) { |
| 5108 | dump += StringPrintf(INDENT "FocusedApplications:\n"); |
| 5109 | for (auto& it : mFocusedApplicationHandlesByDisplay) { |
| 5110 | const int32_t displayId = it.first; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5111 | const std::shared_ptr<InputApplicationHandle>& applicationHandle = it.second; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5112 | const std::chrono::duration timeout = |
| 5113 | applicationHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5114 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5115 | ", name='%s', dispatchingTimeout=%" PRId64 "ms\n", |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5116 | displayId, applicationHandle->getName().c_str(), millis(timeout)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5117 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5118 | } else { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5119 | dump += StringPrintf(INDENT "FocusedApplications: <none>\n"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5120 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5121 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5122 | dump += mFocusResolver.dump(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5123 | dump += dumpPointerCaptureStateLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5124 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5125 | if (!mTouchStatesByDisplay.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5126 | dump += StringPrintf(INDENT "TouchStatesByDisplay:\n"); |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5127 | for (const std::pair<int32_t, TouchState>& pair : mTouchStatesByDisplay) { |
| 5128 | const TouchState& state = pair.second; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5129 | 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] | 5130 | state.displayId, toString(state.down), toString(state.split), |
| 5131 | state.deviceId, state.source); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5132 | if (!state.windows.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5133 | dump += INDENT3 "Windows:\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5134 | for (size_t i = 0; i < state.windows.size(); i++) { |
| 5135 | const TouchedWindow& touchedWindow = state.windows[i]; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5136 | dump += StringPrintf(INDENT4 |
| 5137 | "%zu: name='%s', pointerIds=0x%0x, targetFlags=0x%x\n", |
| 5138 | i, touchedWindow.windowHandle->getName().c_str(), |
| 5139 | touchedWindow.pointerIds.value, touchedWindow.targetFlags); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5140 | } |
| 5141 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5142 | dump += INDENT3 "Windows: <none>\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5143 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5144 | } |
| 5145 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5146 | dump += INDENT "TouchStates: <no displays touched>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5147 | } |
| 5148 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5149 | if (mDragState) { |
| 5150 | dump += StringPrintf(INDENT "DragState:\n"); |
| 5151 | mDragState->dump(dump, INDENT2); |
| 5152 | } |
| 5153 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5154 | if (!mWindowHandlesByDisplay.empty()) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5155 | for (const auto& [displayId, windowHandles] : mWindowHandlesByDisplay) { |
| 5156 | dump += StringPrintf(INDENT "Display: %" PRId32 "\n", displayId); |
| 5157 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
| 5158 | const auto& displayInfo = it->second; |
| 5159 | dump += StringPrintf(INDENT2 "logicalSize=%dx%d\n", displayInfo.logicalWidth, |
| 5160 | displayInfo.logicalHeight); |
| 5161 | displayInfo.transform.dump(dump, "transform", INDENT4); |
| 5162 | } else { |
| 5163 | dump += INDENT2 "No DisplayInfo found!\n"; |
| 5164 | } |
| 5165 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5166 | if (!windowHandles.empty()) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5167 | dump += INDENT2 "Windows:\n"; |
| 5168 | for (size_t i = 0; i < windowHandles.size(); i++) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5169 | const sp<WindowInfoHandle>& windowHandle = windowHandles[i]; |
| 5170 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5171 | |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5172 | dump += StringPrintf(INDENT3 "%zu: name='%s', id=%" PRId32 ", displayId=%d, " |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 5173 | "paused=%s, focusable=%s, " |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5174 | "hasWallpaper=%s, visible=%s, alpha=%.2f, " |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 5175 | "flags=%s, type=%s, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5176 | "frame=[%d,%d][%d,%d], globalScale=%f, " |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5177 | "applicationInfo.name=%s, " |
| 5178 | "applicationInfo.token=%s, " |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 5179 | "touchableRegion=", |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5180 | i, windowInfo->name.c_str(), windowInfo->id, |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 5181 | windowInfo->displayId, toString(windowInfo->paused), |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5182 | toString(windowInfo->focusable), |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5183 | toString(windowInfo->hasWallpaper), |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5184 | toString(windowInfo->visible), windowInfo->alpha, |
Michael Wright | 8759d67 | 2020-07-21 00:46:45 +0100 | [diff] [blame] | 5185 | windowInfo->flags.string().c_str(), |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 5186 | ftl::enum_string(windowInfo->type).c_str(), |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 5187 | windowInfo->frameLeft, windowInfo->frameTop, |
| 5188 | windowInfo->frameRight, windowInfo->frameBottom, |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 5189 | windowInfo->globalScaleFactor, |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5190 | windowInfo->applicationInfo.name.c_str(), |
| 5191 | toString(windowInfo->applicationInfo.token).c_str()); |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 5192 | dump += dumpRegion(windowInfo->touchableRegion); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 5193 | dump += StringPrintf(", inputFeatures=%s", |
| 5194 | windowInfo->inputFeatures.string().c_str()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5195 | dump += StringPrintf(", ownerPid=%d, ownerUid=%d, dispatchingTimeout=%" PRId64 |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 5196 | "ms, trustedOverlay=%s, hasToken=%s, " |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5197 | "touchOcclusionMode=%s\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5198 | windowInfo->ownerPid, windowInfo->ownerUid, |
Bernardo Rufino | c2f1fad | 2020-11-04 17:30:57 +0000 | [diff] [blame] | 5199 | millis(windowInfo->dispatchingTimeout), |
| 5200 | toString(windowInfo->trustedOverlay), |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 5201 | toString(windowInfo->token != nullptr), |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5202 | toString(windowInfo->touchOcclusionMode).c_str()); |
chaviw | 85b4420 | 2020-07-24 11:46:21 -0700 | [diff] [blame] | 5203 | windowInfo->transform.dump(dump, "transform", INDENT4); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5204 | } |
| 5205 | } else { |
| 5206 | dump += INDENT2 "Windows: <none>\n"; |
| 5207 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5208 | } |
| 5209 | } else { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5210 | dump += INDENT "Displays: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5211 | } |
| 5212 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5213 | if (!mGlobalMonitorsByDisplay.empty() || !mGestureMonitorsByDisplay.empty()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5214 | for (auto& it : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5215 | const std::vector<Monitor>& monitors = it.second; |
| 5216 | dump += StringPrintf(INDENT "Global monitors in display %" PRId32 ":\n", it.first); |
| 5217 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5218 | } |
| 5219 | for (auto& it : mGestureMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5220 | const std::vector<Monitor>& monitors = it.second; |
| 5221 | dump += StringPrintf(INDENT "Gesture monitors in display %" PRId32 ":\n", it.first); |
| 5222 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5223 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5224 | } else { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5225 | dump += INDENT "Monitors: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5226 | } |
| 5227 | |
| 5228 | nsecs_t currentTime = now(); |
| 5229 | |
| 5230 | // Dump recently dispatched or dropped events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5231 | if (!mRecentQueue.empty()) { |
| 5232 | dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5233 | for (std::shared_ptr<EventEntry>& entry : mRecentQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5234 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5235 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5236 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5237 | } |
| 5238 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5239 | dump += INDENT "RecentQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5240 | } |
| 5241 | |
| 5242 | // Dump event currently being dispatched. |
| 5243 | if (mPendingEvent) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5244 | dump += INDENT "PendingEvent:\n"; |
| 5245 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5246 | dump += mPendingEvent->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5247 | dump += StringPrintf(", age=%" PRId64 "ms\n", |
| 5248 | ns2ms(currentTime - mPendingEvent->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5249 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5250 | dump += INDENT "PendingEvent: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5251 | } |
| 5252 | |
| 5253 | // Dump inbound events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5254 | if (!mInboundQueue.empty()) { |
| 5255 | dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5256 | for (std::shared_ptr<EventEntry>& entry : mInboundQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5257 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5258 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5259 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5260 | } |
| 5261 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5262 | dump += INDENT "InboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5263 | } |
| 5264 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5265 | if (!mReplacedKeys.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5266 | dump += INDENT "ReplacedKeys:\n"; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5267 | for (const std::pair<KeyReplacement, int32_t>& pair : mReplacedKeys) { |
| 5268 | const KeyReplacement& replacement = pair.first; |
| 5269 | int32_t newKeyCode = pair.second; |
| 5270 | dump += StringPrintf(INDENT2 "originalKeyCode=%d, deviceId=%d -> newKeyCode=%d\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5271 | replacement.keyCode, replacement.deviceId, newKeyCode); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5272 | } |
| 5273 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5274 | dump += INDENT "ReplacedKeys: <empty>\n"; |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5275 | } |
| 5276 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5277 | if (!mCommandQueue.empty()) { |
| 5278 | dump += StringPrintf(INDENT "CommandQueue: size=%zu\n", mCommandQueue.size()); |
| 5279 | } else { |
| 5280 | dump += INDENT "CommandQueue: <empty>\n"; |
| 5281 | } |
| 5282 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5283 | if (!mConnectionsByToken.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5284 | dump += INDENT "Connections:\n"; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5285 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5286 | dump += StringPrintf(INDENT2 "%i: channelName='%s', windowName='%s', " |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5287 | "status=%s, monitor=%s, responsive=%s\n", |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5288 | connection->inputChannel->getFd().get(), |
| 5289 | connection->getInputChannelName().c_str(), |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5290 | connection->getWindowName().c_str(), connection->getStatusLabel(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5291 | toString(connection->monitor), toString(connection->responsive)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5292 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5293 | if (!connection->outboundQueue.empty()) { |
| 5294 | dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n", |
| 5295 | connection->outboundQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5296 | dump += dumpQueue(connection->outboundQueue, currentTime); |
| 5297 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5298 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5299 | dump += INDENT3 "OutboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5300 | } |
| 5301 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5302 | if (!connection->waitQueue.empty()) { |
| 5303 | dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n", |
| 5304 | connection->waitQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5305 | dump += dumpQueue(connection->waitQueue, currentTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5306 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5307 | dump += INDENT3 "WaitQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5308 | } |
| 5309 | } |
| 5310 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5311 | dump += INDENT "Connections: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5312 | } |
| 5313 | |
| 5314 | if (isAppSwitchPendingLocked()) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5315 | dump += StringPrintf(INDENT "AppSwitch: pending, due in %" PRId64 "ms\n", |
| 5316 | ns2ms(mAppSwitchDueTime - now())); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5317 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5318 | dump += INDENT "AppSwitch: not pending\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5319 | } |
| 5320 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5321 | dump += INDENT "Configuration:\n"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5322 | dump += StringPrintf(INDENT2 "KeyRepeatDelay: %" PRId64 "ms\n", ns2ms(mConfig.keyRepeatDelay)); |
| 5323 | dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n", |
| 5324 | ns2ms(mConfig.keyRepeatTimeout)); |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 5325 | dump += mLatencyTracker.dump(INDENT2); |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 5326 | dump += mLatencyAggregator.dump(INDENT2); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5327 | } |
| 5328 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5329 | void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) { |
| 5330 | const size_t numMonitors = monitors.size(); |
| 5331 | for (size_t i = 0; i < numMonitors; i++) { |
| 5332 | const Monitor& monitor = monitors[i]; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5333 | const std::shared_ptr<InputChannel>& channel = monitor.inputChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5334 | dump += StringPrintf(INDENT2 "%zu: '%s', ", i, channel->getName().c_str()); |
| 5335 | dump += "\n"; |
| 5336 | } |
| 5337 | } |
| 5338 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5339 | class LooperEventCallback : public LooperCallback { |
| 5340 | public: |
| 5341 | LooperEventCallback(std::function<int(int events)> callback) : mCallback(callback) {} |
| 5342 | int handleEvent(int /*fd*/, int events, void* /*data*/) override { return mCallback(events); } |
| 5343 | |
| 5344 | private: |
| 5345 | std::function<int(int events)> mCallback; |
| 5346 | }; |
| 5347 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5348 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputChannel(const std::string& name) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5349 | if (DEBUG_CHANNEL_CREATION) { |
| 5350 | ALOGD("channel '%s' ~ createInputChannel", name.c_str()); |
| 5351 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5352 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5353 | std::unique_ptr<InputChannel> serverChannel; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5354 | std::unique_ptr<InputChannel> clientChannel; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5355 | status_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5356 | |
| 5357 | if (result) { |
| 5358 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5359 | } |
| 5360 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5361 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5362 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5363 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5364 | int fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5365 | sp<Connection> connection = |
| 5366 | new Connection(std::move(serverChannel), false /*monitor*/, mIdGenerator); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5367 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5368 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5369 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5370 | } |
| 5371 | mConnectionsByToken.emplace(token, connection); |
| 5372 | |
| 5373 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5374 | this, std::placeholders::_1, token); |
| 5375 | |
| 5376 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, new LooperEventCallback(callback), nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5377 | } // release lock |
| 5378 | |
| 5379 | // Wake the looper because some connections have changed. |
| 5380 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5381 | return clientChannel; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5382 | } |
| 5383 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5384 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputMonitor(int32_t displayId, |
| 5385 | bool isGestureMonitor, |
| 5386 | const std::string& name, |
| 5387 | int32_t pid) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5388 | std::shared_ptr<InputChannel> serverChannel; |
| 5389 | std::unique_ptr<InputChannel> clientChannel; |
| 5390 | status_t result = openInputChannelPair(name, serverChannel, clientChannel); |
| 5391 | if (result) { |
| 5392 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5393 | } |
| 5394 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5395 | { // acquire lock |
| 5396 | std::scoped_lock _l(mLock); |
| 5397 | |
| 5398 | if (displayId < 0) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5399 | return base::Error(BAD_VALUE) << "Attempted to create input monitor with name " << name |
| 5400 | << " without a specified display."; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5401 | } |
| 5402 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5403 | sp<Connection> connection = new Connection(serverChannel, true /*monitor*/, mIdGenerator); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5404 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5405 | const int fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5406 | |
| 5407 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5408 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5409 | } |
| 5410 | mConnectionsByToken.emplace(token, connection); |
| 5411 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5412 | this, std::placeholders::_1, token); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5413 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5414 | auto& monitorsByDisplay = |
| 5415 | isGestureMonitor ? mGestureMonitorsByDisplay : mGlobalMonitorsByDisplay; |
Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 5416 | monitorsByDisplay[displayId].emplace_back(serverChannel, pid); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5417 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5418 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, new LooperEventCallback(callback), nullptr); |
Siarhei Vishniakou | c961c74 | 2021-05-19 19:16:59 +0000 | [diff] [blame] | 5419 | ALOGI("Created monitor %s for display %" PRId32 ", gesture=%s, pid=%" PRId32, name.c_str(), |
| 5420 | displayId, toString(isGestureMonitor), pid); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5421 | } |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5422 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5423 | // Wake the looper because some connections have changed. |
| 5424 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5425 | return clientChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5426 | } |
| 5427 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5428 | status_t InputDispatcher::removeInputChannel(const sp<IBinder>& connectionToken) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5429 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5430 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5431 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5432 | status_t status = removeInputChannelLocked(connectionToken, false /*notify*/); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5433 | if (status) { |
| 5434 | return status; |
| 5435 | } |
| 5436 | } // release lock |
| 5437 | |
| 5438 | // Wake the poll loop because removing the connection may have changed the current |
| 5439 | // synchronization state. |
| 5440 | mLooper->wake(); |
| 5441 | return OK; |
| 5442 | } |
| 5443 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5444 | status_t InputDispatcher::removeInputChannelLocked(const sp<IBinder>& connectionToken, |
| 5445 | bool notify) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5446 | sp<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5447 | if (connection == nullptr) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5448 | // 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] | 5449 | return BAD_VALUE; |
| 5450 | } |
| 5451 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5452 | removeConnectionLocked(connection); |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 5453 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5454 | if (connection->monitor) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5455 | removeMonitorChannelLocked(connectionToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5456 | } |
| 5457 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5458 | mLooper->removeFd(connection->inputChannel->getFd()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5459 | |
| 5460 | nsecs_t currentTime = now(); |
| 5461 | abortBrokenDispatchCycleLocked(currentTime, connection, notify); |
| 5462 | |
| 5463 | connection->status = Connection::STATUS_ZOMBIE; |
| 5464 | return OK; |
| 5465 | } |
| 5466 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5467 | void InputDispatcher::removeMonitorChannelLocked(const sp<IBinder>& connectionToken) { |
| 5468 | removeMonitorChannelLocked(connectionToken, mGlobalMonitorsByDisplay); |
| 5469 | removeMonitorChannelLocked(connectionToken, mGestureMonitorsByDisplay); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5470 | } |
| 5471 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5472 | void InputDispatcher::removeMonitorChannelLocked( |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5473 | const sp<IBinder>& connectionToken, |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5474 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5475 | for (auto it = monitorsByDisplay.begin(); it != monitorsByDisplay.end();) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5476 | std::vector<Monitor>& monitors = it->second; |
| 5477 | const size_t numMonitors = monitors.size(); |
| 5478 | for (size_t i = 0; i < numMonitors; i++) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5479 | if (monitors[i].inputChannel->getConnectionToken() == connectionToken) { |
Siarhei Vishniakou | 59a9f29 | 2021-04-22 18:43:28 +0000 | [diff] [blame] | 5480 | ALOGI("Erasing monitor %s on display %" PRId32 ", pid=%" PRId32, |
| 5481 | monitors[i].inputChannel->getName().c_str(), it->first, monitors[i].pid); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5482 | monitors.erase(monitors.begin() + i); |
| 5483 | break; |
| 5484 | } |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5485 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5486 | if (monitors.empty()) { |
| 5487 | it = monitorsByDisplay.erase(it); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5488 | } else { |
| 5489 | ++it; |
| 5490 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5491 | } |
| 5492 | } |
| 5493 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5494 | status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) { |
| 5495 | { // acquire lock |
| 5496 | std::scoped_lock _l(mLock); |
| 5497 | std::optional<int32_t> foundDisplayId = findGestureMonitorDisplayByTokenLocked(token); |
| 5498 | |
| 5499 | if (!foundDisplayId) { |
| 5500 | ALOGW("Attempted to pilfer pointers from an un-registered monitor or invalid token"); |
| 5501 | return BAD_VALUE; |
| 5502 | } |
| 5503 | int32_t displayId = foundDisplayId.value(); |
| 5504 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5505 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 5506 | mTouchStatesByDisplay.find(displayId); |
| 5507 | if (stateIt == mTouchStatesByDisplay.end()) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5508 | ALOGW("Failed to pilfer pointers: no pointers on display %" PRId32 ".", displayId); |
| 5509 | return BAD_VALUE; |
| 5510 | } |
| 5511 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5512 | TouchState& state = stateIt->second; |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5513 | std::shared_ptr<InputChannel> requestingChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5514 | std::optional<int32_t> foundDeviceId; |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 5515 | for (const auto& monitor : state.gestureMonitors) { |
| 5516 | if (monitor.inputChannel->getConnectionToken() == token) { |
| 5517 | requestingChannel = monitor.inputChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5518 | foundDeviceId = state.deviceId; |
| 5519 | } |
| 5520 | } |
| 5521 | if (!foundDeviceId || !state.down) { |
| 5522 | 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] | 5523 | " Ignoring."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5524 | return BAD_VALUE; |
| 5525 | } |
| 5526 | int32_t deviceId = foundDeviceId.value(); |
| 5527 | |
| 5528 | // Send cancel events to all the input channels we're stealing from. |
| 5529 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5530 | "gesture monitor stole pointer stream"); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5531 | options.deviceId = deviceId; |
| 5532 | options.displayId = displayId; |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5533 | std::string canceledWindows = "["; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5534 | for (const TouchedWindow& window : state.windows) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5535 | std::shared_ptr<InputChannel> channel = |
| 5536 | getInputChannelLocked(window.windowHandle->getToken()); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5537 | if (channel != nullptr) { |
| 5538 | synthesizeCancelationEventsForInputChannelLocked(channel, options); |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5539 | canceledWindows += channel->getName() + ", "; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5540 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5541 | } |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5542 | canceledWindows += "]"; |
| 5543 | ALOGI("Monitor %s is stealing touch from %s", requestingChannel->getName().c_str(), |
| 5544 | canceledWindows.c_str()); |
| 5545 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5546 | // Then clear the current touch state so we stop dispatching to them as well. |
| 5547 | state.filterNonMonitors(); |
| 5548 | } |
| 5549 | return OK; |
| 5550 | } |
| 5551 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5552 | void InputDispatcher::requestPointerCapture(const sp<IBinder>& windowToken, bool enabled) { |
| 5553 | { // acquire lock |
| 5554 | std::scoped_lock _l(mLock); |
| 5555 | if (DEBUG_FOCUS) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5556 | const sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(windowToken); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5557 | ALOGI("Request to %s Pointer Capture from: %s.", enabled ? "enable" : "disable", |
| 5558 | windowHandle != nullptr ? windowHandle->getName().c_str() |
| 5559 | : "token without window"); |
| 5560 | } |
| 5561 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5562 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5563 | if (focusedToken != windowToken) { |
| 5564 | ALOGW("Ignoring request to %s Pointer Capture: window does not have focus.", |
| 5565 | enabled ? "enable" : "disable"); |
| 5566 | return; |
| 5567 | } |
| 5568 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5569 | if (enabled == mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5570 | ALOGW("Ignoring request to %s Pointer Capture: " |
| 5571 | "window has %s requested pointer capture.", |
| 5572 | enabled ? "enable" : "disable", enabled ? "already" : "not"); |
| 5573 | return; |
| 5574 | } |
| 5575 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5576 | setPointerCaptureLocked(enabled); |
| 5577 | } // release lock |
| 5578 | |
| 5579 | // Wake the thread to process command entries. |
| 5580 | mLooper->wake(); |
| 5581 | } |
| 5582 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5583 | std::optional<int32_t> InputDispatcher::findGestureMonitorDisplayByTokenLocked( |
| 5584 | const sp<IBinder>& token) { |
| 5585 | for (const auto& it : mGestureMonitorsByDisplay) { |
| 5586 | const std::vector<Monitor>& monitors = it.second; |
| 5587 | for (const Monitor& monitor : monitors) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5588 | if (monitor.inputChannel->getConnectionToken() == token) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5589 | return it.first; |
| 5590 | } |
| 5591 | } |
| 5592 | } |
| 5593 | return std::nullopt; |
| 5594 | } |
| 5595 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5596 | std::optional<int32_t> InputDispatcher::findMonitorPidByTokenLocked(const sp<IBinder>& token) { |
| 5597 | std::optional<int32_t> gesturePid = findMonitorPidByToken(mGestureMonitorsByDisplay, token); |
| 5598 | if (gesturePid.has_value()) { |
| 5599 | return gesturePid; |
| 5600 | } |
| 5601 | return findMonitorPidByToken(mGlobalMonitorsByDisplay, token); |
| 5602 | } |
| 5603 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5604 | sp<Connection> InputDispatcher::getConnectionLocked(const sp<IBinder>& inputConnectionToken) const { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 5605 | if (inputConnectionToken == nullptr) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5606 | return nullptr; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 5607 | } |
| 5608 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5609 | for (const auto& [token, connection] : mConnectionsByToken) { |
| 5610 | if (token == inputConnectionToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5611 | return connection; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5612 | } |
| 5613 | } |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 5614 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5615 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5616 | } |
| 5617 | |
Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 5618 | std::string InputDispatcher::getConnectionNameLocked(const sp<IBinder>& connectionToken) const { |
| 5619 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 5620 | if (connection == nullptr) { |
| 5621 | return "<nullptr>"; |
| 5622 | } |
| 5623 | return connection->getInputChannelName(); |
| 5624 | } |
| 5625 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5626 | void InputDispatcher::removeConnectionLocked(const sp<Connection>& connection) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5627 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5628 | mConnectionsByToken.erase(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5629 | } |
| 5630 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5631 | void InputDispatcher::doDispatchCycleFinishedCommand(nsecs_t finishTime, |
| 5632 | const sp<Connection>& connection, uint32_t seq, |
| 5633 | bool handled, nsecs_t consumeTime) { |
| 5634 | // Handle post-event policy actions. |
| 5635 | std::deque<DispatchEntry*>::iterator dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 5636 | if (dispatchEntryIt == connection->waitQueue.end()) { |
| 5637 | return; |
| 5638 | } |
| 5639 | DispatchEntry* dispatchEntry = *dispatchEntryIt; |
| 5640 | const nsecs_t eventDuration = finishTime - dispatchEntry->deliveryTime; |
| 5641 | if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) { |
| 5642 | ALOGI("%s spent %" PRId64 "ms processing %s", connection->getWindowName().c_str(), |
| 5643 | ns2ms(eventDuration), dispatchEntry->eventEntry->getDescription().c_str()); |
| 5644 | } |
| 5645 | if (shouldReportFinishedEvent(*dispatchEntry, *connection)) { |
| 5646 | mLatencyTracker.trackFinishedEvent(dispatchEntry->eventEntry->id, |
| 5647 | connection->inputChannel->getConnectionToken(), |
| 5648 | dispatchEntry->deliveryTime, consumeTime, finishTime); |
| 5649 | } |
| 5650 | |
| 5651 | bool restartEvent; |
| 5652 | if (dispatchEntry->eventEntry->type == EventEntry::Type::KEY) { |
| 5653 | KeyEntry& keyEntry = static_cast<KeyEntry&>(*(dispatchEntry->eventEntry)); |
| 5654 | restartEvent = |
| 5655 | afterKeyEventLockedInterruptable(connection, dispatchEntry, keyEntry, handled); |
| 5656 | } else if (dispatchEntry->eventEntry->type == EventEntry::Type::MOTION) { |
| 5657 | MotionEntry& motionEntry = static_cast<MotionEntry&>(*(dispatchEntry->eventEntry)); |
| 5658 | restartEvent = afterMotionEventLockedInterruptable(connection, dispatchEntry, motionEntry, |
| 5659 | handled); |
| 5660 | } else { |
| 5661 | restartEvent = false; |
| 5662 | } |
| 5663 | |
| 5664 | // Dequeue the event and start the next cycle. |
| 5665 | // Because the lock might have been released, it is possible that the |
| 5666 | // contents of the wait queue to have been drained, so we need to double-check |
| 5667 | // a few things. |
| 5668 | dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 5669 | if (dispatchEntryIt != connection->waitQueue.end()) { |
| 5670 | dispatchEntry = *dispatchEntryIt; |
| 5671 | connection->waitQueue.erase(dispatchEntryIt); |
| 5672 | const sp<IBinder>& connectionToken = connection->inputChannel->getConnectionToken(); |
| 5673 | mAnrTracker.erase(dispatchEntry->timeoutTime, connectionToken); |
| 5674 | if (!connection->responsive) { |
| 5675 | connection->responsive = isConnectionResponsive(*connection); |
| 5676 | if (connection->responsive) { |
| 5677 | // The connection was unresponsive, and now it's responsive. |
| 5678 | processConnectionResponsiveLocked(*connection); |
| 5679 | } |
| 5680 | } |
| 5681 | traceWaitQueueLength(*connection); |
| 5682 | if (restartEvent && connection->status == Connection::STATUS_NORMAL) { |
| 5683 | connection->outboundQueue.push_front(dispatchEntry); |
| 5684 | traceOutboundQueueLength(*connection); |
| 5685 | } else { |
| 5686 | releaseDispatchEntry(dispatchEntry); |
| 5687 | } |
| 5688 | } |
| 5689 | |
| 5690 | // Start the next dispatch cycle for this connection. |
| 5691 | startDispatchCycleLocked(now(), connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5692 | } |
| 5693 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5694 | void InputDispatcher::sendFocusChangedCommandLocked(const sp<IBinder>& oldToken, |
| 5695 | const sp<IBinder>& newToken) { |
| 5696 | auto command = [this, oldToken, newToken]() REQUIRES(mLock) { |
| 5697 | scoped_unlock unlock(mLock); |
| 5698 | mPolicy->notifyFocusChanged(oldToken, newToken); |
| 5699 | }; |
| 5700 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5701 | } |
| 5702 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5703 | void InputDispatcher::sendDropWindowCommandLocked(const sp<IBinder>& token, float x, float y) { |
| 5704 | auto command = [this, token, x, y]() REQUIRES(mLock) { |
| 5705 | scoped_unlock unlock(mLock); |
| 5706 | mPolicy->notifyDropWindow(token, x, y); |
| 5707 | }; |
| 5708 | postCommandLocked(std::move(command)); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5709 | } |
| 5710 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5711 | void InputDispatcher::sendUntrustedTouchCommandLocked(const std::string& obscuringPackage) { |
| 5712 | auto command = [this, obscuringPackage]() REQUIRES(mLock) { |
| 5713 | scoped_unlock unlock(mLock); |
| 5714 | mPolicy->notifyUntrustedTouch(obscuringPackage); |
| 5715 | }; |
| 5716 | postCommandLocked(std::move(command)); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 5717 | } |
| 5718 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5719 | void InputDispatcher::onAnrLocked(const sp<Connection>& connection) { |
| 5720 | if (connection == nullptr) { |
| 5721 | LOG_ALWAYS_FATAL("Caller must check for nullness"); |
| 5722 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5723 | // Since we are allowing the policy to extend the timeout, maybe the waitQueue |
| 5724 | // is already healthy again. Don't raise ANR in this situation |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5725 | if (connection->waitQueue.empty()) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5726 | ALOGI("Not raising ANR because the connection %s has recovered", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5727 | connection->inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5728 | return; |
| 5729 | } |
| 5730 | /** |
| 5731 | * The "oldestEntry" is the entry that was first sent to the application. That entry, however, |
| 5732 | * may not be the one that caused the timeout to occur. One possibility is that window timeout |
| 5733 | * has changed. This could cause newer entries to time out before the already dispatched |
| 5734 | * entries. In that situation, the newest entries caused ANR. But in all likelihood, the app |
| 5735 | * processes the events linearly. So providing information about the oldest entry seems to be |
| 5736 | * most useful. |
| 5737 | */ |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5738 | DispatchEntry* oldestEntry = *connection->waitQueue.begin(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5739 | const nsecs_t currentWait = now() - oldestEntry->deliveryTime; |
| 5740 | std::string reason = |
| 5741 | android::base::StringPrintf("%s is not responding. Waited %" PRId64 "ms for %s", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5742 | connection->inputChannel->getName().c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5743 | ns2ms(currentWait), |
| 5744 | oldestEntry->eventEntry->getDescription().c_str()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5745 | sp<IBinder> connectionToken = connection->inputChannel->getConnectionToken(); |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 5746 | updateLastAnrStateLocked(getWindowHandleLocked(connectionToken), reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5747 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5748 | processConnectionUnresponsiveLocked(*connection, std::move(reason)); |
| 5749 | |
| 5750 | // Stop waking up for events on this connection, it is already unresponsive |
| 5751 | cancelEventsForAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5752 | } |
| 5753 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5754 | void InputDispatcher::onAnrLocked(std::shared_ptr<InputApplicationHandle> application) { |
| 5755 | std::string reason = |
| 5756 | StringPrintf("%s does not have a focused window", application->getName().c_str()); |
| 5757 | updateLastAnrStateLocked(*application, reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5758 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5759 | auto command = [this, application = std::move(application)]() REQUIRES(mLock) { |
| 5760 | scoped_unlock unlock(mLock); |
| 5761 | mPolicy->notifyNoFocusedWindowAnr(application); |
| 5762 | }; |
| 5763 | postCommandLocked(std::move(command)); |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 5764 | } |
| 5765 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5766 | void InputDispatcher::updateLastAnrStateLocked(const sp<WindowInfoHandle>& window, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5767 | const std::string& reason) { |
| 5768 | const std::string windowLabel = getApplicationWindowLabel(nullptr, window); |
| 5769 | updateLastAnrStateLocked(windowLabel, reason); |
| 5770 | } |
| 5771 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5772 | void InputDispatcher::updateLastAnrStateLocked(const InputApplicationHandle& application, |
| 5773 | const std::string& reason) { |
| 5774 | const std::string windowLabel = getApplicationWindowLabel(&application, nullptr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5775 | updateLastAnrStateLocked(windowLabel, reason); |
| 5776 | } |
| 5777 | |
| 5778 | void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel, |
| 5779 | const std::string& reason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5780 | // 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] | 5781 | time_t t = time(nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5782 | struct tm tm; |
| 5783 | localtime_r(&t, &tm); |
| 5784 | char timestr[64]; |
| 5785 | strftime(timestr, sizeof(timestr), "%F %T", &tm); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5786 | mLastAnrState.clear(); |
| 5787 | mLastAnrState += INDENT "ANR:\n"; |
| 5788 | mLastAnrState += StringPrintf(INDENT2 "Time: %s\n", timestr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5789 | mLastAnrState += StringPrintf(INDENT2 "Reason: %s\n", reason.c_str()); |
| 5790 | mLastAnrState += StringPrintf(INDENT2 "Window: %s\n", windowLabel.c_str()); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5791 | dumpDispatchStateLocked(mLastAnrState); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5792 | } |
| 5793 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5794 | void InputDispatcher::doInterceptKeyBeforeDispatchingCommand(const sp<IBinder>& focusedWindowToken, |
| 5795 | KeyEntry& entry) { |
| 5796 | const KeyEvent event = createKeyEvent(entry); |
| 5797 | nsecs_t delay = 0; |
| 5798 | { // release lock |
| 5799 | scoped_unlock unlock(mLock); |
| 5800 | android::base::Timer t; |
| 5801 | delay = mPolicy->interceptKeyBeforeDispatching(focusedWindowToken, &event, |
| 5802 | entry.policyFlags); |
| 5803 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 5804 | ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms", |
| 5805 | std::to_string(t.duration().count()).c_str()); |
| 5806 | } |
| 5807 | } // acquire lock |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5808 | |
| 5809 | if (delay < 0) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5810 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_SKIP; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5811 | } else if (delay == 0) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5812 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5813 | } else { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5814 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER; |
| 5815 | entry.interceptKeyWakeupTime = now() + delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5816 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5817 | } |
| 5818 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5819 | void InputDispatcher::sendMonitorUnresponsiveCommandLocked(int32_t pid, std::string reason) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5820 | auto command = [this, pid, reason = std::move(reason)]() REQUIRES(mLock) { |
| 5821 | scoped_unlock unlock(mLock); |
| 5822 | mPolicy->notifyMonitorUnresponsive(pid, reason); |
| 5823 | }; |
| 5824 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5825 | } |
| 5826 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5827 | void InputDispatcher::sendWindowUnresponsiveCommandLocked(const sp<IBinder>& token, |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5828 | std::string reason) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5829 | auto command = [this, token, reason = std::move(reason)]() REQUIRES(mLock) { |
| 5830 | scoped_unlock unlock(mLock); |
| 5831 | mPolicy->notifyWindowUnresponsive(token, reason); |
| 5832 | }; |
| 5833 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5834 | } |
| 5835 | |
| 5836 | void InputDispatcher::sendMonitorResponsiveCommandLocked(int32_t pid) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5837 | auto command = [this, pid]() REQUIRES(mLock) { |
| 5838 | scoped_unlock unlock(mLock); |
| 5839 | mPolicy->notifyMonitorResponsive(pid); |
| 5840 | }; |
| 5841 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5842 | } |
| 5843 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5844 | void InputDispatcher::sendWindowResponsiveCommandLocked(const sp<IBinder>& connectionToken) { |
| 5845 | auto command = [this, connectionToken]() REQUIRES(mLock) { |
| 5846 | scoped_unlock unlock(mLock); |
| 5847 | mPolicy->notifyWindowResponsive(connectionToken); |
| 5848 | }; |
| 5849 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5850 | } |
| 5851 | |
| 5852 | /** |
| 5853 | * Tell the policy that a connection has become unresponsive so that it can start ANR. |
| 5854 | * Check whether the connection of interest is a monitor or a window, and add the corresponding |
| 5855 | * command entry to the command queue. |
| 5856 | */ |
| 5857 | void InputDispatcher::processConnectionUnresponsiveLocked(const Connection& connection, |
| 5858 | std::string reason) { |
| 5859 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
| 5860 | if (connection.monitor) { |
| 5861 | ALOGW("Monitor %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 5862 | reason.c_str()); |
| 5863 | std::optional<int32_t> pid = findMonitorPidByTokenLocked(connectionToken); |
| 5864 | if (!pid.has_value()) { |
| 5865 | ALOGE("Could not find unresponsive monitor for connection %s", |
| 5866 | connection.inputChannel->getName().c_str()); |
| 5867 | return; |
| 5868 | } |
| 5869 | sendMonitorUnresponsiveCommandLocked(pid.value(), std::move(reason)); |
| 5870 | return; |
| 5871 | } |
| 5872 | // If not a monitor, must be a window |
| 5873 | ALOGW("Window %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 5874 | reason.c_str()); |
| 5875 | sendWindowUnresponsiveCommandLocked(connectionToken, std::move(reason)); |
| 5876 | } |
| 5877 | |
| 5878 | /** |
| 5879 | * Tell the policy that a connection has become responsive so that it can stop ANR. |
| 5880 | */ |
| 5881 | void InputDispatcher::processConnectionResponsiveLocked(const Connection& connection) { |
| 5882 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
| 5883 | if (connection.monitor) { |
| 5884 | std::optional<int32_t> pid = findMonitorPidByTokenLocked(connectionToken); |
| 5885 | if (!pid.has_value()) { |
| 5886 | ALOGE("Could not find responsive monitor for connection %s", |
| 5887 | connection.inputChannel->getName().c_str()); |
| 5888 | return; |
| 5889 | } |
| 5890 | sendMonitorResponsiveCommandLocked(pid.value()); |
| 5891 | return; |
| 5892 | } |
| 5893 | // If not a monitor, must be a window |
| 5894 | sendWindowResponsiveCommandLocked(connectionToken); |
| 5895 | } |
| 5896 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5897 | bool InputDispatcher::afterKeyEventLockedInterruptable(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5898 | DispatchEntry* dispatchEntry, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5899 | KeyEntry& keyEntry, bool handled) { |
| 5900 | if (keyEntry.flags & AKEY_EVENT_FLAG_FALLBACK) { |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5901 | if (!handled) { |
| 5902 | // Report the key as unhandled, since the fallback was not handled. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5903 | mReporter->reportUnhandledKey(keyEntry.id); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5904 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5905 | return false; |
| 5906 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5907 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5908 | // Get the fallback key state. |
| 5909 | // Clear it out after dispatching the UP. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5910 | int32_t originalKeyCode = keyEntry.keyCode; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5911 | int32_t fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5912 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5913 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 5914 | } |
| 5915 | |
| 5916 | if (handled || !dispatchEntry->hasForegroundTarget()) { |
| 5917 | // If the application handles the original key for which we previously |
| 5918 | // generated a fallback or if the window is not a foreground window, |
| 5919 | // then cancel the associated fallback key, if any. |
| 5920 | if (fallbackKeyCode != -1) { |
| 5921 | // Dispatch the unhandled key to the policy with the cancel flag. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5922 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 5923 | ALOGD("Unhandled key event: Asking policy to cancel fallback action. " |
| 5924 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 5925 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, |
| 5926 | keyEntry.policyFlags); |
| 5927 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5928 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5929 | event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5930 | |
| 5931 | mLock.unlock(); |
| 5932 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5933 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), &event, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5934 | keyEntry.policyFlags, &event); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5935 | |
| 5936 | mLock.lock(); |
| 5937 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5938 | // Cancel the fallback key. |
| 5939 | if (fallbackKeyCode != AKEYCODE_UNKNOWN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5940 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5941 | "application handled the original non-fallback key " |
| 5942 | "or is no longer a foreground target, " |
| 5943 | "canceling previously dispatched fallback key"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5944 | options.keyCode = fallbackKeyCode; |
| 5945 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5946 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5947 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 5948 | } |
| 5949 | } else { |
| 5950 | // If the application did not handle a non-fallback key, first check |
| 5951 | // that we are in a good state to perform unhandled key event processing |
| 5952 | // Then ask the policy what to do with it. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5953 | bool initialDown = keyEntry.action == AKEY_EVENT_ACTION_DOWN && keyEntry.repeatCount == 0; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5954 | if (fallbackKeyCode == -1 && !initialDown) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5955 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 5956 | ALOGD("Unhandled key event: Skipping unhandled key event processing " |
| 5957 | "since this is not an initial down. " |
| 5958 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 5959 | originalKeyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 5960 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5961 | return false; |
| 5962 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5963 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5964 | // Dispatch the unhandled key to the policy. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5965 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 5966 | ALOGD("Unhandled key event: Asking policy to perform fallback action. " |
| 5967 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 5968 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 5969 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5970 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5971 | |
| 5972 | mLock.unlock(); |
| 5973 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5974 | bool fallback = |
| 5975 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5976 | &event, keyEntry.policyFlags, &event); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5977 | |
| 5978 | mLock.lock(); |
| 5979 | |
| 5980 | if (connection->status != Connection::STATUS_NORMAL) { |
| 5981 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 5982 | return false; |
| 5983 | } |
| 5984 | |
| 5985 | // Latch the fallback keycode for this key on an initial down. |
| 5986 | // The fallback keycode cannot change at any other point in the lifecycle. |
| 5987 | if (initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5988 | if (fallback) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5989 | fallbackKeyCode = event.getKeyCode(); |
| 5990 | } else { |
| 5991 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
| 5992 | } |
| 5993 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
| 5994 | } |
| 5995 | |
| 5996 | ALOG_ASSERT(fallbackKeyCode != -1); |
| 5997 | |
| 5998 | // Cancel the fallback key if the policy decides not to send it anymore. |
| 5999 | // We will continue to dispatch the key to the policy but we will no |
| 6000 | // longer dispatch a fallback key to the application. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6001 | if (fallbackKeyCode != AKEYCODE_UNKNOWN && |
| 6002 | (!fallback || fallbackKeyCode != event.getKeyCode())) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6003 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6004 | if (fallback) { |
| 6005 | ALOGD("Unhandled key event: Policy requested to send key %d" |
| 6006 | "as a fallback for %d, but on the DOWN it had requested " |
| 6007 | "to send %d instead. Fallback canceled.", |
| 6008 | event.getKeyCode(), originalKeyCode, fallbackKeyCode); |
| 6009 | } else { |
| 6010 | ALOGD("Unhandled key event: Policy did not request fallback for %d, " |
| 6011 | "but on the DOWN it had requested to send %d. " |
| 6012 | "Fallback canceled.", |
| 6013 | originalKeyCode, fallbackKeyCode); |
| 6014 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6015 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6016 | |
| 6017 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
| 6018 | "canceling fallback, policy no longer desires it"); |
| 6019 | options.keyCode = fallbackKeyCode; |
| 6020 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
| 6021 | |
| 6022 | fallback = false; |
| 6023 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6024 | if (keyEntry.action != AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6025 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6026 | } |
| 6027 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6028 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6029 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6030 | { |
| 6031 | std::string msg; |
| 6032 | const KeyedVector<int32_t, int32_t>& fallbackKeys = |
| 6033 | connection->inputState.getFallbackKeys(); |
| 6034 | for (size_t i = 0; i < fallbackKeys.size(); i++) { |
| 6035 | msg += StringPrintf(", %d->%d", fallbackKeys.keyAt(i), fallbackKeys.valueAt(i)); |
| 6036 | } |
| 6037 | ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.", |
| 6038 | fallbackKeys.size(), msg.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6039 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6040 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6041 | |
| 6042 | if (fallback) { |
| 6043 | // Restart the dispatch cycle using the fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6044 | keyEntry.eventTime = event.getEventTime(); |
| 6045 | keyEntry.deviceId = event.getDeviceId(); |
| 6046 | keyEntry.source = event.getSource(); |
| 6047 | keyEntry.displayId = event.getDisplayId(); |
| 6048 | keyEntry.flags = event.getFlags() | AKEY_EVENT_FLAG_FALLBACK; |
| 6049 | keyEntry.keyCode = fallbackKeyCode; |
| 6050 | keyEntry.scanCode = event.getScanCode(); |
| 6051 | keyEntry.metaState = event.getMetaState(); |
| 6052 | keyEntry.repeatCount = event.getRepeatCount(); |
| 6053 | keyEntry.downTime = event.getDownTime(); |
| 6054 | keyEntry.syntheticRepeat = false; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6055 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6056 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6057 | ALOGD("Unhandled key event: Dispatching fallback key. " |
| 6058 | "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x", |
| 6059 | originalKeyCode, fallbackKeyCode, keyEntry.metaState); |
| 6060 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6061 | return true; // restart the event |
| 6062 | } else { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6063 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6064 | ALOGD("Unhandled key event: No fallback key."); |
| 6065 | } |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6066 | |
| 6067 | // Report the key as unhandled, since there is no fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6068 | mReporter->reportUnhandledKey(keyEntry.id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6069 | } |
| 6070 | } |
| 6071 | return false; |
| 6072 | } |
| 6073 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6074 | bool InputDispatcher::afterMotionEventLockedInterruptable(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6075 | DispatchEntry* dispatchEntry, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6076 | MotionEntry& motionEntry, bool handled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6077 | return false; |
| 6078 | } |
| 6079 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6080 | void InputDispatcher::traceInboundQueueLengthLocked() { |
| 6081 | if (ATRACE_ENABLED()) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 6082 | ATRACE_INT("iq", mInboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6083 | } |
| 6084 | } |
| 6085 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6086 | void InputDispatcher::traceOutboundQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6087 | if (ATRACE_ENABLED()) { |
| 6088 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6089 | snprintf(counterName, sizeof(counterName), "oq:%s", connection.getWindowName().c_str()); |
| 6090 | ATRACE_INT(counterName, connection.outboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6091 | } |
| 6092 | } |
| 6093 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6094 | void InputDispatcher::traceWaitQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6095 | if (ATRACE_ENABLED()) { |
| 6096 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6097 | snprintf(counterName, sizeof(counterName), "wq:%s", connection.getWindowName().c_str()); |
| 6098 | ATRACE_INT(counterName, connection.waitQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6099 | } |
| 6100 | } |
| 6101 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6102 | void InputDispatcher::dump(std::string& dump) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6103 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6104 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6105 | dump += "Input Dispatcher State:\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6106 | dumpDispatchStateLocked(dump); |
| 6107 | |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6108 | if (!mLastAnrState.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6109 | dump += "\nInput Dispatcher State at time of last ANR:\n"; |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6110 | dump += mLastAnrState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6111 | } |
| 6112 | } |
| 6113 | |
| 6114 | void InputDispatcher::monitor() { |
| 6115 | // 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] | 6116 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6117 | mLooper->wake(); |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6118 | mDispatcherIsAlive.wait(_l); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6119 | } |
| 6120 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6121 | /** |
| 6122 | * Wake up the dispatcher and wait until it processes all events and commands. |
| 6123 | * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so |
| 6124 | * this method can be safely called from any thread, as long as you've ensured that |
| 6125 | * the work you are interested in completing has already been queued. |
| 6126 | */ |
| 6127 | bool InputDispatcher::waitForIdle() { |
| 6128 | /** |
| 6129 | * Timeout should represent the longest possible time that a device might spend processing |
| 6130 | * events and commands. |
| 6131 | */ |
| 6132 | constexpr std::chrono::duration TIMEOUT = 100ms; |
| 6133 | std::unique_lock lock(mLock); |
| 6134 | mLooper->wake(); |
| 6135 | std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT); |
| 6136 | return result == std::cv_status::no_timeout; |
| 6137 | } |
| 6138 | |
Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 6139 | /** |
| 6140 | * Sets focus to the window identified by the token. This must be called |
| 6141 | * after updating any input window handles. |
| 6142 | * |
| 6143 | * Params: |
| 6144 | * request.token - input channel token used to identify the window that should gain focus. |
| 6145 | * request.focusedToken - the token that the caller expects currently to be focused. If the |
| 6146 | * specified token does not match the currently focused window, this request will be dropped. |
| 6147 | * If the specified focused token matches the currently focused window, the call will succeed. |
| 6148 | * Set this to "null" if this call should succeed no matter what the currently focused token is. |
| 6149 | * request.timestamp - SYSTEM_TIME_MONOTONIC timestamp in nanos set by the client (wm) |
| 6150 | * when requesting the focus change. This determines which request gets |
| 6151 | * precedence if there is a focus change request from another source such as pointer down. |
| 6152 | */ |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6153 | void InputDispatcher::setFocusedWindow(const FocusRequest& request) { |
| 6154 | { // acquire lock |
| 6155 | std::scoped_lock _l(mLock); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6156 | std::optional<FocusResolver::FocusChanges> changes = |
| 6157 | mFocusResolver.setFocusedWindow(request, getWindowHandlesLocked(request.displayId)); |
| 6158 | if (changes) { |
| 6159 | onFocusChangedLocked(*changes); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6160 | } |
| 6161 | } // release lock |
| 6162 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6163 | mLooper->wake(); |
| 6164 | } |
| 6165 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6166 | void InputDispatcher::onFocusChangedLocked(const FocusResolver::FocusChanges& changes) { |
| 6167 | if (changes.oldFocus) { |
| 6168 | std::shared_ptr<InputChannel> focusedInputChannel = getInputChannelLocked(changes.oldFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6169 | if (focusedInputChannel) { |
| 6170 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 6171 | "focus left window"); |
| 6172 | synthesizeCancelationEventsForInputChannelLocked(focusedInputChannel, options); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6173 | enqueueFocusEventLocked(changes.oldFocus, false /*hasFocus*/, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6174 | } |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6175 | } |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6176 | if (changes.newFocus) { |
| 6177 | enqueueFocusEventLocked(changes.newFocus, true /*hasFocus*/, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6178 | } |
| 6179 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6180 | // If a window has pointer capture, then it must have focus. We need to ensure that this |
| 6181 | // contract is upheld when pointer capture is being disabled due to a loss of window focus. |
| 6182 | // If the window loses focus before it loses pointer capture, then the window can be in a state |
| 6183 | // where it has pointer capture but not focus, violating the contract. Therefore we must |
| 6184 | // dispatch the pointer capture event before the focus event. Since focus events are added to |
| 6185 | // the front of the queue (above), we add the pointer capture event to the front of the queue |
| 6186 | // after the focus events are added. This ensures the pointer capture event ends up at the |
| 6187 | // front. |
| 6188 | disablePointerCaptureForcedLocked(); |
| 6189 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6190 | if (mFocusedDisplayId == changes.displayId) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6191 | sendFocusChangedCommandLocked(changes.oldFocus, changes.newFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6192 | } |
| 6193 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6194 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6195 | void InputDispatcher::disablePointerCaptureForcedLocked() { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6196 | if (!mCurrentPointerCaptureRequest.enable && !mWindowTokenWithPointerCapture) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6197 | return; |
| 6198 | } |
| 6199 | |
| 6200 | ALOGD_IF(DEBUG_FOCUS, "Disabling Pointer Capture because the window lost focus."); |
| 6201 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6202 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6203 | setPointerCaptureLocked(false); |
| 6204 | } |
| 6205 | |
| 6206 | if (!mWindowTokenWithPointerCapture) { |
| 6207 | // No need to send capture changes because no window has capture. |
| 6208 | return; |
| 6209 | } |
| 6210 | |
| 6211 | if (mPendingEvent != nullptr) { |
| 6212 | // Move the pending event to the front of the queue. This will give the chance |
| 6213 | // for the pending event to be dropped if it is a captured event. |
| 6214 | mInboundQueue.push_front(mPendingEvent); |
| 6215 | mPendingEvent = nullptr; |
| 6216 | } |
| 6217 | |
| 6218 | auto entry = std::make_unique<PointerCaptureChangedEntry>(mIdGenerator.nextId(), now(), |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6219 | mCurrentPointerCaptureRequest); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6220 | mInboundQueue.push_front(std::move(entry)); |
| 6221 | } |
| 6222 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6223 | void InputDispatcher::setPointerCaptureLocked(bool enable) { |
| 6224 | mCurrentPointerCaptureRequest.enable = enable; |
| 6225 | mCurrentPointerCaptureRequest.seq++; |
| 6226 | auto command = [this, request = mCurrentPointerCaptureRequest]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6227 | scoped_unlock unlock(mLock); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6228 | mPolicy->setPointerCapture(request); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6229 | }; |
| 6230 | postCommandLocked(std::move(command)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6231 | } |
| 6232 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6233 | void InputDispatcher::displayRemoved(int32_t displayId) { |
| 6234 | { // acquire lock |
| 6235 | std::scoped_lock _l(mLock); |
| 6236 | // Set an empty list to remove all handles from the specific display. |
| 6237 | setInputWindowsLocked(/* window handles */ {}, displayId); |
| 6238 | setFocusedApplicationLocked(displayId, nullptr); |
| 6239 | // Call focus resolver to clean up stale requests. This must be called after input windows |
| 6240 | // have been removed for the removed display. |
| 6241 | mFocusResolver.displayRemoved(displayId); |
| 6242 | } // release lock |
| 6243 | |
| 6244 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6245 | mLooper->wake(); |
| 6246 | } |
| 6247 | |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6248 | void InputDispatcher::onWindowInfosChanged(const std::vector<WindowInfo>& windowInfos, |
| 6249 | const std::vector<DisplayInfo>& displayInfos) { |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6250 | // The listener sends the windows as a flattened array. Separate the windows by display for |
| 6251 | // more convenient parsing. |
| 6252 | std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>> handlesPerDisplay; |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6253 | for (const auto& info : windowInfos) { |
| 6254 | handlesPerDisplay.emplace(info.displayId, std::vector<sp<WindowInfoHandle>>()); |
| 6255 | handlesPerDisplay[info.displayId].push_back(new WindowInfoHandle(info)); |
| 6256 | } |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6257 | |
| 6258 | { // acquire lock |
| 6259 | std::scoped_lock _l(mLock); |
| 6260 | mDisplayInfos.clear(); |
| 6261 | for (const auto& displayInfo : displayInfos) { |
| 6262 | mDisplayInfos.emplace(displayInfo.displayId, displayInfo); |
| 6263 | } |
| 6264 | |
| 6265 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 6266 | setInputWindowsLocked(handles, displayId); |
| 6267 | } |
| 6268 | } |
| 6269 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6270 | mLooper->wake(); |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6271 | } |
| 6272 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6273 | bool InputDispatcher::shouldDropInput( |
| 6274 | const EventEntry& entry, const sp<android::gui::WindowInfoHandle>& windowHandle) const { |
| 6275 | if (windowHandle->getInfo()->inputFeatures.test(WindowInfo::Feature::DROP_INPUT) || |
| 6276 | (windowHandle->getInfo()->inputFeatures.test(WindowInfo::Feature::DROP_INPUT_IF_OBSCURED) && |
| 6277 | isWindowObscuredLocked(windowHandle))) { |
| 6278 | ALOGW("Dropping %s event targeting %s as requested by input feature %s on display " |
| 6279 | "%" PRId32 ".", |
| 6280 | ftl::enum_string(entry.type).c_str(), windowHandle->getName().c_str(), |
| 6281 | windowHandle->getInfo()->inputFeatures.string().c_str(), |
| 6282 | windowHandle->getInfo()->displayId); |
| 6283 | return true; |
| 6284 | } |
| 6285 | return false; |
| 6286 | } |
| 6287 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 6288 | void InputDispatcher::DispatcherWindowListener::onWindowInfosChanged( |
| 6289 | const std::vector<gui::WindowInfo>& windowInfos, |
| 6290 | const std::vector<DisplayInfo>& displayInfos) { |
| 6291 | mDispatcher.onWindowInfosChanged(windowInfos, displayInfos); |
| 6292 | } |
| 6293 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 6294 | } // namespace android::inputdispatcher |