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 | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 527 | } // namespace |
| 528 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 529 | // --- InputDispatcher --- |
| 530 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 531 | InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy) |
| 532 | : mPolicy(policy), |
| 533 | mPendingEvent(nullptr), |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 534 | mLastDropReason(DropReason::NOT_DROPPED), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 535 | mIdGenerator(IdGenerator::Source::INPUT_DISPATCHER), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 536 | mAppSwitchSawKeyDown(false), |
| 537 | mAppSwitchDueTime(LONG_LONG_MAX), |
| 538 | mNextUnblockedEvent(nullptr), |
| 539 | mDispatchEnabled(false), |
| 540 | mDispatchFrozen(false), |
| 541 | mInputFilterEnabled(false), |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 542 | // mInTouchMode will be initialized by the WindowManager to the default device config. |
| 543 | // To avoid leaking stack in case that call never comes, and for tests, |
| 544 | // initialize it here anyways. |
| 545 | mInTouchMode(true), |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 546 | mMaximumObscuringOpacityForTouch(1.0f), |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 547 | mFocusedDisplayId(ADISPLAY_ID_DEFAULT), |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 548 | mWindowTokenWithPointerCapture(nullptr), |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 549 | mLatencyAggregator(), |
| 550 | mLatencyTracker(&mLatencyAggregator), |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 551 | mCompatService(getCompatService()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 552 | mLooper = new Looper(false); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 553 | mReporter = createInputReporter(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 554 | |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 555 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 556 | |
| 557 | policy->getDispatcherConfiguration(&mConfig); |
| 558 | } |
| 559 | |
| 560 | InputDispatcher::~InputDispatcher() { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 561 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 562 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 563 | resetKeyRepeatLocked(); |
| 564 | releasePendingEventLocked(); |
| 565 | drainInboundQueueLocked(); |
| 566 | mCommandQueue.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 567 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 568 | while (!mConnectionsByToken.empty()) { |
| 569 | sp<Connection> connection = mConnectionsByToken.begin()->second; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 570 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), |
| 571 | false /* notify */); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 572 | } |
| 573 | } |
| 574 | |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 575 | void InputDispatcher::onFirstRef() { |
| 576 | SurfaceComposerClient::getDefault()->addWindowInfosListener(this); |
| 577 | } |
| 578 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 579 | status_t InputDispatcher::start() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 580 | if (mThread) { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 581 | return ALREADY_EXISTS; |
| 582 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 583 | mThread = std::make_unique<InputThread>( |
| 584 | "InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); }); |
| 585 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | status_t InputDispatcher::stop() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 589 | if (mThread && mThread->isCallingThread()) { |
| 590 | ALOGE("InputDispatcher cannot be stopped from its own thread!"); |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 591 | return INVALID_OPERATION; |
| 592 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 593 | mThread.reset(); |
| 594 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 595 | } |
| 596 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 597 | void InputDispatcher::dispatchOnce() { |
| 598 | nsecs_t nextWakeupTime = LONG_LONG_MAX; |
| 599 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 600 | std::scoped_lock _l(mLock); |
| 601 | mDispatcherIsAlive.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 602 | |
| 603 | // Run a dispatch loop if there are no pending commands. |
| 604 | // The dispatch loop might enqueue commands to run afterwards. |
| 605 | if (!haveCommandsLocked()) { |
| 606 | dispatchOnceInnerLocked(&nextWakeupTime); |
| 607 | } |
| 608 | |
| 609 | // Run all pending commands if there are any. |
| 610 | // 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] | 611 | if (runCommandsLockedInterruptable()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 612 | nextWakeupTime = LONG_LONG_MIN; |
| 613 | } |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 614 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 615 | // If we are still waiting for ack on some events, |
| 616 | // we might have to wake up earlier to check if an app is anr'ing. |
| 617 | const nsecs_t nextAnrCheck = processAnrsLocked(); |
| 618 | nextWakeupTime = std::min(nextWakeupTime, nextAnrCheck); |
| 619 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 620 | // We are about to enter an infinitely long sleep, because we have no commands or |
| 621 | // pending or queued events |
| 622 | if (nextWakeupTime == LONG_LONG_MAX) { |
| 623 | mDispatcherEnteredIdle.notify_all(); |
| 624 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 625 | } // release lock |
| 626 | |
| 627 | // Wait for callback or timeout or wake. (make sure we round up, not down) |
| 628 | nsecs_t currentTime = now(); |
| 629 | int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime); |
| 630 | mLooper->pollOnce(timeoutMillis); |
| 631 | } |
| 632 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 633 | /** |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 634 | * Raise ANR if there is no focused window. |
| 635 | * Before the ANR is raised, do a final state check: |
| 636 | * 1. The currently focused application must be the same one we are waiting for. |
| 637 | * 2. Ensure we still don't have a focused window. |
| 638 | */ |
| 639 | void InputDispatcher::processNoFocusedWindowAnrLocked() { |
| 640 | // Check if the application that we are waiting for is still focused. |
| 641 | std::shared_ptr<InputApplicationHandle> focusedApplication = |
| 642 | getValueByKey(mFocusedApplicationHandlesByDisplay, mAwaitedApplicationDisplayId); |
| 643 | if (focusedApplication == nullptr || |
| 644 | focusedApplication->getApplicationToken() != |
| 645 | mAwaitedFocusedApplication->getApplicationToken()) { |
| 646 | // Unexpected because we should have reset the ANR timer when focused application changed |
| 647 | ALOGE("Waited for a focused window, but focused application has already changed to %s", |
| 648 | focusedApplication->getName().c_str()); |
| 649 | return; // The focused application has changed. |
| 650 | } |
| 651 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 652 | const sp<WindowInfoHandle>& focusedWindowHandle = |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 653 | getFocusedWindowHandleLocked(mAwaitedApplicationDisplayId); |
| 654 | if (focusedWindowHandle != nullptr) { |
| 655 | return; // We now have a focused window. No need for ANR. |
| 656 | } |
| 657 | onAnrLocked(mAwaitedFocusedApplication); |
| 658 | } |
| 659 | |
| 660 | /** |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 661 | * Check if any of the connections' wait queues have events that are too old. |
| 662 | * If we waited for events to be ack'ed for more than the window timeout, raise an ANR. |
| 663 | * Return the time at which we should wake up next. |
| 664 | */ |
| 665 | nsecs_t InputDispatcher::processAnrsLocked() { |
| 666 | const nsecs_t currentTime = now(); |
| 667 | nsecs_t nextAnrCheck = LONG_LONG_MAX; |
| 668 | // Check if we are waiting for a focused window to appear. Raise ANR if waited too long |
| 669 | if (mNoFocusedWindowTimeoutTime.has_value() && mAwaitedFocusedApplication != nullptr) { |
| 670 | if (currentTime >= *mNoFocusedWindowTimeoutTime) { |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 671 | processNoFocusedWindowAnrLocked(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 672 | mAwaitedFocusedApplication.reset(); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 673 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 674 | return LONG_LONG_MIN; |
| 675 | } else { |
Siarhei Vishniakou | 38a6d27 | 2020-10-20 20:29:33 -0500 | [diff] [blame] | 676 | // Keep waiting. We will drop the event when mNoFocusedWindowTimeoutTime comes. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 677 | nextAnrCheck = *mNoFocusedWindowTimeoutTime; |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | // Check if any connection ANRs are due |
| 682 | nextAnrCheck = std::min(nextAnrCheck, mAnrTracker.firstTimeout()); |
| 683 | if (currentTime < nextAnrCheck) { // most likely scenario |
| 684 | return nextAnrCheck; // everything is normal. Let's check again at nextAnrCheck |
| 685 | } |
| 686 | |
| 687 | // If we reached here, we have an unresponsive connection. |
| 688 | sp<Connection> connection = getConnectionLocked(mAnrTracker.firstToken()); |
| 689 | if (connection == nullptr) { |
| 690 | ALOGE("Could not find connection for entry %" PRId64, mAnrTracker.firstTimeout()); |
| 691 | return nextAnrCheck; |
| 692 | } |
| 693 | connection->responsive = false; |
| 694 | // Stop waking up for this unresponsive connection |
| 695 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 696 | onAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 697 | return LONG_LONG_MIN; |
| 698 | } |
| 699 | |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 700 | std::chrono::nanoseconds InputDispatcher::getDispatchingTimeoutLocked(const sp<IBinder>& token) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 701 | sp<WindowInfoHandle> window = getWindowHandleLocked(token); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 702 | if (window != nullptr) { |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 703 | return window->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 704 | } |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 705 | return DEFAULT_INPUT_DISPATCHING_TIMEOUT; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 706 | } |
| 707 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 708 | void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) { |
| 709 | nsecs_t currentTime = now(); |
| 710 | |
Jeff Brown | dc5992e | 2014-04-11 01:27:26 -0700 | [diff] [blame] | 711 | // Reset the key repeat timer whenever normal dispatch is suspended while the |
| 712 | // device is in a non-interactive state. This is to ensure that we abort a key |
| 713 | // repeat if the device is just coming out of sleep. |
| 714 | if (!mDispatchEnabled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 715 | resetKeyRepeatLocked(); |
| 716 | } |
| 717 | |
| 718 | // If dispatching is frozen, do not process timeouts or try to deliver any new events. |
| 719 | if (mDispatchFrozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 720 | if (DEBUG_FOCUS) { |
| 721 | ALOGD("Dispatch frozen. Waiting some more."); |
| 722 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 723 | return; |
| 724 | } |
| 725 | |
| 726 | // Optimize latency of app switches. |
| 727 | // Essentially we start a short timeout when an app switch key (HOME / ENDCALL) has |
| 728 | // been pressed. When it expires, we preempt dispatch and drop all other pending events. |
| 729 | bool isAppSwitchDue = mAppSwitchDueTime <= currentTime; |
| 730 | if (mAppSwitchDueTime < *nextWakeupTime) { |
| 731 | *nextWakeupTime = mAppSwitchDueTime; |
| 732 | } |
| 733 | |
| 734 | // Ready to start a new event. |
| 735 | // If we don't already have a pending event, go grab one. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 736 | if (!mPendingEvent) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 737 | if (mInboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 738 | if (isAppSwitchDue) { |
| 739 | // The inbound queue is empty so the app switch key we were waiting |
| 740 | // for will never arrive. Stop waiting for it. |
| 741 | resetPendingAppSwitchLocked(false); |
| 742 | isAppSwitchDue = false; |
| 743 | } |
| 744 | |
| 745 | // Synthesize a key repeat if appropriate. |
| 746 | if (mKeyRepeatState.lastKeyEntry) { |
| 747 | if (currentTime >= mKeyRepeatState.nextRepeatTime) { |
| 748 | mPendingEvent = synthesizeKeyRepeatLocked(currentTime); |
| 749 | } else { |
| 750 | if (mKeyRepeatState.nextRepeatTime < *nextWakeupTime) { |
| 751 | *nextWakeupTime = mKeyRepeatState.nextRepeatTime; |
| 752 | } |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | // Nothing to do if there is no pending event. |
| 757 | if (!mPendingEvent) { |
| 758 | return; |
| 759 | } |
| 760 | } else { |
| 761 | // Inbound queue has at least one entry. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 762 | mPendingEvent = mInboundQueue.front(); |
| 763 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 764 | traceInboundQueueLengthLocked(); |
| 765 | } |
| 766 | |
| 767 | // Poke user activity for this event. |
| 768 | if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 769 | pokeUserActivityLocked(*mPendingEvent); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 770 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | // Now we have an event to dispatch. |
| 774 | // 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] | 775 | ALOG_ASSERT(mPendingEvent != nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 776 | bool done = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 777 | DropReason dropReason = DropReason::NOT_DROPPED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 778 | if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 779 | dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 780 | } else if (!mDispatchEnabled) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 781 | dropReason = DropReason::DISABLED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | if (mNextUnblockedEvent == mPendingEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 785 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | switch (mPendingEvent->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 789 | case EventEntry::Type::CONFIGURATION_CHANGED: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 790 | const ConfigurationChangedEntry& typedEntry = |
| 791 | static_cast<const ConfigurationChangedEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 792 | done = dispatchConfigurationChangedLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 793 | dropReason = DropReason::NOT_DROPPED; // configuration changes are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 794 | break; |
| 795 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 796 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 797 | case EventEntry::Type::DEVICE_RESET: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 798 | const DeviceResetEntry& typedEntry = |
| 799 | static_cast<const DeviceResetEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 800 | done = dispatchDeviceResetLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 801 | dropReason = DropReason::NOT_DROPPED; // device resets are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 802 | break; |
| 803 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 804 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 805 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 806 | std::shared_ptr<FocusEntry> typedEntry = |
| 807 | std::static_pointer_cast<FocusEntry>(mPendingEvent); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 808 | dispatchFocusLocked(currentTime, typedEntry); |
| 809 | done = true; |
| 810 | dropReason = DropReason::NOT_DROPPED; // focus events are never dropped |
| 811 | break; |
| 812 | } |
| 813 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 814 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 815 | const auto typedEntry = std::static_pointer_cast<TouchModeEntry>(mPendingEvent); |
| 816 | dispatchTouchModeChangeLocked(currentTime, typedEntry); |
| 817 | done = true; |
| 818 | dropReason = DropReason::NOT_DROPPED; // touch mode events are never dropped |
| 819 | break; |
| 820 | } |
| 821 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 822 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 823 | const auto typedEntry = |
| 824 | std::static_pointer_cast<PointerCaptureChangedEntry>(mPendingEvent); |
| 825 | dispatchPointerCaptureChangedLocked(currentTime, typedEntry, dropReason); |
| 826 | done = true; |
| 827 | break; |
| 828 | } |
| 829 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 830 | case EventEntry::Type::DRAG: { |
| 831 | std::shared_ptr<DragEntry> typedEntry = |
| 832 | std::static_pointer_cast<DragEntry>(mPendingEvent); |
| 833 | dispatchDragLocked(currentTime, typedEntry); |
| 834 | done = true; |
| 835 | break; |
| 836 | } |
| 837 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 838 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 839 | std::shared_ptr<KeyEntry> keyEntry = std::static_pointer_cast<KeyEntry>(mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 840 | if (isAppSwitchDue) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 841 | if (isAppSwitchKeyEvent(*keyEntry)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 842 | resetPendingAppSwitchLocked(true); |
| 843 | isAppSwitchDue = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 844 | } else if (dropReason == DropReason::NOT_DROPPED) { |
| 845 | dropReason = DropReason::APP_SWITCH; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 846 | } |
| 847 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 848 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *keyEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 849 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 850 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 851 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 852 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 853 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 854 | done = dispatchKeyLocked(currentTime, keyEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 855 | break; |
| 856 | } |
| 857 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 858 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 859 | std::shared_ptr<MotionEntry> motionEntry = |
| 860 | std::static_pointer_cast<MotionEntry>(mPendingEvent); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 861 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 862 | dropReason = DropReason::APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 863 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 864 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *motionEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 865 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 866 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 867 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 868 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 869 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 870 | done = dispatchMotionLocked(currentTime, motionEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 871 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 872 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 873 | |
| 874 | case EventEntry::Type::SENSOR: { |
| 875 | std::shared_ptr<SensorEntry> sensorEntry = |
| 876 | std::static_pointer_cast<SensorEntry>(mPendingEvent); |
| 877 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 878 | dropReason = DropReason::APP_SWITCH; |
| 879 | } |
| 880 | // Sensor timestamps use SYSTEM_TIME_BOOTTIME time base, so we can't use |
| 881 | // 'currentTime' here, get SYSTEM_TIME_BOOTTIME instead. |
| 882 | nsecs_t bootTime = systemTime(SYSTEM_TIME_BOOTTIME); |
| 883 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(bootTime, *sensorEntry)) { |
| 884 | dropReason = DropReason::STALE; |
| 885 | } |
| 886 | dispatchSensorLocked(currentTime, sensorEntry, &dropReason, nextWakeupTime); |
| 887 | done = true; |
| 888 | break; |
| 889 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | if (done) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 893 | if (dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 894 | dropInboundEventLocked(*mPendingEvent, dropReason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 895 | } |
Michael Wright | 3a98172 | 2015-06-10 15:26:13 +0100 | [diff] [blame] | 896 | mLastDropReason = dropReason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 897 | |
| 898 | releasePendingEventLocked(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 899 | *nextWakeupTime = LONG_LONG_MIN; // force next poll to wake up immediately |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 900 | } |
| 901 | } |
| 902 | |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 903 | /** |
| 904 | * Return true if the events preceding this incoming motion event should be dropped |
| 905 | * Return false otherwise (the default behaviour) |
| 906 | */ |
| 907 | bool InputDispatcher::shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 908 | const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN && |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 909 | (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 910 | |
| 911 | // Optimize case where the current application is unresponsive and the user |
| 912 | // decides to touch a window in a different application. |
| 913 | // If the application takes too long to catch up then we drop all events preceding |
| 914 | // the touch into the other window. |
| 915 | if (isPointerDownEvent && mAwaitedFocusedApplication != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 916 | int32_t displayId = motionEntry.displayId; |
| 917 | int32_t x = static_cast<int32_t>( |
| 918 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 919 | int32_t y = static_cast<int32_t>( |
| 920 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 921 | sp<WindowInfoHandle> touchedWindowHandle = |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 922 | findTouchedWindowAtLocked(displayId, x, y, nullptr); |
| 923 | if (touchedWindowHandle != nullptr && |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 924 | touchedWindowHandle->getApplicationToken() != |
| 925 | mAwaitedFocusedApplication->getApplicationToken()) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 926 | // User touched a different application than the one we are waiting on. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 927 | ALOGI("Pruning input queue because user touched a different application while waiting " |
| 928 | "for %s", |
| 929 | mAwaitedFocusedApplication->getName().c_str()); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 930 | return true; |
| 931 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 932 | |
| 933 | // Alternatively, maybe there's a gesture monitor that could handle this event |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 934 | for (const auto& monitor : getValueByKey(mGestureMonitorsByDisplay, displayId)) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 935 | sp<Connection> connection = |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 936 | getConnectionLocked(monitor.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 34ed4d4 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 937 | if (connection != nullptr && connection->responsive) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 938 | // This monitor could take more input. Drop all events preceding this |
| 939 | // event, so that gesture monitor could get a chance to receive the stream |
| 940 | ALOGW("Pruning the input queue because %s is unresponsive, but we have a " |
| 941 | "responsive gesture monitor that may handle the event", |
| 942 | mAwaitedFocusedApplication->getName().c_str()); |
| 943 | return true; |
| 944 | } |
| 945 | } |
| 946 | } |
| 947 | |
| 948 | // Prevent getting stuck: if we have a pending key event, and some motion events that have not |
| 949 | // yet been processed by some connections, the dispatcher will wait for these motion |
| 950 | // events to be processed before dispatching the key event. This is because these motion events |
| 951 | // may cause a new window to be launched, which the user might expect to receive focus. |
| 952 | // To prevent waiting forever for such events, just send the key to the currently focused window |
| 953 | if (isPointerDownEvent && mKeyIsWaitingForEventsTimeout) { |
| 954 | ALOGD("Received a new pointer down event, stop waiting for events to process and " |
| 955 | "just send the pending key event to the focused window."); |
| 956 | mKeyIsWaitingForEventsTimeout = now(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 957 | } |
| 958 | return false; |
| 959 | } |
| 960 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 961 | bool InputDispatcher::enqueueInboundEventLocked(std::unique_ptr<EventEntry> newEntry) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 962 | bool needWake = mInboundQueue.empty(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 963 | mInboundQueue.push_back(std::move(newEntry)); |
| 964 | EventEntry& entry = *(mInboundQueue.back()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 965 | traceInboundQueueLengthLocked(); |
| 966 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 967 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 968 | case EventEntry::Type::KEY: { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 969 | // Optimize app switch latency. |
| 970 | // If the application takes too long to catch up then we drop all events preceding |
| 971 | // the app switch key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 972 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 973 | if (isAppSwitchKeyEvent(keyEntry)) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 974 | if (keyEntry.action == AKEY_EVENT_ACTION_DOWN) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 975 | mAppSwitchSawKeyDown = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 976 | } else if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 977 | if (mAppSwitchSawKeyDown) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 978 | if (DEBUG_APP_SWITCH) { |
| 979 | ALOGD("App switch is pending!"); |
| 980 | } |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 981 | mAppSwitchDueTime = keyEntry.eventTime + APP_SWITCH_TIMEOUT; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 982 | mAppSwitchSawKeyDown = false; |
| 983 | needWake = true; |
| 984 | } |
| 985 | } |
| 986 | } |
| 987 | break; |
| 988 | } |
| 989 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 990 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 991 | if (shouldPruneInboundQueueLocked(static_cast<MotionEntry&>(entry))) { |
| 992 | mNextUnblockedEvent = mInboundQueue.back(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 993 | needWake = true; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 994 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 995 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 996 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 997 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 998 | LOG_ALWAYS_FATAL("Focus events should be inserted using enqueueFocusEventLocked"); |
| 999 | break; |
| 1000 | } |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1001 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1002 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1003 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1004 | case EventEntry::Type::SENSOR: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1005 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1006 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1007 | // nothing to do |
| 1008 | break; |
| 1009 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1010 | } |
| 1011 | |
| 1012 | return needWake; |
| 1013 | } |
| 1014 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1015 | void InputDispatcher::addRecentEventLocked(std::shared_ptr<EventEntry> entry) { |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1016 | // Do not store sensor event in recent queue to avoid flooding the queue. |
| 1017 | if (entry->type != EventEntry::Type::SENSOR) { |
| 1018 | mRecentQueue.push_back(entry); |
| 1019 | } |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1020 | if (mRecentQueue.size() > RECENT_QUEUE_MAX_SIZE) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1021 | mRecentQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1022 | } |
| 1023 | } |
| 1024 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1025 | sp<WindowInfoHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, int32_t x, |
| 1026 | int32_t y, TouchState* touchState, |
| 1027 | bool addOutsideTargets, |
| 1028 | bool ignoreDragWindow) { |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 1029 | if (addOutsideTargets && touchState == nullptr) { |
| 1030 | 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] | 1031 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1032 | // Traverse windows from front to back to find touched window. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1033 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 1034 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 1035 | if (ignoreDragWindow && haveSameToken(windowHandle, mDragState->dragWindow)) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1036 | continue; |
| 1037 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1038 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1039 | if (windowInfo->displayId == displayId) { |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1040 | auto flags = windowInfo->flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1041 | |
| 1042 | if (windowInfo->visible) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1043 | if (!flags.test(WindowInfo::Flag::NOT_TOUCHABLE)) { |
| 1044 | bool isTouchModal = !flags.test(WindowInfo::Flag::NOT_FOCUSABLE) && |
| 1045 | !flags.test(WindowInfo::Flag::NOT_TOUCH_MODAL); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1046 | if (isTouchModal || windowInfo->touchableRegionContainsPoint(x, y)) { |
| 1047 | // Found window. |
| 1048 | return windowHandle; |
| 1049 | } |
| 1050 | } |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1051 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1052 | if (addOutsideTargets && flags.test(WindowInfo::Flag::WATCH_OUTSIDE_TOUCH)) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1053 | touchState->addOrUpdateWindow(windowHandle, |
| 1054 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE, |
| 1055 | BitSet32(0)); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1056 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1057 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1058 | } |
| 1059 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1060 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1061 | } |
| 1062 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1063 | void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1064 | const char* reason; |
| 1065 | switch (dropReason) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1066 | case DropReason::POLICY: |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1067 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 1068 | ALOGD("Dropped event because policy consumed it."); |
| 1069 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1070 | reason = "inbound event was dropped because the policy consumed it"; |
| 1071 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1072 | case DropReason::DISABLED: |
| 1073 | if (mLastDropReason != DropReason::DISABLED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1074 | ALOGI("Dropped event because input dispatch is disabled."); |
| 1075 | } |
| 1076 | reason = "inbound event was dropped because input dispatch is disabled"; |
| 1077 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1078 | case DropReason::APP_SWITCH: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1079 | ALOGI("Dropped event because of pending overdue app switch."); |
| 1080 | reason = "inbound event was dropped because of pending overdue app switch"; |
| 1081 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1082 | case DropReason::BLOCKED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1083 | ALOGI("Dropped event because the current application is not responding and the user " |
| 1084 | "has started interacting with a different application."); |
| 1085 | reason = "inbound event was dropped because the current application is not responding " |
| 1086 | "and the user has started interacting with a different application"; |
| 1087 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1088 | case DropReason::STALE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1089 | ALOGI("Dropped event because it is stale."); |
| 1090 | reason = "inbound event was dropped because it is stale"; |
| 1091 | break; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1092 | case DropReason::NO_POINTER_CAPTURE: |
| 1093 | ALOGI("Dropped event because there is no window with Pointer Capture."); |
| 1094 | reason = "inbound event was dropped because there is no window with Pointer Capture"; |
| 1095 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1096 | case DropReason::NOT_DROPPED: { |
| 1097 | LOG_ALWAYS_FATAL("Should not be dropping a NOT_DROPPED event"); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1098 | return; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1099 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1100 | } |
| 1101 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1102 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1103 | case EventEntry::Type::KEY: { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1104 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 1105 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1106 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1107 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1108 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1109 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1110 | if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1111 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, reason); |
| 1112 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1113 | } else { |
| 1114 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 1115 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1116 | } |
| 1117 | break; |
| 1118 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1119 | case EventEntry::Type::SENSOR: { |
| 1120 | break; |
| 1121 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1122 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1123 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1124 | break; |
| 1125 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1126 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1127 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1128 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 1129 | case EventEntry::Type::DEVICE_RESET: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1130 | 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] | 1131 | break; |
| 1132 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1133 | } |
| 1134 | } |
| 1135 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1136 | static bool isAppSwitchKeyCode(int32_t keyCode) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1137 | return keyCode == AKEYCODE_HOME || keyCode == AKEYCODE_ENDCALL || |
| 1138 | keyCode == AKEYCODE_APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1139 | } |
| 1140 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1141 | bool InputDispatcher::isAppSwitchKeyEvent(const KeyEntry& keyEntry) { |
| 1142 | return !(keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) && isAppSwitchKeyCode(keyEntry.keyCode) && |
| 1143 | (keyEntry.policyFlags & POLICY_FLAG_TRUSTED) && |
| 1144 | (keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1145 | } |
| 1146 | |
| 1147 | bool InputDispatcher::isAppSwitchPendingLocked() { |
| 1148 | return mAppSwitchDueTime != LONG_LONG_MAX; |
| 1149 | } |
| 1150 | |
| 1151 | void InputDispatcher::resetPendingAppSwitchLocked(bool handled) { |
| 1152 | mAppSwitchDueTime = LONG_LONG_MAX; |
| 1153 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1154 | if (DEBUG_APP_SWITCH) { |
| 1155 | if (handled) { |
| 1156 | ALOGD("App switch has arrived."); |
| 1157 | } else { |
| 1158 | ALOGD("App switch was abandoned."); |
| 1159 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1160 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1161 | } |
| 1162 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1163 | bool InputDispatcher::haveCommandsLocked() const { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1164 | return !mCommandQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1165 | } |
| 1166 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1167 | bool InputDispatcher::runCommandsLockedInterruptable() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1168 | if (mCommandQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1169 | return false; |
| 1170 | } |
| 1171 | |
| 1172 | do { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1173 | auto command = std::move(mCommandQueue.front()); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1174 | mCommandQueue.pop_front(); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1175 | // Commands are run with the lock held, but may release and re-acquire the lock from within. |
| 1176 | command(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1177 | } while (!mCommandQueue.empty()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1178 | return true; |
| 1179 | } |
| 1180 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1181 | void InputDispatcher::postCommandLocked(Command&& command) { |
| 1182 | mCommandQueue.push_back(command); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1183 | } |
| 1184 | |
| 1185 | void InputDispatcher::drainInboundQueueLocked() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1186 | while (!mInboundQueue.empty()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1187 | std::shared_ptr<EventEntry> entry = mInboundQueue.front(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1188 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1189 | releaseInboundEventLocked(entry); |
| 1190 | } |
| 1191 | traceInboundQueueLengthLocked(); |
| 1192 | } |
| 1193 | |
| 1194 | void InputDispatcher::releasePendingEventLocked() { |
| 1195 | if (mPendingEvent) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1196 | releaseInboundEventLocked(mPendingEvent); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1197 | mPendingEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1198 | } |
| 1199 | } |
| 1200 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1201 | void InputDispatcher::releaseInboundEventLocked(std::shared_ptr<EventEntry> entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1202 | InjectionState* injectionState = entry->injectionState; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1203 | if (injectionState && injectionState->injectionResult == InputEventInjectionResult::PENDING) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1204 | if (DEBUG_DISPATCH_CYCLE) { |
| 1205 | ALOGD("Injected inbound event was dropped."); |
| 1206 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1207 | setInjectionResult(*entry, InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1208 | } |
| 1209 | if (entry == mNextUnblockedEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1210 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1211 | } |
| 1212 | addRecentEventLocked(entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1213 | } |
| 1214 | |
| 1215 | void InputDispatcher::resetKeyRepeatLocked() { |
| 1216 | if (mKeyRepeatState.lastKeyEntry) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1217 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1218 | } |
| 1219 | } |
| 1220 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1221 | std::shared_ptr<KeyEntry> InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) { |
| 1222 | std::shared_ptr<KeyEntry> entry = mKeyRepeatState.lastKeyEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1223 | |
Michael Wright | 2e73295 | 2014-09-24 13:26:59 -0700 | [diff] [blame] | 1224 | uint32_t policyFlags = entry->policyFlags & |
| 1225 | (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1226 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1227 | std::shared_ptr<KeyEntry> newEntry = |
| 1228 | std::make_unique<KeyEntry>(mIdGenerator.nextId(), currentTime, entry->deviceId, |
| 1229 | entry->source, entry->displayId, policyFlags, entry->action, |
| 1230 | entry->flags, entry->keyCode, entry->scanCode, |
| 1231 | entry->metaState, entry->repeatCount + 1, entry->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1232 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1233 | newEntry->syntheticRepeat = true; |
| 1234 | mKeyRepeatState.lastKeyEntry = newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1235 | mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1236 | return newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1237 | } |
| 1238 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1239 | bool InputDispatcher::dispatchConfigurationChangedLocked(nsecs_t currentTime, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1240 | const ConfigurationChangedEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1241 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1242 | ALOGD("dispatchConfigurationChanged - eventTime=%" PRId64, entry.eventTime); |
| 1243 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1244 | |
| 1245 | // Reset key repeating in case a keyboard device was added or removed or something. |
| 1246 | resetKeyRepeatLocked(); |
| 1247 | |
| 1248 | // 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] | 1249 | auto command = [this, eventTime = entry.eventTime]() REQUIRES(mLock) { |
| 1250 | scoped_unlock unlock(mLock); |
| 1251 | mPolicy->notifyConfigurationChanged(eventTime); |
| 1252 | }; |
| 1253 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1254 | return true; |
| 1255 | } |
| 1256 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1257 | bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime, |
| 1258 | const DeviceResetEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1259 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1260 | ALOGD("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry.eventTime, |
| 1261 | entry.deviceId); |
| 1262 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1263 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 1264 | // Reset key repeating in case a keyboard device was disabled or enabled. |
| 1265 | if (mKeyRepeatState.lastKeyEntry && mKeyRepeatState.lastKeyEntry->deviceId == entry.deviceId) { |
| 1266 | resetKeyRepeatLocked(); |
| 1267 | } |
| 1268 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1269 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, "device was reset"); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1270 | options.deviceId = entry.deviceId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1271 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1272 | return true; |
| 1273 | } |
| 1274 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1275 | void InputDispatcher::enqueueFocusEventLocked(const sp<IBinder>& windowToken, bool hasFocus, |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1276 | const std::string& reason) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1277 | if (mPendingEvent != nullptr) { |
| 1278 | // Move the pending event to the front of the queue. This will give the chance |
| 1279 | // for the pending event to get dispatched to the newly focused window |
| 1280 | mInboundQueue.push_front(mPendingEvent); |
| 1281 | mPendingEvent = nullptr; |
| 1282 | } |
| 1283 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1284 | std::unique_ptr<FocusEntry> focusEntry = |
| 1285 | std::make_unique<FocusEntry>(mIdGenerator.nextId(), now(), windowToken, hasFocus, |
| 1286 | reason); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1287 | |
| 1288 | // This event should go to the front of the queue, but behind all other focus events |
| 1289 | // Find the last focus event, and insert right after it |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1290 | std::deque<std::shared_ptr<EventEntry>>::reverse_iterator it = |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1291 | std::find_if(mInboundQueue.rbegin(), mInboundQueue.rend(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1292 | [](const std::shared_ptr<EventEntry>& event) { |
| 1293 | return event->type == EventEntry::Type::FOCUS; |
| 1294 | }); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1295 | |
| 1296 | // 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] | 1297 | mInboundQueue.insert(it.base(), std::move(focusEntry)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1298 | } |
| 1299 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1300 | void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime, std::shared_ptr<FocusEntry> entry) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 1301 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1302 | if (channel == nullptr) { |
| 1303 | return; // Window has gone away |
| 1304 | } |
| 1305 | InputTarget target; |
| 1306 | target.inputChannel = channel; |
| 1307 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1308 | entry->dispatchInProgress = true; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1309 | std::string message = std::string("Focus ") + (entry->hasFocus ? "entering " : "leaving ") + |
| 1310 | channel->getName(); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 1311 | std::string reason = std::string("reason=").append(entry->reason); |
| 1312 | android_log_event_list(LOGTAG_INPUT_FOCUS) << message << reason << LOG_ID_EVENTS; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1313 | dispatchEventLocked(currentTime, entry, {target}); |
| 1314 | } |
| 1315 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1316 | void InputDispatcher::dispatchPointerCaptureChangedLocked( |
| 1317 | nsecs_t currentTime, const std::shared_ptr<PointerCaptureChangedEntry>& entry, |
| 1318 | DropReason& dropReason) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1319 | dropReason = DropReason::NOT_DROPPED; |
| 1320 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1321 | const bool haveWindowWithPointerCapture = mWindowTokenWithPointerCapture != nullptr; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1322 | sp<IBinder> token; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1323 | |
| 1324 | if (entry->pointerCaptureRequest.enable) { |
| 1325 | // Enable Pointer Capture. |
| 1326 | if (haveWindowWithPointerCapture && |
| 1327 | (entry->pointerCaptureRequest == mCurrentPointerCaptureRequest)) { |
| 1328 | LOG_ALWAYS_FATAL("This request to enable Pointer Capture has already been dispatched " |
| 1329 | "to the window."); |
| 1330 | } |
| 1331 | if (!mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1332 | // This can happen if a window requests capture and immediately releases capture. |
| 1333 | ALOGW("No window requested Pointer Capture."); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1334 | dropReason = DropReason::NO_POINTER_CAPTURE; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1335 | return; |
| 1336 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1337 | if (entry->pointerCaptureRequest.seq != mCurrentPointerCaptureRequest.seq) { |
| 1338 | ALOGI("Skipping dispatch of Pointer Capture being enabled: sequence number mismatch."); |
| 1339 | return; |
| 1340 | } |
| 1341 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1342 | token = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1343 | LOG_ALWAYS_FATAL_IF(!token, "Cannot find focused window for Pointer Capture."); |
| 1344 | mWindowTokenWithPointerCapture = token; |
| 1345 | } else { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1346 | // Disable Pointer Capture. |
| 1347 | // We do not check if the sequence number matches for requests to disable Pointer Capture |
| 1348 | // for two reasons: |
| 1349 | // 1. Pointer Capture can be disabled by a focus change, which means we can get two entries |
| 1350 | // to disable capture with the same sequence number: one generated by |
| 1351 | // disablePointerCaptureForcedLocked() and another as an acknowledgement of Pointer |
| 1352 | // Capture being disabled in InputReader. |
| 1353 | // 2. We respect any request to disable Pointer Capture generated by InputReader, since the |
| 1354 | // actual Pointer Capture state that affects events being generated by input devices is |
| 1355 | // in InputReader. |
| 1356 | if (!haveWindowWithPointerCapture) { |
| 1357 | // Pointer capture was already forcefully disabled because of focus change. |
| 1358 | dropReason = DropReason::NOT_DROPPED; |
| 1359 | return; |
| 1360 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1361 | token = mWindowTokenWithPointerCapture; |
| 1362 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1363 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1364 | setPointerCaptureLocked(false); |
| 1365 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1366 | } |
| 1367 | |
| 1368 | auto channel = getInputChannelLocked(token); |
| 1369 | if (channel == nullptr) { |
| 1370 | // Window has gone away, clean up Pointer Capture state. |
| 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 | return; |
| 1376 | } |
| 1377 | InputTarget target; |
| 1378 | target.inputChannel = channel; |
| 1379 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1380 | entry->dispatchInProgress = true; |
| 1381 | dispatchEventLocked(currentTime, entry, {target}); |
| 1382 | |
| 1383 | dropReason = DropReason::NOT_DROPPED; |
| 1384 | } |
| 1385 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1386 | void InputDispatcher::dispatchTouchModeChangeLocked(nsecs_t currentTime, |
| 1387 | const std::shared_ptr<TouchModeEntry>& entry) { |
| 1388 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
| 1389 | getWindowHandlesLocked(mFocusedDisplayId); |
| 1390 | if (windowHandles.empty()) { |
| 1391 | return; |
| 1392 | } |
| 1393 | const std::vector<InputTarget> inputTargets = |
| 1394 | getInputTargetsFromWindowHandlesLocked(windowHandles); |
| 1395 | if (inputTargets.empty()) { |
| 1396 | return; |
| 1397 | } |
| 1398 | entry->dispatchInProgress = true; |
| 1399 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1400 | } |
| 1401 | |
| 1402 | std::vector<InputTarget> InputDispatcher::getInputTargetsFromWindowHandlesLocked( |
| 1403 | const std::vector<sp<WindowInfoHandle>>& windowHandles) const { |
| 1404 | std::vector<InputTarget> inputTargets; |
| 1405 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
| 1406 | // TODO(b/193718270): Due to performance concerns, consider notifying visible windows only. |
| 1407 | const sp<IBinder>& token = handle->getToken(); |
| 1408 | if (token == nullptr) { |
| 1409 | continue; |
| 1410 | } |
| 1411 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(token); |
| 1412 | if (channel == nullptr) { |
| 1413 | continue; // Window has gone away |
| 1414 | } |
| 1415 | InputTarget target; |
| 1416 | target.inputChannel = channel; |
| 1417 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1418 | inputTargets.push_back(target); |
| 1419 | } |
| 1420 | return inputTargets; |
| 1421 | } |
| 1422 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1423 | bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, std::shared_ptr<KeyEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1424 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1425 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1426 | if (!entry->dispatchInProgress) { |
| 1427 | if (entry->repeatCount == 0 && entry->action == AKEY_EVENT_ACTION_DOWN && |
| 1428 | (entry->policyFlags & POLICY_FLAG_TRUSTED) && |
| 1429 | (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { |
| 1430 | if (mKeyRepeatState.lastKeyEntry && |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1431 | mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode && |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1432 | // We have seen two identical key downs in a row which indicates that the device |
| 1433 | // driver is automatically generating key repeats itself. We take note of the |
| 1434 | // repeat here, but we disable our own next key repeat timer since it is clear that |
| 1435 | // we will not need to synthesize key repeats ourselves. |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1436 | mKeyRepeatState.lastKeyEntry->deviceId == entry->deviceId) { |
| 1437 | // Make sure we don't get key down from a different device. If a different |
| 1438 | // device Id has same key pressed down, the new device Id will replace the |
| 1439 | // current one to hold the key repeat with repeat count reset. |
| 1440 | // In the future when got a KEY_UP on the device id, drop it and do not |
| 1441 | // stop the key repeat on current device. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1442 | entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; |
| 1443 | resetKeyRepeatLocked(); |
| 1444 | mKeyRepeatState.nextRepeatTime = LONG_LONG_MAX; // don't generate repeats ourselves |
| 1445 | } else { |
| 1446 | // Not a repeat. Save key down state in case we do see a repeat later. |
| 1447 | resetKeyRepeatLocked(); |
| 1448 | mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; |
| 1449 | } |
| 1450 | mKeyRepeatState.lastKeyEntry = entry; |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1451 | } else if (entry->action == AKEY_EVENT_ACTION_UP && mKeyRepeatState.lastKeyEntry && |
| 1452 | mKeyRepeatState.lastKeyEntry->deviceId != entry->deviceId) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1453 | // 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] | 1454 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 1455 | ALOGD("deviceId=%d got KEY_UP as stale", entry->deviceId); |
| 1456 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1457 | } else if (!entry->syntheticRepeat) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1458 | resetKeyRepeatLocked(); |
| 1459 | } |
| 1460 | |
| 1461 | if (entry->repeatCount == 1) { |
| 1462 | entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; |
| 1463 | } else { |
| 1464 | entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; |
| 1465 | } |
| 1466 | |
| 1467 | entry->dispatchInProgress = true; |
| 1468 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1469 | logOutboundKeyDetails("dispatchKey - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1470 | } |
| 1471 | |
| 1472 | // Handle case where the policy asked us to try again later last time. |
| 1473 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER) { |
| 1474 | if (currentTime < entry->interceptKeyWakeupTime) { |
| 1475 | if (entry->interceptKeyWakeupTime < *nextWakeupTime) { |
| 1476 | *nextWakeupTime = entry->interceptKeyWakeupTime; |
| 1477 | } |
| 1478 | return false; // wait until next wakeup |
| 1479 | } |
| 1480 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; |
| 1481 | entry->interceptKeyWakeupTime = 0; |
| 1482 | } |
| 1483 | |
| 1484 | // Give the policy a chance to intercept the key. |
| 1485 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN) { |
| 1486 | if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1487 | sp<IBinder> focusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1488 | mFocusResolver.getFocusedWindowToken(getTargetDisplayId(*entry)); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1489 | |
| 1490 | auto command = [this, focusedWindowToken, entry]() REQUIRES(mLock) { |
| 1491 | doInterceptKeyBeforeDispatchingCommand(focusedWindowToken, *entry); |
| 1492 | }; |
| 1493 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1494 | return false; // wait for the command to run |
| 1495 | } else { |
| 1496 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
| 1497 | } |
| 1498 | } else if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_SKIP) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1499 | if (*dropReason == DropReason::NOT_DROPPED) { |
| 1500 | *dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1501 | } |
| 1502 | } |
| 1503 | |
| 1504 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1505 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1506 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1507 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1508 | : InputEventInjectionResult::FAILED); |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1509 | mReporter->reportDroppedKey(entry->id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1510 | return true; |
| 1511 | } |
| 1512 | |
| 1513 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1514 | std::vector<InputTarget> inputTargets; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1515 | InputEventInjectionResult injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1516 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1517 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1518 | return false; |
| 1519 | } |
| 1520 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1521 | setInjectionResult(*entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1522 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1523 | return true; |
| 1524 | } |
| 1525 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1526 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1527 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1528 | |
| 1529 | // Dispatch the key. |
| 1530 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1531 | return true; |
| 1532 | } |
| 1533 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1534 | void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1535 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1536 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 ", " |
| 1537 | "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, " |
| 1538 | "metaState=0x%x, repeatCount=%d, downTime=%" PRId64, |
| 1539 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, |
| 1540 | entry.policyFlags, entry.action, entry.flags, entry.keyCode, entry.scanCode, |
| 1541 | entry.metaState, entry.repeatCount, entry.downTime); |
| 1542 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1543 | } |
| 1544 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1545 | void InputDispatcher::dispatchSensorLocked(nsecs_t currentTime, |
| 1546 | const std::shared_ptr<SensorEntry>& entry, |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1547 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1548 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1549 | ALOGD("notifySensorEvent eventTime=%" PRId64 ", hwTimestamp=%" PRId64 ", deviceId=%d, " |
| 1550 | "source=0x%x, sensorType=%s", |
| 1551 | entry->eventTime, entry->hwTimestamp, entry->deviceId, entry->source, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1552 | ftl::enum_string(entry->sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1553 | } |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1554 | auto command = [this, entry]() REQUIRES(mLock) { |
| 1555 | scoped_unlock unlock(mLock); |
| 1556 | |
| 1557 | if (entry->accuracyChanged) { |
| 1558 | mPolicy->notifySensorAccuracy(entry->deviceId, entry->sensorType, entry->accuracy); |
| 1559 | } |
| 1560 | mPolicy->notifySensorEvent(entry->deviceId, entry->sensorType, entry->accuracy, |
| 1561 | entry->hwTimestamp, entry->values); |
| 1562 | }; |
| 1563 | postCommandLocked(std::move(command)); |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1564 | } |
| 1565 | |
| 1566 | bool InputDispatcher::flushSensor(int deviceId, InputDeviceSensorType sensorType) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1567 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1568 | ALOGD("flushSensor deviceId=%d, sensorType=%s", deviceId, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1569 | ftl::enum_string(sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1570 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1571 | { // acquire lock |
| 1572 | std::scoped_lock _l(mLock); |
| 1573 | |
| 1574 | for (auto it = mInboundQueue.begin(); it != mInboundQueue.end(); it++) { |
| 1575 | std::shared_ptr<EventEntry> entry = *it; |
| 1576 | if (entry->type == EventEntry::Type::SENSOR) { |
| 1577 | it = mInboundQueue.erase(it); |
| 1578 | releaseInboundEventLocked(entry); |
| 1579 | } |
| 1580 | } |
| 1581 | } |
| 1582 | return true; |
| 1583 | } |
| 1584 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1585 | bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, std::shared_ptr<MotionEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1586 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1587 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1588 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1589 | if (!entry->dispatchInProgress) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1590 | entry->dispatchInProgress = true; |
| 1591 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1592 | logOutboundMotionDetails("dispatchMotion - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1593 | } |
| 1594 | |
| 1595 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1596 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1597 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1598 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1599 | : InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1600 | return true; |
| 1601 | } |
| 1602 | |
| 1603 | bool isPointerEvent = entry->source & AINPUT_SOURCE_CLASS_POINTER; |
| 1604 | |
| 1605 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1606 | std::vector<InputTarget> inputTargets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1607 | |
| 1608 | bool conflictingPointerActions = false; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1609 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1610 | if (isPointerEvent) { |
| 1611 | // Pointer event. (eg. touchscreen) |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1612 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1613 | findTouchedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1614 | &conflictingPointerActions); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1615 | } else { |
| 1616 | // Non touch event. (eg. trackball) |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1617 | injectionResult = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1618 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1619 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1620 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1621 | return false; |
| 1622 | } |
| 1623 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1624 | setInjectionResult(*entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1625 | if (injectionResult == InputEventInjectionResult::PERMISSION_DENIED) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1626 | ALOGW("Permission denied, dropping the motion (isPointer=%s)", toString(isPointerEvent)); |
| 1627 | return true; |
| 1628 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1629 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1630 | CancelationOptions::Mode mode(isPointerEvent |
| 1631 | ? CancelationOptions::CANCEL_POINTER_EVENTS |
| 1632 | : CancelationOptions::CANCEL_NON_POINTER_EVENTS); |
| 1633 | CancelationOptions options(mode, "input event injection failed"); |
| 1634 | synthesizeCancelationEventsForMonitorsLocked(options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1635 | return true; |
| 1636 | } |
| 1637 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1638 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1639 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1640 | |
| 1641 | // Dispatch the motion. |
| 1642 | if (conflictingPointerActions) { |
| 1643 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1644 | "conflicting pointer actions"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1645 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1646 | } |
| 1647 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1648 | return true; |
| 1649 | } |
| 1650 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1651 | void InputDispatcher::enqueueDragEventLocked(const sp<WindowInfoHandle>& windowHandle, |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1652 | bool isExiting, const MotionEntry& motionEntry) { |
| 1653 | // If the window needs enqueue a drag event, the pointerCount should be 1 and the action should |
| 1654 | // be AMOTION_EVENT_ACTION_MOVE, that could guarantee the first pointer is always valid. |
| 1655 | LOG_ALWAYS_FATAL_IF(motionEntry.pointerCount != 1); |
| 1656 | PointerCoords pointerCoords; |
| 1657 | pointerCoords.copyFrom(motionEntry.pointerCoords[0]); |
| 1658 | pointerCoords.transform(windowHandle->getInfo()->transform); |
| 1659 | |
| 1660 | std::unique_ptr<DragEntry> dragEntry = |
| 1661 | std::make_unique<DragEntry>(mIdGenerator.nextId(), motionEntry.eventTime, |
| 1662 | windowHandle->getToken(), isExiting, pointerCoords.getX(), |
| 1663 | pointerCoords.getY()); |
| 1664 | |
| 1665 | enqueueInboundEventLocked(std::move(dragEntry)); |
| 1666 | } |
| 1667 | |
| 1668 | void InputDispatcher::dispatchDragLocked(nsecs_t currentTime, std::shared_ptr<DragEntry> entry) { |
| 1669 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
| 1670 | if (channel == nullptr) { |
| 1671 | return; // Window has gone away |
| 1672 | } |
| 1673 | InputTarget target; |
| 1674 | target.inputChannel = channel; |
| 1675 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1676 | entry->dispatchInProgress = true; |
| 1677 | dispatchEventLocked(currentTime, entry, {target}); |
| 1678 | } |
| 1679 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1680 | void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1681 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1682 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
| 1683 | ", policyFlags=0x%x, " |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1684 | "action=%s, actionButton=0x%x, flags=0x%x, " |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1685 | "metaState=0x%x, buttonState=0x%x," |
| 1686 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64, |
| 1687 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1688 | entry.policyFlags, MotionEvent::actionToString(entry.action).c_str(), |
| 1689 | entry.actionButton, entry.flags, entry.metaState, entry.buttonState, entry.edgeFlags, |
| 1690 | entry.xPrecision, entry.yPrecision, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1691 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1692 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
| 1693 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
| 1694 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 1695 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 1696 | "orientation=%f", |
| 1697 | i, entry.pointerProperties[i].id, entry.pointerProperties[i].toolType, |
| 1698 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 1699 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 1700 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 1701 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 1702 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 1703 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 1704 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 1705 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 1706 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| 1707 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1708 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1709 | } |
| 1710 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1711 | void InputDispatcher::dispatchEventLocked(nsecs_t currentTime, |
| 1712 | std::shared_ptr<EventEntry> eventEntry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1713 | const std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1714 | ATRACE_CALL(); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1715 | if (DEBUG_DISPATCH_CYCLE) { |
| 1716 | ALOGD("dispatchEventToCurrentInputTargets"); |
| 1717 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1718 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1719 | updateInteractionTokensLocked(*eventEntry, inputTargets); |
| 1720 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1721 | ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true |
| 1722 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1723 | pokeUserActivityLocked(*eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1724 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1725 | for (const InputTarget& inputTarget : inputTargets) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 1726 | sp<Connection> connection = |
| 1727 | getConnectionLocked(inputTarget.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 1728 | if (connection != nullptr) { |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 1729 | prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1730 | } else { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1731 | if (DEBUG_FOCUS) { |
| 1732 | ALOGD("Dropping event delivery to target with channel '%s' because it " |
| 1733 | "is no longer registered with the input dispatcher.", |
| 1734 | inputTarget.inputChannel->getName().c_str()); |
| 1735 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1736 | } |
| 1737 | } |
| 1738 | } |
| 1739 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1740 | void InputDispatcher::cancelEventsForAnrLocked(const sp<Connection>& connection) { |
| 1741 | // We will not be breaking any connections here, even if the policy wants us to abort dispatch. |
| 1742 | // If the policy decides to close the app, we will get a channel removal event via |
| 1743 | // unregisterInputChannel, and will clean up the connection that way. We are already not |
| 1744 | // sending new pointers to the connection when it blocked, but focused events will continue to |
| 1745 | // pile up. |
| 1746 | ALOGW("Canceling events for %s because it is unresponsive", |
| 1747 | connection->inputChannel->getName().c_str()); |
| 1748 | if (connection->status == Connection::STATUS_NORMAL) { |
| 1749 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, |
| 1750 | "application not responding"); |
| 1751 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1752 | } |
| 1753 | } |
| 1754 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1755 | void InputDispatcher::resetNoFocusedWindowTimeoutLocked() { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1756 | if (DEBUG_FOCUS) { |
| 1757 | ALOGD("Resetting ANR timeouts."); |
| 1758 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1759 | |
| 1760 | // Reset input target wait timeout. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1761 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1762 | mAwaitedFocusedApplication.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1763 | } |
| 1764 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1765 | /** |
| 1766 | * Get the display id that the given event should go to. If this event specifies a valid display id, |
| 1767 | * then it should be dispatched to that display. Otherwise, the event goes to the focused display. |
| 1768 | * Focused display is the display that the user most recently interacted with. |
| 1769 | */ |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1770 | int32_t InputDispatcher::getTargetDisplayId(const EventEntry& entry) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1771 | int32_t displayId; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1772 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1773 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1774 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 1775 | displayId = keyEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1776 | break; |
| 1777 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1778 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1779 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1780 | displayId = motionEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1781 | break; |
| 1782 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1783 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1784 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1785 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1786 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1787 | case EventEntry::Type::DEVICE_RESET: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1788 | case EventEntry::Type::SENSOR: |
| 1789 | case EventEntry::Type::DRAG: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1790 | 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] | 1791 | return ADISPLAY_ID_NONE; |
| 1792 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1793 | } |
| 1794 | return displayId == ADISPLAY_ID_NONE ? mFocusedDisplayId : displayId; |
| 1795 | } |
| 1796 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1797 | bool InputDispatcher::shouldWaitToSendKeyLocked(nsecs_t currentTime, |
| 1798 | const char* focusedWindowName) { |
| 1799 | if (mAnrTracker.empty()) { |
| 1800 | // already processed all events that we waited for |
| 1801 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1802 | return false; |
| 1803 | } |
| 1804 | |
| 1805 | if (!mKeyIsWaitingForEventsTimeout.has_value()) { |
| 1806 | // Start the timer |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 1807 | // 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] | 1808 | mKeyIsWaitingForEventsTimeout = currentTime + |
| 1809 | std::chrono::duration_cast<std::chrono::nanoseconds>(KEY_WAITING_FOR_EVENTS_TIMEOUT) |
| 1810 | .count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1811 | return true; |
| 1812 | } |
| 1813 | |
| 1814 | // We still have pending events, and already started the timer |
| 1815 | if (currentTime < *mKeyIsWaitingForEventsTimeout) { |
| 1816 | return true; // Still waiting |
| 1817 | } |
| 1818 | |
| 1819 | // Waited too long, and some connection still hasn't processed all motions |
| 1820 | // Just send the key to the focused window |
| 1821 | ALOGW("Dispatching key to %s even though there are other unprocessed events", |
| 1822 | focusedWindowName); |
| 1823 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1824 | return false; |
| 1825 | } |
| 1826 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1827 | InputEventInjectionResult InputDispatcher::findFocusedWindowTargetsLocked( |
| 1828 | nsecs_t currentTime, const EventEntry& entry, std::vector<InputTarget>& inputTargets, |
| 1829 | nsecs_t* nextWakeupTime) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1830 | std::string reason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1831 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1832 | int32_t displayId = getTargetDisplayId(entry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1833 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1834 | std::shared_ptr<InputApplicationHandle> focusedApplicationHandle = |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1835 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 1836 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1837 | // If there is no currently focused window and no focused application |
| 1838 | // then drop the event. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1839 | if (focusedWindowHandle == nullptr && focusedApplicationHandle == nullptr) { |
| 1840 | ALOGI("Dropping %s event because there is no focused window or focused application in " |
| 1841 | "display %" PRId32 ".", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1842 | ftl::enum_string(entry.type).c_str(), displayId); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1843 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1844 | } |
| 1845 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 1846 | // Drop key events if requested by input feature |
| 1847 | if (focusedWindowHandle != nullptr && shouldDropInput(entry, focusedWindowHandle)) { |
| 1848 | return InputEventInjectionResult::FAILED; |
| 1849 | } |
| 1850 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1851 | // Compatibility behavior: raise ANR if there is a focused application, but no focused window. |
| 1852 | // Only start counting when we have a focused event to dispatch. The ANR is canceled if we |
| 1853 | // start interacting with another application via touch (app switch). This code can be removed |
| 1854 | // if the "no focused window ANR" is moved to the policy. Input doesn't know whether |
| 1855 | // an app is expected to have a focused window. |
| 1856 | if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) { |
| 1857 | if (!mNoFocusedWindowTimeoutTime.has_value()) { |
| 1858 | // 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] | 1859 | std::chrono::nanoseconds timeout = focusedApplicationHandle->getDispatchingTimeout( |
| 1860 | DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
| 1861 | mNoFocusedWindowTimeoutTime = currentTime + timeout.count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1862 | mAwaitedFocusedApplication = focusedApplicationHandle; |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 1863 | mAwaitedApplicationDisplayId = displayId; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1864 | ALOGW("Waiting because no window has focus but %s may eventually add a " |
| 1865 | "window when it finishes starting up. Will wait for %" PRId64 "ms", |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1866 | mAwaitedFocusedApplication->getName().c_str(), millis(timeout)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1867 | *nextWakeupTime = *mNoFocusedWindowTimeoutTime; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1868 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1869 | } else if (currentTime > *mNoFocusedWindowTimeoutTime) { |
| 1870 | // Already raised ANR. Drop the event |
| 1871 | ALOGE("Dropping %s event because there is no focused window", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1872 | ftl::enum_string(entry.type).c_str()); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1873 | return InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1874 | } else { |
| 1875 | // Still waiting for the focused window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1876 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1877 | } |
| 1878 | } |
| 1879 | |
| 1880 | // we have a valid, non-null focused window |
| 1881 | resetNoFocusedWindowTimeoutLocked(); |
| 1882 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1883 | // Check permissions. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1884 | if (!checkInjectionPermission(focusedWindowHandle, entry.injectionState)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1885 | return InputEventInjectionResult::PERMISSION_DENIED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1886 | } |
| 1887 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1888 | if (focusedWindowHandle->getInfo()->paused) { |
| 1889 | ALOGI("Waiting because %s is paused", focusedWindowHandle->getName().c_str()); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1890 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1891 | } |
| 1892 | |
| 1893 | // If the event is a key event, then we must wait for all previous events to |
| 1894 | // complete before delivering it because previous events may have the |
| 1895 | // side-effect of transferring focus to a different window and we want to |
| 1896 | // ensure that the following keys are sent to the new window. |
| 1897 | // |
| 1898 | // Suppose the user touches a button in a window then immediately presses "A". |
| 1899 | // If the button causes a pop-up window to appear then we want to ensure that |
| 1900 | // the "A" key is delivered to the new pop-up window. This is because users |
| 1901 | // often anticipate pending UI changes when typing on a keyboard. |
| 1902 | // To obtain this behavior, we must serialize key events with respect to all |
| 1903 | // prior input events. |
| 1904 | if (entry.type == EventEntry::Type::KEY) { |
| 1905 | if (shouldWaitToSendKeyLocked(currentTime, focusedWindowHandle->getName().c_str())) { |
| 1906 | *nextWakeupTime = *mKeyIsWaitingForEventsTimeout; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1907 | return InputEventInjectionResult::PENDING; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1908 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1909 | } |
| 1910 | |
| 1911 | // Success! Output targets. |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1912 | addWindowTargetLocked(focusedWindowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1913 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS, |
| 1914 | BitSet32(0), inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1915 | |
| 1916 | // Done. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1917 | return InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1918 | } |
| 1919 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1920 | /** |
| 1921 | * Given a list of monitors, remove the ones we cannot find a connection for, and the ones |
| 1922 | * that are currently unresponsive. |
| 1923 | */ |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 1924 | std::vector<Monitor> InputDispatcher::selectResponsiveMonitorsLocked( |
| 1925 | const std::vector<Monitor>& monitors) const { |
| 1926 | std::vector<Monitor> responsiveMonitors; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1927 | std::copy_if(monitors.begin(), monitors.end(), std::back_inserter(responsiveMonitors), |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 1928 | [this](const Monitor& monitor) REQUIRES(mLock) { |
| 1929 | sp<Connection> connection = |
| 1930 | getConnectionLocked(monitor.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1931 | if (connection == nullptr) { |
| 1932 | ALOGE("Could not find connection for monitor %s", |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 1933 | monitor.inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1934 | return false; |
| 1935 | } |
| 1936 | if (!connection->responsive) { |
| 1937 | ALOGW("Unresponsive monitor %s will not get the new gesture", |
| 1938 | connection->inputChannel->getName().c_str()); |
| 1939 | return false; |
| 1940 | } |
| 1941 | return true; |
| 1942 | }); |
| 1943 | return responsiveMonitors; |
| 1944 | } |
| 1945 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1946 | InputEventInjectionResult InputDispatcher::findTouchedWindowTargetsLocked( |
| 1947 | nsecs_t currentTime, const MotionEntry& entry, std::vector<InputTarget>& inputTargets, |
| 1948 | nsecs_t* nextWakeupTime, bool* outConflictingPointerActions) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1949 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1950 | enum InjectionPermission { |
| 1951 | INJECTION_PERMISSION_UNKNOWN, |
| 1952 | INJECTION_PERMISSION_GRANTED, |
| 1953 | INJECTION_PERMISSION_DENIED |
| 1954 | }; |
| 1955 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1956 | // For security reasons, we defer updating the touch state until we are sure that |
| 1957 | // event injection will be allowed. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1958 | int32_t displayId = entry.displayId; |
| 1959 | int32_t action = entry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1960 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
| 1961 | |
| 1962 | // 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] | 1963 | InputEventInjectionResult injectionResult = InputEventInjectionResult::PENDING; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1964 | InjectionPermission injectionPermission = INJECTION_PERMISSION_UNKNOWN; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1965 | sp<WindowInfoHandle> newHoverWindowHandle(mLastHoverWindowHandle); |
| 1966 | sp<WindowInfoHandle> newTouchedWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1967 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1968 | // Copy current touch state into tempTouchState. |
| 1969 | // This state will be used to update mTouchStatesByDisplay at the end of this function. |
| 1970 | // 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] | 1971 | const TouchState* oldState = nullptr; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1972 | TouchState tempTouchState; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 1973 | std::unordered_map<int32_t, TouchState>::iterator oldStateIt = |
| 1974 | mTouchStatesByDisplay.find(displayId); |
| 1975 | if (oldStateIt != mTouchStatesByDisplay.end()) { |
| 1976 | oldState = &(oldStateIt->second); |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1977 | tempTouchState.copyFrom(*oldState); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1978 | } |
| 1979 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1980 | bool isSplit = tempTouchState.split; |
| 1981 | bool switchedDevice = tempTouchState.deviceId >= 0 && tempTouchState.displayId >= 0 && |
| 1982 | (tempTouchState.deviceId != entry.deviceId || tempTouchState.source != entry.source || |
| 1983 | tempTouchState.displayId != displayId); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1984 | bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE || |
| 1985 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 1986 | maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); |
| 1987 | bool newGesture = (maskedAction == AMOTION_EVENT_ACTION_DOWN || |
| 1988 | maskedAction == AMOTION_EVENT_ACTION_SCROLL || isHoverAction); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1989 | const bool isFromMouse = entry.source == AINPUT_SOURCE_MOUSE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1990 | bool wrongDevice = false; |
| 1991 | if (newGesture) { |
| 1992 | bool down = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1993 | if (switchedDevice && tempTouchState.down && !down && !isHoverAction) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1994 | ALOGI("Dropping event because a pointer for a different device is already down " |
| 1995 | "in display %" PRId32, |
| 1996 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1997 | // TODO: test multiple simultaneous input streams. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1998 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1999 | switchedDevice = false; |
| 2000 | wrongDevice = true; |
| 2001 | goto Failed; |
| 2002 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2003 | tempTouchState.reset(); |
| 2004 | tempTouchState.down = down; |
| 2005 | tempTouchState.deviceId = entry.deviceId; |
| 2006 | tempTouchState.source = entry.source; |
| 2007 | tempTouchState.displayId = displayId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2008 | isSplit = false; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2009 | } else if (switchedDevice && maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2010 | ALOGI("Dropping move event because a pointer for a different device is already active " |
| 2011 | "in display %" PRId32, |
| 2012 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2013 | // TODO: test multiple simultaneous input streams. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2014 | injectionResult = InputEventInjectionResult::PERMISSION_DENIED; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2015 | switchedDevice = false; |
| 2016 | wrongDevice = true; |
| 2017 | goto Failed; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2018 | } |
| 2019 | |
| 2020 | if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { |
| 2021 | /* Case 1: New splittable pointer going down, or need target for hover or scroll. */ |
| 2022 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2023 | int32_t x; |
| 2024 | int32_t y; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2025 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2026 | // Always dispatch mouse events to cursor position. |
| 2027 | if (isFromMouse) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2028 | x = int32_t(entry.xCursorPosition); |
| 2029 | y = int32_t(entry.yCursorPosition); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2030 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2031 | x = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2032 | y = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2033 | } |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2034 | const bool isDown = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 2035 | newTouchedWindowHandle = findTouchedWindowAtLocked(displayId, x, y, &tempTouchState, |
| 2036 | isDown /*addOutsideTargets*/); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2037 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2038 | // Figure out whether splitting will be allowed for this window. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2039 | if (newTouchedWindowHandle != nullptr && |
| 2040 | newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
Garfield Tan | addb02b | 2019-06-25 16:36:13 -0700 | [diff] [blame] | 2041 | // New window supports splitting, but we should never split mouse events. |
| 2042 | isSplit = !isFromMouse; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2043 | } else if (isSplit) { |
| 2044 | // New window does not support splitting but we have already split events. |
| 2045 | // Ignore the new window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2046 | newTouchedWindowHandle = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2047 | } |
| 2048 | |
| 2049 | // Handle the case where we did not find a window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2050 | if (newTouchedWindowHandle == nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2051 | // 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] | 2052 | newTouchedWindowHandle = tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2053 | } |
| 2054 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2055 | if (newTouchedWindowHandle != nullptr && newTouchedWindowHandle->getInfo()->paused) { |
| 2056 | ALOGI("Not sending touch event to %s because it is paused", |
| 2057 | newTouchedWindowHandle->getName().c_str()); |
| 2058 | newTouchedWindowHandle = nullptr; |
| 2059 | } |
| 2060 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 2061 | // Ensure the window has a connection and the connection is responsive |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2062 | if (newTouchedWindowHandle != nullptr) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 2063 | const bool isResponsive = hasResponsiveConnectionLocked(*newTouchedWindowHandle); |
| 2064 | if (!isResponsive) { |
| 2065 | ALOGW("%s will not receive the new gesture at %" PRIu64, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2066 | newTouchedWindowHandle->getName().c_str(), entry.eventTime); |
| 2067 | newTouchedWindowHandle = nullptr; |
| 2068 | } |
| 2069 | } |
| 2070 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2071 | // Drop events that can't be trusted due to occlusion |
| 2072 | if (newTouchedWindowHandle != nullptr && |
| 2073 | mBlockUntrustedTouchesMode != BlockUntrustedTouchesMode::DISABLED) { |
| 2074 | TouchOcclusionInfo occlusionInfo = |
| 2075 | computeTouchOcclusionInfoLocked(newTouchedWindowHandle, x, y); |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 2076 | if (!isTouchTrustedLocked(occlusionInfo)) { |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2077 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2078 | ALOGD("Stack of obscuring windows during untrusted touch (%d, %d):", x, y); |
| 2079 | for (const auto& log : occlusionInfo.debugInfo) { |
| 2080 | ALOGD("%s", log.c_str()); |
| 2081 | } |
| 2082 | } |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2083 | sendUntrustedTouchCommandLocked(occlusionInfo.obscuringPackage); |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 2084 | if (mBlockUntrustedTouchesMode == BlockUntrustedTouchesMode::BLOCK) { |
| 2085 | ALOGW("Dropping untrusted touch event due to %s/%d", |
| 2086 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid); |
| 2087 | newTouchedWindowHandle = nullptr; |
| 2088 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2089 | } |
| 2090 | } |
| 2091 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2092 | // Drop touch events if requested by input feature |
| 2093 | if (newTouchedWindowHandle != nullptr && shouldDropInput(entry, newTouchedWindowHandle)) { |
| 2094 | newTouchedWindowHandle = nullptr; |
| 2095 | } |
| 2096 | |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2097 | const std::vector<Monitor> newGestureMonitors = isDown |
| 2098 | ? selectResponsiveMonitorsLocked( |
| 2099 | getValueByKey(mGestureMonitorsByDisplay, displayId)) |
| 2100 | : std::vector<Monitor>{}; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2101 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2102 | if (newTouchedWindowHandle == nullptr && newGestureMonitors.empty()) { |
| 2103 | 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] | 2104 | "(%d, %d) in display %" PRId32 ".", |
| 2105 | x, y, displayId); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2106 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2107 | goto Failed; |
| 2108 | } |
| 2109 | |
| 2110 | if (newTouchedWindowHandle != nullptr) { |
| 2111 | // Set target flags. |
| 2112 | int32_t targetFlags = InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS; |
| 2113 | if (isSplit) { |
| 2114 | targetFlags |= InputTarget::FLAG_SPLIT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2115 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2116 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
| 2117 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
| 2118 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
| 2119 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 2120 | } |
| 2121 | |
| 2122 | // Update hover state. |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2123 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 2124 | newHoverWindowHandle = nullptr; |
| 2125 | } else if (isHoverAction) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2126 | newHoverWindowHandle = newTouchedWindowHandle; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2127 | } |
| 2128 | |
| 2129 | // Update the temporary touch state. |
| 2130 | BitSet32 pointerIds; |
| 2131 | if (isSplit) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2132 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2133 | pointerIds.markBit(pointerId); |
| 2134 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2135 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2136 | } |
| 2137 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2138 | tempTouchState.addGestureMonitors(newGestureMonitors); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2139 | } else { |
| 2140 | /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ |
| 2141 | |
| 2142 | // If the pointer is not currently down, then ignore the event. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2143 | if (!tempTouchState.down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2144 | if (DEBUG_FOCUS) { |
| 2145 | ALOGD("Dropping event because the pointer is not down or we previously " |
| 2146 | "dropped the pointer down event in display %" PRId32, |
| 2147 | displayId); |
| 2148 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2149 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2150 | goto Failed; |
| 2151 | } |
| 2152 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2153 | addDragEventLocked(entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2154 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2155 | // Check whether touches should slip outside of the current foreground window. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2156 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.pointerCount == 1 && |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2157 | tempTouchState.isSlippery()) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2158 | int32_t x = int32_t(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2159 | 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] | 2160 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2161 | sp<WindowInfoHandle> oldTouchedWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2162 | tempTouchState.getFirstForegroundWindowHandle(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2163 | newTouchedWindowHandle = findTouchedWindowAtLocked(displayId, x, y, &tempTouchState); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2164 | |
| 2165 | // Drop touch events if requested by input feature |
| 2166 | if (newTouchedWindowHandle != nullptr && |
| 2167 | shouldDropInput(entry, newTouchedWindowHandle)) { |
| 2168 | newTouchedWindowHandle = nullptr; |
| 2169 | } |
| 2170 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2171 | if (oldTouchedWindowHandle != newTouchedWindowHandle && |
| 2172 | oldTouchedWindowHandle != nullptr && newTouchedWindowHandle != nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2173 | if (DEBUG_FOCUS) { |
| 2174 | ALOGD("Touch is slipping out of window %s into window %s in display %" PRId32, |
| 2175 | oldTouchedWindowHandle->getName().c_str(), |
| 2176 | newTouchedWindowHandle->getName().c_str(), displayId); |
| 2177 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2178 | // Make a slippery exit from the old window. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2179 | tempTouchState.addOrUpdateWindow(oldTouchedWindowHandle, |
| 2180 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT, |
| 2181 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2182 | |
| 2183 | // Make a slippery entrance into the new window. |
| 2184 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
| 2185 | isSplit = true; |
| 2186 | } |
| 2187 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2188 | int32_t targetFlags = |
| 2189 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2190 | if (isSplit) { |
| 2191 | targetFlags |= InputTarget::FLAG_SPLIT; |
| 2192 | } |
| 2193 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
| 2194 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2195 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
| 2196 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2197 | } |
| 2198 | |
| 2199 | BitSet32 pointerIds; |
| 2200 | if (isSplit) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2201 | pointerIds.markBit(entry.pointerProperties[0].id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2202 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2203 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2204 | } |
| 2205 | } |
| 2206 | } |
| 2207 | |
| 2208 | if (newHoverWindowHandle != mLastHoverWindowHandle) { |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2209 | // Let the previous window know that the hover sequence is over, unless we already did it |
| 2210 | // when dispatching it as is to newTouchedWindowHandle. |
| 2211 | if (mLastHoverWindowHandle != nullptr && |
| 2212 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT || |
| 2213 | mLastHoverWindowHandle != newTouchedWindowHandle)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2214 | if (DEBUG_HOVER) { |
| 2215 | ALOGD("Sending hover exit event to window %s.", |
| 2216 | mLastHoverWindowHandle->getName().c_str()); |
| 2217 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2218 | tempTouchState.addOrUpdateWindow(mLastHoverWindowHandle, |
| 2219 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT, BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2220 | } |
| 2221 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2222 | // Let the new window know that the hover sequence is starting, unless we already did it |
| 2223 | // when dispatching it as is to newTouchedWindowHandle. |
| 2224 | if (newHoverWindowHandle != nullptr && |
| 2225 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2226 | newHoverWindowHandle != newTouchedWindowHandle)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2227 | if (DEBUG_HOVER) { |
| 2228 | ALOGD("Sending hover enter event to window %s.", |
| 2229 | newHoverWindowHandle->getName().c_str()); |
| 2230 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2231 | tempTouchState.addOrUpdateWindow(newHoverWindowHandle, |
| 2232 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER, |
| 2233 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2234 | } |
| 2235 | } |
| 2236 | |
| 2237 | // Check permission to inject into all touched foreground windows and ensure there |
| 2238 | // is at least one touched foreground window. |
| 2239 | { |
| 2240 | bool haveForegroundWindow = false; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2241 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2242 | if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) { |
| 2243 | haveForegroundWindow = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2244 | if (!checkInjectionPermission(touchedWindow.windowHandle, entry.injectionState)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2245 | injectionResult = InputEventInjectionResult::PERMISSION_DENIED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2246 | injectionPermission = INJECTION_PERMISSION_DENIED; |
| 2247 | goto Failed; |
| 2248 | } |
| 2249 | } |
| 2250 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2251 | bool hasGestureMonitor = !tempTouchState.gestureMonitors.empty(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2252 | if (!haveForegroundWindow && !hasGestureMonitor) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2253 | ALOGI("Dropping event because there is no touched foreground window in display " |
| 2254 | "%" PRId32 " or gesture monitor to receive it.", |
| 2255 | displayId); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2256 | injectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2257 | goto Failed; |
| 2258 | } |
| 2259 | |
| 2260 | // Permission granted to injection into all touched foreground windows. |
| 2261 | injectionPermission = INJECTION_PERMISSION_GRANTED; |
| 2262 | } |
| 2263 | |
| 2264 | // Check whether windows listening for outside touches are owned by the same UID. If it is |
| 2265 | // set the policy flag that we will not reveal coordinate information to this window. |
| 2266 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2267 | sp<WindowInfoHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2268 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2269 | if (foregroundWindowHandle) { |
| 2270 | const int32_t foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2271 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2272 | if (touchedWindow.targetFlags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2273 | sp<WindowInfoHandle> windowInfoHandle = touchedWindow.windowHandle; |
| 2274 | if (windowInfoHandle->getInfo()->ownerUid != foregroundWindowUid) { |
| 2275 | tempTouchState.addOrUpdateWindow(windowInfoHandle, |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2276 | InputTarget::FLAG_ZERO_COORDS, |
| 2277 | BitSet32(0)); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2278 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2279 | } |
| 2280 | } |
| 2281 | } |
| 2282 | } |
| 2283 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2284 | // If this is the first pointer going down and the touched window has a wallpaper |
| 2285 | // then also add the touched wallpaper windows so they are locked in for the duration |
| 2286 | // of the touch gesture. |
| 2287 | // We do not collect wallpapers during HOVER_MOVE or SCROLL because the wallpaper |
| 2288 | // engine only supports touch events. We would need to add a mechanism similar |
| 2289 | // to View.onGenericMotionEvent to enable wallpapers to handle these events. |
| 2290 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2291 | sp<WindowInfoHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2292 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2293 | if (foregroundWindowHandle && foregroundWindowHandle->getInfo()->hasWallpaper) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2294 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2295 | getWindowHandlesLocked(displayId); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2296 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 2297 | const WindowInfo* info = windowHandle->getInfo(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2298 | if (info->displayId == displayId && |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2299 | windowHandle->getInfo()->type == WindowInfo::Type::WALLPAPER) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2300 | tempTouchState |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2301 | .addOrUpdateWindow(windowHandle, |
| 2302 | InputTarget::FLAG_WINDOW_IS_OBSCURED | |
| 2303 | InputTarget:: |
| 2304 | FLAG_WINDOW_IS_PARTIALLY_OBSCURED | |
| 2305 | InputTarget::FLAG_DISPATCH_AS_IS, |
| 2306 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2307 | } |
| 2308 | } |
| 2309 | } |
| 2310 | } |
| 2311 | |
| 2312 | // Success! Output targets. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2313 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2314 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2315 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2316 | addWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2317 | touchedWindow.pointerIds, inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2318 | } |
| 2319 | |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2320 | for (const auto& monitor : tempTouchState.gestureMonitors) { |
| 2321 | addMonitoringTargetLocked(monitor, displayId, inputTargets); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2322 | } |
| 2323 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2324 | // Drop the outside or hover touch windows since we will not care about them |
| 2325 | // in the next iteration. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2326 | tempTouchState.filterNonAsIsTouchWindows(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2327 | |
| 2328 | Failed: |
| 2329 | // Check injection permission once and for all. |
| 2330 | if (injectionPermission == INJECTION_PERMISSION_UNKNOWN) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2331 | if (checkInjectionPermission(nullptr, entry.injectionState)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2332 | injectionPermission = INJECTION_PERMISSION_GRANTED; |
| 2333 | } else { |
| 2334 | injectionPermission = INJECTION_PERMISSION_DENIED; |
| 2335 | } |
| 2336 | } |
| 2337 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2338 | if (injectionPermission != INJECTION_PERMISSION_GRANTED) { |
| 2339 | return injectionResult; |
| 2340 | } |
| 2341 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2342 | // Update final pieces of touch state if the injector had permission. |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2343 | if (!wrongDevice) { |
| 2344 | if (switchedDevice) { |
| 2345 | if (DEBUG_FOCUS) { |
| 2346 | ALOGD("Conflicting pointer actions: Switched to a different device."); |
| 2347 | } |
| 2348 | *outConflictingPointerActions = true; |
| 2349 | } |
| 2350 | |
| 2351 | if (isHoverAction) { |
| 2352 | // Started hovering, therefore no longer down. |
| 2353 | if (oldState && oldState->down) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2354 | if (DEBUG_FOCUS) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2355 | ALOGD("Conflicting pointer actions: Hover received while pointer was " |
| 2356 | "down."); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2357 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2358 | *outConflictingPointerActions = true; |
| 2359 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2360 | tempTouchState.reset(); |
| 2361 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2362 | maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) { |
| 2363 | tempTouchState.deviceId = entry.deviceId; |
| 2364 | tempTouchState.source = entry.source; |
| 2365 | tempTouchState.displayId = displayId; |
| 2366 | } |
| 2367 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP || |
| 2368 | maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
| 2369 | // All pointers up or canceled. |
| 2370 | tempTouchState.reset(); |
| 2371 | } else if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 2372 | // First pointer went down. |
| 2373 | if (oldState && oldState->down) { |
| 2374 | if (DEBUG_FOCUS) { |
| 2375 | ALOGD("Conflicting pointer actions: Down received while already down."); |
| 2376 | } |
| 2377 | *outConflictingPointerActions = true; |
| 2378 | } |
| 2379 | } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 2380 | // One pointer went up. |
| 2381 | if (isSplit) { |
| 2382 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
| 2383 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2384 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2385 | for (size_t i = 0; i < tempTouchState.windows.size();) { |
| 2386 | TouchedWindow& touchedWindow = tempTouchState.windows[i]; |
| 2387 | if (touchedWindow.targetFlags & InputTarget::FLAG_SPLIT) { |
| 2388 | touchedWindow.pointerIds.clearBit(pointerId); |
| 2389 | if (touchedWindow.pointerIds.isEmpty()) { |
| 2390 | tempTouchState.windows.erase(tempTouchState.windows.begin() + i); |
| 2391 | continue; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2392 | } |
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 | i += 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2395 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2396 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2397 | } |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2398 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2399 | // Save changes unless the action was scroll in which case the temporary touch |
| 2400 | // state was only valid for this one action. |
| 2401 | if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) { |
| 2402 | if (tempTouchState.displayId >= 0) { |
| 2403 | mTouchStatesByDisplay[displayId] = tempTouchState; |
| 2404 | } else { |
| 2405 | mTouchStatesByDisplay.erase(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2406 | } |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2407 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2408 | |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2409 | // Update hover state. |
| 2410 | mLastHoverWindowHandle = newHoverWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2411 | } |
| 2412 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2413 | return injectionResult; |
| 2414 | } |
| 2415 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2416 | void InputDispatcher::finishDragAndDrop(int32_t displayId, float x, float y) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2417 | const sp<WindowInfoHandle> dropWindow = |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2418 | findTouchedWindowAtLocked(displayId, x, y, nullptr /*touchState*/, |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 2419 | false /*addOutsideTargets*/, true /*ignoreDragWindow*/); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2420 | if (dropWindow) { |
| 2421 | vec2 local = dropWindow->getInfo()->transform.transform(x, y); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2422 | sendDropWindowCommandLocked(dropWindow->getToken(), local.x, local.y); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 2423 | } else { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2424 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2425 | } |
| 2426 | mDragState.reset(); |
| 2427 | } |
| 2428 | |
| 2429 | void InputDispatcher::addDragEventLocked(const MotionEntry& entry) { |
| 2430 | if (entry.pointerCount != 1 || !mDragState) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2431 | return; |
| 2432 | } |
| 2433 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2434 | if (!mDragState->isStartDrag) { |
| 2435 | mDragState->isStartDrag = true; |
| 2436 | mDragState->isStylusButtonDownAtStart = |
| 2437 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2438 | } |
| 2439 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2440 | int32_t maskedAction = entry.action & AMOTION_EVENT_ACTION_MASK; |
| 2441 | int32_t x = static_cast<int32_t>(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 2442 | int32_t y = static_cast<int32_t>(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
| 2443 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2444 | // Handle the special case : stylus button no longer pressed. |
| 2445 | bool isStylusButtonDown = (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2446 | if (mDragState->isStylusButtonDownAtStart && !isStylusButtonDown) { |
| 2447 | finishDragAndDrop(entry.displayId, x, y); |
| 2448 | return; |
| 2449 | } |
| 2450 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2451 | const sp<WindowInfoHandle> hoverWindowHandle = |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2452 | findTouchedWindowAtLocked(entry.displayId, x, y, nullptr /*touchState*/, |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 2453 | false /*addOutsideTargets*/, true /*ignoreDragWindow*/); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2454 | // enqueue drag exit if needed. |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2455 | if (hoverWindowHandle != mDragState->dragHoverWindowHandle && |
| 2456 | !haveSameToken(hoverWindowHandle, mDragState->dragHoverWindowHandle)) { |
| 2457 | if (mDragState->dragHoverWindowHandle != nullptr) { |
| 2458 | enqueueDragEventLocked(mDragState->dragHoverWindowHandle, true /*isExiting*/, |
| 2459 | entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2460 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2461 | mDragState->dragHoverWindowHandle = hoverWindowHandle; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2462 | } |
| 2463 | // enqueue drag location if needed. |
| 2464 | if (hoverWindowHandle != nullptr) { |
| 2465 | enqueueDragEventLocked(hoverWindowHandle, false /*isExiting*/, entry); |
| 2466 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2467 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP) { |
| 2468 | finishDragAndDrop(entry.displayId, x, y); |
| 2469 | } else if (maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2470 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2471 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2472 | } |
| 2473 | } |
| 2474 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2475 | void InputDispatcher::addWindowTargetLocked(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2476 | int32_t targetFlags, BitSet32 pointerIds, |
| 2477 | std::vector<InputTarget>& inputTargets) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2478 | std::vector<InputTarget>::iterator it = |
| 2479 | std::find_if(inputTargets.begin(), inputTargets.end(), |
| 2480 | [&windowHandle](const InputTarget& inputTarget) { |
| 2481 | return inputTarget.inputChannel->getConnectionToken() == |
| 2482 | windowHandle->getToken(); |
| 2483 | }); |
Chavi Weingarten | 97b8eec | 2020-01-09 18:09:08 +0000 | [diff] [blame] | 2484 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2485 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2486 | |
| 2487 | if (it == inputTargets.end()) { |
| 2488 | InputTarget inputTarget; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 2489 | std::shared_ptr<InputChannel> inputChannel = |
| 2490 | getInputChannelLocked(windowHandle->getToken()); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2491 | if (inputChannel == nullptr) { |
| 2492 | ALOGW("Window %s already unregistered input channel", windowHandle->getName().c_str()); |
| 2493 | return; |
| 2494 | } |
| 2495 | inputTarget.inputChannel = inputChannel; |
| 2496 | inputTarget.flags = targetFlags; |
| 2497 | inputTarget.globalScaleFactor = windowInfo->globalScaleFactor; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2498 | const auto& displayInfoIt = mDisplayInfos.find(windowInfo->displayId); |
| 2499 | if (displayInfoIt != mDisplayInfos.end()) { |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 2500 | inputTarget.displayTransform = displayInfoIt->second.transform; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2501 | } else { |
| 2502 | ALOGI_IF(isPerWindowInputRotationEnabled(), |
| 2503 | "DisplayInfo not found for window on display: %d", windowInfo->displayId); |
| 2504 | } |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2505 | inputTargets.push_back(inputTarget); |
| 2506 | it = inputTargets.end() - 1; |
| 2507 | } |
| 2508 | |
| 2509 | ALOG_ASSERT(it->flags == targetFlags); |
| 2510 | ALOG_ASSERT(it->globalScaleFactor == windowInfo->globalScaleFactor); |
| 2511 | |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2512 | it->addPointers(pointerIds, windowInfo->transform); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2513 | } |
| 2514 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2515 | void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2516 | int32_t displayId) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2517 | std::unordered_map<int32_t, std::vector<Monitor>>::const_iterator it = |
| 2518 | mGlobalMonitorsByDisplay.find(displayId); |
| 2519 | |
| 2520 | if (it != mGlobalMonitorsByDisplay.end()) { |
| 2521 | const std::vector<Monitor>& monitors = it->second; |
| 2522 | for (const Monitor& monitor : monitors) { |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2523 | addMonitoringTargetLocked(monitor, displayId, inputTargets); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2524 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2525 | } |
| 2526 | } |
| 2527 | |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2528 | void InputDispatcher::addMonitoringTargetLocked(const Monitor& monitor, int32_t displayId, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2529 | std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2530 | InputTarget target; |
| 2531 | target.inputChannel = monitor.inputChannel; |
| 2532 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2533 | ui::Transform t; |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 2534 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
| 2535 | // Input monitors always get un-rotated display coordinates. We undo the display |
| 2536 | // rotation that is present in the display transform so that display rotation is not |
| 2537 | // applied to these input targets. |
| 2538 | const auto& displayInfo = it->second; |
| 2539 | int32_t width = displayInfo.logicalWidth; |
| 2540 | int32_t height = displayInfo.logicalHeight; |
| 2541 | const auto orientation = displayInfo.transform.getOrientation(); |
| 2542 | uint32_t inverseOrientation = orientation; |
| 2543 | if (orientation == ui::Transform::ROT_90) { |
| 2544 | inverseOrientation = ui::Transform::ROT_270; |
| 2545 | std::swap(width, height); |
| 2546 | } else if (orientation == ui::Transform::ROT_270) { |
| 2547 | inverseOrientation = ui::Transform::ROT_90; |
| 2548 | std::swap(width, height); |
| 2549 | } |
| 2550 | target.displayTransform = |
| 2551 | ui::Transform(inverseOrientation, width, height) * displayInfo.transform; |
| 2552 | t = t * target.displayTransform; |
| 2553 | } |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2554 | target.setDefaultPointerTransform(t); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2555 | inputTargets.push_back(target); |
| 2556 | } |
| 2557 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2558 | bool InputDispatcher::checkInjectionPermission(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2559 | const InjectionState* injectionState) { |
| 2560 | if (injectionState && |
| 2561 | (windowHandle == nullptr || |
| 2562 | windowHandle->getInfo()->ownerUid != injectionState->injectorUid) && |
| 2563 | !hasInjectionPermission(injectionState->injectorPid, injectionState->injectorUid)) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2564 | if (windowHandle != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2565 | 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] | 2566 | "owned by uid %d", |
| 2567 | injectionState->injectorPid, injectionState->injectorUid, |
| 2568 | windowHandle->getName().c_str(), windowHandle->getInfo()->ownerUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2569 | } else { |
| 2570 | ALOGW("Permission denied: injecting event from pid %d uid %d", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2571 | injectionState->injectorPid, injectionState->injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2572 | } |
| 2573 | return false; |
| 2574 | } |
| 2575 | return true; |
| 2576 | } |
| 2577 | |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2578 | /** |
| 2579 | * Indicate whether one window handle should be considered as obscuring |
| 2580 | * another window handle. We only check a few preconditions. Actually |
| 2581 | * checking the bounds is left to the caller. |
| 2582 | */ |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2583 | static bool canBeObscuredBy(const sp<WindowInfoHandle>& windowHandle, |
| 2584 | const sp<WindowInfoHandle>& otherHandle) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2585 | // Compare by token so cloned layers aren't counted |
| 2586 | if (haveSameToken(windowHandle, otherHandle)) { |
| 2587 | return false; |
| 2588 | } |
| 2589 | auto info = windowHandle->getInfo(); |
| 2590 | auto otherInfo = otherHandle->getInfo(); |
| 2591 | if (!otherInfo->visible) { |
| 2592 | return false; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2593 | } else if (otherInfo->alpha == 0 && otherInfo->flags.test(WindowInfo::Flag::NOT_TOUCHABLE)) { |
Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame] | 2594 | // Those act as if they were invisible, so we don't need to flag them. |
| 2595 | // We do want to potentially flag touchable windows even if they have 0 |
| 2596 | // opacity, since they can consume touches and alter the effects of the |
| 2597 | // user interaction (eg. apps that rely on |
| 2598 | // FLAG_WINDOW_IS_PARTIALLY_OBSCURED should still be told about those |
| 2599 | // windows), hence we also check for FLAG_NOT_TOUCHABLE. |
| 2600 | return false; |
Bernardo Rufino | 8007daf | 2020-09-22 09:40:01 +0000 | [diff] [blame] | 2601 | } else if (info->ownerUid == otherInfo->ownerUid) { |
| 2602 | // If ownerUid is the same we don't generate occlusion events as there |
| 2603 | // is no security boundary within an uid. |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2604 | return false; |
Chris Ye | fcdff3e | 2020-05-10 15:16:04 -0700 | [diff] [blame] | 2605 | } else if (otherInfo->trustedOverlay) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2606 | return false; |
| 2607 | } else if (otherInfo->displayId != info->displayId) { |
| 2608 | return false; |
| 2609 | } |
| 2610 | return true; |
| 2611 | } |
| 2612 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2613 | /** |
| 2614 | * Returns touch occlusion information in the form of TouchOcclusionInfo. To check if the touch is |
| 2615 | * untrusted, one should check: |
| 2616 | * |
| 2617 | * 1. If result.hasBlockingOcclusion is true. |
| 2618 | * If it's, it means the touch should be blocked due to a window with occlusion mode of |
| 2619 | * BLOCK_UNTRUSTED. |
| 2620 | * |
| 2621 | * 2. If result.obscuringOpacity > mMaximumObscuringOpacityForTouch. |
| 2622 | * If it is (and 1 is false), then the touch should be blocked because a stack of windows |
| 2623 | * (possibly only one) with occlusion mode of USE_OPACITY from one UID resulted in a composed |
| 2624 | * obscuring opacity above the threshold. Note that if there was no window of occlusion mode |
| 2625 | * USE_OPACITY, result.obscuringOpacity would've been 0 and since |
| 2626 | * mMaximumObscuringOpacityForTouch >= 0, the condition above would never be true. |
| 2627 | * |
| 2628 | * If neither of those is true, then it means the touch can be allowed. |
| 2629 | */ |
| 2630 | InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2631 | const sp<WindowInfoHandle>& windowHandle, int32_t x, int32_t y) const { |
| 2632 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2633 | int32_t displayId = windowInfo->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2634 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2635 | TouchOcclusionInfo info; |
| 2636 | info.hasBlockingOcclusion = false; |
| 2637 | info.obscuringOpacity = 0; |
| 2638 | info.obscuringUid = -1; |
| 2639 | std::map<int32_t, float> opacityByUid; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2640 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2641 | if (windowHandle == otherHandle) { |
| 2642 | break; // All future windows are below us. Exit early. |
| 2643 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2644 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 2645 | if (canBeObscuredBy(windowHandle, otherHandle) && otherInfo->frameContainsPoint(x, y) && |
| 2646 | !haveSameApplicationToken(windowInfo, otherInfo)) { |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2647 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2648 | info.debugInfo.push_back( |
| 2649 | dumpWindowForTouchOcclusion(otherInfo, /* isTouchedWindow */ false)); |
| 2650 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2651 | // canBeObscuredBy() has returned true above, which means this window is untrusted, so |
| 2652 | // we perform the checks below to see if the touch can be propagated or not based on the |
| 2653 | // window's touch occlusion mode |
| 2654 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::BLOCK_UNTRUSTED) { |
| 2655 | info.hasBlockingOcclusion = true; |
| 2656 | info.obscuringUid = otherInfo->ownerUid; |
| 2657 | info.obscuringPackage = otherInfo->packageName; |
| 2658 | break; |
| 2659 | } |
| 2660 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::USE_OPACITY) { |
| 2661 | uint32_t uid = otherInfo->ownerUid; |
| 2662 | float opacity = |
| 2663 | (opacityByUid.find(uid) == opacityByUid.end()) ? 0 : opacityByUid[uid]; |
| 2664 | // Given windows A and B: |
| 2665 | // opacity(A, B) = 1 - [1 - opacity(A)] * [1 - opacity(B)] |
| 2666 | opacity = 1 - (1 - opacity) * (1 - otherInfo->alpha); |
| 2667 | opacityByUid[uid] = opacity; |
| 2668 | if (opacity > info.obscuringOpacity) { |
| 2669 | info.obscuringOpacity = opacity; |
| 2670 | info.obscuringUid = uid; |
| 2671 | info.obscuringPackage = otherInfo->packageName; |
| 2672 | } |
| 2673 | } |
| 2674 | } |
| 2675 | } |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2676 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2677 | info.debugInfo.push_back( |
| 2678 | dumpWindowForTouchOcclusion(windowInfo, /* isTouchedWindow */ true)); |
| 2679 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2680 | return info; |
| 2681 | } |
| 2682 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2683 | std::string InputDispatcher::dumpWindowForTouchOcclusion(const WindowInfo* info, |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2684 | bool isTouchedWindow) const { |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 2685 | return StringPrintf(INDENT2 |
| 2686 | "* %stype=%s, package=%s/%" PRId32 ", id=%" PRId32 ", mode=%s, alpha=%.2f, " |
| 2687 | "frame=[%" PRId32 ",%" PRId32 "][%" PRId32 ",%" PRId32 |
| 2688 | "], touchableRegion=%s, window={%s}, flags={%s}, inputFeatures={%s}, " |
| 2689 | "hasToken=%s, applicationInfo.name=%s, applicationInfo.token=%s\n", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2690 | isTouchedWindow ? "[TOUCHED] " : "", ftl::enum_string(info->type).c_str(), |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 2691 | info->packageName.c_str(), info->ownerUid, info->id, |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 2692 | toString(info->touchOcclusionMode).c_str(), info->alpha, info->frameLeft, |
| 2693 | info->frameTop, info->frameRight, info->frameBottom, |
| 2694 | dumpRegion(info->touchableRegion).c_str(), info->name.c_str(), |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 2695 | info->flags.string().c_str(), info->inputFeatures.string().c_str(), |
| 2696 | toString(info->token != nullptr), info->applicationInfo.name.c_str(), |
| 2697 | toString(info->applicationInfo.token).c_str()); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2698 | } |
| 2699 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2700 | bool InputDispatcher::isTouchTrustedLocked(const TouchOcclusionInfo& occlusionInfo) const { |
| 2701 | if (occlusionInfo.hasBlockingOcclusion) { |
| 2702 | ALOGW("Untrusted touch due to occlusion by %s/%d", occlusionInfo.obscuringPackage.c_str(), |
| 2703 | occlusionInfo.obscuringUid); |
| 2704 | return false; |
| 2705 | } |
| 2706 | if (occlusionInfo.obscuringOpacity > mMaximumObscuringOpacityForTouch) { |
| 2707 | ALOGW("Untrusted touch due to occlusion by %s/%d (obscuring opacity = " |
| 2708 | "%.2f, maximum allowed = %.2f)", |
| 2709 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid, |
| 2710 | occlusionInfo.obscuringOpacity, mMaximumObscuringOpacityForTouch); |
| 2711 | return false; |
| 2712 | } |
| 2713 | return true; |
| 2714 | } |
| 2715 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2716 | bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2717 | int32_t x, int32_t y) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2718 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2719 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 2720 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2721 | if (windowHandle == otherHandle) { |
| 2722 | break; // All future windows are below us. Exit early. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2723 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2724 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2725 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2726 | otherInfo->frameContainsPoint(x, y)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2727 | return true; |
| 2728 | } |
| 2729 | } |
| 2730 | return false; |
| 2731 | } |
| 2732 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2733 | bool InputDispatcher::isWindowObscuredLocked(const sp<WindowInfoHandle>& windowHandle) const { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2734 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2735 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 2736 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 2737 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2738 | if (windowHandle == otherHandle) { |
| 2739 | break; // All future windows are below us. Exit early. |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2740 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2741 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2742 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2743 | otherInfo->overlaps(windowInfo)) { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2744 | return true; |
| 2745 | } |
| 2746 | } |
| 2747 | return false; |
| 2748 | } |
| 2749 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2750 | std::string InputDispatcher::getApplicationWindowLabel( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2751 | const InputApplicationHandle* applicationHandle, const sp<WindowInfoHandle>& windowHandle) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2752 | if (applicationHandle != nullptr) { |
| 2753 | if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2754 | return applicationHandle->getName() + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2755 | } else { |
| 2756 | return applicationHandle->getName(); |
| 2757 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2758 | } else if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2759 | return windowHandle->getInfo()->applicationInfo.name + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2760 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2761 | return "<unknown application or window>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2762 | } |
| 2763 | } |
| 2764 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2765 | void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 2766 | if (eventEntry.type == EventEntry::Type::FOCUS || |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2767 | eventEntry.type == EventEntry::Type::POINTER_CAPTURE_CHANGED || |
| 2768 | eventEntry.type == EventEntry::Type::DRAG) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 2769 | // Focus or pointer capture changed events are passed to apps, but do not represent user |
| 2770 | // activity. |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2771 | return; |
| 2772 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2773 | int32_t displayId = getTargetDisplayId(eventEntry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2774 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2775 | if (focusedWindowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2776 | const WindowInfo* info = focusedWindowHandle->getInfo(); |
| 2777 | if (info->inputFeatures.test(WindowInfo::Feature::DISABLE_USER_ACTIVITY)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2778 | if (DEBUG_DISPATCH_CYCLE) { |
| 2779 | ALOGD("Not poking user activity: disabled by window '%s'.", info->name.c_str()); |
| 2780 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2781 | return; |
| 2782 | } |
| 2783 | } |
| 2784 | |
| 2785 | int32_t eventType = USER_ACTIVITY_EVENT_OTHER; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2786 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2787 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2788 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 2789 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2790 | return; |
| 2791 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2792 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2793 | if (MotionEvent::isTouchEvent(motionEntry.source, motionEntry.action)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2794 | eventType = USER_ACTIVITY_EVENT_TOUCH; |
| 2795 | } |
| 2796 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2797 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2798 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2799 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 2800 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2801 | return; |
| 2802 | } |
| 2803 | eventType = USER_ACTIVITY_EVENT_BUTTON; |
| 2804 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2805 | } |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 2806 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 2807 | break; |
| 2808 | } |
| 2809 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2810 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2811 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 2812 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2813 | case EventEntry::Type::SENSOR: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2814 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 2815 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2816 | LOG_ALWAYS_FATAL("%s events are not user activity", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2817 | ftl::enum_string(eventEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2818 | break; |
| 2819 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2820 | } |
| 2821 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2822 | auto command = [this, eventTime = eventEntry.eventTime, eventType, displayId]() |
| 2823 | REQUIRES(mLock) { |
| 2824 | scoped_unlock unlock(mLock); |
| 2825 | mPolicy->pokeUserActivity(eventTime, eventType, displayId); |
| 2826 | }; |
| 2827 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2828 | } |
| 2829 | |
| 2830 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2831 | const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2832 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2833 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2834 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2835 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2836 | StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2837 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2838 | ATRACE_NAME(message.c_str()); |
| 2839 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2840 | if (DEBUG_DISPATCH_CYCLE) { |
| 2841 | ALOGD("channel '%s' ~ prepareDispatchCycle - flags=0x%08x, " |
| 2842 | "globalScaleFactor=%f, pointerIds=0x%x %s", |
| 2843 | connection->getInputChannelName().c_str(), inputTarget.flags, |
| 2844 | inputTarget.globalScaleFactor, inputTarget.pointerIds.value, |
| 2845 | inputTarget.getPointerInfoString().c_str()); |
| 2846 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2847 | |
| 2848 | // Skip this event if the connection status is not normal. |
| 2849 | // We don't want to enqueue additional outbound events if the connection is broken. |
| 2850 | if (connection->status != Connection::STATUS_NORMAL) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2851 | if (DEBUG_DISPATCH_CYCLE) { |
| 2852 | ALOGD("channel '%s' ~ Dropping event because the channel status is %s", |
| 2853 | connection->getInputChannelName().c_str(), connection->getStatusLabel()); |
| 2854 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2855 | return; |
| 2856 | } |
| 2857 | |
| 2858 | // Split a motion event if needed. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2859 | if (inputTarget.flags & InputTarget::FLAG_SPLIT) { |
| 2860 | LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION, |
| 2861 | "Entry type %s should not have FLAG_SPLIT", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2862 | ftl::enum_string(eventEntry->type).c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2863 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2864 | const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry); |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2865 | if (inputTarget.pointerIds.count() != originalMotionEntry.pointerCount) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2866 | std::unique_ptr<MotionEntry> splitMotionEntry = |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2867 | splitMotionEvent(originalMotionEntry, inputTarget.pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2868 | if (!splitMotionEntry) { |
| 2869 | return; // split event was dropped |
| 2870 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2871 | if (DEBUG_FOCUS) { |
| 2872 | ALOGD("channel '%s' ~ Split motion event.", |
| 2873 | connection->getInputChannelName().c_str()); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2874 | logOutboundMotionDetails(" ", *splitMotionEntry); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2875 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2876 | enqueueDispatchEntriesLocked(currentTime, connection, std::move(splitMotionEntry), |
| 2877 | inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2878 | return; |
| 2879 | } |
| 2880 | } |
| 2881 | |
| 2882 | // Not splitting. Enqueue dispatch entries for the event as is. |
| 2883 | enqueueDispatchEntriesLocked(currentTime, connection, eventEntry, inputTarget); |
| 2884 | } |
| 2885 | |
| 2886 | void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2887 | const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2888 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2889 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2890 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2891 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2892 | StringPrintf("enqueueDispatchEntriesLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2893 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2894 | ATRACE_NAME(message.c_str()); |
| 2895 | } |
| 2896 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2897 | bool wasEmpty = connection->outboundQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2898 | |
| 2899 | // Enqueue dispatch entries for the requested modes. |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2900 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2901 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2902 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2903 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2904 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2905 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2906 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2907 | InputTarget::FLAG_DISPATCH_AS_IS); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2908 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2909 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2910 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2911 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2912 | |
| 2913 | // If the outbound queue was previously empty, start the dispatch cycle going. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2914 | if (wasEmpty && !connection->outboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2915 | startDispatchCycleLocked(currentTime, connection); |
| 2916 | } |
| 2917 | } |
| 2918 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2919 | void InputDispatcher::enqueueDispatchEntryLocked(const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2920 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2921 | const InputTarget& inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2922 | int32_t dispatchMode) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2923 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2924 | std::string message = StringPrintf("enqueueDispatchEntry(inputChannel=%s, dispatchMode=%s)", |
| 2925 | connection->getInputChannelName().c_str(), |
| 2926 | dispatchModeToString(dispatchMode).c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2927 | ATRACE_NAME(message.c_str()); |
| 2928 | } |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2929 | int32_t inputTargetFlags = inputTarget.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2930 | if (!(inputTargetFlags & dispatchMode)) { |
| 2931 | return; |
| 2932 | } |
| 2933 | inputTargetFlags = (inputTargetFlags & ~InputTarget::FLAG_DISPATCH_MASK) | dispatchMode; |
| 2934 | |
| 2935 | // This is a new event. |
| 2936 | // Enqueue a new dispatch entry onto the outbound queue for this connection. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2937 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2938 | createDispatchEntry(inputTarget, eventEntry, inputTargetFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2939 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2940 | // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a |
| 2941 | // different EventEntry than what was passed in. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2942 | EventEntry& newEntry = *(dispatchEntry->eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2943 | // Apply target flags and update the connection's input state. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2944 | switch (newEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2945 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2946 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2947 | dispatchEntry->resolvedEventId = keyEntry.id; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2948 | dispatchEntry->resolvedAction = keyEntry.action; |
| 2949 | dispatchEntry->resolvedFlags = keyEntry.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2950 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2951 | if (!connection->inputState.trackKey(keyEntry, dispatchEntry->resolvedAction, |
| 2952 | dispatchEntry->resolvedFlags)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2953 | if (DEBUG_DISPATCH_CYCLE) { |
| 2954 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent key " |
| 2955 | "event", |
| 2956 | connection->getInputChannelName().c_str()); |
| 2957 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2958 | return; // skip the inconsistent event |
| 2959 | } |
| 2960 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2961 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2962 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2963 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2964 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2965 | // Assign a default value to dispatchEntry that will never be generated by InputReader, |
| 2966 | // and assign a InputDispatcher value if it doesn't change in the if-else chain below. |
| 2967 | constexpr int32_t DEFAULT_RESOLVED_EVENT_ID = |
| 2968 | static_cast<int32_t>(IdGenerator::Source::OTHER); |
| 2969 | dispatchEntry->resolvedEventId = DEFAULT_RESOLVED_EVENT_ID; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2970 | if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 2971 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; |
| 2972 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT) { |
| 2973 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; |
| 2974 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER) { |
| 2975 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 2976 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT) { |
| 2977 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_CANCEL; |
| 2978 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER) { |
| 2979 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_DOWN; |
| 2980 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2981 | dispatchEntry->resolvedAction = motionEntry.action; |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2982 | dispatchEntry->resolvedEventId = motionEntry.id; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2983 | } |
| 2984 | if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE && |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2985 | !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source, |
| 2986 | motionEntry.displayId)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2987 | if (DEBUG_DISPATCH_CYCLE) { |
| 2988 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: filling in missing hover " |
| 2989 | "enter event", |
| 2990 | connection->getInputChannelName().c_str()); |
| 2991 | } |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 2992 | // We keep the 'resolvedEventId' here equal to the original 'motionEntry.id' because |
| 2993 | // this is a one-to-one event conversion. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2994 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 2995 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2996 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2997 | dispatchEntry->resolvedFlags = motionEntry.flags; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2998 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_OBSCURED) { |
| 2999 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; |
| 3000 | } |
| 3001 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED) { |
| 3002 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 3003 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3004 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3005 | if (!connection->inputState.trackMotion(motionEntry, dispatchEntry->resolvedAction, |
| 3006 | dispatchEntry->resolvedFlags)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3007 | if (DEBUG_DISPATCH_CYCLE) { |
| 3008 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent motion " |
| 3009 | "event", |
| 3010 | connection->getInputChannelName().c_str()); |
| 3011 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3012 | return; // skip the inconsistent event |
| 3013 | } |
| 3014 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3015 | dispatchEntry->resolvedEventId = |
| 3016 | dispatchEntry->resolvedEventId == DEFAULT_RESOLVED_EVENT_ID |
| 3017 | ? mIdGenerator.nextId() |
| 3018 | : motionEntry.id; |
| 3019 | if (ATRACE_ENABLED() && dispatchEntry->resolvedEventId != motionEntry.id) { |
| 3020 | std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32 |
| 3021 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3022 | motionEntry.id, dispatchEntry->resolvedEventId); |
| 3023 | ATRACE_NAME(message.c_str()); |
| 3024 | } |
| 3025 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 3026 | if ((motionEntry.flags & AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) && |
| 3027 | (motionEntry.policyFlags & POLICY_FLAG_TRUSTED)) { |
| 3028 | // Skip reporting pointer down outside focus to the policy. |
| 3029 | break; |
| 3030 | } |
| 3031 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3032 | dispatchPointerDownOutsideFocus(motionEntry.source, dispatchEntry->resolvedAction, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3033 | inputTarget.inputChannel->getConnectionToken()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3034 | |
| 3035 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3036 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3037 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3038 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3039 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3040 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3041 | break; |
| 3042 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3043 | case EventEntry::Type::SENSOR: { |
| 3044 | LOG_ALWAYS_FATAL("SENSOR events should not go to apps via input channel"); |
| 3045 | break; |
| 3046 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3047 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 3048 | case EventEntry::Type::DEVICE_RESET: { |
| 3049 | LOG_ALWAYS_FATAL("%s events should not go to apps", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3050 | ftl::enum_string(newEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3051 | break; |
| 3052 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3053 | } |
| 3054 | |
| 3055 | // Remember that we are waiting for this dispatch to complete. |
| 3056 | if (dispatchEntry->hasForegroundTarget()) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3057 | incrementPendingForegroundDispatches(newEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3058 | } |
| 3059 | |
| 3060 | // Enqueue the dispatch entry. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3061 | connection->outboundQueue.push_back(dispatchEntry.release()); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3062 | traceOutboundQueueLength(*connection); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3063 | } |
| 3064 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3065 | /** |
| 3066 | * This function is purely for debugging. It helps us understand where the user interaction |
| 3067 | * was taking place. For example, if user is touching launcher, we will see a log that user |
| 3068 | * started interacting with launcher. In that example, the event would go to the wallpaper as well. |
| 3069 | * We will see both launcher and wallpaper in that list. |
| 3070 | * Once the interaction with a particular set of connections starts, no new logs will be printed |
| 3071 | * until the set of interacted connections changes. |
| 3072 | * |
| 3073 | * The following items are skipped, to reduce the logspam: |
| 3074 | * ACTION_OUTSIDE: any windows that are receiving ACTION_OUTSIDE are not logged |
| 3075 | * ACTION_UP: any windows that receive ACTION_UP are not logged (for both keys and motions). |
| 3076 | * This includes situations like the soft BACK button key. When the user releases (lifts up the |
| 3077 | * finger) the back button, then navigation bar will inject KEYCODE_BACK with ACTION_UP. |
| 3078 | * Both of those ACTION_UP events would not be logged |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3079 | */ |
| 3080 | void InputDispatcher::updateInteractionTokensLocked(const EventEntry& entry, |
| 3081 | const std::vector<InputTarget>& targets) { |
| 3082 | // Skip ACTION_UP events, and all events other than keys and motions |
| 3083 | if (entry.type == EventEntry::Type::KEY) { |
| 3084 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 3085 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
| 3086 | return; |
| 3087 | } |
| 3088 | } else if (entry.type == EventEntry::Type::MOTION) { |
| 3089 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 3090 | if (motionEntry.action == AMOTION_EVENT_ACTION_UP || |
| 3091 | motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
| 3092 | return; |
| 3093 | } |
| 3094 | } else { |
| 3095 | return; // Not a key or a motion |
| 3096 | } |
| 3097 | |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 3098 | std::unordered_set<sp<IBinder>, StrongPointerHash<IBinder>> newConnectionTokens; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3099 | std::vector<sp<Connection>> newConnections; |
| 3100 | for (const InputTarget& target : targets) { |
| 3101 | if ((target.flags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) == |
| 3102 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 3103 | continue; // Skip windows that receive ACTION_OUTSIDE |
| 3104 | } |
| 3105 | |
| 3106 | sp<IBinder> token = target.inputChannel->getConnectionToken(); |
| 3107 | sp<Connection> connection = getConnectionLocked(token); |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3108 | if (connection == nullptr) { |
| 3109 | continue; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3110 | } |
| 3111 | newConnectionTokens.insert(std::move(token)); |
| 3112 | newConnections.emplace_back(connection); |
| 3113 | } |
| 3114 | if (newConnectionTokens == mInteractionConnectionTokens) { |
| 3115 | return; // no change |
| 3116 | } |
| 3117 | mInteractionConnectionTokens = newConnectionTokens; |
| 3118 | |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3119 | std::string targetList; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3120 | for (const sp<Connection>& connection : newConnections) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3121 | targetList += connection->getWindowName() + ", "; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3122 | } |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3123 | std::string message = "Interaction with: " + targetList; |
| 3124 | if (targetList.empty()) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3125 | message += "<none>"; |
| 3126 | } |
| 3127 | android_log_event_list(LOGTAG_INPUT_INTERACTION) << message << LOG_ID_EVENTS; |
| 3128 | } |
| 3129 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3130 | void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3131 | const sp<IBinder>& token) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3132 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3133 | uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK; |
| 3134 | if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3135 | return; |
| 3136 | } |
| 3137 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 3138 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3139 | if (focusedToken == token) { |
| 3140 | // ignore since token is focused |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3141 | return; |
| 3142 | } |
| 3143 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3144 | auto command = [this, token]() REQUIRES(mLock) { |
| 3145 | scoped_unlock unlock(mLock); |
| 3146 | mPolicy->onPointerDownOutsideFocus(token); |
| 3147 | }; |
| 3148 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3149 | } |
| 3150 | |
| 3151 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3152 | const sp<Connection>& connection) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3153 | if (ATRACE_ENABLED()) { |
| 3154 | std::string message = StringPrintf("startDispatchCycleLocked(inputChannel=%s)", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3155 | connection->getInputChannelName().c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3156 | ATRACE_NAME(message.c_str()); |
| 3157 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3158 | if (DEBUG_DISPATCH_CYCLE) { |
| 3159 | ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str()); |
| 3160 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3161 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3162 | while (connection->status == Connection::STATUS_NORMAL && !connection->outboundQueue.empty()) { |
| 3163 | DispatchEntry* dispatchEntry = connection->outboundQueue.front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3164 | dispatchEntry->deliveryTime = currentTime; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3165 | const std::chrono::nanoseconds timeout = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3166 | getDispatchingTimeoutLocked(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3167 | dispatchEntry->timeoutTime = currentTime + timeout.count(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3168 | |
| 3169 | // Publish the event. |
| 3170 | status_t status; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3171 | const EventEntry& eventEntry = *(dispatchEntry->eventEntry); |
| 3172 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3173 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3174 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 3175 | std::array<uint8_t, 32> hmac = getSignature(keyEntry, *dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3176 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3177 | // Publish the key event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3178 | status = connection->inputPublisher |
| 3179 | .publishKeyEvent(dispatchEntry->seq, |
| 3180 | dispatchEntry->resolvedEventId, keyEntry.deviceId, |
| 3181 | keyEntry.source, keyEntry.displayId, |
| 3182 | std::move(hmac), dispatchEntry->resolvedAction, |
| 3183 | dispatchEntry->resolvedFlags, keyEntry.keyCode, |
| 3184 | keyEntry.scanCode, keyEntry.metaState, |
| 3185 | keyEntry.repeatCount, keyEntry.downTime, |
| 3186 | keyEntry.eventTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3187 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3188 | } |
| 3189 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3190 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3191 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3192 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3193 | PointerCoords scaledCoords[MAX_POINTERS]; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3194 | const PointerCoords* usingCoords = motionEntry.pointerCoords; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3195 | |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3196 | // 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] | 3197 | if ((motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) && |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3198 | !(dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS)) { |
| 3199 | float globalScaleFactor = dispatchEntry->globalScaleFactor; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3200 | if (globalScaleFactor != 1.0f) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3201 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
| 3202 | scaledCoords[i] = motionEntry.pointerCoords[i]; |
chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3203 | // Don't apply window scale here since we don't want scale to affect raw |
| 3204 | // coordinates. The scale will be sent back to the client and applied |
| 3205 | // later when requesting relative coordinates. |
| 3206 | scaledCoords[i].scale(globalScaleFactor, 1 /* windowXScale */, |
| 3207 | 1 /* windowYScale */); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3208 | } |
| 3209 | usingCoords = scaledCoords; |
| 3210 | } |
| 3211 | } else { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3212 | // We don't want the dispatch target to know. |
| 3213 | if (dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3214 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3215 | scaledCoords[i].clear(); |
| 3216 | } |
| 3217 | usingCoords = scaledCoords; |
| 3218 | } |
| 3219 | } |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3220 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3221 | std::array<uint8_t, 32> hmac = getSignature(motionEntry, *dispatchEntry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3222 | |
| 3223 | // Publish the motion event. |
| 3224 | status = connection->inputPublisher |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3225 | .publishMotionEvent(dispatchEntry->seq, |
| 3226 | dispatchEntry->resolvedEventId, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3227 | motionEntry.deviceId, motionEntry.source, |
| 3228 | motionEntry.displayId, std::move(hmac), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3229 | dispatchEntry->resolvedAction, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3230 | motionEntry.actionButton, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3231 | dispatchEntry->resolvedFlags, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3232 | motionEntry.edgeFlags, motionEntry.metaState, |
| 3233 | motionEntry.buttonState, |
| 3234 | motionEntry.classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3235 | dispatchEntry->transform, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3236 | motionEntry.xPrecision, motionEntry.yPrecision, |
| 3237 | motionEntry.xCursorPosition, |
| 3238 | motionEntry.yCursorPosition, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 3239 | dispatchEntry->rawTransform, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3240 | motionEntry.downTime, motionEntry.eventTime, |
| 3241 | motionEntry.pointerCount, |
| 3242 | motionEntry.pointerProperties, usingCoords); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3243 | break; |
| 3244 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3245 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3246 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3247 | const FocusEntry& focusEntry = static_cast<const FocusEntry&>(eventEntry); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3248 | status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3249 | focusEntry.id, |
| 3250 | focusEntry.hasFocus, |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3251 | mInTouchMode); |
| 3252 | break; |
| 3253 | } |
| 3254 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3255 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 3256 | const TouchModeEntry& touchModeEntry = |
| 3257 | static_cast<const TouchModeEntry&>(eventEntry); |
| 3258 | status = connection->inputPublisher |
| 3259 | .publishTouchModeEvent(dispatchEntry->seq, touchModeEntry.id, |
| 3260 | touchModeEntry.inTouchMode); |
| 3261 | |
| 3262 | break; |
| 3263 | } |
| 3264 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3265 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 3266 | const auto& captureEntry = |
| 3267 | static_cast<const PointerCaptureChangedEntry&>(eventEntry); |
| 3268 | status = connection->inputPublisher |
| 3269 | .publishCaptureEvent(dispatchEntry->seq, captureEntry.id, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 3270 | captureEntry.pointerCaptureRequest.enable); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3271 | break; |
| 3272 | } |
| 3273 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3274 | case EventEntry::Type::DRAG: { |
| 3275 | const DragEntry& dragEntry = static_cast<const DragEntry&>(eventEntry); |
| 3276 | status = connection->inputPublisher.publishDragEvent(dispatchEntry->seq, |
| 3277 | dragEntry.id, dragEntry.x, |
| 3278 | dragEntry.y, |
| 3279 | dragEntry.isExiting); |
| 3280 | break; |
| 3281 | } |
| 3282 | |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3283 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3284 | case EventEntry::Type::DEVICE_RESET: |
| 3285 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3286 | LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3287 | ftl::enum_string(eventEntry.type).c_str()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3288 | return; |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3289 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3290 | } |
| 3291 | |
| 3292 | // Check the result. |
| 3293 | if (status) { |
| 3294 | if (status == WOULD_BLOCK) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3295 | if (connection->waitQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3296 | ALOGE("channel '%s' ~ Could not publish event because the pipe is full. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3297 | "This is unexpected because the wait queue is empty, so the pipe " |
| 3298 | "should be empty and we shouldn't have any problems writing an " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3299 | "event to it, status=%s(%d)", |
| 3300 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3301 | status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3302 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 3303 | } else { |
| 3304 | // Pipe is full and we are waiting for the app to finish process some events |
| 3305 | // before sending more events to it. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3306 | if (DEBUG_DISPATCH_CYCLE) { |
| 3307 | ALOGD("channel '%s' ~ Could not publish event because the pipe is full, " |
| 3308 | "waiting for the application to catch up", |
| 3309 | connection->getInputChannelName().c_str()); |
| 3310 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3311 | } |
| 3312 | } else { |
| 3313 | ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3314 | "status=%s(%d)", |
| 3315 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3316 | status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3317 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 3318 | } |
| 3319 | return; |
| 3320 | } |
| 3321 | |
| 3322 | // Re-enqueue the event on the wait queue. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3323 | connection->outboundQueue.erase(std::remove(connection->outboundQueue.begin(), |
| 3324 | connection->outboundQueue.end(), |
| 3325 | dispatchEntry)); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3326 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3327 | connection->waitQueue.push_back(dispatchEntry); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3328 | if (connection->responsive) { |
| 3329 | mAnrTracker.insert(dispatchEntry->timeoutTime, |
| 3330 | connection->inputChannel->getConnectionToken()); |
| 3331 | } |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3332 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3333 | } |
| 3334 | } |
| 3335 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3336 | std::array<uint8_t, 32> InputDispatcher::sign(const VerifiedInputEvent& event) const { |
| 3337 | size_t size; |
| 3338 | switch (event.type) { |
| 3339 | case VerifiedInputEvent::Type::KEY: { |
| 3340 | size = sizeof(VerifiedKeyEvent); |
| 3341 | break; |
| 3342 | } |
| 3343 | case VerifiedInputEvent::Type::MOTION: { |
| 3344 | size = sizeof(VerifiedMotionEvent); |
| 3345 | break; |
| 3346 | } |
| 3347 | } |
| 3348 | const uint8_t* start = reinterpret_cast<const uint8_t*>(&event); |
| 3349 | return mHmacKeyManager.sign(start, size); |
| 3350 | } |
| 3351 | |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3352 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3353 | const MotionEntry& motionEntry, const DispatchEntry& dispatchEntry) const { |
| 3354 | int32_t actionMasked = dispatchEntry.resolvedAction & AMOTION_EVENT_ACTION_MASK; |
| 3355 | if ((actionMasked == AMOTION_EVENT_ACTION_UP) || (actionMasked == AMOTION_EVENT_ACTION_DOWN)) { |
| 3356 | // Only sign events up and down events as the purely move events |
| 3357 | // are tied to their up/down counterparts so signing would be redundant. |
| 3358 | VerifiedMotionEvent verifiedEvent = verifiedMotionEventFromMotionEntry(motionEntry); |
| 3359 | verifiedEvent.actionMasked = actionMasked; |
| 3360 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS; |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3361 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3362 | } |
| 3363 | return INVALID_HMAC; |
| 3364 | } |
| 3365 | |
| 3366 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3367 | const KeyEntry& keyEntry, const DispatchEntry& dispatchEntry) const { |
| 3368 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(keyEntry); |
| 3369 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_KEY_EVENT_FLAGS; |
| 3370 | verifiedEvent.action = dispatchEntry.resolvedAction; |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3371 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3372 | } |
| 3373 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3374 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3375 | const sp<Connection>& connection, uint32_t seq, |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 3376 | bool handled, nsecs_t consumeTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3377 | if (DEBUG_DISPATCH_CYCLE) { |
| 3378 | ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s", |
| 3379 | connection->getInputChannelName().c_str(), seq, toString(handled)); |
| 3380 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3381 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3382 | if (connection->status == Connection::STATUS_BROKEN || |
| 3383 | connection->status == Connection::STATUS_ZOMBIE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3384 | return; |
| 3385 | } |
| 3386 | |
| 3387 | // Notify other system components and prepare to start the next dispatch cycle. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3388 | auto command = [this, currentTime, connection, seq, handled, consumeTime]() REQUIRES(mLock) { |
| 3389 | doDispatchCycleFinishedCommand(currentTime, connection, seq, handled, consumeTime); |
| 3390 | }; |
| 3391 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3392 | } |
| 3393 | |
| 3394 | void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3395 | const sp<Connection>& connection, |
| 3396 | bool notify) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3397 | if (DEBUG_DISPATCH_CYCLE) { |
| 3398 | ALOGD("channel '%s' ~ abortBrokenDispatchCycle - notify=%s", |
| 3399 | connection->getInputChannelName().c_str(), toString(notify)); |
| 3400 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3401 | |
| 3402 | // Clear the dispatch queues. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3403 | drainDispatchQueue(connection->outboundQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3404 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3405 | drainDispatchQueue(connection->waitQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3406 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3407 | |
| 3408 | // The connection appears to be unrecoverably broken. |
| 3409 | // Ignore already broken or zombie connections. |
| 3410 | if (connection->status == Connection::STATUS_NORMAL) { |
| 3411 | connection->status = Connection::STATUS_BROKEN; |
| 3412 | |
| 3413 | if (notify) { |
| 3414 | // Notify other system components. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3415 | ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", |
| 3416 | connection->getInputChannelName().c_str()); |
| 3417 | |
| 3418 | auto command = [this, connection]() REQUIRES(mLock) { |
| 3419 | if (connection->status == Connection::STATUS_ZOMBIE) return; |
| 3420 | scoped_unlock unlock(mLock); |
| 3421 | mPolicy->notifyInputChannelBroken(connection->inputChannel->getConnectionToken()); |
| 3422 | }; |
| 3423 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3424 | } |
| 3425 | } |
| 3426 | } |
| 3427 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3428 | void InputDispatcher::drainDispatchQueue(std::deque<DispatchEntry*>& queue) { |
| 3429 | while (!queue.empty()) { |
| 3430 | DispatchEntry* dispatchEntry = queue.front(); |
| 3431 | queue.pop_front(); |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3432 | releaseDispatchEntry(dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3433 | } |
| 3434 | } |
| 3435 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3436 | void InputDispatcher::releaseDispatchEntry(DispatchEntry* dispatchEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3437 | if (dispatchEntry->hasForegroundTarget()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3438 | decrementPendingForegroundDispatches(*(dispatchEntry->eventEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3439 | } |
| 3440 | delete dispatchEntry; |
| 3441 | } |
| 3442 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3443 | int InputDispatcher::handleReceiveCallback(int events, sp<IBinder> connectionToken) { |
| 3444 | std::scoped_lock _l(mLock); |
| 3445 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 3446 | if (connection == nullptr) { |
| 3447 | ALOGW("Received looper callback for unknown input channel token %p. events=0x%x", |
| 3448 | connectionToken.get(), events); |
| 3449 | return 0; // remove the callback |
| 3450 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3451 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3452 | bool notify; |
| 3453 | if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { |
| 3454 | if (!(events & ALOOPER_EVENT_INPUT)) { |
| 3455 | ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. " |
| 3456 | "events=0x%x", |
| 3457 | connection->getInputChannelName().c_str(), events); |
| 3458 | return 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3459 | } |
| 3460 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3461 | nsecs_t currentTime = now(); |
| 3462 | bool gotOne = false; |
| 3463 | status_t status = OK; |
| 3464 | for (;;) { |
| 3465 | Result<InputPublisher::ConsumerResponse> result = |
| 3466 | connection->inputPublisher.receiveConsumerResponse(); |
| 3467 | if (!result.ok()) { |
| 3468 | status = result.error().code(); |
| 3469 | break; |
| 3470 | } |
| 3471 | |
| 3472 | if (std::holds_alternative<InputPublisher::Finished>(*result)) { |
| 3473 | const InputPublisher::Finished& finish = |
| 3474 | std::get<InputPublisher::Finished>(*result); |
| 3475 | finishDispatchCycleLocked(currentTime, connection, finish.seq, finish.handled, |
| 3476 | finish.consumeTime); |
| 3477 | } else if (std::holds_alternative<InputPublisher::Timeline>(*result)) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3478 | if (shouldReportMetricsForConnection(*connection)) { |
| 3479 | const InputPublisher::Timeline& timeline = |
| 3480 | std::get<InputPublisher::Timeline>(*result); |
| 3481 | mLatencyTracker |
| 3482 | .trackGraphicsLatency(timeline.inputEventId, |
| 3483 | connection->inputChannel->getConnectionToken(), |
| 3484 | std::move(timeline.graphicsTimeline)); |
| 3485 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3486 | } |
| 3487 | gotOne = true; |
| 3488 | } |
| 3489 | if (gotOne) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3490 | runCommandsLockedInterruptable(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3491 | if (status == WOULD_BLOCK) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3492 | return 1; |
| 3493 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3494 | } |
| 3495 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3496 | notify = status != DEAD_OBJECT || !connection->monitor; |
| 3497 | if (notify) { |
| 3498 | ALOGE("channel '%s' ~ Failed to receive finished signal. status=%s(%d)", |
| 3499 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3500 | status); |
| 3501 | } |
| 3502 | } else { |
| 3503 | // Monitor channels are never explicitly unregistered. |
| 3504 | // We do it automatically when the remote endpoint is closed so don't warn about them. |
| 3505 | const bool stillHaveWindowHandle = |
| 3506 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()) != nullptr; |
| 3507 | notify = !connection->monitor && stillHaveWindowHandle; |
| 3508 | if (notify) { |
| 3509 | ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred. events=0x%x", |
| 3510 | connection->getInputChannelName().c_str(), events); |
| 3511 | } |
| 3512 | } |
| 3513 | |
| 3514 | // Remove the channel. |
| 3515 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), notify); |
| 3516 | return 0; // remove the callback |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3517 | } |
| 3518 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3519 | void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3520 | const CancelationOptions& options) { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3521 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 3522 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3523 | } |
| 3524 | } |
| 3525 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3526 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3527 | const CancelationOptions& options) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3528 | synthesizeCancelationEventsForMonitorsLocked(options, mGlobalMonitorsByDisplay); |
| 3529 | synthesizeCancelationEventsForMonitorsLocked(options, mGestureMonitorsByDisplay); |
| 3530 | } |
| 3531 | |
| 3532 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
| 3533 | const CancelationOptions& options, |
| 3534 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { |
| 3535 | for (const auto& it : monitorsByDisplay) { |
| 3536 | const std::vector<Monitor>& monitors = it.second; |
| 3537 | for (const Monitor& monitor : monitors) { |
| 3538 | synthesizeCancelationEventsForInputChannelLocked(monitor.inputChannel, options); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3539 | } |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3540 | } |
| 3541 | } |
| 3542 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3543 | void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked( |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 3544 | const std::shared_ptr<InputChannel>& channel, const CancelationOptions& options) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 3545 | sp<Connection> connection = getConnectionLocked(channel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3546 | if (connection == nullptr) { |
| 3547 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3548 | } |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3549 | |
| 3550 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3551 | } |
| 3552 | |
| 3553 | void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( |
| 3554 | const sp<Connection>& connection, const CancelationOptions& options) { |
| 3555 | if (connection->status == Connection::STATUS_BROKEN) { |
| 3556 | return; |
| 3557 | } |
| 3558 | |
| 3559 | nsecs_t currentTime = now(); |
| 3560 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3561 | std::vector<std::unique_ptr<EventEntry>> cancelationEvents = |
Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 3562 | connection->inputState.synthesizeCancelationEvents(currentTime, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3563 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3564 | if (cancelationEvents.empty()) { |
| 3565 | return; |
| 3566 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3567 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 3568 | ALOGD("channel '%s' ~ Synthesized %zu cancelation events to bring channel back in sync " |
| 3569 | "with reality: %s, mode=%d.", |
| 3570 | connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason, |
| 3571 | options.mode); |
| 3572 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3573 | |
| 3574 | InputTarget target; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3575 | sp<WindowInfoHandle> windowHandle = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3576 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3577 | if (windowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3578 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3579 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3580 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3581 | } |
| 3582 | target.inputChannel = connection->inputChannel; |
| 3583 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 3584 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3585 | for (size_t i = 0; i < cancelationEvents.size(); i++) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3586 | std::unique_ptr<EventEntry> cancelationEventEntry = std::move(cancelationEvents[i]); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3587 | switch (cancelationEventEntry->type) { |
| 3588 | case EventEntry::Type::KEY: { |
| 3589 | logOutboundKeyDetails("cancel - ", |
| 3590 | static_cast<const KeyEntry&>(*cancelationEventEntry)); |
| 3591 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3592 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3593 | case EventEntry::Type::MOTION: { |
| 3594 | logOutboundMotionDetails("cancel - ", |
| 3595 | static_cast<const MotionEntry&>(*cancelationEventEntry)); |
| 3596 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3597 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3598 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3599 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3600 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3601 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3602 | LOG_ALWAYS_FATAL("Canceling %s events is not supported", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3603 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3604 | break; |
| 3605 | } |
| 3606 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3607 | case EventEntry::Type::DEVICE_RESET: |
| 3608 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3609 | 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] | 3610 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3611 | break; |
| 3612 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3613 | } |
| 3614 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3615 | enqueueDispatchEntryLocked(connection, std::move(cancelationEventEntry), target, |
| 3616 | InputTarget::FLAG_DISPATCH_AS_IS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3617 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3618 | |
| 3619 | startDispatchCycleLocked(currentTime, connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3620 | } |
| 3621 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3622 | void InputDispatcher::synthesizePointerDownEventsForConnectionLocked( |
| 3623 | const sp<Connection>& connection) { |
| 3624 | if (connection->status == Connection::STATUS_BROKEN) { |
| 3625 | return; |
| 3626 | } |
| 3627 | |
| 3628 | nsecs_t currentTime = now(); |
| 3629 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3630 | std::vector<std::unique_ptr<EventEntry>> downEvents = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3631 | connection->inputState.synthesizePointerDownEvents(currentTime); |
| 3632 | |
| 3633 | if (downEvents.empty()) { |
| 3634 | return; |
| 3635 | } |
| 3636 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3637 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3638 | ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.", |
| 3639 | connection->getInputChannelName().c_str(), downEvents.size()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3640 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3641 | |
| 3642 | InputTarget target; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3643 | sp<WindowInfoHandle> windowHandle = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3644 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3645 | if (windowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3646 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3647 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3648 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3649 | } |
| 3650 | target.inputChannel = connection->inputChannel; |
| 3651 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 3652 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3653 | for (std::unique_ptr<EventEntry>& downEventEntry : downEvents) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3654 | switch (downEventEntry->type) { |
| 3655 | case EventEntry::Type::MOTION: { |
| 3656 | logOutboundMotionDetails("down - ", |
| 3657 | static_cast<const MotionEntry&>(*downEventEntry)); |
| 3658 | break; |
| 3659 | } |
| 3660 | |
| 3661 | case EventEntry::Type::KEY: |
| 3662 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3663 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3664 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3665 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3666 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3667 | case EventEntry::Type::SENSOR: |
| 3668 | case EventEntry::Type::DRAG: { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3669 | 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] | 3670 | ftl::enum_string(downEventEntry->type).c_str()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3671 | break; |
| 3672 | } |
| 3673 | } |
| 3674 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3675 | enqueueDispatchEntryLocked(connection, std::move(downEventEntry), target, |
| 3676 | InputTarget::FLAG_DISPATCH_AS_IS); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3677 | } |
| 3678 | |
| 3679 | startDispatchCycleLocked(currentTime, connection); |
| 3680 | } |
| 3681 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3682 | std::unique_ptr<MotionEntry> InputDispatcher::splitMotionEvent( |
| 3683 | const MotionEntry& originalMotionEntry, BitSet32 pointerIds) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3684 | ALOG_ASSERT(pointerIds.value != 0); |
| 3685 | |
| 3686 | uint32_t splitPointerIndexMap[MAX_POINTERS]; |
| 3687 | PointerProperties splitPointerProperties[MAX_POINTERS]; |
| 3688 | PointerCoords splitPointerCoords[MAX_POINTERS]; |
| 3689 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3690 | uint32_t originalPointerCount = originalMotionEntry.pointerCount; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3691 | uint32_t splitPointerCount = 0; |
| 3692 | |
| 3693 | for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3694 | originalPointerIndex++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3695 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3696 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3697 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3698 | if (pointerIds.hasBit(pointerId)) { |
| 3699 | splitPointerIndexMap[splitPointerCount] = originalPointerIndex; |
| 3700 | splitPointerProperties[splitPointerCount].copyFrom(pointerProperties); |
| 3701 | splitPointerCoords[splitPointerCount].copyFrom( |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3702 | originalMotionEntry.pointerCoords[originalPointerIndex]); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3703 | splitPointerCount += 1; |
| 3704 | } |
| 3705 | } |
| 3706 | |
| 3707 | if (splitPointerCount != pointerIds.count()) { |
| 3708 | // This is bad. We are missing some of the pointers that we expected to deliver. |
| 3709 | // Most likely this indicates that we received an ACTION_MOVE events that has |
| 3710 | // different pointer ids than we expected based on the previous ACTION_DOWN |
| 3711 | // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers |
| 3712 | // in this way. |
| 3713 | ALOGW("Dropping split motion event because the pointer count is %d but " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3714 | "we expected there to be %d pointers. This probably means we received " |
| 3715 | "a broken sequence of pointer ids from the input device.", |
| 3716 | splitPointerCount, pointerIds.count()); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3717 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3718 | } |
| 3719 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3720 | int32_t action = originalMotionEntry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3721 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3722 | if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN || |
| 3723 | maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3724 | int32_t originalPointerIndex = getMotionEventActionPointerIndex(action); |
| 3725 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3726 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3727 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3728 | if (pointerIds.hasBit(pointerId)) { |
| 3729 | if (pointerIds.count() == 1) { |
| 3730 | // The first/last pointer went down/up. |
| 3731 | action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3732 | ? AMOTION_EVENT_ACTION_DOWN |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3733 | : (originalMotionEntry.flags & AMOTION_EVENT_FLAG_CANCELED) != 0 |
| 3734 | ? AMOTION_EVENT_ACTION_CANCEL |
| 3735 | : AMOTION_EVENT_ACTION_UP; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3736 | } else { |
| 3737 | // A secondary pointer went down/up. |
| 3738 | uint32_t splitPointerIndex = 0; |
| 3739 | while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { |
| 3740 | splitPointerIndex += 1; |
| 3741 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3742 | action = maskedAction | |
| 3743 | (splitPointerIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3744 | } |
| 3745 | } else { |
| 3746 | // An unrelated pointer changed. |
| 3747 | action = AMOTION_EVENT_ACTION_MOVE; |
| 3748 | } |
| 3749 | } |
| 3750 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3751 | int32_t newId = mIdGenerator.nextId(); |
| 3752 | if (ATRACE_ENABLED()) { |
| 3753 | std::string message = StringPrintf("Split MotionEvent(id=0x%" PRIx32 |
| 3754 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3755 | originalMotionEntry.id, newId); |
| 3756 | ATRACE_NAME(message.c_str()); |
| 3757 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3758 | std::unique_ptr<MotionEntry> splitMotionEntry = |
| 3759 | std::make_unique<MotionEntry>(newId, originalMotionEntry.eventTime, |
| 3760 | originalMotionEntry.deviceId, originalMotionEntry.source, |
| 3761 | originalMotionEntry.displayId, |
| 3762 | originalMotionEntry.policyFlags, action, |
| 3763 | originalMotionEntry.actionButton, |
| 3764 | originalMotionEntry.flags, originalMotionEntry.metaState, |
| 3765 | originalMotionEntry.buttonState, |
| 3766 | originalMotionEntry.classification, |
| 3767 | originalMotionEntry.edgeFlags, |
| 3768 | originalMotionEntry.xPrecision, |
| 3769 | originalMotionEntry.yPrecision, |
| 3770 | originalMotionEntry.xCursorPosition, |
| 3771 | originalMotionEntry.yCursorPosition, |
| 3772 | originalMotionEntry.downTime, splitPointerCount, |
| 3773 | splitPointerProperties, splitPointerCoords, 0, 0); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3774 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3775 | if (originalMotionEntry.injectionState) { |
| 3776 | splitMotionEntry->injectionState = originalMotionEntry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3777 | splitMotionEntry->injectionState->refCount += 1; |
| 3778 | } |
| 3779 | |
| 3780 | return splitMotionEntry; |
| 3781 | } |
| 3782 | |
| 3783 | void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3784 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 3785 | ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args->eventTime); |
| 3786 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3787 | |
| 3788 | bool needWake; |
| 3789 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3790 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3791 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3792 | std::unique_ptr<ConfigurationChangedEntry> newEntry = |
| 3793 | std::make_unique<ConfigurationChangedEntry>(args->id, args->eventTime); |
| 3794 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3795 | } // release lock |
| 3796 | |
| 3797 | if (needWake) { |
| 3798 | mLooper->wake(); |
| 3799 | } |
| 3800 | } |
| 3801 | |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3802 | /** |
| 3803 | * If one of the meta shortcuts is detected, process them here: |
| 3804 | * Meta + Backspace -> generate BACK |
| 3805 | * Meta + Enter -> generate HOME |
| 3806 | * This will potentially overwrite keyCode and metaState. |
| 3807 | */ |
| 3808 | void InputDispatcher::accelerateMetaShortcuts(const int32_t deviceId, const int32_t action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3809 | int32_t& keyCode, int32_t& metaState) { |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3810 | if (metaState & AMETA_META_ON && action == AKEY_EVENT_ACTION_DOWN) { |
| 3811 | int32_t newKeyCode = AKEYCODE_UNKNOWN; |
| 3812 | if (keyCode == AKEYCODE_DEL) { |
| 3813 | newKeyCode = AKEYCODE_BACK; |
| 3814 | } else if (keyCode == AKEYCODE_ENTER) { |
| 3815 | newKeyCode = AKEYCODE_HOME; |
| 3816 | } |
| 3817 | if (newKeyCode != AKEYCODE_UNKNOWN) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3818 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3819 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3820 | mReplacedKeys[replacement] = newKeyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3821 | keyCode = newKeyCode; |
| 3822 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3823 | } |
| 3824 | } else if (action == AKEY_EVENT_ACTION_UP) { |
| 3825 | // In order to maintain a consistent stream of up and down events, check to see if the key |
| 3826 | // going up is one we've replaced in a down event and haven't yet replaced in an up event, |
| 3827 | // 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] | 3828 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3829 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3830 | auto replacementIt = mReplacedKeys.find(replacement); |
| 3831 | if (replacementIt != mReplacedKeys.end()) { |
| 3832 | keyCode = replacementIt->second; |
| 3833 | mReplacedKeys.erase(replacementIt); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3834 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3835 | } |
| 3836 | } |
| 3837 | } |
| 3838 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3839 | void InputDispatcher::notifyKey(const NotifyKeyArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3840 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 3841 | ALOGD("notifyKey - eventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
| 3842 | "policyFlags=0x%x, action=0x%x, " |
| 3843 | "flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, downTime=%" PRId64, |
| 3844 | args->eventTime, args->deviceId, args->source, args->displayId, args->policyFlags, |
| 3845 | args->action, args->flags, args->keyCode, args->scanCode, args->metaState, |
| 3846 | args->downTime); |
| 3847 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3848 | if (!validateKeyEvent(args->action)) { |
| 3849 | return; |
| 3850 | } |
| 3851 | |
| 3852 | uint32_t policyFlags = args->policyFlags; |
| 3853 | int32_t flags = args->flags; |
| 3854 | int32_t metaState = args->metaState; |
Siarhei Vishniakou | 622bd32 | 2018-10-29 18:02:27 -0700 | [diff] [blame] | 3855 | // InputDispatcher tracks and generates key repeats on behalf of |
| 3856 | // whatever notifies it, so repeatCount should always be set to 0 |
| 3857 | constexpr int32_t repeatCount = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3858 | if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { |
| 3859 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 3860 | flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
| 3861 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3862 | if (policyFlags & POLICY_FLAG_FUNCTION) { |
| 3863 | metaState |= AMETA_FUNCTION_ON; |
| 3864 | } |
| 3865 | |
| 3866 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 3867 | |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3868 | int32_t keyCode = args->keyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3869 | accelerateMetaShortcuts(args->deviceId, args->action, keyCode, metaState); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3870 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3871 | KeyEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3872 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3873 | args->action, flags, keyCode, args->scanCode, metaState, repeatCount, |
| 3874 | args->downTime, args->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3875 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3876 | android::base::Timer t; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3877 | mPolicy->interceptKeyBeforeQueueing(&event, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3878 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3879 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3880 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3881 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3882 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3883 | bool needWake; |
| 3884 | { // acquire lock |
| 3885 | mLock.lock(); |
| 3886 | |
| 3887 | if (shouldSendKeyToInputFilterLocked(args)) { |
| 3888 | mLock.unlock(); |
| 3889 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 3890 | policyFlags |= POLICY_FLAG_FILTERED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3891 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 3892 | return; // event was consumed by the filter |
| 3893 | } |
| 3894 | |
| 3895 | mLock.lock(); |
| 3896 | } |
| 3897 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3898 | std::unique_ptr<KeyEntry> newEntry = |
| 3899 | std::make_unique<KeyEntry>(args->id, args->eventTime, args->deviceId, args->source, |
| 3900 | args->displayId, policyFlags, args->action, flags, |
| 3901 | keyCode, args->scanCode, metaState, repeatCount, |
| 3902 | args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3903 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3904 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3905 | mLock.unlock(); |
| 3906 | } // release lock |
| 3907 | |
| 3908 | if (needWake) { |
| 3909 | mLooper->wake(); |
| 3910 | } |
| 3911 | } |
| 3912 | |
| 3913 | bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) { |
| 3914 | return mInputFilterEnabled; |
| 3915 | } |
| 3916 | |
| 3917 | void InputDispatcher::notifyMotion(const NotifyMotionArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3918 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 3919 | ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 3920 | "displayId=%" PRId32 ", policyFlags=0x%x, " |
| 3921 | "action=0x%x, actionButton=0x%x, flags=0x%x, metaState=0x%x, buttonState=0x%x, " |
| 3922 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, xCursorPosition=%f, " |
| 3923 | "yCursorPosition=%f, downTime=%" PRId64, |
| 3924 | args->id, args->eventTime, args->deviceId, args->source, args->displayId, |
| 3925 | args->policyFlags, args->action, args->actionButton, args->flags, args->metaState, |
| 3926 | args->buttonState, args->edgeFlags, args->xPrecision, args->yPrecision, |
| 3927 | args->xCursorPosition, args->yCursorPosition, args->downTime); |
| 3928 | for (uint32_t i = 0; i < args->pointerCount; i++) { |
| 3929 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
| 3930 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 3931 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 3932 | "orientation=%f", |
| 3933 | i, args->pointerProperties[i].id, args->pointerProperties[i].toolType, |
| 3934 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 3935 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 3936 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 3937 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 3938 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 3939 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 3940 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 3941 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 3942 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| 3943 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3944 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3945 | if (!validateMotionEvent(args->action, args->actionButton, args->pointerCount, |
| 3946 | args->pointerProperties)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3947 | return; |
| 3948 | } |
| 3949 | |
| 3950 | uint32_t policyFlags = args->policyFlags; |
| 3951 | policyFlags |= POLICY_FLAG_TRUSTED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3952 | |
| 3953 | android::base::Timer t; |
Charles Chen | 3611f1f | 2019-01-29 17:26:18 +0800 | [diff] [blame] | 3954 | mPolicy->interceptMotionBeforeQueueing(args->displayId, args->eventTime, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3955 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3956 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3957 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3958 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3959 | |
| 3960 | bool needWake; |
| 3961 | { // acquire lock |
| 3962 | mLock.lock(); |
| 3963 | |
| 3964 | if (shouldSendMotionToInputFilterLocked(args)) { |
| 3965 | mLock.unlock(); |
| 3966 | |
| 3967 | MotionEvent event; |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 3968 | ui::Transform identityTransform; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3969 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
| 3970 | args->action, args->actionButton, args->flags, args->edgeFlags, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 3971 | args->metaState, args->buttonState, args->classification, |
| 3972 | identityTransform, args->xPrecision, args->yPrecision, |
| 3973 | args->xCursorPosition, args->yCursorPosition, identityTransform, |
| 3974 | args->downTime, args->eventTime, args->pointerCount, |
| 3975 | args->pointerProperties, args->pointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3976 | |
| 3977 | policyFlags |= POLICY_FLAG_FILTERED; |
| 3978 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 3979 | return; // event was consumed by the filter |
| 3980 | } |
| 3981 | |
| 3982 | mLock.lock(); |
| 3983 | } |
| 3984 | |
| 3985 | // Just enqueue a new motion event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3986 | std::unique_ptr<MotionEntry> newEntry = |
| 3987 | std::make_unique<MotionEntry>(args->id, args->eventTime, args->deviceId, |
| 3988 | args->source, args->displayId, policyFlags, |
| 3989 | args->action, args->actionButton, args->flags, |
| 3990 | args->metaState, args->buttonState, |
| 3991 | args->classification, args->edgeFlags, |
| 3992 | args->xPrecision, args->yPrecision, |
| 3993 | args->xCursorPosition, args->yCursorPosition, |
| 3994 | args->downTime, args->pointerCount, |
| 3995 | args->pointerProperties, args->pointerCoords, 0, 0); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3996 | |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 3997 | if (args->id != android::os::IInputConstants::INVALID_INPUT_EVENT_ID && |
| 3998 | IdGenerator::getSource(args->id) == IdGenerator::Source::INPUT_READER && |
| 3999 | !mInputFilterEnabled) { |
| 4000 | const bool isDown = args->action == AMOTION_EVENT_ACTION_DOWN; |
| 4001 | mLatencyTracker.trackListener(args->id, isDown, args->eventTime, args->readTime); |
| 4002 | } |
| 4003 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4004 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4005 | mLock.unlock(); |
| 4006 | } // release lock |
| 4007 | |
| 4008 | if (needWake) { |
| 4009 | mLooper->wake(); |
| 4010 | } |
| 4011 | } |
| 4012 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4013 | void InputDispatcher::notifySensor(const NotifySensorArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4014 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4015 | ALOGD("notifySensor - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 4016 | " sensorType=%s", |
| 4017 | args->id, args->eventTime, args->deviceId, args->source, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 4018 | ftl::enum_string(args->sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4019 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4020 | |
| 4021 | bool needWake; |
| 4022 | { // acquire lock |
| 4023 | mLock.lock(); |
| 4024 | |
| 4025 | // Just enqueue a new sensor event. |
| 4026 | std::unique_ptr<SensorEntry> newEntry = |
| 4027 | std::make_unique<SensorEntry>(args->id, args->eventTime, args->deviceId, |
| 4028 | args->source, 0 /* policyFlags*/, args->hwTimestamp, |
| 4029 | args->sensorType, args->accuracy, |
| 4030 | args->accuracyChanged, args->values); |
| 4031 | |
| 4032 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
| 4033 | mLock.unlock(); |
| 4034 | } // release lock |
| 4035 | |
| 4036 | if (needWake) { |
| 4037 | mLooper->wake(); |
| 4038 | } |
| 4039 | } |
| 4040 | |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 4041 | void InputDispatcher::notifyVibratorState(const NotifyVibratorStateArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4042 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4043 | ALOGD("notifyVibratorState - eventTime=%" PRId64 ", device=%d, isOn=%d", args->eventTime, |
| 4044 | args->deviceId, args->isOn); |
| 4045 | } |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 4046 | mPolicy->notifyVibratorState(args->deviceId, args->isOn); |
| 4047 | } |
| 4048 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4049 | bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4050 | return mInputFilterEnabled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4051 | } |
| 4052 | |
| 4053 | void InputDispatcher::notifySwitch(const NotifySwitchArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4054 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4055 | ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, " |
| 4056 | "switchMask=0x%08x", |
| 4057 | args->eventTime, args->policyFlags, args->switchValues, args->switchMask); |
| 4058 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4059 | |
| 4060 | uint32_t policyFlags = args->policyFlags; |
| 4061 | policyFlags |= POLICY_FLAG_TRUSTED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4062 | mPolicy->notifySwitch(args->eventTime, args->switchValues, args->switchMask, policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4063 | } |
| 4064 | |
| 4065 | void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4066 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4067 | ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args->eventTime, |
| 4068 | args->deviceId); |
| 4069 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4070 | |
| 4071 | bool needWake; |
| 4072 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4073 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4074 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4075 | std::unique_ptr<DeviceResetEntry> newEntry = |
| 4076 | std::make_unique<DeviceResetEntry>(args->id, args->eventTime, args->deviceId); |
| 4077 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4078 | } // release lock |
| 4079 | |
| 4080 | if (needWake) { |
| 4081 | mLooper->wake(); |
| 4082 | } |
| 4083 | } |
| 4084 | |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4085 | void InputDispatcher::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4086 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4087 | ALOGD("notifyPointerCaptureChanged - eventTime=%" PRId64 ", enabled=%s", args->eventTime, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4088 | args->request.enable ? "true" : "false"); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4089 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4090 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4091 | bool needWake; |
| 4092 | { // acquire lock |
| 4093 | std::scoped_lock _l(mLock); |
| 4094 | auto entry = std::make_unique<PointerCaptureChangedEntry>(args->id, args->eventTime, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4095 | args->request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4096 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 4097 | } // release lock |
| 4098 | |
| 4099 | if (needWake) { |
| 4100 | mLooper->wake(); |
| 4101 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4102 | } |
| 4103 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4104 | InputEventInjectionResult InputDispatcher::injectInputEvent( |
| 4105 | const InputEvent* event, int32_t injectorPid, int32_t injectorUid, |
| 4106 | InputEventInjectionSync syncMode, std::chrono::milliseconds timeout, uint32_t policyFlags) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4107 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4108 | ALOGD("injectInputEvent - eventType=%d, injectorPid=%d, injectorUid=%d, " |
| 4109 | "syncMode=%d, timeout=%lld, policyFlags=0x%08x", |
| 4110 | event->getType(), injectorPid, injectorUid, syncMode, timeout.count(), policyFlags); |
| 4111 | } |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 4112 | 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] | 4113 | |
| 4114 | policyFlags |= POLICY_FLAG_INJECTED; |
| 4115 | if (hasInjectionPermission(injectorPid, injectorUid)) { |
| 4116 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 4117 | } |
| 4118 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4119 | // 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] | 4120 | // that have gone through the InputFilter. If the event passed through the InputFilter, assign |
| 4121 | // the provided device id. If the InputFilter is accessibility, and it modifies or synthesizes |
| 4122 | // the injected event, it is responsible for setting POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY. |
| 4123 | // For those events, we will set FLAG_IS_ACCESSIBILITY_EVENT to allow apps to distinguish them |
| 4124 | // from events that originate from actual hardware. |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4125 | int32_t resolvedDeviceId = VIRTUAL_KEYBOARD_ID; |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4126 | if (policyFlags & POLICY_FLAG_FILTERED) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4127 | resolvedDeviceId = event->getDeviceId(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4128 | } |
| 4129 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4130 | std::queue<std::unique_ptr<EventEntry>> injectedEntries; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4131 | switch (event->getType()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4132 | case AINPUT_EVENT_TYPE_KEY: { |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4133 | const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event); |
| 4134 | int32_t action = incomingKey.getAction(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4135 | if (!validateKeyEvent(action)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4136 | return InputEventInjectionResult::FAILED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4137 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4138 | |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4139 | int32_t flags = incomingKey.getFlags(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4140 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4141 | flags |= AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4142 | } |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4143 | int32_t keyCode = incomingKey.getKeyCode(); |
| 4144 | int32_t metaState = incomingKey.getMetaState(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4145 | accelerateMetaShortcuts(resolvedDeviceId, action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4146 | /*byref*/ keyCode, /*byref*/ metaState); |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4147 | KeyEvent keyEvent; |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4148 | keyEvent.initialize(incomingKey.getId(), resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4149 | incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode, |
| 4150 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), |
| 4151 | incomingKey.getDownTime(), incomingKey.getEventTime()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4152 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4153 | if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) { |
| 4154 | policyFlags |= POLICY_FLAG_VIRTUAL; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4155 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4156 | |
| 4157 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 4158 | android::base::Timer t; |
| 4159 | mPolicy->interceptKeyBeforeQueueing(&keyEvent, /*byref*/ policyFlags); |
| 4160 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4161 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
| 4162 | std::to_string(t.duration().count()).c_str()); |
| 4163 | } |
| 4164 | } |
| 4165 | |
| 4166 | mLock.lock(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4167 | std::unique_ptr<KeyEntry> injectedEntry = |
| 4168 | std::make_unique<KeyEntry>(incomingKey.getId(), incomingKey.getEventTime(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4169 | resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4170 | incomingKey.getDisplayId(), policyFlags, action, |
| 4171 | flags, keyCode, incomingKey.getScanCode(), metaState, |
| 4172 | incomingKey.getRepeatCount(), |
| 4173 | incomingKey.getDownTime()); |
| 4174 | injectedEntries.push(std::move(injectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4175 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4176 | } |
| 4177 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4178 | case AINPUT_EVENT_TYPE_MOTION: { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4179 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
| 4180 | int32_t action = motionEvent.getAction(); |
| 4181 | size_t pointerCount = motionEvent.getPointerCount(); |
| 4182 | const PointerProperties* pointerProperties = motionEvent.getPointerProperties(); |
| 4183 | int32_t actionButton = motionEvent.getActionButton(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4184 | int32_t flags = motionEvent.getFlags(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4185 | int32_t displayId = motionEvent.getDisplayId(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4186 | if (!validateMotionEvent(action, actionButton, pointerCount, pointerProperties)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4187 | return InputEventInjectionResult::FAILED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4188 | } |
| 4189 | |
| 4190 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4191 | nsecs_t eventTime = motionEvent.getEventTime(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4192 | android::base::Timer t; |
| 4193 | mPolicy->interceptMotionBeforeQueueing(displayId, eventTime, /*byref*/ policyFlags); |
| 4194 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4195 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
| 4196 | std::to_string(t.duration().count()).c_str()); |
| 4197 | } |
| 4198 | } |
| 4199 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4200 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4201 | flags |= AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4202 | } |
| 4203 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4204 | mLock.lock(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4205 | const nsecs_t* sampleEventTimes = motionEvent.getSampleEventTimes(); |
| 4206 | const PointerCoords* samplePointerCoords = motionEvent.getSamplePointerCoords(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4207 | std::unique_ptr<MotionEntry> injectedEntry = |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4208 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, |
| 4209 | resolvedDeviceId, motionEvent.getSource(), |
| 4210 | motionEvent.getDisplayId(), policyFlags, action, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4211 | actionButton, flags, motionEvent.getMetaState(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4212 | motionEvent.getButtonState(), |
| 4213 | motionEvent.getClassification(), |
| 4214 | motionEvent.getEdgeFlags(), |
| 4215 | motionEvent.getXPrecision(), |
| 4216 | motionEvent.getYPrecision(), |
| 4217 | motionEvent.getRawXCursorPosition(), |
| 4218 | motionEvent.getRawYCursorPosition(), |
| 4219 | motionEvent.getDownTime(), uint32_t(pointerCount), |
| 4220 | pointerProperties, samplePointerCoords, |
| 4221 | motionEvent.getXOffset(), |
| 4222 | motionEvent.getYOffset()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4223 | injectedEntries.push(std::move(injectedEntry)); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4224 | for (size_t i = motionEvent.getHistorySize(); i > 0; i--) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4225 | sampleEventTimes += 1; |
| 4226 | samplePointerCoords += pointerCount; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4227 | std::unique_ptr<MotionEntry> nextInjectedEntry = |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4228 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, |
| 4229 | resolvedDeviceId, motionEvent.getSource(), |
| 4230 | motionEvent.getDisplayId(), policyFlags, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4231 | action, actionButton, flags, |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4232 | motionEvent.getMetaState(), |
| 4233 | motionEvent.getButtonState(), |
| 4234 | motionEvent.getClassification(), |
| 4235 | motionEvent.getEdgeFlags(), |
| 4236 | motionEvent.getXPrecision(), |
| 4237 | motionEvent.getYPrecision(), |
| 4238 | motionEvent.getRawXCursorPosition(), |
| 4239 | motionEvent.getRawYCursorPosition(), |
| 4240 | motionEvent.getDownTime(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4241 | uint32_t(pointerCount), pointerProperties, |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4242 | samplePointerCoords, motionEvent.getXOffset(), |
| 4243 | motionEvent.getYOffset()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4244 | injectedEntries.push(std::move(nextInjectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4245 | } |
| 4246 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4247 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4248 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4249 | default: |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 4250 | ALOGW("Cannot inject %s events", inputEventTypeToString(event->getType())); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4251 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4252 | } |
| 4253 | |
| 4254 | InjectionState* injectionState = new InjectionState(injectorPid, injectorUid); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4255 | if (syncMode == InputEventInjectionSync::NONE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4256 | injectionState->injectionIsAsync = true; |
| 4257 | } |
| 4258 | |
| 4259 | injectionState->refCount += 1; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4260 | injectedEntries.back()->injectionState = injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4261 | |
| 4262 | bool needWake = false; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4263 | while (!injectedEntries.empty()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4264 | needWake |= enqueueInboundEventLocked(std::move(injectedEntries.front())); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4265 | injectedEntries.pop(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4266 | } |
| 4267 | |
| 4268 | mLock.unlock(); |
| 4269 | |
| 4270 | if (needWake) { |
| 4271 | mLooper->wake(); |
| 4272 | } |
| 4273 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4274 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4275 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4276 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4277 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4278 | if (syncMode == InputEventInjectionSync::NONE) { |
| 4279 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4280 | } else { |
| 4281 | for (;;) { |
| 4282 | injectionResult = injectionState->injectionResult; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4283 | if (injectionResult != InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4284 | break; |
| 4285 | } |
| 4286 | |
| 4287 | nsecs_t remainingTimeout = endTime - now(); |
| 4288 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4289 | if (DEBUG_INJECTION) { |
| 4290 | ALOGD("injectInputEvent - Timed out waiting for injection result " |
| 4291 | "to become available."); |
| 4292 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4293 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4294 | break; |
| 4295 | } |
| 4296 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4297 | mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4298 | } |
| 4299 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4300 | if (injectionResult == InputEventInjectionResult::SUCCEEDED && |
| 4301 | syncMode == InputEventInjectionSync::WAIT_FOR_FINISHED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4302 | while (injectionState->pendingForegroundDispatches != 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4303 | if (DEBUG_INJECTION) { |
| 4304 | ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", |
| 4305 | injectionState->pendingForegroundDispatches); |
| 4306 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4307 | nsecs_t remainingTimeout = endTime - now(); |
| 4308 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4309 | if (DEBUG_INJECTION) { |
| 4310 | ALOGD("injectInputEvent - Timed out waiting for pending foreground " |
| 4311 | "dispatches to finish."); |
| 4312 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4313 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4314 | break; |
| 4315 | } |
| 4316 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4317 | mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4318 | } |
| 4319 | } |
| 4320 | } |
| 4321 | |
| 4322 | injectionState->release(); |
| 4323 | } // release lock |
| 4324 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4325 | if (DEBUG_INJECTION) { |
| 4326 | ALOGD("injectInputEvent - Finished with result %d. injectorPid=%d, injectorUid=%d", |
| 4327 | injectionResult, injectorPid, injectorUid); |
| 4328 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4329 | |
| 4330 | return injectionResult; |
| 4331 | } |
| 4332 | |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4333 | std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4334 | std::array<uint8_t, 32> calculatedHmac; |
| 4335 | std::unique_ptr<VerifiedInputEvent> result; |
| 4336 | switch (event.getType()) { |
| 4337 | case AINPUT_EVENT_TYPE_KEY: { |
| 4338 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event); |
| 4339 | VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent); |
| 4340 | result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4341 | calculatedHmac = sign(verifiedKeyEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4342 | break; |
| 4343 | } |
| 4344 | case AINPUT_EVENT_TYPE_MOTION: { |
| 4345 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event); |
| 4346 | VerifiedMotionEvent verifiedMotionEvent = |
| 4347 | verifiedMotionEventFromMotionEvent(motionEvent); |
| 4348 | result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4349 | calculatedHmac = sign(verifiedMotionEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4350 | break; |
| 4351 | } |
| 4352 | default: { |
| 4353 | ALOGE("Cannot verify events of type %" PRId32, event.getType()); |
| 4354 | return nullptr; |
| 4355 | } |
| 4356 | } |
| 4357 | if (calculatedHmac == INVALID_HMAC) { |
| 4358 | return nullptr; |
| 4359 | } |
| 4360 | if (calculatedHmac != event.getHmac()) { |
| 4361 | return nullptr; |
| 4362 | } |
| 4363 | return result; |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4364 | } |
| 4365 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4366 | bool InputDispatcher::hasInjectionPermission(int32_t injectorPid, int32_t injectorUid) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4367 | return injectorUid == 0 || |
| 4368 | mPolicy->checkInjectEventsPermissionNonReentrant(injectorPid, injectorUid); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4369 | } |
| 4370 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4371 | void InputDispatcher::setInjectionResult(EventEntry& entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4372 | InputEventInjectionResult injectionResult) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4373 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4374 | if (injectionState) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4375 | if (DEBUG_INJECTION) { |
| 4376 | ALOGD("Setting input event injection result to %d. " |
| 4377 | "injectorPid=%d, injectorUid=%d", |
| 4378 | injectionResult, injectionState->injectorPid, injectionState->injectorUid); |
| 4379 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4380 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4381 | if (injectionState->injectionIsAsync && !(entry.policyFlags & POLICY_FLAG_FILTERED)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4382 | // Log the outcome since the injector did not wait for the injection result. |
| 4383 | switch (injectionResult) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4384 | case InputEventInjectionResult::SUCCEEDED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4385 | ALOGV("Asynchronous input event injection succeeded."); |
| 4386 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4387 | case InputEventInjectionResult::FAILED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4388 | ALOGW("Asynchronous input event injection failed."); |
| 4389 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4390 | case InputEventInjectionResult::PERMISSION_DENIED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4391 | ALOGW("Asynchronous input event injection permission denied."); |
| 4392 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4393 | case InputEventInjectionResult::TIMED_OUT: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4394 | ALOGW("Asynchronous input event injection timed out."); |
| 4395 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4396 | case InputEventInjectionResult::PENDING: |
| 4397 | ALOGE("Setting result to 'PENDING' for asynchronous injection"); |
| 4398 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4399 | } |
| 4400 | } |
| 4401 | |
| 4402 | injectionState->injectionResult = injectionResult; |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4403 | mInjectionResultAvailable.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4404 | } |
| 4405 | } |
| 4406 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4407 | void InputDispatcher::incrementPendingForegroundDispatches(EventEntry& entry) { |
| 4408 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4409 | if (injectionState) { |
| 4410 | injectionState->pendingForegroundDispatches += 1; |
| 4411 | } |
| 4412 | } |
| 4413 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4414 | void InputDispatcher::decrementPendingForegroundDispatches(EventEntry& entry) { |
| 4415 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4416 | if (injectionState) { |
| 4417 | injectionState->pendingForegroundDispatches -= 1; |
| 4418 | |
| 4419 | if (injectionState->pendingForegroundDispatches == 0) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4420 | mInjectionSyncFinished.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4421 | } |
| 4422 | } |
| 4423 | } |
| 4424 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4425 | const std::vector<sp<WindowInfoHandle>>& InputDispatcher::getWindowHandlesLocked( |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4426 | int32_t displayId) const { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4427 | static const std::vector<sp<WindowInfoHandle>> EMPTY_WINDOW_HANDLES; |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4428 | auto it = mWindowHandlesByDisplay.find(displayId); |
| 4429 | return it != mWindowHandlesByDisplay.end() ? it->second : EMPTY_WINDOW_HANDLES; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4430 | } |
| 4431 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4432 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4433 | const sp<IBinder>& windowHandleToken) const { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4434 | if (windowHandleToken == nullptr) { |
| 4435 | return nullptr; |
| 4436 | } |
| 4437 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4438 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4439 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 4440 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4441 | if (windowHandle->getToken() == windowHandleToken) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4442 | return windowHandle; |
| 4443 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4444 | } |
| 4445 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4446 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4447 | } |
| 4448 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4449 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked(const sp<IBinder>& windowHandleToken, |
| 4450 | int displayId) const { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4451 | if (windowHandleToken == nullptr) { |
| 4452 | return nullptr; |
| 4453 | } |
| 4454 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4455 | for (const sp<WindowInfoHandle>& windowHandle : getWindowHandlesLocked(displayId)) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4456 | if (windowHandle->getToken() == windowHandleToken) { |
| 4457 | return windowHandle; |
| 4458 | } |
| 4459 | } |
| 4460 | return nullptr; |
| 4461 | } |
| 4462 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4463 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
| 4464 | const sp<WindowInfoHandle>& windowHandle) const { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4465 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4466 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 4467 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4468 | if (handle->getId() == windowHandle->getId() && |
| 4469 | handle->getToken() == windowHandle->getToken()) { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4470 | if (windowHandle->getInfo()->displayId != it.first) { |
| 4471 | ALOGE("Found window %s in display %" PRId32 |
| 4472 | ", but it should belong to display %" PRId32, |
| 4473 | windowHandle->getName().c_str(), it.first, |
| 4474 | windowHandle->getInfo()->displayId); |
| 4475 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4476 | return handle; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4477 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4478 | } |
| 4479 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4480 | return nullptr; |
| 4481 | } |
| 4482 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4483 | sp<WindowInfoHandle> InputDispatcher::getFocusedWindowHandleLocked(int displayId) const { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4484 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(displayId); |
| 4485 | return getWindowHandleLocked(focusedToken, displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4486 | } |
| 4487 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4488 | bool InputDispatcher::hasResponsiveConnectionLocked(WindowInfoHandle& windowHandle) const { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4489 | sp<Connection> connection = getConnectionLocked(windowHandle.getToken()); |
| 4490 | const bool noInputChannel = |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4491 | windowHandle.getInfo()->inputFeatures.test(WindowInfo::Feature::NO_INPUT_CHANNEL); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4492 | if (connection != nullptr && noInputChannel) { |
| 4493 | ALOGW("%s has feature NO_INPUT_CHANNEL, but it matched to connection %s", |
| 4494 | windowHandle.getName().c_str(), connection->inputChannel->getName().c_str()); |
| 4495 | return false; |
| 4496 | } |
| 4497 | |
| 4498 | if (connection == nullptr) { |
| 4499 | if (!noInputChannel) { |
| 4500 | ALOGI("Could not find connection for %s", windowHandle.getName().c_str()); |
| 4501 | } |
| 4502 | return false; |
| 4503 | } |
| 4504 | if (!connection->responsive) { |
| 4505 | ALOGW("Window %s is not responsive", windowHandle.getName().c_str()); |
| 4506 | return false; |
| 4507 | } |
| 4508 | return true; |
| 4509 | } |
| 4510 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4511 | std::shared_ptr<InputChannel> InputDispatcher::getInputChannelLocked( |
| 4512 | const sp<IBinder>& token) const { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4513 | auto connectionIt = mConnectionsByToken.find(token); |
| 4514 | if (connectionIt == mConnectionsByToken.end()) { |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4515 | return nullptr; |
| 4516 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4517 | return connectionIt->second->inputChannel; |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4518 | } |
| 4519 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4520 | void InputDispatcher::updateWindowHandlesForDisplayLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4521 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
| 4522 | if (windowInfoHandles.empty()) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4523 | // Remove all handles on a display if there are no windows left. |
| 4524 | mWindowHandlesByDisplay.erase(displayId); |
| 4525 | return; |
| 4526 | } |
| 4527 | |
| 4528 | // Since we compare the pointer of input window handles across window updates, we need |
| 4529 | // 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] | 4530 | const std::vector<sp<WindowInfoHandle>>& oldHandles = getWindowHandlesLocked(displayId); |
| 4531 | std::unordered_map<int32_t /*id*/, sp<WindowInfoHandle>> oldHandlesById; |
| 4532 | for (const sp<WindowInfoHandle>& handle : oldHandles) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4533 | oldHandlesById[handle->getId()] = handle; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4534 | } |
| 4535 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4536 | std::vector<sp<WindowInfoHandle>> newHandles; |
| 4537 | for (const sp<WindowInfoHandle>& handle : windowInfoHandles) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4538 | const WindowInfo* info = handle->getInfo(); |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 4539 | if (getInputChannelLocked(handle->getToken()) == nullptr) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4540 | const bool noInputChannel = |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4541 | info->inputFeatures.test(WindowInfo::Feature::NO_INPUT_CHANNEL); |
| 4542 | const bool canReceiveInput = !info->flags.test(WindowInfo::Flag::NOT_TOUCHABLE) || |
| 4543 | !info->flags.test(WindowInfo::Flag::NOT_FOCUSABLE); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4544 | if (canReceiveInput && !noInputChannel) { |
John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 4545 | ALOGV("Window handle %s has no registered input channel", |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4546 | handle->getName().c_str()); |
Robert Carr | 2984b7a | 2020-04-13 17:06:45 -0700 | [diff] [blame] | 4547 | continue; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4548 | } |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4549 | } |
| 4550 | |
| 4551 | if (info->displayId != displayId) { |
| 4552 | ALOGE("Window %s updated by wrong display %d, should belong to display %d", |
| 4553 | handle->getName().c_str(), displayId, info->displayId); |
| 4554 | continue; |
| 4555 | } |
| 4556 | |
Robert Carr | edd1360 | 2020-04-13 17:24:34 -0700 | [diff] [blame] | 4557 | if ((oldHandlesById.find(handle->getId()) != oldHandlesById.end()) && |
| 4558 | (oldHandlesById.at(handle->getId())->getToken() == handle->getToken())) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4559 | const sp<WindowInfoHandle>& oldHandle = oldHandlesById.at(handle->getId()); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4560 | oldHandle->updateFrom(handle); |
| 4561 | newHandles.push_back(oldHandle); |
| 4562 | } else { |
| 4563 | newHandles.push_back(handle); |
| 4564 | } |
| 4565 | } |
| 4566 | |
| 4567 | // Insert or replace |
| 4568 | mWindowHandlesByDisplay[displayId] = newHandles; |
| 4569 | } |
| 4570 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4571 | void InputDispatcher::setInputWindows( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4572 | const std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>>& handlesPerDisplay) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 4573 | // TODO(b/198444055): Remove setInputWindows from InputDispatcher. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4574 | { // acquire lock |
| 4575 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 4576 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 4577 | setInputWindowsLocked(handles, displayId); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4578 | } |
| 4579 | } |
| 4580 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4581 | mLooper->wake(); |
| 4582 | } |
| 4583 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4584 | /** |
| 4585 | * Called from InputManagerService, update window handle list by displayId that can receive input. |
| 4586 | * A window handle contains information about InputChannel, Touch Region, Types, Focused,... |
| 4587 | * If set an empty list, remove all handles from the specific display. |
| 4588 | * For focused handle, check if need to change and send a cancel event to previous one. |
| 4589 | * For removed handle, check if need to send a cancel event if already in touch. |
| 4590 | */ |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4591 | void InputDispatcher::setInputWindowsLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4592 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4593 | if (DEBUG_FOCUS) { |
| 4594 | std::string windowList; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4595 | for (const sp<WindowInfoHandle>& iwh : windowInfoHandles) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4596 | windowList += iwh->getName() + " "; |
| 4597 | } |
| 4598 | ALOGD("setInputWindows displayId=%" PRId32 " %s", displayId, windowList.c_str()); |
| 4599 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4600 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4601 | // Ensure all tokens are null if the window has feature NO_INPUT_CHANNEL |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4602 | for (const sp<WindowInfoHandle>& window : windowInfoHandles) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4603 | const bool noInputWindow = |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4604 | window->getInfo()->inputFeatures.test(WindowInfo::Feature::NO_INPUT_CHANNEL); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4605 | if (noInputWindow && window->getToken() != nullptr) { |
| 4606 | ALOGE("%s has feature NO_INPUT_WINDOW, but a non-null token. Clearing", |
| 4607 | window->getName().c_str()); |
| 4608 | window->releaseChannel(); |
| 4609 | } |
| 4610 | } |
| 4611 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4612 | // Copy old handles for release if they are no longer present. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4613 | const std::vector<sp<WindowInfoHandle>> oldWindowHandles = getWindowHandlesLocked(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4614 | |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4615 | // Save the old windows' orientation by ID before it gets updated. |
| 4616 | std::unordered_map<int32_t, uint32_t> oldWindowOrientations; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4617 | for (const sp<WindowInfoHandle>& handle : oldWindowHandles) { |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4618 | oldWindowOrientations.emplace(handle->getId(), |
| 4619 | handle->getInfo()->transform.getOrientation()); |
| 4620 | } |
| 4621 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4622 | updateWindowHandlesForDisplayLocked(windowInfoHandles, displayId); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4623 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4624 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4625 | if (mLastHoverWindowHandle && |
| 4626 | std::find(windowHandles.begin(), windowHandles.end(), mLastHoverWindowHandle) == |
| 4627 | windowHandles.end()) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4628 | mLastHoverWindowHandle = nullptr; |
| 4629 | } |
| 4630 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4631 | std::optional<FocusResolver::FocusChanges> changes = |
| 4632 | mFocusResolver.setInputWindows(displayId, windowHandles); |
| 4633 | if (changes) { |
| 4634 | onFocusChangedLocked(*changes); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4635 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4636 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4637 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 4638 | mTouchStatesByDisplay.find(displayId); |
| 4639 | if (stateIt != mTouchStatesByDisplay.end()) { |
| 4640 | TouchState& state = stateIt->second; |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4641 | for (size_t i = 0; i < state.windows.size();) { |
| 4642 | TouchedWindow& touchedWindow = state.windows[i]; |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4643 | if (getWindowHandleLocked(touchedWindow.windowHandle) == nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4644 | if (DEBUG_FOCUS) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4645 | ALOGD("Touched window was removed: %s in display %" PRId32, |
| 4646 | touchedWindow.windowHandle->getName().c_str(), displayId); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4647 | } |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4648 | std::shared_ptr<InputChannel> touchedInputChannel = |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4649 | getInputChannelLocked(touchedWindow.windowHandle->getToken()); |
| 4650 | if (touchedInputChannel != nullptr) { |
| 4651 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4652 | "touched window was removed"); |
| 4653 | synthesizeCancelationEventsForInputChannelLocked(touchedInputChannel, options); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4654 | // Since we are about to drop the touch, cancel the events for the wallpaper as |
| 4655 | // well. |
| 4656 | if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND && |
| 4657 | touchedWindow.windowHandle->getInfo()->hasWallpaper) { |
| 4658 | sp<WindowInfoHandle> wallpaper = state.getWallpaperWindow(); |
| 4659 | if (wallpaper != nullptr) { |
| 4660 | sp<Connection> wallpaperConnection = |
| 4661 | getConnectionLocked(wallpaper->getToken()); |
| 4662 | synthesizeCancelationEventsForConnectionLocked(wallpaperConnection, |
| 4663 | options); |
| 4664 | } |
| 4665 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4666 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4667 | state.windows.erase(state.windows.begin() + i); |
| 4668 | } else { |
| 4669 | ++i; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4670 | } |
| 4671 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4672 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4673 | // 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] | 4674 | // could just clear the state here. |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4675 | if (mDragState && |
| 4676 | std::find(windowHandles.begin(), windowHandles.end(), mDragState->dragWindow) == |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4677 | windowHandles.end()) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4678 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4679 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4680 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4681 | |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4682 | if (isPerWindowInputRotationEnabled()) { |
| 4683 | // Determine if the orientation of any of the input windows have changed, and cancel all |
| 4684 | // pointer events if necessary. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4685 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
| 4686 | const sp<WindowInfoHandle> newWindowHandle = getWindowHandleLocked(oldWindowHandle); |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4687 | if (newWindowHandle != nullptr && |
| 4688 | newWindowHandle->getInfo()->transform.getOrientation() != |
| 4689 | oldWindowOrientations[oldWindowHandle->getId()]) { |
| 4690 | std::shared_ptr<InputChannel> inputChannel = |
| 4691 | getInputChannelLocked(newWindowHandle->getToken()); |
| 4692 | if (inputChannel != nullptr) { |
| 4693 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4694 | "touched window's orientation changed"); |
| 4695 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
| 4696 | } |
| 4697 | } |
| 4698 | } |
| 4699 | } |
| 4700 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4701 | // Release information for windows that are no longer present. |
| 4702 | // This ensures that unused input channels are released promptly. |
| 4703 | // Otherwise, they might stick around until the window handle is destroyed |
| 4704 | // which might not happen until the next GC. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4705 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4706 | if (getWindowHandleLocked(oldWindowHandle) == nullptr) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4707 | if (DEBUG_FOCUS) { |
| 4708 | ALOGD("Window went away: %s", oldWindowHandle->getName().c_str()); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4709 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4710 | oldWindowHandle->releaseChannel(); |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 4711 | // To avoid making too many calls into the compat framework, only |
| 4712 | // check for window flags when windows are going away. |
| 4713 | // TODO(b/157929241) : delete this. This is only needed temporarily |
| 4714 | // in order to gather some data about the flag usage |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4715 | if (oldWindowHandle->getInfo()->flags.test(WindowInfo::Flag::SLIPPERY)) { |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 4716 | ALOGW("%s has FLAG_SLIPPERY. Please report this in b/157929241", |
| 4717 | oldWindowHandle->getName().c_str()); |
| 4718 | if (mCompatService != nullptr) { |
| 4719 | mCompatService->reportChangeByUid(IInputConstants::BLOCK_FLAG_SLIPPERY, |
| 4720 | oldWindowHandle->getInfo()->ownerUid); |
| 4721 | } |
| 4722 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4723 | } |
chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 4724 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4725 | } |
| 4726 | |
| 4727 | void InputDispatcher::setFocusedApplication( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4728 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4729 | if (DEBUG_FOCUS) { |
| 4730 | ALOGD("setFocusedApplication displayId=%" PRId32 " %s", displayId, |
| 4731 | inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>"); |
| 4732 | } |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4733 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4734 | std::scoped_lock _l(mLock); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 4735 | setFocusedApplicationLocked(displayId, inputApplicationHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4736 | } // release lock |
| 4737 | |
| 4738 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4739 | mLooper->wake(); |
| 4740 | } |
| 4741 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 4742 | void InputDispatcher::setFocusedApplicationLocked( |
| 4743 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
| 4744 | std::shared_ptr<InputApplicationHandle> oldFocusedApplicationHandle = |
| 4745 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 4746 | |
| 4747 | if (sharedPointersEqual(oldFocusedApplicationHandle, inputApplicationHandle)) { |
| 4748 | return; // This application is already focused. No need to wake up or change anything. |
| 4749 | } |
| 4750 | |
| 4751 | // Set the new application handle. |
| 4752 | if (inputApplicationHandle != nullptr) { |
| 4753 | mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle; |
| 4754 | } else { |
| 4755 | mFocusedApplicationHandlesByDisplay.erase(displayId); |
| 4756 | } |
| 4757 | |
| 4758 | // No matter what the old focused application was, stop waiting on it because it is |
| 4759 | // no longer focused. |
| 4760 | resetNoFocusedWindowTimeoutLocked(); |
| 4761 | } |
| 4762 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4763 | /** |
| 4764 | * Sets the focused display, which is responsible for receiving focus-dispatched input events where |
| 4765 | * the display not specified. |
| 4766 | * |
| 4767 | * We track any unreleased events for each window. If a window loses the ability to receive the |
| 4768 | * released event, we will send a cancel event to it. So when the focused display is changed, we |
| 4769 | * cancel all the unreleased display-unspecified events for the focused window on the old focused |
| 4770 | * display. The display-specified events won't be affected. |
| 4771 | */ |
| 4772 | void InputDispatcher::setFocusedDisplay(int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4773 | if (DEBUG_FOCUS) { |
| 4774 | ALOGD("setFocusedDisplay displayId=%" PRId32, displayId); |
| 4775 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4776 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4777 | std::scoped_lock _l(mLock); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4778 | |
| 4779 | if (mFocusedDisplayId != displayId) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4780 | sp<IBinder> oldFocusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4781 | mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4782 | if (oldFocusedWindowToken != nullptr) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4783 | std::shared_ptr<InputChannel> inputChannel = |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4784 | getInputChannelLocked(oldFocusedWindowToken); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4785 | if (inputChannel != nullptr) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4786 | CancelationOptions |
| 4787 | options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 4788 | "The display which contains this window no longer has focus."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4789 | options.displayId = ADISPLAY_ID_NONE; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4790 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
| 4791 | } |
| 4792 | } |
| 4793 | mFocusedDisplayId = displayId; |
| 4794 | |
Chris Ye | 3c2d6f5 | 2020-08-09 10:39:48 -0700 | [diff] [blame] | 4795 | // Find new focused window and validate |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4796 | sp<IBinder> newFocusedWindowToken = mFocusResolver.getFocusedWindowToken(displayId); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 4797 | sendFocusChangedCommandLocked(oldFocusedWindowToken, newFocusedWindowToken); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4798 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4799 | if (newFocusedWindowToken == nullptr) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4800 | ALOGW("Focused display #%" PRId32 " does not have a focused window.", displayId); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4801 | if (mFocusResolver.hasFocusedWindowTokens()) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4802 | ALOGE("But another display has a focused window\n%s", |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4803 | mFocusResolver.dumpFocusedWindows().c_str()); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4804 | } |
| 4805 | } |
| 4806 | } |
| 4807 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4808 | if (DEBUG_FOCUS) { |
| 4809 | logDispatchStateLocked(); |
| 4810 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4811 | } // release lock |
| 4812 | |
| 4813 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4814 | mLooper->wake(); |
| 4815 | } |
| 4816 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4817 | void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4818 | if (DEBUG_FOCUS) { |
| 4819 | ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); |
| 4820 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4821 | |
| 4822 | bool changed; |
| 4823 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4824 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4825 | |
| 4826 | if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { |
| 4827 | if (mDispatchFrozen && !frozen) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4828 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4829 | } |
| 4830 | |
| 4831 | if (mDispatchEnabled && !enabled) { |
| 4832 | resetAndDropEverythingLocked("dispatcher is being disabled"); |
| 4833 | } |
| 4834 | |
| 4835 | mDispatchEnabled = enabled; |
| 4836 | mDispatchFrozen = frozen; |
| 4837 | changed = true; |
| 4838 | } else { |
| 4839 | changed = false; |
| 4840 | } |
| 4841 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4842 | if (DEBUG_FOCUS) { |
| 4843 | logDispatchStateLocked(); |
| 4844 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4845 | } // release lock |
| 4846 | |
| 4847 | if (changed) { |
| 4848 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4849 | mLooper->wake(); |
| 4850 | } |
| 4851 | } |
| 4852 | |
| 4853 | void InputDispatcher::setInputFilterEnabled(bool enabled) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4854 | if (DEBUG_FOCUS) { |
| 4855 | ALOGD("setInputFilterEnabled: enabled=%d", enabled); |
| 4856 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4857 | |
| 4858 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4859 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4860 | |
| 4861 | if (mInputFilterEnabled == enabled) { |
| 4862 | return; |
| 4863 | } |
| 4864 | |
| 4865 | mInputFilterEnabled = enabled; |
| 4866 | resetAndDropEverythingLocked("input filter is being enabled or disabled"); |
| 4867 | } // release lock |
| 4868 | |
| 4869 | // Wake up poll loop since there might be work to do to drop everything. |
| 4870 | mLooper->wake(); |
| 4871 | } |
| 4872 | |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 4873 | void InputDispatcher::setInTouchMode(bool inTouchMode) { |
| 4874 | std::scoped_lock lock(mLock); |
| 4875 | mInTouchMode = inTouchMode; |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 4876 | // TODO(b/193718270): Fire TouchModeEvent here. |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 4877 | } |
| 4878 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 4879 | void InputDispatcher::setMaximumObscuringOpacityForTouch(float opacity) { |
| 4880 | if (opacity < 0 || opacity > 1) { |
| 4881 | LOG_ALWAYS_FATAL("Maximum obscuring opacity for touch should be >= 0 and <= 1"); |
| 4882 | return; |
| 4883 | } |
| 4884 | |
| 4885 | std::scoped_lock lock(mLock); |
| 4886 | mMaximumObscuringOpacityForTouch = opacity; |
| 4887 | } |
| 4888 | |
| 4889 | void InputDispatcher::setBlockUntrustedTouchesMode(BlockUntrustedTouchesMode mode) { |
| 4890 | std::scoped_lock lock(mLock); |
| 4891 | mBlockUntrustedTouchesMode = mode; |
| 4892 | } |
| 4893 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4894 | std::pair<TouchState*, TouchedWindow*> InputDispatcher::findTouchStateAndWindowLocked( |
| 4895 | const sp<IBinder>& token) { |
| 4896 | for (auto& [displayId, state] : mTouchStatesByDisplay) { |
| 4897 | for (TouchedWindow& w : state.windows) { |
| 4898 | if (w.windowHandle->getToken() == token) { |
| 4899 | return std::make_pair(&state, &w); |
| 4900 | } |
| 4901 | } |
| 4902 | } |
| 4903 | return std::make_pair(nullptr, nullptr); |
| 4904 | } |
| 4905 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4906 | bool InputDispatcher::transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken, |
| 4907 | bool isDragDrop) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4908 | if (fromToken == toToken) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4909 | if (DEBUG_FOCUS) { |
| 4910 | ALOGD("Trivial transfer to same window."); |
| 4911 | } |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4912 | return true; |
| 4913 | } |
| 4914 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4915 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4916 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4917 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4918 | // Find the target touch state and touched window by fromToken. |
| 4919 | auto [state, touchedWindow] = findTouchStateAndWindowLocked(fromToken); |
| 4920 | if (state == nullptr || touchedWindow == nullptr) { |
| 4921 | ALOGD("Focus transfer failed because from window is not being touched."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4922 | return false; |
| 4923 | } |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4924 | |
| 4925 | const int32_t displayId = state->displayId; |
| 4926 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(toToken, displayId); |
| 4927 | if (toWindowHandle == nullptr) { |
| 4928 | ALOGW("Cannot transfer focus because to window not found."); |
| 4929 | return false; |
| 4930 | } |
| 4931 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4932 | if (DEBUG_FOCUS) { |
| 4933 | ALOGD("transferTouchFocus: fromWindowHandle=%s, toWindowHandle=%s", |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4934 | touchedWindow->windowHandle->getName().c_str(), |
| 4935 | toWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4936 | } |
| 4937 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4938 | // Erase old window. |
| 4939 | int32_t oldTargetFlags = touchedWindow->targetFlags; |
| 4940 | BitSet32 pointerIds = touchedWindow->pointerIds; |
| 4941 | state->removeWindowByToken(fromToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4942 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4943 | // Add new window. |
| 4944 | int32_t newTargetFlags = oldTargetFlags & |
| 4945 | (InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_SPLIT | |
| 4946 | InputTarget::FLAG_DISPATCH_AS_IS); |
| 4947 | state->addOrUpdateWindow(toWindowHandle, newTargetFlags, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4948 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4949 | // Store the dragging window. |
| 4950 | if (isDragDrop) { |
| 4951 | mDragState = std::make_unique<DragState>(toWindowHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4952 | } |
| 4953 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4954 | // Synthesize cancel for old window and down for new window. |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4955 | sp<Connection> fromConnection = getConnectionLocked(fromToken); |
| 4956 | sp<Connection> toConnection = getConnectionLocked(toToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4957 | if (fromConnection != nullptr && toConnection != nullptr) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4958 | fromConnection->inputState.mergePointerStateTo(toConnection->inputState); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4959 | CancelationOptions |
| 4960 | options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4961 | "transferring touch focus from this window to another window"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4962 | synthesizeCancelationEventsForConnectionLocked(fromConnection, options); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4963 | synthesizePointerDownEventsForConnectionLocked(toConnection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4964 | } |
| 4965 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4966 | if (DEBUG_FOCUS) { |
| 4967 | logDispatchStateLocked(); |
| 4968 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4969 | } // release lock |
| 4970 | |
| 4971 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4972 | mLooper->wake(); |
| 4973 | return true; |
| 4974 | } |
| 4975 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4976 | // Binder call |
| 4977 | bool InputDispatcher::transferTouch(const sp<IBinder>& destChannelToken) { |
| 4978 | sp<IBinder> fromToken; |
| 4979 | { // acquire lock |
| 4980 | std::scoped_lock _l(mLock); |
| 4981 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4982 | auto it = std::find_if(mTouchStatesByDisplay.begin(), mTouchStatesByDisplay.end(), |
| 4983 | [](const auto& pair) { return pair.second.windows.size() == 1; }); |
| 4984 | if (it == mTouchStatesByDisplay.end()) { |
| 4985 | ALOGW("Cannot transfer touch state because there is no exact window being touched"); |
| 4986 | return false; |
| 4987 | } |
| 4988 | const int32_t displayId = it->first; |
| 4989 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(destChannelToken, displayId); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4990 | if (toWindowHandle == nullptr) { |
| 4991 | ALOGW("Could not find window associated with token=%p", destChannelToken.get()); |
| 4992 | return false; |
| 4993 | } |
| 4994 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4995 | TouchState& state = it->second; |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4996 | const TouchedWindow& touchedWindow = state.windows[0]; |
| 4997 | fromToken = touchedWindow.windowHandle->getToken(); |
| 4998 | } // release lock |
| 4999 | |
| 5000 | return transferTouchFocus(fromToken, destChannelToken); |
| 5001 | } |
| 5002 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5003 | void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5004 | if (DEBUG_FOCUS) { |
| 5005 | ALOGD("Resetting and dropping all events (%s).", reason); |
| 5006 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5007 | |
| 5008 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, reason); |
| 5009 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 5010 | |
| 5011 | resetKeyRepeatLocked(); |
| 5012 | releasePendingEventLocked(); |
| 5013 | drainInboundQueueLocked(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5014 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5015 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5016 | mAnrTracker.clear(); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5017 | mTouchStatesByDisplay.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5018 | mLastHoverWindowHandle.clear(); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5019 | mReplacedKeys.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5020 | } |
| 5021 | |
| 5022 | void InputDispatcher::logDispatchStateLocked() { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5023 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5024 | dumpDispatchStateLocked(dump); |
| 5025 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5026 | std::istringstream stream(dump); |
| 5027 | std::string line; |
| 5028 | |
| 5029 | while (std::getline(stream, line, '\n')) { |
| 5030 | ALOGD("%s", line.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5031 | } |
| 5032 | } |
| 5033 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5034 | std::string InputDispatcher::dumpPointerCaptureStateLocked() { |
| 5035 | std::string dump; |
| 5036 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5037 | dump += StringPrintf(INDENT "Pointer Capture Requested: %s\n", |
| 5038 | toString(mCurrentPointerCaptureRequest.enable)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5039 | |
| 5040 | std::string windowName = "None"; |
| 5041 | if (mWindowTokenWithPointerCapture) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5042 | const sp<WindowInfoHandle> captureWindowHandle = |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5043 | getWindowHandleLocked(mWindowTokenWithPointerCapture); |
| 5044 | windowName = captureWindowHandle ? captureWindowHandle->getName().c_str() |
| 5045 | : "token has capture without window"; |
| 5046 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5047 | 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] | 5048 | |
| 5049 | return dump; |
| 5050 | } |
| 5051 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5052 | void InputDispatcher::dumpDispatchStateLocked(std::string& dump) { |
Siarhei Vishniakou | 043a3ec | 2019-05-01 11:30:46 -0700 | [diff] [blame] | 5053 | dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled)); |
| 5054 | dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen)); |
| 5055 | dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5056 | dump += StringPrintf(INDENT "FocusedDisplayId: %" PRId32 "\n", mFocusedDisplayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5057 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5058 | if (!mFocusedApplicationHandlesByDisplay.empty()) { |
| 5059 | dump += StringPrintf(INDENT "FocusedApplications:\n"); |
| 5060 | for (auto& it : mFocusedApplicationHandlesByDisplay) { |
| 5061 | const int32_t displayId = it.first; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5062 | const std::shared_ptr<InputApplicationHandle>& applicationHandle = it.second; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5063 | const std::chrono::duration timeout = |
| 5064 | applicationHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5065 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5066 | ", name='%s', dispatchingTimeout=%" PRId64 "ms\n", |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5067 | displayId, applicationHandle->getName().c_str(), millis(timeout)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5068 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5069 | } else { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5070 | dump += StringPrintf(INDENT "FocusedApplications: <none>\n"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5071 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5072 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5073 | dump += mFocusResolver.dump(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5074 | dump += dumpPointerCaptureStateLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5075 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5076 | if (!mTouchStatesByDisplay.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5077 | dump += StringPrintf(INDENT "TouchStatesByDisplay:\n"); |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5078 | for (const std::pair<int32_t, TouchState>& pair : mTouchStatesByDisplay) { |
| 5079 | const TouchState& state = pair.second; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5080 | 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] | 5081 | state.displayId, toString(state.down), toString(state.split), |
| 5082 | state.deviceId, state.source); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5083 | if (!state.windows.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5084 | dump += INDENT3 "Windows:\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5085 | for (size_t i = 0; i < state.windows.size(); i++) { |
| 5086 | const TouchedWindow& touchedWindow = state.windows[i]; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5087 | dump += StringPrintf(INDENT4 |
| 5088 | "%zu: name='%s', pointerIds=0x%0x, targetFlags=0x%x\n", |
| 5089 | i, touchedWindow.windowHandle->getName().c_str(), |
| 5090 | touchedWindow.pointerIds.value, touchedWindow.targetFlags); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5091 | } |
| 5092 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5093 | dump += INDENT3 "Windows: <none>\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5094 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5095 | } |
| 5096 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5097 | dump += INDENT "TouchStates: <no displays touched>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5098 | } |
| 5099 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5100 | if (mDragState) { |
| 5101 | dump += StringPrintf(INDENT "DragState:\n"); |
| 5102 | mDragState->dump(dump, INDENT2); |
| 5103 | } |
| 5104 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5105 | if (!mWindowHandlesByDisplay.empty()) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5106 | for (const auto& [displayId, windowHandles] : mWindowHandlesByDisplay) { |
| 5107 | dump += StringPrintf(INDENT "Display: %" PRId32 "\n", displayId); |
| 5108 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
| 5109 | const auto& displayInfo = it->second; |
| 5110 | dump += StringPrintf(INDENT2 "logicalSize=%dx%d\n", displayInfo.logicalWidth, |
| 5111 | displayInfo.logicalHeight); |
| 5112 | displayInfo.transform.dump(dump, "transform", INDENT4); |
| 5113 | } else { |
| 5114 | dump += INDENT2 "No DisplayInfo found!\n"; |
| 5115 | } |
| 5116 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5117 | if (!windowHandles.empty()) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5118 | dump += INDENT2 "Windows:\n"; |
| 5119 | for (size_t i = 0; i < windowHandles.size(); i++) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5120 | const sp<WindowInfoHandle>& windowHandle = windowHandles[i]; |
| 5121 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5122 | |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5123 | dump += StringPrintf(INDENT3 "%zu: name='%s', id=%" PRId32 ", displayId=%d, " |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 5124 | "paused=%s, focusable=%s, " |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5125 | "hasWallpaper=%s, visible=%s, alpha=%.2f, " |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 5126 | "flags=%s, type=%s, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5127 | "frame=[%d,%d][%d,%d], globalScale=%f, " |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5128 | "applicationInfo.name=%s, " |
| 5129 | "applicationInfo.token=%s, " |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 5130 | "touchableRegion=", |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5131 | i, windowInfo->name.c_str(), windowInfo->id, |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 5132 | windowInfo->displayId, toString(windowInfo->paused), |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5133 | toString(windowInfo->focusable), |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5134 | toString(windowInfo->hasWallpaper), |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5135 | toString(windowInfo->visible), windowInfo->alpha, |
Michael Wright | 8759d67 | 2020-07-21 00:46:45 +0100 | [diff] [blame] | 5136 | windowInfo->flags.string().c_str(), |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 5137 | ftl::enum_string(windowInfo->type).c_str(), |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 5138 | windowInfo->frameLeft, windowInfo->frameTop, |
| 5139 | windowInfo->frameRight, windowInfo->frameBottom, |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 5140 | windowInfo->globalScaleFactor, |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5141 | windowInfo->applicationInfo.name.c_str(), |
| 5142 | toString(windowInfo->applicationInfo.token).c_str()); |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 5143 | dump += dumpRegion(windowInfo->touchableRegion); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 5144 | dump += StringPrintf(", inputFeatures=%s", |
| 5145 | windowInfo->inputFeatures.string().c_str()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5146 | dump += StringPrintf(", ownerPid=%d, ownerUid=%d, dispatchingTimeout=%" PRId64 |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 5147 | "ms, trustedOverlay=%s, hasToken=%s, " |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5148 | "touchOcclusionMode=%s\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5149 | windowInfo->ownerPid, windowInfo->ownerUid, |
Bernardo Rufino | c2f1fad | 2020-11-04 17:30:57 +0000 | [diff] [blame] | 5150 | millis(windowInfo->dispatchingTimeout), |
| 5151 | toString(windowInfo->trustedOverlay), |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 5152 | toString(windowInfo->token != nullptr), |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5153 | toString(windowInfo->touchOcclusionMode).c_str()); |
chaviw | 85b4420 | 2020-07-24 11:46:21 -0700 | [diff] [blame] | 5154 | windowInfo->transform.dump(dump, "transform", INDENT4); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5155 | } |
| 5156 | } else { |
| 5157 | dump += INDENT2 "Windows: <none>\n"; |
| 5158 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5159 | } |
| 5160 | } else { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5161 | dump += INDENT "Displays: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5162 | } |
| 5163 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5164 | if (!mGlobalMonitorsByDisplay.empty() || !mGestureMonitorsByDisplay.empty()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5165 | for (auto& it : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5166 | const std::vector<Monitor>& monitors = it.second; |
| 5167 | dump += StringPrintf(INDENT "Global monitors in display %" PRId32 ":\n", it.first); |
| 5168 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5169 | } |
| 5170 | for (auto& it : mGestureMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5171 | const std::vector<Monitor>& monitors = it.second; |
| 5172 | dump += StringPrintf(INDENT "Gesture monitors in display %" PRId32 ":\n", it.first); |
| 5173 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5174 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5175 | } else { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5176 | dump += INDENT "Monitors: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5177 | } |
| 5178 | |
| 5179 | nsecs_t currentTime = now(); |
| 5180 | |
| 5181 | // Dump recently dispatched or dropped events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5182 | if (!mRecentQueue.empty()) { |
| 5183 | dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5184 | for (std::shared_ptr<EventEntry>& entry : mRecentQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5185 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5186 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5187 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5188 | } |
| 5189 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5190 | dump += INDENT "RecentQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5191 | } |
| 5192 | |
| 5193 | // Dump event currently being dispatched. |
| 5194 | if (mPendingEvent) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5195 | dump += INDENT "PendingEvent:\n"; |
| 5196 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5197 | dump += mPendingEvent->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5198 | dump += StringPrintf(", age=%" PRId64 "ms\n", |
| 5199 | ns2ms(currentTime - mPendingEvent->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5200 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5201 | dump += INDENT "PendingEvent: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5202 | } |
| 5203 | |
| 5204 | // Dump inbound events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5205 | if (!mInboundQueue.empty()) { |
| 5206 | dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5207 | for (std::shared_ptr<EventEntry>& entry : mInboundQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5208 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5209 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5210 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5211 | } |
| 5212 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5213 | dump += INDENT "InboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5214 | } |
| 5215 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5216 | if (!mReplacedKeys.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5217 | dump += INDENT "ReplacedKeys:\n"; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5218 | for (const std::pair<KeyReplacement, int32_t>& pair : mReplacedKeys) { |
| 5219 | const KeyReplacement& replacement = pair.first; |
| 5220 | int32_t newKeyCode = pair.second; |
| 5221 | dump += StringPrintf(INDENT2 "originalKeyCode=%d, deviceId=%d -> newKeyCode=%d\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5222 | replacement.keyCode, replacement.deviceId, newKeyCode); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5223 | } |
| 5224 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5225 | dump += INDENT "ReplacedKeys: <empty>\n"; |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5226 | } |
| 5227 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5228 | if (!mCommandQueue.empty()) { |
| 5229 | dump += StringPrintf(INDENT "CommandQueue: size=%zu\n", mCommandQueue.size()); |
| 5230 | } else { |
| 5231 | dump += INDENT "CommandQueue: <empty>\n"; |
| 5232 | } |
| 5233 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5234 | if (!mConnectionsByToken.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5235 | dump += INDENT "Connections:\n"; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5236 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5237 | dump += StringPrintf(INDENT2 "%i: channelName='%s', windowName='%s', " |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5238 | "status=%s, monitor=%s, responsive=%s\n", |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5239 | connection->inputChannel->getFd().get(), |
| 5240 | connection->getInputChannelName().c_str(), |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5241 | connection->getWindowName().c_str(), connection->getStatusLabel(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5242 | toString(connection->monitor), toString(connection->responsive)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5243 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5244 | if (!connection->outboundQueue.empty()) { |
| 5245 | dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n", |
| 5246 | connection->outboundQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5247 | dump += dumpQueue(connection->outboundQueue, currentTime); |
| 5248 | |
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 += INDENT3 "OutboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5251 | } |
| 5252 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5253 | if (!connection->waitQueue.empty()) { |
| 5254 | dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n", |
| 5255 | connection->waitQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5256 | dump += dumpQueue(connection->waitQueue, currentTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5257 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5258 | dump += INDENT3 "WaitQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5259 | } |
| 5260 | } |
| 5261 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5262 | dump += INDENT "Connections: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5263 | } |
| 5264 | |
| 5265 | if (isAppSwitchPendingLocked()) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5266 | dump += StringPrintf(INDENT "AppSwitch: pending, due in %" PRId64 "ms\n", |
| 5267 | ns2ms(mAppSwitchDueTime - now())); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5268 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5269 | dump += INDENT "AppSwitch: not pending\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5270 | } |
| 5271 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5272 | dump += INDENT "Configuration:\n"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5273 | dump += StringPrintf(INDENT2 "KeyRepeatDelay: %" PRId64 "ms\n", ns2ms(mConfig.keyRepeatDelay)); |
| 5274 | dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n", |
| 5275 | ns2ms(mConfig.keyRepeatTimeout)); |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 5276 | dump += mLatencyTracker.dump(INDENT2); |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 5277 | dump += mLatencyAggregator.dump(INDENT2); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5278 | } |
| 5279 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5280 | void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) { |
| 5281 | const size_t numMonitors = monitors.size(); |
| 5282 | for (size_t i = 0; i < numMonitors; i++) { |
| 5283 | const Monitor& monitor = monitors[i]; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5284 | const std::shared_ptr<InputChannel>& channel = monitor.inputChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5285 | dump += StringPrintf(INDENT2 "%zu: '%s', ", i, channel->getName().c_str()); |
| 5286 | dump += "\n"; |
| 5287 | } |
| 5288 | } |
| 5289 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5290 | class LooperEventCallback : public LooperCallback { |
| 5291 | public: |
| 5292 | LooperEventCallback(std::function<int(int events)> callback) : mCallback(callback) {} |
| 5293 | int handleEvent(int /*fd*/, int events, void* /*data*/) override { return mCallback(events); } |
| 5294 | |
| 5295 | private: |
| 5296 | std::function<int(int events)> mCallback; |
| 5297 | }; |
| 5298 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5299 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputChannel(const std::string& name) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5300 | if (DEBUG_CHANNEL_CREATION) { |
| 5301 | ALOGD("channel '%s' ~ createInputChannel", name.c_str()); |
| 5302 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5303 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5304 | std::unique_ptr<InputChannel> serverChannel; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5305 | std::unique_ptr<InputChannel> clientChannel; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5306 | status_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5307 | |
| 5308 | if (result) { |
| 5309 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5310 | } |
| 5311 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5312 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5313 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5314 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5315 | int fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5316 | sp<Connection> connection = |
| 5317 | new Connection(std::move(serverChannel), false /*monitor*/, mIdGenerator); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5318 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5319 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5320 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5321 | } |
| 5322 | mConnectionsByToken.emplace(token, connection); |
| 5323 | |
| 5324 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5325 | this, std::placeholders::_1, token); |
| 5326 | |
| 5327 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, new LooperEventCallback(callback), nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5328 | } // release lock |
| 5329 | |
| 5330 | // Wake the looper because some connections have changed. |
| 5331 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5332 | return clientChannel; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5333 | } |
| 5334 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5335 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputMonitor(int32_t displayId, |
| 5336 | bool isGestureMonitor, |
| 5337 | const std::string& name, |
| 5338 | int32_t pid) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5339 | std::shared_ptr<InputChannel> serverChannel; |
| 5340 | std::unique_ptr<InputChannel> clientChannel; |
| 5341 | status_t result = openInputChannelPair(name, serverChannel, clientChannel); |
| 5342 | if (result) { |
| 5343 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5344 | } |
| 5345 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5346 | { // acquire lock |
| 5347 | std::scoped_lock _l(mLock); |
| 5348 | |
| 5349 | if (displayId < 0) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5350 | return base::Error(BAD_VALUE) << "Attempted to create input monitor with name " << name |
| 5351 | << " without a specified display."; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5352 | } |
| 5353 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5354 | sp<Connection> connection = new Connection(serverChannel, true /*monitor*/, mIdGenerator); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5355 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5356 | const int fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5357 | |
| 5358 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5359 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5360 | } |
| 5361 | mConnectionsByToken.emplace(token, connection); |
| 5362 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5363 | this, std::placeholders::_1, token); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5364 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5365 | auto& monitorsByDisplay = |
| 5366 | isGestureMonitor ? mGestureMonitorsByDisplay : mGlobalMonitorsByDisplay; |
Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 5367 | monitorsByDisplay[displayId].emplace_back(serverChannel, pid); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5368 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5369 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, new LooperEventCallback(callback), nullptr); |
Siarhei Vishniakou | c961c74 | 2021-05-19 19:16:59 +0000 | [diff] [blame] | 5370 | ALOGI("Created monitor %s for display %" PRId32 ", gesture=%s, pid=%" PRId32, name.c_str(), |
| 5371 | displayId, toString(isGestureMonitor), pid); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5372 | } |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5373 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5374 | // Wake the looper because some connections have changed. |
| 5375 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5376 | return clientChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5377 | } |
| 5378 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5379 | status_t InputDispatcher::removeInputChannel(const sp<IBinder>& connectionToken) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5380 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5381 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5382 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5383 | status_t status = removeInputChannelLocked(connectionToken, false /*notify*/); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5384 | if (status) { |
| 5385 | return status; |
| 5386 | } |
| 5387 | } // release lock |
| 5388 | |
| 5389 | // Wake the poll loop because removing the connection may have changed the current |
| 5390 | // synchronization state. |
| 5391 | mLooper->wake(); |
| 5392 | return OK; |
| 5393 | } |
| 5394 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5395 | status_t InputDispatcher::removeInputChannelLocked(const sp<IBinder>& connectionToken, |
| 5396 | bool notify) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5397 | sp<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5398 | if (connection == nullptr) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5399 | // 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] | 5400 | return BAD_VALUE; |
| 5401 | } |
| 5402 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5403 | removeConnectionLocked(connection); |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 5404 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5405 | if (connection->monitor) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5406 | removeMonitorChannelLocked(connectionToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5407 | } |
| 5408 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5409 | mLooper->removeFd(connection->inputChannel->getFd()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5410 | |
| 5411 | nsecs_t currentTime = now(); |
| 5412 | abortBrokenDispatchCycleLocked(currentTime, connection, notify); |
| 5413 | |
| 5414 | connection->status = Connection::STATUS_ZOMBIE; |
| 5415 | return OK; |
| 5416 | } |
| 5417 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5418 | void InputDispatcher::removeMonitorChannelLocked(const sp<IBinder>& connectionToken) { |
| 5419 | removeMonitorChannelLocked(connectionToken, mGlobalMonitorsByDisplay); |
| 5420 | removeMonitorChannelLocked(connectionToken, mGestureMonitorsByDisplay); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5421 | } |
| 5422 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5423 | void InputDispatcher::removeMonitorChannelLocked( |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5424 | const sp<IBinder>& connectionToken, |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5425 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5426 | for (auto it = monitorsByDisplay.begin(); it != monitorsByDisplay.end();) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5427 | std::vector<Monitor>& monitors = it->second; |
| 5428 | const size_t numMonitors = monitors.size(); |
| 5429 | for (size_t i = 0; i < numMonitors; i++) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5430 | if (monitors[i].inputChannel->getConnectionToken() == connectionToken) { |
Siarhei Vishniakou | 59a9f29 | 2021-04-22 18:43:28 +0000 | [diff] [blame] | 5431 | ALOGI("Erasing monitor %s on display %" PRId32 ", pid=%" PRId32, |
| 5432 | monitors[i].inputChannel->getName().c_str(), it->first, monitors[i].pid); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5433 | monitors.erase(monitors.begin() + i); |
| 5434 | break; |
| 5435 | } |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5436 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5437 | if (monitors.empty()) { |
| 5438 | it = monitorsByDisplay.erase(it); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5439 | } else { |
| 5440 | ++it; |
| 5441 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5442 | } |
| 5443 | } |
| 5444 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5445 | status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) { |
| 5446 | { // acquire lock |
| 5447 | std::scoped_lock _l(mLock); |
| 5448 | std::optional<int32_t> foundDisplayId = findGestureMonitorDisplayByTokenLocked(token); |
| 5449 | |
| 5450 | if (!foundDisplayId) { |
| 5451 | ALOGW("Attempted to pilfer pointers from an un-registered monitor or invalid token"); |
| 5452 | return BAD_VALUE; |
| 5453 | } |
| 5454 | int32_t displayId = foundDisplayId.value(); |
| 5455 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5456 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 5457 | mTouchStatesByDisplay.find(displayId); |
| 5458 | if (stateIt == mTouchStatesByDisplay.end()) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5459 | ALOGW("Failed to pilfer pointers: no pointers on display %" PRId32 ".", displayId); |
| 5460 | return BAD_VALUE; |
| 5461 | } |
| 5462 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5463 | TouchState& state = stateIt->second; |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5464 | std::shared_ptr<InputChannel> requestingChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5465 | std::optional<int32_t> foundDeviceId; |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 5466 | for (const auto& monitor : state.gestureMonitors) { |
| 5467 | if (monitor.inputChannel->getConnectionToken() == token) { |
| 5468 | requestingChannel = monitor.inputChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5469 | foundDeviceId = state.deviceId; |
| 5470 | } |
| 5471 | } |
| 5472 | if (!foundDeviceId || !state.down) { |
| 5473 | 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] | 5474 | " Ignoring."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5475 | return BAD_VALUE; |
| 5476 | } |
| 5477 | int32_t deviceId = foundDeviceId.value(); |
| 5478 | |
| 5479 | // Send cancel events to all the input channels we're stealing from. |
| 5480 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5481 | "gesture monitor stole pointer stream"); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5482 | options.deviceId = deviceId; |
| 5483 | options.displayId = displayId; |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5484 | std::string canceledWindows = "["; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5485 | for (const TouchedWindow& window : state.windows) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5486 | std::shared_ptr<InputChannel> channel = |
| 5487 | getInputChannelLocked(window.windowHandle->getToken()); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5488 | if (channel != nullptr) { |
| 5489 | synthesizeCancelationEventsForInputChannelLocked(channel, options); |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5490 | canceledWindows += channel->getName() + ", "; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5491 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5492 | } |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5493 | canceledWindows += "]"; |
| 5494 | ALOGI("Monitor %s is stealing touch from %s", requestingChannel->getName().c_str(), |
| 5495 | canceledWindows.c_str()); |
| 5496 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5497 | // Then clear the current touch state so we stop dispatching to them as well. |
| 5498 | state.filterNonMonitors(); |
| 5499 | } |
| 5500 | return OK; |
| 5501 | } |
| 5502 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5503 | void InputDispatcher::requestPointerCapture(const sp<IBinder>& windowToken, bool enabled) { |
| 5504 | { // acquire lock |
| 5505 | std::scoped_lock _l(mLock); |
| 5506 | if (DEBUG_FOCUS) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5507 | const sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(windowToken); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5508 | ALOGI("Request to %s Pointer Capture from: %s.", enabled ? "enable" : "disable", |
| 5509 | windowHandle != nullptr ? windowHandle->getName().c_str() |
| 5510 | : "token without window"); |
| 5511 | } |
| 5512 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5513 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5514 | if (focusedToken != windowToken) { |
| 5515 | ALOGW("Ignoring request to %s Pointer Capture: window does not have focus.", |
| 5516 | enabled ? "enable" : "disable"); |
| 5517 | return; |
| 5518 | } |
| 5519 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5520 | if (enabled == mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5521 | ALOGW("Ignoring request to %s Pointer Capture: " |
| 5522 | "window has %s requested pointer capture.", |
| 5523 | enabled ? "enable" : "disable", enabled ? "already" : "not"); |
| 5524 | return; |
| 5525 | } |
| 5526 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5527 | setPointerCaptureLocked(enabled); |
| 5528 | } // release lock |
| 5529 | |
| 5530 | // Wake the thread to process command entries. |
| 5531 | mLooper->wake(); |
| 5532 | } |
| 5533 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5534 | std::optional<int32_t> InputDispatcher::findGestureMonitorDisplayByTokenLocked( |
| 5535 | const sp<IBinder>& token) { |
| 5536 | for (const auto& it : mGestureMonitorsByDisplay) { |
| 5537 | const std::vector<Monitor>& monitors = it.second; |
| 5538 | for (const Monitor& monitor : monitors) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5539 | if (monitor.inputChannel->getConnectionToken() == token) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5540 | return it.first; |
| 5541 | } |
| 5542 | } |
| 5543 | } |
| 5544 | return std::nullopt; |
| 5545 | } |
| 5546 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5547 | std::optional<int32_t> InputDispatcher::findMonitorPidByTokenLocked(const sp<IBinder>& token) { |
| 5548 | std::optional<int32_t> gesturePid = findMonitorPidByToken(mGestureMonitorsByDisplay, token); |
| 5549 | if (gesturePid.has_value()) { |
| 5550 | return gesturePid; |
| 5551 | } |
| 5552 | return findMonitorPidByToken(mGlobalMonitorsByDisplay, token); |
| 5553 | } |
| 5554 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5555 | sp<Connection> InputDispatcher::getConnectionLocked(const sp<IBinder>& inputConnectionToken) const { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 5556 | if (inputConnectionToken == nullptr) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5557 | return nullptr; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 5558 | } |
| 5559 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5560 | for (const auto& [token, connection] : mConnectionsByToken) { |
| 5561 | if (token == inputConnectionToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5562 | return connection; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5563 | } |
| 5564 | } |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 5565 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5566 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5567 | } |
| 5568 | |
Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 5569 | std::string InputDispatcher::getConnectionNameLocked(const sp<IBinder>& connectionToken) const { |
| 5570 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 5571 | if (connection == nullptr) { |
| 5572 | return "<nullptr>"; |
| 5573 | } |
| 5574 | return connection->getInputChannelName(); |
| 5575 | } |
| 5576 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5577 | void InputDispatcher::removeConnectionLocked(const sp<Connection>& connection) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5578 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5579 | mConnectionsByToken.erase(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5580 | } |
| 5581 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5582 | void InputDispatcher::doDispatchCycleFinishedCommand(nsecs_t finishTime, |
| 5583 | const sp<Connection>& connection, uint32_t seq, |
| 5584 | bool handled, nsecs_t consumeTime) { |
| 5585 | // Handle post-event policy actions. |
| 5586 | std::deque<DispatchEntry*>::iterator dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 5587 | if (dispatchEntryIt == connection->waitQueue.end()) { |
| 5588 | return; |
| 5589 | } |
| 5590 | DispatchEntry* dispatchEntry = *dispatchEntryIt; |
| 5591 | const nsecs_t eventDuration = finishTime - dispatchEntry->deliveryTime; |
| 5592 | if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) { |
| 5593 | ALOGI("%s spent %" PRId64 "ms processing %s", connection->getWindowName().c_str(), |
| 5594 | ns2ms(eventDuration), dispatchEntry->eventEntry->getDescription().c_str()); |
| 5595 | } |
| 5596 | if (shouldReportFinishedEvent(*dispatchEntry, *connection)) { |
| 5597 | mLatencyTracker.trackFinishedEvent(dispatchEntry->eventEntry->id, |
| 5598 | connection->inputChannel->getConnectionToken(), |
| 5599 | dispatchEntry->deliveryTime, consumeTime, finishTime); |
| 5600 | } |
| 5601 | |
| 5602 | bool restartEvent; |
| 5603 | if (dispatchEntry->eventEntry->type == EventEntry::Type::KEY) { |
| 5604 | KeyEntry& keyEntry = static_cast<KeyEntry&>(*(dispatchEntry->eventEntry)); |
| 5605 | restartEvent = |
| 5606 | afterKeyEventLockedInterruptable(connection, dispatchEntry, keyEntry, handled); |
| 5607 | } else if (dispatchEntry->eventEntry->type == EventEntry::Type::MOTION) { |
| 5608 | MotionEntry& motionEntry = static_cast<MotionEntry&>(*(dispatchEntry->eventEntry)); |
| 5609 | restartEvent = afterMotionEventLockedInterruptable(connection, dispatchEntry, motionEntry, |
| 5610 | handled); |
| 5611 | } else { |
| 5612 | restartEvent = false; |
| 5613 | } |
| 5614 | |
| 5615 | // Dequeue the event and start the next cycle. |
| 5616 | // Because the lock might have been released, it is possible that the |
| 5617 | // contents of the wait queue to have been drained, so we need to double-check |
| 5618 | // a few things. |
| 5619 | dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 5620 | if (dispatchEntryIt != connection->waitQueue.end()) { |
| 5621 | dispatchEntry = *dispatchEntryIt; |
| 5622 | connection->waitQueue.erase(dispatchEntryIt); |
| 5623 | const sp<IBinder>& connectionToken = connection->inputChannel->getConnectionToken(); |
| 5624 | mAnrTracker.erase(dispatchEntry->timeoutTime, connectionToken); |
| 5625 | if (!connection->responsive) { |
| 5626 | connection->responsive = isConnectionResponsive(*connection); |
| 5627 | if (connection->responsive) { |
| 5628 | // The connection was unresponsive, and now it's responsive. |
| 5629 | processConnectionResponsiveLocked(*connection); |
| 5630 | } |
| 5631 | } |
| 5632 | traceWaitQueueLength(*connection); |
| 5633 | if (restartEvent && connection->status == Connection::STATUS_NORMAL) { |
| 5634 | connection->outboundQueue.push_front(dispatchEntry); |
| 5635 | traceOutboundQueueLength(*connection); |
| 5636 | } else { |
| 5637 | releaseDispatchEntry(dispatchEntry); |
| 5638 | } |
| 5639 | } |
| 5640 | |
| 5641 | // Start the next dispatch cycle for this connection. |
| 5642 | startDispatchCycleLocked(now(), connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5643 | } |
| 5644 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5645 | void InputDispatcher::sendFocusChangedCommandLocked(const sp<IBinder>& oldToken, |
| 5646 | const sp<IBinder>& newToken) { |
| 5647 | auto command = [this, oldToken, newToken]() REQUIRES(mLock) { |
| 5648 | scoped_unlock unlock(mLock); |
| 5649 | mPolicy->notifyFocusChanged(oldToken, newToken); |
| 5650 | }; |
| 5651 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5652 | } |
| 5653 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5654 | void InputDispatcher::sendDropWindowCommandLocked(const sp<IBinder>& token, float x, float y) { |
| 5655 | auto command = [this, token, x, y]() REQUIRES(mLock) { |
| 5656 | scoped_unlock unlock(mLock); |
| 5657 | mPolicy->notifyDropWindow(token, x, y); |
| 5658 | }; |
| 5659 | postCommandLocked(std::move(command)); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5660 | } |
| 5661 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5662 | void InputDispatcher::sendUntrustedTouchCommandLocked(const std::string& obscuringPackage) { |
| 5663 | auto command = [this, obscuringPackage]() REQUIRES(mLock) { |
| 5664 | scoped_unlock unlock(mLock); |
| 5665 | mPolicy->notifyUntrustedTouch(obscuringPackage); |
| 5666 | }; |
| 5667 | postCommandLocked(std::move(command)); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 5668 | } |
| 5669 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5670 | void InputDispatcher::onAnrLocked(const sp<Connection>& connection) { |
| 5671 | if (connection == nullptr) { |
| 5672 | LOG_ALWAYS_FATAL("Caller must check for nullness"); |
| 5673 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5674 | // Since we are allowing the policy to extend the timeout, maybe the waitQueue |
| 5675 | // is already healthy again. Don't raise ANR in this situation |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5676 | if (connection->waitQueue.empty()) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5677 | ALOGI("Not raising ANR because the connection %s has recovered", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5678 | connection->inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5679 | return; |
| 5680 | } |
| 5681 | /** |
| 5682 | * The "oldestEntry" is the entry that was first sent to the application. That entry, however, |
| 5683 | * may not be the one that caused the timeout to occur. One possibility is that window timeout |
| 5684 | * has changed. This could cause newer entries to time out before the already dispatched |
| 5685 | * entries. In that situation, the newest entries caused ANR. But in all likelihood, the app |
| 5686 | * processes the events linearly. So providing information about the oldest entry seems to be |
| 5687 | * most useful. |
| 5688 | */ |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5689 | DispatchEntry* oldestEntry = *connection->waitQueue.begin(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5690 | const nsecs_t currentWait = now() - oldestEntry->deliveryTime; |
| 5691 | std::string reason = |
| 5692 | android::base::StringPrintf("%s is not responding. Waited %" PRId64 "ms for %s", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5693 | connection->inputChannel->getName().c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5694 | ns2ms(currentWait), |
| 5695 | oldestEntry->eventEntry->getDescription().c_str()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5696 | sp<IBinder> connectionToken = connection->inputChannel->getConnectionToken(); |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 5697 | updateLastAnrStateLocked(getWindowHandleLocked(connectionToken), reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5698 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5699 | processConnectionUnresponsiveLocked(*connection, std::move(reason)); |
| 5700 | |
| 5701 | // Stop waking up for events on this connection, it is already unresponsive |
| 5702 | cancelEventsForAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5703 | } |
| 5704 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5705 | void InputDispatcher::onAnrLocked(std::shared_ptr<InputApplicationHandle> application) { |
| 5706 | std::string reason = |
| 5707 | StringPrintf("%s does not have a focused window", application->getName().c_str()); |
| 5708 | updateLastAnrStateLocked(*application, reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5709 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5710 | auto command = [this, application = std::move(application)]() REQUIRES(mLock) { |
| 5711 | scoped_unlock unlock(mLock); |
| 5712 | mPolicy->notifyNoFocusedWindowAnr(application); |
| 5713 | }; |
| 5714 | postCommandLocked(std::move(command)); |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 5715 | } |
| 5716 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5717 | void InputDispatcher::updateLastAnrStateLocked(const sp<WindowInfoHandle>& window, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5718 | const std::string& reason) { |
| 5719 | const std::string windowLabel = getApplicationWindowLabel(nullptr, window); |
| 5720 | updateLastAnrStateLocked(windowLabel, reason); |
| 5721 | } |
| 5722 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5723 | void InputDispatcher::updateLastAnrStateLocked(const InputApplicationHandle& application, |
| 5724 | const std::string& reason) { |
| 5725 | const std::string windowLabel = getApplicationWindowLabel(&application, nullptr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5726 | updateLastAnrStateLocked(windowLabel, reason); |
| 5727 | } |
| 5728 | |
| 5729 | void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel, |
| 5730 | const std::string& reason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5731 | // 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] | 5732 | time_t t = time(nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5733 | struct tm tm; |
| 5734 | localtime_r(&t, &tm); |
| 5735 | char timestr[64]; |
| 5736 | strftime(timestr, sizeof(timestr), "%F %T", &tm); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5737 | mLastAnrState.clear(); |
| 5738 | mLastAnrState += INDENT "ANR:\n"; |
| 5739 | mLastAnrState += StringPrintf(INDENT2 "Time: %s\n", timestr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5740 | mLastAnrState += StringPrintf(INDENT2 "Reason: %s\n", reason.c_str()); |
| 5741 | mLastAnrState += StringPrintf(INDENT2 "Window: %s\n", windowLabel.c_str()); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5742 | dumpDispatchStateLocked(mLastAnrState); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5743 | } |
| 5744 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5745 | void InputDispatcher::doInterceptKeyBeforeDispatchingCommand(const sp<IBinder>& focusedWindowToken, |
| 5746 | KeyEntry& entry) { |
| 5747 | const KeyEvent event = createKeyEvent(entry); |
| 5748 | nsecs_t delay = 0; |
| 5749 | { // release lock |
| 5750 | scoped_unlock unlock(mLock); |
| 5751 | android::base::Timer t; |
| 5752 | delay = mPolicy->interceptKeyBeforeDispatching(focusedWindowToken, &event, |
| 5753 | entry.policyFlags); |
| 5754 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 5755 | ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms", |
| 5756 | std::to_string(t.duration().count()).c_str()); |
| 5757 | } |
| 5758 | } // acquire lock |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5759 | |
| 5760 | if (delay < 0) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5761 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_SKIP; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5762 | } else if (delay == 0) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5763 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5764 | } else { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5765 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER; |
| 5766 | entry.interceptKeyWakeupTime = now() + delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5767 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5768 | } |
| 5769 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5770 | void InputDispatcher::sendMonitorUnresponsiveCommandLocked(int32_t pid, std::string reason) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5771 | auto command = [this, pid, reason = std::move(reason)]() REQUIRES(mLock) { |
| 5772 | scoped_unlock unlock(mLock); |
| 5773 | mPolicy->notifyMonitorUnresponsive(pid, reason); |
| 5774 | }; |
| 5775 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5776 | } |
| 5777 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5778 | void InputDispatcher::sendWindowUnresponsiveCommandLocked(const sp<IBinder>& token, |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5779 | std::string reason) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5780 | auto command = [this, token, reason = std::move(reason)]() REQUIRES(mLock) { |
| 5781 | scoped_unlock unlock(mLock); |
| 5782 | mPolicy->notifyWindowUnresponsive(token, reason); |
| 5783 | }; |
| 5784 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5785 | } |
| 5786 | |
| 5787 | void InputDispatcher::sendMonitorResponsiveCommandLocked(int32_t pid) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5788 | auto command = [this, pid]() REQUIRES(mLock) { |
| 5789 | scoped_unlock unlock(mLock); |
| 5790 | mPolicy->notifyMonitorResponsive(pid); |
| 5791 | }; |
| 5792 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5793 | } |
| 5794 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5795 | void InputDispatcher::sendWindowResponsiveCommandLocked(const sp<IBinder>& connectionToken) { |
| 5796 | auto command = [this, connectionToken]() REQUIRES(mLock) { |
| 5797 | scoped_unlock unlock(mLock); |
| 5798 | mPolicy->notifyWindowResponsive(connectionToken); |
| 5799 | }; |
| 5800 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5801 | } |
| 5802 | |
| 5803 | /** |
| 5804 | * Tell the policy that a connection has become unresponsive so that it can start ANR. |
| 5805 | * Check whether the connection of interest is a monitor or a window, and add the corresponding |
| 5806 | * command entry to the command queue. |
| 5807 | */ |
| 5808 | void InputDispatcher::processConnectionUnresponsiveLocked(const Connection& connection, |
| 5809 | std::string reason) { |
| 5810 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
| 5811 | if (connection.monitor) { |
| 5812 | ALOGW("Monitor %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 5813 | reason.c_str()); |
| 5814 | std::optional<int32_t> pid = findMonitorPidByTokenLocked(connectionToken); |
| 5815 | if (!pid.has_value()) { |
| 5816 | ALOGE("Could not find unresponsive monitor for connection %s", |
| 5817 | connection.inputChannel->getName().c_str()); |
| 5818 | return; |
| 5819 | } |
| 5820 | sendMonitorUnresponsiveCommandLocked(pid.value(), std::move(reason)); |
| 5821 | return; |
| 5822 | } |
| 5823 | // If not a monitor, must be a window |
| 5824 | ALOGW("Window %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 5825 | reason.c_str()); |
| 5826 | sendWindowUnresponsiveCommandLocked(connectionToken, std::move(reason)); |
| 5827 | } |
| 5828 | |
| 5829 | /** |
| 5830 | * Tell the policy that a connection has become responsive so that it can stop ANR. |
| 5831 | */ |
| 5832 | void InputDispatcher::processConnectionResponsiveLocked(const Connection& connection) { |
| 5833 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
| 5834 | if (connection.monitor) { |
| 5835 | std::optional<int32_t> pid = findMonitorPidByTokenLocked(connectionToken); |
| 5836 | if (!pid.has_value()) { |
| 5837 | ALOGE("Could not find responsive monitor for connection %s", |
| 5838 | connection.inputChannel->getName().c_str()); |
| 5839 | return; |
| 5840 | } |
| 5841 | sendMonitorResponsiveCommandLocked(pid.value()); |
| 5842 | return; |
| 5843 | } |
| 5844 | // If not a monitor, must be a window |
| 5845 | sendWindowResponsiveCommandLocked(connectionToken); |
| 5846 | } |
| 5847 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5848 | bool InputDispatcher::afterKeyEventLockedInterruptable(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5849 | DispatchEntry* dispatchEntry, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5850 | KeyEntry& keyEntry, bool handled) { |
| 5851 | if (keyEntry.flags & AKEY_EVENT_FLAG_FALLBACK) { |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5852 | if (!handled) { |
| 5853 | // Report the key as unhandled, since the fallback was not handled. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5854 | mReporter->reportUnhandledKey(keyEntry.id); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5855 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5856 | return false; |
| 5857 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5858 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5859 | // Get the fallback key state. |
| 5860 | // Clear it out after dispatching the UP. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5861 | int32_t originalKeyCode = keyEntry.keyCode; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5862 | int32_t fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5863 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5864 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 5865 | } |
| 5866 | |
| 5867 | if (handled || !dispatchEntry->hasForegroundTarget()) { |
| 5868 | // If the application handles the original key for which we previously |
| 5869 | // generated a fallback or if the window is not a foreground window, |
| 5870 | // then cancel the associated fallback key, if any. |
| 5871 | if (fallbackKeyCode != -1) { |
| 5872 | // Dispatch the unhandled key to the policy with the cancel flag. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5873 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 5874 | ALOGD("Unhandled key event: Asking policy to cancel fallback action. " |
| 5875 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 5876 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, |
| 5877 | keyEntry.policyFlags); |
| 5878 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5879 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5880 | event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5881 | |
| 5882 | mLock.unlock(); |
| 5883 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5884 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), &event, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5885 | keyEntry.policyFlags, &event); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5886 | |
| 5887 | mLock.lock(); |
| 5888 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5889 | // Cancel the fallback key. |
| 5890 | if (fallbackKeyCode != AKEYCODE_UNKNOWN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5891 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5892 | "application handled the original non-fallback key " |
| 5893 | "or is no longer a foreground target, " |
| 5894 | "canceling previously dispatched fallback key"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5895 | options.keyCode = fallbackKeyCode; |
| 5896 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5897 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5898 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 5899 | } |
| 5900 | } else { |
| 5901 | // If the application did not handle a non-fallback key, first check |
| 5902 | // that we are in a good state to perform unhandled key event processing |
| 5903 | // Then ask the policy what to do with it. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5904 | bool initialDown = keyEntry.action == AKEY_EVENT_ACTION_DOWN && keyEntry.repeatCount == 0; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5905 | if (fallbackKeyCode == -1 && !initialDown) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5906 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 5907 | ALOGD("Unhandled key event: Skipping unhandled key event processing " |
| 5908 | "since this is not an initial down. " |
| 5909 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 5910 | originalKeyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 5911 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5912 | return false; |
| 5913 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5914 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5915 | // Dispatch the unhandled key to the policy. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5916 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 5917 | ALOGD("Unhandled key event: Asking policy to perform fallback action. " |
| 5918 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 5919 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 5920 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5921 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5922 | |
| 5923 | mLock.unlock(); |
| 5924 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5925 | bool fallback = |
| 5926 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5927 | &event, keyEntry.policyFlags, &event); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5928 | |
| 5929 | mLock.lock(); |
| 5930 | |
| 5931 | if (connection->status != Connection::STATUS_NORMAL) { |
| 5932 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 5933 | return false; |
| 5934 | } |
| 5935 | |
| 5936 | // Latch the fallback keycode for this key on an initial down. |
| 5937 | // The fallback keycode cannot change at any other point in the lifecycle. |
| 5938 | if (initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5939 | if (fallback) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5940 | fallbackKeyCode = event.getKeyCode(); |
| 5941 | } else { |
| 5942 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
| 5943 | } |
| 5944 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
| 5945 | } |
| 5946 | |
| 5947 | ALOG_ASSERT(fallbackKeyCode != -1); |
| 5948 | |
| 5949 | // Cancel the fallback key if the policy decides not to send it anymore. |
| 5950 | // We will continue to dispatch the key to the policy but we will no |
| 5951 | // longer dispatch a fallback key to the application. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5952 | if (fallbackKeyCode != AKEYCODE_UNKNOWN && |
| 5953 | (!fallback || fallbackKeyCode != event.getKeyCode())) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5954 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 5955 | if (fallback) { |
| 5956 | ALOGD("Unhandled key event: Policy requested to send key %d" |
| 5957 | "as a fallback for %d, but on the DOWN it had requested " |
| 5958 | "to send %d instead. Fallback canceled.", |
| 5959 | event.getKeyCode(), originalKeyCode, fallbackKeyCode); |
| 5960 | } else { |
| 5961 | ALOGD("Unhandled key event: Policy did not request fallback for %d, " |
| 5962 | "but on the DOWN it had requested to send %d. " |
| 5963 | "Fallback canceled.", |
| 5964 | originalKeyCode, fallbackKeyCode); |
| 5965 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5966 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5967 | |
| 5968 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
| 5969 | "canceling fallback, policy no longer desires it"); |
| 5970 | options.keyCode = fallbackKeyCode; |
| 5971 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
| 5972 | |
| 5973 | fallback = false; |
| 5974 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5975 | if (keyEntry.action != AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5976 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5977 | } |
| 5978 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5979 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5980 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 5981 | { |
| 5982 | std::string msg; |
| 5983 | const KeyedVector<int32_t, int32_t>& fallbackKeys = |
| 5984 | connection->inputState.getFallbackKeys(); |
| 5985 | for (size_t i = 0; i < fallbackKeys.size(); i++) { |
| 5986 | msg += StringPrintf(", %d->%d", fallbackKeys.keyAt(i), fallbackKeys.valueAt(i)); |
| 5987 | } |
| 5988 | ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.", |
| 5989 | fallbackKeys.size(), msg.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5990 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5991 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5992 | |
| 5993 | if (fallback) { |
| 5994 | // Restart the dispatch cycle using the fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5995 | keyEntry.eventTime = event.getEventTime(); |
| 5996 | keyEntry.deviceId = event.getDeviceId(); |
| 5997 | keyEntry.source = event.getSource(); |
| 5998 | keyEntry.displayId = event.getDisplayId(); |
| 5999 | keyEntry.flags = event.getFlags() | AKEY_EVENT_FLAG_FALLBACK; |
| 6000 | keyEntry.keyCode = fallbackKeyCode; |
| 6001 | keyEntry.scanCode = event.getScanCode(); |
| 6002 | keyEntry.metaState = event.getMetaState(); |
| 6003 | keyEntry.repeatCount = event.getRepeatCount(); |
| 6004 | keyEntry.downTime = event.getDownTime(); |
| 6005 | keyEntry.syntheticRepeat = false; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6006 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6007 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6008 | ALOGD("Unhandled key event: Dispatching fallback key. " |
| 6009 | "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x", |
| 6010 | originalKeyCode, fallbackKeyCode, keyEntry.metaState); |
| 6011 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6012 | return true; // restart the event |
| 6013 | } else { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6014 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6015 | ALOGD("Unhandled key event: No fallback key."); |
| 6016 | } |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6017 | |
| 6018 | // Report the key as unhandled, since there is no fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6019 | mReporter->reportUnhandledKey(keyEntry.id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6020 | } |
| 6021 | } |
| 6022 | return false; |
| 6023 | } |
| 6024 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6025 | bool InputDispatcher::afterMotionEventLockedInterruptable(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6026 | DispatchEntry* dispatchEntry, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6027 | MotionEntry& motionEntry, bool handled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6028 | return false; |
| 6029 | } |
| 6030 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6031 | void InputDispatcher::traceInboundQueueLengthLocked() { |
| 6032 | if (ATRACE_ENABLED()) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 6033 | ATRACE_INT("iq", mInboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6034 | } |
| 6035 | } |
| 6036 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6037 | void InputDispatcher::traceOutboundQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6038 | if (ATRACE_ENABLED()) { |
| 6039 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6040 | snprintf(counterName, sizeof(counterName), "oq:%s", connection.getWindowName().c_str()); |
| 6041 | ATRACE_INT(counterName, connection.outboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6042 | } |
| 6043 | } |
| 6044 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6045 | void InputDispatcher::traceWaitQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6046 | if (ATRACE_ENABLED()) { |
| 6047 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6048 | snprintf(counterName, sizeof(counterName), "wq:%s", connection.getWindowName().c_str()); |
| 6049 | ATRACE_INT(counterName, connection.waitQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6050 | } |
| 6051 | } |
| 6052 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6053 | void InputDispatcher::dump(std::string& dump) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6054 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6055 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6056 | dump += "Input Dispatcher State:\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6057 | dumpDispatchStateLocked(dump); |
| 6058 | |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6059 | if (!mLastAnrState.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6060 | dump += "\nInput Dispatcher State at time of last ANR:\n"; |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6061 | dump += mLastAnrState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6062 | } |
| 6063 | } |
| 6064 | |
| 6065 | void InputDispatcher::monitor() { |
| 6066 | // 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] | 6067 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6068 | mLooper->wake(); |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6069 | mDispatcherIsAlive.wait(_l); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6070 | } |
| 6071 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6072 | /** |
| 6073 | * Wake up the dispatcher and wait until it processes all events and commands. |
| 6074 | * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so |
| 6075 | * this method can be safely called from any thread, as long as you've ensured that |
| 6076 | * the work you are interested in completing has already been queued. |
| 6077 | */ |
| 6078 | bool InputDispatcher::waitForIdle() { |
| 6079 | /** |
| 6080 | * Timeout should represent the longest possible time that a device might spend processing |
| 6081 | * events and commands. |
| 6082 | */ |
| 6083 | constexpr std::chrono::duration TIMEOUT = 100ms; |
| 6084 | std::unique_lock lock(mLock); |
| 6085 | mLooper->wake(); |
| 6086 | std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT); |
| 6087 | return result == std::cv_status::no_timeout; |
| 6088 | } |
| 6089 | |
Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 6090 | /** |
| 6091 | * Sets focus to the window identified by the token. This must be called |
| 6092 | * after updating any input window handles. |
| 6093 | * |
| 6094 | * Params: |
| 6095 | * request.token - input channel token used to identify the window that should gain focus. |
| 6096 | * request.focusedToken - the token that the caller expects currently to be focused. If the |
| 6097 | * specified token does not match the currently focused window, this request will be dropped. |
| 6098 | * If the specified focused token matches the currently focused window, the call will succeed. |
| 6099 | * Set this to "null" if this call should succeed no matter what the currently focused token is. |
| 6100 | * request.timestamp - SYSTEM_TIME_MONOTONIC timestamp in nanos set by the client (wm) |
| 6101 | * when requesting the focus change. This determines which request gets |
| 6102 | * precedence if there is a focus change request from another source such as pointer down. |
| 6103 | */ |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6104 | void InputDispatcher::setFocusedWindow(const FocusRequest& request) { |
| 6105 | { // acquire lock |
| 6106 | std::scoped_lock _l(mLock); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6107 | std::optional<FocusResolver::FocusChanges> changes = |
| 6108 | mFocusResolver.setFocusedWindow(request, getWindowHandlesLocked(request.displayId)); |
| 6109 | if (changes) { |
| 6110 | onFocusChangedLocked(*changes); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6111 | } |
| 6112 | } // release lock |
| 6113 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6114 | mLooper->wake(); |
| 6115 | } |
| 6116 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6117 | void InputDispatcher::onFocusChangedLocked(const FocusResolver::FocusChanges& changes) { |
| 6118 | if (changes.oldFocus) { |
| 6119 | std::shared_ptr<InputChannel> focusedInputChannel = getInputChannelLocked(changes.oldFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6120 | if (focusedInputChannel) { |
| 6121 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 6122 | "focus left window"); |
| 6123 | synthesizeCancelationEventsForInputChannelLocked(focusedInputChannel, options); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6124 | enqueueFocusEventLocked(changes.oldFocus, false /*hasFocus*/, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6125 | } |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6126 | } |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6127 | if (changes.newFocus) { |
| 6128 | enqueueFocusEventLocked(changes.newFocus, true /*hasFocus*/, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6129 | } |
| 6130 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6131 | // If a window has pointer capture, then it must have focus. We need to ensure that this |
| 6132 | // contract is upheld when pointer capture is being disabled due to a loss of window focus. |
| 6133 | // If the window loses focus before it loses pointer capture, then the window can be in a state |
| 6134 | // where it has pointer capture but not focus, violating the contract. Therefore we must |
| 6135 | // dispatch the pointer capture event before the focus event. Since focus events are added to |
| 6136 | // the front of the queue (above), we add the pointer capture event to the front of the queue |
| 6137 | // after the focus events are added. This ensures the pointer capture event ends up at the |
| 6138 | // front. |
| 6139 | disablePointerCaptureForcedLocked(); |
| 6140 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6141 | if (mFocusedDisplayId == changes.displayId) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6142 | sendFocusChangedCommandLocked(changes.oldFocus, changes.newFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6143 | } |
| 6144 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6145 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6146 | void InputDispatcher::disablePointerCaptureForcedLocked() { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6147 | if (!mCurrentPointerCaptureRequest.enable && !mWindowTokenWithPointerCapture) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6148 | return; |
| 6149 | } |
| 6150 | |
| 6151 | ALOGD_IF(DEBUG_FOCUS, "Disabling Pointer Capture because the window lost focus."); |
| 6152 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6153 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6154 | setPointerCaptureLocked(false); |
| 6155 | } |
| 6156 | |
| 6157 | if (!mWindowTokenWithPointerCapture) { |
| 6158 | // No need to send capture changes because no window has capture. |
| 6159 | return; |
| 6160 | } |
| 6161 | |
| 6162 | if (mPendingEvent != nullptr) { |
| 6163 | // Move the pending event to the front of the queue. This will give the chance |
| 6164 | // for the pending event to be dropped if it is a captured event. |
| 6165 | mInboundQueue.push_front(mPendingEvent); |
| 6166 | mPendingEvent = nullptr; |
| 6167 | } |
| 6168 | |
| 6169 | auto entry = std::make_unique<PointerCaptureChangedEntry>(mIdGenerator.nextId(), now(), |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6170 | mCurrentPointerCaptureRequest); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6171 | mInboundQueue.push_front(std::move(entry)); |
| 6172 | } |
| 6173 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6174 | void InputDispatcher::setPointerCaptureLocked(bool enable) { |
| 6175 | mCurrentPointerCaptureRequest.enable = enable; |
| 6176 | mCurrentPointerCaptureRequest.seq++; |
| 6177 | auto command = [this, request = mCurrentPointerCaptureRequest]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6178 | scoped_unlock unlock(mLock); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6179 | mPolicy->setPointerCapture(request); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6180 | }; |
| 6181 | postCommandLocked(std::move(command)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6182 | } |
| 6183 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6184 | void InputDispatcher::displayRemoved(int32_t displayId) { |
| 6185 | { // acquire lock |
| 6186 | std::scoped_lock _l(mLock); |
| 6187 | // Set an empty list to remove all handles from the specific display. |
| 6188 | setInputWindowsLocked(/* window handles */ {}, displayId); |
| 6189 | setFocusedApplicationLocked(displayId, nullptr); |
| 6190 | // Call focus resolver to clean up stale requests. This must be called after input windows |
| 6191 | // have been removed for the removed display. |
| 6192 | mFocusResolver.displayRemoved(displayId); |
| 6193 | } // release lock |
| 6194 | |
| 6195 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6196 | mLooper->wake(); |
| 6197 | } |
| 6198 | |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6199 | void InputDispatcher::onWindowInfosChanged(const std::vector<WindowInfo>& windowInfos, |
| 6200 | const std::vector<DisplayInfo>& displayInfos) { |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6201 | // The listener sends the windows as a flattened array. Separate the windows by display for |
| 6202 | // more convenient parsing. |
| 6203 | std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>> handlesPerDisplay; |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6204 | for (const auto& info : windowInfos) { |
| 6205 | handlesPerDisplay.emplace(info.displayId, std::vector<sp<WindowInfoHandle>>()); |
| 6206 | handlesPerDisplay[info.displayId].push_back(new WindowInfoHandle(info)); |
| 6207 | } |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6208 | |
| 6209 | { // acquire lock |
| 6210 | std::scoped_lock _l(mLock); |
| 6211 | mDisplayInfos.clear(); |
| 6212 | for (const auto& displayInfo : displayInfos) { |
| 6213 | mDisplayInfos.emplace(displayInfo.displayId, displayInfo); |
| 6214 | } |
| 6215 | |
| 6216 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 6217 | setInputWindowsLocked(handles, displayId); |
| 6218 | } |
| 6219 | } |
| 6220 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6221 | mLooper->wake(); |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6222 | } |
| 6223 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6224 | bool InputDispatcher::shouldDropInput( |
| 6225 | const EventEntry& entry, const sp<android::gui::WindowInfoHandle>& windowHandle) const { |
| 6226 | if (windowHandle->getInfo()->inputFeatures.test(WindowInfo::Feature::DROP_INPUT) || |
| 6227 | (windowHandle->getInfo()->inputFeatures.test(WindowInfo::Feature::DROP_INPUT_IF_OBSCURED) && |
| 6228 | isWindowObscuredLocked(windowHandle))) { |
| 6229 | ALOGW("Dropping %s event targeting %s as requested by input feature %s on display " |
| 6230 | "%" PRId32 ".", |
| 6231 | ftl::enum_string(entry.type).c_str(), windowHandle->getName().c_str(), |
| 6232 | windowHandle->getInfo()->inputFeatures.string().c_str(), |
| 6233 | windowHandle->getInfo()->displayId); |
| 6234 | return true; |
| 6235 | } |
| 6236 | return false; |
| 6237 | } |
| 6238 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 6239 | } // namespace android::inputdispatcher |