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 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 22 | #include <android-base/chrono_utils.h> |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 23 | #include <android-base/logging.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> |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 28 | #include <ftl/enum.h> |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 29 | #if defined(__ANDROID__) |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 30 | #include <gui/SurfaceComposerClient.h> |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 31 | #endif |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 32 | #include <input/InputDevice.h> |
Siarhei Vishniakou | 6e1e987 | 2022-11-08 17:51:35 -0800 | [diff] [blame] | 33 | #include <input/PrintTools.h> |
tyiu | 1573a67 | 2023-02-21 22:38:32 +0000 | [diff] [blame] | 34 | #include <openssl/mem.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" |
Arthur Hung | 1a1007b | 2022-05-11 07:15:01 +0000 | [diff] [blame] | 48 | #include "DebugConfig.h" |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 49 | #include "InputDispatcher.h" |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 50 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 51 | #define INDENT " " |
| 52 | #define INDENT2 " " |
| 53 | #define INDENT3 " " |
| 54 | #define INDENT4 " " |
| 55 | |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 56 | using namespace android::ftl::flag_operators; |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 57 | using android::base::Error; |
Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 58 | using android::base::HwTimeoutMultiplier; |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 59 | using android::base::Result; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 60 | using android::base::StringPrintf; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 61 | using android::gui::DisplayInfo; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 62 | using android::gui::FocusRequest; |
| 63 | using android::gui::TouchOcclusionMode; |
| 64 | using android::gui::WindowInfo; |
| 65 | using android::gui::WindowInfoHandle; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 66 | using android::os::InputEventInjectionResult; |
| 67 | using android::os::InputEventInjectionSync; |
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 { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 72 | // Temporarily releases a held mutex for the lifetime of the instance. |
| 73 | // Named to match std::scoped_lock |
| 74 | class scoped_unlock { |
| 75 | public: |
| 76 | explicit scoped_unlock(std::mutex& mutex) : mMutex(mutex) { mMutex.unlock(); } |
| 77 | ~scoped_unlock() { mMutex.lock(); } |
| 78 | |
| 79 | private: |
| 80 | std::mutex& mMutex; |
| 81 | }; |
| 82 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 83 | // Default input dispatching timeout if there is no focused application or paused window |
| 84 | // from which to determine an appropriate dispatching timeout. |
Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 85 | const std::chrono::duration DEFAULT_INPUT_DISPATCHING_TIMEOUT = std::chrono::milliseconds( |
| 86 | android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS * |
| 87 | HwTimeoutMultiplier()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 88 | |
| 89 | // Amount of time to allow for all pending events to be processed when an app switch |
| 90 | // key is on the way. This is used to preempt input dispatch and drop input events |
| 91 | // 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] | 92 | constexpr nsecs_t APP_SWITCH_TIMEOUT = 500 * 1000000LL; // 0.5sec |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 93 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 94 | const std::chrono::duration STALE_EVENT_TIMEOUT = std::chrono::seconds(10) * HwTimeoutMultiplier(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 95 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 96 | // 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] | 97 | constexpr nsecs_t SLOW_EVENT_PROCESSING_WARNING_TIMEOUT = 2000 * 1000000LL; // 2sec |
| 98 | |
| 99 | // Log a warning when an interception call takes longer than this to process. |
| 100 | constexpr std::chrono::milliseconds SLOW_INTERCEPTION_THRESHOLD = 50ms; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 101 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 102 | // Additional key latency in case a connection is still processing some motion events. |
| 103 | // This will help with the case when a user touched a button that opens a new window, |
| 104 | // and gives us the chance to dispatch the key to this new window. |
| 105 | constexpr std::chrono::nanoseconds KEY_WAITING_FOR_EVENTS_TIMEOUT = 500ms; |
| 106 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 107 | // Number of recent events to keep for debugging purposes. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 108 | constexpr size_t RECENT_QUEUE_MAX_SIZE = 10; |
| 109 | |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 110 | // Event log tags. See EventLogTags.logtags for reference. |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 111 | constexpr int LOGTAG_INPUT_INTERACTION = 62000; |
| 112 | constexpr int LOGTAG_INPUT_FOCUS = 62001; |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 113 | constexpr int LOGTAG_INPUT_CANCEL = 62003; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 114 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 115 | const ui::Transform kIdentityTransform; |
| 116 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 117 | inline nsecs_t now() { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 118 | return systemTime(SYSTEM_TIME_MONOTONIC); |
| 119 | } |
| 120 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 121 | inline const std::string binderToString(const sp<IBinder>& binder) { |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 122 | if (binder == nullptr) { |
| 123 | return "<null>"; |
| 124 | } |
| 125 | return StringPrintf("%p", binder.get()); |
| 126 | } |
| 127 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 128 | inline int32_t getMotionEventActionPointerIndex(int32_t action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 129 | return (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> |
| 130 | AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 133 | Result<void> checkKeyAction(int32_t action) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 134 | switch (action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 135 | case AKEY_EVENT_ACTION_DOWN: |
| 136 | case AKEY_EVENT_ACTION_UP: |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 137 | return {}; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 138 | default: |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 139 | return Error() << "Key event has invalid action code " << action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 143 | Result<void> validateKeyEvent(int32_t action) { |
| 144 | return checkKeyAction(action); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 145 | } |
| 146 | |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 147 | Result<void> checkMotionAction(int32_t action, int32_t actionButton, int32_t pointerCount) { |
Siarhei Vishniakou | 2f61bdc | 2022-12-02 08:55:51 -0800 | [diff] [blame] | 148 | switch (MotionEvent::getActionMasked(action)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 149 | case AMOTION_EVENT_ACTION_DOWN: |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 150 | case AMOTION_EVENT_ACTION_UP: { |
| 151 | if (pointerCount != 1) { |
| 152 | return Error() << "invalid pointer count " << pointerCount; |
| 153 | } |
| 154 | return {}; |
| 155 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 156 | case AMOTION_EVENT_ACTION_MOVE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 157 | case AMOTION_EVENT_ACTION_HOVER_ENTER: |
| 158 | case AMOTION_EVENT_ACTION_HOVER_MOVE: |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 159 | case AMOTION_EVENT_ACTION_HOVER_EXIT: { |
| 160 | if (pointerCount < 1) { |
| 161 | return Error() << "invalid pointer count " << pointerCount; |
| 162 | } |
| 163 | return {}; |
| 164 | } |
Siarhei Vishniakou | 2f61bdc | 2022-12-02 08:55:51 -0800 | [diff] [blame] | 165 | case AMOTION_EVENT_ACTION_CANCEL: |
| 166 | case AMOTION_EVENT_ACTION_OUTSIDE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 167 | case AMOTION_EVENT_ACTION_SCROLL: |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 168 | return {}; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 169 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 170 | case AMOTION_EVENT_ACTION_POINTER_UP: { |
Siarhei Vishniakou | 2f61bdc | 2022-12-02 08:55:51 -0800 | [diff] [blame] | 171 | const int32_t index = MotionEvent::getActionIndex(action); |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 172 | if (index < 0) { |
| 173 | return Error() << "invalid index " << index << " for " |
| 174 | << MotionEvent::actionToString(action); |
| 175 | } |
| 176 | if (index >= pointerCount) { |
| 177 | return Error() << "invalid index " << index << " for pointerCount " << pointerCount; |
| 178 | } |
| 179 | if (pointerCount <= 1) { |
| 180 | return Error() << "invalid pointer count " << pointerCount << " for " |
| 181 | << MotionEvent::actionToString(action); |
| 182 | } |
| 183 | return {}; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 184 | } |
| 185 | case AMOTION_EVENT_ACTION_BUTTON_PRESS: |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 186 | case AMOTION_EVENT_ACTION_BUTTON_RELEASE: { |
| 187 | if (actionButton == 0) { |
| 188 | return Error() << "action button should be nonzero for " |
| 189 | << MotionEvent::actionToString(action); |
| 190 | } |
| 191 | return {}; |
| 192 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 193 | default: |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 194 | return Error() << "invalid action " << action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 198 | int64_t millis(std::chrono::nanoseconds t) { |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 199 | return std::chrono::duration_cast<std::chrono::milliseconds>(t).count(); |
| 200 | } |
| 201 | |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 202 | Result<void> validateMotionEvent(int32_t action, int32_t actionButton, size_t pointerCount, |
| 203 | const PointerProperties* pointerProperties) { |
| 204 | Result<void> actionCheck = checkMotionAction(action, actionButton, pointerCount); |
| 205 | if (!actionCheck.ok()) { |
| 206 | return actionCheck; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 207 | } |
| 208 | if (pointerCount < 1 || pointerCount > MAX_POINTERS) { |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 209 | return Error() << "Motion event has invalid pointer count " << pointerCount |
| 210 | << "; value must be between 1 and " << MAX_POINTERS << "."; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 211 | } |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 212 | std::bitset<MAX_POINTER_ID + 1> pointerIdBits; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 213 | for (size_t i = 0; i < pointerCount; i++) { |
| 214 | int32_t id = pointerProperties[i].id; |
| 215 | if (id < 0 || id > MAX_POINTER_ID) { |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 216 | return Error() << "Motion event has invalid pointer id " << id |
| 217 | << "; value must be between 0 and " << MAX_POINTER_ID; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 218 | } |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 219 | if (pointerIdBits.test(id)) { |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 220 | return Error() << "Motion event has duplicate pointer id " << id; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 221 | } |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 222 | pointerIdBits.set(id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 223 | } |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 224 | return {}; |
| 225 | } |
| 226 | |
| 227 | Result<void> validateInputEvent(const InputEvent& event) { |
| 228 | switch (event.getType()) { |
| 229 | case InputEventType::KEY: { |
| 230 | const KeyEvent& key = static_cast<const KeyEvent&>(event); |
| 231 | const int32_t action = key.getAction(); |
| 232 | return validateKeyEvent(action); |
| 233 | } |
| 234 | case InputEventType::MOTION: { |
| 235 | const MotionEvent& motion = static_cast<const MotionEvent&>(event); |
| 236 | const int32_t action = motion.getAction(); |
| 237 | const size_t pointerCount = motion.getPointerCount(); |
| 238 | const PointerProperties* pointerProperties = motion.getPointerProperties(); |
| 239 | const int32_t actionButton = motion.getActionButton(); |
| 240 | return validateMotionEvent(action, actionButton, pointerCount, pointerProperties); |
| 241 | } |
| 242 | default: { |
| 243 | return {}; |
| 244 | } |
| 245 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 246 | } |
| 247 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 248 | std::string dumpRegion(const Region& region) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 249 | if (region.isEmpty()) { |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 250 | return "<empty>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 251 | } |
| 252 | |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 253 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 254 | bool first = true; |
| 255 | Region::const_iterator cur = region.begin(); |
| 256 | Region::const_iterator const tail = region.end(); |
| 257 | while (cur != tail) { |
| 258 | if (first) { |
| 259 | first = false; |
| 260 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 261 | dump += "|"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 262 | } |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 263 | 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] | 264 | cur++; |
| 265 | } |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 266 | return dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 267 | } |
| 268 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 269 | std::string dumpQueue(const std::deque<DispatchEntry*>& queue, nsecs_t currentTime) { |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 270 | constexpr size_t maxEntries = 50; // max events to print |
| 271 | constexpr size_t skipBegin = maxEntries / 2; |
| 272 | const size_t skipEnd = queue.size() - maxEntries / 2; |
| 273 | // skip from maxEntries / 2 ... size() - maxEntries/2 |
| 274 | // only print from 0 .. skipBegin and then from skipEnd .. size() |
| 275 | |
| 276 | std::string dump; |
| 277 | for (size_t i = 0; i < queue.size(); i++) { |
| 278 | const DispatchEntry& entry = *queue[i]; |
| 279 | if (i >= skipBegin && i < skipEnd) { |
| 280 | dump += StringPrintf(INDENT4 "<skipped %zu entries>\n", skipEnd - skipBegin); |
| 281 | i = skipEnd - 1; // it will be incremented to "skipEnd" by 'continue' |
| 282 | continue; |
| 283 | } |
| 284 | dump.append(INDENT4); |
| 285 | dump += entry.eventEntry->getDescription(); |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 286 | dump += StringPrintf(", seq=%" PRIu32 ", targetFlags=%s, resolvedAction=%d, age=%" PRId64 |
| 287 | "ms", |
| 288 | entry.seq, entry.targetFlags.string().c_str(), entry.resolvedAction, |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 289 | ns2ms(currentTime - entry.eventEntry->eventTime)); |
| 290 | if (entry.deliveryTime != 0) { |
| 291 | // This entry was delivered, so add information on how long we've been waiting |
| 292 | dump += StringPrintf(", wait=%" PRId64 "ms", ns2ms(currentTime - entry.deliveryTime)); |
| 293 | } |
| 294 | dump.append("\n"); |
| 295 | } |
| 296 | return dump; |
| 297 | } |
| 298 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 299 | /** |
| 300 | * Find the entry in std::unordered_map by key, and return it. |
| 301 | * If the entry is not found, return a default constructed entry. |
| 302 | * |
| 303 | * Useful when the entries are vectors, since an empty vector will be returned |
| 304 | * if the entry is not found. |
| 305 | * Also useful when the entries are sp<>. If an entry is not found, nullptr is returned. |
| 306 | */ |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 307 | template <typename K, typename V> |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 308 | V getValueByKey(const std::unordered_map<K, V>& map, K key) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 309 | auto it = map.find(key); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 310 | return it != map.end() ? it->second : V{}; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 311 | } |
| 312 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 313 | bool haveSameToken(const sp<WindowInfoHandle>& first, const sp<WindowInfoHandle>& second) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 314 | if (first == second) { |
| 315 | return true; |
| 316 | } |
| 317 | |
| 318 | if (first == nullptr || second == nullptr) { |
| 319 | return false; |
| 320 | } |
| 321 | |
| 322 | return first->getToken() == second->getToken(); |
| 323 | } |
| 324 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 325 | bool haveSameApplicationToken(const WindowInfo* first, const WindowInfo* second) { |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 326 | if (first == nullptr || second == nullptr) { |
| 327 | return false; |
| 328 | } |
| 329 | return first->applicationInfo.token != nullptr && |
| 330 | first->applicationInfo.token == second->applicationInfo.token; |
| 331 | } |
| 332 | |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 333 | template <typename T> |
| 334 | size_t firstMarkedBit(T set) { |
| 335 | // TODO: replace with std::countr_zero from <bit> when that's available |
| 336 | LOG_ALWAYS_FATAL_IF(set.none()); |
| 337 | size_t i = 0; |
| 338 | while (!set.test(i)) { |
| 339 | i++; |
| 340 | } |
| 341 | return i; |
| 342 | } |
| 343 | |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 344 | std::unique_ptr<DispatchEntry> createDispatchEntry( |
| 345 | const InputTarget& inputTarget, std::shared_ptr<EventEntry> eventEntry, |
| 346 | ftl::Flags<InputTarget::Flags> inputTargetFlags) { |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 347 | if (inputTarget.useDefaultPointerTransform()) { |
| 348 | const ui::Transform& transform = inputTarget.getDefaultPointerTransform(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 349 | return std::make_unique<DispatchEntry>(eventEntry, inputTargetFlags, transform, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 350 | inputTarget.displayTransform, |
| 351 | inputTarget.globalScaleFactor); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | ALOG_ASSERT(eventEntry->type == EventEntry::Type::MOTION); |
| 355 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); |
| 356 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 357 | std::vector<PointerCoords> pointerCoords; |
| 358 | pointerCoords.resize(motionEntry.pointerCount); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 359 | |
| 360 | // Use the first pointer information to normalize all other pointers. This could be any pointer |
| 361 | // 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] | 362 | // uses the transform for the normalized pointer. |
| 363 | const ui::Transform& firstPointerTransform = |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 364 | inputTarget.pointerTransforms[firstMarkedBit(inputTarget.pointerIds)]; |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 365 | ui::Transform inverseFirstTransform = firstPointerTransform.inverse(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 366 | |
| 367 | // Iterate through all pointers in the event to normalize against the first. |
| 368 | for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.pointerCount; pointerIndex++) { |
| 369 | const PointerProperties& pointerProperties = motionEntry.pointerProperties[pointerIndex]; |
| 370 | uint32_t pointerId = uint32_t(pointerProperties.id); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 371 | const ui::Transform& currTransform = inputTarget.pointerTransforms[pointerId]; |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 372 | |
| 373 | pointerCoords[pointerIndex].copyFrom(motionEntry.pointerCoords[pointerIndex]); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 374 | // First, apply the current pointer's transform to update the coordinates into |
| 375 | // window space. |
| 376 | pointerCoords[pointerIndex].transform(currTransform); |
| 377 | // Next, apply the inverse transform of the normalized coordinates so the |
| 378 | // current coordinates are transformed into the normalized coordinate space. |
| 379 | pointerCoords[pointerIndex].transform(inverseFirstTransform); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 382 | std::unique_ptr<MotionEntry> combinedMotionEntry = |
| 383 | std::make_unique<MotionEntry>(motionEntry.id, motionEntry.eventTime, |
| 384 | motionEntry.deviceId, motionEntry.source, |
| 385 | motionEntry.displayId, motionEntry.policyFlags, |
| 386 | motionEntry.action, motionEntry.actionButton, |
| 387 | motionEntry.flags, motionEntry.metaState, |
| 388 | motionEntry.buttonState, motionEntry.classification, |
| 389 | motionEntry.edgeFlags, motionEntry.xPrecision, |
| 390 | motionEntry.yPrecision, motionEntry.xCursorPosition, |
| 391 | motionEntry.yCursorPosition, motionEntry.downTime, |
| 392 | motionEntry.pointerCount, motionEntry.pointerProperties, |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 393 | pointerCoords.data()); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 394 | |
| 395 | if (motionEntry.injectionState) { |
| 396 | combinedMotionEntry->injectionState = motionEntry.injectionState; |
| 397 | combinedMotionEntry->injectionState->refCount += 1; |
| 398 | } |
| 399 | |
| 400 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 401 | std::make_unique<DispatchEntry>(std::move(combinedMotionEntry), inputTargetFlags, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 402 | firstPointerTransform, inputTarget.displayTransform, |
| 403 | inputTarget.globalScaleFactor); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 404 | return dispatchEntry; |
| 405 | } |
| 406 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 407 | status_t openInputChannelPair(const std::string& name, std::shared_ptr<InputChannel>& serverChannel, |
| 408 | std::unique_ptr<InputChannel>& clientChannel) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 409 | std::unique_ptr<InputChannel> uniqueServerChannel; |
| 410 | status_t result = InputChannel::openInputChannelPair(name, uniqueServerChannel, clientChannel); |
| 411 | |
| 412 | serverChannel = std::move(uniqueServerChannel); |
| 413 | return result; |
| 414 | } |
| 415 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 416 | template <typename T> |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 417 | 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] | 418 | if (lhs == nullptr && rhs == nullptr) { |
| 419 | return true; |
| 420 | } |
| 421 | if (lhs == nullptr || rhs == nullptr) { |
| 422 | return false; |
| 423 | } |
| 424 | return *lhs == *rhs; |
| 425 | } |
| 426 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 427 | KeyEvent createKeyEvent(const KeyEntry& entry) { |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 428 | KeyEvent event; |
| 429 | event.initialize(entry.id, entry.deviceId, entry.source, entry.displayId, INVALID_HMAC, |
| 430 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, |
| 431 | entry.repeatCount, entry.downTime, entry.eventTime); |
| 432 | return event; |
| 433 | } |
| 434 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 435 | bool shouldReportMetricsForConnection(const Connection& connection) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 436 | // Do not keep track of gesture monitors. They receive every event and would disproportionately |
| 437 | // affect the statistics. |
| 438 | if (connection.monitor) { |
| 439 | return false; |
| 440 | } |
| 441 | // If the connection is experiencing ANR, let's skip it. We have separate ANR metrics |
| 442 | if (!connection.responsive) { |
| 443 | return false; |
| 444 | } |
| 445 | return true; |
| 446 | } |
| 447 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 448 | bool shouldReportFinishedEvent(const DispatchEntry& dispatchEntry, const Connection& connection) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 449 | const EventEntry& eventEntry = *dispatchEntry.eventEntry; |
| 450 | const int32_t& inputEventId = eventEntry.id; |
| 451 | if (inputEventId != dispatchEntry.resolvedEventId) { |
| 452 | // Event was transmuted |
| 453 | return false; |
| 454 | } |
| 455 | if (inputEventId == android::os::IInputConstants::INVALID_INPUT_EVENT_ID) { |
| 456 | return false; |
| 457 | } |
| 458 | // Only track latency for events that originated from hardware |
| 459 | if (eventEntry.isSynthesized()) { |
| 460 | return false; |
| 461 | } |
| 462 | const EventEntry::Type& inputEventEntryType = eventEntry.type; |
| 463 | if (inputEventEntryType == EventEntry::Type::KEY) { |
| 464 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 465 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
| 466 | return false; |
| 467 | } |
| 468 | } else if (inputEventEntryType == EventEntry::Type::MOTION) { |
| 469 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 470 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL || |
| 471 | motionEntry.action == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 472 | return false; |
| 473 | } |
| 474 | } else { |
| 475 | // Not a key or a motion |
| 476 | return false; |
| 477 | } |
| 478 | if (!shouldReportMetricsForConnection(connection)) { |
| 479 | return false; |
| 480 | } |
| 481 | return true; |
| 482 | } |
| 483 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 484 | /** |
| 485 | * Connection is responsive if it has no events in the waitQueue that are older than the |
| 486 | * current time. |
| 487 | */ |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 488 | bool isConnectionResponsive(const Connection& connection) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 489 | const nsecs_t currentTime = now(); |
| 490 | for (const DispatchEntry* entry : connection.waitQueue) { |
| 491 | if (entry->timeoutTime < currentTime) { |
| 492 | return false; |
| 493 | } |
| 494 | } |
| 495 | return true; |
| 496 | } |
| 497 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 498 | // Returns true if the event type passed as argument represents a user activity. |
| 499 | bool isUserActivityEvent(const EventEntry& eventEntry) { |
| 500 | switch (eventEntry.type) { |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 501 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 502 | case EventEntry::Type::DEVICE_RESET: |
| 503 | case EventEntry::Type::DRAG: |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 504 | case EventEntry::Type::FOCUS: |
| 505 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 506 | case EventEntry::Type::SENSOR: |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 507 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 508 | return false; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 509 | case EventEntry::Type::KEY: |
| 510 | case EventEntry::Type::MOTION: |
| 511 | return true; |
| 512 | } |
| 513 | } |
| 514 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 515 | // Returns true if the given window can accept pointer events at the given display location. |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 516 | bool windowAcceptsTouchAt(const WindowInfo& windowInfo, int32_t displayId, float x, float y, |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 517 | bool isStylus, const ui::Transform& displayTransform) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 518 | const auto inputConfig = windowInfo.inputConfig; |
| 519 | if (windowInfo.displayId != displayId || |
| 520 | inputConfig.test(WindowInfo::InputConfig::NOT_VISIBLE)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 521 | return false; |
| 522 | } |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 523 | const bool windowCanInterceptTouch = isStylus && windowInfo.interceptsStylus(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 524 | if (inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE) && !windowCanInterceptTouch) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 525 | return false; |
| 526 | } |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 527 | |
| 528 | // Window Manager works in the logical display coordinate space. When it specifies bounds for a |
| 529 | // window as (l, t, r, b), the range of x in [l, r) and y in [t, b) are considered to be inside |
| 530 | // the window. Points on the right and bottom edges should not be inside the window, so we need |
| 531 | // to be careful about performing a hit test when the display is rotated, since the "right" and |
| 532 | // "bottom" of the window will be different in the display (un-rotated) space compared to in the |
| 533 | // logical display in which WM determined the bounds. Perform the hit test in the logical |
| 534 | // display space to ensure these edges are considered correctly in all orientations. |
| 535 | const auto touchableRegion = displayTransform.transform(windowInfo.touchableRegion); |
| 536 | const auto p = displayTransform.transform(x, y); |
| 537 | if (!touchableRegion.contains(std::floor(p.x), std::floor(p.y))) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 538 | return false; |
| 539 | } |
| 540 | return true; |
| 541 | } |
| 542 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 543 | bool isPointerFromStylus(const MotionEntry& entry, int32_t pointerIndex) { |
| 544 | return isFromSource(entry.source, AINPUT_SOURCE_STYLUS) && |
Prabir Pradhan | e562696 | 2022-10-27 20:30:53 +0000 | [diff] [blame] | 545 | isStylusToolType(entry.pointerProperties[pointerIndex].toolType); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 546 | } |
| 547 | |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 548 | // Determines if the given window can be targeted as InputTarget::Flags::FOREGROUND. |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 549 | // Foreground events are only sent to "foreground targetable" windows, but not all gestures sent to |
| 550 | // such window are necessarily targeted with the flag. For example, an event with ACTION_OUTSIDE can |
| 551 | // be sent to such a window, but it is not a foreground event and doesn't use |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 552 | // InputTarget::Flags::FOREGROUND. |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 553 | bool canReceiveForegroundTouches(const WindowInfo& info) { |
| 554 | // A non-touchable window can still receive touch events (e.g. in the case of |
| 555 | // STYLUS_INTERCEPTOR), so prevent such windows from receiving foreground events for touches. |
| 556 | return !info.inputConfig.test(gui::WindowInfo::InputConfig::NOT_TOUCHABLE) && !info.isSpy(); |
| 557 | } |
| 558 | |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 559 | bool isWindowOwnedBy(const sp<WindowInfoHandle>& windowHandle, int32_t pid, int32_t uid) { |
| 560 | if (windowHandle == nullptr) { |
| 561 | return false; |
| 562 | } |
| 563 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 564 | if (pid == windowInfo->ownerPid && uid == windowInfo->ownerUid) { |
| 565 | return true; |
| 566 | } |
| 567 | return false; |
| 568 | } |
| 569 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 570 | // Checks targeted injection using the window's owner's uid. |
| 571 | // Returns an empty string if an entry can be sent to the given window, or an error message if the |
| 572 | // entry is a targeted injection whose uid target doesn't match the window owner. |
| 573 | std::optional<std::string> verifyTargetedInjection(const sp<WindowInfoHandle>& window, |
| 574 | const EventEntry& entry) { |
| 575 | if (entry.injectionState == nullptr || !entry.injectionState->targetUid) { |
| 576 | // The event was not injected, or the injected event does not target a window. |
| 577 | return {}; |
| 578 | } |
| 579 | const int32_t uid = *entry.injectionState->targetUid; |
| 580 | if (window == nullptr) { |
| 581 | return StringPrintf("No valid window target for injection into uid %d.", uid); |
| 582 | } |
| 583 | if (entry.injectionState->targetUid != window->getInfo()->ownerUid) { |
| 584 | return StringPrintf("Injected event targeted at uid %d would be dispatched to window '%s' " |
| 585 | "owned by uid %d.", |
| 586 | uid, window->getName().c_str(), window->getInfo()->ownerUid); |
| 587 | } |
| 588 | return {}; |
| 589 | } |
| 590 | |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 591 | std::pair<float, float> resolveTouchedPosition(const MotionEntry& entry) { |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 592 | const bool isFromMouse = isFromSource(entry.source, AINPUT_SOURCE_MOUSE); |
| 593 | // Always dispatch mouse events to cursor position. |
| 594 | if (isFromMouse) { |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 595 | return {entry.xCursorPosition, entry.yCursorPosition}; |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | const int32_t pointerIndex = getMotionEventActionPointerIndex(entry.action); |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 599 | return {entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 600 | entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_Y)}; |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 601 | } |
| 602 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 603 | std::optional<nsecs_t> getDownTime(const EventEntry& eventEntry) { |
| 604 | if (eventEntry.type == EventEntry::Type::KEY) { |
| 605 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 606 | return keyEntry.downTime; |
| 607 | } else if (eventEntry.type == EventEntry::Type::MOTION) { |
| 608 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 609 | return motionEntry.downTime; |
| 610 | } |
| 611 | return std::nullopt; |
| 612 | } |
| 613 | |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 614 | /** |
| 615 | * Compare the old touch state to the new touch state, and generate the corresponding touched |
| 616 | * windows (== input targets). |
| 617 | * If a window had the hovering pointer, but now it doesn't, produce HOVER_EXIT for that window. |
| 618 | * If the pointer just entered the new window, produce HOVER_ENTER. |
| 619 | * For pointers remaining in the window, produce HOVER_MOVE. |
| 620 | */ |
| 621 | std::vector<TouchedWindow> getHoveringWindowsLocked(const TouchState* oldState, |
| 622 | const TouchState& newTouchState, |
| 623 | const MotionEntry& entry) { |
| 624 | std::vector<TouchedWindow> out; |
| 625 | const int32_t maskedAction = MotionEvent::getActionMasked(entry.action); |
| 626 | if (maskedAction != AMOTION_EVENT_ACTION_HOVER_ENTER && |
| 627 | maskedAction != AMOTION_EVENT_ACTION_HOVER_MOVE && |
| 628 | maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 629 | // Not a hover event - don't need to do anything |
| 630 | return out; |
| 631 | } |
| 632 | |
| 633 | // We should consider all hovering pointers here. But for now, just use the first one |
| 634 | const int32_t pointerId = entry.pointerProperties[0].id; |
| 635 | |
| 636 | std::set<sp<WindowInfoHandle>> oldWindows; |
| 637 | if (oldState != nullptr) { |
| 638 | oldWindows = oldState->getWindowsWithHoveringPointer(entry.deviceId, pointerId); |
| 639 | } |
| 640 | |
| 641 | std::set<sp<WindowInfoHandle>> newWindows = |
| 642 | newTouchState.getWindowsWithHoveringPointer(entry.deviceId, pointerId); |
| 643 | |
| 644 | // If the pointer is no longer in the new window set, send HOVER_EXIT. |
| 645 | for (const sp<WindowInfoHandle>& oldWindow : oldWindows) { |
| 646 | if (newWindows.find(oldWindow) == newWindows.end()) { |
| 647 | TouchedWindow touchedWindow; |
| 648 | touchedWindow.windowHandle = oldWindow; |
| 649 | touchedWindow.targetFlags = InputTarget::Flags::DISPATCH_AS_HOVER_EXIT; |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 650 | touchedWindow.pointerIds.set(pointerId); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 651 | out.push_back(touchedWindow); |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | for (const sp<WindowInfoHandle>& newWindow : newWindows) { |
| 656 | TouchedWindow touchedWindow; |
| 657 | touchedWindow.windowHandle = newWindow; |
| 658 | if (oldWindows.find(newWindow) == oldWindows.end()) { |
| 659 | // Any windows that have this pointer now, and didn't have it before, should get |
| 660 | // HOVER_ENTER |
| 661 | touchedWindow.targetFlags = InputTarget::Flags::DISPATCH_AS_HOVER_ENTER; |
| 662 | } else { |
| 663 | // This pointer was already sent to the window. Use ACTION_HOVER_MOVE. |
Siarhei Vishniakou | c2eb850 | 2023-04-11 18:33:36 -0700 | [diff] [blame] | 664 | if (CC_UNLIKELY(maskedAction != AMOTION_EVENT_ACTION_HOVER_MOVE)) { |
| 665 | LOG(FATAL) << "Expected ACTION_HOVER_MOVE instead of " << entry.getDescription(); |
| 666 | } |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 667 | touchedWindow.targetFlags = InputTarget::Flags::DISPATCH_AS_IS; |
| 668 | } |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 669 | touchedWindow.pointerIds.set(pointerId); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 670 | if (canReceiveForegroundTouches(*newWindow->getInfo())) { |
| 671 | touchedWindow.targetFlags |= InputTarget::Flags::FOREGROUND; |
| 672 | } |
| 673 | out.push_back(touchedWindow); |
| 674 | } |
| 675 | return out; |
| 676 | } |
| 677 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 678 | template <typename T> |
| 679 | std::vector<T>& operator+=(std::vector<T>& left, const std::vector<T>& right) { |
| 680 | left.insert(left.end(), right.begin(), right.end()); |
| 681 | return left; |
| 682 | } |
| 683 | |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 684 | // Filter windows in a TouchState and targets in a vector to remove untrusted windows/targets from |
| 685 | // both. |
| 686 | void filterUntrustedTargets(TouchState& touchState, std::vector<InputTarget>& targets) { |
| 687 | std::erase_if(touchState.windows, [&](const TouchedWindow& window) { |
| 688 | if (!window.windowHandle->getInfo()->inputConfig.test( |
| 689 | WindowInfo::InputConfig::TRUSTED_OVERLAY)) { |
| 690 | // In addition to TouchState, erase this window from the input targets! We don't have a |
| 691 | // good way to do this today except by adding a nested loop. |
| 692 | // TODO(b/282025641): simplify this code once InputTargets are being identified |
| 693 | // separately from TouchedWindows. |
| 694 | std::erase_if(targets, [&](const InputTarget& target) { |
| 695 | return target.inputChannel->getConnectionToken() == window.windowHandle->getToken(); |
| 696 | }); |
| 697 | return true; |
| 698 | } |
| 699 | return false; |
| 700 | }); |
| 701 | } |
| 702 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 703 | } // namespace |
| 704 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 705 | // --- InputDispatcher --- |
| 706 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 707 | InputDispatcher::InputDispatcher(InputDispatcherPolicyInterface& policy) |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 708 | : InputDispatcher(policy, STALE_EVENT_TIMEOUT) {} |
| 709 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 710 | InputDispatcher::InputDispatcher(InputDispatcherPolicyInterface& policy, |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 711 | std::chrono::nanoseconds staleEventTimeout) |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 712 | : mPolicy(policy), |
| 713 | mPendingEvent(nullptr), |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 714 | mLastDropReason(DropReason::NOT_DROPPED), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 715 | mIdGenerator(IdGenerator::Source::INPUT_DISPATCHER), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 716 | mAppSwitchSawKeyDown(false), |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 717 | mAppSwitchDueTime(LLONG_MAX), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 718 | mNextUnblockedEvent(nullptr), |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 719 | mMonitorDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 720 | mDispatchEnabled(false), |
| 721 | mDispatchFrozen(false), |
| 722 | mInputFilterEnabled(false), |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 723 | mMaximumObscuringOpacityForTouch(1.0f), |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 724 | mFocusedDisplayId(ADISPLAY_ID_DEFAULT), |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 725 | mWindowTokenWithPointerCapture(nullptr), |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 726 | mStaleEventTimeout(staleEventTimeout), |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 727 | mLatencyAggregator(), |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 728 | mLatencyTracker(&mLatencyAggregator) { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 729 | mLooper = sp<Looper>::make(false); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 730 | mReporter = createInputReporter(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 731 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 732 | mWindowInfoListener = sp<DispatcherWindowListener>::make(*this); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 733 | #if defined(__ANDROID__) |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 734 | SurfaceComposerClient::getDefault()->addWindowInfosListener(mWindowInfoListener); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 735 | #endif |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 736 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | InputDispatcher::~InputDispatcher() { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 740 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 741 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 742 | resetKeyRepeatLocked(); |
| 743 | releasePendingEventLocked(); |
| 744 | drainInboundQueueLocked(); |
| 745 | mCommandQueue.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 746 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 747 | while (!mConnectionsByToken.empty()) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 748 | std::shared_ptr<Connection> connection = mConnectionsByToken.begin()->second; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 749 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), /*notify=*/false); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 750 | } |
| 751 | } |
| 752 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 753 | status_t InputDispatcher::start() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 754 | if (mThread) { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 755 | return ALREADY_EXISTS; |
| 756 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 757 | mThread = std::make_unique<InputThread>( |
| 758 | "InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); }); |
| 759 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | status_t InputDispatcher::stop() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 763 | if (mThread && mThread->isCallingThread()) { |
| 764 | ALOGE("InputDispatcher cannot be stopped from its own thread!"); |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 765 | return INVALID_OPERATION; |
| 766 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 767 | mThread.reset(); |
| 768 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 769 | } |
| 770 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 771 | void InputDispatcher::dispatchOnce() { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 772 | nsecs_t nextWakeupTime = LLONG_MAX; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 773 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 774 | std::scoped_lock _l(mLock); |
| 775 | mDispatcherIsAlive.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 776 | |
| 777 | // Run a dispatch loop if there are no pending commands. |
| 778 | // The dispatch loop might enqueue commands to run afterwards. |
| 779 | if (!haveCommandsLocked()) { |
| 780 | dispatchOnceInnerLocked(&nextWakeupTime); |
| 781 | } |
| 782 | |
| 783 | // Run all pending commands if there are any. |
| 784 | // 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] | 785 | if (runCommandsLockedInterruptable()) { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 786 | nextWakeupTime = LLONG_MIN; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 787 | } |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 788 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 789 | // If we are still waiting for ack on some events, |
| 790 | // we might have to wake up earlier to check if an app is anr'ing. |
| 791 | const nsecs_t nextAnrCheck = processAnrsLocked(); |
| 792 | nextWakeupTime = std::min(nextWakeupTime, nextAnrCheck); |
| 793 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 794 | // We are about to enter an infinitely long sleep, because we have no commands or |
| 795 | // pending or queued events |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 796 | if (nextWakeupTime == LLONG_MAX) { |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 797 | mDispatcherEnteredIdle.notify_all(); |
| 798 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 799 | } // release lock |
| 800 | |
| 801 | // Wait for callback or timeout or wake. (make sure we round up, not down) |
| 802 | nsecs_t currentTime = now(); |
| 803 | int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime); |
| 804 | mLooper->pollOnce(timeoutMillis); |
| 805 | } |
| 806 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 807 | /** |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 808 | * Raise ANR if there is no focused window. |
| 809 | * Before the ANR is raised, do a final state check: |
| 810 | * 1. The currently focused application must be the same one we are waiting for. |
| 811 | * 2. Ensure we still don't have a focused window. |
| 812 | */ |
| 813 | void InputDispatcher::processNoFocusedWindowAnrLocked() { |
| 814 | // Check if the application that we are waiting for is still focused. |
| 815 | std::shared_ptr<InputApplicationHandle> focusedApplication = |
| 816 | getValueByKey(mFocusedApplicationHandlesByDisplay, mAwaitedApplicationDisplayId); |
| 817 | if (focusedApplication == nullptr || |
| 818 | focusedApplication->getApplicationToken() != |
| 819 | mAwaitedFocusedApplication->getApplicationToken()) { |
| 820 | // Unexpected because we should have reset the ANR timer when focused application changed |
| 821 | ALOGE("Waited for a focused window, but focused application has already changed to %s", |
| 822 | focusedApplication->getName().c_str()); |
| 823 | return; // The focused application has changed. |
| 824 | } |
| 825 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 826 | const sp<WindowInfoHandle>& focusedWindowHandle = |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 827 | getFocusedWindowHandleLocked(mAwaitedApplicationDisplayId); |
| 828 | if (focusedWindowHandle != nullptr) { |
| 829 | return; // We now have a focused window. No need for ANR. |
| 830 | } |
| 831 | onAnrLocked(mAwaitedFocusedApplication); |
| 832 | } |
| 833 | |
| 834 | /** |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 835 | * Check if any of the connections' wait queues have events that are too old. |
| 836 | * If we waited for events to be ack'ed for more than the window timeout, raise an ANR. |
| 837 | * Return the time at which we should wake up next. |
| 838 | */ |
| 839 | nsecs_t InputDispatcher::processAnrsLocked() { |
| 840 | const nsecs_t currentTime = now(); |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 841 | nsecs_t nextAnrCheck = LLONG_MAX; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 842 | // Check if we are waiting for a focused window to appear. Raise ANR if waited too long |
| 843 | if (mNoFocusedWindowTimeoutTime.has_value() && mAwaitedFocusedApplication != nullptr) { |
| 844 | if (currentTime >= *mNoFocusedWindowTimeoutTime) { |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 845 | processNoFocusedWindowAnrLocked(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 846 | mAwaitedFocusedApplication.reset(); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 847 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 848 | return LLONG_MIN; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 849 | } else { |
Siarhei Vishniakou | 38a6d27 | 2020-10-20 20:29:33 -0500 | [diff] [blame] | 850 | // Keep waiting. We will drop the event when mNoFocusedWindowTimeoutTime comes. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 851 | nextAnrCheck = *mNoFocusedWindowTimeoutTime; |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | // Check if any connection ANRs are due |
| 856 | nextAnrCheck = std::min(nextAnrCheck, mAnrTracker.firstTimeout()); |
| 857 | if (currentTime < nextAnrCheck) { // most likely scenario |
| 858 | return nextAnrCheck; // everything is normal. Let's check again at nextAnrCheck |
| 859 | } |
| 860 | |
| 861 | // If we reached here, we have an unresponsive connection. |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 862 | std::shared_ptr<Connection> connection = getConnectionLocked(mAnrTracker.firstToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 863 | if (connection == nullptr) { |
| 864 | ALOGE("Could not find connection for entry %" PRId64, mAnrTracker.firstTimeout()); |
| 865 | return nextAnrCheck; |
| 866 | } |
| 867 | connection->responsive = false; |
| 868 | // Stop waking up for this unresponsive connection |
| 869 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 870 | onAnrLocked(connection); |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 871 | return LLONG_MIN; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 872 | } |
| 873 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 874 | std::chrono::nanoseconds InputDispatcher::getDispatchingTimeoutLocked( |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 875 | const std::shared_ptr<Connection>& connection) { |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 876 | if (connection->monitor) { |
| 877 | return mMonitorDispatchingTimeout; |
| 878 | } |
| 879 | const sp<WindowInfoHandle> window = |
| 880 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 881 | if (window != nullptr) { |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 882 | return window->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 883 | } |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 884 | return DEFAULT_INPUT_DISPATCHING_TIMEOUT; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 885 | } |
| 886 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 887 | void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) { |
| 888 | nsecs_t currentTime = now(); |
| 889 | |
Jeff Brown | dc5992e | 2014-04-11 01:27:26 -0700 | [diff] [blame] | 890 | // Reset the key repeat timer whenever normal dispatch is suspended while the |
| 891 | // device is in a non-interactive state. This is to ensure that we abort a key |
| 892 | // repeat if the device is just coming out of sleep. |
| 893 | if (!mDispatchEnabled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 894 | resetKeyRepeatLocked(); |
| 895 | } |
| 896 | |
| 897 | // If dispatching is frozen, do not process timeouts or try to deliver any new events. |
| 898 | if (mDispatchFrozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 899 | if (DEBUG_FOCUS) { |
| 900 | ALOGD("Dispatch frozen. Waiting some more."); |
| 901 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 902 | return; |
| 903 | } |
| 904 | |
| 905 | // Optimize latency of app switches. |
| 906 | // Essentially we start a short timeout when an app switch key (HOME / ENDCALL) has |
| 907 | // been pressed. When it expires, we preempt dispatch and drop all other pending events. |
| 908 | bool isAppSwitchDue = mAppSwitchDueTime <= currentTime; |
| 909 | if (mAppSwitchDueTime < *nextWakeupTime) { |
| 910 | *nextWakeupTime = mAppSwitchDueTime; |
| 911 | } |
| 912 | |
| 913 | // Ready to start a new event. |
| 914 | // If we don't already have a pending event, go grab one. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 915 | if (!mPendingEvent) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 916 | if (mInboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 917 | if (isAppSwitchDue) { |
| 918 | // The inbound queue is empty so the app switch key we were waiting |
| 919 | // for will never arrive. Stop waiting for it. |
| 920 | resetPendingAppSwitchLocked(false); |
| 921 | isAppSwitchDue = false; |
| 922 | } |
| 923 | |
| 924 | // Synthesize a key repeat if appropriate. |
| 925 | if (mKeyRepeatState.lastKeyEntry) { |
| 926 | if (currentTime >= mKeyRepeatState.nextRepeatTime) { |
| 927 | mPendingEvent = synthesizeKeyRepeatLocked(currentTime); |
| 928 | } else { |
| 929 | if (mKeyRepeatState.nextRepeatTime < *nextWakeupTime) { |
| 930 | *nextWakeupTime = mKeyRepeatState.nextRepeatTime; |
| 931 | } |
| 932 | } |
| 933 | } |
| 934 | |
| 935 | // Nothing to do if there is no pending event. |
| 936 | if (!mPendingEvent) { |
| 937 | return; |
| 938 | } |
| 939 | } else { |
| 940 | // Inbound queue has at least one entry. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 941 | mPendingEvent = mInboundQueue.front(); |
| 942 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 943 | traceInboundQueueLengthLocked(); |
| 944 | } |
| 945 | |
| 946 | // Poke user activity for this event. |
| 947 | if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 948 | pokeUserActivityLocked(*mPendingEvent); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 949 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | // Now we have an event to dispatch. |
| 953 | // 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] | 954 | ALOG_ASSERT(mPendingEvent != nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 955 | bool done = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 956 | DropReason dropReason = DropReason::NOT_DROPPED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 957 | if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 958 | dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 959 | } else if (!mDispatchEnabled) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 960 | dropReason = DropReason::DISABLED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | if (mNextUnblockedEvent == mPendingEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 964 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 965 | } |
| 966 | |
| 967 | switch (mPendingEvent->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 968 | case EventEntry::Type::CONFIGURATION_CHANGED: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 969 | const ConfigurationChangedEntry& typedEntry = |
| 970 | static_cast<const ConfigurationChangedEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 971 | done = dispatchConfigurationChangedLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 972 | dropReason = DropReason::NOT_DROPPED; // configuration changes are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 973 | break; |
| 974 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 975 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 976 | case EventEntry::Type::DEVICE_RESET: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 977 | const DeviceResetEntry& typedEntry = |
| 978 | static_cast<const DeviceResetEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 979 | done = dispatchDeviceResetLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 980 | dropReason = DropReason::NOT_DROPPED; // device resets are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 981 | break; |
| 982 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 983 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 984 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 985 | std::shared_ptr<FocusEntry> typedEntry = |
| 986 | std::static_pointer_cast<FocusEntry>(mPendingEvent); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 987 | dispatchFocusLocked(currentTime, typedEntry); |
| 988 | done = true; |
| 989 | dropReason = DropReason::NOT_DROPPED; // focus events are never dropped |
| 990 | break; |
| 991 | } |
| 992 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 993 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 994 | const auto typedEntry = std::static_pointer_cast<TouchModeEntry>(mPendingEvent); |
| 995 | dispatchTouchModeChangeLocked(currentTime, typedEntry); |
| 996 | done = true; |
| 997 | dropReason = DropReason::NOT_DROPPED; // touch mode events are never dropped |
| 998 | break; |
| 999 | } |
| 1000 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1001 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 1002 | const auto typedEntry = |
| 1003 | std::static_pointer_cast<PointerCaptureChangedEntry>(mPendingEvent); |
| 1004 | dispatchPointerCaptureChangedLocked(currentTime, typedEntry, dropReason); |
| 1005 | done = true; |
| 1006 | break; |
| 1007 | } |
| 1008 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1009 | case EventEntry::Type::DRAG: { |
| 1010 | std::shared_ptr<DragEntry> typedEntry = |
| 1011 | std::static_pointer_cast<DragEntry>(mPendingEvent); |
| 1012 | dispatchDragLocked(currentTime, typedEntry); |
| 1013 | done = true; |
| 1014 | break; |
| 1015 | } |
| 1016 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1017 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1018 | std::shared_ptr<KeyEntry> keyEntry = std::static_pointer_cast<KeyEntry>(mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1019 | if (isAppSwitchDue) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1020 | if (isAppSwitchKeyEvent(*keyEntry)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1021 | resetPendingAppSwitchLocked(true); |
| 1022 | isAppSwitchDue = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1023 | } else if (dropReason == DropReason::NOT_DROPPED) { |
| 1024 | dropReason = DropReason::APP_SWITCH; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1025 | } |
| 1026 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1027 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *keyEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1028 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1029 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1030 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 1031 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1032 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1033 | done = dispatchKeyLocked(currentTime, keyEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1034 | break; |
| 1035 | } |
| 1036 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1037 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1038 | std::shared_ptr<MotionEntry> motionEntry = |
| 1039 | std::static_pointer_cast<MotionEntry>(mPendingEvent); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1040 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 1041 | dropReason = DropReason::APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1042 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1043 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *motionEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1044 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1045 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1046 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 1047 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1048 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1049 | done = dispatchMotionLocked(currentTime, motionEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1050 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1051 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1052 | |
| 1053 | case EventEntry::Type::SENSOR: { |
| 1054 | std::shared_ptr<SensorEntry> sensorEntry = |
| 1055 | std::static_pointer_cast<SensorEntry>(mPendingEvent); |
| 1056 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 1057 | dropReason = DropReason::APP_SWITCH; |
| 1058 | } |
| 1059 | // Sensor timestamps use SYSTEM_TIME_BOOTTIME time base, so we can't use |
| 1060 | // 'currentTime' here, get SYSTEM_TIME_BOOTTIME instead. |
| 1061 | nsecs_t bootTime = systemTime(SYSTEM_TIME_BOOTTIME); |
| 1062 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(bootTime, *sensorEntry)) { |
| 1063 | dropReason = DropReason::STALE; |
| 1064 | } |
| 1065 | dispatchSensorLocked(currentTime, sensorEntry, &dropReason, nextWakeupTime); |
| 1066 | done = true; |
| 1067 | break; |
| 1068 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1069 | } |
| 1070 | |
| 1071 | if (done) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1072 | if (dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1073 | dropInboundEventLocked(*mPendingEvent, dropReason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1074 | } |
Michael Wright | 3a98172 | 2015-06-10 15:26:13 +0100 | [diff] [blame] | 1075 | mLastDropReason = dropReason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1076 | |
| 1077 | releasePendingEventLocked(); |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 1078 | *nextWakeupTime = LLONG_MIN; // force next poll to wake up immediately |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1079 | } |
| 1080 | } |
| 1081 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 1082 | bool InputDispatcher::isStaleEvent(nsecs_t currentTime, const EventEntry& entry) { |
| 1083 | return std::chrono::nanoseconds(currentTime - entry.eventTime) >= mStaleEventTimeout; |
| 1084 | } |
| 1085 | |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1086 | /** |
| 1087 | * Return true if the events preceding this incoming motion event should be dropped |
| 1088 | * Return false otherwise (the default behaviour) |
| 1089 | */ |
| 1090 | bool InputDispatcher::shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1091 | const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN && |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 1092 | isFromSource(motionEntry.source, AINPUT_SOURCE_CLASS_POINTER); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1093 | |
| 1094 | // Optimize case where the current application is unresponsive and the user |
| 1095 | // decides to touch a window in a different application. |
| 1096 | // If the application takes too long to catch up then we drop all events preceding |
| 1097 | // the touch into the other window. |
| 1098 | if (isPointerDownEvent && mAwaitedFocusedApplication != nullptr) { |
Siarhei Vishniakou | 9306c38 | 2022-09-30 15:30:31 -0700 | [diff] [blame] | 1099 | const int32_t displayId = motionEntry.displayId; |
| 1100 | const auto [x, y] = resolveTouchedPosition(motionEntry); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1101 | const bool isStylus = isPointerFromStylus(motionEntry, /*pointerIndex=*/0); |
Siarhei Vishniakou | 9306c38 | 2022-09-30 15:30:31 -0700 | [diff] [blame] | 1102 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1103 | auto [touchedWindowHandle, _] = findTouchedWindowAtLocked(displayId, x, y, isStylus); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1104 | if (touchedWindowHandle != nullptr && |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1105 | touchedWindowHandle->getApplicationToken() != |
| 1106 | mAwaitedFocusedApplication->getApplicationToken()) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1107 | // User touched a different application than the one we are waiting on. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1108 | ALOGI("Pruning input queue because user touched a different application while waiting " |
| 1109 | "for %s", |
| 1110 | mAwaitedFocusedApplication->getName().c_str()); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1111 | return true; |
| 1112 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1113 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1114 | // Alternatively, maybe there's a spy window that could handle this event. |
| 1115 | const std::vector<sp<WindowInfoHandle>> touchedSpies = |
| 1116 | findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus); |
| 1117 | for (const auto& windowHandle : touchedSpies) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 1118 | const std::shared_ptr<Connection> connection = |
| 1119 | getConnectionLocked(windowHandle->getToken()); |
Siarhei Vishniakou | 34ed4d4 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1120 | if (connection != nullptr && connection->responsive) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1121 | // This spy window could take more input. Drop all events preceding this |
| 1122 | // event, so that the spy window can get a chance to receive the stream. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1123 | ALOGW("Pruning the input queue because %s is unresponsive, but we have a " |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1124 | "responsive spy window that may handle the event.", |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1125 | mAwaitedFocusedApplication->getName().c_str()); |
| 1126 | return true; |
| 1127 | } |
| 1128 | } |
| 1129 | } |
| 1130 | |
| 1131 | // Prevent getting stuck: if we have a pending key event, and some motion events that have not |
| 1132 | // yet been processed by some connections, the dispatcher will wait for these motion |
| 1133 | // events to be processed before dispatching the key event. This is because these motion events |
| 1134 | // may cause a new window to be launched, which the user might expect to receive focus. |
| 1135 | // To prevent waiting forever for such events, just send the key to the currently focused window |
| 1136 | if (isPointerDownEvent && mKeyIsWaitingForEventsTimeout) { |
| 1137 | ALOGD("Received a new pointer down event, stop waiting for events to process and " |
| 1138 | "just send the pending key event to the focused window."); |
| 1139 | mKeyIsWaitingForEventsTimeout = now(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1140 | } |
| 1141 | return false; |
| 1142 | } |
| 1143 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1144 | bool InputDispatcher::enqueueInboundEventLocked(std::unique_ptr<EventEntry> newEntry) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1145 | bool needWake = mInboundQueue.empty(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1146 | mInboundQueue.push_back(std::move(newEntry)); |
| 1147 | EventEntry& entry = *(mInboundQueue.back()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1148 | traceInboundQueueLengthLocked(); |
| 1149 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1150 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1151 | case EventEntry::Type::KEY: { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1152 | LOG_ALWAYS_FATAL_IF((entry.policyFlags & POLICY_FLAG_TRUSTED) == 0, |
| 1153 | "Unexpected untrusted event."); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1154 | // Optimize app switch latency. |
| 1155 | // If the application takes too long to catch up then we drop all events preceding |
| 1156 | // the app switch key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1157 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1158 | if (isAppSwitchKeyEvent(keyEntry)) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1159 | if (keyEntry.action == AKEY_EVENT_ACTION_DOWN) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1160 | mAppSwitchSawKeyDown = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1161 | } else if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1162 | if (mAppSwitchSawKeyDown) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1163 | if (DEBUG_APP_SWITCH) { |
| 1164 | ALOGD("App switch is pending!"); |
| 1165 | } |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1166 | mAppSwitchDueTime = keyEntry.eventTime + APP_SWITCH_TIMEOUT; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1167 | mAppSwitchSawKeyDown = false; |
| 1168 | needWake = true; |
| 1169 | } |
| 1170 | } |
| 1171 | } |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 1172 | |
| 1173 | // If a new up event comes in, and the pending event with same key code has been asked |
| 1174 | // to try again later because of the policy. We have to reset the intercept key wake up |
| 1175 | // time for it may have been handled in the policy and could be dropped. |
| 1176 | if (keyEntry.action == AKEY_EVENT_ACTION_UP && mPendingEvent && |
| 1177 | mPendingEvent->type == EventEntry::Type::KEY) { |
| 1178 | KeyEntry& pendingKey = static_cast<KeyEntry&>(*mPendingEvent); |
| 1179 | if (pendingKey.keyCode == keyEntry.keyCode && |
| 1180 | pendingKey.interceptKeyResult == |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1181 | KeyEntry::InterceptKeyResult::TRY_AGAIN_LATER) { |
| 1182 | pendingKey.interceptKeyResult = KeyEntry::InterceptKeyResult::UNKNOWN; |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 1183 | pendingKey.interceptKeyWakeupTime = 0; |
| 1184 | needWake = true; |
| 1185 | } |
| 1186 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1187 | break; |
| 1188 | } |
| 1189 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1190 | case EventEntry::Type::MOTION: { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1191 | LOG_ALWAYS_FATAL_IF((entry.policyFlags & POLICY_FLAG_TRUSTED) == 0, |
| 1192 | "Unexpected untrusted event."); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1193 | if (shouldPruneInboundQueueLocked(static_cast<MotionEntry&>(entry))) { |
| 1194 | mNextUnblockedEvent = mInboundQueue.back(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1195 | needWake = true; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1196 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1197 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1198 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1199 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1200 | LOG_ALWAYS_FATAL("Focus events should be inserted using enqueueFocusEventLocked"); |
| 1201 | break; |
| 1202 | } |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1203 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1204 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1205 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1206 | case EventEntry::Type::SENSOR: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1207 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1208 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1209 | // nothing to do |
| 1210 | break; |
| 1211 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1212 | } |
| 1213 | |
| 1214 | return needWake; |
| 1215 | } |
| 1216 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1217 | void InputDispatcher::addRecentEventLocked(std::shared_ptr<EventEntry> entry) { |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1218 | // Do not store sensor event in recent queue to avoid flooding the queue. |
| 1219 | if (entry->type != EventEntry::Type::SENSOR) { |
| 1220 | mRecentQueue.push_back(entry); |
| 1221 | } |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1222 | if (mRecentQueue.size() > RECENT_QUEUE_MAX_SIZE) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1223 | mRecentQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1224 | } |
| 1225 | } |
| 1226 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1227 | std::pair<sp<WindowInfoHandle>, std::vector<InputTarget>> |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 1228 | InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, float x, float y, bool isStylus, |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1229 | bool ignoreDragWindow) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1230 | // Traverse windows from front to back to find touched window. |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1231 | std::vector<InputTarget> outsideTargets; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1232 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1233 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 1234 | if (ignoreDragWindow && haveSameToken(windowHandle, mDragState->dragWindow)) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1235 | continue; |
| 1236 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1237 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1238 | const WindowInfo& info = *windowHandle->getInfo(); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 1239 | if (!info.isSpy() && |
| 1240 | windowAcceptsTouchAt(info, displayId, x, y, isStylus, getTransformLocked(displayId))) { |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1241 | return {windowHandle, outsideTargets}; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1242 | } |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1243 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1244 | if (info.inputConfig.test(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH)) { |
| 1245 | addWindowTargetLocked(windowHandle, InputTarget::Flags::DISPATCH_AS_OUTSIDE, |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 1246 | /*pointerIds=*/{}, /*firstDownTimeInTarget=*/std::nullopt, |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1247 | outsideTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1248 | } |
| 1249 | } |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1250 | return {nullptr, {}}; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1251 | } |
| 1252 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1253 | std::vector<sp<WindowInfoHandle>> InputDispatcher::findTouchedSpyWindowsAtLocked( |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 1254 | int32_t displayId, float x, float y, bool isStylus) const { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1255 | // Traverse windows from front to back and gather the touched spy windows. |
| 1256 | std::vector<sp<WindowInfoHandle>> spyWindows; |
| 1257 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
| 1258 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 1259 | const WindowInfo& info = *windowHandle->getInfo(); |
| 1260 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 1261 | if (!windowAcceptsTouchAt(info, displayId, x, y, isStylus, getTransformLocked(displayId))) { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1262 | continue; |
| 1263 | } |
| 1264 | if (!info.isSpy()) { |
| 1265 | // The first touched non-spy window was found, so return the spy windows touched so far. |
| 1266 | return spyWindows; |
| 1267 | } |
| 1268 | spyWindows.push_back(windowHandle); |
| 1269 | } |
| 1270 | return spyWindows; |
| 1271 | } |
| 1272 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1273 | void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1274 | const char* reason; |
| 1275 | switch (dropReason) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1276 | case DropReason::POLICY: |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 1277 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1278 | ALOGD("Dropped event because policy consumed it."); |
| 1279 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1280 | reason = "inbound event was dropped because the policy consumed it"; |
| 1281 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1282 | case DropReason::DISABLED: |
| 1283 | if (mLastDropReason != DropReason::DISABLED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1284 | ALOGI("Dropped event because input dispatch is disabled."); |
| 1285 | } |
| 1286 | reason = "inbound event was dropped because input dispatch is disabled"; |
| 1287 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1288 | case DropReason::APP_SWITCH: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1289 | ALOGI("Dropped event because of pending overdue app switch."); |
| 1290 | reason = "inbound event was dropped because of pending overdue app switch"; |
| 1291 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1292 | case DropReason::BLOCKED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1293 | ALOGI("Dropped event because the current application is not responding and the user " |
| 1294 | "has started interacting with a different application."); |
| 1295 | reason = "inbound event was dropped because the current application is not responding " |
| 1296 | "and the user has started interacting with a different application"; |
| 1297 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1298 | case DropReason::STALE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1299 | ALOGI("Dropped event because it is stale."); |
| 1300 | reason = "inbound event was dropped because it is stale"; |
| 1301 | break; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1302 | case DropReason::NO_POINTER_CAPTURE: |
| 1303 | ALOGI("Dropped event because there is no window with Pointer Capture."); |
| 1304 | reason = "inbound event was dropped because there is no window with Pointer Capture"; |
| 1305 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1306 | case DropReason::NOT_DROPPED: { |
| 1307 | LOG_ALWAYS_FATAL("Should not be dropping a NOT_DROPPED event"); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1308 | return; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1309 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1310 | } |
| 1311 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1312 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1313 | case EventEntry::Type::KEY: { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1314 | CancelationOptions options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, reason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1315 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1316 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1317 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1318 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1319 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1320 | if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1321 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, reason); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1322 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1323 | } else { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1324 | CancelationOptions options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, |
| 1325 | reason); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1326 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1327 | } |
| 1328 | break; |
| 1329 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1330 | case EventEntry::Type::SENSOR: { |
| 1331 | break; |
| 1332 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1333 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1334 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1335 | break; |
| 1336 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1337 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1338 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1339 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 1340 | case EventEntry::Type::DEVICE_RESET: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1341 | 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] | 1342 | break; |
| 1343 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1344 | } |
| 1345 | } |
| 1346 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1347 | static bool isAppSwitchKeyCode(int32_t keyCode) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1348 | return keyCode == AKEYCODE_HOME || keyCode == AKEYCODE_ENDCALL || |
| 1349 | keyCode == AKEYCODE_APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1350 | } |
| 1351 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1352 | bool InputDispatcher::isAppSwitchKeyEvent(const KeyEntry& keyEntry) { |
| 1353 | return !(keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) && isAppSwitchKeyCode(keyEntry.keyCode) && |
| 1354 | (keyEntry.policyFlags & POLICY_FLAG_TRUSTED) && |
| 1355 | (keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1356 | } |
| 1357 | |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 1358 | bool InputDispatcher::isAppSwitchPendingLocked() const { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 1359 | return mAppSwitchDueTime != LLONG_MAX; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1360 | } |
| 1361 | |
| 1362 | void InputDispatcher::resetPendingAppSwitchLocked(bool handled) { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 1363 | mAppSwitchDueTime = LLONG_MAX; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1364 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1365 | if (DEBUG_APP_SWITCH) { |
| 1366 | if (handled) { |
| 1367 | ALOGD("App switch has arrived."); |
| 1368 | } else { |
| 1369 | ALOGD("App switch was abandoned."); |
| 1370 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1371 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1372 | } |
| 1373 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1374 | bool InputDispatcher::haveCommandsLocked() const { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1375 | return !mCommandQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1376 | } |
| 1377 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1378 | bool InputDispatcher::runCommandsLockedInterruptable() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1379 | if (mCommandQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1380 | return false; |
| 1381 | } |
| 1382 | |
| 1383 | do { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1384 | auto command = std::move(mCommandQueue.front()); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1385 | mCommandQueue.pop_front(); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1386 | // Commands are run with the lock held, but may release and re-acquire the lock from within. |
| 1387 | command(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1388 | } while (!mCommandQueue.empty()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1389 | return true; |
| 1390 | } |
| 1391 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1392 | void InputDispatcher::postCommandLocked(Command&& command) { |
| 1393 | mCommandQueue.push_back(command); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1394 | } |
| 1395 | |
| 1396 | void InputDispatcher::drainInboundQueueLocked() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1397 | while (!mInboundQueue.empty()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1398 | std::shared_ptr<EventEntry> entry = mInboundQueue.front(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1399 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1400 | releaseInboundEventLocked(entry); |
| 1401 | } |
| 1402 | traceInboundQueueLengthLocked(); |
| 1403 | } |
| 1404 | |
| 1405 | void InputDispatcher::releasePendingEventLocked() { |
| 1406 | if (mPendingEvent) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1407 | releaseInboundEventLocked(mPendingEvent); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1408 | mPendingEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1409 | } |
| 1410 | } |
| 1411 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1412 | void InputDispatcher::releaseInboundEventLocked(std::shared_ptr<EventEntry> entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1413 | InjectionState* injectionState = entry->injectionState; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1414 | if (injectionState && injectionState->injectionResult == InputEventInjectionResult::PENDING) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1415 | if (DEBUG_DISPATCH_CYCLE) { |
| 1416 | ALOGD("Injected inbound event was dropped."); |
| 1417 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1418 | setInjectionResult(*entry, InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1419 | } |
| 1420 | if (entry == mNextUnblockedEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1421 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1422 | } |
| 1423 | addRecentEventLocked(entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1424 | } |
| 1425 | |
| 1426 | void InputDispatcher::resetKeyRepeatLocked() { |
| 1427 | if (mKeyRepeatState.lastKeyEntry) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1428 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1429 | } |
| 1430 | } |
| 1431 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1432 | std::shared_ptr<KeyEntry> InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) { |
| 1433 | std::shared_ptr<KeyEntry> entry = mKeyRepeatState.lastKeyEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1434 | |
Michael Wright | 2e73295 | 2014-09-24 13:26:59 -0700 | [diff] [blame] | 1435 | uint32_t policyFlags = entry->policyFlags & |
| 1436 | (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1437 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1438 | std::shared_ptr<KeyEntry> newEntry = |
| 1439 | std::make_unique<KeyEntry>(mIdGenerator.nextId(), currentTime, entry->deviceId, |
| 1440 | entry->source, entry->displayId, policyFlags, entry->action, |
| 1441 | entry->flags, entry->keyCode, entry->scanCode, |
| 1442 | entry->metaState, entry->repeatCount + 1, entry->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1443 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1444 | newEntry->syntheticRepeat = true; |
| 1445 | mKeyRepeatState.lastKeyEntry = newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1446 | mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1447 | return newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1448 | } |
| 1449 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1450 | bool InputDispatcher::dispatchConfigurationChangedLocked(nsecs_t currentTime, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1451 | const ConfigurationChangedEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1452 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1453 | ALOGD("dispatchConfigurationChanged - eventTime=%" PRId64, entry.eventTime); |
| 1454 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1455 | |
| 1456 | // Reset key repeating in case a keyboard device was added or removed or something. |
| 1457 | resetKeyRepeatLocked(); |
| 1458 | |
| 1459 | // 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] | 1460 | auto command = [this, eventTime = entry.eventTime]() REQUIRES(mLock) { |
| 1461 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 1462 | mPolicy.notifyConfigurationChanged(eventTime); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1463 | }; |
| 1464 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1465 | return true; |
| 1466 | } |
| 1467 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1468 | bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime, |
| 1469 | const DeviceResetEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1470 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1471 | ALOGD("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry.eventTime, |
| 1472 | entry.deviceId); |
| 1473 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1474 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 1475 | // Reset key repeating in case a keyboard device was disabled or enabled. |
| 1476 | if (mKeyRepeatState.lastKeyEntry && mKeyRepeatState.lastKeyEntry->deviceId == entry.deviceId) { |
| 1477 | resetKeyRepeatLocked(); |
| 1478 | } |
| 1479 | |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1480 | CancelationOptions options(CancelationOptions::Mode::CANCEL_ALL_EVENTS, "device was reset"); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1481 | options.deviceId = entry.deviceId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1482 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 1483 | |
| 1484 | // Remove all active pointers from this device |
| 1485 | for (auto& [_, touchState] : mTouchStatesByDisplay) { |
| 1486 | touchState.removeAllPointersForDevice(entry.deviceId); |
| 1487 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1488 | return true; |
| 1489 | } |
| 1490 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1491 | void InputDispatcher::enqueueFocusEventLocked(const sp<IBinder>& windowToken, bool hasFocus, |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1492 | const std::string& reason) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1493 | if (mPendingEvent != nullptr) { |
| 1494 | // Move the pending event to the front of the queue. This will give the chance |
| 1495 | // for the pending event to get dispatched to the newly focused window |
| 1496 | mInboundQueue.push_front(mPendingEvent); |
| 1497 | mPendingEvent = nullptr; |
| 1498 | } |
| 1499 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1500 | std::unique_ptr<FocusEntry> focusEntry = |
| 1501 | std::make_unique<FocusEntry>(mIdGenerator.nextId(), now(), windowToken, hasFocus, |
| 1502 | reason); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1503 | |
| 1504 | // This event should go to the front of the queue, but behind all other focus events |
| 1505 | // Find the last focus event, and insert right after it |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1506 | std::deque<std::shared_ptr<EventEntry>>::reverse_iterator it = |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1507 | std::find_if(mInboundQueue.rbegin(), mInboundQueue.rend(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1508 | [](const std::shared_ptr<EventEntry>& event) { |
| 1509 | return event->type == EventEntry::Type::FOCUS; |
| 1510 | }); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1511 | |
| 1512 | // 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] | 1513 | mInboundQueue.insert(it.base(), std::move(focusEntry)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1514 | } |
| 1515 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1516 | void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime, std::shared_ptr<FocusEntry> entry) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 1517 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1518 | if (channel == nullptr) { |
| 1519 | return; // Window has gone away |
| 1520 | } |
| 1521 | InputTarget target; |
| 1522 | target.inputChannel = channel; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 1523 | target.flags = InputTarget::Flags::DISPATCH_AS_IS; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1524 | entry->dispatchInProgress = true; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1525 | std::string message = std::string("Focus ") + (entry->hasFocus ? "entering " : "leaving ") + |
| 1526 | channel->getName(); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 1527 | std::string reason = std::string("reason=").append(entry->reason); |
| 1528 | android_log_event_list(LOGTAG_INPUT_FOCUS) << message << reason << LOG_ID_EVENTS; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1529 | dispatchEventLocked(currentTime, entry, {target}); |
| 1530 | } |
| 1531 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1532 | void InputDispatcher::dispatchPointerCaptureChangedLocked( |
| 1533 | nsecs_t currentTime, const std::shared_ptr<PointerCaptureChangedEntry>& entry, |
| 1534 | DropReason& dropReason) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1535 | dropReason = DropReason::NOT_DROPPED; |
| 1536 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1537 | const bool haveWindowWithPointerCapture = mWindowTokenWithPointerCapture != nullptr; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1538 | sp<IBinder> token; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1539 | |
| 1540 | if (entry->pointerCaptureRequest.enable) { |
| 1541 | // Enable Pointer Capture. |
| 1542 | if (haveWindowWithPointerCapture && |
| 1543 | (entry->pointerCaptureRequest == mCurrentPointerCaptureRequest)) { |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 1544 | // This can happen if pointer capture is disabled and re-enabled before we notify the |
| 1545 | // app of the state change, so there is no need to notify the app. |
| 1546 | ALOGI("Skipping dispatch of Pointer Capture being enabled: no state change."); |
| 1547 | return; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1548 | } |
| 1549 | if (!mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1550 | // This can happen if a window requests capture and immediately releases capture. |
| 1551 | ALOGW("No window requested Pointer Capture."); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1552 | dropReason = DropReason::NO_POINTER_CAPTURE; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1553 | return; |
| 1554 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1555 | if (entry->pointerCaptureRequest.seq != mCurrentPointerCaptureRequest.seq) { |
| 1556 | ALOGI("Skipping dispatch of Pointer Capture being enabled: sequence number mismatch."); |
| 1557 | return; |
| 1558 | } |
| 1559 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1560 | token = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1561 | LOG_ALWAYS_FATAL_IF(!token, "Cannot find focused window for Pointer Capture."); |
| 1562 | mWindowTokenWithPointerCapture = token; |
| 1563 | } else { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1564 | // Disable Pointer Capture. |
| 1565 | // We do not check if the sequence number matches for requests to disable Pointer Capture |
| 1566 | // for two reasons: |
| 1567 | // 1. Pointer Capture can be disabled by a focus change, which means we can get two entries |
| 1568 | // to disable capture with the same sequence number: one generated by |
| 1569 | // disablePointerCaptureForcedLocked() and another as an acknowledgement of Pointer |
| 1570 | // Capture being disabled in InputReader. |
| 1571 | // 2. We respect any request to disable Pointer Capture generated by InputReader, since the |
| 1572 | // actual Pointer Capture state that affects events being generated by input devices is |
| 1573 | // in InputReader. |
| 1574 | if (!haveWindowWithPointerCapture) { |
| 1575 | // Pointer capture was already forcefully disabled because of focus change. |
| 1576 | dropReason = DropReason::NOT_DROPPED; |
| 1577 | return; |
| 1578 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1579 | token = mWindowTokenWithPointerCapture; |
| 1580 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1581 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1582 | setPointerCaptureLocked(false); |
| 1583 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1584 | } |
| 1585 | |
| 1586 | auto channel = getInputChannelLocked(token); |
| 1587 | if (channel == nullptr) { |
| 1588 | // Window has gone away, clean up Pointer Capture state. |
| 1589 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1590 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1591 | setPointerCaptureLocked(false); |
| 1592 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1593 | return; |
| 1594 | } |
| 1595 | InputTarget target; |
| 1596 | target.inputChannel = channel; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 1597 | target.flags = InputTarget::Flags::DISPATCH_AS_IS; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1598 | entry->dispatchInProgress = true; |
| 1599 | dispatchEventLocked(currentTime, entry, {target}); |
| 1600 | |
| 1601 | dropReason = DropReason::NOT_DROPPED; |
| 1602 | } |
| 1603 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1604 | void InputDispatcher::dispatchTouchModeChangeLocked(nsecs_t currentTime, |
| 1605 | const std::shared_ptr<TouchModeEntry>& entry) { |
| 1606 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 1607 | getWindowHandlesLocked(entry->displayId); |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1608 | if (windowHandles.empty()) { |
| 1609 | return; |
| 1610 | } |
| 1611 | const std::vector<InputTarget> inputTargets = |
| 1612 | getInputTargetsFromWindowHandlesLocked(windowHandles); |
| 1613 | if (inputTargets.empty()) { |
| 1614 | return; |
| 1615 | } |
| 1616 | entry->dispatchInProgress = true; |
| 1617 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1618 | } |
| 1619 | |
| 1620 | std::vector<InputTarget> InputDispatcher::getInputTargetsFromWindowHandlesLocked( |
| 1621 | const std::vector<sp<WindowInfoHandle>>& windowHandles) const { |
| 1622 | std::vector<InputTarget> inputTargets; |
| 1623 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1624 | const sp<IBinder>& token = handle->getToken(); |
| 1625 | if (token == nullptr) { |
| 1626 | continue; |
| 1627 | } |
| 1628 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(token); |
| 1629 | if (channel == nullptr) { |
| 1630 | continue; // Window has gone away |
| 1631 | } |
| 1632 | InputTarget target; |
| 1633 | target.inputChannel = channel; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 1634 | target.flags = InputTarget::Flags::DISPATCH_AS_IS; |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1635 | inputTargets.push_back(target); |
| 1636 | } |
| 1637 | return inputTargets; |
| 1638 | } |
| 1639 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1640 | bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, std::shared_ptr<KeyEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1641 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1642 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1643 | if (!entry->dispatchInProgress) { |
| 1644 | if (entry->repeatCount == 0 && entry->action == AKEY_EVENT_ACTION_DOWN && |
| 1645 | (entry->policyFlags & POLICY_FLAG_TRUSTED) && |
| 1646 | (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { |
| 1647 | if (mKeyRepeatState.lastKeyEntry && |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1648 | mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode && |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1649 | // We have seen two identical key downs in a row which indicates that the device |
| 1650 | // driver is automatically generating key repeats itself. We take note of the |
| 1651 | // repeat here, but we disable our own next key repeat timer since it is clear that |
| 1652 | // we will not need to synthesize key repeats ourselves. |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1653 | mKeyRepeatState.lastKeyEntry->deviceId == entry->deviceId) { |
| 1654 | // Make sure we don't get key down from a different device. If a different |
| 1655 | // device Id has same key pressed down, the new device Id will replace the |
| 1656 | // current one to hold the key repeat with repeat count reset. |
| 1657 | // In the future when got a KEY_UP on the device id, drop it and do not |
| 1658 | // stop the key repeat on current device. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1659 | entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; |
| 1660 | resetKeyRepeatLocked(); |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 1661 | mKeyRepeatState.nextRepeatTime = LLONG_MAX; // don't generate repeats ourselves |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1662 | } else { |
| 1663 | // Not a repeat. Save key down state in case we do see a repeat later. |
| 1664 | resetKeyRepeatLocked(); |
| 1665 | mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; |
| 1666 | } |
| 1667 | mKeyRepeatState.lastKeyEntry = entry; |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1668 | } else if (entry->action == AKEY_EVENT_ACTION_UP && mKeyRepeatState.lastKeyEntry && |
| 1669 | mKeyRepeatState.lastKeyEntry->deviceId != entry->deviceId) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1670 | // The key on device 'deviceId' is still down, do not stop key repeat |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 1671 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1672 | ALOGD("deviceId=%d got KEY_UP as stale", entry->deviceId); |
| 1673 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1674 | } else if (!entry->syntheticRepeat) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1675 | resetKeyRepeatLocked(); |
| 1676 | } |
| 1677 | |
| 1678 | if (entry->repeatCount == 1) { |
| 1679 | entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; |
| 1680 | } else { |
| 1681 | entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; |
| 1682 | } |
| 1683 | |
| 1684 | entry->dispatchInProgress = true; |
| 1685 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1686 | logOutboundKeyDetails("dispatchKey - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1687 | } |
| 1688 | |
| 1689 | // Handle case where the policy asked us to try again later last time. |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1690 | if (entry->interceptKeyResult == KeyEntry::InterceptKeyResult::TRY_AGAIN_LATER) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1691 | if (currentTime < entry->interceptKeyWakeupTime) { |
| 1692 | if (entry->interceptKeyWakeupTime < *nextWakeupTime) { |
| 1693 | *nextWakeupTime = entry->interceptKeyWakeupTime; |
| 1694 | } |
| 1695 | return false; // wait until next wakeup |
| 1696 | } |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1697 | entry->interceptKeyResult = KeyEntry::InterceptKeyResult::UNKNOWN; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1698 | entry->interceptKeyWakeupTime = 0; |
| 1699 | } |
| 1700 | |
| 1701 | // Give the policy a chance to intercept the key. |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1702 | if (entry->interceptKeyResult == KeyEntry::InterceptKeyResult::UNKNOWN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1703 | if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1704 | sp<IBinder> focusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1705 | mFocusResolver.getFocusedWindowToken(getTargetDisplayId(*entry)); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1706 | |
| 1707 | auto command = [this, focusedWindowToken, entry]() REQUIRES(mLock) { |
| 1708 | doInterceptKeyBeforeDispatchingCommand(focusedWindowToken, *entry); |
| 1709 | }; |
| 1710 | postCommandLocked(std::move(command)); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1711 | // Poke user activity for keys not passed to user |
| 1712 | pokeUserActivityLocked(*entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1713 | return false; // wait for the command to run |
| 1714 | } else { |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1715 | entry->interceptKeyResult = KeyEntry::InterceptKeyResult::CONTINUE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1716 | } |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1717 | } else if (entry->interceptKeyResult == KeyEntry::InterceptKeyResult::SKIP) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1718 | if (*dropReason == DropReason::NOT_DROPPED) { |
| 1719 | *dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1720 | } |
| 1721 | } |
| 1722 | |
| 1723 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1724 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1725 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1726 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1727 | : InputEventInjectionResult::FAILED); |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1728 | mReporter->reportDroppedKey(entry->id); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1729 | // Poke user activity for undispatched keys |
| 1730 | pokeUserActivityLocked(*entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1731 | return true; |
| 1732 | } |
| 1733 | |
| 1734 | // Identify targets. |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1735 | InputEventInjectionResult injectionResult; |
| 1736 | sp<WindowInfoHandle> focusedWindow = |
| 1737 | findFocusedWindowTargetLocked(currentTime, *entry, nextWakeupTime, |
| 1738 | /*byref*/ injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1739 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1740 | return false; |
| 1741 | } |
| 1742 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1743 | setInjectionResult(*entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1744 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1745 | return true; |
| 1746 | } |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1747 | LOG_ALWAYS_FATAL_IF(focusedWindow == nullptr); |
| 1748 | |
| 1749 | std::vector<InputTarget> inputTargets; |
| 1750 | addWindowTargetLocked(focusedWindow, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 1751 | InputTarget::Flags::FOREGROUND | InputTarget::Flags::DISPATCH_AS_IS, |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 1752 | /*pointerIds=*/{}, getDownTime(*entry), inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1753 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1754 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1755 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1756 | |
| 1757 | // Dispatch the key. |
| 1758 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1759 | return true; |
| 1760 | } |
| 1761 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1762 | void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1763 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1764 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 ", " |
| 1765 | "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, " |
| 1766 | "metaState=0x%x, repeatCount=%d, downTime=%" PRId64, |
| 1767 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, |
| 1768 | entry.policyFlags, entry.action, entry.flags, entry.keyCode, entry.scanCode, |
| 1769 | entry.metaState, entry.repeatCount, entry.downTime); |
| 1770 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1771 | } |
| 1772 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1773 | void InputDispatcher::dispatchSensorLocked(nsecs_t currentTime, |
| 1774 | const std::shared_ptr<SensorEntry>& entry, |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1775 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1776 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1777 | ALOGD("notifySensorEvent eventTime=%" PRId64 ", hwTimestamp=%" PRId64 ", deviceId=%d, " |
| 1778 | "source=0x%x, sensorType=%s", |
| 1779 | entry->eventTime, entry->hwTimestamp, entry->deviceId, entry->source, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1780 | ftl::enum_string(entry->sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1781 | } |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1782 | auto command = [this, entry]() REQUIRES(mLock) { |
| 1783 | scoped_unlock unlock(mLock); |
| 1784 | |
| 1785 | if (entry->accuracyChanged) { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 1786 | mPolicy.notifySensorAccuracy(entry->deviceId, entry->sensorType, entry->accuracy); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1787 | } |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 1788 | mPolicy.notifySensorEvent(entry->deviceId, entry->sensorType, entry->accuracy, |
| 1789 | entry->hwTimestamp, entry->values); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1790 | }; |
| 1791 | postCommandLocked(std::move(command)); |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1792 | } |
| 1793 | |
| 1794 | bool InputDispatcher::flushSensor(int deviceId, InputDeviceSensorType sensorType) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1795 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1796 | ALOGD("flushSensor deviceId=%d, sensorType=%s", deviceId, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1797 | ftl::enum_string(sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1798 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1799 | { // acquire lock |
| 1800 | std::scoped_lock _l(mLock); |
| 1801 | |
| 1802 | for (auto it = mInboundQueue.begin(); it != mInboundQueue.end(); it++) { |
| 1803 | std::shared_ptr<EventEntry> entry = *it; |
| 1804 | if (entry->type == EventEntry::Type::SENSOR) { |
| 1805 | it = mInboundQueue.erase(it); |
| 1806 | releaseInboundEventLocked(entry); |
| 1807 | } |
| 1808 | } |
| 1809 | } |
| 1810 | return true; |
| 1811 | } |
| 1812 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1813 | bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, std::shared_ptr<MotionEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1814 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1815 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1816 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1817 | if (!entry->dispatchInProgress) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1818 | entry->dispatchInProgress = true; |
| 1819 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1820 | logOutboundMotionDetails("dispatchMotion - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1821 | } |
| 1822 | |
| 1823 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1824 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1825 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1826 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1827 | : InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1828 | return true; |
| 1829 | } |
| 1830 | |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 1831 | const bool isPointerEvent = isFromSource(entry->source, AINPUT_SOURCE_CLASS_POINTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1832 | |
| 1833 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1834 | std::vector<InputTarget> inputTargets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1835 | |
| 1836 | bool conflictingPointerActions = false; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1837 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1838 | if (isPointerEvent) { |
| 1839 | // Pointer event. (eg. touchscreen) |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 1840 | |
| 1841 | if (mDragState && |
| 1842 | (entry->action & AMOTION_EVENT_ACTION_MASK) == AMOTION_EVENT_ACTION_POINTER_DOWN) { |
| 1843 | // If drag and drop ongoing and pointer down occur: pilfer drag window pointers |
| 1844 | pilferPointersLocked(mDragState->dragWindow->getToken()); |
| 1845 | } |
| 1846 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1847 | inputTargets = |
Siarhei Vishniakou | 4fe5739 | 2022-10-25 13:44:30 -0700 | [diff] [blame] | 1848 | findTouchedWindowTargetsLocked(currentTime, *entry, &conflictingPointerActions, |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1849 | /*byref*/ injectionResult); |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 1850 | LOG_ALWAYS_FATAL_IF(injectionResult != InputEventInjectionResult::SUCCEEDED && |
| 1851 | !inputTargets.empty()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1852 | } else { |
| 1853 | // Non touch event. (eg. trackball) |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1854 | sp<WindowInfoHandle> focusedWindow = |
| 1855 | findFocusedWindowTargetLocked(currentTime, *entry, nextWakeupTime, injectionResult); |
| 1856 | if (injectionResult == InputEventInjectionResult::SUCCEEDED) { |
| 1857 | LOG_ALWAYS_FATAL_IF(focusedWindow == nullptr); |
| 1858 | addWindowTargetLocked(focusedWindow, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 1859 | InputTarget::Flags::FOREGROUND | |
| 1860 | InputTarget::Flags::DISPATCH_AS_IS, |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 1861 | /*pointerIds=*/{}, getDownTime(*entry), inputTargets); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1862 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1863 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1864 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1865 | return false; |
| 1866 | } |
| 1867 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1868 | setInjectionResult(*entry, injectionResult); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1869 | if (injectionResult == InputEventInjectionResult::TARGET_MISMATCH) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1870 | return true; |
| 1871 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1872 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1873 | CancelationOptions::Mode mode( |
| 1874 | isPointerEvent ? CancelationOptions::Mode::CANCEL_POINTER_EVENTS |
| 1875 | : CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS); |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1876 | CancelationOptions options(mode, "input event injection failed"); |
| 1877 | synthesizeCancelationEventsForMonitorsLocked(options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1878 | return true; |
| 1879 | } |
| 1880 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1881 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1882 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1883 | |
| 1884 | // Dispatch the motion. |
| 1885 | if (conflictingPointerActions) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1886 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1887 | "conflicting pointer actions"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1888 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1889 | } |
| 1890 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1891 | return true; |
| 1892 | } |
| 1893 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1894 | void InputDispatcher::enqueueDragEventLocked(const sp<WindowInfoHandle>& windowHandle, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 1895 | bool isExiting, const int32_t rawX, |
| 1896 | const int32_t rawY) { |
| 1897 | const vec2 xy = windowHandle->getInfo()->transform.transform(vec2(rawX, rawY)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1898 | std::unique_ptr<DragEntry> dragEntry = |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 1899 | std::make_unique<DragEntry>(mIdGenerator.nextId(), now(), windowHandle->getToken(), |
| 1900 | isExiting, xy.x, xy.y); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1901 | |
| 1902 | enqueueInboundEventLocked(std::move(dragEntry)); |
| 1903 | } |
| 1904 | |
| 1905 | void InputDispatcher::dispatchDragLocked(nsecs_t currentTime, std::shared_ptr<DragEntry> entry) { |
| 1906 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
| 1907 | if (channel == nullptr) { |
| 1908 | return; // Window has gone away |
| 1909 | } |
| 1910 | InputTarget target; |
| 1911 | target.inputChannel = channel; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 1912 | target.flags = InputTarget::Flags::DISPATCH_AS_IS; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1913 | entry->dispatchInProgress = true; |
| 1914 | dispatchEventLocked(currentTime, entry, {target}); |
| 1915 | } |
| 1916 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1917 | void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1918 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 1919 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=%s, displayId=%" PRId32 |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1920 | ", policyFlags=0x%x, " |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1921 | "action=%s, actionButton=0x%x, flags=0x%x, " |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1922 | "metaState=0x%x, buttonState=0x%x," |
| 1923 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64, |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 1924 | prefix, entry.eventTime, entry.deviceId, |
| 1925 | inputEventSourceToString(entry.source).c_str(), entry.displayId, entry.policyFlags, |
| 1926 | MotionEvent::actionToString(entry.action).c_str(), entry.actionButton, entry.flags, |
| 1927 | entry.metaState, entry.buttonState, entry.edgeFlags, entry.xPrecision, |
| 1928 | entry.yPrecision, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1929 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1930 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1931 | ALOGD(" Pointer %d: id=%d, toolType=%s, " |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1932 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 1933 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 1934 | "orientation=%f", |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1935 | i, entry.pointerProperties[i].id, |
| 1936 | ftl::enum_string(entry.pointerProperties[i].toolType).c_str(), |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1937 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 1938 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 1939 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 1940 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 1941 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 1942 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 1943 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 1944 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 1945 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| 1946 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1947 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1948 | } |
| 1949 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1950 | void InputDispatcher::dispatchEventLocked(nsecs_t currentTime, |
| 1951 | std::shared_ptr<EventEntry> eventEntry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1952 | const std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1953 | ATRACE_CALL(); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1954 | if (DEBUG_DISPATCH_CYCLE) { |
| 1955 | ALOGD("dispatchEventToCurrentInputTargets"); |
| 1956 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1957 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1958 | updateInteractionTokensLocked(*eventEntry, inputTargets); |
| 1959 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1960 | ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true |
| 1961 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1962 | pokeUserActivityLocked(*eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1963 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1964 | for (const InputTarget& inputTarget : inputTargets) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 1965 | std::shared_ptr<Connection> connection = |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 1966 | getConnectionLocked(inputTarget.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 1967 | if (connection != nullptr) { |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 1968 | prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1969 | } else { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1970 | if (DEBUG_FOCUS) { |
| 1971 | ALOGD("Dropping event delivery to target with channel '%s' because it " |
| 1972 | "is no longer registered with the input dispatcher.", |
| 1973 | inputTarget.inputChannel->getName().c_str()); |
| 1974 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1975 | } |
| 1976 | } |
| 1977 | } |
| 1978 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 1979 | void InputDispatcher::cancelEventsForAnrLocked(const std::shared_ptr<Connection>& connection) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1980 | // We will not be breaking any connections here, even if the policy wants us to abort dispatch. |
| 1981 | // If the policy decides to close the app, we will get a channel removal event via |
| 1982 | // unregisterInputChannel, and will clean up the connection that way. We are already not |
| 1983 | // sending new pointers to the connection when it blocked, but focused events will continue to |
| 1984 | // pile up. |
| 1985 | ALOGW("Canceling events for %s because it is unresponsive", |
| 1986 | connection->inputChannel->getName().c_str()); |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 1987 | if (connection->status == Connection::Status::NORMAL) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1988 | CancelationOptions options(CancelationOptions::Mode::CANCEL_ALL_EVENTS, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1989 | "application not responding"); |
| 1990 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1991 | } |
| 1992 | } |
| 1993 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1994 | void InputDispatcher::resetNoFocusedWindowTimeoutLocked() { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1995 | if (DEBUG_FOCUS) { |
| 1996 | ALOGD("Resetting ANR timeouts."); |
| 1997 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1998 | |
| 1999 | // Reset input target wait timeout. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2000 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2001 | mAwaitedFocusedApplication.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2002 | } |
| 2003 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2004 | /** |
| 2005 | * Get the display id that the given event should go to. If this event specifies a valid display id, |
| 2006 | * then it should be dispatched to that display. Otherwise, the event goes to the focused display. |
| 2007 | * Focused display is the display that the user most recently interacted with. |
| 2008 | */ |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2009 | int32_t InputDispatcher::getTargetDisplayId(const EventEntry& entry) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2010 | int32_t displayId; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2011 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2012 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2013 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 2014 | displayId = keyEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2015 | break; |
| 2016 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2017 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2018 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 2019 | displayId = motionEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2020 | break; |
| 2021 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 2022 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 2023 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2024 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2025 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2026 | case EventEntry::Type::DEVICE_RESET: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2027 | case EventEntry::Type::SENSOR: |
| 2028 | case EventEntry::Type::DRAG: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2029 | 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] | 2030 | return ADISPLAY_ID_NONE; |
| 2031 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2032 | } |
| 2033 | return displayId == ADISPLAY_ID_NONE ? mFocusedDisplayId : displayId; |
| 2034 | } |
| 2035 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2036 | bool InputDispatcher::shouldWaitToSendKeyLocked(nsecs_t currentTime, |
| 2037 | const char* focusedWindowName) { |
| 2038 | if (mAnrTracker.empty()) { |
| 2039 | // already processed all events that we waited for |
| 2040 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 2041 | return false; |
| 2042 | } |
| 2043 | |
| 2044 | if (!mKeyIsWaitingForEventsTimeout.has_value()) { |
| 2045 | // Start the timer |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 2046 | // 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] | 2047 | mKeyIsWaitingForEventsTimeout = currentTime + |
| 2048 | std::chrono::duration_cast<std::chrono::nanoseconds>(KEY_WAITING_FOR_EVENTS_TIMEOUT) |
| 2049 | .count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2050 | return true; |
| 2051 | } |
| 2052 | |
| 2053 | // We still have pending events, and already started the timer |
| 2054 | if (currentTime < *mKeyIsWaitingForEventsTimeout) { |
| 2055 | return true; // Still waiting |
| 2056 | } |
| 2057 | |
| 2058 | // Waited too long, and some connection still hasn't processed all motions |
| 2059 | // Just send the key to the focused window |
| 2060 | ALOGW("Dispatching key to %s even though there are other unprocessed events", |
| 2061 | focusedWindowName); |
| 2062 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 2063 | return false; |
| 2064 | } |
| 2065 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2066 | sp<WindowInfoHandle> InputDispatcher::findFocusedWindowTargetLocked( |
| 2067 | nsecs_t currentTime, const EventEntry& entry, nsecs_t* nextWakeupTime, |
| 2068 | InputEventInjectionResult& outInjectionResult) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2069 | std::string reason; |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2070 | outInjectionResult = InputEventInjectionResult::FAILED; // Default result |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2071 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2072 | int32_t displayId = getTargetDisplayId(entry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2073 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2074 | std::shared_ptr<InputApplicationHandle> focusedApplicationHandle = |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2075 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 2076 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2077 | // If there is no currently focused window and no focused application |
| 2078 | // then drop the event. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2079 | if (focusedWindowHandle == nullptr && focusedApplicationHandle == nullptr) { |
| 2080 | ALOGI("Dropping %s event because there is no focused window or focused application in " |
| 2081 | "display %" PRId32 ".", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2082 | ftl::enum_string(entry.type).c_str(), displayId); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2083 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2084 | } |
| 2085 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2086 | // Drop key events if requested by input feature |
| 2087 | if (focusedWindowHandle != nullptr && shouldDropInput(entry, focusedWindowHandle)) { |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2088 | return nullptr; |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2089 | } |
| 2090 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2091 | // Compatibility behavior: raise ANR if there is a focused application, but no focused window. |
| 2092 | // Only start counting when we have a focused event to dispatch. The ANR is canceled if we |
| 2093 | // start interacting with another application via touch (app switch). This code can be removed |
| 2094 | // if the "no focused window ANR" is moved to the policy. Input doesn't know whether |
| 2095 | // an app is expected to have a focused window. |
| 2096 | if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) { |
| 2097 | if (!mNoFocusedWindowTimeoutTime.has_value()) { |
| 2098 | // 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] | 2099 | std::chrono::nanoseconds timeout = focusedApplicationHandle->getDispatchingTimeout( |
| 2100 | DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
| 2101 | mNoFocusedWindowTimeoutTime = currentTime + timeout.count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2102 | mAwaitedFocusedApplication = focusedApplicationHandle; |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 2103 | mAwaitedApplicationDisplayId = displayId; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2104 | ALOGW("Waiting because no window has focus but %s may eventually add a " |
| 2105 | "window when it finishes starting up. Will wait for %" PRId64 "ms", |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 2106 | mAwaitedFocusedApplication->getName().c_str(), millis(timeout)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2107 | *nextWakeupTime = *mNoFocusedWindowTimeoutTime; |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2108 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 2109 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2110 | } else if (currentTime > *mNoFocusedWindowTimeoutTime) { |
| 2111 | // Already raised ANR. Drop the event |
| 2112 | ALOGE("Dropping %s event because there is no focused window", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2113 | ftl::enum_string(entry.type).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2114 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2115 | } else { |
| 2116 | // Still waiting for the focused window |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2117 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 2118 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2119 | } |
| 2120 | } |
| 2121 | |
| 2122 | // we have a valid, non-null focused window |
| 2123 | resetNoFocusedWindowTimeoutLocked(); |
| 2124 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2125 | // Verify targeted injection. |
| 2126 | if (const auto err = verifyTargetedInjection(focusedWindowHandle, entry); err) { |
| 2127 | ALOGW("Dropping injected event: %s", (*err).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2128 | outInjectionResult = InputEventInjectionResult::TARGET_MISMATCH; |
| 2129 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2130 | } |
| 2131 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2132 | if (focusedWindowHandle->getInfo()->inputConfig.test( |
| 2133 | WindowInfo::InputConfig::PAUSE_DISPATCHING)) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2134 | ALOGI("Waiting because %s is paused", focusedWindowHandle->getName().c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2135 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 2136 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2137 | } |
| 2138 | |
| 2139 | // If the event is a key event, then we must wait for all previous events to |
| 2140 | // complete before delivering it because previous events may have the |
| 2141 | // side-effect of transferring focus to a different window and we want to |
| 2142 | // ensure that the following keys are sent to the new window. |
| 2143 | // |
| 2144 | // Suppose the user touches a button in a window then immediately presses "A". |
| 2145 | // If the button causes a pop-up window to appear then we want to ensure that |
| 2146 | // the "A" key is delivered to the new pop-up window. This is because users |
| 2147 | // often anticipate pending UI changes when typing on a keyboard. |
| 2148 | // To obtain this behavior, we must serialize key events with respect to all |
| 2149 | // prior input events. |
| 2150 | if (entry.type == EventEntry::Type::KEY) { |
| 2151 | if (shouldWaitToSendKeyLocked(currentTime, focusedWindowHandle->getName().c_str())) { |
| 2152 | *nextWakeupTime = *mKeyIsWaitingForEventsTimeout; |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2153 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 2154 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2155 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2156 | } |
| 2157 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2158 | outInjectionResult = InputEventInjectionResult::SUCCEEDED; |
| 2159 | return focusedWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2160 | } |
| 2161 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2162 | /** |
| 2163 | * Given a list of monitors, remove the ones we cannot find a connection for, and the ones |
| 2164 | * that are currently unresponsive. |
| 2165 | */ |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2166 | std::vector<Monitor> InputDispatcher::selectResponsiveMonitorsLocked( |
| 2167 | const std::vector<Monitor>& monitors) const { |
| 2168 | std::vector<Monitor> responsiveMonitors; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2169 | std::copy_if(monitors.begin(), monitors.end(), std::back_inserter(responsiveMonitors), |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2170 | [this](const Monitor& monitor) REQUIRES(mLock) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 2171 | std::shared_ptr<Connection> connection = |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2172 | getConnectionLocked(monitor.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2173 | if (connection == nullptr) { |
| 2174 | ALOGE("Could not find connection for monitor %s", |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2175 | monitor.inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2176 | return false; |
| 2177 | } |
| 2178 | if (!connection->responsive) { |
| 2179 | ALOGW("Unresponsive monitor %s will not get the new gesture", |
| 2180 | connection->inputChannel->getName().c_str()); |
| 2181 | return false; |
| 2182 | } |
| 2183 | return true; |
| 2184 | }); |
| 2185 | return responsiveMonitors; |
| 2186 | } |
| 2187 | |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 2188 | /** |
| 2189 | * In general, touch should be always split between windows. Some exceptions: |
| 2190 | * 1. Don't split touch is if we have an active pointer down, and a new pointer is going down that's |
| 2191 | * from the same device, *and* the window that's receiving the current pointer does not support |
| 2192 | * split touch. |
| 2193 | * 2. Don't split mouse events |
| 2194 | */ |
| 2195 | bool InputDispatcher::shouldSplitTouch(const TouchState& touchState, |
| 2196 | const MotionEntry& entry) const { |
| 2197 | if (isFromSource(entry.source, AINPUT_SOURCE_MOUSE)) { |
| 2198 | // We should never split mouse events |
| 2199 | return false; |
| 2200 | } |
| 2201 | for (const TouchedWindow& touchedWindow : touchState.windows) { |
| 2202 | if (touchedWindow.windowHandle->getInfo()->isSpy()) { |
| 2203 | // Spy windows should not affect whether or not touch is split. |
| 2204 | continue; |
| 2205 | } |
| 2206 | if (touchedWindow.windowHandle->getInfo()->supportsSplitTouch()) { |
| 2207 | continue; |
| 2208 | } |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2209 | if (touchedWindow.windowHandle->getInfo()->inputConfig.test( |
| 2210 | gui::WindowInfo::InputConfig::IS_WALLPAPER)) { |
| 2211 | // Wallpaper window should not affect whether or not touch is split |
| 2212 | continue; |
| 2213 | } |
| 2214 | |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 2215 | // Eventually, touchedWindow will contain the deviceId of each pointer that's currently |
| 2216 | // being sent there. For now, use deviceId from touch state. |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 2217 | if (entry.deviceId == touchState.deviceId && touchedWindow.pointerIds.any()) { |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 2218 | return false; |
| 2219 | } |
| 2220 | } |
| 2221 | return true; |
| 2222 | } |
| 2223 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2224 | std::vector<InputTarget> InputDispatcher::findTouchedWindowTargetsLocked( |
Siarhei Vishniakou | 4fe5739 | 2022-10-25 13:44:30 -0700 | [diff] [blame] | 2225 | nsecs_t currentTime, const MotionEntry& entry, bool* outConflictingPointerActions, |
| 2226 | InputEventInjectionResult& outInjectionResult) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2227 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2228 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2229 | std::vector<InputTarget> targets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2230 | // For security reasons, we defer updating the touch state until we are sure that |
| 2231 | // event injection will be allowed. |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2232 | const int32_t displayId = entry.displayId; |
| 2233 | const int32_t action = entry.action; |
Siarhei Vishniakou | bf88052 | 2023-05-01 11:03:22 -0700 | [diff] [blame] | 2234 | const int32_t maskedAction = MotionEvent::getActionMasked(action); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2235 | |
| 2236 | // Update the touch state as needed based on the properties of the touch event. |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2237 | outInjectionResult = InputEventInjectionResult::PENDING; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2238 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2239 | // Copy current touch state into tempTouchState. |
| 2240 | // This state will be used to update mTouchStatesByDisplay at the end of this function. |
| 2241 | // 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] | 2242 | const TouchState* oldState = nullptr; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2243 | TouchState tempTouchState; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2244 | if (const auto it = mTouchStatesByDisplay.find(displayId); it != mTouchStatesByDisplay.end()) { |
| 2245 | oldState = &(it->second); |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 2246 | tempTouchState = *oldState; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2247 | } |
| 2248 | |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 2249 | bool isSplit = shouldSplitTouch(tempTouchState, entry); |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 2250 | const bool switchedDevice = (oldState != nullptr) && |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2251 | (oldState->deviceId != entry.deviceId || oldState->source != entry.source); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2252 | |
| 2253 | const bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE || |
| 2254 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2255 | maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2256 | // A DOWN could be generated from POINTER_DOWN if the initial pointers did not land into any |
| 2257 | // touchable windows. |
| 2258 | const bool wasDown = oldState != nullptr && oldState->isDown(); |
| 2259 | const bool isDown = (maskedAction == AMOTION_EVENT_ACTION_DOWN) || |
| 2260 | (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN && !wasDown); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2261 | const bool newGesture = isDown || maskedAction == AMOTION_EVENT_ACTION_SCROLL || |
| 2262 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2263 | maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE; |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 2264 | const bool isFromMouse = isFromSource(entry.source, AINPUT_SOURCE_MOUSE); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2265 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2266 | // If pointers are already down, let's finish the current gesture and ignore the new events |
| 2267 | // from another device. However, if the new event is a down event, let's cancel the current |
| 2268 | // touch and let the new one take over. |
| 2269 | if (switchedDevice && wasDown && !isDown) { |
| 2270 | LOG(INFO) << "Dropping event because a pointer for device " << oldState->deviceId |
| 2271 | << " is already down in display " << displayId << ": " << entry.getDescription(); |
| 2272 | // TODO(b/211379801): test multiple simultaneous input streams. |
| 2273 | outInjectionResult = InputEventInjectionResult::FAILED; |
| 2274 | return {}; // wrong device |
| 2275 | } |
| 2276 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2277 | if (newGesture) { |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2278 | // If a new gesture is starting, clear the touch state completely. |
| 2279 | tempTouchState.reset(); |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2280 | tempTouchState.deviceId = entry.deviceId; |
| 2281 | tempTouchState.source = entry.source; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2282 | isSplit = false; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2283 | } else if (switchedDevice && maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2284 | ALOGI("Dropping move event because a pointer for a different device is already active " |
| 2285 | "in display %" PRId32, |
| 2286 | displayId); |
Siarhei Vishniakou | 16e4fa0 | 2023-02-16 17:48:56 -0800 | [diff] [blame] | 2287 | // TODO(b/211379801): test multiple simultaneous input streams. |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2288 | outInjectionResult = InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2289 | return {}; // wrong device |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2290 | } |
| 2291 | |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2292 | if (isHoverAction) { |
| 2293 | // For hover actions, we will treat 'tempTouchState' as a new state, so let's erase |
| 2294 | // all of the existing hovering pointers and recompute. |
| 2295 | tempTouchState.clearHoveringPointers(); |
| 2296 | } |
| 2297 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2298 | if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { |
| 2299 | /* Case 1: New splittable pointer going down, or need target for hover or scroll. */ |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 2300 | const auto [x, y] = resolveTouchedPosition(entry); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2301 | const int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2302 | // Outside targets should be added upon first dispatched DOWN event. That means, this should |
| 2303 | // be a pointer that would generate ACTION_DOWN, *and* touch should not already be down. |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2304 | const bool isStylus = isPointerFromStylus(entry, pointerIndex); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2305 | auto [newTouchedWindowHandle, outsideTargets] = |
| 2306 | findTouchedWindowAtLocked(displayId, x, y, isStylus); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2307 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2308 | if (isDown) { |
| 2309 | targets += outsideTargets; |
| 2310 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2311 | // Handle the case where we did not find a window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2312 | if (newTouchedWindowHandle == nullptr) { |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 2313 | ALOGD("No new touched window at (%.1f, %.1f) in display %" PRId32, x, y, displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2314 | // 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] | 2315 | newTouchedWindowHandle = tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2316 | } |
| 2317 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2318 | // Verify targeted injection. |
| 2319 | if (const auto err = verifyTargetedInjection(newTouchedWindowHandle, entry); err) { |
| 2320 | ALOGW("Dropping injected touch event: %s", (*err).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2321 | outInjectionResult = os::InputEventInjectionResult::TARGET_MISMATCH; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2322 | newTouchedWindowHandle = nullptr; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2323 | return {}; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2324 | } |
| 2325 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2326 | // Figure out whether splitting will be allowed for this window. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2327 | if (newTouchedWindowHandle != nullptr) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2328 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
| 2329 | // New window supports splitting, but we should never split mouse events. |
| 2330 | isSplit = !isFromMouse; |
| 2331 | } else if (isSplit) { |
| 2332 | // New window does not support splitting but we have already split events. |
| 2333 | // Ignore the new window. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2334 | newTouchedWindowHandle = nullptr; |
| 2335 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2336 | } else { |
| 2337 | // No window is touched, so set split to true. This will allow the next pointer down to |
Prabir Pradhan | 713bb3e | 2021-12-20 02:07:40 -0800 | [diff] [blame] | 2338 | // be delivered to a new window which supports split touch. Pointers from a mouse device |
| 2339 | // should never be split. |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 2340 | isSplit = !isFromMouse; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2341 | } |
| 2342 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2343 | std::vector<sp<WindowInfoHandle>> newTouchedWindows = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2344 | findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2345 | if (newTouchedWindowHandle != nullptr) { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2346 | // Process the foreground window first so that it is the first to receive the event. |
| 2347 | newTouchedWindows.insert(newTouchedWindows.begin(), newTouchedWindowHandle); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2348 | } |
| 2349 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2350 | if (newTouchedWindows.empty()) { |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 2351 | ALOGI("Dropping event because there is no touchable window at (%.1f, %.1f) on display " |
| 2352 | "%d.", |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2353 | x, y, displayId); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2354 | outInjectionResult = InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2355 | return {}; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2356 | } |
| 2357 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2358 | for (const sp<WindowInfoHandle>& windowHandle : newTouchedWindows) { |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 2359 | if (!canWindowReceiveMotionLocked(windowHandle, entry)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2360 | continue; |
| 2361 | } |
| 2362 | |
Siarhei Vishniakou | b681c20 | 2023-05-01 11:22:33 -0700 | [diff] [blame] | 2363 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2364 | maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) { |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2365 | const int32_t pointerId = entry.pointerProperties[0].id; |
Siarhei Vishniakou | b681c20 | 2023-05-01 11:22:33 -0700 | [diff] [blame] | 2366 | // The "windowHandle" is the target of this hovering pointer. |
| 2367 | tempTouchState.addHoveringPointerToWindow(windowHandle, entry.deviceId, pointerId); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2368 | } |
| 2369 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2370 | // Set target flags. |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2371 | ftl::Flags<InputTarget::Flags> targetFlags = InputTarget::Flags::DISPATCH_AS_IS; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2372 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2373 | if (canReceiveForegroundTouches(*windowHandle->getInfo())) { |
| 2374 | // There should only be one touched window that can be "foreground" for the pointer. |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2375 | targetFlags |= InputTarget::Flags::FOREGROUND; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2376 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2377 | |
| 2378 | if (isSplit) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2379 | targetFlags |= InputTarget::Flags::SPLIT; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2380 | } |
| 2381 | if (isWindowObscuredAtPointLocked(windowHandle, x, y)) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2382 | targetFlags |= InputTarget::Flags::WINDOW_IS_OBSCURED; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2383 | } else if (isWindowObscuredLocked(windowHandle)) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2384 | targetFlags |= InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2385 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2386 | |
| 2387 | // Update the temporary touch state. |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 2388 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2389 | if (!isHoverAction) { |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 2390 | pointerIds.set(entry.pointerProperties[pointerIndex].id); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2391 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2392 | |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2393 | const bool isDownOrPointerDown = maskedAction == AMOTION_EVENT_ACTION_DOWN || |
| 2394 | maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN; |
| 2395 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2396 | // TODO(b/211379801): Currently, even if pointerIds are empty (hover case), we would |
| 2397 | // still add a window to the touch state. We should avoid doing that, but some of the |
| 2398 | // later checks ("at least one foreground window") rely on this in order to dispatch |
| 2399 | // the event properly, so that needs to be updated, possibly by looking at InputTargets. |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2400 | tempTouchState.addOrUpdateWindow(windowHandle, targetFlags, pointerIds, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2401 | isDownOrPointerDown |
| 2402 | ? std::make_optional(entry.eventTime) |
| 2403 | : std::nullopt); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2404 | |
| 2405 | // If this is the pointer going down and the touched window has a wallpaper |
| 2406 | // then also add the touched wallpaper windows so they are locked in for the duration |
| 2407 | // of the touch gesture. |
| 2408 | // We do not collect wallpapers during HOVER_MOVE or SCROLL because the wallpaper |
| 2409 | // engine only supports touch events. We would need to add a mechanism similar |
| 2410 | // to View.onGenericMotionEvent to enable wallpapers to handle these events. |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2411 | if (isDownOrPointerDown) { |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2412 | if (targetFlags.test(InputTarget::Flags::FOREGROUND) && |
| 2413 | windowHandle->getInfo()->inputConfig.test( |
| 2414 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER)) { |
| 2415 | sp<WindowInfoHandle> wallpaper = findWallpaperWindowBelow(windowHandle); |
| 2416 | if (wallpaper != nullptr) { |
| 2417 | ftl::Flags<InputTarget::Flags> wallpaperFlags = |
| 2418 | InputTarget::Flags::WINDOW_IS_OBSCURED | |
| 2419 | InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED | |
| 2420 | InputTarget::Flags::DISPATCH_AS_IS; |
| 2421 | if (isSplit) { |
| 2422 | wallpaperFlags |= InputTarget::Flags::SPLIT; |
| 2423 | } |
| 2424 | tempTouchState.addOrUpdateWindow(wallpaper, wallpaperFlags, pointerIds, |
| 2425 | entry.eventTime); |
| 2426 | } |
| 2427 | } |
| 2428 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2429 | } |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 2430 | |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 2431 | // If a window is already pilfering some pointers, give it this new pointer as well and |
| 2432 | // make it pilfering. This will prevent other non-spy windows from getting this pointer, |
| 2433 | // which is a specific behaviour that we want. |
| 2434 | const int32_t pointerId = entry.pointerProperties[pointerIndex].id; |
| 2435 | for (TouchedWindow& touchedWindow : tempTouchState.windows) { |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 2436 | if (touchedWindow.pointerIds.test(pointerId) && |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 2437 | touchedWindow.pilferedPointerIds.count() > 0) { |
| 2438 | // This window is already pilfering some pointers, and this new pointer is also |
| 2439 | // going to it. Therefore, take over this pointer and don't give it to anyone |
| 2440 | // else. |
| 2441 | touchedWindow.pilferedPointerIds.set(pointerId); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 2442 | } |
| 2443 | } |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 2444 | |
| 2445 | // Restrict all pilfered pointers to the pilfering windows. |
| 2446 | tempTouchState.cancelPointersForNonPilferingWindows(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2447 | } else { |
| 2448 | /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ |
| 2449 | |
| 2450 | // If the pointer is not currently down, then ignore the event. |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2451 | if (!tempTouchState.isDown() && maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT) { |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2452 | LOG(INFO) << "Dropping event because the pointer is not down or we previously " |
| 2453 | "dropped the pointer down event in display " |
| 2454 | << displayId << ": " << entry.getDescription(); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2455 | outInjectionResult = InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2456 | return {}; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2457 | } |
| 2458 | |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2459 | // If the pointer is not currently hovering, then ignore the event. |
| 2460 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 2461 | const int32_t pointerId = entry.pointerProperties[0].id; |
| 2462 | if (oldState == nullptr || |
| 2463 | oldState->getWindowsWithHoveringPointer(entry.deviceId, pointerId).empty()) { |
| 2464 | LOG(INFO) << "Dropping event because the hovering pointer is not in any windows in " |
| 2465 | "display " |
| 2466 | << displayId << ": " << entry.getDescription(); |
| 2467 | outInjectionResult = InputEventInjectionResult::FAILED; |
| 2468 | return {}; |
| 2469 | } |
| 2470 | tempTouchState.removeHoveringPointer(entry.deviceId, pointerId); |
| 2471 | } |
| 2472 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2473 | addDragEventLocked(entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2474 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2475 | // Check whether touches should slip outside of the current foreground window. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2476 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.pointerCount == 1 && |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2477 | tempTouchState.isSlippery()) { |
Siarhei Vishniakou | 9306c38 | 2022-09-30 15:30:31 -0700 | [diff] [blame] | 2478 | const auto [x, y] = resolveTouchedPosition(entry); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2479 | const bool isStylus = isPointerFromStylus(entry, /*pointerIndex=*/0); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2480 | sp<WindowInfoHandle> oldTouchedWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2481 | tempTouchState.getFirstForegroundWindowHandle(); |
Siarhei Vishniakou | 0f6558d | 2023-04-21 12:05:13 -0700 | [diff] [blame] | 2482 | LOG_ALWAYS_FATAL_IF(oldTouchedWindowHandle == nullptr); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2483 | auto [newTouchedWindowHandle, _] = findTouchedWindowAtLocked(displayId, x, y, isStylus); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2484 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2485 | // Verify targeted injection. |
| 2486 | if (const auto err = verifyTargetedInjection(newTouchedWindowHandle, entry); err) { |
| 2487 | ALOGW("Dropping injected event: %s", (*err).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2488 | outInjectionResult = os::InputEventInjectionResult::TARGET_MISMATCH; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2489 | return {}; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2490 | } |
| 2491 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2492 | // Drop touch events if requested by input feature |
| 2493 | if (newTouchedWindowHandle != nullptr && |
| 2494 | shouldDropInput(entry, newTouchedWindowHandle)) { |
| 2495 | newTouchedWindowHandle = nullptr; |
| 2496 | } |
| 2497 | |
Siarhei Vishniakou | afa08cc | 2023-05-08 22:35:50 -0700 | [diff] [blame] | 2498 | if (newTouchedWindowHandle != nullptr && |
| 2499 | !haveSameToken(oldTouchedWindowHandle, newTouchedWindowHandle)) { |
Siarhei Vishniakou | 0f6558d | 2023-04-21 12:05:13 -0700 | [diff] [blame] | 2500 | ALOGD("Touch is slipping out of window %s into window %s in display %" PRId32, |
| 2501 | oldTouchedWindowHandle->getName().c_str(), |
| 2502 | newTouchedWindowHandle->getName().c_str(), displayId); |
| 2503 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2504 | // Make a slippery exit from the old window. |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 2505 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2506 | const int32_t pointerId = entry.pointerProperties[0].id; |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 2507 | pointerIds.set(pointerId); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2508 | |
| 2509 | const TouchedWindow& touchedWindow = |
| 2510 | tempTouchState.getTouchedWindow(oldTouchedWindowHandle); |
| 2511 | addWindowTargetLocked(oldTouchedWindowHandle, |
| 2512 | InputTarget::Flags::DISPATCH_AS_SLIPPERY_EXIT, pointerIds, |
| 2513 | touchedWindow.firstDownTimeInTarget, targets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2514 | |
| 2515 | // Make a slippery entrance into the new window. |
| 2516 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
Prabir Pradhan | 713bb3e | 2021-12-20 02:07:40 -0800 | [diff] [blame] | 2517 | isSplit = !isFromMouse; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2518 | } |
| 2519 | |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2520 | ftl::Flags<InputTarget::Flags> targetFlags = |
| 2521 | InputTarget::Flags::DISPATCH_AS_SLIPPERY_ENTER; |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2522 | if (canReceiveForegroundTouches(*newTouchedWindowHandle->getInfo())) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2523 | targetFlags |= InputTarget::Flags::FOREGROUND; |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2524 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2525 | if (isSplit) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2526 | targetFlags |= InputTarget::Flags::SPLIT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2527 | } |
| 2528 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2529 | targetFlags |= InputTarget::Flags::WINDOW_IS_OBSCURED; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2530 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2531 | targetFlags |= InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2532 | } |
| 2533 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2534 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds, |
| 2535 | entry.eventTime); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2536 | |
| 2537 | // Check if the wallpaper window should deliver the corresponding event. |
| 2538 | slipWallpaperTouch(targetFlags, oldTouchedWindowHandle, newTouchedWindowHandle, |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2539 | tempTouchState, pointerId, targets); |
| 2540 | tempTouchState.removeTouchedPointerFromWindow(pointerId, oldTouchedWindowHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2541 | } |
| 2542 | } |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 2543 | |
| 2544 | // Update the pointerIds for non-splittable when it received pointer down. |
| 2545 | if (!isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN) { |
| 2546 | // If no split, we suppose all touched windows should receive pointer down. |
| 2547 | const int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
| 2548 | for (size_t i = 0; i < tempTouchState.windows.size(); i++) { |
| 2549 | TouchedWindow& touchedWindow = tempTouchState.windows[i]; |
| 2550 | // Ignore drag window for it should just track one pointer. |
| 2551 | if (mDragState && mDragState->dragWindow == touchedWindow.windowHandle) { |
| 2552 | continue; |
| 2553 | } |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 2554 | touchedWindow.pointerIds.set(entry.pointerProperties[pointerIndex].id); |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 2555 | } |
| 2556 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2557 | } |
| 2558 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2559 | // Update dispatching for hover enter and exit. |
Sam Dubey | f886dec | 2023-01-27 13:28:19 +0000 | [diff] [blame] | 2560 | { |
| 2561 | std::vector<TouchedWindow> hoveringWindows = |
| 2562 | getHoveringWindowsLocked(oldState, tempTouchState, entry); |
| 2563 | for (const TouchedWindow& touchedWindow : hoveringWindows) { |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame^] | 2564 | std::optional<InputTarget> target = |
| 2565 | createInputTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags, |
| 2566 | touchedWindow.firstDownTimeInTarget); |
| 2567 | if (!target) { |
| 2568 | continue; |
| 2569 | } |
| 2570 | // Hardcode to single hovering pointer for now. |
| 2571 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
| 2572 | pointerIds.set(entry.pointerProperties[0].id); |
| 2573 | target->addPointers(pointerIds, touchedWindow.windowHandle->getInfo()->transform); |
| 2574 | targets.push_back(*target); |
Sam Dubey | f886dec | 2023-01-27 13:28:19 +0000 | [diff] [blame] | 2575 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2576 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2577 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2578 | // Ensure that all touched windows are valid for injection. |
| 2579 | if (entry.injectionState != nullptr) { |
| 2580 | std::string errs; |
| 2581 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2582 | if (touchedWindow.targetFlags.test(InputTarget::Flags::DISPATCH_AS_OUTSIDE)) { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2583 | // Allow ACTION_OUTSIDE events generated by targeted injection to be |
| 2584 | // dispatched to any uid, since the coords will be zeroed out later. |
| 2585 | continue; |
| 2586 | } |
| 2587 | const auto err = verifyTargetedInjection(touchedWindow.windowHandle, entry); |
| 2588 | if (err) errs += "\n - " + *err; |
| 2589 | } |
| 2590 | if (!errs.empty()) { |
| 2591 | ALOGW("Dropping targeted injection: At least one touched window is not owned by uid " |
| 2592 | "%d:%s", |
| 2593 | *entry.injectionState->targetUid, errs.c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2594 | outInjectionResult = InputEventInjectionResult::TARGET_MISMATCH; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2595 | return {}; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2596 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2597 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2598 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2599 | // Check whether windows listening for outside touches are owned by the same UID. If the owner |
| 2600 | // has a different UID, then we will not reveal coordinate information to this window. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2601 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2602 | sp<WindowInfoHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2603 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2604 | if (foregroundWindowHandle) { |
| 2605 | const int32_t foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2606 | for (InputTarget& target : targets) { |
| 2607 | if (target.flags.test(InputTarget::Flags::DISPATCH_AS_OUTSIDE)) { |
| 2608 | sp<WindowInfoHandle> targetWindow = |
| 2609 | getWindowHandleLocked(target.inputChannel->getConnectionToken()); |
| 2610 | if (targetWindow->getInfo()->ownerUid != foregroundWindowUid) { |
| 2611 | target.flags |= InputTarget::Flags::ZERO_COORDS; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2612 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2613 | } |
| 2614 | } |
| 2615 | } |
| 2616 | } |
| 2617 | |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 2618 | // If this is a touchpad navigation gesture, it needs to only be sent to trusted targets, as we |
| 2619 | // only want the system UI to handle these gestures. |
| 2620 | const bool isTouchpadNavGesture = isFromSource(entry.source, AINPUT_SOURCE_MOUSE) && |
| 2621 | entry.classification == MotionClassification::MULTI_FINGER_SWIPE; |
| 2622 | if (isTouchpadNavGesture) { |
| 2623 | filterUntrustedTargets(/* byref */ tempTouchState, /* byref */ targets); |
| 2624 | } |
| 2625 | |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2626 | // Output targets from the touch state. |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2627 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 2628 | if (touchedWindow.pointerIds.none() && !touchedWindow.hasHoveringPointers(entry.deviceId)) { |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2629 | // Windows with hovering pointers are getting persisted inside TouchState. |
| 2630 | // Do not send this event to those windows. |
| 2631 | continue; |
| 2632 | } |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 2633 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2634 | addWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags, |
| 2635 | touchedWindow.pointerIds, touchedWindow.firstDownTimeInTarget, |
| 2636 | targets); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2637 | } |
Sam Dubey | 39d37cf | 2022-12-07 18:05:35 +0000 | [diff] [blame] | 2638 | |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2639 | if (targets.empty()) { |
| 2640 | LOG(INFO) << "Dropping event because no targets were found: " << entry.getDescription(); |
| 2641 | outInjectionResult = InputEventInjectionResult::FAILED; |
| 2642 | return {}; |
| 2643 | } |
| 2644 | |
| 2645 | // If we only have windows getting ACTION_OUTSIDE, then drop the event, because there is no |
| 2646 | // window that is actually receiving the entire gesture. |
| 2647 | if (std::all_of(targets.begin(), targets.end(), [](const InputTarget& target) { |
| 2648 | return target.flags.test(InputTarget::Flags::DISPATCH_AS_OUTSIDE); |
| 2649 | })) { |
| 2650 | LOG(INFO) << "Dropping event because all windows would just receive ACTION_OUTSIDE: " |
| 2651 | << entry.getDescription(); |
| 2652 | outInjectionResult = InputEventInjectionResult::FAILED; |
| 2653 | return {}; |
| 2654 | } |
| 2655 | |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2656 | outInjectionResult = InputEventInjectionResult::SUCCEEDED; |
Sam Dubey | f886dec | 2023-01-27 13:28:19 +0000 | [diff] [blame] | 2657 | // Drop the outside or hover touch windows since we will not care about them |
| 2658 | // in the next iteration. |
| 2659 | tempTouchState.filterNonAsIsTouchWindows(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2660 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2661 | // Update final pieces of touch state if the injector had permission. |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2662 | if (switchedDevice) { |
| 2663 | if (DEBUG_FOCUS) { |
| 2664 | ALOGD("Conflicting pointer actions: Switched to a different device."); |
| 2665 | } |
| 2666 | *outConflictingPointerActions = true; |
| 2667 | } |
| 2668 | |
| 2669 | if (isHoverAction) { |
| 2670 | // Started hovering, therefore no longer down. |
Siarhei Vishniakou | 3ad385b | 2022-11-04 10:09:53 -0700 | [diff] [blame] | 2671 | if (oldState && oldState->isDown()) { |
Siarhei Vishniakou | 6e1e987 | 2022-11-08 17:51:35 -0800 | [diff] [blame] | 2672 | ALOGD_IF(DEBUG_FOCUS, |
| 2673 | "Conflicting pointer actions: Hover received while pointer was down."); |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2674 | *outConflictingPointerActions = true; |
| 2675 | } |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2676 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2677 | maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) { |
| 2678 | tempTouchState.deviceId = entry.deviceId; |
| 2679 | tempTouchState.source = entry.source; |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2680 | } |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2681 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP) { |
| 2682 | // Pointer went up. |
| 2683 | tempTouchState.removeTouchedPointer(entry.pointerProperties[0].id); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2684 | } else if (maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2685 | // All pointers up or canceled. |
| 2686 | tempTouchState.reset(); |
| 2687 | } else if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 2688 | // First pointer went down. |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2689 | if (oldState && (oldState->isDown() || oldState->hasHoveringPointers())) { |
| 2690 | ALOGD("Conflicting pointer actions: Down received while already down or hovering."); |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2691 | *outConflictingPointerActions = true; |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2692 | } |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2693 | } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 2694 | // One pointer went up. |
| 2695 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
| 2696 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2697 | |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2698 | for (size_t i = 0; i < tempTouchState.windows.size();) { |
| 2699 | TouchedWindow& touchedWindow = tempTouchState.windows[i]; |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 2700 | touchedWindow.pointerIds.reset(pointerId); |
| 2701 | if (touchedWindow.pointerIds.none()) { |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2702 | tempTouchState.windows.erase(tempTouchState.windows.begin() + i); |
| 2703 | continue; |
| 2704 | } |
| 2705 | i += 1; |
| 2706 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2707 | } |
| 2708 | |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2709 | // Save changes unless the action was scroll in which case the temporary touch |
| 2710 | // state was only valid for this one action. |
| 2711 | if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) { |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 2712 | if (displayId >= 0) { |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2713 | tempTouchState.clearWindowsWithoutPointers(); |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2714 | mTouchStatesByDisplay[displayId] = tempTouchState; |
| 2715 | } else { |
| 2716 | mTouchStatesByDisplay.erase(displayId); |
| 2717 | } |
| 2718 | } |
| 2719 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 2720 | if (tempTouchState.windows.empty()) { |
| 2721 | mTouchStatesByDisplay.erase(displayId); |
| 2722 | } |
| 2723 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2724 | return targets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2725 | } |
| 2726 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2727 | void InputDispatcher::finishDragAndDrop(int32_t displayId, float x, float y) { |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2728 | // Prevent stylus interceptor windows from affecting drag and drop behavior for now, until we |
| 2729 | // have an explicit reason to support it. |
| 2730 | constexpr bool isStylus = false; |
| 2731 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2732 | auto [dropWindow, _] = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2733 | findTouchedWindowAtLocked(displayId, x, y, isStylus, /*ignoreDragWindow=*/true); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2734 | if (dropWindow) { |
| 2735 | vec2 local = dropWindow->getInfo()->transform.transform(x, y); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2736 | sendDropWindowCommandLocked(dropWindow->getToken(), local.x, local.y); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 2737 | } else { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2738 | ALOGW("No window found when drop."); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2739 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2740 | } |
| 2741 | mDragState.reset(); |
| 2742 | } |
| 2743 | |
| 2744 | void InputDispatcher::addDragEventLocked(const MotionEntry& entry) { |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 2745 | if (!mDragState || mDragState->dragWindow->getInfo()->displayId != entry.displayId) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2746 | return; |
| 2747 | } |
| 2748 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2749 | if (!mDragState->isStartDrag) { |
| 2750 | mDragState->isStartDrag = true; |
| 2751 | mDragState->isStylusButtonDownAtStart = |
| 2752 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2753 | } |
| 2754 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2755 | // Find the pointer index by id. |
| 2756 | int32_t pointerIndex = 0; |
| 2757 | for (; static_cast<uint32_t>(pointerIndex) < entry.pointerCount; pointerIndex++) { |
| 2758 | const PointerProperties& pointerProperties = entry.pointerProperties[pointerIndex]; |
| 2759 | if (pointerProperties.id == mDragState->pointerId) { |
| 2760 | break; |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2761 | } |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2762 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2763 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2764 | if (uint32_t(pointerIndex) == entry.pointerCount) { |
| 2765 | LOG_ALWAYS_FATAL("Should find a valid pointer index by id %d", mDragState->pointerId); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2766 | } |
| 2767 | |
| 2768 | const int32_t maskedAction = entry.action & AMOTION_EVENT_ACTION_MASK; |
| 2769 | const int32_t x = entry.pointerCoords[pointerIndex].getX(); |
| 2770 | const int32_t y = entry.pointerCoords[pointerIndex].getY(); |
| 2771 | |
| 2772 | switch (maskedAction) { |
| 2773 | case AMOTION_EVENT_ACTION_MOVE: { |
| 2774 | // Handle the special case : stylus button no longer pressed. |
| 2775 | bool isStylusButtonDown = |
| 2776 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2777 | if (mDragState->isStylusButtonDownAtStart && !isStylusButtonDown) { |
| 2778 | finishDragAndDrop(entry.displayId, x, y); |
| 2779 | return; |
| 2780 | } |
| 2781 | |
| 2782 | // Prevent stylus interceptor windows from affecting drag and drop behavior for now, |
| 2783 | // until we have an explicit reason to support it. |
| 2784 | constexpr bool isStylus = false; |
| 2785 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2786 | auto [hoverWindowHandle, _] = findTouchedWindowAtLocked(entry.displayId, x, y, isStylus, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2787 | /*ignoreDragWindow=*/true); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2788 | // enqueue drag exit if needed. |
| 2789 | if (hoverWindowHandle != mDragState->dragHoverWindowHandle && |
| 2790 | !haveSameToken(hoverWindowHandle, mDragState->dragHoverWindowHandle)) { |
| 2791 | if (mDragState->dragHoverWindowHandle != nullptr) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2792 | enqueueDragEventLocked(mDragState->dragHoverWindowHandle, /*isExiting=*/true, x, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2793 | y); |
| 2794 | } |
| 2795 | mDragState->dragHoverWindowHandle = hoverWindowHandle; |
| 2796 | } |
| 2797 | // enqueue drag location if needed. |
| 2798 | if (hoverWindowHandle != nullptr) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2799 | enqueueDragEventLocked(hoverWindowHandle, /*isExiting=*/false, x, y); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2800 | } |
| 2801 | break; |
| 2802 | } |
| 2803 | |
| 2804 | case AMOTION_EVENT_ACTION_POINTER_UP: |
| 2805 | if (getMotionEventActionPointerIndex(entry.action) != pointerIndex) { |
| 2806 | break; |
| 2807 | } |
| 2808 | // The drag pointer is up. |
| 2809 | [[fallthrough]]; |
| 2810 | case AMOTION_EVENT_ACTION_UP: |
| 2811 | finishDragAndDrop(entry.displayId, x, y); |
| 2812 | break; |
| 2813 | case AMOTION_EVENT_ACTION_CANCEL: { |
| 2814 | ALOGD("Receiving cancel when drag and drop."); |
| 2815 | sendDropWindowCommandLocked(nullptr, 0, 0); |
| 2816 | mDragState.reset(); |
| 2817 | break; |
| 2818 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2819 | } |
| 2820 | } |
| 2821 | |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2822 | std::optional<InputTarget> InputDispatcher::createInputTargetLocked( |
| 2823 | const sp<android::gui::WindowInfoHandle>& windowHandle, |
| 2824 | ftl::Flags<InputTarget::Flags> targetFlags, |
| 2825 | std::optional<nsecs_t> firstDownTimeInTarget) const { |
| 2826 | std::shared_ptr<InputChannel> inputChannel = getInputChannelLocked(windowHandle->getToken()); |
| 2827 | if (inputChannel == nullptr) { |
| 2828 | ALOGW("Not creating InputTarget for %s, no input channel", windowHandle->getName().c_str()); |
| 2829 | return {}; |
| 2830 | } |
| 2831 | InputTarget inputTarget; |
| 2832 | inputTarget.inputChannel = inputChannel; |
| 2833 | inputTarget.flags = targetFlags; |
| 2834 | inputTarget.globalScaleFactor = windowHandle->getInfo()->globalScaleFactor; |
| 2835 | inputTarget.firstDownTimeInTarget = firstDownTimeInTarget; |
| 2836 | const auto& displayInfoIt = mDisplayInfos.find(windowHandle->getInfo()->displayId); |
| 2837 | if (displayInfoIt != mDisplayInfos.end()) { |
| 2838 | inputTarget.displayTransform = displayInfoIt->second.transform; |
| 2839 | } else { |
| 2840 | // DisplayInfo not found for this window on display windowInfo->displayId. |
| 2841 | // TODO(b/198444055): Make this an error message after 'setInputWindows' API is removed. |
| 2842 | } |
| 2843 | return inputTarget; |
| 2844 | } |
| 2845 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2846 | void InputDispatcher::addWindowTargetLocked(const sp<WindowInfoHandle>& windowHandle, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2847 | ftl::Flags<InputTarget::Flags> targetFlags, |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 2848 | std::bitset<MAX_POINTER_ID + 1> pointerIds, |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2849 | std::optional<nsecs_t> firstDownTimeInTarget, |
Siarhei Vishniakou | f75cddb | 2022-10-25 10:42:16 -0700 | [diff] [blame] | 2850 | std::vector<InputTarget>& inputTargets) const { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2851 | std::vector<InputTarget>::iterator it = |
| 2852 | std::find_if(inputTargets.begin(), inputTargets.end(), |
| 2853 | [&windowHandle](const InputTarget& inputTarget) { |
| 2854 | return inputTarget.inputChannel->getConnectionToken() == |
| 2855 | windowHandle->getToken(); |
| 2856 | }); |
Chavi Weingarten | 97b8eec | 2020-01-09 18:09:08 +0000 | [diff] [blame] | 2857 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2858 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2859 | |
| 2860 | if (it == inputTargets.end()) { |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2861 | std::optional<InputTarget> target = |
| 2862 | createInputTargetLocked(windowHandle, targetFlags, firstDownTimeInTarget); |
| 2863 | if (!target) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2864 | return; |
| 2865 | } |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2866 | inputTargets.push_back(*target); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2867 | it = inputTargets.end() - 1; |
| 2868 | } |
| 2869 | |
| 2870 | ALOG_ASSERT(it->flags == targetFlags); |
| 2871 | ALOG_ASSERT(it->globalScaleFactor == windowInfo->globalScaleFactor); |
| 2872 | |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2873 | it->addPointers(pointerIds, windowInfo->transform); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2874 | } |
| 2875 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2876 | void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2877 | int32_t displayId) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2878 | auto monitorsIt = mGlobalMonitorsByDisplay.find(displayId); |
| 2879 | if (monitorsIt == mGlobalMonitorsByDisplay.end()) return; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2880 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2881 | for (const Monitor& monitor : selectResponsiveMonitorsLocked(monitorsIt->second)) { |
| 2882 | InputTarget target; |
| 2883 | target.inputChannel = monitor.inputChannel; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2884 | target.flags = InputTarget::Flags::DISPATCH_AS_IS; |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2885 | // target.firstDownTimeInTarget is not set for global monitors. It is only required in split |
| 2886 | // touch and global monitoring works as intended even without setting firstDownTimeInTarget |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2887 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
| 2888 | target.displayTransform = it->second.transform; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2889 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2890 | target.setDefaultPointerTransform(target.displayTransform); |
| 2891 | inputTargets.push_back(target); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2892 | } |
| 2893 | } |
| 2894 | |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2895 | /** |
| 2896 | * Indicate whether one window handle should be considered as obscuring |
| 2897 | * another window handle. We only check a few preconditions. Actually |
| 2898 | * checking the bounds is left to the caller. |
| 2899 | */ |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2900 | static bool canBeObscuredBy(const sp<WindowInfoHandle>& windowHandle, |
| 2901 | const sp<WindowInfoHandle>& otherHandle) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2902 | // Compare by token so cloned layers aren't counted |
| 2903 | if (haveSameToken(windowHandle, otherHandle)) { |
| 2904 | return false; |
| 2905 | } |
| 2906 | auto info = windowHandle->getInfo(); |
| 2907 | auto otherInfo = otherHandle->getInfo(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2908 | if (otherInfo->inputConfig.test(WindowInfo::InputConfig::NOT_VISIBLE)) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2909 | return false; |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2910 | } else if (otherInfo->alpha == 0 && |
| 2911 | otherInfo->inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE)) { |
Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame] | 2912 | // Those act as if they were invisible, so we don't need to flag them. |
| 2913 | // We do want to potentially flag touchable windows even if they have 0 |
| 2914 | // opacity, since they can consume touches and alter the effects of the |
| 2915 | // user interaction (eg. apps that rely on |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2916 | // Flags::WINDOW_IS_PARTIALLY_OBSCURED should still be told about those |
Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame] | 2917 | // windows), hence we also check for FLAG_NOT_TOUCHABLE. |
| 2918 | return false; |
Bernardo Rufino | 8007daf | 2020-09-22 09:40:01 +0000 | [diff] [blame] | 2919 | } else if (info->ownerUid == otherInfo->ownerUid) { |
| 2920 | // If ownerUid is the same we don't generate occlusion events as there |
| 2921 | // is no security boundary within an uid. |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2922 | return false; |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2923 | } else if (otherInfo->inputConfig.test(gui::WindowInfo::InputConfig::TRUSTED_OVERLAY)) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2924 | return false; |
| 2925 | } else if (otherInfo->displayId != info->displayId) { |
| 2926 | return false; |
| 2927 | } |
| 2928 | return true; |
| 2929 | } |
| 2930 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2931 | /** |
| 2932 | * Returns touch occlusion information in the form of TouchOcclusionInfo. To check if the touch is |
| 2933 | * untrusted, one should check: |
| 2934 | * |
| 2935 | * 1. If result.hasBlockingOcclusion is true. |
| 2936 | * If it's, it means the touch should be blocked due to a window with occlusion mode of |
| 2937 | * BLOCK_UNTRUSTED. |
| 2938 | * |
| 2939 | * 2. If result.obscuringOpacity > mMaximumObscuringOpacityForTouch. |
| 2940 | * If it is (and 1 is false), then the touch should be blocked because a stack of windows |
| 2941 | * (possibly only one) with occlusion mode of USE_OPACITY from one UID resulted in a composed |
| 2942 | * obscuring opacity above the threshold. Note that if there was no window of occlusion mode |
| 2943 | * USE_OPACITY, result.obscuringOpacity would've been 0 and since |
| 2944 | * mMaximumObscuringOpacityForTouch >= 0, the condition above would never be true. |
| 2945 | * |
| 2946 | * If neither of those is true, then it means the touch can be allowed. |
| 2947 | */ |
| 2948 | InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2949 | const sp<WindowInfoHandle>& windowHandle, int32_t x, int32_t y) const { |
| 2950 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2951 | int32_t displayId = windowInfo->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2952 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2953 | TouchOcclusionInfo info; |
| 2954 | info.hasBlockingOcclusion = false; |
| 2955 | info.obscuringOpacity = 0; |
| 2956 | info.obscuringUid = -1; |
| 2957 | std::map<int32_t, float> opacityByUid; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2958 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2959 | if (windowHandle == otherHandle) { |
| 2960 | break; // All future windows are below us. Exit early. |
| 2961 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2962 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 2963 | if (canBeObscuredBy(windowHandle, otherHandle) && otherInfo->frameContainsPoint(x, y) && |
| 2964 | !haveSameApplicationToken(windowInfo, otherInfo)) { |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2965 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2966 | info.debugInfo.push_back( |
| 2967 | dumpWindowForTouchOcclusion(otherInfo, /* isTouchedWindow */ false)); |
| 2968 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2969 | // canBeObscuredBy() has returned true above, which means this window is untrusted, so |
| 2970 | // we perform the checks below to see if the touch can be propagated or not based on the |
| 2971 | // window's touch occlusion mode |
| 2972 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::BLOCK_UNTRUSTED) { |
| 2973 | info.hasBlockingOcclusion = true; |
| 2974 | info.obscuringUid = otherInfo->ownerUid; |
| 2975 | info.obscuringPackage = otherInfo->packageName; |
| 2976 | break; |
| 2977 | } |
| 2978 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::USE_OPACITY) { |
| 2979 | uint32_t uid = otherInfo->ownerUid; |
| 2980 | float opacity = |
| 2981 | (opacityByUid.find(uid) == opacityByUid.end()) ? 0 : opacityByUid[uid]; |
| 2982 | // Given windows A and B: |
| 2983 | // opacity(A, B) = 1 - [1 - opacity(A)] * [1 - opacity(B)] |
| 2984 | opacity = 1 - (1 - opacity) * (1 - otherInfo->alpha); |
| 2985 | opacityByUid[uid] = opacity; |
| 2986 | if (opacity > info.obscuringOpacity) { |
| 2987 | info.obscuringOpacity = opacity; |
| 2988 | info.obscuringUid = uid; |
| 2989 | info.obscuringPackage = otherInfo->packageName; |
| 2990 | } |
| 2991 | } |
| 2992 | } |
| 2993 | } |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2994 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2995 | info.debugInfo.push_back( |
| 2996 | dumpWindowForTouchOcclusion(windowInfo, /* isTouchedWindow */ true)); |
| 2997 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2998 | return info; |
| 2999 | } |
| 3000 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3001 | std::string InputDispatcher::dumpWindowForTouchOcclusion(const WindowInfo* info, |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 3002 | bool isTouchedWindow) const { |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 3003 | return StringPrintf(INDENT2 "* %spackage=%s/%" PRId32 ", id=%" PRId32 ", mode=%s, alpha=%.2f, " |
| 3004 | "frame=[%" PRId32 ",%" PRId32 "][%" PRId32 ",%" PRId32 |
| 3005 | "], touchableRegion=%s, window={%s}, inputConfig={%s}, " |
| 3006 | "hasToken=%s, applicationInfo.name=%s, applicationInfo.token=%s\n", |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 3007 | isTouchedWindow ? "[TOUCHED] " : "", info->packageName.c_str(), |
| 3008 | info->ownerUid, info->id, toString(info->touchOcclusionMode).c_str(), |
| 3009 | info->alpha, info->frameLeft, info->frameTop, info->frameRight, |
| 3010 | info->frameBottom, dumpRegion(info->touchableRegion).c_str(), |
| 3011 | info->name.c_str(), info->inputConfig.string().c_str(), |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 3012 | toString(info->token != nullptr), info->applicationInfo.name.c_str(), |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 3013 | binderToString(info->applicationInfo.token).c_str()); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 3014 | } |
| 3015 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3016 | bool InputDispatcher::isTouchTrustedLocked(const TouchOcclusionInfo& occlusionInfo) const { |
| 3017 | if (occlusionInfo.hasBlockingOcclusion) { |
| 3018 | ALOGW("Untrusted touch due to occlusion by %s/%d", occlusionInfo.obscuringPackage.c_str(), |
| 3019 | occlusionInfo.obscuringUid); |
| 3020 | return false; |
| 3021 | } |
| 3022 | if (occlusionInfo.obscuringOpacity > mMaximumObscuringOpacityForTouch) { |
| 3023 | ALOGW("Untrusted touch due to occlusion by %s/%d (obscuring opacity = " |
| 3024 | "%.2f, maximum allowed = %.2f)", |
| 3025 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid, |
| 3026 | occlusionInfo.obscuringOpacity, mMaximumObscuringOpacityForTouch); |
| 3027 | return false; |
| 3028 | } |
| 3029 | return true; |
| 3030 | } |
| 3031 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3032 | bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3033 | int32_t x, int32_t y) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3034 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3035 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 3036 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3037 | if (windowHandle == otherHandle) { |
| 3038 | break; // All future windows are below us. Exit early. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3039 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3040 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3041 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 3042 | otherInfo->frameContainsPoint(x, y)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3043 | return true; |
| 3044 | } |
| 3045 | } |
| 3046 | return false; |
| 3047 | } |
| 3048 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3049 | bool InputDispatcher::isWindowObscuredLocked(const sp<WindowInfoHandle>& windowHandle) const { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 3050 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3051 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 3052 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 3053 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3054 | if (windowHandle == otherHandle) { |
| 3055 | break; // All future windows are below us. Exit early. |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 3056 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3057 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3058 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 3059 | otherInfo->overlaps(windowInfo)) { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 3060 | return true; |
| 3061 | } |
| 3062 | } |
| 3063 | return false; |
| 3064 | } |
| 3065 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 3066 | std::string InputDispatcher::getApplicationWindowLabel( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3067 | const InputApplicationHandle* applicationHandle, const sp<WindowInfoHandle>& windowHandle) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3068 | if (applicationHandle != nullptr) { |
| 3069 | if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 3070 | return applicationHandle->getName() + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3071 | } else { |
| 3072 | return applicationHandle->getName(); |
| 3073 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3074 | } else if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 3075 | return windowHandle->getInfo()->applicationInfo.name + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3076 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3077 | return "<unknown application or window>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3078 | } |
| 3079 | } |
| 3080 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3081 | void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3082 | if (!isUserActivityEvent(eventEntry)) { |
| 3083 | // Not poking user activity if the event type does not represent a user activity |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3084 | return; |
| 3085 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3086 | int32_t displayId = getTargetDisplayId(eventEntry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3087 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 3088 | const WindowInfo* windowDisablingUserActivityInfo = nullptr; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3089 | if (focusedWindowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3090 | const WindowInfo* info = focusedWindowHandle->getInfo(); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 3091 | if (info->inputConfig.test(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY)) { |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 3092 | windowDisablingUserActivityInfo = info; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3093 | } |
| 3094 | } |
| 3095 | |
| 3096 | int32_t eventType = USER_ACTIVITY_EVENT_OTHER; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3097 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3098 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3099 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 3100 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3101 | return; |
| 3102 | } |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 3103 | if (windowDisablingUserActivityInfo != nullptr) { |
| 3104 | if (DEBUG_DISPATCH_CYCLE) { |
| 3105 | ALOGD("Not poking user activity: disabled by window '%s'.", |
| 3106 | windowDisablingUserActivityInfo->name.c_str()); |
| 3107 | } |
| 3108 | return; |
| 3109 | } |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3110 | if (MotionEvent::isTouchEvent(motionEntry.source, motionEntry.action)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3111 | eventType = USER_ACTIVITY_EVENT_TOUCH; |
| 3112 | } |
| 3113 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3114 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3115 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3116 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 3117 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3118 | return; |
| 3119 | } |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 3120 | // If the key code is unknown, we don't consider it user activity |
| 3121 | if (keyEntry.keyCode == AKEYCODE_UNKNOWN) { |
| 3122 | return; |
| 3123 | } |
| 3124 | // Don't inhibit events that were intercepted or are not passed to |
| 3125 | // the apps, like system shortcuts |
| 3126 | if (windowDisablingUserActivityInfo != nullptr && |
| 3127 | keyEntry.interceptKeyResult != KeyEntry::InterceptKeyResult::SKIP && |
| 3128 | keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER) { |
| 3129 | if (DEBUG_DISPATCH_CYCLE) { |
| 3130 | ALOGD("Not poking user activity: disabled by window '%s'.", |
| 3131 | windowDisablingUserActivityInfo->name.c_str()); |
| 3132 | } |
| 3133 | return; |
| 3134 | } |
| 3135 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3136 | eventType = USER_ACTIVITY_EVENT_BUTTON; |
| 3137 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3138 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3139 | default: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3140 | LOG_ALWAYS_FATAL("%s events are not user activity", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3141 | ftl::enum_string(eventEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3142 | break; |
| 3143 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3144 | } |
| 3145 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3146 | auto command = [this, eventTime = eventEntry.eventTime, eventType, displayId]() |
| 3147 | REQUIRES(mLock) { |
| 3148 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 3149 | mPolicy.pokeUserActivity(eventTime, eventType, displayId); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3150 | }; |
| 3151 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3152 | } |
| 3153 | |
| 3154 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3155 | const std::shared_ptr<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3156 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3157 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3158 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3159 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3160 | StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3161 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3162 | ATRACE_NAME(message.c_str()); |
| 3163 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3164 | if (DEBUG_DISPATCH_CYCLE) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3165 | ALOGD("channel '%s' ~ prepareDispatchCycle - flags=%s, " |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 3166 | "globalScaleFactor=%f, pointerIds=%s %s", |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3167 | connection->getInputChannelName().c_str(), inputTarget.flags.string().c_str(), |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 3168 | inputTarget.globalScaleFactor, bitsetToString(inputTarget.pointerIds).c_str(), |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3169 | inputTarget.getPointerInfoString().c_str()); |
| 3170 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3171 | |
| 3172 | // Skip this event if the connection status is not normal. |
| 3173 | // We don't want to enqueue additional outbound events if the connection is broken. |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3174 | if (connection->status != Connection::Status::NORMAL) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3175 | if (DEBUG_DISPATCH_CYCLE) { |
| 3176 | ALOGD("channel '%s' ~ Dropping event because the channel status is %s", |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3177 | connection->getInputChannelName().c_str(), |
| 3178 | ftl::enum_string(connection->status).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3179 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3180 | return; |
| 3181 | } |
| 3182 | |
| 3183 | // Split a motion event if needed. |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3184 | if (inputTarget.flags.test(InputTarget::Flags::SPLIT)) { |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3185 | LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3186 | "Entry type %s should not have Flags::SPLIT", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3187 | ftl::enum_string(eventEntry->type).c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3188 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3189 | const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry); |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3190 | if (inputTarget.pointerIds.count() != originalMotionEntry.pointerCount) { |
Siarhei Vishniakou | 16e4fa0 | 2023-02-16 17:48:56 -0800 | [diff] [blame] | 3191 | if (!inputTarget.firstDownTimeInTarget.has_value()) { |
| 3192 | logDispatchStateLocked(); |
| 3193 | LOG(FATAL) << "Splitting motion events requires a down time to be set for the " |
| 3194 | "target on connection " |
| 3195 | << connection->getInputChannelName() << " for " |
| 3196 | << originalMotionEntry.getDescription(); |
| 3197 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3198 | std::unique_ptr<MotionEntry> splitMotionEntry = |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3199 | splitMotionEvent(originalMotionEntry, inputTarget.pointerIds, |
| 3200 | inputTarget.firstDownTimeInTarget.value()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3201 | if (!splitMotionEntry) { |
| 3202 | return; // split event was dropped |
| 3203 | } |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3204 | if (splitMotionEntry->action == AMOTION_EVENT_ACTION_CANCEL) { |
| 3205 | std::string reason = std::string("reason=pointer cancel on split window"); |
| 3206 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 3207 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 3208 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3209 | if (DEBUG_FOCUS) { |
| 3210 | ALOGD("channel '%s' ~ Split motion event.", |
| 3211 | connection->getInputChannelName().c_str()); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3212 | logOutboundMotionDetails(" ", *splitMotionEntry); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3213 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3214 | enqueueDispatchEntriesLocked(currentTime, connection, std::move(splitMotionEntry), |
| 3215 | inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3216 | return; |
| 3217 | } |
| 3218 | } |
| 3219 | |
| 3220 | // Not splitting. Enqueue dispatch entries for the event as is. |
| 3221 | enqueueDispatchEntriesLocked(currentTime, connection, eventEntry, inputTarget); |
| 3222 | } |
| 3223 | |
| 3224 | void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3225 | const std::shared_ptr<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3226 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3227 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3228 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3229 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3230 | StringPrintf("enqueueDispatchEntriesLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3231 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3232 | ATRACE_NAME(message.c_str()); |
| 3233 | } |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3234 | LOG_ALWAYS_FATAL_IF(!inputTarget.flags.any(InputTarget::DISPATCH_MASK), |
| 3235 | "No dispatch flags are set for %s", eventEntry->getDescription().c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3236 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 3237 | const bool wasEmpty = connection->outboundQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3238 | |
| 3239 | // Enqueue dispatch entries for the requested modes. |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3240 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3241 | InputTarget::Flags::DISPATCH_AS_HOVER_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3242 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3243 | InputTarget::Flags::DISPATCH_AS_OUTSIDE); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3244 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3245 | InputTarget::Flags::DISPATCH_AS_HOVER_ENTER); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3246 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3247 | InputTarget::Flags::DISPATCH_AS_IS); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3248 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3249 | InputTarget::Flags::DISPATCH_AS_SLIPPERY_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3250 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3251 | InputTarget::Flags::DISPATCH_AS_SLIPPERY_ENTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3252 | |
| 3253 | // If the outbound queue was previously empty, start the dispatch cycle going. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3254 | if (wasEmpty && !connection->outboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3255 | startDispatchCycleLocked(currentTime, connection); |
| 3256 | } |
| 3257 | } |
| 3258 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3259 | void InputDispatcher::enqueueDispatchEntryLocked(const std::shared_ptr<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3260 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3261 | const InputTarget& inputTarget, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3262 | ftl::Flags<InputTarget::Flags> dispatchMode) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3263 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3264 | std::string message = StringPrintf("enqueueDispatchEntry(inputChannel=%s, dispatchMode=%s)", |
| 3265 | connection->getInputChannelName().c_str(), |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3266 | dispatchMode.string().c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3267 | ATRACE_NAME(message.c_str()); |
| 3268 | } |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3269 | ftl::Flags<InputTarget::Flags> inputTargetFlags = inputTarget.flags; |
| 3270 | if (!inputTargetFlags.any(dispatchMode)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3271 | return; |
| 3272 | } |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3273 | |
| 3274 | inputTargetFlags.clear(InputTarget::DISPATCH_MASK); |
| 3275 | inputTargetFlags |= dispatchMode; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3276 | |
| 3277 | // This is a new event. |
| 3278 | // Enqueue a new dispatch entry onto the outbound queue for this connection. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3279 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3280 | createDispatchEntry(inputTarget, eventEntry, inputTargetFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3281 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3282 | // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a |
| 3283 | // different EventEntry than what was passed in. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3284 | EventEntry& newEntry = *(dispatchEntry->eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3285 | // Apply target flags and update the connection's input state. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3286 | switch (newEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3287 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3288 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3289 | dispatchEntry->resolvedEventId = keyEntry.id; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3290 | dispatchEntry->resolvedAction = keyEntry.action; |
| 3291 | dispatchEntry->resolvedFlags = keyEntry.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3292 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3293 | if (!connection->inputState.trackKey(keyEntry, dispatchEntry->resolvedAction, |
| 3294 | dispatchEntry->resolvedFlags)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3295 | if (DEBUG_DISPATCH_CYCLE) { |
| 3296 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent key " |
| 3297 | "event", |
| 3298 | connection->getInputChannelName().c_str()); |
| 3299 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3300 | return; // skip the inconsistent event |
| 3301 | } |
| 3302 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3303 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3304 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3305 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3306 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3307 | // Assign a default value to dispatchEntry that will never be generated by InputReader, |
| 3308 | // and assign a InputDispatcher value if it doesn't change in the if-else chain below. |
| 3309 | constexpr int32_t DEFAULT_RESOLVED_EVENT_ID = |
| 3310 | static_cast<int32_t>(IdGenerator::Source::OTHER); |
| 3311 | dispatchEntry->resolvedEventId = DEFAULT_RESOLVED_EVENT_ID; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3312 | if (dispatchMode.test(InputTarget::Flags::DISPATCH_AS_OUTSIDE)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3313 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3314 | } else if (dispatchMode.test(InputTarget::Flags::DISPATCH_AS_HOVER_EXIT)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3315 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3316 | } else if (dispatchMode.test(InputTarget::Flags::DISPATCH_AS_HOVER_ENTER)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3317 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3318 | } else if (dispatchMode.test(InputTarget::Flags::DISPATCH_AS_SLIPPERY_EXIT)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3319 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_CANCEL; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3320 | } else if (dispatchMode.test(InputTarget::Flags::DISPATCH_AS_SLIPPERY_ENTER)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3321 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_DOWN; |
| 3322 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3323 | dispatchEntry->resolvedAction = motionEntry.action; |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3324 | dispatchEntry->resolvedEventId = motionEntry.id; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3325 | } |
| 3326 | if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE && |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3327 | !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source, |
| 3328 | motionEntry.displayId)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3329 | if (DEBUG_DISPATCH_CYCLE) { |
| 3330 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: filling in missing hover " |
| 3331 | "enter event", |
| 3332 | connection->getInputChannelName().c_str()); |
| 3333 | } |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3334 | // We keep the 'resolvedEventId' here equal to the original 'motionEntry.id' because |
| 3335 | // this is a one-to-one event conversion. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3336 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 3337 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3338 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3339 | dispatchEntry->resolvedFlags = motionEntry.flags; |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 3340 | if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_CANCEL) { |
| 3341 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_CANCELED; |
| 3342 | } |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3343 | if (dispatchEntry->targetFlags.test(InputTarget::Flags::WINDOW_IS_OBSCURED)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3344 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; |
| 3345 | } |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3346 | if (dispatchEntry->targetFlags.test(InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3347 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 3348 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3349 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3350 | if (!connection->inputState.trackMotion(motionEntry, dispatchEntry->resolvedAction, |
| 3351 | dispatchEntry->resolvedFlags)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3352 | if (DEBUG_DISPATCH_CYCLE) { |
| 3353 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent motion " |
| 3354 | "event", |
| 3355 | connection->getInputChannelName().c_str()); |
| 3356 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3357 | return; // skip the inconsistent event |
| 3358 | } |
| 3359 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3360 | dispatchEntry->resolvedEventId = |
| 3361 | dispatchEntry->resolvedEventId == DEFAULT_RESOLVED_EVENT_ID |
| 3362 | ? mIdGenerator.nextId() |
| 3363 | : motionEntry.id; |
| 3364 | if (ATRACE_ENABLED() && dispatchEntry->resolvedEventId != motionEntry.id) { |
| 3365 | std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32 |
| 3366 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3367 | motionEntry.id, dispatchEntry->resolvedEventId); |
| 3368 | ATRACE_NAME(message.c_str()); |
| 3369 | } |
| 3370 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 3371 | if ((motionEntry.flags & AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) && |
| 3372 | (motionEntry.policyFlags & POLICY_FLAG_TRUSTED)) { |
| 3373 | // Skip reporting pointer down outside focus to the policy. |
| 3374 | break; |
| 3375 | } |
| 3376 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3377 | dispatchPointerDownOutsideFocus(motionEntry.source, dispatchEntry->resolvedAction, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3378 | inputTarget.inputChannel->getConnectionToken()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3379 | |
| 3380 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3381 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3382 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3383 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3384 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3385 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3386 | break; |
| 3387 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3388 | case EventEntry::Type::SENSOR: { |
| 3389 | LOG_ALWAYS_FATAL("SENSOR events should not go to apps via input channel"); |
| 3390 | break; |
| 3391 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3392 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 3393 | case EventEntry::Type::DEVICE_RESET: { |
| 3394 | LOG_ALWAYS_FATAL("%s events should not go to apps", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3395 | ftl::enum_string(newEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3396 | break; |
| 3397 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3398 | } |
| 3399 | |
| 3400 | // Remember that we are waiting for this dispatch to complete. |
| 3401 | if (dispatchEntry->hasForegroundTarget()) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3402 | incrementPendingForegroundDispatches(newEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3403 | } |
| 3404 | |
| 3405 | // Enqueue the dispatch entry. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3406 | connection->outboundQueue.push_back(dispatchEntry.release()); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3407 | traceOutboundQueueLength(*connection); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3408 | } |
| 3409 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3410 | /** |
| 3411 | * This function is purely for debugging. It helps us understand where the user interaction |
| 3412 | * was taking place. For example, if user is touching launcher, we will see a log that user |
| 3413 | * started interacting with launcher. In that example, the event would go to the wallpaper as well. |
| 3414 | * We will see both launcher and wallpaper in that list. |
| 3415 | * Once the interaction with a particular set of connections starts, no new logs will be printed |
| 3416 | * until the set of interacted connections changes. |
| 3417 | * |
| 3418 | * The following items are skipped, to reduce the logspam: |
| 3419 | * ACTION_OUTSIDE: any windows that are receiving ACTION_OUTSIDE are not logged |
| 3420 | * ACTION_UP: any windows that receive ACTION_UP are not logged (for both keys and motions). |
| 3421 | * This includes situations like the soft BACK button key. When the user releases (lifts up the |
| 3422 | * finger) the back button, then navigation bar will inject KEYCODE_BACK with ACTION_UP. |
| 3423 | * Both of those ACTION_UP events would not be logged |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3424 | */ |
| 3425 | void InputDispatcher::updateInteractionTokensLocked(const EventEntry& entry, |
| 3426 | const std::vector<InputTarget>& targets) { |
| 3427 | // Skip ACTION_UP events, and all events other than keys and motions |
| 3428 | if (entry.type == EventEntry::Type::KEY) { |
| 3429 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 3430 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
| 3431 | return; |
| 3432 | } |
| 3433 | } else if (entry.type == EventEntry::Type::MOTION) { |
| 3434 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 3435 | if (motionEntry.action == AMOTION_EVENT_ACTION_UP || |
| 3436 | motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
| 3437 | return; |
| 3438 | } |
| 3439 | } else { |
| 3440 | return; // Not a key or a motion |
| 3441 | } |
| 3442 | |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 3443 | std::unordered_set<sp<IBinder>, StrongPointerHash<IBinder>> newConnectionTokens; |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3444 | std::vector<std::shared_ptr<Connection>> newConnections; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3445 | for (const InputTarget& target : targets) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3446 | if (target.flags.test(InputTarget::Flags::DISPATCH_AS_OUTSIDE)) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3447 | continue; // Skip windows that receive ACTION_OUTSIDE |
| 3448 | } |
| 3449 | |
| 3450 | sp<IBinder> token = target.inputChannel->getConnectionToken(); |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3451 | std::shared_ptr<Connection> connection = getConnectionLocked(token); |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3452 | if (connection == nullptr) { |
| 3453 | continue; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3454 | } |
| 3455 | newConnectionTokens.insert(std::move(token)); |
| 3456 | newConnections.emplace_back(connection); |
| 3457 | } |
| 3458 | if (newConnectionTokens == mInteractionConnectionTokens) { |
| 3459 | return; // no change |
| 3460 | } |
| 3461 | mInteractionConnectionTokens = newConnectionTokens; |
| 3462 | |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3463 | std::string targetList; |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3464 | for (const std::shared_ptr<Connection>& connection : newConnections) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3465 | targetList += connection->getWindowName() + ", "; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3466 | } |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3467 | std::string message = "Interaction with: " + targetList; |
| 3468 | if (targetList.empty()) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3469 | message += "<none>"; |
| 3470 | } |
| 3471 | android_log_event_list(LOGTAG_INPUT_INTERACTION) << message << LOG_ID_EVENTS; |
| 3472 | } |
| 3473 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3474 | void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3475 | const sp<IBinder>& token) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3476 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3477 | uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK; |
| 3478 | if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3479 | return; |
| 3480 | } |
| 3481 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 3482 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3483 | if (focusedToken == token) { |
| 3484 | // ignore since token is focused |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3485 | return; |
| 3486 | } |
| 3487 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3488 | auto command = [this, token]() REQUIRES(mLock) { |
| 3489 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 3490 | mPolicy.onPointerDownOutsideFocus(token); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3491 | }; |
| 3492 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3493 | } |
| 3494 | |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3495 | status_t InputDispatcher::publishMotionEvent(Connection& connection, |
| 3496 | DispatchEntry& dispatchEntry) const { |
| 3497 | const EventEntry& eventEntry = *(dispatchEntry.eventEntry); |
| 3498 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 3499 | |
| 3500 | PointerCoords scaledCoords[MAX_POINTERS]; |
| 3501 | const PointerCoords* usingCoords = motionEntry.pointerCoords; |
| 3502 | |
| 3503 | // Set the X and Y offset and X and Y scale depending on the input source. |
| 3504 | if ((motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) && |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3505 | !(dispatchEntry.targetFlags.test(InputTarget::Flags::ZERO_COORDS))) { |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3506 | float globalScaleFactor = dispatchEntry.globalScaleFactor; |
| 3507 | if (globalScaleFactor != 1.0f) { |
| 3508 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
| 3509 | scaledCoords[i] = motionEntry.pointerCoords[i]; |
| 3510 | // Don't apply window scale here since we don't want scale to affect raw |
| 3511 | // coordinates. The scale will be sent back to the client and applied |
| 3512 | // later when requesting relative coordinates. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 3513 | scaledCoords[i].scale(globalScaleFactor, /*windowXScale=*/1, /*windowYScale=*/1); |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3514 | } |
| 3515 | usingCoords = scaledCoords; |
| 3516 | } |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3517 | } else if (dispatchEntry.targetFlags.test(InputTarget::Flags::ZERO_COORDS)) { |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3518 | // We don't want the dispatch target to know the coordinates |
| 3519 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
| 3520 | scaledCoords[i].clear(); |
| 3521 | } |
| 3522 | usingCoords = scaledCoords; |
| 3523 | } |
| 3524 | |
| 3525 | std::array<uint8_t, 32> hmac = getSignature(motionEntry, dispatchEntry); |
| 3526 | |
| 3527 | // Publish the motion event. |
| 3528 | return connection.inputPublisher |
| 3529 | .publishMotionEvent(dispatchEntry.seq, dispatchEntry.resolvedEventId, |
| 3530 | motionEntry.deviceId, motionEntry.source, motionEntry.displayId, |
| 3531 | std::move(hmac), dispatchEntry.resolvedAction, |
| 3532 | motionEntry.actionButton, dispatchEntry.resolvedFlags, |
| 3533 | motionEntry.edgeFlags, motionEntry.metaState, |
| 3534 | motionEntry.buttonState, motionEntry.classification, |
| 3535 | dispatchEntry.transform, motionEntry.xPrecision, |
| 3536 | motionEntry.yPrecision, motionEntry.xCursorPosition, |
| 3537 | motionEntry.yCursorPosition, dispatchEntry.rawTransform, |
| 3538 | motionEntry.downTime, motionEntry.eventTime, |
| 3539 | motionEntry.pointerCount, motionEntry.pointerProperties, |
| 3540 | usingCoords); |
| 3541 | } |
| 3542 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3543 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3544 | const std::shared_ptr<Connection>& connection) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3545 | if (ATRACE_ENABLED()) { |
| 3546 | std::string message = StringPrintf("startDispatchCycleLocked(inputChannel=%s)", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3547 | connection->getInputChannelName().c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3548 | ATRACE_NAME(message.c_str()); |
| 3549 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3550 | if (DEBUG_DISPATCH_CYCLE) { |
| 3551 | ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str()); |
| 3552 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3553 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3554 | while (connection->status == Connection::Status::NORMAL && !connection->outboundQueue.empty()) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3555 | DispatchEntry* dispatchEntry = connection->outboundQueue.front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3556 | dispatchEntry->deliveryTime = currentTime; |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3557 | const std::chrono::nanoseconds timeout = getDispatchingTimeoutLocked(connection); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3558 | dispatchEntry->timeoutTime = currentTime + timeout.count(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3559 | |
| 3560 | // Publish the event. |
| 3561 | status_t status; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3562 | const EventEntry& eventEntry = *(dispatchEntry->eventEntry); |
| 3563 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3564 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3565 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 3566 | std::array<uint8_t, 32> hmac = getSignature(keyEntry, *dispatchEntry); |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 3567 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 3568 | LOG(DEBUG) << "Publishing " << *dispatchEntry << " to " |
| 3569 | << connection->getInputChannelName(); |
| 3570 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3571 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3572 | // Publish the key event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3573 | status = connection->inputPublisher |
| 3574 | .publishKeyEvent(dispatchEntry->seq, |
| 3575 | dispatchEntry->resolvedEventId, keyEntry.deviceId, |
| 3576 | keyEntry.source, keyEntry.displayId, |
| 3577 | std::move(hmac), dispatchEntry->resolvedAction, |
| 3578 | dispatchEntry->resolvedFlags, keyEntry.keyCode, |
| 3579 | keyEntry.scanCode, keyEntry.metaState, |
| 3580 | keyEntry.repeatCount, keyEntry.downTime, |
| 3581 | keyEntry.eventTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3582 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3583 | } |
| 3584 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3585 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 3586 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 3587 | LOG(DEBUG) << "Publishing " << *dispatchEntry << " to " |
| 3588 | << connection->getInputChannelName(); |
| 3589 | } |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3590 | status = publishMotionEvent(*connection, *dispatchEntry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3591 | break; |
| 3592 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3593 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3594 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3595 | const FocusEntry& focusEntry = static_cast<const FocusEntry&>(eventEntry); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3596 | status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3597 | focusEntry.id, |
Antonio Kantek | 3cfec7b | 2021-11-05 18:26:17 -0700 | [diff] [blame] | 3598 | focusEntry.hasFocus); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3599 | break; |
| 3600 | } |
| 3601 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3602 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 3603 | const TouchModeEntry& touchModeEntry = |
| 3604 | static_cast<const TouchModeEntry&>(eventEntry); |
| 3605 | status = connection->inputPublisher |
| 3606 | .publishTouchModeEvent(dispatchEntry->seq, touchModeEntry.id, |
| 3607 | touchModeEntry.inTouchMode); |
| 3608 | |
| 3609 | break; |
| 3610 | } |
| 3611 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3612 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 3613 | const auto& captureEntry = |
| 3614 | static_cast<const PointerCaptureChangedEntry&>(eventEntry); |
| 3615 | status = connection->inputPublisher |
| 3616 | .publishCaptureEvent(dispatchEntry->seq, captureEntry.id, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 3617 | captureEntry.pointerCaptureRequest.enable); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3618 | break; |
| 3619 | } |
| 3620 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3621 | case EventEntry::Type::DRAG: { |
| 3622 | const DragEntry& dragEntry = static_cast<const DragEntry&>(eventEntry); |
| 3623 | status = connection->inputPublisher.publishDragEvent(dispatchEntry->seq, |
| 3624 | dragEntry.id, dragEntry.x, |
| 3625 | dragEntry.y, |
| 3626 | dragEntry.isExiting); |
| 3627 | break; |
| 3628 | } |
| 3629 | |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3630 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3631 | case EventEntry::Type::DEVICE_RESET: |
| 3632 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3633 | LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3634 | ftl::enum_string(eventEntry.type).c_str()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3635 | return; |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3636 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3637 | } |
| 3638 | |
| 3639 | // Check the result. |
| 3640 | if (status) { |
| 3641 | if (status == WOULD_BLOCK) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3642 | if (connection->waitQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3643 | ALOGE("channel '%s' ~ Could not publish event because the pipe is full. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3644 | "This is unexpected because the wait queue is empty, so the pipe " |
| 3645 | "should be empty and we shouldn't have any problems writing an " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3646 | "event to it, status=%s(%d)", |
| 3647 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3648 | status); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 3649 | abortBrokenDispatchCycleLocked(currentTime, connection, /*notify=*/true); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3650 | } else { |
| 3651 | // Pipe is full and we are waiting for the app to finish process some events |
| 3652 | // before sending more events to it. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3653 | if (DEBUG_DISPATCH_CYCLE) { |
| 3654 | ALOGD("channel '%s' ~ Could not publish event because the pipe is full, " |
| 3655 | "waiting for the application to catch up", |
| 3656 | connection->getInputChannelName().c_str()); |
| 3657 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3658 | } |
| 3659 | } else { |
| 3660 | ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3661 | "status=%s(%d)", |
| 3662 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3663 | status); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 3664 | abortBrokenDispatchCycleLocked(currentTime, connection, /*notify=*/true); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3665 | } |
| 3666 | return; |
| 3667 | } |
| 3668 | |
| 3669 | // Re-enqueue the event on the wait queue. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3670 | connection->outboundQueue.erase(std::remove(connection->outboundQueue.begin(), |
| 3671 | connection->outboundQueue.end(), |
| 3672 | dispatchEntry)); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3673 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3674 | connection->waitQueue.push_back(dispatchEntry); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3675 | if (connection->responsive) { |
| 3676 | mAnrTracker.insert(dispatchEntry->timeoutTime, |
| 3677 | connection->inputChannel->getConnectionToken()); |
| 3678 | } |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3679 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3680 | } |
| 3681 | } |
| 3682 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3683 | std::array<uint8_t, 32> InputDispatcher::sign(const VerifiedInputEvent& event) const { |
| 3684 | size_t size; |
| 3685 | switch (event.type) { |
| 3686 | case VerifiedInputEvent::Type::KEY: { |
| 3687 | size = sizeof(VerifiedKeyEvent); |
| 3688 | break; |
| 3689 | } |
| 3690 | case VerifiedInputEvent::Type::MOTION: { |
| 3691 | size = sizeof(VerifiedMotionEvent); |
| 3692 | break; |
| 3693 | } |
| 3694 | } |
| 3695 | const uint8_t* start = reinterpret_cast<const uint8_t*>(&event); |
| 3696 | return mHmacKeyManager.sign(start, size); |
| 3697 | } |
| 3698 | |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3699 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3700 | const MotionEntry& motionEntry, const DispatchEntry& dispatchEntry) const { |
Siarhei Vishniakou | bf88052 | 2023-05-01 11:03:22 -0700 | [diff] [blame] | 3701 | const int32_t actionMasked = MotionEvent::getActionMasked(dispatchEntry.resolvedAction); |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3702 | if (actionMasked != AMOTION_EVENT_ACTION_UP && actionMasked != AMOTION_EVENT_ACTION_DOWN) { |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3703 | // Only sign events up and down events as the purely move events |
| 3704 | // are tied to their up/down counterparts so signing would be redundant. |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3705 | return INVALID_HMAC; |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3706 | } |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3707 | |
| 3708 | VerifiedMotionEvent verifiedEvent = |
| 3709 | verifiedMotionEventFromMotionEntry(motionEntry, dispatchEntry.rawTransform); |
| 3710 | verifiedEvent.actionMasked = actionMasked; |
| 3711 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS; |
| 3712 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3713 | } |
| 3714 | |
| 3715 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3716 | const KeyEntry& keyEntry, const DispatchEntry& dispatchEntry) const { |
| 3717 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(keyEntry); |
| 3718 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_KEY_EVENT_FLAGS; |
| 3719 | verifiedEvent.action = dispatchEntry.resolvedAction; |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3720 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3721 | } |
| 3722 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3723 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3724 | const std::shared_ptr<Connection>& connection, |
| 3725 | uint32_t seq, bool handled, nsecs_t consumeTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3726 | if (DEBUG_DISPATCH_CYCLE) { |
| 3727 | ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s", |
| 3728 | connection->getInputChannelName().c_str(), seq, toString(handled)); |
| 3729 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3730 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3731 | if (connection->status == Connection::Status::BROKEN || |
| 3732 | connection->status == Connection::Status::ZOMBIE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3733 | return; |
| 3734 | } |
| 3735 | |
| 3736 | // Notify other system components and prepare to start the next dispatch cycle. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3737 | auto command = [this, currentTime, connection, seq, handled, consumeTime]() REQUIRES(mLock) { |
| 3738 | doDispatchCycleFinishedCommand(currentTime, connection, seq, handled, consumeTime); |
| 3739 | }; |
| 3740 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3741 | } |
| 3742 | |
| 3743 | void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3744 | const std::shared_ptr<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3745 | bool notify) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3746 | if (DEBUG_DISPATCH_CYCLE) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 3747 | LOG(DEBUG) << "channel '" << connection->getInputChannelName() << "'~ " << __func__ |
| 3748 | << " - notify=" << toString(notify); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3749 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3750 | |
| 3751 | // Clear the dispatch queues. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3752 | drainDispatchQueue(connection->outboundQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3753 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3754 | drainDispatchQueue(connection->waitQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3755 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3756 | |
| 3757 | // The connection appears to be unrecoverably broken. |
| 3758 | // Ignore already broken or zombie connections. |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3759 | if (connection->status == Connection::Status::NORMAL) { |
| 3760 | connection->status = Connection::Status::BROKEN; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3761 | |
| 3762 | if (notify) { |
| 3763 | // Notify other system components. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3764 | ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", |
| 3765 | connection->getInputChannelName().c_str()); |
| 3766 | |
| 3767 | auto command = [this, connection]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3768 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 3769 | mPolicy.notifyInputChannelBroken(connection->inputChannel->getConnectionToken()); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3770 | }; |
| 3771 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3772 | } |
| 3773 | } |
| 3774 | } |
| 3775 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3776 | void InputDispatcher::drainDispatchQueue(std::deque<DispatchEntry*>& queue) { |
| 3777 | while (!queue.empty()) { |
| 3778 | DispatchEntry* dispatchEntry = queue.front(); |
| 3779 | queue.pop_front(); |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3780 | releaseDispatchEntry(dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3781 | } |
| 3782 | } |
| 3783 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3784 | void InputDispatcher::releaseDispatchEntry(DispatchEntry* dispatchEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3785 | if (dispatchEntry->hasForegroundTarget()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3786 | decrementPendingForegroundDispatches(*(dispatchEntry->eventEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3787 | } |
| 3788 | delete dispatchEntry; |
| 3789 | } |
| 3790 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3791 | int InputDispatcher::handleReceiveCallback(int events, sp<IBinder> connectionToken) { |
| 3792 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3793 | std::shared_ptr<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3794 | if (connection == nullptr) { |
| 3795 | ALOGW("Received looper callback for unknown input channel token %p. events=0x%x", |
| 3796 | connectionToken.get(), events); |
| 3797 | return 0; // remove the callback |
| 3798 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3799 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3800 | bool notify; |
| 3801 | if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { |
| 3802 | if (!(events & ALOOPER_EVENT_INPUT)) { |
| 3803 | ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. " |
| 3804 | "events=0x%x", |
| 3805 | connection->getInputChannelName().c_str(), events); |
| 3806 | return 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3807 | } |
| 3808 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3809 | nsecs_t currentTime = now(); |
| 3810 | bool gotOne = false; |
| 3811 | status_t status = OK; |
| 3812 | for (;;) { |
| 3813 | Result<InputPublisher::ConsumerResponse> result = |
| 3814 | connection->inputPublisher.receiveConsumerResponse(); |
| 3815 | if (!result.ok()) { |
| 3816 | status = result.error().code(); |
| 3817 | break; |
| 3818 | } |
| 3819 | |
| 3820 | if (std::holds_alternative<InputPublisher::Finished>(*result)) { |
| 3821 | const InputPublisher::Finished& finish = |
| 3822 | std::get<InputPublisher::Finished>(*result); |
| 3823 | finishDispatchCycleLocked(currentTime, connection, finish.seq, finish.handled, |
| 3824 | finish.consumeTime); |
| 3825 | } else if (std::holds_alternative<InputPublisher::Timeline>(*result)) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3826 | if (shouldReportMetricsForConnection(*connection)) { |
| 3827 | const InputPublisher::Timeline& timeline = |
| 3828 | std::get<InputPublisher::Timeline>(*result); |
| 3829 | mLatencyTracker |
| 3830 | .trackGraphicsLatency(timeline.inputEventId, |
| 3831 | connection->inputChannel->getConnectionToken(), |
| 3832 | std::move(timeline.graphicsTimeline)); |
| 3833 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3834 | } |
| 3835 | gotOne = true; |
| 3836 | } |
| 3837 | if (gotOne) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3838 | runCommandsLockedInterruptable(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3839 | if (status == WOULD_BLOCK) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3840 | return 1; |
| 3841 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3842 | } |
| 3843 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3844 | notify = status != DEAD_OBJECT || !connection->monitor; |
| 3845 | if (notify) { |
| 3846 | ALOGE("channel '%s' ~ Failed to receive finished signal. status=%s(%d)", |
| 3847 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3848 | status); |
| 3849 | } |
| 3850 | } else { |
| 3851 | // Monitor channels are never explicitly unregistered. |
| 3852 | // We do it automatically when the remote endpoint is closed so don't warn about them. |
| 3853 | const bool stillHaveWindowHandle = |
| 3854 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()) != nullptr; |
| 3855 | notify = !connection->monitor && stillHaveWindowHandle; |
| 3856 | if (notify) { |
| 3857 | ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred. events=0x%x", |
| 3858 | connection->getInputChannelName().c_str(), events); |
| 3859 | } |
| 3860 | } |
| 3861 | |
| 3862 | // Remove the channel. |
| 3863 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), notify); |
| 3864 | return 0; // remove the callback |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3865 | } |
| 3866 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3867 | void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3868 | const CancelationOptions& options) { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3869 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 3870 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3871 | } |
| 3872 | } |
| 3873 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3874 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3875 | const CancelationOptions& options) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 3876 | for (const auto& [_, monitors] : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3877 | for (const Monitor& monitor : monitors) { |
| 3878 | synthesizeCancelationEventsForInputChannelLocked(monitor.inputChannel, options); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3879 | } |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3880 | } |
| 3881 | } |
| 3882 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3883 | void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked( |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 3884 | const std::shared_ptr<InputChannel>& channel, const CancelationOptions& options) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3885 | std::shared_ptr<Connection> connection = getConnectionLocked(channel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3886 | if (connection == nullptr) { |
| 3887 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3888 | } |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3889 | |
| 3890 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3891 | } |
| 3892 | |
| 3893 | void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3894 | const std::shared_ptr<Connection>& connection, const CancelationOptions& options) { |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3895 | if (connection->status == Connection::Status::BROKEN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3896 | return; |
| 3897 | } |
| 3898 | |
| 3899 | nsecs_t currentTime = now(); |
| 3900 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3901 | std::vector<std::unique_ptr<EventEntry>> cancelationEvents = |
Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 3902 | connection->inputState.synthesizeCancelationEvents(currentTime, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3903 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3904 | if (cancelationEvents.empty()) { |
| 3905 | return; |
| 3906 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3907 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 3908 | ALOGD("channel '%s' ~ Synthesized %zu cancelation events to bring channel back in sync " |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3909 | "with reality: %s, mode=%s.", |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3910 | connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason, |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3911 | ftl::enum_string(options.mode).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3912 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3913 | |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3914 | std::string reason = std::string("reason=").append(options.reason); |
| 3915 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 3916 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 3917 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3918 | InputTarget target; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3919 | sp<WindowInfoHandle> windowHandle = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3920 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3921 | if (windowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3922 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3923 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3924 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3925 | } |
| 3926 | target.inputChannel = connection->inputChannel; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3927 | target.flags = InputTarget::Flags::DISPATCH_AS_IS; |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3928 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 3929 | const bool wasEmpty = connection->outboundQueue.empty(); |
| 3930 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3931 | for (size_t i = 0; i < cancelationEvents.size(); i++) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3932 | std::unique_ptr<EventEntry> cancelationEventEntry = std::move(cancelationEvents[i]); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3933 | switch (cancelationEventEntry->type) { |
| 3934 | case EventEntry::Type::KEY: { |
| 3935 | logOutboundKeyDetails("cancel - ", |
| 3936 | static_cast<const KeyEntry&>(*cancelationEventEntry)); |
| 3937 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3938 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3939 | case EventEntry::Type::MOTION: { |
| 3940 | logOutboundMotionDetails("cancel - ", |
| 3941 | static_cast<const MotionEntry&>(*cancelationEventEntry)); |
| 3942 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3943 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3944 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3945 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3946 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3947 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3948 | LOG_ALWAYS_FATAL("Canceling %s events is not supported", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3949 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3950 | break; |
| 3951 | } |
| 3952 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3953 | case EventEntry::Type::DEVICE_RESET: |
| 3954 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3955 | 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] | 3956 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3957 | break; |
| 3958 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3959 | } |
| 3960 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3961 | enqueueDispatchEntryLocked(connection, std::move(cancelationEventEntry), target, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3962 | InputTarget::Flags::DISPATCH_AS_IS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3963 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3964 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 3965 | // If the outbound queue was previously empty, start the dispatch cycle going. |
| 3966 | if (wasEmpty && !connection->outboundQueue.empty()) { |
| 3967 | startDispatchCycleLocked(currentTime, connection); |
| 3968 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3969 | } |
| 3970 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3971 | void InputDispatcher::synthesizePointerDownEventsForConnectionLocked( |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3972 | const nsecs_t downTime, const std::shared_ptr<Connection>& connection, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 3973 | ftl::Flags<InputTarget::Flags> targetFlags) { |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3974 | if (connection->status == Connection::Status::BROKEN) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3975 | return; |
| 3976 | } |
| 3977 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3978 | std::vector<std::unique_ptr<EventEntry>> downEvents = |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3979 | connection->inputState.synthesizePointerDownEvents(downTime); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3980 | |
| 3981 | if (downEvents.empty()) { |
| 3982 | return; |
| 3983 | } |
| 3984 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3985 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3986 | ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.", |
| 3987 | connection->getInputChannelName().c_str(), downEvents.size()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3988 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3989 | |
| 3990 | InputTarget target; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3991 | sp<WindowInfoHandle> windowHandle = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3992 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3993 | if (windowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3994 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3995 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3996 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3997 | } |
| 3998 | target.inputChannel = connection->inputChannel; |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 3999 | target.flags = targetFlags; |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4000 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 4001 | const bool wasEmpty = connection->outboundQueue.empty(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4002 | for (std::unique_ptr<EventEntry>& downEventEntry : downEvents) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4003 | switch (downEventEntry->type) { |
| 4004 | case EventEntry::Type::MOTION: { |
| 4005 | logOutboundMotionDetails("down - ", |
| 4006 | static_cast<const MotionEntry&>(*downEventEntry)); |
| 4007 | break; |
| 4008 | } |
| 4009 | |
| 4010 | case EventEntry::Type::KEY: |
| 4011 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 4012 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4013 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4014 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4015 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4016 | case EventEntry::Type::SENSOR: |
| 4017 | case EventEntry::Type::DRAG: { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4018 | 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] | 4019 | ftl::enum_string(downEventEntry->type).c_str()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4020 | break; |
| 4021 | } |
| 4022 | } |
| 4023 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4024 | enqueueDispatchEntryLocked(connection, std::move(downEventEntry), target, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 4025 | InputTarget::Flags::DISPATCH_AS_IS); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4026 | } |
| 4027 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 4028 | // If the outbound queue was previously empty, start the dispatch cycle going. |
| 4029 | if (wasEmpty && !connection->outboundQueue.empty()) { |
| 4030 | startDispatchCycleLocked(downTime, connection); |
| 4031 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4032 | } |
| 4033 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4034 | void InputDispatcher::synthesizeCancelationEventsForWindowLocked( |
| 4035 | const sp<WindowInfoHandle>& windowHandle, const CancelationOptions& options) { |
| 4036 | if (windowHandle != nullptr) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 4037 | std::shared_ptr<Connection> wallpaperConnection = |
| 4038 | getConnectionLocked(windowHandle->getToken()); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4039 | if (wallpaperConnection != nullptr) { |
| 4040 | synthesizeCancelationEventsForConnectionLocked(wallpaperConnection, options); |
| 4041 | } |
| 4042 | } |
| 4043 | } |
| 4044 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4045 | std::unique_ptr<MotionEntry> InputDispatcher::splitMotionEvent( |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 4046 | const MotionEntry& originalMotionEntry, std::bitset<MAX_POINTER_ID + 1> pointerIds, |
| 4047 | nsecs_t splitDownTime) { |
| 4048 | ALOG_ASSERT(pointerIds.any()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4049 | |
| 4050 | uint32_t splitPointerIndexMap[MAX_POINTERS]; |
| 4051 | PointerProperties splitPointerProperties[MAX_POINTERS]; |
| 4052 | PointerCoords splitPointerCoords[MAX_POINTERS]; |
| 4053 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 4054 | uint32_t originalPointerCount = originalMotionEntry.pointerCount; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4055 | uint32_t splitPointerCount = 0; |
| 4056 | |
| 4057 | for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4058 | originalPointerIndex++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4059 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 4060 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4061 | uint32_t pointerId = uint32_t(pointerProperties.id); |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 4062 | if (pointerIds.test(pointerId)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4063 | splitPointerIndexMap[splitPointerCount] = originalPointerIndex; |
| 4064 | splitPointerProperties[splitPointerCount].copyFrom(pointerProperties); |
| 4065 | splitPointerCoords[splitPointerCount].copyFrom( |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 4066 | originalMotionEntry.pointerCoords[originalPointerIndex]); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4067 | splitPointerCount += 1; |
| 4068 | } |
| 4069 | } |
| 4070 | |
| 4071 | if (splitPointerCount != pointerIds.count()) { |
| 4072 | // This is bad. We are missing some of the pointers that we expected to deliver. |
| 4073 | // Most likely this indicates that we received an ACTION_MOVE events that has |
| 4074 | // different pointer ids than we expected based on the previous ACTION_DOWN |
| 4075 | // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers |
| 4076 | // in this way. |
| 4077 | ALOGW("Dropping split motion event because the pointer count is %d but " |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 4078 | "we expected there to be %zu pointers. This probably means we received " |
Siarhei Vishniakou | 16e4fa0 | 2023-02-16 17:48:56 -0800 | [diff] [blame] | 4079 | "a broken sequence of pointer ids from the input device: %s", |
| 4080 | splitPointerCount, pointerIds.count(), originalMotionEntry.getDescription().c_str()); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4081 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4082 | } |
| 4083 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 4084 | int32_t action = originalMotionEntry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4085 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4086 | if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN || |
| 4087 | maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4088 | int32_t originalPointerIndex = getMotionEventActionPointerIndex(action); |
| 4089 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 4090 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4091 | uint32_t pointerId = uint32_t(pointerProperties.id); |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 4092 | if (pointerIds.test(pointerId)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4093 | if (pointerIds.count() == 1) { |
| 4094 | // The first/last pointer went down/up. |
| 4095 | action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4096 | ? AMOTION_EVENT_ACTION_DOWN |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4097 | : (originalMotionEntry.flags & AMOTION_EVENT_FLAG_CANCELED) != 0 |
| 4098 | ? AMOTION_EVENT_ACTION_CANCEL |
| 4099 | : AMOTION_EVENT_ACTION_UP; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4100 | } else { |
| 4101 | // A secondary pointer went down/up. |
| 4102 | uint32_t splitPointerIndex = 0; |
| 4103 | while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { |
| 4104 | splitPointerIndex += 1; |
| 4105 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4106 | action = maskedAction | |
| 4107 | (splitPointerIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4108 | } |
| 4109 | } else { |
| 4110 | // An unrelated pointer changed. |
| 4111 | action = AMOTION_EVENT_ACTION_MOVE; |
| 4112 | } |
| 4113 | } |
| 4114 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4115 | if (action == AMOTION_EVENT_ACTION_DOWN) { |
| 4116 | LOG_ALWAYS_FATAL_IF(splitDownTime != originalMotionEntry.eventTime, |
| 4117 | "Split motion event has mismatching downTime and eventTime for " |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4118 | "ACTION_DOWN, motionEntry=%s, splitDownTime=%" PRId64, |
| 4119 | originalMotionEntry.getDescription().c_str(), splitDownTime); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4120 | } |
| 4121 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4122 | int32_t newId = mIdGenerator.nextId(); |
| 4123 | if (ATRACE_ENABLED()) { |
| 4124 | std::string message = StringPrintf("Split MotionEvent(id=0x%" PRIx32 |
| 4125 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 4126 | originalMotionEntry.id, newId); |
| 4127 | ATRACE_NAME(message.c_str()); |
| 4128 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4129 | std::unique_ptr<MotionEntry> splitMotionEntry = |
| 4130 | std::make_unique<MotionEntry>(newId, originalMotionEntry.eventTime, |
| 4131 | originalMotionEntry.deviceId, originalMotionEntry.source, |
| 4132 | originalMotionEntry.displayId, |
| 4133 | originalMotionEntry.policyFlags, action, |
| 4134 | originalMotionEntry.actionButton, |
| 4135 | originalMotionEntry.flags, originalMotionEntry.metaState, |
| 4136 | originalMotionEntry.buttonState, |
| 4137 | originalMotionEntry.classification, |
| 4138 | originalMotionEntry.edgeFlags, |
| 4139 | originalMotionEntry.xPrecision, |
| 4140 | originalMotionEntry.yPrecision, |
| 4141 | originalMotionEntry.xCursorPosition, |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4142 | originalMotionEntry.yCursorPosition, splitDownTime, |
| 4143 | splitPointerCount, splitPointerProperties, |
| 4144 | splitPointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4145 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 4146 | if (originalMotionEntry.injectionState) { |
| 4147 | splitMotionEntry->injectionState = originalMotionEntry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4148 | splitMotionEntry->injectionState->refCount += 1; |
| 4149 | } |
| 4150 | |
| 4151 | return splitMotionEntry; |
| 4152 | } |
| 4153 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4154 | void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4155 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4156 | ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args.eventTime); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4157 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4158 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4159 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4160 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4161 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4162 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4163 | std::unique_ptr<ConfigurationChangedEntry> newEntry = |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4164 | std::make_unique<ConfigurationChangedEntry>(args.id, args.eventTime); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4165 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4166 | } // release lock |
| 4167 | |
| 4168 | if (needWake) { |
| 4169 | mLooper->wake(); |
| 4170 | } |
| 4171 | } |
| 4172 | |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 4173 | /** |
| 4174 | * If one of the meta shortcuts is detected, process them here: |
Vaibhav Devmurari | 34cd5b0 | 2023-02-23 14:51:18 +0000 | [diff] [blame] | 4175 | * Meta + Backspace; Meta + Grave; Meta + Left arrow -> generate BACK |
| 4176 | * Most System shortcuts are handled in PhoneWindowManager.java except 'Back' shortcuts. Unlike |
| 4177 | * Back, other shortcuts DO NOT need to be sent to applications and are fully handled by the system. |
| 4178 | * But for Back key and Back shortcuts, we need to send KEYCODE_BACK to applications which can |
| 4179 | * potentially handle the back key presses. |
| 4180 | * Note: We don't send any Meta based KeyEvents to applications, so we need to convert to a KeyEvent |
| 4181 | * where meta modifier is off before sending. Currently only use case is 'Back'. |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 4182 | */ |
| 4183 | void InputDispatcher::accelerateMetaShortcuts(const int32_t deviceId, const int32_t action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4184 | int32_t& keyCode, int32_t& metaState) { |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 4185 | if (metaState & AMETA_META_ON && action == AKEY_EVENT_ACTION_DOWN) { |
| 4186 | int32_t newKeyCode = AKEYCODE_UNKNOWN; |
Vaibhav Devmurari | 34cd5b0 | 2023-02-23 14:51:18 +0000 | [diff] [blame] | 4187 | if (keyCode == AKEYCODE_DEL || keyCode == AKEYCODE_GRAVE || keyCode == AKEYCODE_DPAD_LEFT) { |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 4188 | newKeyCode = AKEYCODE_BACK; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 4189 | } |
| 4190 | if (newKeyCode != AKEYCODE_UNKNOWN) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4191 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 4192 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4193 | mReplacedKeys[replacement] = newKeyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 4194 | keyCode = newKeyCode; |
| 4195 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 4196 | } |
| 4197 | } else if (action == AKEY_EVENT_ACTION_UP) { |
| 4198 | // In order to maintain a consistent stream of up and down events, check to see if the key |
| 4199 | // going up is one we've replaced in a down event and haven't yet replaced in an up event, |
| 4200 | // 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] | 4201 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 4202 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4203 | auto replacementIt = mReplacedKeys.find(replacement); |
| 4204 | if (replacementIt != mReplacedKeys.end()) { |
| 4205 | keyCode = replacementIt->second; |
| 4206 | mReplacedKeys.erase(replacementIt); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 4207 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 4208 | } |
| 4209 | } |
| 4210 | } |
| 4211 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4212 | void InputDispatcher::notifyKey(const NotifyKeyArgs& args) { |
Prabir Pradhan | 96282b0 | 2023-02-24 22:36:17 +0000 | [diff] [blame] | 4213 | ALOGD_IF(debugInboundEventDetails(), |
| 4214 | "notifyKey - id=%" PRIx32 ", eventTime=%" PRId64 |
| 4215 | ", deviceId=%d, source=%s, displayId=%" PRId32 |
| 4216 | "policyFlags=0x%x, action=%s, flags=0x%x, keyCode=%s, scanCode=0x%x, metaState=0x%x, " |
| 4217 | "downTime=%" PRId64, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4218 | args.id, args.eventTime, args.deviceId, inputEventSourceToString(args.source).c_str(), |
| 4219 | args.displayId, args.policyFlags, KeyEvent::actionToString(args.action), args.flags, |
| 4220 | KeyEvent::getLabel(args.keyCode), args.scanCode, args.metaState, args.downTime); |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4221 | Result<void> keyCheck = validateKeyEvent(args.action); |
| 4222 | if (!keyCheck.ok()) { |
| 4223 | LOG(ERROR) << "invalid key event: " << keyCheck.error(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4224 | return; |
| 4225 | } |
| 4226 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4227 | uint32_t policyFlags = args.policyFlags; |
| 4228 | int32_t flags = args.flags; |
| 4229 | int32_t metaState = args.metaState; |
Siarhei Vishniakou | 622bd32 | 2018-10-29 18:02:27 -0700 | [diff] [blame] | 4230 | // InputDispatcher tracks and generates key repeats on behalf of |
| 4231 | // whatever notifies it, so repeatCount should always be set to 0 |
| 4232 | constexpr int32_t repeatCount = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4233 | if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { |
| 4234 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 4235 | flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
| 4236 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4237 | if (policyFlags & POLICY_FLAG_FUNCTION) { |
| 4238 | metaState |= AMETA_FUNCTION_ON; |
| 4239 | } |
| 4240 | |
| 4241 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 4242 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4243 | int32_t keyCode = args.keyCode; |
| 4244 | accelerateMetaShortcuts(args.deviceId, args.action, keyCode, metaState); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 4245 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4246 | KeyEvent event; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4247 | event.initialize(args.id, args.deviceId, args.source, args.displayId, INVALID_HMAC, args.action, |
| 4248 | flags, keyCode, args.scanCode, metaState, repeatCount, args.downTime, |
| 4249 | args.eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4250 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4251 | android::base::Timer t; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4252 | mPolicy.interceptKeyBeforeQueueing(event, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4253 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4254 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4255 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4256 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4257 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4258 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4259 | { // acquire lock |
| 4260 | mLock.lock(); |
| 4261 | |
| 4262 | if (shouldSendKeyToInputFilterLocked(args)) { |
| 4263 | mLock.unlock(); |
| 4264 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4265 | policyFlags |= POLICY_FLAG_FILTERED; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4266 | if (!mPolicy.filterInputEvent(event, policyFlags)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4267 | return; // event was consumed by the filter |
| 4268 | } |
| 4269 | |
| 4270 | mLock.lock(); |
| 4271 | } |
| 4272 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4273 | std::unique_ptr<KeyEntry> newEntry = |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4274 | std::make_unique<KeyEntry>(args.id, args.eventTime, args.deviceId, args.source, |
| 4275 | args.displayId, policyFlags, args.action, flags, keyCode, |
| 4276 | args.scanCode, metaState, repeatCount, args.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4277 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4278 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4279 | mLock.unlock(); |
| 4280 | } // release lock |
| 4281 | |
| 4282 | if (needWake) { |
| 4283 | mLooper->wake(); |
| 4284 | } |
| 4285 | } |
| 4286 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4287 | bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs& args) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4288 | return mInputFilterEnabled; |
| 4289 | } |
| 4290 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4291 | void InputDispatcher::notifyMotion(const NotifyMotionArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4292 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 96282b0 | 2023-02-24 22:36:17 +0000 | [diff] [blame] | 4293 | ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=%s, " |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4294 | "displayId=%" PRId32 ", policyFlags=0x%x, " |
Siarhei Vishniakou | 6ebd069 | 2022-10-20 15:05:45 -0700 | [diff] [blame] | 4295 | "action=%s, actionButton=0x%x, flags=0x%x, metaState=0x%x, buttonState=0x%x, " |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4296 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, xCursorPosition=%f, " |
| 4297 | "yCursorPosition=%f, downTime=%" PRId64, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4298 | args.id, args.eventTime, args.deviceId, inputEventSourceToString(args.source).c_str(), |
| 4299 | args.displayId, args.policyFlags, MotionEvent::actionToString(args.action).c_str(), |
| 4300 | args.actionButton, args.flags, args.metaState, args.buttonState, args.edgeFlags, |
| 4301 | args.xPrecision, args.yPrecision, args.xCursorPosition, args.yCursorPosition, |
| 4302 | args.downTime); |
| 4303 | for (uint32_t i = 0; i < args.pointerCount; i++) { |
Prabir Pradhan | 96282b0 | 2023-02-24 22:36:17 +0000 | [diff] [blame] | 4304 | ALOGD(" Pointer %d: id=%d, toolType=%s, x=%f, y=%f, pressure=%f, size=%f, " |
| 4305 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, orientation=%f", |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4306 | i, args.pointerProperties[i].id, |
| 4307 | ftl::enum_string(args.pointerProperties[i].toolType).c_str(), |
| 4308 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 4309 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 4310 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 4311 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 4312 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 4313 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 4314 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 4315 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 4316 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4317 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4318 | } |
Siarhei Vishniakou | 4ca9727 | 2023-03-01 11:31:35 -0800 | [diff] [blame] | 4319 | |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4320 | Result<void> motionCheck = validateMotionEvent(args.action, args.actionButton, |
| 4321 | args.pointerCount, args.pointerProperties); |
| 4322 | if (!motionCheck.ok()) { |
| 4323 | LOG(ERROR) << "Invalid event: " << args.dump() << "; reason: " << motionCheck.error(); |
Siarhei Vishniakou | 4ca9727 | 2023-03-01 11:31:35 -0800 | [diff] [blame] | 4324 | return; |
| 4325 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4326 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4327 | uint32_t policyFlags = args.policyFlags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4328 | policyFlags |= POLICY_FLAG_TRUSTED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4329 | |
| 4330 | android::base::Timer t; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4331 | mPolicy.interceptMotionBeforeQueueing(args.displayId, args.eventTime, policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4332 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4333 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4334 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4335 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4336 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4337 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4338 | { // acquire lock |
| 4339 | mLock.lock(); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 4340 | if (!(policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
| 4341 | // Set the flag anyway if we already have an ongoing gesture. That would allow us to |
| 4342 | // complete the processing of the current stroke. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4343 | const auto touchStateIt = mTouchStatesByDisplay.find(args.displayId); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 4344 | if (touchStateIt != mTouchStatesByDisplay.end()) { |
| 4345 | const TouchState& touchState = touchStateIt->second; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4346 | if (touchState.deviceId == args.deviceId && touchState.isDown()) { |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 4347 | policyFlags |= POLICY_FLAG_PASS_TO_USER; |
| 4348 | } |
| 4349 | } |
| 4350 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4351 | |
| 4352 | if (shouldSendMotionToInputFilterLocked(args)) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4353 | ui::Transform displayTransform; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4354 | if (const auto it = mDisplayInfos.find(args.displayId); it != mDisplayInfos.end()) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4355 | displayTransform = it->second.transform; |
| 4356 | } |
| 4357 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4358 | mLock.unlock(); |
| 4359 | |
| 4360 | MotionEvent event; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4361 | event.initialize(args.id, args.deviceId, args.source, args.displayId, INVALID_HMAC, |
| 4362 | args.action, args.actionButton, args.flags, args.edgeFlags, |
| 4363 | args.metaState, args.buttonState, args.classification, |
| 4364 | displayTransform, args.xPrecision, args.yPrecision, |
| 4365 | args.xCursorPosition, args.yCursorPosition, displayTransform, |
| 4366 | args.downTime, args.eventTime, args.pointerCount, |
| 4367 | args.pointerProperties, args.pointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4368 | |
| 4369 | policyFlags |= POLICY_FLAG_FILTERED; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4370 | if (!mPolicy.filterInputEvent(event, policyFlags)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4371 | return; // event was consumed by the filter |
| 4372 | } |
| 4373 | |
| 4374 | mLock.lock(); |
| 4375 | } |
| 4376 | |
| 4377 | // Just enqueue a new motion event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4378 | std::unique_ptr<MotionEntry> newEntry = |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4379 | std::make_unique<MotionEntry>(args.id, args.eventTime, args.deviceId, args.source, |
| 4380 | args.displayId, policyFlags, args.action, |
| 4381 | args.actionButton, args.flags, args.metaState, |
| 4382 | args.buttonState, args.classification, args.edgeFlags, |
| 4383 | args.xPrecision, args.yPrecision, |
| 4384 | args.xCursorPosition, args.yCursorPosition, |
| 4385 | args.downTime, args.pointerCount, |
| 4386 | args.pointerProperties, args.pointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4387 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4388 | if (args.id != android::os::IInputConstants::INVALID_INPUT_EVENT_ID && |
| 4389 | IdGenerator::getSource(args.id) == IdGenerator::Source::INPUT_READER && |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 4390 | !mInputFilterEnabled) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4391 | const bool isDown = args.action == AMOTION_EVENT_ACTION_DOWN; |
| 4392 | mLatencyTracker.trackListener(args.id, isDown, args.eventTime, args.readTime); |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 4393 | } |
| 4394 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4395 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4396 | mLock.unlock(); |
| 4397 | } // release lock |
| 4398 | |
| 4399 | if (needWake) { |
| 4400 | mLooper->wake(); |
| 4401 | } |
| 4402 | } |
| 4403 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4404 | void InputDispatcher::notifySensor(const NotifySensorArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4405 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4406 | ALOGD("notifySensor - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 4407 | " sensorType=%s", |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4408 | args.id, args.eventTime, args.deviceId, args.source, |
| 4409 | ftl::enum_string(args.sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4410 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4411 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4412 | bool needWake = false; |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4413 | { // acquire lock |
| 4414 | mLock.lock(); |
| 4415 | |
| 4416 | // Just enqueue a new sensor event. |
| 4417 | std::unique_ptr<SensorEntry> newEntry = |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4418 | std::make_unique<SensorEntry>(args.id, args.eventTime, args.deviceId, args.source, |
| 4419 | /* policyFlags=*/0, args.hwTimestamp, args.sensorType, |
| 4420 | args.accuracy, args.accuracyChanged, args.values); |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4421 | |
| 4422 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
| 4423 | mLock.unlock(); |
| 4424 | } // release lock |
| 4425 | |
| 4426 | if (needWake) { |
| 4427 | mLooper->wake(); |
| 4428 | } |
| 4429 | } |
| 4430 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4431 | void InputDispatcher::notifyVibratorState(const NotifyVibratorStateArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4432 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4433 | ALOGD("notifyVibratorState - eventTime=%" PRId64 ", device=%d, isOn=%d", args.eventTime, |
| 4434 | args.deviceId, args.isOn); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4435 | } |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4436 | mPolicy.notifyVibratorState(args.deviceId, args.isOn); |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 4437 | } |
| 4438 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4439 | bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs& args) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4440 | return mInputFilterEnabled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4441 | } |
| 4442 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4443 | void InputDispatcher::notifySwitch(const NotifySwitchArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4444 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4445 | ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, " |
| 4446 | "switchMask=0x%08x", |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4447 | args.eventTime, args.policyFlags, args.switchValues, args.switchMask); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4448 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4449 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4450 | uint32_t policyFlags = args.policyFlags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4451 | policyFlags |= POLICY_FLAG_TRUSTED; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4452 | mPolicy.notifySwitch(args.eventTime, args.switchValues, args.switchMask, policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4453 | } |
| 4454 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4455 | void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4456 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4457 | ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args.eventTime, |
| 4458 | args.deviceId); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4459 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4460 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4461 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4462 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4463 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4464 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4465 | std::unique_ptr<DeviceResetEntry> newEntry = |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4466 | std::make_unique<DeviceResetEntry>(args.id, args.eventTime, args.deviceId); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4467 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4468 | } // release lock |
| 4469 | |
| 4470 | if (needWake) { |
| 4471 | mLooper->wake(); |
| 4472 | } |
| 4473 | } |
| 4474 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4475 | void InputDispatcher::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4476 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4477 | ALOGD("notifyPointerCaptureChanged - eventTime=%" PRId64 ", enabled=%s", args.eventTime, |
| 4478 | args.request.enable ? "true" : "false"); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4479 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4480 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4481 | bool needWake = false; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4482 | { // acquire lock |
| 4483 | std::scoped_lock _l(mLock); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4484 | auto entry = |
| 4485 | std::make_unique<PointerCaptureChangedEntry>(args.id, args.eventTime, args.request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4486 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 4487 | } // release lock |
| 4488 | |
| 4489 | if (needWake) { |
| 4490 | mLooper->wake(); |
| 4491 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4492 | } |
| 4493 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4494 | InputEventInjectionResult InputDispatcher::injectInputEvent(const InputEvent* event, |
| 4495 | std::optional<int32_t> targetUid, |
| 4496 | InputEventInjectionSync syncMode, |
| 4497 | std::chrono::milliseconds timeout, |
| 4498 | uint32_t policyFlags) { |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4499 | Result<void> eventValidation = validateInputEvent(*event); |
| 4500 | if (!eventValidation.ok()) { |
| 4501 | LOG(INFO) << "Injection failed: invalid event: " << eventValidation.error(); |
| 4502 | return InputEventInjectionResult::FAILED; |
| 4503 | } |
| 4504 | |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4505 | if (debugInboundEventDetails()) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4506 | LOG(DEBUG) << __func__ << ": targetUid=" << toString(targetUid) |
| 4507 | << ", syncMode=" << ftl::enum_string(syncMode) << ", timeout=" << timeout.count() |
| 4508 | << "ms, policyFlags=0x" << std::hex << policyFlags << std::dec |
| 4509 | << ", event=" << *event; |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4510 | } |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 4511 | 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] | 4512 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4513 | policyFlags |= POLICY_FLAG_INJECTED | POLICY_FLAG_TRUSTED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4514 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4515 | // 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] | 4516 | // that have gone through the InputFilter. If the event passed through the InputFilter, assign |
| 4517 | // the provided device id. If the InputFilter is accessibility, and it modifies or synthesizes |
| 4518 | // the injected event, it is responsible for setting POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY. |
| 4519 | // For those events, we will set FLAG_IS_ACCESSIBILITY_EVENT to allow apps to distinguish them |
| 4520 | // from events that originate from actual hardware. |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4521 | int32_t resolvedDeviceId = VIRTUAL_KEYBOARD_ID; |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4522 | if (policyFlags & POLICY_FLAG_FILTERED) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4523 | resolvedDeviceId = event->getDeviceId(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4524 | } |
| 4525 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4526 | std::queue<std::unique_ptr<EventEntry>> injectedEntries; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4527 | switch (event->getType()) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4528 | case InputEventType::KEY: { |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4529 | const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event); |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4530 | const int32_t action = incomingKey.getAction(); |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4531 | int32_t flags = incomingKey.getFlags(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4532 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4533 | flags |= AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4534 | } |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4535 | int32_t keyCode = incomingKey.getKeyCode(); |
| 4536 | int32_t metaState = incomingKey.getMetaState(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4537 | accelerateMetaShortcuts(resolvedDeviceId, action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4538 | /*byref*/ keyCode, /*byref*/ metaState); |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4539 | KeyEvent keyEvent; |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4540 | keyEvent.initialize(incomingKey.getId(), resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4541 | incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode, |
| 4542 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), |
| 4543 | incomingKey.getDownTime(), incomingKey.getEventTime()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4544 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4545 | if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) { |
| 4546 | policyFlags |= POLICY_FLAG_VIRTUAL; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4547 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4548 | |
| 4549 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 4550 | android::base::Timer t; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4551 | mPolicy.interceptKeyBeforeQueueing(keyEvent, /*byref*/ policyFlags); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4552 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4553 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
| 4554 | std::to_string(t.duration().count()).c_str()); |
| 4555 | } |
| 4556 | } |
| 4557 | |
| 4558 | mLock.lock(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4559 | std::unique_ptr<KeyEntry> injectedEntry = |
| 4560 | std::make_unique<KeyEntry>(incomingKey.getId(), incomingKey.getEventTime(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4561 | resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4562 | incomingKey.getDisplayId(), policyFlags, action, |
| 4563 | flags, keyCode, incomingKey.getScanCode(), metaState, |
| 4564 | incomingKey.getRepeatCount(), |
| 4565 | incomingKey.getDownTime()); |
| 4566 | injectedEntries.push(std::move(injectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4567 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4568 | } |
| 4569 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4570 | case InputEventType::MOTION: { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4571 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4572 | const bool isPointerEvent = |
| 4573 | isFromSource(event->getSource(), AINPUT_SOURCE_CLASS_POINTER); |
| 4574 | // If a pointer event has no displayId specified, inject it to the default display. |
| 4575 | const uint32_t displayId = isPointerEvent && (event->getDisplayId() == ADISPLAY_ID_NONE) |
| 4576 | ? ADISPLAY_ID_DEFAULT |
| 4577 | : event->getDisplayId(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4578 | int32_t flags = motionEvent.getFlags(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4579 | |
| 4580 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4581 | nsecs_t eventTime = motionEvent.getEventTime(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4582 | android::base::Timer t; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4583 | mPolicy.interceptMotionBeforeQueueing(displayId, eventTime, /*byref*/ policyFlags); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4584 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4585 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
| 4586 | std::to_string(t.duration().count()).c_str()); |
| 4587 | } |
| 4588 | } |
| 4589 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4590 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4591 | flags |= AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4592 | } |
| 4593 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4594 | mLock.lock(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4595 | const nsecs_t* sampleEventTimes = motionEvent.getSampleEventTimes(); |
| 4596 | const PointerCoords* samplePointerCoords = motionEvent.getSamplePointerCoords(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4597 | std::unique_ptr<MotionEntry> injectedEntry = |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4598 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, |
| 4599 | resolvedDeviceId, motionEvent.getSource(), |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4600 | displayId, policyFlags, motionEvent.getAction(), |
| 4601 | motionEvent.getActionButton(), flags, |
| 4602 | motionEvent.getMetaState(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4603 | motionEvent.getButtonState(), |
| 4604 | motionEvent.getClassification(), |
| 4605 | motionEvent.getEdgeFlags(), |
| 4606 | motionEvent.getXPrecision(), |
| 4607 | motionEvent.getYPrecision(), |
| 4608 | motionEvent.getRawXCursorPosition(), |
| 4609 | motionEvent.getRawYCursorPosition(), |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4610 | motionEvent.getDownTime(), |
| 4611 | motionEvent.getPointerCount(), |
| 4612 | motionEvent.getPointerProperties(), |
| 4613 | samplePointerCoords); |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4614 | transformMotionEntryForInjectionLocked(*injectedEntry, motionEvent.getTransform()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4615 | injectedEntries.push(std::move(injectedEntry)); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4616 | for (size_t i = motionEvent.getHistorySize(); i > 0; i--) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4617 | sampleEventTimes += 1; |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4618 | samplePointerCoords += motionEvent.getPointerCount(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4619 | std::unique_ptr<MotionEntry> nextInjectedEntry = |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4620 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, |
| 4621 | resolvedDeviceId, motionEvent.getSource(), |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4622 | displayId, policyFlags, |
| 4623 | motionEvent.getAction(), |
| 4624 | motionEvent.getActionButton(), flags, |
| 4625 | motionEvent.getMetaState(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4626 | motionEvent.getButtonState(), |
| 4627 | motionEvent.getClassification(), |
| 4628 | motionEvent.getEdgeFlags(), |
| 4629 | motionEvent.getXPrecision(), |
| 4630 | motionEvent.getYPrecision(), |
| 4631 | motionEvent.getRawXCursorPosition(), |
| 4632 | motionEvent.getRawYCursorPosition(), |
| 4633 | motionEvent.getDownTime(), |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4634 | motionEvent.getPointerCount(), |
| 4635 | motionEvent.getPointerProperties(), |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4636 | samplePointerCoords); |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4637 | transformMotionEntryForInjectionLocked(*nextInjectedEntry, |
| 4638 | motionEvent.getTransform()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4639 | injectedEntries.push(std::move(nextInjectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4640 | } |
| 4641 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4642 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4643 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4644 | default: |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4645 | LOG(WARNING) << "Cannot inject " << ftl::enum_string(event->getType()) << " events"; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4646 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4647 | } |
| 4648 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4649 | InjectionState* injectionState = new InjectionState(targetUid); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4650 | if (syncMode == InputEventInjectionSync::NONE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4651 | injectionState->injectionIsAsync = true; |
| 4652 | } |
| 4653 | |
| 4654 | injectionState->refCount += 1; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4655 | injectedEntries.back()->injectionState = injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4656 | |
| 4657 | bool needWake = false; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4658 | while (!injectedEntries.empty()) { |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 4659 | if (DEBUG_INJECTION) { |
| 4660 | LOG(DEBUG) << "Injecting " << injectedEntries.front()->getDescription(); |
| 4661 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4662 | needWake |= enqueueInboundEventLocked(std::move(injectedEntries.front())); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4663 | injectedEntries.pop(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4664 | } |
| 4665 | |
| 4666 | mLock.unlock(); |
| 4667 | |
| 4668 | if (needWake) { |
| 4669 | mLooper->wake(); |
| 4670 | } |
| 4671 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4672 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4673 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4674 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4675 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4676 | if (syncMode == InputEventInjectionSync::NONE) { |
| 4677 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4678 | } else { |
| 4679 | for (;;) { |
| 4680 | injectionResult = injectionState->injectionResult; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4681 | if (injectionResult != InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4682 | break; |
| 4683 | } |
| 4684 | |
| 4685 | nsecs_t remainingTimeout = endTime - now(); |
| 4686 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4687 | if (DEBUG_INJECTION) { |
| 4688 | ALOGD("injectInputEvent - Timed out waiting for injection result " |
| 4689 | "to become available."); |
| 4690 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4691 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4692 | break; |
| 4693 | } |
| 4694 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4695 | mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4696 | } |
| 4697 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4698 | if (injectionResult == InputEventInjectionResult::SUCCEEDED && |
| 4699 | syncMode == InputEventInjectionSync::WAIT_FOR_FINISHED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4700 | while (injectionState->pendingForegroundDispatches != 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4701 | if (DEBUG_INJECTION) { |
| 4702 | ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", |
| 4703 | injectionState->pendingForegroundDispatches); |
| 4704 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4705 | nsecs_t remainingTimeout = endTime - now(); |
| 4706 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4707 | if (DEBUG_INJECTION) { |
| 4708 | ALOGD("injectInputEvent - Timed out waiting for pending foreground " |
| 4709 | "dispatches to finish."); |
| 4710 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4711 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4712 | break; |
| 4713 | } |
| 4714 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4715 | mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4716 | } |
| 4717 | } |
| 4718 | } |
| 4719 | |
| 4720 | injectionState->release(); |
| 4721 | } // release lock |
| 4722 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4723 | if (DEBUG_INJECTION) { |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 4724 | LOG(DEBUG) << "injectInputEvent - Finished with result " |
| 4725 | << ftl::enum_string(injectionResult); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4726 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4727 | |
| 4728 | return injectionResult; |
| 4729 | } |
| 4730 | |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4731 | std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4732 | std::array<uint8_t, 32> calculatedHmac; |
| 4733 | std::unique_ptr<VerifiedInputEvent> result; |
| 4734 | switch (event.getType()) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4735 | case InputEventType::KEY: { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4736 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event); |
| 4737 | VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent); |
| 4738 | result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4739 | calculatedHmac = sign(verifiedKeyEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4740 | break; |
| 4741 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4742 | case InputEventType::MOTION: { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4743 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event); |
| 4744 | VerifiedMotionEvent verifiedMotionEvent = |
| 4745 | verifiedMotionEventFromMotionEvent(motionEvent); |
| 4746 | result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4747 | calculatedHmac = sign(verifiedMotionEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4748 | break; |
| 4749 | } |
| 4750 | default: { |
| 4751 | ALOGE("Cannot verify events of type %" PRId32, event.getType()); |
| 4752 | return nullptr; |
| 4753 | } |
| 4754 | } |
| 4755 | if (calculatedHmac == INVALID_HMAC) { |
| 4756 | return nullptr; |
| 4757 | } |
tyiu | 1573a67 | 2023-02-21 22:38:32 +0000 | [diff] [blame] | 4758 | if (0 != CRYPTO_memcmp(calculatedHmac.data(), event.getHmac().data(), calculatedHmac.size())) { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4759 | return nullptr; |
| 4760 | } |
| 4761 | return result; |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4762 | } |
| 4763 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4764 | void InputDispatcher::setInjectionResult(EventEntry& entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4765 | InputEventInjectionResult injectionResult) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4766 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4767 | if (injectionState) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4768 | if (DEBUG_INJECTION) { |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 4769 | LOG(DEBUG) << "Setting input event injection result to " |
| 4770 | << ftl::enum_string(injectionResult); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4771 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4772 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4773 | if (injectionState->injectionIsAsync && !(entry.policyFlags & POLICY_FLAG_FILTERED)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4774 | // Log the outcome since the injector did not wait for the injection result. |
| 4775 | switch (injectionResult) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4776 | case InputEventInjectionResult::SUCCEEDED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4777 | ALOGV("Asynchronous input event injection succeeded."); |
| 4778 | break; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4779 | case InputEventInjectionResult::TARGET_MISMATCH: |
| 4780 | ALOGV("Asynchronous input event injection target mismatch."); |
| 4781 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4782 | case InputEventInjectionResult::FAILED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4783 | ALOGW("Asynchronous input event injection failed."); |
| 4784 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4785 | case InputEventInjectionResult::TIMED_OUT: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4786 | ALOGW("Asynchronous input event injection timed out."); |
| 4787 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4788 | case InputEventInjectionResult::PENDING: |
| 4789 | ALOGE("Setting result to 'PENDING' for asynchronous injection"); |
| 4790 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4791 | } |
| 4792 | } |
| 4793 | |
| 4794 | injectionState->injectionResult = injectionResult; |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4795 | mInjectionResultAvailable.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4796 | } |
| 4797 | } |
| 4798 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4799 | void InputDispatcher::transformMotionEntryForInjectionLocked( |
| 4800 | MotionEntry& entry, const ui::Transform& injectedTransform) const { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4801 | // Input injection works in the logical display coordinate space, but the input pipeline works |
| 4802 | // display space, so we need to transform the injected events accordingly. |
| 4803 | const auto it = mDisplayInfos.find(entry.displayId); |
| 4804 | if (it == mDisplayInfos.end()) return; |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4805 | const auto& transformToDisplay = it->second.transform.inverse() * injectedTransform; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4806 | |
Prabir Pradhan | d9a2ebe | 2022-07-20 19:25:13 +0000 | [diff] [blame] | 4807 | if (entry.xCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION && |
| 4808 | entry.yCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) { |
| 4809 | const vec2 cursor = |
| 4810 | MotionEvent::calculateTransformedXY(entry.source, transformToDisplay, |
| 4811 | {entry.xCursorPosition, entry.yCursorPosition}); |
| 4812 | entry.xCursorPosition = cursor.x; |
| 4813 | entry.yCursorPosition = cursor.y; |
| 4814 | } |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4815 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
Prabir Pradhan | 8e6ce22 | 2022-02-24 09:08:54 -0800 | [diff] [blame] | 4816 | entry.pointerCoords[i] = |
| 4817 | MotionEvent::calculateTransformedCoords(entry.source, transformToDisplay, |
| 4818 | entry.pointerCoords[i]); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4819 | } |
| 4820 | } |
| 4821 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4822 | void InputDispatcher::incrementPendingForegroundDispatches(EventEntry& entry) { |
| 4823 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4824 | if (injectionState) { |
| 4825 | injectionState->pendingForegroundDispatches += 1; |
| 4826 | } |
| 4827 | } |
| 4828 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4829 | void InputDispatcher::decrementPendingForegroundDispatches(EventEntry& entry) { |
| 4830 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4831 | if (injectionState) { |
| 4832 | injectionState->pendingForegroundDispatches -= 1; |
| 4833 | |
| 4834 | if (injectionState->pendingForegroundDispatches == 0) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4835 | mInjectionSyncFinished.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4836 | } |
| 4837 | } |
| 4838 | } |
| 4839 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4840 | const std::vector<sp<WindowInfoHandle>>& InputDispatcher::getWindowHandlesLocked( |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4841 | int32_t displayId) const { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4842 | static const std::vector<sp<WindowInfoHandle>> EMPTY_WINDOW_HANDLES; |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4843 | auto it = mWindowHandlesByDisplay.find(displayId); |
| 4844 | return it != mWindowHandlesByDisplay.end() ? it->second : EMPTY_WINDOW_HANDLES; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4845 | } |
| 4846 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4847 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4848 | const sp<IBinder>& windowHandleToken) const { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4849 | if (windowHandleToken == nullptr) { |
| 4850 | return nullptr; |
| 4851 | } |
| 4852 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4853 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4854 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 4855 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4856 | if (windowHandle->getToken() == windowHandleToken) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4857 | return windowHandle; |
| 4858 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4859 | } |
| 4860 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4861 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4862 | } |
| 4863 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4864 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked(const sp<IBinder>& windowHandleToken, |
| 4865 | int displayId) const { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4866 | if (windowHandleToken == nullptr) { |
| 4867 | return nullptr; |
| 4868 | } |
| 4869 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4870 | for (const sp<WindowInfoHandle>& windowHandle : getWindowHandlesLocked(displayId)) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4871 | if (windowHandle->getToken() == windowHandleToken) { |
| 4872 | return windowHandle; |
| 4873 | } |
| 4874 | } |
| 4875 | return nullptr; |
| 4876 | } |
| 4877 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4878 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
| 4879 | const sp<WindowInfoHandle>& windowHandle) const { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4880 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4881 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 4882 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4883 | if (handle->getId() == windowHandle->getId() && |
| 4884 | handle->getToken() == windowHandle->getToken()) { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4885 | if (windowHandle->getInfo()->displayId != it.first) { |
| 4886 | ALOGE("Found window %s in display %" PRId32 |
| 4887 | ", but it should belong to display %" PRId32, |
| 4888 | windowHandle->getName().c_str(), it.first, |
| 4889 | windowHandle->getInfo()->displayId); |
| 4890 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4891 | return handle; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4892 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4893 | } |
| 4894 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4895 | return nullptr; |
| 4896 | } |
| 4897 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4898 | sp<WindowInfoHandle> InputDispatcher::getFocusedWindowHandleLocked(int displayId) const { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4899 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(displayId); |
| 4900 | return getWindowHandleLocked(focusedToken, displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4901 | } |
| 4902 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4903 | ui::Transform InputDispatcher::getTransformLocked(int32_t displayId) const { |
| 4904 | auto displayInfoIt = mDisplayInfos.find(displayId); |
| 4905 | return displayInfoIt != mDisplayInfos.end() ? displayInfoIt->second.transform |
| 4906 | : kIdentityTransform; |
| 4907 | } |
| 4908 | |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 4909 | bool InputDispatcher::canWindowReceiveMotionLocked(const sp<WindowInfoHandle>& window, |
| 4910 | const MotionEntry& motionEntry) const { |
| 4911 | const WindowInfo& info = *window->getInfo(); |
| 4912 | |
| 4913 | // Skip spy window targets that are not valid for targeted injection. |
| 4914 | if (const auto err = verifyTargetedInjection(window, motionEntry); err) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4915 | return false; |
| 4916 | } |
| 4917 | |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 4918 | if (info.inputConfig.test(WindowInfo::InputConfig::PAUSE_DISPATCHING)) { |
| 4919 | ALOGI("Not sending touch event to %s because it is paused", window->getName().c_str()); |
| 4920 | return false; |
| 4921 | } |
| 4922 | |
| 4923 | if (info.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) { |
| 4924 | ALOGW("Not sending touch gesture to %s because it has config NO_INPUT_CHANNEL", |
| 4925 | window->getName().c_str()); |
| 4926 | return false; |
| 4927 | } |
| 4928 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 4929 | std::shared_ptr<Connection> connection = getConnectionLocked(window->getToken()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4930 | if (connection == nullptr) { |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 4931 | ALOGW("Not sending touch to %s because there's no corresponding connection", |
| 4932 | window->getName().c_str()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4933 | return false; |
| 4934 | } |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 4935 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4936 | if (!connection->responsive) { |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 4937 | ALOGW("Not sending touch to %s because it is not responsive", window->getName().c_str()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4938 | return false; |
| 4939 | } |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 4940 | |
| 4941 | // Drop events that can't be trusted due to occlusion |
| 4942 | const auto [x, y] = resolveTouchedPosition(motionEntry); |
| 4943 | TouchOcclusionInfo occlusionInfo = computeTouchOcclusionInfoLocked(window, x, y); |
| 4944 | if (!isTouchTrustedLocked(occlusionInfo)) { |
| 4945 | if (DEBUG_TOUCH_OCCLUSION) { |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 4946 | ALOGD("Stack of obscuring windows during untrusted touch (%.1f, %.1f):", x, y); |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 4947 | for (const auto& log : occlusionInfo.debugInfo) { |
| 4948 | ALOGD("%s", log.c_str()); |
| 4949 | } |
| 4950 | } |
| 4951 | ALOGW("Dropping untrusted touch event due to %s/%d", occlusionInfo.obscuringPackage.c_str(), |
| 4952 | occlusionInfo.obscuringUid); |
| 4953 | return false; |
| 4954 | } |
| 4955 | |
| 4956 | // Drop touch events if requested by input feature |
| 4957 | if (shouldDropInput(motionEntry, window)) { |
| 4958 | return false; |
| 4959 | } |
| 4960 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4961 | return true; |
| 4962 | } |
| 4963 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4964 | std::shared_ptr<InputChannel> InputDispatcher::getInputChannelLocked( |
| 4965 | const sp<IBinder>& token) const { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4966 | auto connectionIt = mConnectionsByToken.find(token); |
| 4967 | if (connectionIt == mConnectionsByToken.end()) { |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4968 | return nullptr; |
| 4969 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4970 | return connectionIt->second->inputChannel; |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4971 | } |
| 4972 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4973 | void InputDispatcher::updateWindowHandlesForDisplayLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4974 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
| 4975 | if (windowInfoHandles.empty()) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4976 | // Remove all handles on a display if there are no windows left. |
| 4977 | mWindowHandlesByDisplay.erase(displayId); |
| 4978 | return; |
| 4979 | } |
| 4980 | |
| 4981 | // Since we compare the pointer of input window handles across window updates, we need |
| 4982 | // 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] | 4983 | const std::vector<sp<WindowInfoHandle>>& oldHandles = getWindowHandlesLocked(displayId); |
| 4984 | std::unordered_map<int32_t /*id*/, sp<WindowInfoHandle>> oldHandlesById; |
| 4985 | for (const sp<WindowInfoHandle>& handle : oldHandles) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4986 | oldHandlesById[handle->getId()] = handle; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4987 | } |
| 4988 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4989 | std::vector<sp<WindowInfoHandle>> newHandles; |
| 4990 | for (const sp<WindowInfoHandle>& handle : windowInfoHandles) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4991 | const WindowInfo* info = handle->getInfo(); |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 4992 | if (getInputChannelLocked(handle->getToken()) == nullptr) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4993 | const bool noInputChannel = |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 4994 | info->inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4995 | const bool canReceiveInput = |
| 4996 | !info->inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE) || |
| 4997 | !info->inputConfig.test(WindowInfo::InputConfig::NOT_FOCUSABLE); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4998 | if (canReceiveInput && !noInputChannel) { |
John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 4999 | ALOGV("Window handle %s has no registered input channel", |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5000 | handle->getName().c_str()); |
Robert Carr | 2984b7a | 2020-04-13 17:06:45 -0700 | [diff] [blame] | 5001 | continue; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5002 | } |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5003 | } |
| 5004 | |
| 5005 | if (info->displayId != displayId) { |
| 5006 | ALOGE("Window %s updated by wrong display %d, should belong to display %d", |
| 5007 | handle->getName().c_str(), displayId, info->displayId); |
| 5008 | continue; |
| 5009 | } |
| 5010 | |
Robert Carr | edd1360 | 2020-04-13 17:24:34 -0700 | [diff] [blame] | 5011 | if ((oldHandlesById.find(handle->getId()) != oldHandlesById.end()) && |
| 5012 | (oldHandlesById.at(handle->getId())->getToken() == handle->getToken())) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5013 | const sp<WindowInfoHandle>& oldHandle = oldHandlesById.at(handle->getId()); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5014 | oldHandle->updateFrom(handle); |
| 5015 | newHandles.push_back(oldHandle); |
| 5016 | } else { |
| 5017 | newHandles.push_back(handle); |
| 5018 | } |
| 5019 | } |
| 5020 | |
| 5021 | // Insert or replace |
| 5022 | mWindowHandlesByDisplay[displayId] = newHandles; |
| 5023 | } |
| 5024 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5025 | void InputDispatcher::setInputWindows( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5026 | const std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>>& handlesPerDisplay) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5027 | // TODO(b/198444055): Remove setInputWindows from InputDispatcher. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5028 | { // acquire lock |
| 5029 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 5030 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 5031 | setInputWindowsLocked(handles, displayId); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5032 | } |
| 5033 | } |
| 5034 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5035 | mLooper->wake(); |
| 5036 | } |
| 5037 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5038 | /** |
| 5039 | * Called from InputManagerService, update window handle list by displayId that can receive input. |
| 5040 | * A window handle contains information about InputChannel, Touch Region, Types, Focused,... |
| 5041 | * If set an empty list, remove all handles from the specific display. |
| 5042 | * For focused handle, check if need to change and send a cancel event to previous one. |
| 5043 | * For removed handle, check if need to send a cancel event if already in touch. |
| 5044 | */ |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5045 | void InputDispatcher::setInputWindowsLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5046 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5047 | if (DEBUG_FOCUS) { |
| 5048 | std::string windowList; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5049 | for (const sp<WindowInfoHandle>& iwh : windowInfoHandles) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5050 | windowList += iwh->getName() + " "; |
| 5051 | } |
| 5052 | ALOGD("setInputWindows displayId=%" PRId32 " %s", displayId, windowList.c_str()); |
| 5053 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5054 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 5055 | // Check preconditions for new input windows |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5056 | for (const sp<WindowInfoHandle>& window : windowInfoHandles) { |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 5057 | const WindowInfo& info = *window->getInfo(); |
| 5058 | |
| 5059 | // Ensure all tokens are null if the window has feature NO_INPUT_CHANNEL |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 5060 | const bool noInputWindow = info.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5061 | if (noInputWindow && window->getToken() != nullptr) { |
| 5062 | ALOGE("%s has feature NO_INPUT_WINDOW, but a non-null token. Clearing", |
| 5063 | window->getName().c_str()); |
| 5064 | window->releaseChannel(); |
| 5065 | } |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 5066 | |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 5067 | // Ensure all spy windows are trusted overlays |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5068 | LOG_ALWAYS_FATAL_IF(info.isSpy() && |
| 5069 | !info.inputConfig.test( |
| 5070 | WindowInfo::InputConfig::TRUSTED_OVERLAY), |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 5071 | "%s has feature SPY, but is not a trusted overlay.", |
| 5072 | window->getName().c_str()); |
| 5073 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 5074 | // Ensure all stylus interceptors are trusted overlays |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5075 | LOG_ALWAYS_FATAL_IF(info.interceptsStylus() && |
| 5076 | !info.inputConfig.test( |
| 5077 | WindowInfo::InputConfig::TRUSTED_OVERLAY), |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 5078 | "%s has feature INTERCEPTS_STYLUS, but is not a trusted overlay.", |
| 5079 | window->getName().c_str()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5080 | } |
| 5081 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5082 | // Copy old handles for release if they are no longer present. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5083 | const std::vector<sp<WindowInfoHandle>> oldWindowHandles = getWindowHandlesLocked(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5084 | |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 5085 | // Save the old windows' orientation by ID before it gets updated. |
| 5086 | std::unordered_map<int32_t, uint32_t> oldWindowOrientations; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5087 | for (const sp<WindowInfoHandle>& handle : oldWindowHandles) { |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 5088 | oldWindowOrientations.emplace(handle->getId(), |
| 5089 | handle->getInfo()->transform.getOrientation()); |
| 5090 | } |
| 5091 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5092 | updateWindowHandlesForDisplayLocked(windowInfoHandles, displayId); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5093 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5094 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5095 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5096 | std::optional<FocusResolver::FocusChanges> changes = |
| 5097 | mFocusResolver.setInputWindows(displayId, windowHandles); |
| 5098 | if (changes) { |
| 5099 | onFocusChangedLocked(*changes); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5100 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5101 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5102 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 5103 | mTouchStatesByDisplay.find(displayId); |
| 5104 | if (stateIt != mTouchStatesByDisplay.end()) { |
| 5105 | TouchState& state = stateIt->second; |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5106 | for (size_t i = 0; i < state.windows.size();) { |
| 5107 | TouchedWindow& touchedWindow = state.windows[i]; |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 5108 | if (getWindowHandleLocked(touchedWindow.windowHandle) == nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5109 | if (DEBUG_FOCUS) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5110 | ALOGD("Touched window was removed: %s in display %" PRId32, |
| 5111 | touchedWindow.windowHandle->getName().c_str(), displayId); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5112 | } |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5113 | std::shared_ptr<InputChannel> touchedInputChannel = |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5114 | getInputChannelLocked(touchedWindow.windowHandle->getToken()); |
| 5115 | if (touchedInputChannel != nullptr) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 5116 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5117 | "touched window was removed"); |
| 5118 | synthesizeCancelationEventsForInputChannelLocked(touchedInputChannel, options); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5119 | // Since we are about to drop the touch, cancel the events for the wallpaper as |
| 5120 | // well. |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5121 | if (touchedWindow.targetFlags.test(InputTarget::Flags::FOREGROUND) && |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5122 | touchedWindow.windowHandle->getInfo()->inputConfig.test( |
| 5123 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER)) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5124 | sp<WindowInfoHandle> wallpaper = state.getWallpaperWindow(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5125 | synthesizeCancelationEventsForWindowLocked(wallpaper, options); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5126 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5127 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5128 | state.windows.erase(state.windows.begin() + i); |
| 5129 | } else { |
| 5130 | ++i; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5131 | } |
| 5132 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5133 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5134 | // 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] | 5135 | // could just clear the state here. |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 5136 | if (mDragState && mDragState->dragWindow->getInfo()->displayId == displayId && |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5137 | std::find(windowHandles.begin(), windowHandles.end(), mDragState->dragWindow) == |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5138 | windowHandles.end()) { |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 5139 | ALOGI("Drag window went away: %s", mDragState->dragWindow->getName().c_str()); |
| 5140 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5141 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5142 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5143 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 5144 | |
Prabir Pradhan | 8b89c2f | 2021-07-29 16:30:14 +0000 | [diff] [blame] | 5145 | // Determine if the orientation of any of the input windows have changed, and cancel all |
| 5146 | // pointer events if necessary. |
| 5147 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
| 5148 | const sp<WindowInfoHandle> newWindowHandle = getWindowHandleLocked(oldWindowHandle); |
| 5149 | if (newWindowHandle != nullptr && |
| 5150 | newWindowHandle->getInfo()->transform.getOrientation() != |
| 5151 | oldWindowOrientations[oldWindowHandle->getId()]) { |
| 5152 | std::shared_ptr<InputChannel> inputChannel = |
| 5153 | getInputChannelLocked(newWindowHandle->getToken()); |
| 5154 | if (inputChannel != nullptr) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 5155 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
Prabir Pradhan | 8b89c2f | 2021-07-29 16:30:14 +0000 | [diff] [blame] | 5156 | "touched window's orientation changed"); |
| 5157 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 5158 | } |
| 5159 | } |
| 5160 | } |
| 5161 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5162 | // Release information for windows that are no longer present. |
| 5163 | // This ensures that unused input channels are released promptly. |
| 5164 | // Otherwise, they might stick around until the window handle is destroyed |
| 5165 | // which might not happen until the next GC. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5166 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 5167 | if (getWindowHandleLocked(oldWindowHandle) == nullptr) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5168 | if (DEBUG_FOCUS) { |
| 5169 | ALOGD("Window went away: %s", oldWindowHandle->getName().c_str()); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 5170 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5171 | oldWindowHandle->releaseChannel(); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 5172 | } |
chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 5173 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5174 | } |
| 5175 | |
| 5176 | void InputDispatcher::setFocusedApplication( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5177 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5178 | if (DEBUG_FOCUS) { |
| 5179 | ALOGD("setFocusedApplication displayId=%" PRId32 " %s", displayId, |
| 5180 | inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>"); |
| 5181 | } |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 5182 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5183 | std::scoped_lock _l(mLock); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 5184 | setFocusedApplicationLocked(displayId, inputApplicationHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5185 | } // release lock |
| 5186 | |
| 5187 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5188 | mLooper->wake(); |
| 5189 | } |
| 5190 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 5191 | void InputDispatcher::setFocusedApplicationLocked( |
| 5192 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
| 5193 | std::shared_ptr<InputApplicationHandle> oldFocusedApplicationHandle = |
| 5194 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 5195 | |
| 5196 | if (sharedPointersEqual(oldFocusedApplicationHandle, inputApplicationHandle)) { |
| 5197 | return; // This application is already focused. No need to wake up or change anything. |
| 5198 | } |
| 5199 | |
| 5200 | // Set the new application handle. |
| 5201 | if (inputApplicationHandle != nullptr) { |
| 5202 | mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle; |
| 5203 | } else { |
| 5204 | mFocusedApplicationHandlesByDisplay.erase(displayId); |
| 5205 | } |
| 5206 | |
| 5207 | // No matter what the old focused application was, stop waiting on it because it is |
| 5208 | // no longer focused. |
| 5209 | resetNoFocusedWindowTimeoutLocked(); |
| 5210 | } |
| 5211 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5212 | /** |
| 5213 | * Sets the focused display, which is responsible for receiving focus-dispatched input events where |
| 5214 | * the display not specified. |
| 5215 | * |
| 5216 | * We track any unreleased events for each window. If a window loses the ability to receive the |
| 5217 | * released event, we will send a cancel event to it. So when the focused display is changed, we |
| 5218 | * cancel all the unreleased display-unspecified events for the focused window on the old focused |
| 5219 | * display. The display-specified events won't be affected. |
| 5220 | */ |
| 5221 | void InputDispatcher::setFocusedDisplay(int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5222 | if (DEBUG_FOCUS) { |
| 5223 | ALOGD("setFocusedDisplay displayId=%" PRId32, displayId); |
| 5224 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5225 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5226 | std::scoped_lock _l(mLock); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5227 | |
| 5228 | if (mFocusedDisplayId != displayId) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5229 | sp<IBinder> oldFocusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5230 | mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5231 | if (oldFocusedWindowToken != nullptr) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5232 | std::shared_ptr<InputChannel> inputChannel = |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5233 | getInputChannelLocked(oldFocusedWindowToken); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5234 | if (inputChannel != nullptr) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5235 | CancelationOptions |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 5236 | options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5237 | "The display which contains this window no longer has focus."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5238 | options.displayId = ADISPLAY_ID_NONE; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5239 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
| 5240 | } |
| 5241 | } |
| 5242 | mFocusedDisplayId = displayId; |
| 5243 | |
Chris Ye | 3c2d6f5 | 2020-08-09 10:39:48 -0700 | [diff] [blame] | 5244 | // Find new focused window and validate |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5245 | sp<IBinder> newFocusedWindowToken = mFocusResolver.getFocusedWindowToken(displayId); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5246 | sendFocusChangedCommandLocked(oldFocusedWindowToken, newFocusedWindowToken); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5247 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5248 | if (newFocusedWindowToken == nullptr) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5249 | ALOGW("Focused display #%" PRId32 " does not have a focused window.", displayId); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5250 | if (mFocusResolver.hasFocusedWindowTokens()) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5251 | ALOGE("But another display has a focused window\n%s", |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5252 | mFocusResolver.dumpFocusedWindows().c_str()); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5253 | } |
| 5254 | } |
| 5255 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5256 | } // release lock |
| 5257 | |
| 5258 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5259 | mLooper->wake(); |
| 5260 | } |
| 5261 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5262 | void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5263 | if (DEBUG_FOCUS) { |
| 5264 | ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); |
| 5265 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5266 | |
| 5267 | bool changed; |
| 5268 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5269 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5270 | |
| 5271 | if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { |
| 5272 | if (mDispatchFrozen && !frozen) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5273 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5274 | } |
| 5275 | |
| 5276 | if (mDispatchEnabled && !enabled) { |
| 5277 | resetAndDropEverythingLocked("dispatcher is being disabled"); |
| 5278 | } |
| 5279 | |
| 5280 | mDispatchEnabled = enabled; |
| 5281 | mDispatchFrozen = frozen; |
| 5282 | changed = true; |
| 5283 | } else { |
| 5284 | changed = false; |
| 5285 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5286 | } // release lock |
| 5287 | |
| 5288 | if (changed) { |
| 5289 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5290 | mLooper->wake(); |
| 5291 | } |
| 5292 | } |
| 5293 | |
| 5294 | void InputDispatcher::setInputFilterEnabled(bool enabled) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5295 | if (DEBUG_FOCUS) { |
| 5296 | ALOGD("setInputFilterEnabled: enabled=%d", enabled); |
| 5297 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5298 | |
| 5299 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5300 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5301 | |
| 5302 | if (mInputFilterEnabled == enabled) { |
| 5303 | return; |
| 5304 | } |
| 5305 | |
| 5306 | mInputFilterEnabled = enabled; |
| 5307 | resetAndDropEverythingLocked("input filter is being enabled or disabled"); |
| 5308 | } // release lock |
| 5309 | |
| 5310 | // Wake up poll loop since there might be work to do to drop everything. |
| 5311 | mLooper->wake(); |
| 5312 | } |
| 5313 | |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 5314 | bool InputDispatcher::setInTouchMode(bool inTouchMode, int32_t pid, int32_t uid, bool hasPermission, |
| 5315 | int32_t displayId) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5316 | bool needWake = false; |
| 5317 | { |
| 5318 | std::scoped_lock lock(mLock); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5319 | ALOGD_IF(DEBUG_TOUCH_MODE, |
| 5320 | "Request to change touch mode to %s (calling pid=%d, uid=%d, " |
| 5321 | "hasPermission=%s, target displayId=%d, mTouchModePerDisplay[displayId]=%s)", |
| 5322 | toString(inTouchMode), pid, uid, toString(hasPermission), displayId, |
| 5323 | mTouchModePerDisplay.count(displayId) == 0 |
| 5324 | ? "not set" |
| 5325 | : std::to_string(mTouchModePerDisplay[displayId]).c_str()); |
| 5326 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5327 | auto touchModeIt = mTouchModePerDisplay.find(displayId); |
| 5328 | if (touchModeIt != mTouchModePerDisplay.end() && touchModeIt->second == inTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5329 | return false; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5330 | } |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5331 | if (!hasPermission) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 5332 | if (!focusedWindowIsOwnedByLocked(pid, uid) && |
| 5333 | !recentWindowsAreOwnedByLocked(pid, uid)) { |
| 5334 | ALOGD("Touch mode switch rejected, caller (pid=%d, uid=%d) doesn't own the focused " |
| 5335 | "window nor none of the previously interacted window", |
| 5336 | pid, uid); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5337 | return false; |
| 5338 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5339 | } |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5340 | mTouchModePerDisplay[displayId] = inTouchMode; |
| 5341 | auto entry = std::make_unique<TouchModeEntry>(mIdGenerator.nextId(), now(), inTouchMode, |
| 5342 | displayId); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5343 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 5344 | } // release lock |
| 5345 | |
| 5346 | if (needWake) { |
| 5347 | mLooper->wake(); |
| 5348 | } |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5349 | return true; |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 5350 | } |
| 5351 | |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 5352 | bool InputDispatcher::focusedWindowIsOwnedByLocked(int32_t pid, int32_t uid) { |
| 5353 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
| 5354 | if (focusedToken == nullptr) { |
| 5355 | return false; |
| 5356 | } |
| 5357 | sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(focusedToken); |
| 5358 | return isWindowOwnedBy(windowHandle, pid, uid); |
| 5359 | } |
| 5360 | |
| 5361 | bool InputDispatcher::recentWindowsAreOwnedByLocked(int32_t pid, int32_t uid) { |
| 5362 | return std::find_if(mInteractionConnectionTokens.begin(), mInteractionConnectionTokens.end(), |
| 5363 | [&](const sp<IBinder>& connectionToken) REQUIRES(mLock) { |
| 5364 | const sp<WindowInfoHandle> windowHandle = |
| 5365 | getWindowHandleLocked(connectionToken); |
| 5366 | return isWindowOwnedBy(windowHandle, pid, uid); |
| 5367 | }) != mInteractionConnectionTokens.end(); |
| 5368 | } |
| 5369 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 5370 | void InputDispatcher::setMaximumObscuringOpacityForTouch(float opacity) { |
| 5371 | if (opacity < 0 || opacity > 1) { |
| 5372 | LOG_ALWAYS_FATAL("Maximum obscuring opacity for touch should be >= 0 and <= 1"); |
| 5373 | return; |
| 5374 | } |
| 5375 | |
| 5376 | std::scoped_lock lock(mLock); |
| 5377 | mMaximumObscuringOpacityForTouch = opacity; |
| 5378 | } |
| 5379 | |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5380 | std::tuple<TouchState*, TouchedWindow*, int32_t /*displayId*/> |
| 5381 | InputDispatcher::findTouchStateWindowAndDisplayLocked(const sp<IBinder>& token) { |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5382 | for (auto& [displayId, state] : mTouchStatesByDisplay) { |
| 5383 | for (TouchedWindow& w : state.windows) { |
| 5384 | if (w.windowHandle->getToken() == token) { |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5385 | return std::make_tuple(&state, &w, displayId); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5386 | } |
| 5387 | } |
| 5388 | } |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5389 | return std::make_tuple(nullptr, nullptr, ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5390 | } |
| 5391 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5392 | bool InputDispatcher::transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken, |
| 5393 | bool isDragDrop) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 5394 | if (fromToken == toToken) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5395 | if (DEBUG_FOCUS) { |
| 5396 | ALOGD("Trivial transfer to same window."); |
| 5397 | } |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 5398 | return true; |
| 5399 | } |
| 5400 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5401 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5402 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5403 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5404 | // Find the target touch state and touched window by fromToken. |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5405 | auto [state, touchedWindow, displayId] = findTouchStateWindowAndDisplayLocked(fromToken); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5406 | if (state == nullptr || touchedWindow == nullptr) { |
| 5407 | ALOGD("Focus transfer failed because from window is not being touched."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5408 | return false; |
| 5409 | } |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5410 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5411 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(toToken, displayId); |
| 5412 | if (toWindowHandle == nullptr) { |
| 5413 | ALOGW("Cannot transfer focus because to window not found."); |
| 5414 | return false; |
| 5415 | } |
| 5416 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5417 | if (DEBUG_FOCUS) { |
| 5418 | ALOGD("transferTouchFocus: fromWindowHandle=%s, toWindowHandle=%s", |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5419 | touchedWindow->windowHandle->getName().c_str(), |
| 5420 | toWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5421 | } |
| 5422 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5423 | // Erase old window. |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5424 | ftl::Flags<InputTarget::Flags> oldTargetFlags = touchedWindow->targetFlags; |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 5425 | std::bitset<MAX_POINTER_ID + 1> pointerIds = touchedWindow->pointerIds; |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5426 | sp<WindowInfoHandle> fromWindowHandle = touchedWindow->windowHandle; |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5427 | state->removeWindowByToken(fromToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5428 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5429 | // Add new window. |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 5430 | nsecs_t downTimeInTarget = now(); |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5431 | ftl::Flags<InputTarget::Flags> newTargetFlags = |
| 5432 | oldTargetFlags & (InputTarget::Flags::SPLIT | InputTarget::Flags::DISPATCH_AS_IS); |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 5433 | if (canReceiveForegroundTouches(*toWindowHandle->getInfo())) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5434 | newTargetFlags |= InputTarget::Flags::FOREGROUND; |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 5435 | } |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 5436 | state->addOrUpdateWindow(toWindowHandle, newTargetFlags, pointerIds, downTimeInTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5437 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5438 | // Store the dragging window. |
| 5439 | if (isDragDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 5440 | if (pointerIds.count() != 1) { |
| 5441 | ALOGW("The drag and drop cannot be started when there is no pointer or more than 1" |
| 5442 | " pointer on the window."); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 5443 | return false; |
| 5444 | } |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 5445 | // Track the pointer id for drag window and generate the drag state. |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 5446 | const size_t id = firstMarkedBit(pointerIds); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 5447 | mDragState = std::make_unique<DragState>(toWindowHandle, id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5448 | } |
| 5449 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5450 | // Synthesize cancel for old window and down for new window. |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 5451 | std::shared_ptr<Connection> fromConnection = getConnectionLocked(fromToken); |
| 5452 | std::shared_ptr<Connection> toConnection = getConnectionLocked(toToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5453 | if (fromConnection != nullptr && toConnection != nullptr) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5454 | fromConnection->inputState.mergePointerStateTo(toConnection->inputState); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5455 | CancelationOptions |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 5456 | options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5457 | "transferring touch focus from this window to another window"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5458 | synthesizeCancelationEventsForConnectionLocked(fromConnection, options); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5459 | synthesizePointerDownEventsForConnectionLocked(downTimeInTarget, toConnection, |
| 5460 | newTargetFlags); |
| 5461 | |
| 5462 | // Check if the wallpaper window should deliver the corresponding event. |
| 5463 | transferWallpaperTouch(oldTargetFlags, newTargetFlags, fromWindowHandle, toWindowHandle, |
| 5464 | *state, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5465 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5466 | } // release lock |
| 5467 | |
| 5468 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5469 | mLooper->wake(); |
| 5470 | return true; |
| 5471 | } |
| 5472 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5473 | /** |
| 5474 | * Get the touched foreground window on the given display. |
| 5475 | * Return null if there are no windows touched on that display, or if more than one foreground |
| 5476 | * window is being touched. |
| 5477 | */ |
| 5478 | sp<WindowInfoHandle> InputDispatcher::findTouchedForegroundWindowLocked(int32_t displayId) const { |
| 5479 | auto stateIt = mTouchStatesByDisplay.find(displayId); |
| 5480 | if (stateIt == mTouchStatesByDisplay.end()) { |
| 5481 | ALOGI("No touch state on display %" PRId32, displayId); |
| 5482 | return nullptr; |
| 5483 | } |
| 5484 | |
| 5485 | const TouchState& state = stateIt->second; |
| 5486 | sp<WindowInfoHandle> touchedForegroundWindow; |
| 5487 | // If multiple foreground windows are touched, return nullptr |
| 5488 | for (const TouchedWindow& window : state.windows) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5489 | if (window.targetFlags.test(InputTarget::Flags::FOREGROUND)) { |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5490 | if (touchedForegroundWindow != nullptr) { |
| 5491 | ALOGI("Two or more foreground windows: %s and %s", |
| 5492 | touchedForegroundWindow->getName().c_str(), |
| 5493 | window.windowHandle->getName().c_str()); |
| 5494 | return nullptr; |
| 5495 | } |
| 5496 | touchedForegroundWindow = window.windowHandle; |
| 5497 | } |
| 5498 | } |
| 5499 | return touchedForegroundWindow; |
| 5500 | } |
| 5501 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5502 | // Binder call |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5503 | bool InputDispatcher::transferTouch(const sp<IBinder>& destChannelToken, int32_t displayId) { |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5504 | sp<IBinder> fromToken; |
| 5505 | { // acquire lock |
| 5506 | std::scoped_lock _l(mLock); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5507 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(destChannelToken, displayId); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5508 | if (toWindowHandle == nullptr) { |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5509 | ALOGW("Could not find window associated with token=%p on display %" PRId32, |
| 5510 | destChannelToken.get(), displayId); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5511 | return false; |
| 5512 | } |
| 5513 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5514 | sp<WindowInfoHandle> from = findTouchedForegroundWindowLocked(displayId); |
| 5515 | if (from == nullptr) { |
| 5516 | ALOGE("Could not find a source window in %s for %p", __func__, destChannelToken.get()); |
| 5517 | return false; |
| 5518 | } |
| 5519 | |
| 5520 | fromToken = from->getToken(); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5521 | } // release lock |
| 5522 | |
| 5523 | return transferTouchFocus(fromToken, destChannelToken); |
| 5524 | } |
| 5525 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5526 | void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5527 | if (DEBUG_FOCUS) { |
| 5528 | ALOGD("Resetting and dropping all events (%s).", reason); |
| 5529 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5530 | |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 5531 | CancelationOptions options(CancelationOptions::Mode::CANCEL_ALL_EVENTS, reason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5532 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 5533 | |
| 5534 | resetKeyRepeatLocked(); |
| 5535 | releasePendingEventLocked(); |
| 5536 | drainInboundQueueLocked(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5537 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5538 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5539 | mAnrTracker.clear(); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5540 | mTouchStatesByDisplay.clear(); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5541 | mReplacedKeys.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5542 | } |
| 5543 | |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 5544 | void InputDispatcher::logDispatchStateLocked() const { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5545 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5546 | dumpDispatchStateLocked(dump); |
| 5547 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5548 | std::istringstream stream(dump); |
| 5549 | std::string line; |
| 5550 | |
| 5551 | while (std::getline(stream, line, '\n')) { |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 5552 | ALOGI("%s", line.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5553 | } |
| 5554 | } |
| 5555 | |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 5556 | std::string InputDispatcher::dumpPointerCaptureStateLocked() const { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5557 | std::string dump; |
| 5558 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5559 | dump += StringPrintf(INDENT "Pointer Capture Requested: %s\n", |
| 5560 | toString(mCurrentPointerCaptureRequest.enable)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5561 | |
| 5562 | std::string windowName = "None"; |
| 5563 | if (mWindowTokenWithPointerCapture) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5564 | const sp<WindowInfoHandle> captureWindowHandle = |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5565 | getWindowHandleLocked(mWindowTokenWithPointerCapture); |
| 5566 | windowName = captureWindowHandle ? captureWindowHandle->getName().c_str() |
| 5567 | : "token has capture without window"; |
| 5568 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5569 | 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] | 5570 | |
| 5571 | return dump; |
| 5572 | } |
| 5573 | |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 5574 | void InputDispatcher::dumpDispatchStateLocked(std::string& dump) const { |
Siarhei Vishniakou | 043a3ec | 2019-05-01 11:30:46 -0700 | [diff] [blame] | 5575 | dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled)); |
| 5576 | dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen)); |
| 5577 | dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5578 | dump += StringPrintf(INDENT "FocusedDisplayId: %" PRId32 "\n", mFocusedDisplayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5579 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5580 | if (!mFocusedApplicationHandlesByDisplay.empty()) { |
| 5581 | dump += StringPrintf(INDENT "FocusedApplications:\n"); |
| 5582 | for (auto& it : mFocusedApplicationHandlesByDisplay) { |
| 5583 | const int32_t displayId = it.first; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5584 | const std::shared_ptr<InputApplicationHandle>& applicationHandle = it.second; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5585 | const std::chrono::duration timeout = |
| 5586 | applicationHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5587 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5588 | ", name='%s', dispatchingTimeout=%" PRId64 "ms\n", |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5589 | displayId, applicationHandle->getName().c_str(), millis(timeout)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5590 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5591 | } else { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5592 | dump += StringPrintf(INDENT "FocusedApplications: <none>\n"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5593 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5594 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5595 | dump += mFocusResolver.dump(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5596 | dump += dumpPointerCaptureStateLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5597 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5598 | if (!mTouchStatesByDisplay.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5599 | dump += StringPrintf(INDENT "TouchStatesByDisplay:\n"); |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5600 | for (const auto& [displayId, state] : mTouchStatesByDisplay) { |
Siarhei Vishniakou | 6e1e987 | 2022-11-08 17:51:35 -0800 | [diff] [blame] | 5601 | std::string touchStateDump = addLinePrefix(state.dump(), INDENT2); |
| 5602 | dump += INDENT2 + std::to_string(displayId) + " : " + touchStateDump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5603 | } |
| 5604 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5605 | dump += INDENT "TouchStates: <no displays touched>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5606 | } |
| 5607 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5608 | if (mDragState) { |
| 5609 | dump += StringPrintf(INDENT "DragState:\n"); |
| 5610 | mDragState->dump(dump, INDENT2); |
| 5611 | } |
| 5612 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5613 | if (!mWindowHandlesByDisplay.empty()) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5614 | for (const auto& [displayId, windowHandles] : mWindowHandlesByDisplay) { |
| 5615 | dump += StringPrintf(INDENT "Display: %" PRId32 "\n", displayId); |
| 5616 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
| 5617 | const auto& displayInfo = it->second; |
| 5618 | dump += StringPrintf(INDENT2 "logicalSize=%dx%d\n", displayInfo.logicalWidth, |
| 5619 | displayInfo.logicalHeight); |
| 5620 | displayInfo.transform.dump(dump, "transform", INDENT4); |
| 5621 | } else { |
| 5622 | dump += INDENT2 "No DisplayInfo found!\n"; |
| 5623 | } |
| 5624 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5625 | if (!windowHandles.empty()) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5626 | dump += INDENT2 "Windows:\n"; |
| 5627 | for (size_t i = 0; i < windowHandles.size(); i++) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5628 | const sp<WindowInfoHandle>& windowHandle = windowHandles[i]; |
| 5629 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5630 | |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5631 | dump += StringPrintf(INDENT3 "%zu: name='%s', id=%" PRId32 ", displayId=%d, " |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5632 | "inputConfig=%s, alpha=%.2f, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5633 | "frame=[%d,%d][%d,%d], globalScale=%f, " |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5634 | "applicationInfo.name=%s, " |
| 5635 | "applicationInfo.token=%s, " |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 5636 | "touchableRegion=", |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5637 | i, windowInfo->name.c_str(), windowInfo->id, |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5638 | windowInfo->displayId, |
| 5639 | windowInfo->inputConfig.string().c_str(), |
| 5640 | windowInfo->alpha, windowInfo->frameLeft, |
| 5641 | windowInfo->frameTop, windowInfo->frameRight, |
| 5642 | windowInfo->frameBottom, windowInfo->globalScaleFactor, |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5643 | windowInfo->applicationInfo.name.c_str(), |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 5644 | binderToString(windowInfo->applicationInfo.token).c_str()); |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 5645 | dump += dumpRegion(windowInfo->touchableRegion); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5646 | dump += StringPrintf(", ownerPid=%d, ownerUid=%d, dispatchingTimeout=%" PRId64 |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5647 | "ms, hasToken=%s, " |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5648 | "touchOcclusionMode=%s\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5649 | windowInfo->ownerPid, windowInfo->ownerUid, |
Bernardo Rufino | c2f1fad | 2020-11-04 17:30:57 +0000 | [diff] [blame] | 5650 | millis(windowInfo->dispatchingTimeout), |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 5651 | binderToString(windowInfo->token).c_str(), |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5652 | toString(windowInfo->touchOcclusionMode).c_str()); |
chaviw | 85b4420 | 2020-07-24 11:46:21 -0700 | [diff] [blame] | 5653 | windowInfo->transform.dump(dump, "transform", INDENT4); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5654 | } |
| 5655 | } else { |
| 5656 | dump += INDENT2 "Windows: <none>\n"; |
| 5657 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5658 | } |
| 5659 | } else { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5660 | dump += INDENT "Displays: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5661 | } |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 5662 | dump += INDENT "Window Infos:\n"; |
| 5663 | dump += StringPrintf(INDENT2 "vsync id: %" PRId64 "\n", mWindowInfosVsyncId); |
| 5664 | dump += StringPrintf(INDENT2 "timestamp (ns): %" PRId64 "\n", mWindowInfosTimestamp); |
| 5665 | dump += "\n"; |
| 5666 | dump += StringPrintf(INDENT2 "max update delay (ns): %" PRId64 "\n", mMaxWindowInfosDelay); |
| 5667 | dump += StringPrintf(INDENT2 "max update delay vsync id: %" PRId64 "\n", |
| 5668 | mMaxWindowInfosDelayVsyncId); |
| 5669 | dump += "\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5670 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5671 | if (!mGlobalMonitorsByDisplay.empty()) { |
| 5672 | for (const auto& [displayId, monitors] : mGlobalMonitorsByDisplay) { |
| 5673 | dump += StringPrintf(INDENT "Global monitors on display %d:\n", displayId); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5674 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5675 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5676 | } else { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5677 | dump += INDENT "Global Monitors: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5678 | } |
| 5679 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5680 | const nsecs_t currentTime = now(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5681 | |
| 5682 | // Dump recently dispatched or dropped events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5683 | if (!mRecentQueue.empty()) { |
| 5684 | dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size()); |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 5685 | for (const std::shared_ptr<EventEntry>& entry : mRecentQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5686 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5687 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5688 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5689 | } |
| 5690 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5691 | dump += INDENT "RecentQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5692 | } |
| 5693 | |
| 5694 | // Dump event currently being dispatched. |
| 5695 | if (mPendingEvent) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5696 | dump += INDENT "PendingEvent:\n"; |
| 5697 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5698 | dump += mPendingEvent->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5699 | dump += StringPrintf(", age=%" PRId64 "ms\n", |
| 5700 | ns2ms(currentTime - mPendingEvent->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5701 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5702 | dump += INDENT "PendingEvent: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5703 | } |
| 5704 | |
| 5705 | // Dump inbound events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5706 | if (!mInboundQueue.empty()) { |
| 5707 | dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size()); |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 5708 | for (const std::shared_ptr<EventEntry>& entry : mInboundQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5709 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5710 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5711 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5712 | } |
| 5713 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5714 | dump += INDENT "InboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5715 | } |
| 5716 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5717 | if (!mReplacedKeys.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5718 | dump += INDENT "ReplacedKeys:\n"; |
Michael Wright | 3cec446 | 2022-11-24 22:05:46 +0000 | [diff] [blame] | 5719 | for (const auto& [replacement, newKeyCode] : mReplacedKeys) { |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5720 | dump += StringPrintf(INDENT2 "originalKeyCode=%d, deviceId=%d -> newKeyCode=%d\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5721 | replacement.keyCode, replacement.deviceId, newKeyCode); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5722 | } |
| 5723 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5724 | dump += INDENT "ReplacedKeys: <empty>\n"; |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5725 | } |
| 5726 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5727 | if (!mCommandQueue.empty()) { |
| 5728 | dump += StringPrintf(INDENT "CommandQueue: size=%zu\n", mCommandQueue.size()); |
| 5729 | } else { |
| 5730 | dump += INDENT "CommandQueue: <empty>\n"; |
| 5731 | } |
| 5732 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5733 | if (!mConnectionsByToken.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5734 | dump += INDENT "Connections:\n"; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5735 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5736 | dump += StringPrintf(INDENT2 "%i: channelName='%s', windowName='%s', " |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5737 | "status=%s, monitor=%s, responsive=%s\n", |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5738 | connection->inputChannel->getFd().get(), |
| 5739 | connection->getInputChannelName().c_str(), |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5740 | connection->getWindowName().c_str(), |
| 5741 | ftl::enum_string(connection->status).c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5742 | toString(connection->monitor), toString(connection->responsive)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5743 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5744 | if (!connection->outboundQueue.empty()) { |
| 5745 | dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n", |
| 5746 | connection->outboundQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5747 | dump += dumpQueue(connection->outboundQueue, currentTime); |
| 5748 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5749 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5750 | dump += INDENT3 "OutboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5751 | } |
| 5752 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5753 | if (!connection->waitQueue.empty()) { |
| 5754 | dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n", |
| 5755 | connection->waitQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5756 | dump += dumpQueue(connection->waitQueue, currentTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5757 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5758 | dump += INDENT3 "WaitQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5759 | } |
| 5760 | } |
| 5761 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5762 | dump += INDENT "Connections: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5763 | } |
| 5764 | |
| 5765 | if (isAppSwitchPendingLocked()) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5766 | dump += StringPrintf(INDENT "AppSwitch: pending, due in %" PRId64 "ms\n", |
| 5767 | ns2ms(mAppSwitchDueTime - now())); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5768 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5769 | dump += INDENT "AppSwitch: not pending\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5770 | } |
| 5771 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5772 | if (!mTouchModePerDisplay.empty()) { |
| 5773 | dump += INDENT "TouchModePerDisplay:\n"; |
| 5774 | for (const auto& [displayId, touchMode] : mTouchModePerDisplay) { |
| 5775 | dump += StringPrintf(INDENT2 "Display: %" PRId32 " TouchMode: %s\n", displayId, |
| 5776 | std::to_string(touchMode).c_str()); |
| 5777 | } |
| 5778 | } else { |
| 5779 | dump += INDENT "TouchModePerDisplay: <none>\n"; |
| 5780 | } |
| 5781 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5782 | dump += INDENT "Configuration:\n"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5783 | dump += StringPrintf(INDENT2 "KeyRepeatDelay: %" PRId64 "ms\n", ns2ms(mConfig.keyRepeatDelay)); |
| 5784 | dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n", |
| 5785 | ns2ms(mConfig.keyRepeatTimeout)); |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 5786 | dump += mLatencyTracker.dump(INDENT2); |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 5787 | dump += mLatencyAggregator.dump(INDENT2); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5788 | } |
| 5789 | |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 5790 | void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) const { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5791 | const size_t numMonitors = monitors.size(); |
| 5792 | for (size_t i = 0; i < numMonitors; i++) { |
| 5793 | const Monitor& monitor = monitors[i]; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5794 | const std::shared_ptr<InputChannel>& channel = monitor.inputChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5795 | dump += StringPrintf(INDENT2 "%zu: '%s', ", i, channel->getName().c_str()); |
| 5796 | dump += "\n"; |
| 5797 | } |
| 5798 | } |
| 5799 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5800 | class LooperEventCallback : public LooperCallback { |
| 5801 | public: |
| 5802 | LooperEventCallback(std::function<int(int events)> callback) : mCallback(callback) {} |
| 5803 | int handleEvent(int /*fd*/, int events, void* /*data*/) override { return mCallback(events); } |
| 5804 | |
| 5805 | private: |
| 5806 | std::function<int(int events)> mCallback; |
| 5807 | }; |
| 5808 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5809 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputChannel(const std::string& name) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5810 | if (DEBUG_CHANNEL_CREATION) { |
| 5811 | ALOGD("channel '%s' ~ createInputChannel", name.c_str()); |
| 5812 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5813 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5814 | std::unique_ptr<InputChannel> serverChannel; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5815 | std::unique_ptr<InputChannel> clientChannel; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5816 | status_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5817 | |
| 5818 | if (result) { |
| 5819 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5820 | } |
| 5821 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5822 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5823 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5824 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5825 | int fd = serverChannel->getFd(); |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 5826 | std::shared_ptr<Connection> connection = |
| 5827 | std::make_shared<Connection>(std::move(serverChannel), /*monitor=*/false, |
| 5828 | mIdGenerator); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5829 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5830 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5831 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5832 | } |
| 5833 | mConnectionsByToken.emplace(token, connection); |
| 5834 | |
| 5835 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5836 | this, std::placeholders::_1, token); |
| 5837 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5838 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, sp<LooperEventCallback>::make(callback), |
| 5839 | nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5840 | } // release lock |
| 5841 | |
| 5842 | // Wake the looper because some connections have changed. |
| 5843 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5844 | return clientChannel; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5845 | } |
| 5846 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5847 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputMonitor(int32_t displayId, |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5848 | const std::string& name, |
| 5849 | int32_t pid) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5850 | std::shared_ptr<InputChannel> serverChannel; |
| 5851 | std::unique_ptr<InputChannel> clientChannel; |
| 5852 | status_t result = openInputChannelPair(name, serverChannel, clientChannel); |
| 5853 | if (result) { |
| 5854 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5855 | } |
| 5856 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5857 | { // acquire lock |
| 5858 | std::scoped_lock _l(mLock); |
| 5859 | |
| 5860 | if (displayId < 0) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5861 | return base::Error(BAD_VALUE) << "Attempted to create input monitor with name " << name |
| 5862 | << " without a specified display."; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5863 | } |
| 5864 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 5865 | std::shared_ptr<Connection> connection = |
| 5866 | std::make_shared<Connection>(serverChannel, /*monitor=*/true, mIdGenerator); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5867 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5868 | const int fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5869 | |
| 5870 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5871 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5872 | } |
| 5873 | mConnectionsByToken.emplace(token, connection); |
| 5874 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5875 | this, std::placeholders::_1, token); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5876 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5877 | mGlobalMonitorsByDisplay[displayId].emplace_back(serverChannel, pid); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5878 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5879 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, sp<LooperEventCallback>::make(callback), |
| 5880 | nullptr); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5881 | } |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5882 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5883 | // Wake the looper because some connections have changed. |
| 5884 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5885 | return clientChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5886 | } |
| 5887 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5888 | status_t InputDispatcher::removeInputChannel(const sp<IBinder>& connectionToken) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5889 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5890 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5891 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5892 | status_t status = removeInputChannelLocked(connectionToken, /*notify=*/false); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5893 | if (status) { |
| 5894 | return status; |
| 5895 | } |
| 5896 | } // release lock |
| 5897 | |
| 5898 | // Wake the poll loop because removing the connection may have changed the current |
| 5899 | // synchronization state. |
| 5900 | mLooper->wake(); |
| 5901 | return OK; |
| 5902 | } |
| 5903 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5904 | status_t InputDispatcher::removeInputChannelLocked(const sp<IBinder>& connectionToken, |
| 5905 | bool notify) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 5906 | std::shared_ptr<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5907 | if (connection == nullptr) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5908 | // 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] | 5909 | return BAD_VALUE; |
| 5910 | } |
| 5911 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5912 | removeConnectionLocked(connection); |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 5913 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5914 | if (connection->monitor) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5915 | removeMonitorChannelLocked(connectionToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5916 | } |
| 5917 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5918 | mLooper->removeFd(connection->inputChannel->getFd()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5919 | |
| 5920 | nsecs_t currentTime = now(); |
| 5921 | abortBrokenDispatchCycleLocked(currentTime, connection, notify); |
| 5922 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5923 | connection->status = Connection::Status::ZOMBIE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5924 | return OK; |
| 5925 | } |
| 5926 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5927 | void InputDispatcher::removeMonitorChannelLocked(const sp<IBinder>& connectionToken) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5928 | for (auto it = mGlobalMonitorsByDisplay.begin(); it != mGlobalMonitorsByDisplay.end();) { |
| 5929 | auto& [displayId, monitors] = *it; |
| 5930 | std::erase_if(monitors, [connectionToken](const Monitor& monitor) { |
| 5931 | return monitor.inputChannel->getConnectionToken() == connectionToken; |
| 5932 | }); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5933 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5934 | if (monitors.empty()) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5935 | it = mGlobalMonitorsByDisplay.erase(it); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5936 | } else { |
| 5937 | ++it; |
| 5938 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5939 | } |
| 5940 | } |
| 5941 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5942 | status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5943 | std::scoped_lock _l(mLock); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 5944 | return pilferPointersLocked(token); |
| 5945 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5946 | |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 5947 | status_t InputDispatcher::pilferPointersLocked(const sp<IBinder>& token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5948 | const std::shared_ptr<InputChannel> requestingChannel = getInputChannelLocked(token); |
| 5949 | if (!requestingChannel) { |
| 5950 | ALOGW("Attempted to pilfer pointers from an un-registered channel or invalid token"); |
| 5951 | return BAD_VALUE; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5952 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5953 | |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5954 | auto [statePtr, windowPtr, displayId] = findTouchStateWindowAndDisplayLocked(token); |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 5955 | if (statePtr == nullptr || windowPtr == nullptr || windowPtr->pointerIds.none()) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5956 | ALOGW("Attempted to pilfer points from a channel without any on-going pointer streams." |
| 5957 | " Ignoring."); |
| 5958 | return BAD_VALUE; |
| 5959 | } |
| 5960 | |
| 5961 | TouchState& state = *statePtr; |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 5962 | TouchedWindow& window = *windowPtr; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5963 | // Send cancel events to all the input channels we're stealing from. |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 5964 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5965 | "input channel stole pointer stream"); |
| 5966 | options.deviceId = state.deviceId; |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5967 | options.displayId = displayId; |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 5968 | options.pointerIds = window.pointerIds; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5969 | std::string canceledWindows; |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 5970 | for (const TouchedWindow& w : state.windows) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5971 | const std::shared_ptr<InputChannel> channel = |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 5972 | getInputChannelLocked(w.windowHandle->getToken()); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5973 | if (channel != nullptr && channel->getConnectionToken() != token) { |
| 5974 | synthesizeCancelationEventsForInputChannelLocked(channel, options); |
| 5975 | canceledWindows += canceledWindows.empty() ? "[" : ", "; |
| 5976 | canceledWindows += channel->getName(); |
| 5977 | } |
| 5978 | } |
| 5979 | canceledWindows += canceledWindows.empty() ? "[]" : "]"; |
| 5980 | ALOGI("Channel %s is stealing touch from %s", requestingChannel->getName().c_str(), |
| 5981 | canceledWindows.c_str()); |
| 5982 | |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 5983 | // Prevent the gesture from being sent to any other windows. |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 5984 | // This only blocks relevant pointers to be sent to other windows |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 5985 | window.pilferedPointerIds |= window.pointerIds; |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 5986 | |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 5987 | state.cancelPointersForWindowsExcept(window.pointerIds, token); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5988 | return OK; |
| 5989 | } |
| 5990 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5991 | void InputDispatcher::requestPointerCapture(const sp<IBinder>& windowToken, bool enabled) { |
| 5992 | { // acquire lock |
| 5993 | std::scoped_lock _l(mLock); |
| 5994 | if (DEBUG_FOCUS) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5995 | const sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(windowToken); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5996 | ALOGI("Request to %s Pointer Capture from: %s.", enabled ? "enable" : "disable", |
| 5997 | windowHandle != nullptr ? windowHandle->getName().c_str() |
| 5998 | : "token without window"); |
| 5999 | } |
| 6000 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6001 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6002 | if (focusedToken != windowToken) { |
| 6003 | ALOGW("Ignoring request to %s Pointer Capture: window does not have focus.", |
| 6004 | enabled ? "enable" : "disable"); |
| 6005 | return; |
| 6006 | } |
| 6007 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6008 | if (enabled == mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6009 | ALOGW("Ignoring request to %s Pointer Capture: " |
| 6010 | "window has %s requested pointer capture.", |
| 6011 | enabled ? "enable" : "disable", enabled ? "already" : "not"); |
| 6012 | return; |
| 6013 | } |
| 6014 | |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 6015 | if (enabled) { |
| 6016 | if (std::find(mIneligibleDisplaysForPointerCapture.begin(), |
| 6017 | mIneligibleDisplaysForPointerCapture.end(), |
| 6018 | mFocusedDisplayId) != mIneligibleDisplaysForPointerCapture.end()) { |
| 6019 | ALOGW("Ignoring request to enable Pointer Capture: display is not eligible"); |
| 6020 | return; |
| 6021 | } |
| 6022 | } |
| 6023 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6024 | setPointerCaptureLocked(enabled); |
| 6025 | } // release lock |
| 6026 | |
| 6027 | // Wake the thread to process command entries. |
| 6028 | mLooper->wake(); |
| 6029 | } |
| 6030 | |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 6031 | void InputDispatcher::setDisplayEligibilityForPointerCapture(int32_t displayId, bool isEligible) { |
| 6032 | { // acquire lock |
| 6033 | std::scoped_lock _l(mLock); |
| 6034 | std::erase(mIneligibleDisplaysForPointerCapture, displayId); |
| 6035 | if (!isEligible) { |
| 6036 | mIneligibleDisplaysForPointerCapture.push_back(displayId); |
| 6037 | } |
| 6038 | } // release lock |
| 6039 | } |
| 6040 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6041 | std::optional<int32_t> InputDispatcher::findMonitorPidByTokenLocked(const sp<IBinder>& token) { |
| 6042 | for (const auto& [_, monitors] : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6043 | for (const Monitor& monitor : monitors) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 6044 | if (monitor.inputChannel->getConnectionToken() == token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6045 | return monitor.pid; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6046 | } |
| 6047 | } |
| 6048 | } |
| 6049 | return std::nullopt; |
| 6050 | } |
| 6051 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6052 | std::shared_ptr<Connection> InputDispatcher::getConnectionLocked( |
| 6053 | const sp<IBinder>& inputConnectionToken) const { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 6054 | if (inputConnectionToken == nullptr) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 6055 | return nullptr; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 6056 | } |
| 6057 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 6058 | for (const auto& [token, connection] : mConnectionsByToken) { |
| 6059 | if (token == inputConnectionToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 6060 | return connection; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6061 | } |
| 6062 | } |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 6063 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 6064 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6065 | } |
| 6066 | |
Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 6067 | std::string InputDispatcher::getConnectionNameLocked(const sp<IBinder>& connectionToken) const { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6068 | std::shared_ptr<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 6069 | if (connection == nullptr) { |
| 6070 | return "<nullptr>"; |
| 6071 | } |
| 6072 | return connection->getInputChannelName(); |
| 6073 | } |
| 6074 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6075 | void InputDispatcher::removeConnectionLocked(const std::shared_ptr<Connection>& connection) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6076 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 6077 | mConnectionsByToken.erase(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6078 | } |
| 6079 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6080 | void InputDispatcher::doDispatchCycleFinishedCommand(nsecs_t finishTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6081 | const std::shared_ptr<Connection>& connection, |
| 6082 | uint32_t seq, bool handled, |
| 6083 | nsecs_t consumeTime) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6084 | // Handle post-event policy actions. |
| 6085 | std::deque<DispatchEntry*>::iterator dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 6086 | if (dispatchEntryIt == connection->waitQueue.end()) { |
| 6087 | return; |
| 6088 | } |
| 6089 | DispatchEntry* dispatchEntry = *dispatchEntryIt; |
| 6090 | const nsecs_t eventDuration = finishTime - dispatchEntry->deliveryTime; |
| 6091 | if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) { |
| 6092 | ALOGI("%s spent %" PRId64 "ms processing %s", connection->getWindowName().c_str(), |
| 6093 | ns2ms(eventDuration), dispatchEntry->eventEntry->getDescription().c_str()); |
| 6094 | } |
| 6095 | if (shouldReportFinishedEvent(*dispatchEntry, *connection)) { |
| 6096 | mLatencyTracker.trackFinishedEvent(dispatchEntry->eventEntry->id, |
| 6097 | connection->inputChannel->getConnectionToken(), |
| 6098 | dispatchEntry->deliveryTime, consumeTime, finishTime); |
| 6099 | } |
| 6100 | |
| 6101 | bool restartEvent; |
| 6102 | if (dispatchEntry->eventEntry->type == EventEntry::Type::KEY) { |
| 6103 | KeyEntry& keyEntry = static_cast<KeyEntry&>(*(dispatchEntry->eventEntry)); |
| 6104 | restartEvent = |
| 6105 | afterKeyEventLockedInterruptable(connection, dispatchEntry, keyEntry, handled); |
| 6106 | } else if (dispatchEntry->eventEntry->type == EventEntry::Type::MOTION) { |
| 6107 | MotionEntry& motionEntry = static_cast<MotionEntry&>(*(dispatchEntry->eventEntry)); |
| 6108 | restartEvent = afterMotionEventLockedInterruptable(connection, dispatchEntry, motionEntry, |
| 6109 | handled); |
| 6110 | } else { |
| 6111 | restartEvent = false; |
| 6112 | } |
| 6113 | |
| 6114 | // Dequeue the event and start the next cycle. |
| 6115 | // Because the lock might have been released, it is possible that the |
| 6116 | // contents of the wait queue to have been drained, so we need to double-check |
| 6117 | // a few things. |
| 6118 | dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 6119 | if (dispatchEntryIt != connection->waitQueue.end()) { |
| 6120 | dispatchEntry = *dispatchEntryIt; |
| 6121 | connection->waitQueue.erase(dispatchEntryIt); |
| 6122 | const sp<IBinder>& connectionToken = connection->inputChannel->getConnectionToken(); |
| 6123 | mAnrTracker.erase(dispatchEntry->timeoutTime, connectionToken); |
| 6124 | if (!connection->responsive) { |
| 6125 | connection->responsive = isConnectionResponsive(*connection); |
| 6126 | if (connection->responsive) { |
| 6127 | // The connection was unresponsive, and now it's responsive. |
| 6128 | processConnectionResponsiveLocked(*connection); |
| 6129 | } |
| 6130 | } |
| 6131 | traceWaitQueueLength(*connection); |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 6132 | if (restartEvent && connection->status == Connection::Status::NORMAL) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6133 | connection->outboundQueue.push_front(dispatchEntry); |
| 6134 | traceOutboundQueueLength(*connection); |
| 6135 | } else { |
| 6136 | releaseDispatchEntry(dispatchEntry); |
| 6137 | } |
| 6138 | } |
| 6139 | |
| 6140 | // Start the next dispatch cycle for this connection. |
| 6141 | startDispatchCycleLocked(now(), connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6142 | } |
| 6143 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6144 | void InputDispatcher::sendFocusChangedCommandLocked(const sp<IBinder>& oldToken, |
| 6145 | const sp<IBinder>& newToken) { |
| 6146 | auto command = [this, oldToken, newToken]() REQUIRES(mLock) { |
| 6147 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6148 | mPolicy.notifyFocusChanged(oldToken, newToken); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6149 | }; |
| 6150 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6151 | } |
| 6152 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6153 | void InputDispatcher::sendDropWindowCommandLocked(const sp<IBinder>& token, float x, float y) { |
| 6154 | auto command = [this, token, x, y]() REQUIRES(mLock) { |
| 6155 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6156 | mPolicy.notifyDropWindow(token, x, y); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6157 | }; |
| 6158 | postCommandLocked(std::move(command)); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 6159 | } |
| 6160 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6161 | void InputDispatcher::onAnrLocked(const std::shared_ptr<Connection>& connection) { |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6162 | if (connection == nullptr) { |
| 6163 | LOG_ALWAYS_FATAL("Caller must check for nullness"); |
| 6164 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6165 | // Since we are allowing the policy to extend the timeout, maybe the waitQueue |
| 6166 | // is already healthy again. Don't raise ANR in this situation |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6167 | if (connection->waitQueue.empty()) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6168 | ALOGI("Not raising ANR because the connection %s has recovered", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6169 | connection->inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6170 | return; |
| 6171 | } |
| 6172 | /** |
| 6173 | * The "oldestEntry" is the entry that was first sent to the application. That entry, however, |
| 6174 | * may not be the one that caused the timeout to occur. One possibility is that window timeout |
| 6175 | * has changed. This could cause newer entries to time out before the already dispatched |
| 6176 | * entries. In that situation, the newest entries caused ANR. But in all likelihood, the app |
| 6177 | * processes the events linearly. So providing information about the oldest entry seems to be |
| 6178 | * most useful. |
| 6179 | */ |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6180 | DispatchEntry* oldestEntry = *connection->waitQueue.begin(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6181 | const nsecs_t currentWait = now() - oldestEntry->deliveryTime; |
| 6182 | std::string reason = |
| 6183 | android::base::StringPrintf("%s is not responding. Waited %" PRId64 "ms for %s", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6184 | connection->inputChannel->getName().c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6185 | ns2ms(currentWait), |
| 6186 | oldestEntry->eventEntry->getDescription().c_str()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6187 | sp<IBinder> connectionToken = connection->inputChannel->getConnectionToken(); |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 6188 | updateLastAnrStateLocked(getWindowHandleLocked(connectionToken), reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6189 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6190 | processConnectionUnresponsiveLocked(*connection, std::move(reason)); |
| 6191 | |
| 6192 | // Stop waking up for events on this connection, it is already unresponsive |
| 6193 | cancelEventsForAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6194 | } |
| 6195 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6196 | void InputDispatcher::onAnrLocked(std::shared_ptr<InputApplicationHandle> application) { |
| 6197 | std::string reason = |
| 6198 | StringPrintf("%s does not have a focused window", application->getName().c_str()); |
| 6199 | updateLastAnrStateLocked(*application, reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6200 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6201 | auto command = [this, application = std::move(application)]() REQUIRES(mLock) { |
| 6202 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6203 | mPolicy.notifyNoFocusedWindowAnr(application); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6204 | }; |
| 6205 | postCommandLocked(std::move(command)); |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 6206 | } |
| 6207 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 6208 | void InputDispatcher::updateLastAnrStateLocked(const sp<WindowInfoHandle>& window, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6209 | const std::string& reason) { |
| 6210 | const std::string windowLabel = getApplicationWindowLabel(nullptr, window); |
| 6211 | updateLastAnrStateLocked(windowLabel, reason); |
| 6212 | } |
| 6213 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6214 | void InputDispatcher::updateLastAnrStateLocked(const InputApplicationHandle& application, |
| 6215 | const std::string& reason) { |
| 6216 | const std::string windowLabel = getApplicationWindowLabel(&application, nullptr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6217 | updateLastAnrStateLocked(windowLabel, reason); |
| 6218 | } |
| 6219 | |
| 6220 | void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel, |
| 6221 | const std::string& reason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6222 | // 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] | 6223 | time_t t = time(nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6224 | struct tm tm; |
| 6225 | localtime_r(&t, &tm); |
| 6226 | char timestr[64]; |
| 6227 | strftime(timestr, sizeof(timestr), "%F %T", &tm); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6228 | mLastAnrState.clear(); |
| 6229 | mLastAnrState += INDENT "ANR:\n"; |
| 6230 | mLastAnrState += StringPrintf(INDENT2 "Time: %s\n", timestr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6231 | mLastAnrState += StringPrintf(INDENT2 "Reason: %s\n", reason.c_str()); |
| 6232 | mLastAnrState += StringPrintf(INDENT2 "Window: %s\n", windowLabel.c_str()); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6233 | dumpDispatchStateLocked(mLastAnrState); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6234 | } |
| 6235 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6236 | void InputDispatcher::doInterceptKeyBeforeDispatchingCommand(const sp<IBinder>& focusedWindowToken, |
| 6237 | KeyEntry& entry) { |
| 6238 | const KeyEvent event = createKeyEvent(entry); |
| 6239 | nsecs_t delay = 0; |
| 6240 | { // release lock |
| 6241 | scoped_unlock unlock(mLock); |
| 6242 | android::base::Timer t; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6243 | delay = mPolicy.interceptKeyBeforeDispatching(focusedWindowToken, event, entry.policyFlags); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6244 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 6245 | ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms", |
| 6246 | std::to_string(t.duration().count()).c_str()); |
| 6247 | } |
| 6248 | } // acquire lock |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6249 | |
| 6250 | if (delay < 0) { |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 6251 | entry.interceptKeyResult = KeyEntry::InterceptKeyResult::SKIP; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6252 | } else if (delay == 0) { |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 6253 | entry.interceptKeyResult = KeyEntry::InterceptKeyResult::CONTINUE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6254 | } else { |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 6255 | entry.interceptKeyResult = KeyEntry::InterceptKeyResult::TRY_AGAIN_LATER; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6256 | entry.interceptKeyWakeupTime = now() + delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6257 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6258 | } |
| 6259 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6260 | void InputDispatcher::sendWindowUnresponsiveCommandLocked(const sp<IBinder>& token, |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6261 | std::optional<int32_t> pid, |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6262 | std::string reason) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6263 | auto command = [this, token, pid, reason = std::move(reason)]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6264 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6265 | mPolicy.notifyWindowUnresponsive(token, pid, reason); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6266 | }; |
| 6267 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6268 | } |
| 6269 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6270 | void InputDispatcher::sendWindowResponsiveCommandLocked(const sp<IBinder>& token, |
| 6271 | std::optional<int32_t> pid) { |
| 6272 | auto command = [this, token, pid]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6273 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6274 | mPolicy.notifyWindowResponsive(token, pid); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6275 | }; |
| 6276 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6277 | } |
| 6278 | |
| 6279 | /** |
| 6280 | * Tell the policy that a connection has become unresponsive so that it can start ANR. |
| 6281 | * Check whether the connection of interest is a monitor or a window, and add the corresponding |
| 6282 | * command entry to the command queue. |
| 6283 | */ |
| 6284 | void InputDispatcher::processConnectionUnresponsiveLocked(const Connection& connection, |
| 6285 | std::string reason) { |
| 6286 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6287 | std::optional<int32_t> pid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6288 | if (connection.monitor) { |
| 6289 | ALOGW("Monitor %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 6290 | reason.c_str()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6291 | pid = findMonitorPidByTokenLocked(connectionToken); |
| 6292 | } else { |
| 6293 | // The connection is a window |
| 6294 | ALOGW("Window %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 6295 | reason.c_str()); |
| 6296 | const sp<WindowInfoHandle> handle = getWindowHandleLocked(connectionToken); |
| 6297 | if (handle != nullptr) { |
| 6298 | pid = handle->getInfo()->ownerPid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6299 | } |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6300 | } |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6301 | sendWindowUnresponsiveCommandLocked(connectionToken, pid, std::move(reason)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6302 | } |
| 6303 | |
| 6304 | /** |
| 6305 | * Tell the policy that a connection has become responsive so that it can stop ANR. |
| 6306 | */ |
| 6307 | void InputDispatcher::processConnectionResponsiveLocked(const Connection& connection) { |
| 6308 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6309 | std::optional<int32_t> pid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6310 | if (connection.monitor) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6311 | pid = findMonitorPidByTokenLocked(connectionToken); |
| 6312 | } else { |
| 6313 | // The connection is a window |
| 6314 | const sp<WindowInfoHandle> handle = getWindowHandleLocked(connectionToken); |
| 6315 | if (handle != nullptr) { |
| 6316 | pid = handle->getInfo()->ownerPid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6317 | } |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6318 | } |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6319 | sendWindowResponsiveCommandLocked(connectionToken, pid); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6320 | } |
| 6321 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6322 | bool InputDispatcher::afterKeyEventLockedInterruptable( |
| 6323 | const std::shared_ptr<Connection>& connection, DispatchEntry* dispatchEntry, |
| 6324 | KeyEntry& keyEntry, bool handled) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6325 | if (keyEntry.flags & AKEY_EVENT_FLAG_FALLBACK) { |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6326 | if (!handled) { |
| 6327 | // Report the key as unhandled, since the fallback was not handled. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6328 | mReporter->reportUnhandledKey(keyEntry.id); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6329 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6330 | return false; |
| 6331 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6332 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6333 | // Get the fallback key state. |
| 6334 | // Clear it out after dispatching the UP. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6335 | int32_t originalKeyCode = keyEntry.keyCode; |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6336 | std::optional<int32_t> fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6337 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6338 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6339 | } |
| 6340 | |
| 6341 | if (handled || !dispatchEntry->hasForegroundTarget()) { |
| 6342 | // If the application handles the original key for which we previously |
| 6343 | // generated a fallback or if the window is not a foreground window, |
| 6344 | // then cancel the associated fallback key, if any. |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6345 | if (fallbackKeyCode) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6346 | // Dispatch the unhandled key to the policy with the cancel flag. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6347 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6348 | ALOGD("Unhandled key event: Asking policy to cancel fallback action. " |
| 6349 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6350 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, |
| 6351 | keyEntry.policyFlags); |
| 6352 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6353 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6354 | event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6355 | |
| 6356 | mLock.unlock(); |
| 6357 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6358 | if (const auto unhandledKeyFallback = |
| 6359 | mPolicy.dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), |
| 6360 | event, keyEntry.policyFlags); |
| 6361 | unhandledKeyFallback) { |
| 6362 | event = *unhandledKeyFallback; |
| 6363 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6364 | |
| 6365 | mLock.lock(); |
| 6366 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6367 | // Cancel the fallback key. |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6368 | if (*fallbackKeyCode != AKEYCODE_UNKNOWN) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 6369 | CancelationOptions options(CancelationOptions::Mode::CANCEL_FALLBACK_EVENTS, |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6370 | "application handled the original non-fallback key " |
| 6371 | "or is no longer a foreground target, " |
| 6372 | "canceling previously dispatched fallback key"); |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6373 | options.keyCode = *fallbackKeyCode; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6374 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6375 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6376 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6377 | } |
| 6378 | } else { |
| 6379 | // If the application did not handle a non-fallback key, first check |
| 6380 | // that we are in a good state to perform unhandled key event processing |
| 6381 | // Then ask the policy what to do with it. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6382 | bool initialDown = keyEntry.action == AKEY_EVENT_ACTION_DOWN && keyEntry.repeatCount == 0; |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6383 | if (!fallbackKeyCode && !initialDown) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6384 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6385 | ALOGD("Unhandled key event: Skipping unhandled key event processing " |
| 6386 | "since this is not an initial down. " |
| 6387 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6388 | originalKeyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 6389 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6390 | return false; |
| 6391 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6392 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6393 | // Dispatch the unhandled key to the policy. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6394 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6395 | ALOGD("Unhandled key event: Asking policy to perform fallback action. " |
| 6396 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6397 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 6398 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6399 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6400 | |
| 6401 | mLock.unlock(); |
| 6402 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6403 | bool fallback = false; |
| 6404 | if (auto fb = mPolicy.dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), |
| 6405 | event, keyEntry.policyFlags); |
| 6406 | fb) { |
| 6407 | fallback = true; |
| 6408 | event = *fb; |
| 6409 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6410 | |
| 6411 | mLock.lock(); |
| 6412 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 6413 | if (connection->status != Connection::Status::NORMAL) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6414 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6415 | return false; |
| 6416 | } |
| 6417 | |
| 6418 | // Latch the fallback keycode for this key on an initial down. |
| 6419 | // The fallback keycode cannot change at any other point in the lifecycle. |
| 6420 | if (initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6421 | if (fallback) { |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6422 | *fallbackKeyCode = event.getKeyCode(); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6423 | } else { |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6424 | *fallbackKeyCode = AKEYCODE_UNKNOWN; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6425 | } |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6426 | connection->inputState.setFallbackKey(originalKeyCode, *fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6427 | } |
| 6428 | |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6429 | ALOG_ASSERT(fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6430 | |
| 6431 | // Cancel the fallback key if the policy decides not to send it anymore. |
| 6432 | // We will continue to dispatch the key to the policy but we will no |
| 6433 | // longer dispatch a fallback key to the application. |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6434 | if (*fallbackKeyCode != AKEYCODE_UNKNOWN && |
| 6435 | (!fallback || *fallbackKeyCode != event.getKeyCode())) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6436 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6437 | if (fallback) { |
| 6438 | ALOGD("Unhandled key event: Policy requested to send key %d" |
| 6439 | "as a fallback for %d, but on the DOWN it had requested " |
| 6440 | "to send %d instead. Fallback canceled.", |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6441 | event.getKeyCode(), originalKeyCode, *fallbackKeyCode); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6442 | } else { |
| 6443 | ALOGD("Unhandled key event: Policy did not request fallback for %d, " |
| 6444 | "but on the DOWN it had requested to send %d. " |
| 6445 | "Fallback canceled.", |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6446 | originalKeyCode, *fallbackKeyCode); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6447 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6448 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6449 | |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 6450 | CancelationOptions options(CancelationOptions::Mode::CANCEL_FALLBACK_EVENTS, |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6451 | "canceling fallback, policy no longer desires it"); |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6452 | options.keyCode = *fallbackKeyCode; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6453 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
| 6454 | |
| 6455 | fallback = false; |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6456 | *fallbackKeyCode = AKEYCODE_UNKNOWN; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6457 | if (keyEntry.action != AKEY_EVENT_ACTION_UP) { |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6458 | connection->inputState.setFallbackKey(originalKeyCode, *fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6459 | } |
| 6460 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6461 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6462 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6463 | { |
| 6464 | std::string msg; |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6465 | const std::map<int32_t, int32_t>& fallbackKeys = |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6466 | connection->inputState.getFallbackKeys(); |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6467 | for (const auto& [key, value] : fallbackKeys) { |
| 6468 | msg += StringPrintf(", %d->%d", key, value); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6469 | } |
| 6470 | ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.", |
| 6471 | fallbackKeys.size(), msg.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6472 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6473 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6474 | |
| 6475 | if (fallback) { |
| 6476 | // Restart the dispatch cycle using the fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6477 | keyEntry.eventTime = event.getEventTime(); |
| 6478 | keyEntry.deviceId = event.getDeviceId(); |
| 6479 | keyEntry.source = event.getSource(); |
| 6480 | keyEntry.displayId = event.getDisplayId(); |
| 6481 | keyEntry.flags = event.getFlags() | AKEY_EVENT_FLAG_FALLBACK; |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6482 | keyEntry.keyCode = *fallbackKeyCode; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6483 | keyEntry.scanCode = event.getScanCode(); |
| 6484 | keyEntry.metaState = event.getMetaState(); |
| 6485 | keyEntry.repeatCount = event.getRepeatCount(); |
| 6486 | keyEntry.downTime = event.getDownTime(); |
| 6487 | keyEntry.syntheticRepeat = false; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6488 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6489 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6490 | ALOGD("Unhandled key event: Dispatching fallback key. " |
| 6491 | "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x", |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6492 | originalKeyCode, *fallbackKeyCode, keyEntry.metaState); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6493 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6494 | return true; // restart the event |
| 6495 | } else { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6496 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6497 | ALOGD("Unhandled key event: No fallback key."); |
| 6498 | } |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6499 | |
| 6500 | // Report the key as unhandled, since there is no fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6501 | mReporter->reportUnhandledKey(keyEntry.id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6502 | } |
| 6503 | } |
| 6504 | return false; |
| 6505 | } |
| 6506 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6507 | bool InputDispatcher::afterMotionEventLockedInterruptable( |
| 6508 | const std::shared_ptr<Connection>& connection, DispatchEntry* dispatchEntry, |
| 6509 | MotionEntry& motionEntry, bool handled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6510 | return false; |
| 6511 | } |
| 6512 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6513 | void InputDispatcher::traceInboundQueueLengthLocked() { |
| 6514 | if (ATRACE_ENABLED()) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 6515 | ATRACE_INT("iq", mInboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6516 | } |
| 6517 | } |
| 6518 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6519 | void InputDispatcher::traceOutboundQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6520 | if (ATRACE_ENABLED()) { |
| 6521 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6522 | snprintf(counterName, sizeof(counterName), "oq:%s", connection.getWindowName().c_str()); |
| 6523 | ATRACE_INT(counterName, connection.outboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6524 | } |
| 6525 | } |
| 6526 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6527 | void InputDispatcher::traceWaitQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6528 | if (ATRACE_ENABLED()) { |
| 6529 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6530 | snprintf(counterName, sizeof(counterName), "wq:%s", connection.getWindowName().c_str()); |
| 6531 | ATRACE_INT(counterName, connection.waitQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6532 | } |
| 6533 | } |
| 6534 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6535 | void InputDispatcher::dump(std::string& dump) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6536 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6537 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6538 | dump += "Input Dispatcher State:\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6539 | dumpDispatchStateLocked(dump); |
| 6540 | |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6541 | if (!mLastAnrState.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6542 | dump += "\nInput Dispatcher State at time of last ANR:\n"; |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6543 | dump += mLastAnrState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6544 | } |
| 6545 | } |
| 6546 | |
| 6547 | void InputDispatcher::monitor() { |
| 6548 | // 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] | 6549 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6550 | mLooper->wake(); |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6551 | mDispatcherIsAlive.wait(_l); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6552 | } |
| 6553 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6554 | /** |
| 6555 | * Wake up the dispatcher and wait until it processes all events and commands. |
| 6556 | * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so |
| 6557 | * this method can be safely called from any thread, as long as you've ensured that |
| 6558 | * the work you are interested in completing has already been queued. |
| 6559 | */ |
| 6560 | bool InputDispatcher::waitForIdle() { |
| 6561 | /** |
| 6562 | * Timeout should represent the longest possible time that a device might spend processing |
| 6563 | * events and commands. |
| 6564 | */ |
| 6565 | constexpr std::chrono::duration TIMEOUT = 100ms; |
| 6566 | std::unique_lock lock(mLock); |
| 6567 | mLooper->wake(); |
| 6568 | std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT); |
| 6569 | return result == std::cv_status::no_timeout; |
| 6570 | } |
| 6571 | |
Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 6572 | /** |
| 6573 | * Sets focus to the window identified by the token. This must be called |
| 6574 | * after updating any input window handles. |
| 6575 | * |
| 6576 | * Params: |
| 6577 | * request.token - input channel token used to identify the window that should gain focus. |
| 6578 | * request.focusedToken - the token that the caller expects currently to be focused. If the |
| 6579 | * specified token does not match the currently focused window, this request will be dropped. |
| 6580 | * If the specified focused token matches the currently focused window, the call will succeed. |
| 6581 | * Set this to "null" if this call should succeed no matter what the currently focused token is. |
| 6582 | * request.timestamp - SYSTEM_TIME_MONOTONIC timestamp in nanos set by the client (wm) |
| 6583 | * when requesting the focus change. This determines which request gets |
| 6584 | * precedence if there is a focus change request from another source such as pointer down. |
| 6585 | */ |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6586 | void InputDispatcher::setFocusedWindow(const FocusRequest& request) { |
| 6587 | { // acquire lock |
| 6588 | std::scoped_lock _l(mLock); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6589 | std::optional<FocusResolver::FocusChanges> changes = |
| 6590 | mFocusResolver.setFocusedWindow(request, getWindowHandlesLocked(request.displayId)); |
| 6591 | if (changes) { |
| 6592 | onFocusChangedLocked(*changes); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6593 | } |
| 6594 | } // release lock |
| 6595 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6596 | mLooper->wake(); |
| 6597 | } |
| 6598 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6599 | void InputDispatcher::onFocusChangedLocked(const FocusResolver::FocusChanges& changes) { |
| 6600 | if (changes.oldFocus) { |
| 6601 | std::shared_ptr<InputChannel> focusedInputChannel = getInputChannelLocked(changes.oldFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6602 | if (focusedInputChannel) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 6603 | CancelationOptions options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6604 | "focus left window"); |
| 6605 | synthesizeCancelationEventsForInputChannelLocked(focusedInputChannel, options); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6606 | enqueueFocusEventLocked(changes.oldFocus, /*hasFocus=*/false, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6607 | } |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6608 | } |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6609 | if (changes.newFocus) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6610 | enqueueFocusEventLocked(changes.newFocus, /*hasFocus=*/true, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6611 | } |
| 6612 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6613 | // If a window has pointer capture, then it must have focus. We need to ensure that this |
| 6614 | // contract is upheld when pointer capture is being disabled due to a loss of window focus. |
| 6615 | // If the window loses focus before it loses pointer capture, then the window can be in a state |
| 6616 | // where it has pointer capture but not focus, violating the contract. Therefore we must |
| 6617 | // dispatch the pointer capture event before the focus event. Since focus events are added to |
| 6618 | // the front of the queue (above), we add the pointer capture event to the front of the queue |
| 6619 | // after the focus events are added. This ensures the pointer capture event ends up at the |
| 6620 | // front. |
| 6621 | disablePointerCaptureForcedLocked(); |
| 6622 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6623 | if (mFocusedDisplayId == changes.displayId) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6624 | sendFocusChangedCommandLocked(changes.oldFocus, changes.newFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6625 | } |
| 6626 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6627 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6628 | void InputDispatcher::disablePointerCaptureForcedLocked() { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6629 | if (!mCurrentPointerCaptureRequest.enable && !mWindowTokenWithPointerCapture) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6630 | return; |
| 6631 | } |
| 6632 | |
| 6633 | ALOGD_IF(DEBUG_FOCUS, "Disabling Pointer Capture because the window lost focus."); |
| 6634 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6635 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6636 | setPointerCaptureLocked(false); |
| 6637 | } |
| 6638 | |
| 6639 | if (!mWindowTokenWithPointerCapture) { |
| 6640 | // No need to send capture changes because no window has capture. |
| 6641 | return; |
| 6642 | } |
| 6643 | |
| 6644 | if (mPendingEvent != nullptr) { |
| 6645 | // Move the pending event to the front of the queue. This will give the chance |
| 6646 | // for the pending event to be dropped if it is a captured event. |
| 6647 | mInboundQueue.push_front(mPendingEvent); |
| 6648 | mPendingEvent = nullptr; |
| 6649 | } |
| 6650 | |
| 6651 | auto entry = std::make_unique<PointerCaptureChangedEntry>(mIdGenerator.nextId(), now(), |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6652 | mCurrentPointerCaptureRequest); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6653 | mInboundQueue.push_front(std::move(entry)); |
| 6654 | } |
| 6655 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6656 | void InputDispatcher::setPointerCaptureLocked(bool enable) { |
| 6657 | mCurrentPointerCaptureRequest.enable = enable; |
| 6658 | mCurrentPointerCaptureRequest.seq++; |
| 6659 | auto command = [this, request = mCurrentPointerCaptureRequest]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6660 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6661 | mPolicy.setPointerCapture(request); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6662 | }; |
| 6663 | postCommandLocked(std::move(command)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6664 | } |
| 6665 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6666 | void InputDispatcher::displayRemoved(int32_t displayId) { |
| 6667 | { // acquire lock |
| 6668 | std::scoped_lock _l(mLock); |
| 6669 | // Set an empty list to remove all handles from the specific display. |
| 6670 | setInputWindowsLocked(/* window handles */ {}, displayId); |
| 6671 | setFocusedApplicationLocked(displayId, nullptr); |
| 6672 | // Call focus resolver to clean up stale requests. This must be called after input windows |
| 6673 | // have been removed for the removed display. |
| 6674 | mFocusResolver.displayRemoved(displayId); |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 6675 | // Reset pointer capture eligibility, regardless of previous state. |
| 6676 | std::erase(mIneligibleDisplaysForPointerCapture, displayId); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 6677 | // Remove the associated touch mode state. |
| 6678 | mTouchModePerDisplay.erase(displayId); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6679 | } // release lock |
| 6680 | |
| 6681 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6682 | mLooper->wake(); |
| 6683 | } |
| 6684 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 6685 | void InputDispatcher::onWindowInfosChanged(const gui::WindowInfosUpdate& update) { |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6686 | // The listener sends the windows as a flattened array. Separate the windows by display for |
| 6687 | // more convenient parsing. |
| 6688 | std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>> handlesPerDisplay; |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 6689 | for (const auto& info : update.windowInfos) { |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6690 | handlesPerDisplay.emplace(info.displayId, std::vector<sp<WindowInfoHandle>>()); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6691 | handlesPerDisplay[info.displayId].push_back(sp<WindowInfoHandle>::make(info)); |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6692 | } |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6693 | |
| 6694 | { // acquire lock |
| 6695 | std::scoped_lock _l(mLock); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 6696 | |
| 6697 | // Ensure that we have an entry created for all existing displays so that if a displayId has |
| 6698 | // no windows, we can tell that the windows were removed from the display. |
| 6699 | for (const auto& [displayId, _] : mWindowHandlesByDisplay) { |
| 6700 | handlesPerDisplay[displayId]; |
| 6701 | } |
| 6702 | |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6703 | mDisplayInfos.clear(); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 6704 | for (const auto& displayInfo : update.displayInfos) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6705 | mDisplayInfos.emplace(displayInfo.displayId, displayInfo); |
| 6706 | } |
| 6707 | |
| 6708 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 6709 | setInputWindowsLocked(handles, displayId); |
| 6710 | } |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 6711 | |
| 6712 | mWindowInfosVsyncId = update.vsyncId; |
| 6713 | mWindowInfosTimestamp = update.timestamp; |
| 6714 | |
| 6715 | int64_t delay = systemTime() - update.timestamp; |
| 6716 | if (delay > mMaxWindowInfosDelay) { |
| 6717 | mMaxWindowInfosDelay = delay; |
| 6718 | mMaxWindowInfosDelayVsyncId = update.vsyncId; |
| 6719 | } |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6720 | } |
| 6721 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6722 | mLooper->wake(); |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6723 | } |
| 6724 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6725 | bool InputDispatcher::shouldDropInput( |
| 6726 | const EventEntry& entry, const sp<android::gui::WindowInfoHandle>& windowHandle) const { |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6727 | if (windowHandle->getInfo()->inputConfig.test(WindowInfo::InputConfig::DROP_INPUT) || |
| 6728 | (windowHandle->getInfo()->inputConfig.test( |
| 6729 | WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED) && |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6730 | isWindowObscuredLocked(windowHandle))) { |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6731 | ALOGW("Dropping %s event targeting %s as requested by the input configuration {%s} on " |
| 6732 | "display %" PRId32 ".", |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6733 | ftl::enum_string(entry.type).c_str(), windowHandle->getName().c_str(), |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6734 | windowHandle->getInfo()->inputConfig.string().c_str(), |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6735 | windowHandle->getInfo()->displayId); |
| 6736 | return true; |
| 6737 | } |
| 6738 | return false; |
| 6739 | } |
| 6740 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 6741 | void InputDispatcher::DispatcherWindowListener::onWindowInfosChanged( |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 6742 | const gui::WindowInfosUpdate& update) { |
| 6743 | mDispatcher.onWindowInfosChanged(update); |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 6744 | } |
| 6745 | |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6746 | void InputDispatcher::cancelCurrentTouch() { |
| 6747 | { |
| 6748 | std::scoped_lock _l(mLock); |
| 6749 | ALOGD("Canceling all ongoing pointer gestures on all displays."); |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 6750 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6751 | "cancel current touch"); |
| 6752 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 6753 | |
| 6754 | mTouchStatesByDisplay.clear(); |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6755 | } |
| 6756 | // Wake up poll loop since there might be work to do. |
| 6757 | mLooper->wake(); |
| 6758 | } |
| 6759 | |
Prabir Pradhan | 87112a7 | 2023-04-20 19:13:39 +0000 | [diff] [blame] | 6760 | void InputDispatcher::requestRefreshConfiguration() { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6761 | InputDispatcherConfiguration config = mPolicy.getDispatcherConfiguration(); |
Prabir Pradhan | 87112a7 | 2023-04-20 19:13:39 +0000 | [diff] [blame] | 6762 | |
| 6763 | std::scoped_lock _l(mLock); |
| 6764 | mConfig = config; |
| 6765 | } |
| 6766 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6767 | void InputDispatcher::setMonitorDispatchingTimeoutForTest(std::chrono::nanoseconds timeout) { |
| 6768 | std::scoped_lock _l(mLock); |
| 6769 | mMonitorDispatchingTimeout = timeout; |
| 6770 | } |
| 6771 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6772 | void InputDispatcher::slipWallpaperTouch(ftl::Flags<InputTarget::Flags> targetFlags, |
| 6773 | const sp<WindowInfoHandle>& oldWindowHandle, |
| 6774 | const sp<WindowInfoHandle>& newWindowHandle, |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 6775 | TouchState& state, int32_t pointerId, |
Siarhei Vishniakou | bf88052 | 2023-05-01 11:03:22 -0700 | [diff] [blame] | 6776 | std::vector<InputTarget>& targets) const { |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 6777 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
| 6778 | pointerIds.set(pointerId); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6779 | const bool oldHasWallpaper = oldWindowHandle->getInfo()->inputConfig.test( |
| 6780 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER); |
| 6781 | const bool newHasWallpaper = targetFlags.test(InputTarget::Flags::FOREGROUND) && |
| 6782 | newWindowHandle->getInfo()->inputConfig.test( |
| 6783 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER); |
| 6784 | const sp<WindowInfoHandle> oldWallpaper = |
| 6785 | oldHasWallpaper ? state.getWallpaperWindow() : nullptr; |
| 6786 | const sp<WindowInfoHandle> newWallpaper = |
| 6787 | newHasWallpaper ? findWallpaperWindowBelow(newWindowHandle) : nullptr; |
| 6788 | if (oldWallpaper == newWallpaper) { |
| 6789 | return; |
| 6790 | } |
| 6791 | |
| 6792 | if (oldWallpaper != nullptr) { |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 6793 | const TouchedWindow& oldTouchedWindow = state.getTouchedWindow(oldWallpaper); |
| 6794 | addWindowTargetLocked(oldWallpaper, |
| 6795 | oldTouchedWindow.targetFlags | |
| 6796 | InputTarget::Flags::DISPATCH_AS_SLIPPERY_EXIT, |
| 6797 | pointerIds, oldTouchedWindow.firstDownTimeInTarget, targets); |
| 6798 | state.removeTouchedPointerFromWindow(pointerId, oldWallpaper); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6799 | } |
| 6800 | |
| 6801 | if (newWallpaper != nullptr) { |
| 6802 | state.addOrUpdateWindow(newWallpaper, |
| 6803 | InputTarget::Flags::DISPATCH_AS_SLIPPERY_ENTER | |
| 6804 | InputTarget::Flags::WINDOW_IS_OBSCURED | |
| 6805 | InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED, |
| 6806 | pointerIds); |
| 6807 | } |
| 6808 | } |
| 6809 | |
| 6810 | void InputDispatcher::transferWallpaperTouch(ftl::Flags<InputTarget::Flags> oldTargetFlags, |
| 6811 | ftl::Flags<InputTarget::Flags> newTargetFlags, |
| 6812 | const sp<WindowInfoHandle> fromWindowHandle, |
| 6813 | const sp<WindowInfoHandle> toWindowHandle, |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 6814 | TouchState& state, |
| 6815 | std::bitset<MAX_POINTER_ID + 1> pointerIds) { |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6816 | const bool oldHasWallpaper = oldTargetFlags.test(InputTarget::Flags::FOREGROUND) && |
| 6817 | fromWindowHandle->getInfo()->inputConfig.test( |
| 6818 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER); |
| 6819 | const bool newHasWallpaper = newTargetFlags.test(InputTarget::Flags::FOREGROUND) && |
| 6820 | toWindowHandle->getInfo()->inputConfig.test( |
| 6821 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER); |
| 6822 | |
| 6823 | const sp<WindowInfoHandle> oldWallpaper = |
| 6824 | oldHasWallpaper ? state.getWallpaperWindow() : nullptr; |
| 6825 | const sp<WindowInfoHandle> newWallpaper = |
| 6826 | newHasWallpaper ? findWallpaperWindowBelow(toWindowHandle) : nullptr; |
| 6827 | if (oldWallpaper == newWallpaper) { |
| 6828 | return; |
| 6829 | } |
| 6830 | |
| 6831 | if (oldWallpaper != nullptr) { |
| 6832 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
| 6833 | "transferring touch focus to another window"); |
| 6834 | state.removeWindowByToken(oldWallpaper->getToken()); |
| 6835 | synthesizeCancelationEventsForWindowLocked(oldWallpaper, options); |
| 6836 | } |
| 6837 | |
| 6838 | if (newWallpaper != nullptr) { |
| 6839 | nsecs_t downTimeInTarget = now(); |
| 6840 | ftl::Flags<InputTarget::Flags> wallpaperFlags = |
| 6841 | oldTargetFlags & (InputTarget::Flags::SPLIT | InputTarget::Flags::DISPATCH_AS_IS); |
| 6842 | wallpaperFlags |= InputTarget::Flags::WINDOW_IS_OBSCURED | |
| 6843 | InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED; |
| 6844 | state.addOrUpdateWindow(newWallpaper, wallpaperFlags, pointerIds, downTimeInTarget); |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6845 | std::shared_ptr<Connection> wallpaperConnection = |
| 6846 | getConnectionLocked(newWallpaper->getToken()); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6847 | if (wallpaperConnection != nullptr) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6848 | std::shared_ptr<Connection> toConnection = |
| 6849 | getConnectionLocked(toWindowHandle->getToken()); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6850 | toConnection->inputState.mergePointerStateTo(wallpaperConnection->inputState); |
| 6851 | synthesizePointerDownEventsForConnectionLocked(downTimeInTarget, wallpaperConnection, |
| 6852 | wallpaperFlags); |
| 6853 | } |
| 6854 | } |
| 6855 | } |
| 6856 | |
| 6857 | sp<WindowInfoHandle> InputDispatcher::findWallpaperWindowBelow( |
| 6858 | const sp<WindowInfoHandle>& windowHandle) const { |
| 6859 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
| 6860 | getWindowHandlesLocked(windowHandle->getInfo()->displayId); |
| 6861 | bool foundWindow = false; |
| 6862 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
| 6863 | if (!foundWindow && otherHandle != windowHandle) { |
| 6864 | continue; |
| 6865 | } |
| 6866 | if (windowHandle == otherHandle) { |
| 6867 | foundWindow = true; |
| 6868 | continue; |
| 6869 | } |
| 6870 | |
| 6871 | if (otherHandle->getInfo()->inputConfig.test(WindowInfo::InputConfig::IS_WALLPAPER)) { |
| 6872 | return otherHandle; |
| 6873 | } |
| 6874 | } |
| 6875 | return nullptr; |
| 6876 | } |
| 6877 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 6878 | } // namespace android::inputdispatcher |