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) { |
| 501 | case EventEntry::Type::FOCUS: |
| 502 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 503 | case EventEntry::Type::DRAG: |
| 504 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
| 505 | case EventEntry::Type::SENSOR: |
| 506 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 507 | return false; |
| 508 | case EventEntry::Type::DEVICE_RESET: |
| 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 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 684 | } // namespace |
| 685 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 686 | // --- InputDispatcher --- |
| 687 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 688 | InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy) |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 689 | : InputDispatcher(policy, STALE_EVENT_TIMEOUT) {} |
| 690 | |
| 691 | InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy, |
| 692 | std::chrono::nanoseconds staleEventTimeout) |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 693 | : mPolicy(policy), |
| 694 | mPendingEvent(nullptr), |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 695 | mLastDropReason(DropReason::NOT_DROPPED), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 696 | mIdGenerator(IdGenerator::Source::INPUT_DISPATCHER), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 697 | mAppSwitchSawKeyDown(false), |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 698 | mAppSwitchDueTime(LLONG_MAX), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 699 | mNextUnblockedEvent(nullptr), |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 700 | mMonitorDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 701 | mDispatchEnabled(false), |
| 702 | mDispatchFrozen(false), |
| 703 | mInputFilterEnabled(false), |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 704 | mMaximumObscuringOpacityForTouch(1.0f), |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 705 | mFocusedDisplayId(ADISPLAY_ID_DEFAULT), |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 706 | mWindowTokenWithPointerCapture(nullptr), |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 707 | mStaleEventTimeout(staleEventTimeout), |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 708 | mLatencyAggregator(), |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 709 | mLatencyTracker(&mLatencyAggregator) { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 710 | mLooper = sp<Looper>::make(false); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 711 | mReporter = createInputReporter(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 712 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 713 | mWindowInfoListener = sp<DispatcherWindowListener>::make(*this); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 714 | #if defined(__ANDROID__) |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 715 | SurfaceComposerClient::getDefault()->addWindowInfosListener(mWindowInfoListener); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 716 | #endif |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 717 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | InputDispatcher::~InputDispatcher() { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 721 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 722 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 723 | resetKeyRepeatLocked(); |
| 724 | releasePendingEventLocked(); |
| 725 | drainInboundQueueLocked(); |
| 726 | mCommandQueue.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 727 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 728 | while (!mConnectionsByToken.empty()) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 729 | std::shared_ptr<Connection> connection = mConnectionsByToken.begin()->second; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 730 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), /*notify=*/false); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 731 | } |
| 732 | } |
| 733 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 734 | status_t InputDispatcher::start() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 735 | if (mThread) { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 736 | return ALREADY_EXISTS; |
| 737 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 738 | mThread = std::make_unique<InputThread>( |
| 739 | "InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); }); |
| 740 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | status_t InputDispatcher::stop() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 744 | if (mThread && mThread->isCallingThread()) { |
| 745 | ALOGE("InputDispatcher cannot be stopped from its own thread!"); |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 746 | return INVALID_OPERATION; |
| 747 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 748 | mThread.reset(); |
| 749 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 750 | } |
| 751 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 752 | void InputDispatcher::dispatchOnce() { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 753 | nsecs_t nextWakeupTime = LLONG_MAX; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 754 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 755 | std::scoped_lock _l(mLock); |
| 756 | mDispatcherIsAlive.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 757 | |
| 758 | // Run a dispatch loop if there are no pending commands. |
| 759 | // The dispatch loop might enqueue commands to run afterwards. |
| 760 | if (!haveCommandsLocked()) { |
| 761 | dispatchOnceInnerLocked(&nextWakeupTime); |
| 762 | } |
| 763 | |
| 764 | // Run all pending commands if there are any. |
| 765 | // 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] | 766 | if (runCommandsLockedInterruptable()) { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 767 | nextWakeupTime = LLONG_MIN; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 768 | } |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 769 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 770 | // If we are still waiting for ack on some events, |
| 771 | // we might have to wake up earlier to check if an app is anr'ing. |
| 772 | const nsecs_t nextAnrCheck = processAnrsLocked(); |
| 773 | nextWakeupTime = std::min(nextWakeupTime, nextAnrCheck); |
| 774 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 775 | // We are about to enter an infinitely long sleep, because we have no commands or |
| 776 | // pending or queued events |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 777 | if (nextWakeupTime == LLONG_MAX) { |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 778 | mDispatcherEnteredIdle.notify_all(); |
| 779 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 780 | } // release lock |
| 781 | |
| 782 | // Wait for callback or timeout or wake. (make sure we round up, not down) |
| 783 | nsecs_t currentTime = now(); |
| 784 | int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime); |
| 785 | mLooper->pollOnce(timeoutMillis); |
| 786 | } |
| 787 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 788 | /** |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 789 | * Raise ANR if there is no focused window. |
| 790 | * Before the ANR is raised, do a final state check: |
| 791 | * 1. The currently focused application must be the same one we are waiting for. |
| 792 | * 2. Ensure we still don't have a focused window. |
| 793 | */ |
| 794 | void InputDispatcher::processNoFocusedWindowAnrLocked() { |
| 795 | // Check if the application that we are waiting for is still focused. |
| 796 | std::shared_ptr<InputApplicationHandle> focusedApplication = |
| 797 | getValueByKey(mFocusedApplicationHandlesByDisplay, mAwaitedApplicationDisplayId); |
| 798 | if (focusedApplication == nullptr || |
| 799 | focusedApplication->getApplicationToken() != |
| 800 | mAwaitedFocusedApplication->getApplicationToken()) { |
| 801 | // Unexpected because we should have reset the ANR timer when focused application changed |
| 802 | ALOGE("Waited for a focused window, but focused application has already changed to %s", |
| 803 | focusedApplication->getName().c_str()); |
| 804 | return; // The focused application has changed. |
| 805 | } |
| 806 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 807 | const sp<WindowInfoHandle>& focusedWindowHandle = |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 808 | getFocusedWindowHandleLocked(mAwaitedApplicationDisplayId); |
| 809 | if (focusedWindowHandle != nullptr) { |
| 810 | return; // We now have a focused window. No need for ANR. |
| 811 | } |
| 812 | onAnrLocked(mAwaitedFocusedApplication); |
| 813 | } |
| 814 | |
| 815 | /** |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 816 | * Check if any of the connections' wait queues have events that are too old. |
| 817 | * If we waited for events to be ack'ed for more than the window timeout, raise an ANR. |
| 818 | * Return the time at which we should wake up next. |
| 819 | */ |
| 820 | nsecs_t InputDispatcher::processAnrsLocked() { |
| 821 | const nsecs_t currentTime = now(); |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 822 | nsecs_t nextAnrCheck = LLONG_MAX; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 823 | // Check if we are waiting for a focused window to appear. Raise ANR if waited too long |
| 824 | if (mNoFocusedWindowTimeoutTime.has_value() && mAwaitedFocusedApplication != nullptr) { |
| 825 | if (currentTime >= *mNoFocusedWindowTimeoutTime) { |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 826 | processNoFocusedWindowAnrLocked(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 827 | mAwaitedFocusedApplication.reset(); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 828 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 829 | return LLONG_MIN; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 830 | } else { |
Siarhei Vishniakou | 38a6d27 | 2020-10-20 20:29:33 -0500 | [diff] [blame] | 831 | // Keep waiting. We will drop the event when mNoFocusedWindowTimeoutTime comes. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 832 | nextAnrCheck = *mNoFocusedWindowTimeoutTime; |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | // Check if any connection ANRs are due |
| 837 | nextAnrCheck = std::min(nextAnrCheck, mAnrTracker.firstTimeout()); |
| 838 | if (currentTime < nextAnrCheck) { // most likely scenario |
| 839 | return nextAnrCheck; // everything is normal. Let's check again at nextAnrCheck |
| 840 | } |
| 841 | |
| 842 | // If we reached here, we have an unresponsive connection. |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 843 | std::shared_ptr<Connection> connection = getConnectionLocked(mAnrTracker.firstToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 844 | if (connection == nullptr) { |
| 845 | ALOGE("Could not find connection for entry %" PRId64, mAnrTracker.firstTimeout()); |
| 846 | return nextAnrCheck; |
| 847 | } |
| 848 | connection->responsive = false; |
| 849 | // Stop waking up for this unresponsive connection |
| 850 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 851 | onAnrLocked(connection); |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 852 | return LLONG_MIN; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 853 | } |
| 854 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 855 | std::chrono::nanoseconds InputDispatcher::getDispatchingTimeoutLocked( |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 856 | const std::shared_ptr<Connection>& connection) { |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 857 | if (connection->monitor) { |
| 858 | return mMonitorDispatchingTimeout; |
| 859 | } |
| 860 | const sp<WindowInfoHandle> window = |
| 861 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 862 | if (window != nullptr) { |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 863 | return window->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 864 | } |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 865 | return DEFAULT_INPUT_DISPATCHING_TIMEOUT; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 866 | } |
| 867 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 868 | void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) { |
| 869 | nsecs_t currentTime = now(); |
| 870 | |
Jeff Brown | dc5992e | 2014-04-11 01:27:26 -0700 | [diff] [blame] | 871 | // Reset the key repeat timer whenever normal dispatch is suspended while the |
| 872 | // device is in a non-interactive state. This is to ensure that we abort a key |
| 873 | // repeat if the device is just coming out of sleep. |
| 874 | if (!mDispatchEnabled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 875 | resetKeyRepeatLocked(); |
| 876 | } |
| 877 | |
| 878 | // If dispatching is frozen, do not process timeouts or try to deliver any new events. |
| 879 | if (mDispatchFrozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 880 | if (DEBUG_FOCUS) { |
| 881 | ALOGD("Dispatch frozen. Waiting some more."); |
| 882 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 883 | return; |
| 884 | } |
| 885 | |
| 886 | // Optimize latency of app switches. |
| 887 | // Essentially we start a short timeout when an app switch key (HOME / ENDCALL) has |
| 888 | // been pressed. When it expires, we preempt dispatch and drop all other pending events. |
| 889 | bool isAppSwitchDue = mAppSwitchDueTime <= currentTime; |
| 890 | if (mAppSwitchDueTime < *nextWakeupTime) { |
| 891 | *nextWakeupTime = mAppSwitchDueTime; |
| 892 | } |
| 893 | |
| 894 | // Ready to start a new event. |
| 895 | // If we don't already have a pending event, go grab one. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 896 | if (!mPendingEvent) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 897 | if (mInboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 898 | if (isAppSwitchDue) { |
| 899 | // The inbound queue is empty so the app switch key we were waiting |
| 900 | // for will never arrive. Stop waiting for it. |
| 901 | resetPendingAppSwitchLocked(false); |
| 902 | isAppSwitchDue = false; |
| 903 | } |
| 904 | |
| 905 | // Synthesize a key repeat if appropriate. |
| 906 | if (mKeyRepeatState.lastKeyEntry) { |
| 907 | if (currentTime >= mKeyRepeatState.nextRepeatTime) { |
| 908 | mPendingEvent = synthesizeKeyRepeatLocked(currentTime); |
| 909 | } else { |
| 910 | if (mKeyRepeatState.nextRepeatTime < *nextWakeupTime) { |
| 911 | *nextWakeupTime = mKeyRepeatState.nextRepeatTime; |
| 912 | } |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | // Nothing to do if there is no pending event. |
| 917 | if (!mPendingEvent) { |
| 918 | return; |
| 919 | } |
| 920 | } else { |
| 921 | // Inbound queue has at least one entry. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 922 | mPendingEvent = mInboundQueue.front(); |
| 923 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 924 | traceInboundQueueLengthLocked(); |
| 925 | } |
| 926 | |
| 927 | // Poke user activity for this event. |
| 928 | if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 929 | pokeUserActivityLocked(*mPendingEvent); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 930 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 931 | } |
| 932 | |
| 933 | // Now we have an event to dispatch. |
| 934 | // 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] | 935 | ALOG_ASSERT(mPendingEvent != nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 936 | bool done = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 937 | DropReason dropReason = DropReason::NOT_DROPPED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 938 | if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 939 | dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 940 | } else if (!mDispatchEnabled) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 941 | dropReason = DropReason::DISABLED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | if (mNextUnblockedEvent == mPendingEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 945 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | switch (mPendingEvent->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 949 | case EventEntry::Type::CONFIGURATION_CHANGED: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 950 | const ConfigurationChangedEntry& typedEntry = |
| 951 | static_cast<const ConfigurationChangedEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 952 | done = dispatchConfigurationChangedLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 953 | dropReason = DropReason::NOT_DROPPED; // configuration changes are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 954 | break; |
| 955 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 956 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 957 | case EventEntry::Type::DEVICE_RESET: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 958 | const DeviceResetEntry& typedEntry = |
| 959 | static_cast<const DeviceResetEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 960 | done = dispatchDeviceResetLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 961 | dropReason = DropReason::NOT_DROPPED; // device resets are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 962 | break; |
| 963 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 964 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 965 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 966 | std::shared_ptr<FocusEntry> typedEntry = |
| 967 | std::static_pointer_cast<FocusEntry>(mPendingEvent); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 968 | dispatchFocusLocked(currentTime, typedEntry); |
| 969 | done = true; |
| 970 | dropReason = DropReason::NOT_DROPPED; // focus events are never dropped |
| 971 | break; |
| 972 | } |
| 973 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 974 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 975 | const auto typedEntry = std::static_pointer_cast<TouchModeEntry>(mPendingEvent); |
| 976 | dispatchTouchModeChangeLocked(currentTime, typedEntry); |
| 977 | done = true; |
| 978 | dropReason = DropReason::NOT_DROPPED; // touch mode events are never dropped |
| 979 | break; |
| 980 | } |
| 981 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 982 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 983 | const auto typedEntry = |
| 984 | std::static_pointer_cast<PointerCaptureChangedEntry>(mPendingEvent); |
| 985 | dispatchPointerCaptureChangedLocked(currentTime, typedEntry, dropReason); |
| 986 | done = true; |
| 987 | break; |
| 988 | } |
| 989 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 990 | case EventEntry::Type::DRAG: { |
| 991 | std::shared_ptr<DragEntry> typedEntry = |
| 992 | std::static_pointer_cast<DragEntry>(mPendingEvent); |
| 993 | dispatchDragLocked(currentTime, typedEntry); |
| 994 | done = true; |
| 995 | break; |
| 996 | } |
| 997 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 998 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 999 | std::shared_ptr<KeyEntry> keyEntry = std::static_pointer_cast<KeyEntry>(mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1000 | if (isAppSwitchDue) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1001 | if (isAppSwitchKeyEvent(*keyEntry)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1002 | resetPendingAppSwitchLocked(true); |
| 1003 | isAppSwitchDue = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1004 | } else if (dropReason == DropReason::NOT_DROPPED) { |
| 1005 | dropReason = DropReason::APP_SWITCH; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1006 | } |
| 1007 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1008 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *keyEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1009 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1010 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1011 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 1012 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1013 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1014 | done = dispatchKeyLocked(currentTime, keyEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1015 | break; |
| 1016 | } |
| 1017 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1018 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1019 | std::shared_ptr<MotionEntry> motionEntry = |
| 1020 | std::static_pointer_cast<MotionEntry>(mPendingEvent); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1021 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 1022 | dropReason = DropReason::APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1023 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1024 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *motionEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1025 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1026 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1027 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 1028 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1029 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1030 | done = dispatchMotionLocked(currentTime, motionEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1031 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1032 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1033 | |
| 1034 | case EventEntry::Type::SENSOR: { |
| 1035 | std::shared_ptr<SensorEntry> sensorEntry = |
| 1036 | std::static_pointer_cast<SensorEntry>(mPendingEvent); |
| 1037 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 1038 | dropReason = DropReason::APP_SWITCH; |
| 1039 | } |
| 1040 | // Sensor timestamps use SYSTEM_TIME_BOOTTIME time base, so we can't use |
| 1041 | // 'currentTime' here, get SYSTEM_TIME_BOOTTIME instead. |
| 1042 | nsecs_t bootTime = systemTime(SYSTEM_TIME_BOOTTIME); |
| 1043 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(bootTime, *sensorEntry)) { |
| 1044 | dropReason = DropReason::STALE; |
| 1045 | } |
| 1046 | dispatchSensorLocked(currentTime, sensorEntry, &dropReason, nextWakeupTime); |
| 1047 | done = true; |
| 1048 | break; |
| 1049 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | if (done) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1053 | if (dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1054 | dropInboundEventLocked(*mPendingEvent, dropReason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1055 | } |
Michael Wright | 3a98172 | 2015-06-10 15:26:13 +0100 | [diff] [blame] | 1056 | mLastDropReason = dropReason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1057 | |
| 1058 | releasePendingEventLocked(); |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 1059 | *nextWakeupTime = LLONG_MIN; // force next poll to wake up immediately |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1060 | } |
| 1061 | } |
| 1062 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 1063 | bool InputDispatcher::isStaleEvent(nsecs_t currentTime, const EventEntry& entry) { |
| 1064 | return std::chrono::nanoseconds(currentTime - entry.eventTime) >= mStaleEventTimeout; |
| 1065 | } |
| 1066 | |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1067 | /** |
| 1068 | * Return true if the events preceding this incoming motion event should be dropped |
| 1069 | * Return false otherwise (the default behaviour) |
| 1070 | */ |
| 1071 | bool InputDispatcher::shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1072 | const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN && |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 1073 | isFromSource(motionEntry.source, AINPUT_SOURCE_CLASS_POINTER); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1074 | |
| 1075 | // Optimize case where the current application is unresponsive and the user |
| 1076 | // decides to touch a window in a different application. |
| 1077 | // If the application takes too long to catch up then we drop all events preceding |
| 1078 | // the touch into the other window. |
| 1079 | if (isPointerDownEvent && mAwaitedFocusedApplication != nullptr) { |
Siarhei Vishniakou | 9306c38 | 2022-09-30 15:30:31 -0700 | [diff] [blame] | 1080 | const int32_t displayId = motionEntry.displayId; |
| 1081 | const auto [x, y] = resolveTouchedPosition(motionEntry); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1082 | const bool isStylus = isPointerFromStylus(motionEntry, /*pointerIndex=*/0); |
Siarhei Vishniakou | 9306c38 | 2022-09-30 15:30:31 -0700 | [diff] [blame] | 1083 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1084 | auto [touchedWindowHandle, _] = findTouchedWindowAtLocked(displayId, x, y, isStylus); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1085 | if (touchedWindowHandle != nullptr && |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1086 | touchedWindowHandle->getApplicationToken() != |
| 1087 | mAwaitedFocusedApplication->getApplicationToken()) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1088 | // User touched a different application than the one we are waiting on. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1089 | ALOGI("Pruning input queue because user touched a different application while waiting " |
| 1090 | "for %s", |
| 1091 | mAwaitedFocusedApplication->getName().c_str()); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1092 | return true; |
| 1093 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1094 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1095 | // Alternatively, maybe there's a spy window that could handle this event. |
| 1096 | const std::vector<sp<WindowInfoHandle>> touchedSpies = |
| 1097 | findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus); |
| 1098 | for (const auto& windowHandle : touchedSpies) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 1099 | const std::shared_ptr<Connection> connection = |
| 1100 | getConnectionLocked(windowHandle->getToken()); |
Siarhei Vishniakou | 34ed4d4 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1101 | if (connection != nullptr && connection->responsive) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1102 | // This spy window could take more input. Drop all events preceding this |
| 1103 | // 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] | 1104 | 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] | 1105 | "responsive spy window that may handle the event.", |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1106 | mAwaitedFocusedApplication->getName().c_str()); |
| 1107 | return true; |
| 1108 | } |
| 1109 | } |
| 1110 | } |
| 1111 | |
| 1112 | // Prevent getting stuck: if we have a pending key event, and some motion events that have not |
| 1113 | // yet been processed by some connections, the dispatcher will wait for these motion |
| 1114 | // events to be processed before dispatching the key event. This is because these motion events |
| 1115 | // may cause a new window to be launched, which the user might expect to receive focus. |
| 1116 | // To prevent waiting forever for such events, just send the key to the currently focused window |
| 1117 | if (isPointerDownEvent && mKeyIsWaitingForEventsTimeout) { |
| 1118 | ALOGD("Received a new pointer down event, stop waiting for events to process and " |
| 1119 | "just send the pending key event to the focused window."); |
| 1120 | mKeyIsWaitingForEventsTimeout = now(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1121 | } |
| 1122 | return false; |
| 1123 | } |
| 1124 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1125 | bool InputDispatcher::enqueueInboundEventLocked(std::unique_ptr<EventEntry> newEntry) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1126 | bool needWake = mInboundQueue.empty(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1127 | mInboundQueue.push_back(std::move(newEntry)); |
| 1128 | EventEntry& entry = *(mInboundQueue.back()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1129 | traceInboundQueueLengthLocked(); |
| 1130 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1131 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1132 | case EventEntry::Type::KEY: { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1133 | LOG_ALWAYS_FATAL_IF((entry.policyFlags & POLICY_FLAG_TRUSTED) == 0, |
| 1134 | "Unexpected untrusted event."); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1135 | // Optimize app switch latency. |
| 1136 | // If the application takes too long to catch up then we drop all events preceding |
| 1137 | // the app switch key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1138 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1139 | if (isAppSwitchKeyEvent(keyEntry)) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1140 | if (keyEntry.action == AKEY_EVENT_ACTION_DOWN) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1141 | mAppSwitchSawKeyDown = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1142 | } else if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1143 | if (mAppSwitchSawKeyDown) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1144 | if (DEBUG_APP_SWITCH) { |
| 1145 | ALOGD("App switch is pending!"); |
| 1146 | } |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1147 | mAppSwitchDueTime = keyEntry.eventTime + APP_SWITCH_TIMEOUT; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1148 | mAppSwitchSawKeyDown = false; |
| 1149 | needWake = true; |
| 1150 | } |
| 1151 | } |
| 1152 | } |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 1153 | |
| 1154 | // If a new up event comes in, and the pending event with same key code has been asked |
| 1155 | // to try again later because of the policy. We have to reset the intercept key wake up |
| 1156 | // time for it may have been handled in the policy and could be dropped. |
| 1157 | if (keyEntry.action == AKEY_EVENT_ACTION_UP && mPendingEvent && |
| 1158 | mPendingEvent->type == EventEntry::Type::KEY) { |
| 1159 | KeyEntry& pendingKey = static_cast<KeyEntry&>(*mPendingEvent); |
| 1160 | if (pendingKey.keyCode == keyEntry.keyCode && |
| 1161 | pendingKey.interceptKeyResult == |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1162 | KeyEntry::InterceptKeyResult::TRY_AGAIN_LATER) { |
| 1163 | pendingKey.interceptKeyResult = KeyEntry::InterceptKeyResult::UNKNOWN; |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 1164 | pendingKey.interceptKeyWakeupTime = 0; |
| 1165 | needWake = true; |
| 1166 | } |
| 1167 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1168 | break; |
| 1169 | } |
| 1170 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1171 | case EventEntry::Type::MOTION: { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1172 | LOG_ALWAYS_FATAL_IF((entry.policyFlags & POLICY_FLAG_TRUSTED) == 0, |
| 1173 | "Unexpected untrusted event."); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1174 | if (shouldPruneInboundQueueLocked(static_cast<MotionEntry&>(entry))) { |
| 1175 | mNextUnblockedEvent = mInboundQueue.back(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1176 | needWake = true; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1177 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1178 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1179 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1180 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1181 | LOG_ALWAYS_FATAL("Focus events should be inserted using enqueueFocusEventLocked"); |
| 1182 | break; |
| 1183 | } |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1184 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1185 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1186 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1187 | case EventEntry::Type::SENSOR: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1188 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1189 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1190 | // nothing to do |
| 1191 | break; |
| 1192 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | return needWake; |
| 1196 | } |
| 1197 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1198 | void InputDispatcher::addRecentEventLocked(std::shared_ptr<EventEntry> entry) { |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1199 | // Do not store sensor event in recent queue to avoid flooding the queue. |
| 1200 | if (entry->type != EventEntry::Type::SENSOR) { |
| 1201 | mRecentQueue.push_back(entry); |
| 1202 | } |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1203 | if (mRecentQueue.size() > RECENT_QUEUE_MAX_SIZE) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1204 | mRecentQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1205 | } |
| 1206 | } |
| 1207 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1208 | std::pair<sp<WindowInfoHandle>, std::vector<InputTarget>> |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 1209 | InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, float x, float y, bool isStylus, |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1210 | bool ignoreDragWindow) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1211 | // Traverse windows from front to back to find touched window. |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1212 | std::vector<InputTarget> outsideTargets; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1213 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1214 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 1215 | if (ignoreDragWindow && haveSameToken(windowHandle, mDragState->dragWindow)) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1216 | continue; |
| 1217 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1218 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1219 | const WindowInfo& info = *windowHandle->getInfo(); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 1220 | if (!info.isSpy() && |
| 1221 | windowAcceptsTouchAt(info, displayId, x, y, isStylus, getTransformLocked(displayId))) { |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1222 | return {windowHandle, outsideTargets}; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1223 | } |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1224 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1225 | if (info.inputConfig.test(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH)) { |
| 1226 | addWindowTargetLocked(windowHandle, InputTarget::Flags::DISPATCH_AS_OUTSIDE, |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 1227 | /*pointerIds=*/{}, /*firstDownTimeInTarget=*/std::nullopt, |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1228 | outsideTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1229 | } |
| 1230 | } |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1231 | return {nullptr, {}}; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1232 | } |
| 1233 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1234 | std::vector<sp<WindowInfoHandle>> InputDispatcher::findTouchedSpyWindowsAtLocked( |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 1235 | int32_t displayId, float x, float y, bool isStylus) const { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1236 | // Traverse windows from front to back and gather the touched spy windows. |
| 1237 | std::vector<sp<WindowInfoHandle>> spyWindows; |
| 1238 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
| 1239 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 1240 | const WindowInfo& info = *windowHandle->getInfo(); |
| 1241 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 1242 | if (!windowAcceptsTouchAt(info, displayId, x, y, isStylus, getTransformLocked(displayId))) { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1243 | continue; |
| 1244 | } |
| 1245 | if (!info.isSpy()) { |
| 1246 | // The first touched non-spy window was found, so return the spy windows touched so far. |
| 1247 | return spyWindows; |
| 1248 | } |
| 1249 | spyWindows.push_back(windowHandle); |
| 1250 | } |
| 1251 | return spyWindows; |
| 1252 | } |
| 1253 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1254 | void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1255 | const char* reason; |
| 1256 | switch (dropReason) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1257 | case DropReason::POLICY: |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 1258 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1259 | ALOGD("Dropped event because policy consumed it."); |
| 1260 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1261 | reason = "inbound event was dropped because the policy consumed it"; |
| 1262 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1263 | case DropReason::DISABLED: |
| 1264 | if (mLastDropReason != DropReason::DISABLED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1265 | ALOGI("Dropped event because input dispatch is disabled."); |
| 1266 | } |
| 1267 | reason = "inbound event was dropped because input dispatch is disabled"; |
| 1268 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1269 | case DropReason::APP_SWITCH: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1270 | ALOGI("Dropped event because of pending overdue app switch."); |
| 1271 | reason = "inbound event was dropped because of pending overdue app switch"; |
| 1272 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1273 | case DropReason::BLOCKED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1274 | ALOGI("Dropped event because the current application is not responding and the user " |
| 1275 | "has started interacting with a different application."); |
| 1276 | reason = "inbound event was dropped because the current application is not responding " |
| 1277 | "and the user has started interacting with a different application"; |
| 1278 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1279 | case DropReason::STALE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1280 | ALOGI("Dropped event because it is stale."); |
| 1281 | reason = "inbound event was dropped because it is stale"; |
| 1282 | break; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1283 | case DropReason::NO_POINTER_CAPTURE: |
| 1284 | ALOGI("Dropped event because there is no window with Pointer Capture."); |
| 1285 | reason = "inbound event was dropped because there is no window with Pointer Capture"; |
| 1286 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1287 | case DropReason::NOT_DROPPED: { |
| 1288 | LOG_ALWAYS_FATAL("Should not be dropping a NOT_DROPPED event"); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1289 | return; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1290 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1291 | } |
| 1292 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1293 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1294 | case EventEntry::Type::KEY: { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1295 | CancelationOptions options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, reason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1296 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1297 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1298 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1299 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1300 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1301 | if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1302 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, reason); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1303 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1304 | } else { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1305 | CancelationOptions options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, |
| 1306 | reason); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1307 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1308 | } |
| 1309 | break; |
| 1310 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1311 | case EventEntry::Type::SENSOR: { |
| 1312 | break; |
| 1313 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1314 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1315 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1316 | break; |
| 1317 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1318 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1319 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1320 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 1321 | case EventEntry::Type::DEVICE_RESET: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1322 | 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] | 1323 | break; |
| 1324 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1325 | } |
| 1326 | } |
| 1327 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1328 | static bool isAppSwitchKeyCode(int32_t keyCode) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1329 | return keyCode == AKEYCODE_HOME || keyCode == AKEYCODE_ENDCALL || |
| 1330 | keyCode == AKEYCODE_APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1331 | } |
| 1332 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1333 | bool InputDispatcher::isAppSwitchKeyEvent(const KeyEntry& keyEntry) { |
| 1334 | return !(keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) && isAppSwitchKeyCode(keyEntry.keyCode) && |
| 1335 | (keyEntry.policyFlags & POLICY_FLAG_TRUSTED) && |
| 1336 | (keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1337 | } |
| 1338 | |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 1339 | bool InputDispatcher::isAppSwitchPendingLocked() const { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 1340 | return mAppSwitchDueTime != LLONG_MAX; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1341 | } |
| 1342 | |
| 1343 | void InputDispatcher::resetPendingAppSwitchLocked(bool handled) { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 1344 | mAppSwitchDueTime = LLONG_MAX; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1345 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1346 | if (DEBUG_APP_SWITCH) { |
| 1347 | if (handled) { |
| 1348 | ALOGD("App switch has arrived."); |
| 1349 | } else { |
| 1350 | ALOGD("App switch was abandoned."); |
| 1351 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1352 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1353 | } |
| 1354 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1355 | bool InputDispatcher::haveCommandsLocked() const { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1356 | return !mCommandQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1357 | } |
| 1358 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1359 | bool InputDispatcher::runCommandsLockedInterruptable() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1360 | if (mCommandQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1361 | return false; |
| 1362 | } |
| 1363 | |
| 1364 | do { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1365 | auto command = std::move(mCommandQueue.front()); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1366 | mCommandQueue.pop_front(); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1367 | // Commands are run with the lock held, but may release and re-acquire the lock from within. |
| 1368 | command(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1369 | } while (!mCommandQueue.empty()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1370 | return true; |
| 1371 | } |
| 1372 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1373 | void InputDispatcher::postCommandLocked(Command&& command) { |
| 1374 | mCommandQueue.push_back(command); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1375 | } |
| 1376 | |
| 1377 | void InputDispatcher::drainInboundQueueLocked() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1378 | while (!mInboundQueue.empty()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1379 | std::shared_ptr<EventEntry> entry = mInboundQueue.front(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1380 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1381 | releaseInboundEventLocked(entry); |
| 1382 | } |
| 1383 | traceInboundQueueLengthLocked(); |
| 1384 | } |
| 1385 | |
| 1386 | void InputDispatcher::releasePendingEventLocked() { |
| 1387 | if (mPendingEvent) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1388 | releaseInboundEventLocked(mPendingEvent); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1389 | mPendingEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1390 | } |
| 1391 | } |
| 1392 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1393 | void InputDispatcher::releaseInboundEventLocked(std::shared_ptr<EventEntry> entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1394 | InjectionState* injectionState = entry->injectionState; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1395 | if (injectionState && injectionState->injectionResult == InputEventInjectionResult::PENDING) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1396 | if (DEBUG_DISPATCH_CYCLE) { |
| 1397 | ALOGD("Injected inbound event was dropped."); |
| 1398 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1399 | setInjectionResult(*entry, InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1400 | } |
| 1401 | if (entry == mNextUnblockedEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1402 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1403 | } |
| 1404 | addRecentEventLocked(entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1405 | } |
| 1406 | |
| 1407 | void InputDispatcher::resetKeyRepeatLocked() { |
| 1408 | if (mKeyRepeatState.lastKeyEntry) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1409 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1410 | } |
| 1411 | } |
| 1412 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1413 | std::shared_ptr<KeyEntry> InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) { |
| 1414 | std::shared_ptr<KeyEntry> entry = mKeyRepeatState.lastKeyEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1415 | |
Michael Wright | 2e73295 | 2014-09-24 13:26:59 -0700 | [diff] [blame] | 1416 | uint32_t policyFlags = entry->policyFlags & |
| 1417 | (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1418 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1419 | std::shared_ptr<KeyEntry> newEntry = |
| 1420 | std::make_unique<KeyEntry>(mIdGenerator.nextId(), currentTime, entry->deviceId, |
| 1421 | entry->source, entry->displayId, policyFlags, entry->action, |
| 1422 | entry->flags, entry->keyCode, entry->scanCode, |
| 1423 | entry->metaState, entry->repeatCount + 1, entry->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1424 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1425 | newEntry->syntheticRepeat = true; |
| 1426 | mKeyRepeatState.lastKeyEntry = newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1427 | mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1428 | return newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1429 | } |
| 1430 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1431 | bool InputDispatcher::dispatchConfigurationChangedLocked(nsecs_t currentTime, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1432 | const ConfigurationChangedEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1433 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1434 | ALOGD("dispatchConfigurationChanged - eventTime=%" PRId64, entry.eventTime); |
| 1435 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1436 | |
| 1437 | // Reset key repeating in case a keyboard device was added or removed or something. |
| 1438 | resetKeyRepeatLocked(); |
| 1439 | |
| 1440 | // 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] | 1441 | auto command = [this, eventTime = entry.eventTime]() REQUIRES(mLock) { |
| 1442 | scoped_unlock unlock(mLock); |
| 1443 | mPolicy->notifyConfigurationChanged(eventTime); |
| 1444 | }; |
| 1445 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1446 | return true; |
| 1447 | } |
| 1448 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1449 | bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime, |
| 1450 | const DeviceResetEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1451 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1452 | ALOGD("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry.eventTime, |
| 1453 | entry.deviceId); |
| 1454 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1455 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 1456 | // Reset key repeating in case a keyboard device was disabled or enabled. |
| 1457 | if (mKeyRepeatState.lastKeyEntry && mKeyRepeatState.lastKeyEntry->deviceId == entry.deviceId) { |
| 1458 | resetKeyRepeatLocked(); |
| 1459 | } |
| 1460 | |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1461 | CancelationOptions options(CancelationOptions::Mode::CANCEL_ALL_EVENTS, "device was reset"); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1462 | options.deviceId = entry.deviceId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1463 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1464 | return true; |
| 1465 | } |
| 1466 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1467 | void InputDispatcher::enqueueFocusEventLocked(const sp<IBinder>& windowToken, bool hasFocus, |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1468 | const std::string& reason) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1469 | if (mPendingEvent != nullptr) { |
| 1470 | // Move the pending event to the front of the queue. This will give the chance |
| 1471 | // for the pending event to get dispatched to the newly focused window |
| 1472 | mInboundQueue.push_front(mPendingEvent); |
| 1473 | mPendingEvent = nullptr; |
| 1474 | } |
| 1475 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1476 | std::unique_ptr<FocusEntry> focusEntry = |
| 1477 | std::make_unique<FocusEntry>(mIdGenerator.nextId(), now(), windowToken, hasFocus, |
| 1478 | reason); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1479 | |
| 1480 | // This event should go to the front of the queue, but behind all other focus events |
| 1481 | // Find the last focus event, and insert right after it |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1482 | std::deque<std::shared_ptr<EventEntry>>::reverse_iterator it = |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1483 | std::find_if(mInboundQueue.rbegin(), mInboundQueue.rend(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1484 | [](const std::shared_ptr<EventEntry>& event) { |
| 1485 | return event->type == EventEntry::Type::FOCUS; |
| 1486 | }); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1487 | |
| 1488 | // 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] | 1489 | mInboundQueue.insert(it.base(), std::move(focusEntry)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1490 | } |
| 1491 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1492 | void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime, std::shared_ptr<FocusEntry> entry) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 1493 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1494 | if (channel == nullptr) { |
| 1495 | return; // Window has gone away |
| 1496 | } |
| 1497 | InputTarget target; |
| 1498 | target.inputChannel = channel; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 1499 | target.flags = InputTarget::Flags::DISPATCH_AS_IS; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1500 | entry->dispatchInProgress = true; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1501 | std::string message = std::string("Focus ") + (entry->hasFocus ? "entering " : "leaving ") + |
| 1502 | channel->getName(); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 1503 | std::string reason = std::string("reason=").append(entry->reason); |
| 1504 | android_log_event_list(LOGTAG_INPUT_FOCUS) << message << reason << LOG_ID_EVENTS; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1505 | dispatchEventLocked(currentTime, entry, {target}); |
| 1506 | } |
| 1507 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1508 | void InputDispatcher::dispatchPointerCaptureChangedLocked( |
| 1509 | nsecs_t currentTime, const std::shared_ptr<PointerCaptureChangedEntry>& entry, |
| 1510 | DropReason& dropReason) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1511 | dropReason = DropReason::NOT_DROPPED; |
| 1512 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1513 | const bool haveWindowWithPointerCapture = mWindowTokenWithPointerCapture != nullptr; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1514 | sp<IBinder> token; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1515 | |
| 1516 | if (entry->pointerCaptureRequest.enable) { |
| 1517 | // Enable Pointer Capture. |
| 1518 | if (haveWindowWithPointerCapture && |
| 1519 | (entry->pointerCaptureRequest == mCurrentPointerCaptureRequest)) { |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 1520 | // This can happen if pointer capture is disabled and re-enabled before we notify the |
| 1521 | // app of the state change, so there is no need to notify the app. |
| 1522 | ALOGI("Skipping dispatch of Pointer Capture being enabled: no state change."); |
| 1523 | return; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1524 | } |
| 1525 | if (!mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1526 | // This can happen if a window requests capture and immediately releases capture. |
| 1527 | ALOGW("No window requested Pointer Capture."); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1528 | dropReason = DropReason::NO_POINTER_CAPTURE; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1529 | return; |
| 1530 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1531 | if (entry->pointerCaptureRequest.seq != mCurrentPointerCaptureRequest.seq) { |
| 1532 | ALOGI("Skipping dispatch of Pointer Capture being enabled: sequence number mismatch."); |
| 1533 | return; |
| 1534 | } |
| 1535 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1536 | token = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1537 | LOG_ALWAYS_FATAL_IF(!token, "Cannot find focused window for Pointer Capture."); |
| 1538 | mWindowTokenWithPointerCapture = token; |
| 1539 | } else { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1540 | // Disable Pointer Capture. |
| 1541 | // We do not check if the sequence number matches for requests to disable Pointer Capture |
| 1542 | // for two reasons: |
| 1543 | // 1. Pointer Capture can be disabled by a focus change, which means we can get two entries |
| 1544 | // to disable capture with the same sequence number: one generated by |
| 1545 | // disablePointerCaptureForcedLocked() and another as an acknowledgement of Pointer |
| 1546 | // Capture being disabled in InputReader. |
| 1547 | // 2. We respect any request to disable Pointer Capture generated by InputReader, since the |
| 1548 | // actual Pointer Capture state that affects events being generated by input devices is |
| 1549 | // in InputReader. |
| 1550 | if (!haveWindowWithPointerCapture) { |
| 1551 | // Pointer capture was already forcefully disabled because of focus change. |
| 1552 | dropReason = DropReason::NOT_DROPPED; |
| 1553 | return; |
| 1554 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1555 | token = mWindowTokenWithPointerCapture; |
| 1556 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1557 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1558 | setPointerCaptureLocked(false); |
| 1559 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1560 | } |
| 1561 | |
| 1562 | auto channel = getInputChannelLocked(token); |
| 1563 | if (channel == nullptr) { |
| 1564 | // Window has gone away, clean up Pointer Capture state. |
| 1565 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1566 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1567 | setPointerCaptureLocked(false); |
| 1568 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1569 | return; |
| 1570 | } |
| 1571 | InputTarget target; |
| 1572 | target.inputChannel = channel; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 1573 | target.flags = InputTarget::Flags::DISPATCH_AS_IS; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1574 | entry->dispatchInProgress = true; |
| 1575 | dispatchEventLocked(currentTime, entry, {target}); |
| 1576 | |
| 1577 | dropReason = DropReason::NOT_DROPPED; |
| 1578 | } |
| 1579 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1580 | void InputDispatcher::dispatchTouchModeChangeLocked(nsecs_t currentTime, |
| 1581 | const std::shared_ptr<TouchModeEntry>& entry) { |
| 1582 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 1583 | getWindowHandlesLocked(entry->displayId); |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1584 | if (windowHandles.empty()) { |
| 1585 | return; |
| 1586 | } |
| 1587 | const std::vector<InputTarget> inputTargets = |
| 1588 | getInputTargetsFromWindowHandlesLocked(windowHandles); |
| 1589 | if (inputTargets.empty()) { |
| 1590 | return; |
| 1591 | } |
| 1592 | entry->dispatchInProgress = true; |
| 1593 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1594 | } |
| 1595 | |
| 1596 | std::vector<InputTarget> InputDispatcher::getInputTargetsFromWindowHandlesLocked( |
| 1597 | const std::vector<sp<WindowInfoHandle>>& windowHandles) const { |
| 1598 | std::vector<InputTarget> inputTargets; |
| 1599 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1600 | const sp<IBinder>& token = handle->getToken(); |
| 1601 | if (token == nullptr) { |
| 1602 | continue; |
| 1603 | } |
| 1604 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(token); |
| 1605 | if (channel == nullptr) { |
| 1606 | continue; // Window has gone away |
| 1607 | } |
| 1608 | InputTarget target; |
| 1609 | target.inputChannel = channel; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 1610 | target.flags = InputTarget::Flags::DISPATCH_AS_IS; |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1611 | inputTargets.push_back(target); |
| 1612 | } |
| 1613 | return inputTargets; |
| 1614 | } |
| 1615 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1616 | bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, std::shared_ptr<KeyEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1617 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1618 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1619 | if (!entry->dispatchInProgress) { |
| 1620 | if (entry->repeatCount == 0 && entry->action == AKEY_EVENT_ACTION_DOWN && |
| 1621 | (entry->policyFlags & POLICY_FLAG_TRUSTED) && |
| 1622 | (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { |
| 1623 | if (mKeyRepeatState.lastKeyEntry && |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1624 | mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode && |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1625 | // We have seen two identical key downs in a row which indicates that the device |
| 1626 | // driver is automatically generating key repeats itself. We take note of the |
| 1627 | // repeat here, but we disable our own next key repeat timer since it is clear that |
| 1628 | // we will not need to synthesize key repeats ourselves. |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1629 | mKeyRepeatState.lastKeyEntry->deviceId == entry->deviceId) { |
| 1630 | // Make sure we don't get key down from a different device. If a different |
| 1631 | // device Id has same key pressed down, the new device Id will replace the |
| 1632 | // current one to hold the key repeat with repeat count reset. |
| 1633 | // In the future when got a KEY_UP on the device id, drop it and do not |
| 1634 | // stop the key repeat on current device. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1635 | entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; |
| 1636 | resetKeyRepeatLocked(); |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 1637 | mKeyRepeatState.nextRepeatTime = LLONG_MAX; // don't generate repeats ourselves |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1638 | } else { |
| 1639 | // Not a repeat. Save key down state in case we do see a repeat later. |
| 1640 | resetKeyRepeatLocked(); |
| 1641 | mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; |
| 1642 | } |
| 1643 | mKeyRepeatState.lastKeyEntry = entry; |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1644 | } else if (entry->action == AKEY_EVENT_ACTION_UP && mKeyRepeatState.lastKeyEntry && |
| 1645 | mKeyRepeatState.lastKeyEntry->deviceId != entry->deviceId) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1646 | // 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] | 1647 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1648 | ALOGD("deviceId=%d got KEY_UP as stale", entry->deviceId); |
| 1649 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1650 | } else if (!entry->syntheticRepeat) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1651 | resetKeyRepeatLocked(); |
| 1652 | } |
| 1653 | |
| 1654 | if (entry->repeatCount == 1) { |
| 1655 | entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; |
| 1656 | } else { |
| 1657 | entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; |
| 1658 | } |
| 1659 | |
| 1660 | entry->dispatchInProgress = true; |
| 1661 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1662 | logOutboundKeyDetails("dispatchKey - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1663 | } |
| 1664 | |
| 1665 | // 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] | 1666 | if (entry->interceptKeyResult == KeyEntry::InterceptKeyResult::TRY_AGAIN_LATER) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1667 | if (currentTime < entry->interceptKeyWakeupTime) { |
| 1668 | if (entry->interceptKeyWakeupTime < *nextWakeupTime) { |
| 1669 | *nextWakeupTime = entry->interceptKeyWakeupTime; |
| 1670 | } |
| 1671 | return false; // wait until next wakeup |
| 1672 | } |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1673 | entry->interceptKeyResult = KeyEntry::InterceptKeyResult::UNKNOWN; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1674 | entry->interceptKeyWakeupTime = 0; |
| 1675 | } |
| 1676 | |
| 1677 | // Give the policy a chance to intercept the key. |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1678 | if (entry->interceptKeyResult == KeyEntry::InterceptKeyResult::UNKNOWN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1679 | if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1680 | sp<IBinder> focusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1681 | mFocusResolver.getFocusedWindowToken(getTargetDisplayId(*entry)); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1682 | |
| 1683 | auto command = [this, focusedWindowToken, entry]() REQUIRES(mLock) { |
| 1684 | doInterceptKeyBeforeDispatchingCommand(focusedWindowToken, *entry); |
| 1685 | }; |
| 1686 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1687 | return false; // wait for the command to run |
| 1688 | } else { |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1689 | entry->interceptKeyResult = KeyEntry::InterceptKeyResult::CONTINUE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1690 | } |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1691 | } else if (entry->interceptKeyResult == KeyEntry::InterceptKeyResult::SKIP) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1692 | if (*dropReason == DropReason::NOT_DROPPED) { |
| 1693 | *dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1694 | } |
| 1695 | } |
| 1696 | |
| 1697 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1698 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1699 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1700 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1701 | : InputEventInjectionResult::FAILED); |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1702 | mReporter->reportDroppedKey(entry->id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1703 | return true; |
| 1704 | } |
| 1705 | |
| 1706 | // Identify targets. |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1707 | InputEventInjectionResult injectionResult; |
| 1708 | sp<WindowInfoHandle> focusedWindow = |
| 1709 | findFocusedWindowTargetLocked(currentTime, *entry, nextWakeupTime, |
| 1710 | /*byref*/ injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1711 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1712 | return false; |
| 1713 | } |
| 1714 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1715 | setInjectionResult(*entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1716 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1717 | return true; |
| 1718 | } |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1719 | LOG_ALWAYS_FATAL_IF(focusedWindow == nullptr); |
| 1720 | |
| 1721 | std::vector<InputTarget> inputTargets; |
| 1722 | addWindowTargetLocked(focusedWindow, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 1723 | InputTarget::Flags::FOREGROUND | InputTarget::Flags::DISPATCH_AS_IS, |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 1724 | /*pointerIds=*/{}, getDownTime(*entry), inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1725 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1726 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1727 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1728 | |
| 1729 | // Dispatch the key. |
| 1730 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1731 | return true; |
| 1732 | } |
| 1733 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1734 | void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1735 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1736 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 ", " |
| 1737 | "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, " |
| 1738 | "metaState=0x%x, repeatCount=%d, downTime=%" PRId64, |
| 1739 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, |
| 1740 | entry.policyFlags, entry.action, entry.flags, entry.keyCode, entry.scanCode, |
| 1741 | entry.metaState, entry.repeatCount, entry.downTime); |
| 1742 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1743 | } |
| 1744 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1745 | void InputDispatcher::dispatchSensorLocked(nsecs_t currentTime, |
| 1746 | const std::shared_ptr<SensorEntry>& entry, |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1747 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1748 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1749 | ALOGD("notifySensorEvent eventTime=%" PRId64 ", hwTimestamp=%" PRId64 ", deviceId=%d, " |
| 1750 | "source=0x%x, sensorType=%s", |
| 1751 | entry->eventTime, entry->hwTimestamp, entry->deviceId, entry->source, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1752 | ftl::enum_string(entry->sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1753 | } |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1754 | auto command = [this, entry]() REQUIRES(mLock) { |
| 1755 | scoped_unlock unlock(mLock); |
| 1756 | |
| 1757 | if (entry->accuracyChanged) { |
| 1758 | mPolicy->notifySensorAccuracy(entry->deviceId, entry->sensorType, entry->accuracy); |
| 1759 | } |
| 1760 | mPolicy->notifySensorEvent(entry->deviceId, entry->sensorType, entry->accuracy, |
| 1761 | entry->hwTimestamp, entry->values); |
| 1762 | }; |
| 1763 | postCommandLocked(std::move(command)); |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1764 | } |
| 1765 | |
| 1766 | bool InputDispatcher::flushSensor(int deviceId, InputDeviceSensorType sensorType) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1767 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1768 | ALOGD("flushSensor deviceId=%d, sensorType=%s", deviceId, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1769 | ftl::enum_string(sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1770 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1771 | { // acquire lock |
| 1772 | std::scoped_lock _l(mLock); |
| 1773 | |
| 1774 | for (auto it = mInboundQueue.begin(); it != mInboundQueue.end(); it++) { |
| 1775 | std::shared_ptr<EventEntry> entry = *it; |
| 1776 | if (entry->type == EventEntry::Type::SENSOR) { |
| 1777 | it = mInboundQueue.erase(it); |
| 1778 | releaseInboundEventLocked(entry); |
| 1779 | } |
| 1780 | } |
| 1781 | } |
| 1782 | return true; |
| 1783 | } |
| 1784 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1785 | bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, std::shared_ptr<MotionEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1786 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1787 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1788 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1789 | if (!entry->dispatchInProgress) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1790 | entry->dispatchInProgress = true; |
| 1791 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1792 | logOutboundMotionDetails("dispatchMotion - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1793 | } |
| 1794 | |
| 1795 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1796 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1797 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1798 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1799 | : InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1800 | return true; |
| 1801 | } |
| 1802 | |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 1803 | const bool isPointerEvent = isFromSource(entry->source, AINPUT_SOURCE_CLASS_POINTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1804 | |
| 1805 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1806 | std::vector<InputTarget> inputTargets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1807 | |
| 1808 | bool conflictingPointerActions = false; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1809 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1810 | if (isPointerEvent) { |
| 1811 | // Pointer event. (eg. touchscreen) |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 1812 | |
| 1813 | if (mDragState && |
| 1814 | (entry->action & AMOTION_EVENT_ACTION_MASK) == AMOTION_EVENT_ACTION_POINTER_DOWN) { |
| 1815 | // If drag and drop ongoing and pointer down occur: pilfer drag window pointers |
| 1816 | pilferPointersLocked(mDragState->dragWindow->getToken()); |
| 1817 | } |
| 1818 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1819 | inputTargets = |
Siarhei Vishniakou | 4fe5739 | 2022-10-25 13:44:30 -0700 | [diff] [blame] | 1820 | findTouchedWindowTargetsLocked(currentTime, *entry, &conflictingPointerActions, |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1821 | /*byref*/ injectionResult); |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 1822 | LOG_ALWAYS_FATAL_IF(injectionResult != InputEventInjectionResult::SUCCEEDED && |
| 1823 | !inputTargets.empty()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1824 | } else { |
| 1825 | // Non touch event. (eg. trackball) |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1826 | sp<WindowInfoHandle> focusedWindow = |
| 1827 | findFocusedWindowTargetLocked(currentTime, *entry, nextWakeupTime, injectionResult); |
| 1828 | if (injectionResult == InputEventInjectionResult::SUCCEEDED) { |
| 1829 | LOG_ALWAYS_FATAL_IF(focusedWindow == nullptr); |
| 1830 | addWindowTargetLocked(focusedWindow, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 1831 | InputTarget::Flags::FOREGROUND | |
| 1832 | InputTarget::Flags::DISPATCH_AS_IS, |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 1833 | /*pointerIds=*/{}, getDownTime(*entry), inputTargets); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1834 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1835 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1836 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1837 | return false; |
| 1838 | } |
| 1839 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1840 | setInjectionResult(*entry, injectionResult); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1841 | if (injectionResult == InputEventInjectionResult::TARGET_MISMATCH) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1842 | return true; |
| 1843 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1844 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1845 | CancelationOptions::Mode mode( |
| 1846 | isPointerEvent ? CancelationOptions::Mode::CANCEL_POINTER_EVENTS |
| 1847 | : CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS); |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1848 | CancelationOptions options(mode, "input event injection failed"); |
| 1849 | synthesizeCancelationEventsForMonitorsLocked(options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1850 | return true; |
| 1851 | } |
| 1852 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1853 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1854 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1855 | |
| 1856 | // Dispatch the motion. |
| 1857 | if (conflictingPointerActions) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1858 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1859 | "conflicting pointer actions"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1860 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1861 | } |
| 1862 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1863 | return true; |
| 1864 | } |
| 1865 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1866 | void InputDispatcher::enqueueDragEventLocked(const sp<WindowInfoHandle>& windowHandle, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 1867 | bool isExiting, const int32_t rawX, |
| 1868 | const int32_t rawY) { |
| 1869 | const vec2 xy = windowHandle->getInfo()->transform.transform(vec2(rawX, rawY)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1870 | std::unique_ptr<DragEntry> dragEntry = |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 1871 | std::make_unique<DragEntry>(mIdGenerator.nextId(), now(), windowHandle->getToken(), |
| 1872 | isExiting, xy.x, xy.y); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1873 | |
| 1874 | enqueueInboundEventLocked(std::move(dragEntry)); |
| 1875 | } |
| 1876 | |
| 1877 | void InputDispatcher::dispatchDragLocked(nsecs_t currentTime, std::shared_ptr<DragEntry> entry) { |
| 1878 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
| 1879 | if (channel == nullptr) { |
| 1880 | return; // Window has gone away |
| 1881 | } |
| 1882 | InputTarget target; |
| 1883 | target.inputChannel = channel; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 1884 | target.flags = InputTarget::Flags::DISPATCH_AS_IS; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1885 | entry->dispatchInProgress = true; |
| 1886 | dispatchEventLocked(currentTime, entry, {target}); |
| 1887 | } |
| 1888 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1889 | void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1890 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 1891 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=%s, displayId=%" PRId32 |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1892 | ", policyFlags=0x%x, " |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1893 | "action=%s, actionButton=0x%x, flags=0x%x, " |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1894 | "metaState=0x%x, buttonState=0x%x," |
| 1895 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64, |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 1896 | prefix, entry.eventTime, entry.deviceId, |
| 1897 | inputEventSourceToString(entry.source).c_str(), entry.displayId, entry.policyFlags, |
| 1898 | MotionEvent::actionToString(entry.action).c_str(), entry.actionButton, entry.flags, |
| 1899 | entry.metaState, entry.buttonState, entry.edgeFlags, entry.xPrecision, |
| 1900 | entry.yPrecision, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1901 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1902 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1903 | ALOGD(" Pointer %d: id=%d, toolType=%s, " |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1904 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 1905 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 1906 | "orientation=%f", |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1907 | i, entry.pointerProperties[i].id, |
| 1908 | ftl::enum_string(entry.pointerProperties[i].toolType).c_str(), |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1909 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 1910 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 1911 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 1912 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 1913 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 1914 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 1915 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 1916 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 1917 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| 1918 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1919 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1920 | } |
| 1921 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1922 | void InputDispatcher::dispatchEventLocked(nsecs_t currentTime, |
| 1923 | std::shared_ptr<EventEntry> eventEntry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1924 | const std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1925 | ATRACE_CALL(); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1926 | if (DEBUG_DISPATCH_CYCLE) { |
| 1927 | ALOGD("dispatchEventToCurrentInputTargets"); |
| 1928 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1929 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1930 | updateInteractionTokensLocked(*eventEntry, inputTargets); |
| 1931 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1932 | ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true |
| 1933 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1934 | pokeUserActivityLocked(*eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1935 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1936 | for (const InputTarget& inputTarget : inputTargets) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 1937 | std::shared_ptr<Connection> connection = |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 1938 | getConnectionLocked(inputTarget.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 1939 | if (connection != nullptr) { |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 1940 | prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1941 | } else { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1942 | if (DEBUG_FOCUS) { |
| 1943 | ALOGD("Dropping event delivery to target with channel '%s' because it " |
| 1944 | "is no longer registered with the input dispatcher.", |
| 1945 | inputTarget.inputChannel->getName().c_str()); |
| 1946 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1947 | } |
| 1948 | } |
| 1949 | } |
| 1950 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 1951 | void InputDispatcher::cancelEventsForAnrLocked(const std::shared_ptr<Connection>& connection) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1952 | // We will not be breaking any connections here, even if the policy wants us to abort dispatch. |
| 1953 | // If the policy decides to close the app, we will get a channel removal event via |
| 1954 | // unregisterInputChannel, and will clean up the connection that way. We are already not |
| 1955 | // sending new pointers to the connection when it blocked, but focused events will continue to |
| 1956 | // pile up. |
| 1957 | ALOGW("Canceling events for %s because it is unresponsive", |
| 1958 | connection->inputChannel->getName().c_str()); |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 1959 | if (connection->status == Connection::Status::NORMAL) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1960 | CancelationOptions options(CancelationOptions::Mode::CANCEL_ALL_EVENTS, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1961 | "application not responding"); |
| 1962 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1963 | } |
| 1964 | } |
| 1965 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1966 | void InputDispatcher::resetNoFocusedWindowTimeoutLocked() { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1967 | if (DEBUG_FOCUS) { |
| 1968 | ALOGD("Resetting ANR timeouts."); |
| 1969 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1970 | |
| 1971 | // Reset input target wait timeout. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1972 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1973 | mAwaitedFocusedApplication.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1974 | } |
| 1975 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1976 | /** |
| 1977 | * Get the display id that the given event should go to. If this event specifies a valid display id, |
| 1978 | * then it should be dispatched to that display. Otherwise, the event goes to the focused display. |
| 1979 | * Focused display is the display that the user most recently interacted with. |
| 1980 | */ |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1981 | int32_t InputDispatcher::getTargetDisplayId(const EventEntry& entry) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1982 | int32_t displayId; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1983 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1984 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1985 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 1986 | displayId = keyEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1987 | break; |
| 1988 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1989 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1990 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1991 | displayId = motionEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1992 | break; |
| 1993 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1994 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1995 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1996 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1997 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1998 | case EventEntry::Type::DEVICE_RESET: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1999 | case EventEntry::Type::SENSOR: |
| 2000 | case EventEntry::Type::DRAG: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2001 | 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] | 2002 | return ADISPLAY_ID_NONE; |
| 2003 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2004 | } |
| 2005 | return displayId == ADISPLAY_ID_NONE ? mFocusedDisplayId : displayId; |
| 2006 | } |
| 2007 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2008 | bool InputDispatcher::shouldWaitToSendKeyLocked(nsecs_t currentTime, |
| 2009 | const char* focusedWindowName) { |
| 2010 | if (mAnrTracker.empty()) { |
| 2011 | // already processed all events that we waited for |
| 2012 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 2013 | return false; |
| 2014 | } |
| 2015 | |
| 2016 | if (!mKeyIsWaitingForEventsTimeout.has_value()) { |
| 2017 | // Start the timer |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 2018 | // 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] | 2019 | mKeyIsWaitingForEventsTimeout = currentTime + |
| 2020 | std::chrono::duration_cast<std::chrono::nanoseconds>(KEY_WAITING_FOR_EVENTS_TIMEOUT) |
| 2021 | .count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2022 | return true; |
| 2023 | } |
| 2024 | |
| 2025 | // We still have pending events, and already started the timer |
| 2026 | if (currentTime < *mKeyIsWaitingForEventsTimeout) { |
| 2027 | return true; // Still waiting |
| 2028 | } |
| 2029 | |
| 2030 | // Waited too long, and some connection still hasn't processed all motions |
| 2031 | // Just send the key to the focused window |
| 2032 | ALOGW("Dispatching key to %s even though there are other unprocessed events", |
| 2033 | focusedWindowName); |
| 2034 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 2035 | return false; |
| 2036 | } |
| 2037 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2038 | sp<WindowInfoHandle> InputDispatcher::findFocusedWindowTargetLocked( |
| 2039 | nsecs_t currentTime, const EventEntry& entry, nsecs_t* nextWakeupTime, |
| 2040 | InputEventInjectionResult& outInjectionResult) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2041 | std::string reason; |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2042 | outInjectionResult = InputEventInjectionResult::FAILED; // Default result |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2043 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2044 | int32_t displayId = getTargetDisplayId(entry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2045 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2046 | std::shared_ptr<InputApplicationHandle> focusedApplicationHandle = |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2047 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 2048 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2049 | // If there is no currently focused window and no focused application |
| 2050 | // then drop the event. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2051 | if (focusedWindowHandle == nullptr && focusedApplicationHandle == nullptr) { |
| 2052 | ALOGI("Dropping %s event because there is no focused window or focused application in " |
| 2053 | "display %" PRId32 ".", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2054 | ftl::enum_string(entry.type).c_str(), displayId); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2055 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2056 | } |
| 2057 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2058 | // Drop key events if requested by input feature |
| 2059 | if (focusedWindowHandle != nullptr && shouldDropInput(entry, focusedWindowHandle)) { |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2060 | return nullptr; |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2061 | } |
| 2062 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2063 | // Compatibility behavior: raise ANR if there is a focused application, but no focused window. |
| 2064 | // Only start counting when we have a focused event to dispatch. The ANR is canceled if we |
| 2065 | // start interacting with another application via touch (app switch). This code can be removed |
| 2066 | // if the "no focused window ANR" is moved to the policy. Input doesn't know whether |
| 2067 | // an app is expected to have a focused window. |
| 2068 | if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) { |
| 2069 | if (!mNoFocusedWindowTimeoutTime.has_value()) { |
| 2070 | // 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] | 2071 | std::chrono::nanoseconds timeout = focusedApplicationHandle->getDispatchingTimeout( |
| 2072 | DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
| 2073 | mNoFocusedWindowTimeoutTime = currentTime + timeout.count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2074 | mAwaitedFocusedApplication = focusedApplicationHandle; |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 2075 | mAwaitedApplicationDisplayId = displayId; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2076 | ALOGW("Waiting because no window has focus but %s may eventually add a " |
| 2077 | "window when it finishes starting up. Will wait for %" PRId64 "ms", |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 2078 | mAwaitedFocusedApplication->getName().c_str(), millis(timeout)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2079 | *nextWakeupTime = *mNoFocusedWindowTimeoutTime; |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2080 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 2081 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2082 | } else if (currentTime > *mNoFocusedWindowTimeoutTime) { |
| 2083 | // Already raised ANR. Drop the event |
| 2084 | ALOGE("Dropping %s event because there is no focused window", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2085 | ftl::enum_string(entry.type).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2086 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2087 | } else { |
| 2088 | // Still waiting for the focused window |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2089 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 2090 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2091 | } |
| 2092 | } |
| 2093 | |
| 2094 | // we have a valid, non-null focused window |
| 2095 | resetNoFocusedWindowTimeoutLocked(); |
| 2096 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2097 | // Verify targeted injection. |
| 2098 | if (const auto err = verifyTargetedInjection(focusedWindowHandle, entry); err) { |
| 2099 | ALOGW("Dropping injected event: %s", (*err).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2100 | outInjectionResult = InputEventInjectionResult::TARGET_MISMATCH; |
| 2101 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2102 | } |
| 2103 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2104 | if (focusedWindowHandle->getInfo()->inputConfig.test( |
| 2105 | WindowInfo::InputConfig::PAUSE_DISPATCHING)) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2106 | ALOGI("Waiting because %s is paused", focusedWindowHandle->getName().c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2107 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 2108 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2109 | } |
| 2110 | |
| 2111 | // If the event is a key event, then we must wait for all previous events to |
| 2112 | // complete before delivering it because previous events may have the |
| 2113 | // side-effect of transferring focus to a different window and we want to |
| 2114 | // ensure that the following keys are sent to the new window. |
| 2115 | // |
| 2116 | // Suppose the user touches a button in a window then immediately presses "A". |
| 2117 | // If the button causes a pop-up window to appear then we want to ensure that |
| 2118 | // the "A" key is delivered to the new pop-up window. This is because users |
| 2119 | // often anticipate pending UI changes when typing on a keyboard. |
| 2120 | // To obtain this behavior, we must serialize key events with respect to all |
| 2121 | // prior input events. |
| 2122 | if (entry.type == EventEntry::Type::KEY) { |
| 2123 | if (shouldWaitToSendKeyLocked(currentTime, focusedWindowHandle->getName().c_str())) { |
| 2124 | *nextWakeupTime = *mKeyIsWaitingForEventsTimeout; |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2125 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 2126 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2127 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2128 | } |
| 2129 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2130 | outInjectionResult = InputEventInjectionResult::SUCCEEDED; |
| 2131 | return focusedWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2132 | } |
| 2133 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2134 | /** |
| 2135 | * Given a list of monitors, remove the ones we cannot find a connection for, and the ones |
| 2136 | * that are currently unresponsive. |
| 2137 | */ |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2138 | std::vector<Monitor> InputDispatcher::selectResponsiveMonitorsLocked( |
| 2139 | const std::vector<Monitor>& monitors) const { |
| 2140 | std::vector<Monitor> responsiveMonitors; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2141 | std::copy_if(monitors.begin(), monitors.end(), std::back_inserter(responsiveMonitors), |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2142 | [this](const Monitor& monitor) REQUIRES(mLock) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 2143 | std::shared_ptr<Connection> connection = |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2144 | getConnectionLocked(monitor.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2145 | if (connection == nullptr) { |
| 2146 | ALOGE("Could not find connection for monitor %s", |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2147 | monitor.inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2148 | return false; |
| 2149 | } |
| 2150 | if (!connection->responsive) { |
| 2151 | ALOGW("Unresponsive monitor %s will not get the new gesture", |
| 2152 | connection->inputChannel->getName().c_str()); |
| 2153 | return false; |
| 2154 | } |
| 2155 | return true; |
| 2156 | }); |
| 2157 | return responsiveMonitors; |
| 2158 | } |
| 2159 | |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 2160 | /** |
| 2161 | * In general, touch should be always split between windows. Some exceptions: |
| 2162 | * 1. Don't split touch is if we have an active pointer down, and a new pointer is going down that's |
| 2163 | * from the same device, *and* the window that's receiving the current pointer does not support |
| 2164 | * split touch. |
| 2165 | * 2. Don't split mouse events |
| 2166 | */ |
| 2167 | bool InputDispatcher::shouldSplitTouch(const TouchState& touchState, |
| 2168 | const MotionEntry& entry) const { |
| 2169 | if (isFromSource(entry.source, AINPUT_SOURCE_MOUSE)) { |
| 2170 | // We should never split mouse events |
| 2171 | return false; |
| 2172 | } |
| 2173 | for (const TouchedWindow& touchedWindow : touchState.windows) { |
| 2174 | if (touchedWindow.windowHandle->getInfo()->isSpy()) { |
| 2175 | // Spy windows should not affect whether or not touch is split. |
| 2176 | continue; |
| 2177 | } |
| 2178 | if (touchedWindow.windowHandle->getInfo()->supportsSplitTouch()) { |
| 2179 | continue; |
| 2180 | } |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2181 | if (touchedWindow.windowHandle->getInfo()->inputConfig.test( |
| 2182 | gui::WindowInfo::InputConfig::IS_WALLPAPER)) { |
| 2183 | // Wallpaper window should not affect whether or not touch is split |
| 2184 | continue; |
| 2185 | } |
| 2186 | |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 2187 | // Eventually, touchedWindow will contain the deviceId of each pointer that's currently |
| 2188 | // being sent there. For now, use deviceId from touch state. |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 2189 | if (entry.deviceId == touchState.deviceId && touchedWindow.pointerIds.any()) { |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 2190 | return false; |
| 2191 | } |
| 2192 | } |
| 2193 | return true; |
| 2194 | } |
| 2195 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2196 | std::vector<InputTarget> InputDispatcher::findTouchedWindowTargetsLocked( |
Siarhei Vishniakou | 4fe5739 | 2022-10-25 13:44:30 -0700 | [diff] [blame] | 2197 | nsecs_t currentTime, const MotionEntry& entry, bool* outConflictingPointerActions, |
| 2198 | InputEventInjectionResult& outInjectionResult) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2199 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2200 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2201 | std::vector<InputTarget> targets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2202 | // For security reasons, we defer updating the touch state until we are sure that |
| 2203 | // event injection will be allowed. |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2204 | const int32_t displayId = entry.displayId; |
| 2205 | const int32_t action = entry.action; |
Siarhei Vishniakou | bf88052 | 2023-05-01 11:03:22 -0700 | [diff] [blame] | 2206 | const int32_t maskedAction = MotionEvent::getActionMasked(action); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2207 | |
| 2208 | // 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] | 2209 | outInjectionResult = InputEventInjectionResult::PENDING; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2210 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2211 | // Copy current touch state into tempTouchState. |
| 2212 | // This state will be used to update mTouchStatesByDisplay at the end of this function. |
| 2213 | // 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] | 2214 | const TouchState* oldState = nullptr; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2215 | TouchState tempTouchState; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2216 | if (const auto it = mTouchStatesByDisplay.find(displayId); it != mTouchStatesByDisplay.end()) { |
| 2217 | oldState = &(it->second); |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 2218 | tempTouchState = *oldState; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2219 | } |
| 2220 | |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 2221 | bool isSplit = shouldSplitTouch(tempTouchState, entry); |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 2222 | const bool switchedDevice = (oldState != nullptr) && |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2223 | (oldState->deviceId != entry.deviceId || oldState->source != entry.source); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2224 | |
| 2225 | const bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE || |
| 2226 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2227 | maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2228 | // A DOWN could be generated from POINTER_DOWN if the initial pointers did not land into any |
| 2229 | // touchable windows. |
| 2230 | const bool wasDown = oldState != nullptr && oldState->isDown(); |
| 2231 | const bool isDown = (maskedAction == AMOTION_EVENT_ACTION_DOWN) || |
| 2232 | (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN && !wasDown); |
| 2233 | const bool newGesture = isDown || maskedAction == AMOTION_EVENT_ACTION_SCROLL || isHoverAction; |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 2234 | const bool isFromMouse = isFromSource(entry.source, AINPUT_SOURCE_MOUSE); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2235 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2236 | // If pointers are already down, let's finish the current gesture and ignore the new events |
| 2237 | // from another device. However, if the new event is a down event, let's cancel the current |
| 2238 | // touch and let the new one take over. |
| 2239 | if (switchedDevice && wasDown && !isDown) { |
| 2240 | LOG(INFO) << "Dropping event because a pointer for device " << oldState->deviceId |
| 2241 | << " is already down in display " << displayId << ": " << entry.getDescription(); |
| 2242 | // TODO(b/211379801): test multiple simultaneous input streams. |
| 2243 | outInjectionResult = InputEventInjectionResult::FAILED; |
| 2244 | return {}; // wrong device |
| 2245 | } |
| 2246 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2247 | if (newGesture) { |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2248 | // If a new gesture is starting, clear the touch state completely. |
| 2249 | tempTouchState.reset(); |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2250 | tempTouchState.deviceId = entry.deviceId; |
| 2251 | tempTouchState.source = entry.source; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2252 | isSplit = false; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2253 | } else if (switchedDevice && maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2254 | ALOGI("Dropping move event because a pointer for a different device is already active " |
| 2255 | "in display %" PRId32, |
| 2256 | displayId); |
Siarhei Vishniakou | 16e4fa0 | 2023-02-16 17:48:56 -0800 | [diff] [blame] | 2257 | // TODO(b/211379801): test multiple simultaneous input streams. |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2258 | outInjectionResult = InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2259 | return {}; // wrong device |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2260 | } |
| 2261 | |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2262 | if (isHoverAction) { |
| 2263 | // For hover actions, we will treat 'tempTouchState' as a new state, so let's erase |
| 2264 | // all of the existing hovering pointers and recompute. |
| 2265 | tempTouchState.clearHoveringPointers(); |
| 2266 | } |
| 2267 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2268 | if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { |
| 2269 | /* 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] | 2270 | const auto [x, y] = resolveTouchedPosition(entry); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2271 | const int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2272 | // Outside targets should be added upon first dispatched DOWN event. That means, this should |
| 2273 | // 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] | 2274 | const bool isStylus = isPointerFromStylus(entry, pointerIndex); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2275 | auto [newTouchedWindowHandle, outsideTargets] = |
| 2276 | findTouchedWindowAtLocked(displayId, x, y, isStylus); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2277 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2278 | if (isDown) { |
| 2279 | targets += outsideTargets; |
| 2280 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2281 | // Handle the case where we did not find a window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2282 | if (newTouchedWindowHandle == nullptr) { |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 2283 | 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] | 2284 | // 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] | 2285 | newTouchedWindowHandle = tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2286 | } |
| 2287 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2288 | // Verify targeted injection. |
| 2289 | if (const auto err = verifyTargetedInjection(newTouchedWindowHandle, entry); err) { |
| 2290 | ALOGW("Dropping injected touch event: %s", (*err).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2291 | outInjectionResult = os::InputEventInjectionResult::TARGET_MISMATCH; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2292 | newTouchedWindowHandle = nullptr; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2293 | return {}; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2294 | } |
| 2295 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2296 | // Figure out whether splitting will be allowed for this window. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2297 | if (newTouchedWindowHandle != nullptr) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2298 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
| 2299 | // New window supports splitting, but we should never split mouse events. |
| 2300 | isSplit = !isFromMouse; |
| 2301 | } else if (isSplit) { |
| 2302 | // New window does not support splitting but we have already split events. |
| 2303 | // Ignore the new window. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2304 | newTouchedWindowHandle = nullptr; |
| 2305 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2306 | } else { |
| 2307 | // 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] | 2308 | // be delivered to a new window which supports split touch. Pointers from a mouse device |
| 2309 | // should never be split. |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 2310 | isSplit = !isFromMouse; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2311 | } |
| 2312 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2313 | std::vector<sp<WindowInfoHandle>> newTouchedWindows = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2314 | findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2315 | if (newTouchedWindowHandle != nullptr) { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2316 | // Process the foreground window first so that it is the first to receive the event. |
| 2317 | newTouchedWindows.insert(newTouchedWindows.begin(), newTouchedWindowHandle); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2318 | } |
| 2319 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2320 | if (newTouchedWindows.empty()) { |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 2321 | ALOGI("Dropping event because there is no touchable window at (%.1f, %.1f) on display " |
| 2322 | "%d.", |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2323 | x, y, displayId); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2324 | outInjectionResult = InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2325 | return {}; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2326 | } |
| 2327 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2328 | for (const sp<WindowInfoHandle>& windowHandle : newTouchedWindows) { |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 2329 | if (!canWindowReceiveMotionLocked(windowHandle, entry)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2330 | continue; |
| 2331 | } |
| 2332 | |
Siarhei Vishniakou | b681c20 | 2023-05-01 11:22:33 -0700 | [diff] [blame^] | 2333 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2334 | maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) { |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2335 | const int32_t pointerId = entry.pointerProperties[0].id; |
Siarhei Vishniakou | b681c20 | 2023-05-01 11:22:33 -0700 | [diff] [blame^] | 2336 | // The "windowHandle" is the target of this hovering pointer. |
| 2337 | tempTouchState.addHoveringPointerToWindow(windowHandle, entry.deviceId, pointerId); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2338 | } |
| 2339 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2340 | // Set target flags. |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2341 | ftl::Flags<InputTarget::Flags> targetFlags = InputTarget::Flags::DISPATCH_AS_IS; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2342 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2343 | if (canReceiveForegroundTouches(*windowHandle->getInfo())) { |
| 2344 | // 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] | 2345 | targetFlags |= InputTarget::Flags::FOREGROUND; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2346 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2347 | |
| 2348 | if (isSplit) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2349 | targetFlags |= InputTarget::Flags::SPLIT; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2350 | } |
| 2351 | if (isWindowObscuredAtPointLocked(windowHandle, x, y)) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2352 | targetFlags |= InputTarget::Flags::WINDOW_IS_OBSCURED; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2353 | } else if (isWindowObscuredLocked(windowHandle)) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2354 | targetFlags |= InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2355 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2356 | |
| 2357 | // Update the temporary touch state. |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 2358 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2359 | if (!isHoverAction) { |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 2360 | pointerIds.set(entry.pointerProperties[pointerIndex].id); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2361 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2362 | |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2363 | const bool isDownOrPointerDown = maskedAction == AMOTION_EVENT_ACTION_DOWN || |
| 2364 | maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN; |
| 2365 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2366 | // TODO(b/211379801): Currently, even if pointerIds are empty (hover case), we would |
| 2367 | // still add a window to the touch state. We should avoid doing that, but some of the |
| 2368 | // later checks ("at least one foreground window") rely on this in order to dispatch |
| 2369 | // 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] | 2370 | tempTouchState.addOrUpdateWindow(windowHandle, targetFlags, pointerIds, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2371 | isDownOrPointerDown |
| 2372 | ? std::make_optional(entry.eventTime) |
| 2373 | : std::nullopt); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2374 | |
| 2375 | // If this is the pointer going down and the touched window has a wallpaper |
| 2376 | // then also add the touched wallpaper windows so they are locked in for the duration |
| 2377 | // of the touch gesture. |
| 2378 | // We do not collect wallpapers during HOVER_MOVE or SCROLL because the wallpaper |
| 2379 | // engine only supports touch events. We would need to add a mechanism similar |
| 2380 | // to View.onGenericMotionEvent to enable wallpapers to handle these events. |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2381 | if (isDownOrPointerDown) { |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2382 | if (targetFlags.test(InputTarget::Flags::FOREGROUND) && |
| 2383 | windowHandle->getInfo()->inputConfig.test( |
| 2384 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER)) { |
| 2385 | sp<WindowInfoHandle> wallpaper = findWallpaperWindowBelow(windowHandle); |
| 2386 | if (wallpaper != nullptr) { |
| 2387 | ftl::Flags<InputTarget::Flags> wallpaperFlags = |
| 2388 | InputTarget::Flags::WINDOW_IS_OBSCURED | |
| 2389 | InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED | |
| 2390 | InputTarget::Flags::DISPATCH_AS_IS; |
| 2391 | if (isSplit) { |
| 2392 | wallpaperFlags |= InputTarget::Flags::SPLIT; |
| 2393 | } |
| 2394 | tempTouchState.addOrUpdateWindow(wallpaper, wallpaperFlags, pointerIds, |
| 2395 | entry.eventTime); |
| 2396 | } |
| 2397 | } |
| 2398 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2399 | } |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 2400 | |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 2401 | // If a window is already pilfering some pointers, give it this new pointer as well and |
| 2402 | // make it pilfering. This will prevent other non-spy windows from getting this pointer, |
| 2403 | // which is a specific behaviour that we want. |
| 2404 | const int32_t pointerId = entry.pointerProperties[pointerIndex].id; |
| 2405 | for (TouchedWindow& touchedWindow : tempTouchState.windows) { |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 2406 | if (touchedWindow.pointerIds.test(pointerId) && |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 2407 | touchedWindow.pilferedPointerIds.count() > 0) { |
| 2408 | // This window is already pilfering some pointers, and this new pointer is also |
| 2409 | // going to it. Therefore, take over this pointer and don't give it to anyone |
| 2410 | // else. |
| 2411 | touchedWindow.pilferedPointerIds.set(pointerId); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 2412 | } |
| 2413 | } |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 2414 | |
| 2415 | // Restrict all pilfered pointers to the pilfering windows. |
| 2416 | tempTouchState.cancelPointersForNonPilferingWindows(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2417 | } else { |
| 2418 | /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ |
| 2419 | |
| 2420 | // If the pointer is not currently down, then ignore the event. |
Siarhei Vishniakou | 3ad385b | 2022-11-04 10:09:53 -0700 | [diff] [blame] | 2421 | if (!tempTouchState.isDown()) { |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2422 | LOG(INFO) << "Dropping event because the pointer is not down or we previously " |
| 2423 | "dropped the pointer down event in display " |
| 2424 | << displayId << ": " << entry.getDescription(); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2425 | outInjectionResult = InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2426 | return {}; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2427 | } |
| 2428 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2429 | addDragEventLocked(entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2430 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2431 | // Check whether touches should slip outside of the current foreground window. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2432 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.pointerCount == 1 && |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2433 | tempTouchState.isSlippery()) { |
Siarhei Vishniakou | 9306c38 | 2022-09-30 15:30:31 -0700 | [diff] [blame] | 2434 | const auto [x, y] = resolveTouchedPosition(entry); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2435 | const bool isStylus = isPointerFromStylus(entry, /*pointerIndex=*/0); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2436 | sp<WindowInfoHandle> oldTouchedWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2437 | tempTouchState.getFirstForegroundWindowHandle(); |
Siarhei Vishniakou | 0f6558d | 2023-04-21 12:05:13 -0700 | [diff] [blame] | 2438 | LOG_ALWAYS_FATAL_IF(oldTouchedWindowHandle == nullptr); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2439 | auto [newTouchedWindowHandle, _] = findTouchedWindowAtLocked(displayId, x, y, isStylus); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2440 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2441 | // Verify targeted injection. |
| 2442 | if (const auto err = verifyTargetedInjection(newTouchedWindowHandle, entry); err) { |
| 2443 | ALOGW("Dropping injected event: %s", (*err).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2444 | outInjectionResult = os::InputEventInjectionResult::TARGET_MISMATCH; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2445 | return {}; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2446 | } |
| 2447 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2448 | // Drop touch events if requested by input feature |
| 2449 | if (newTouchedWindowHandle != nullptr && |
| 2450 | shouldDropInput(entry, newTouchedWindowHandle)) { |
| 2451 | newTouchedWindowHandle = nullptr; |
| 2452 | } |
| 2453 | |
Siarhei Vishniakou | 0f6558d | 2023-04-21 12:05:13 -0700 | [diff] [blame] | 2454 | if (!haveSameToken(oldTouchedWindowHandle, newTouchedWindowHandle)) { |
| 2455 | ALOGD("Touch is slipping out of window %s into window %s in display %" PRId32, |
| 2456 | oldTouchedWindowHandle->getName().c_str(), |
| 2457 | newTouchedWindowHandle->getName().c_str(), displayId); |
| 2458 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2459 | // Make a slippery exit from the old window. |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 2460 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2461 | const int32_t pointerId = entry.pointerProperties[0].id; |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 2462 | pointerIds.set(pointerId); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2463 | |
| 2464 | const TouchedWindow& touchedWindow = |
| 2465 | tempTouchState.getTouchedWindow(oldTouchedWindowHandle); |
| 2466 | addWindowTargetLocked(oldTouchedWindowHandle, |
| 2467 | InputTarget::Flags::DISPATCH_AS_SLIPPERY_EXIT, pointerIds, |
| 2468 | touchedWindow.firstDownTimeInTarget, targets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2469 | |
| 2470 | // Make a slippery entrance into the new window. |
| 2471 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
Prabir Pradhan | 713bb3e | 2021-12-20 02:07:40 -0800 | [diff] [blame] | 2472 | isSplit = !isFromMouse; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2473 | } |
| 2474 | |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2475 | ftl::Flags<InputTarget::Flags> targetFlags = |
| 2476 | InputTarget::Flags::DISPATCH_AS_SLIPPERY_ENTER; |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2477 | if (canReceiveForegroundTouches(*newTouchedWindowHandle->getInfo())) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2478 | targetFlags |= InputTarget::Flags::FOREGROUND; |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2479 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2480 | if (isSplit) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2481 | targetFlags |= InputTarget::Flags::SPLIT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2482 | } |
| 2483 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2484 | targetFlags |= InputTarget::Flags::WINDOW_IS_OBSCURED; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2485 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2486 | targetFlags |= InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2487 | } |
| 2488 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2489 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds, |
| 2490 | entry.eventTime); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2491 | |
| 2492 | // Check if the wallpaper window should deliver the corresponding event. |
| 2493 | slipWallpaperTouch(targetFlags, oldTouchedWindowHandle, newTouchedWindowHandle, |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2494 | tempTouchState, pointerId, targets); |
| 2495 | tempTouchState.removeTouchedPointerFromWindow(pointerId, oldTouchedWindowHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2496 | } |
| 2497 | } |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 2498 | |
| 2499 | // Update the pointerIds for non-splittable when it received pointer down. |
| 2500 | if (!isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN) { |
| 2501 | // If no split, we suppose all touched windows should receive pointer down. |
| 2502 | const int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
| 2503 | for (size_t i = 0; i < tempTouchState.windows.size(); i++) { |
| 2504 | TouchedWindow& touchedWindow = tempTouchState.windows[i]; |
| 2505 | // Ignore drag window for it should just track one pointer. |
| 2506 | if (mDragState && mDragState->dragWindow == touchedWindow.windowHandle) { |
| 2507 | continue; |
| 2508 | } |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 2509 | touchedWindow.pointerIds.set(entry.pointerProperties[pointerIndex].id); |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 2510 | } |
| 2511 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2512 | } |
| 2513 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2514 | // Update dispatching for hover enter and exit. |
Sam Dubey | f886dec | 2023-01-27 13:28:19 +0000 | [diff] [blame] | 2515 | { |
| 2516 | std::vector<TouchedWindow> hoveringWindows = |
| 2517 | getHoveringWindowsLocked(oldState, tempTouchState, entry); |
| 2518 | for (const TouchedWindow& touchedWindow : hoveringWindows) { |
| 2519 | addWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags, |
| 2520 | touchedWindow.pointerIds, touchedWindow.firstDownTimeInTarget, |
| 2521 | targets); |
| 2522 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2523 | } |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2524 | // Ensure that we have at least one foreground window or at least one window that cannot be a |
| 2525 | // foreground target. If we only have windows that are not receiving foreground touches (e.g. we |
| 2526 | // only have windows getting ACTION_OUTSIDE), then drop the event, because there is no window |
| 2527 | // that is actually receiving the entire gesture. |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2528 | if (std::none_of(tempTouchState.windows.begin(), tempTouchState.windows.end(), |
| 2529 | [](const TouchedWindow& touchedWindow) { |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2530 | return !canReceiveForegroundTouches( |
| 2531 | *touchedWindow.windowHandle->getInfo()) || |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2532 | touchedWindow.targetFlags.test(InputTarget::Flags::FOREGROUND); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2533 | })) { |
Siarhei Vishniakou | 1fb1891 | 2022-03-08 10:31:39 -0800 | [diff] [blame] | 2534 | ALOGI("Dropping event because there is no touched window on display %d to receive it: %s", |
| 2535 | displayId, entry.getDescription().c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2536 | outInjectionResult = InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2537 | return {}; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2538 | } |
| 2539 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2540 | // Ensure that all touched windows are valid for injection. |
| 2541 | if (entry.injectionState != nullptr) { |
| 2542 | std::string errs; |
| 2543 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2544 | if (touchedWindow.targetFlags.test(InputTarget::Flags::DISPATCH_AS_OUTSIDE)) { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2545 | // Allow ACTION_OUTSIDE events generated by targeted injection to be |
| 2546 | // dispatched to any uid, since the coords will be zeroed out later. |
| 2547 | continue; |
| 2548 | } |
| 2549 | const auto err = verifyTargetedInjection(touchedWindow.windowHandle, entry); |
| 2550 | if (err) errs += "\n - " + *err; |
| 2551 | } |
| 2552 | if (!errs.empty()) { |
| 2553 | ALOGW("Dropping targeted injection: At least one touched window is not owned by uid " |
| 2554 | "%d:%s", |
| 2555 | *entry.injectionState->targetUid, errs.c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2556 | outInjectionResult = InputEventInjectionResult::TARGET_MISMATCH; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2557 | return {}; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2558 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2559 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2560 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2561 | // Check whether windows listening for outside touches are owned by the same UID. If the owner |
| 2562 | // 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] | 2563 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2564 | sp<WindowInfoHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2565 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2566 | if (foregroundWindowHandle) { |
| 2567 | const int32_t foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2568 | for (InputTarget& target : targets) { |
| 2569 | if (target.flags.test(InputTarget::Flags::DISPATCH_AS_OUTSIDE)) { |
| 2570 | sp<WindowInfoHandle> targetWindow = |
| 2571 | getWindowHandleLocked(target.inputChannel->getConnectionToken()); |
| 2572 | if (targetWindow->getInfo()->ownerUid != foregroundWindowUid) { |
| 2573 | target.flags |= InputTarget::Flags::ZERO_COORDS; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2574 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2575 | } |
| 2576 | } |
| 2577 | } |
| 2578 | } |
| 2579 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2580 | // Success! Output targets from the touch state. |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2581 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 2582 | if (touchedWindow.pointerIds.none() && !touchedWindow.hasHoveringPointers(entry.deviceId)) { |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2583 | // Windows with hovering pointers are getting persisted inside TouchState. |
| 2584 | // Do not send this event to those windows. |
| 2585 | continue; |
| 2586 | } |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2587 | addWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags, |
| 2588 | touchedWindow.pointerIds, touchedWindow.firstDownTimeInTarget, |
| 2589 | targets); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2590 | } |
Sam Dubey | 39d37cf | 2022-12-07 18:05:35 +0000 | [diff] [blame] | 2591 | |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2592 | outInjectionResult = InputEventInjectionResult::SUCCEEDED; |
Sam Dubey | f886dec | 2023-01-27 13:28:19 +0000 | [diff] [blame] | 2593 | // Drop the outside or hover touch windows since we will not care about them |
| 2594 | // in the next iteration. |
| 2595 | tempTouchState.filterNonAsIsTouchWindows(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2596 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2597 | // Update final pieces of touch state if the injector had permission. |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2598 | if (switchedDevice) { |
| 2599 | if (DEBUG_FOCUS) { |
| 2600 | ALOGD("Conflicting pointer actions: Switched to a different device."); |
| 2601 | } |
| 2602 | *outConflictingPointerActions = true; |
| 2603 | } |
| 2604 | |
| 2605 | if (isHoverAction) { |
| 2606 | // Started hovering, therefore no longer down. |
Siarhei Vishniakou | 3ad385b | 2022-11-04 10:09:53 -0700 | [diff] [blame] | 2607 | if (oldState && oldState->isDown()) { |
Siarhei Vishniakou | 6e1e987 | 2022-11-08 17:51:35 -0800 | [diff] [blame] | 2608 | ALOGD_IF(DEBUG_FOCUS, |
| 2609 | "Conflicting pointer actions: Hover received while pointer was down."); |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2610 | *outConflictingPointerActions = true; |
| 2611 | } |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2612 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2613 | maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) { |
| 2614 | tempTouchState.deviceId = entry.deviceId; |
| 2615 | tempTouchState.source = entry.source; |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2616 | } |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2617 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP) { |
| 2618 | // Pointer went up. |
| 2619 | tempTouchState.removeTouchedPointer(entry.pointerProperties[0].id); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2620 | } else if (maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2621 | // All pointers up or canceled. |
| 2622 | tempTouchState.reset(); |
| 2623 | } else if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 2624 | // First pointer went down. |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2625 | if (oldState && (oldState->isDown() || oldState->hasHoveringPointers())) { |
| 2626 | ALOGD("Conflicting pointer actions: Down received while already down or hovering."); |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2627 | *outConflictingPointerActions = true; |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2628 | } |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2629 | } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 2630 | // One pointer went up. |
| 2631 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
| 2632 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2633 | |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2634 | for (size_t i = 0; i < tempTouchState.windows.size();) { |
| 2635 | TouchedWindow& touchedWindow = tempTouchState.windows[i]; |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 2636 | touchedWindow.pointerIds.reset(pointerId); |
| 2637 | if (touchedWindow.pointerIds.none()) { |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2638 | tempTouchState.windows.erase(tempTouchState.windows.begin() + i); |
| 2639 | continue; |
| 2640 | } |
| 2641 | i += 1; |
| 2642 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2643 | } |
| 2644 | |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2645 | // Save changes unless the action was scroll in which case the temporary touch |
| 2646 | // state was only valid for this one action. |
| 2647 | if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) { |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 2648 | if (displayId >= 0) { |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2649 | tempTouchState.clearWindowsWithoutPointers(); |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2650 | mTouchStatesByDisplay[displayId] = tempTouchState; |
| 2651 | } else { |
| 2652 | mTouchStatesByDisplay.erase(displayId); |
| 2653 | } |
| 2654 | } |
| 2655 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 2656 | if (tempTouchState.windows.empty()) { |
| 2657 | mTouchStatesByDisplay.erase(displayId); |
| 2658 | } |
| 2659 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2660 | return targets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2661 | } |
| 2662 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2663 | void InputDispatcher::finishDragAndDrop(int32_t displayId, float x, float y) { |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2664 | // Prevent stylus interceptor windows from affecting drag and drop behavior for now, until we |
| 2665 | // have an explicit reason to support it. |
| 2666 | constexpr bool isStylus = false; |
| 2667 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2668 | auto [dropWindow, _] = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2669 | findTouchedWindowAtLocked(displayId, x, y, isStylus, /*ignoreDragWindow=*/true); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2670 | if (dropWindow) { |
| 2671 | vec2 local = dropWindow->getInfo()->transform.transform(x, y); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2672 | sendDropWindowCommandLocked(dropWindow->getToken(), local.x, local.y); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 2673 | } else { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2674 | ALOGW("No window found when drop."); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2675 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2676 | } |
| 2677 | mDragState.reset(); |
| 2678 | } |
| 2679 | |
| 2680 | void InputDispatcher::addDragEventLocked(const MotionEntry& entry) { |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 2681 | if (!mDragState || mDragState->dragWindow->getInfo()->displayId != entry.displayId) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2682 | return; |
| 2683 | } |
| 2684 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2685 | if (!mDragState->isStartDrag) { |
| 2686 | mDragState->isStartDrag = true; |
| 2687 | mDragState->isStylusButtonDownAtStart = |
| 2688 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2689 | } |
| 2690 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2691 | // Find the pointer index by id. |
| 2692 | int32_t pointerIndex = 0; |
| 2693 | for (; static_cast<uint32_t>(pointerIndex) < entry.pointerCount; pointerIndex++) { |
| 2694 | const PointerProperties& pointerProperties = entry.pointerProperties[pointerIndex]; |
| 2695 | if (pointerProperties.id == mDragState->pointerId) { |
| 2696 | break; |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2697 | } |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2698 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2699 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2700 | if (uint32_t(pointerIndex) == entry.pointerCount) { |
| 2701 | 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] | 2702 | } |
| 2703 | |
| 2704 | const int32_t maskedAction = entry.action & AMOTION_EVENT_ACTION_MASK; |
| 2705 | const int32_t x = entry.pointerCoords[pointerIndex].getX(); |
| 2706 | const int32_t y = entry.pointerCoords[pointerIndex].getY(); |
| 2707 | |
| 2708 | switch (maskedAction) { |
| 2709 | case AMOTION_EVENT_ACTION_MOVE: { |
| 2710 | // Handle the special case : stylus button no longer pressed. |
| 2711 | bool isStylusButtonDown = |
| 2712 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2713 | if (mDragState->isStylusButtonDownAtStart && !isStylusButtonDown) { |
| 2714 | finishDragAndDrop(entry.displayId, x, y); |
| 2715 | return; |
| 2716 | } |
| 2717 | |
| 2718 | // Prevent stylus interceptor windows from affecting drag and drop behavior for now, |
| 2719 | // until we have an explicit reason to support it. |
| 2720 | constexpr bool isStylus = false; |
| 2721 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2722 | auto [hoverWindowHandle, _] = findTouchedWindowAtLocked(entry.displayId, x, y, isStylus, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2723 | /*ignoreDragWindow=*/true); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2724 | // enqueue drag exit if needed. |
| 2725 | if (hoverWindowHandle != mDragState->dragHoverWindowHandle && |
| 2726 | !haveSameToken(hoverWindowHandle, mDragState->dragHoverWindowHandle)) { |
| 2727 | if (mDragState->dragHoverWindowHandle != nullptr) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2728 | enqueueDragEventLocked(mDragState->dragHoverWindowHandle, /*isExiting=*/true, x, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2729 | y); |
| 2730 | } |
| 2731 | mDragState->dragHoverWindowHandle = hoverWindowHandle; |
| 2732 | } |
| 2733 | // enqueue drag location if needed. |
| 2734 | if (hoverWindowHandle != nullptr) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2735 | enqueueDragEventLocked(hoverWindowHandle, /*isExiting=*/false, x, y); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2736 | } |
| 2737 | break; |
| 2738 | } |
| 2739 | |
| 2740 | case AMOTION_EVENT_ACTION_POINTER_UP: |
| 2741 | if (getMotionEventActionPointerIndex(entry.action) != pointerIndex) { |
| 2742 | break; |
| 2743 | } |
| 2744 | // The drag pointer is up. |
| 2745 | [[fallthrough]]; |
| 2746 | case AMOTION_EVENT_ACTION_UP: |
| 2747 | finishDragAndDrop(entry.displayId, x, y); |
| 2748 | break; |
| 2749 | case AMOTION_EVENT_ACTION_CANCEL: { |
| 2750 | ALOGD("Receiving cancel when drag and drop."); |
| 2751 | sendDropWindowCommandLocked(nullptr, 0, 0); |
| 2752 | mDragState.reset(); |
| 2753 | break; |
| 2754 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2755 | } |
| 2756 | } |
| 2757 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2758 | void InputDispatcher::addWindowTargetLocked(const sp<WindowInfoHandle>& windowHandle, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2759 | ftl::Flags<InputTarget::Flags> targetFlags, |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 2760 | std::bitset<MAX_POINTER_ID + 1> pointerIds, |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2761 | std::optional<nsecs_t> firstDownTimeInTarget, |
Siarhei Vishniakou | f75cddb | 2022-10-25 10:42:16 -0700 | [diff] [blame] | 2762 | std::vector<InputTarget>& inputTargets) const { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2763 | std::vector<InputTarget>::iterator it = |
| 2764 | std::find_if(inputTargets.begin(), inputTargets.end(), |
| 2765 | [&windowHandle](const InputTarget& inputTarget) { |
| 2766 | return inputTarget.inputChannel->getConnectionToken() == |
| 2767 | windowHandle->getToken(); |
| 2768 | }); |
Chavi Weingarten | 97b8eec | 2020-01-09 18:09:08 +0000 | [diff] [blame] | 2769 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2770 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2771 | |
| 2772 | if (it == inputTargets.end()) { |
| 2773 | InputTarget inputTarget; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 2774 | std::shared_ptr<InputChannel> inputChannel = |
| 2775 | getInputChannelLocked(windowHandle->getToken()); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2776 | if (inputChannel == nullptr) { |
| 2777 | ALOGW("Window %s already unregistered input channel", windowHandle->getName().c_str()); |
| 2778 | return; |
| 2779 | } |
| 2780 | inputTarget.inputChannel = inputChannel; |
| 2781 | inputTarget.flags = targetFlags; |
| 2782 | inputTarget.globalScaleFactor = windowInfo->globalScaleFactor; |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2783 | inputTarget.firstDownTimeInTarget = firstDownTimeInTarget; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2784 | const auto& displayInfoIt = mDisplayInfos.find(windowInfo->displayId); |
| 2785 | if (displayInfoIt != mDisplayInfos.end()) { |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 2786 | inputTarget.displayTransform = displayInfoIt->second.transform; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2787 | } else { |
Siarhei Vishniakou | a06bb55 | 2023-02-07 09:38:56 -0800 | [diff] [blame] | 2788 | // DisplayInfo not found for this window on display windowInfo->displayId. |
| 2789 | // TODO(b/198444055): Make this an error message after 'setInputWindows' API is removed. |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2790 | } |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2791 | inputTargets.push_back(inputTarget); |
| 2792 | it = inputTargets.end() - 1; |
| 2793 | } |
| 2794 | |
| 2795 | ALOG_ASSERT(it->flags == targetFlags); |
| 2796 | ALOG_ASSERT(it->globalScaleFactor == windowInfo->globalScaleFactor); |
| 2797 | |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2798 | it->addPointers(pointerIds, windowInfo->transform); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2799 | } |
| 2800 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2801 | void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2802 | int32_t displayId) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2803 | auto monitorsIt = mGlobalMonitorsByDisplay.find(displayId); |
| 2804 | if (monitorsIt == mGlobalMonitorsByDisplay.end()) return; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2805 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2806 | for (const Monitor& monitor : selectResponsiveMonitorsLocked(monitorsIt->second)) { |
| 2807 | InputTarget target; |
| 2808 | target.inputChannel = monitor.inputChannel; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2809 | target.flags = InputTarget::Flags::DISPATCH_AS_IS; |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2810 | // target.firstDownTimeInTarget is not set for global monitors. It is only required in split |
| 2811 | // touch and global monitoring works as intended even without setting firstDownTimeInTarget |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2812 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
| 2813 | target.displayTransform = it->second.transform; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2814 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2815 | target.setDefaultPointerTransform(target.displayTransform); |
| 2816 | inputTargets.push_back(target); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2817 | } |
| 2818 | } |
| 2819 | |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2820 | /** |
| 2821 | * Indicate whether one window handle should be considered as obscuring |
| 2822 | * another window handle. We only check a few preconditions. Actually |
| 2823 | * checking the bounds is left to the caller. |
| 2824 | */ |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2825 | static bool canBeObscuredBy(const sp<WindowInfoHandle>& windowHandle, |
| 2826 | const sp<WindowInfoHandle>& otherHandle) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2827 | // Compare by token so cloned layers aren't counted |
| 2828 | if (haveSameToken(windowHandle, otherHandle)) { |
| 2829 | return false; |
| 2830 | } |
| 2831 | auto info = windowHandle->getInfo(); |
| 2832 | auto otherInfo = otherHandle->getInfo(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2833 | if (otherInfo->inputConfig.test(WindowInfo::InputConfig::NOT_VISIBLE)) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2834 | return false; |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2835 | } else if (otherInfo->alpha == 0 && |
| 2836 | otherInfo->inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE)) { |
Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame] | 2837 | // Those act as if they were invisible, so we don't need to flag them. |
| 2838 | // We do want to potentially flag touchable windows even if they have 0 |
| 2839 | // opacity, since they can consume touches and alter the effects of the |
| 2840 | // user interaction (eg. apps that rely on |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2841 | // Flags::WINDOW_IS_PARTIALLY_OBSCURED should still be told about those |
Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame] | 2842 | // windows), hence we also check for FLAG_NOT_TOUCHABLE. |
| 2843 | return false; |
Bernardo Rufino | 8007daf | 2020-09-22 09:40:01 +0000 | [diff] [blame] | 2844 | } else if (info->ownerUid == otherInfo->ownerUid) { |
| 2845 | // If ownerUid is the same we don't generate occlusion events as there |
| 2846 | // is no security boundary within an uid. |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2847 | return false; |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2848 | } else if (otherInfo->inputConfig.test(gui::WindowInfo::InputConfig::TRUSTED_OVERLAY)) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2849 | return false; |
| 2850 | } else if (otherInfo->displayId != info->displayId) { |
| 2851 | return false; |
| 2852 | } |
| 2853 | return true; |
| 2854 | } |
| 2855 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2856 | /** |
| 2857 | * Returns touch occlusion information in the form of TouchOcclusionInfo. To check if the touch is |
| 2858 | * untrusted, one should check: |
| 2859 | * |
| 2860 | * 1. If result.hasBlockingOcclusion is true. |
| 2861 | * If it's, it means the touch should be blocked due to a window with occlusion mode of |
| 2862 | * BLOCK_UNTRUSTED. |
| 2863 | * |
| 2864 | * 2. If result.obscuringOpacity > mMaximumObscuringOpacityForTouch. |
| 2865 | * If it is (and 1 is false), then the touch should be blocked because a stack of windows |
| 2866 | * (possibly only one) with occlusion mode of USE_OPACITY from one UID resulted in a composed |
| 2867 | * obscuring opacity above the threshold. Note that if there was no window of occlusion mode |
| 2868 | * USE_OPACITY, result.obscuringOpacity would've been 0 and since |
| 2869 | * mMaximumObscuringOpacityForTouch >= 0, the condition above would never be true. |
| 2870 | * |
| 2871 | * If neither of those is true, then it means the touch can be allowed. |
| 2872 | */ |
| 2873 | InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2874 | const sp<WindowInfoHandle>& windowHandle, int32_t x, int32_t y) const { |
| 2875 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2876 | int32_t displayId = windowInfo->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2877 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2878 | TouchOcclusionInfo info; |
| 2879 | info.hasBlockingOcclusion = false; |
| 2880 | info.obscuringOpacity = 0; |
| 2881 | info.obscuringUid = -1; |
| 2882 | std::map<int32_t, float> opacityByUid; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2883 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2884 | if (windowHandle == otherHandle) { |
| 2885 | break; // All future windows are below us. Exit early. |
| 2886 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2887 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 2888 | if (canBeObscuredBy(windowHandle, otherHandle) && otherInfo->frameContainsPoint(x, y) && |
| 2889 | !haveSameApplicationToken(windowInfo, otherInfo)) { |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2890 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2891 | info.debugInfo.push_back( |
| 2892 | dumpWindowForTouchOcclusion(otherInfo, /* isTouchedWindow */ false)); |
| 2893 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2894 | // canBeObscuredBy() has returned true above, which means this window is untrusted, so |
| 2895 | // we perform the checks below to see if the touch can be propagated or not based on the |
| 2896 | // window's touch occlusion mode |
| 2897 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::BLOCK_UNTRUSTED) { |
| 2898 | info.hasBlockingOcclusion = true; |
| 2899 | info.obscuringUid = otherInfo->ownerUid; |
| 2900 | info.obscuringPackage = otherInfo->packageName; |
| 2901 | break; |
| 2902 | } |
| 2903 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::USE_OPACITY) { |
| 2904 | uint32_t uid = otherInfo->ownerUid; |
| 2905 | float opacity = |
| 2906 | (opacityByUid.find(uid) == opacityByUid.end()) ? 0 : opacityByUid[uid]; |
| 2907 | // Given windows A and B: |
| 2908 | // opacity(A, B) = 1 - [1 - opacity(A)] * [1 - opacity(B)] |
| 2909 | opacity = 1 - (1 - opacity) * (1 - otherInfo->alpha); |
| 2910 | opacityByUid[uid] = opacity; |
| 2911 | if (opacity > info.obscuringOpacity) { |
| 2912 | info.obscuringOpacity = opacity; |
| 2913 | info.obscuringUid = uid; |
| 2914 | info.obscuringPackage = otherInfo->packageName; |
| 2915 | } |
| 2916 | } |
| 2917 | } |
| 2918 | } |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2919 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2920 | info.debugInfo.push_back( |
| 2921 | dumpWindowForTouchOcclusion(windowInfo, /* isTouchedWindow */ true)); |
| 2922 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2923 | return info; |
| 2924 | } |
| 2925 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2926 | std::string InputDispatcher::dumpWindowForTouchOcclusion(const WindowInfo* info, |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2927 | bool isTouchedWindow) const { |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 2928 | return StringPrintf(INDENT2 "* %spackage=%s/%" PRId32 ", id=%" PRId32 ", mode=%s, alpha=%.2f, " |
| 2929 | "frame=[%" PRId32 ",%" PRId32 "][%" PRId32 ",%" PRId32 |
| 2930 | "], touchableRegion=%s, window={%s}, inputConfig={%s}, " |
| 2931 | "hasToken=%s, applicationInfo.name=%s, applicationInfo.token=%s\n", |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2932 | isTouchedWindow ? "[TOUCHED] " : "", info->packageName.c_str(), |
| 2933 | info->ownerUid, info->id, toString(info->touchOcclusionMode).c_str(), |
| 2934 | info->alpha, info->frameLeft, info->frameTop, info->frameRight, |
| 2935 | info->frameBottom, dumpRegion(info->touchableRegion).c_str(), |
| 2936 | info->name.c_str(), info->inputConfig.string().c_str(), |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 2937 | toString(info->token != nullptr), info->applicationInfo.name.c_str(), |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 2938 | binderToString(info->applicationInfo.token).c_str()); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2939 | } |
| 2940 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2941 | bool InputDispatcher::isTouchTrustedLocked(const TouchOcclusionInfo& occlusionInfo) const { |
| 2942 | if (occlusionInfo.hasBlockingOcclusion) { |
| 2943 | ALOGW("Untrusted touch due to occlusion by %s/%d", occlusionInfo.obscuringPackage.c_str(), |
| 2944 | occlusionInfo.obscuringUid); |
| 2945 | return false; |
| 2946 | } |
| 2947 | if (occlusionInfo.obscuringOpacity > mMaximumObscuringOpacityForTouch) { |
| 2948 | ALOGW("Untrusted touch due to occlusion by %s/%d (obscuring opacity = " |
| 2949 | "%.2f, maximum allowed = %.2f)", |
| 2950 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid, |
| 2951 | occlusionInfo.obscuringOpacity, mMaximumObscuringOpacityForTouch); |
| 2952 | return false; |
| 2953 | } |
| 2954 | return true; |
| 2955 | } |
| 2956 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2957 | bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2958 | int32_t x, int32_t y) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2959 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2960 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 2961 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2962 | if (windowHandle == otherHandle) { |
| 2963 | break; // All future windows are below us. Exit early. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2964 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2965 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2966 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2967 | otherInfo->frameContainsPoint(x, y)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2968 | return true; |
| 2969 | } |
| 2970 | } |
| 2971 | return false; |
| 2972 | } |
| 2973 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2974 | bool InputDispatcher::isWindowObscuredLocked(const sp<WindowInfoHandle>& windowHandle) const { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2975 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2976 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 2977 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 2978 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2979 | if (windowHandle == otherHandle) { |
| 2980 | break; // All future windows are below us. Exit early. |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2981 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2982 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2983 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2984 | otherInfo->overlaps(windowInfo)) { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2985 | return true; |
| 2986 | } |
| 2987 | } |
| 2988 | return false; |
| 2989 | } |
| 2990 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2991 | std::string InputDispatcher::getApplicationWindowLabel( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2992 | const InputApplicationHandle* applicationHandle, const sp<WindowInfoHandle>& windowHandle) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2993 | if (applicationHandle != nullptr) { |
| 2994 | if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2995 | return applicationHandle->getName() + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2996 | } else { |
| 2997 | return applicationHandle->getName(); |
| 2998 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2999 | } else if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 3000 | return windowHandle->getInfo()->applicationInfo.name + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3001 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3002 | return "<unknown application or window>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3003 | } |
| 3004 | } |
| 3005 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3006 | void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3007 | if (!isUserActivityEvent(eventEntry)) { |
| 3008 | // 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] | 3009 | return; |
| 3010 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3011 | int32_t displayId = getTargetDisplayId(eventEntry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3012 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3013 | if (focusedWindowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3014 | const WindowInfo* info = focusedWindowHandle->getInfo(); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 3015 | if (info->inputConfig.test(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3016 | if (DEBUG_DISPATCH_CYCLE) { |
| 3017 | ALOGD("Not poking user activity: disabled by window '%s'.", info->name.c_str()); |
| 3018 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3019 | return; |
| 3020 | } |
| 3021 | } |
| 3022 | |
| 3023 | int32_t eventType = USER_ACTIVITY_EVENT_OTHER; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3024 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3025 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3026 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 3027 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3028 | return; |
| 3029 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3030 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3031 | if (MotionEvent::isTouchEvent(motionEntry.source, motionEntry.action)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3032 | eventType = USER_ACTIVITY_EVENT_TOUCH; |
| 3033 | } |
| 3034 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3035 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3036 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3037 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 3038 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3039 | return; |
| 3040 | } |
| 3041 | eventType = USER_ACTIVITY_EVENT_BUTTON; |
| 3042 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3043 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3044 | default: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3045 | LOG_ALWAYS_FATAL("%s events are not user activity", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3046 | ftl::enum_string(eventEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3047 | break; |
| 3048 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3049 | } |
| 3050 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3051 | auto command = [this, eventTime = eventEntry.eventTime, eventType, displayId]() |
| 3052 | REQUIRES(mLock) { |
| 3053 | scoped_unlock unlock(mLock); |
| 3054 | mPolicy->pokeUserActivity(eventTime, eventType, displayId); |
| 3055 | }; |
| 3056 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3057 | } |
| 3058 | |
| 3059 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3060 | const std::shared_ptr<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3061 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3062 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3063 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3064 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3065 | StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3066 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3067 | ATRACE_NAME(message.c_str()); |
| 3068 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3069 | if (DEBUG_DISPATCH_CYCLE) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3070 | ALOGD("channel '%s' ~ prepareDispatchCycle - flags=%s, " |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 3071 | "globalScaleFactor=%f, pointerIds=%s %s", |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3072 | connection->getInputChannelName().c_str(), inputTarget.flags.string().c_str(), |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 3073 | inputTarget.globalScaleFactor, bitsetToString(inputTarget.pointerIds).c_str(), |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3074 | inputTarget.getPointerInfoString().c_str()); |
| 3075 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3076 | |
| 3077 | // Skip this event if the connection status is not normal. |
| 3078 | // 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] | 3079 | if (connection->status != Connection::Status::NORMAL) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3080 | if (DEBUG_DISPATCH_CYCLE) { |
| 3081 | ALOGD("channel '%s' ~ Dropping event because the channel status is %s", |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3082 | connection->getInputChannelName().c_str(), |
| 3083 | ftl::enum_string(connection->status).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3084 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3085 | return; |
| 3086 | } |
| 3087 | |
| 3088 | // Split a motion event if needed. |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3089 | if (inputTarget.flags.test(InputTarget::Flags::SPLIT)) { |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3090 | LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3091 | "Entry type %s should not have Flags::SPLIT", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3092 | ftl::enum_string(eventEntry->type).c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3093 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3094 | const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry); |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3095 | if (inputTarget.pointerIds.count() != originalMotionEntry.pointerCount) { |
Siarhei Vishniakou | 16e4fa0 | 2023-02-16 17:48:56 -0800 | [diff] [blame] | 3096 | if (!inputTarget.firstDownTimeInTarget.has_value()) { |
| 3097 | logDispatchStateLocked(); |
| 3098 | LOG(FATAL) << "Splitting motion events requires a down time to be set for the " |
| 3099 | "target on connection " |
| 3100 | << connection->getInputChannelName() << " for " |
| 3101 | << originalMotionEntry.getDescription(); |
| 3102 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3103 | std::unique_ptr<MotionEntry> splitMotionEntry = |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3104 | splitMotionEvent(originalMotionEntry, inputTarget.pointerIds, |
| 3105 | inputTarget.firstDownTimeInTarget.value()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3106 | if (!splitMotionEntry) { |
| 3107 | return; // split event was dropped |
| 3108 | } |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3109 | if (splitMotionEntry->action == AMOTION_EVENT_ACTION_CANCEL) { |
| 3110 | std::string reason = std::string("reason=pointer cancel on split window"); |
| 3111 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 3112 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 3113 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3114 | if (DEBUG_FOCUS) { |
| 3115 | ALOGD("channel '%s' ~ Split motion event.", |
| 3116 | connection->getInputChannelName().c_str()); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3117 | logOutboundMotionDetails(" ", *splitMotionEntry); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3118 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3119 | enqueueDispatchEntriesLocked(currentTime, connection, std::move(splitMotionEntry), |
| 3120 | inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3121 | return; |
| 3122 | } |
| 3123 | } |
| 3124 | |
| 3125 | // Not splitting. Enqueue dispatch entries for the event as is. |
| 3126 | enqueueDispatchEntriesLocked(currentTime, connection, eventEntry, inputTarget); |
| 3127 | } |
| 3128 | |
| 3129 | void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3130 | const std::shared_ptr<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3131 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3132 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3133 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3134 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3135 | StringPrintf("enqueueDispatchEntriesLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3136 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3137 | ATRACE_NAME(message.c_str()); |
| 3138 | } |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3139 | LOG_ALWAYS_FATAL_IF(!inputTarget.flags.any(InputTarget::DISPATCH_MASK), |
| 3140 | "No dispatch flags are set for %s", eventEntry->getDescription().c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3141 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 3142 | const bool wasEmpty = connection->outboundQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3143 | |
| 3144 | // Enqueue dispatch entries for the requested modes. |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3145 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3146 | InputTarget::Flags::DISPATCH_AS_HOVER_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3147 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3148 | InputTarget::Flags::DISPATCH_AS_OUTSIDE); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3149 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3150 | InputTarget::Flags::DISPATCH_AS_HOVER_ENTER); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3151 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3152 | InputTarget::Flags::DISPATCH_AS_IS); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3153 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3154 | InputTarget::Flags::DISPATCH_AS_SLIPPERY_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3155 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3156 | InputTarget::Flags::DISPATCH_AS_SLIPPERY_ENTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3157 | |
| 3158 | // If the outbound queue was previously empty, start the dispatch cycle going. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3159 | if (wasEmpty && !connection->outboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3160 | startDispatchCycleLocked(currentTime, connection); |
| 3161 | } |
| 3162 | } |
| 3163 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3164 | void InputDispatcher::enqueueDispatchEntryLocked(const std::shared_ptr<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3165 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3166 | const InputTarget& inputTarget, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3167 | ftl::Flags<InputTarget::Flags> dispatchMode) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3168 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3169 | std::string message = StringPrintf("enqueueDispatchEntry(inputChannel=%s, dispatchMode=%s)", |
| 3170 | connection->getInputChannelName().c_str(), |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3171 | dispatchMode.string().c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3172 | ATRACE_NAME(message.c_str()); |
| 3173 | } |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3174 | ftl::Flags<InputTarget::Flags> inputTargetFlags = inputTarget.flags; |
| 3175 | if (!inputTargetFlags.any(dispatchMode)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3176 | return; |
| 3177 | } |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3178 | |
| 3179 | inputTargetFlags.clear(InputTarget::DISPATCH_MASK); |
| 3180 | inputTargetFlags |= dispatchMode; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3181 | |
| 3182 | // This is a new event. |
| 3183 | // Enqueue a new dispatch entry onto the outbound queue for this connection. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3184 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3185 | createDispatchEntry(inputTarget, eventEntry, inputTargetFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3186 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3187 | // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a |
| 3188 | // different EventEntry than what was passed in. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3189 | EventEntry& newEntry = *(dispatchEntry->eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3190 | // Apply target flags and update the connection's input state. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3191 | switch (newEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3192 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3193 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3194 | dispatchEntry->resolvedEventId = keyEntry.id; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3195 | dispatchEntry->resolvedAction = keyEntry.action; |
| 3196 | dispatchEntry->resolvedFlags = keyEntry.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3197 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3198 | if (!connection->inputState.trackKey(keyEntry, dispatchEntry->resolvedAction, |
| 3199 | dispatchEntry->resolvedFlags)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3200 | if (DEBUG_DISPATCH_CYCLE) { |
| 3201 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent key " |
| 3202 | "event", |
| 3203 | connection->getInputChannelName().c_str()); |
| 3204 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3205 | return; // skip the inconsistent event |
| 3206 | } |
| 3207 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3208 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3209 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3210 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3211 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3212 | // Assign a default value to dispatchEntry that will never be generated by InputReader, |
| 3213 | // and assign a InputDispatcher value if it doesn't change in the if-else chain below. |
| 3214 | constexpr int32_t DEFAULT_RESOLVED_EVENT_ID = |
| 3215 | static_cast<int32_t>(IdGenerator::Source::OTHER); |
| 3216 | dispatchEntry->resolvedEventId = DEFAULT_RESOLVED_EVENT_ID; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3217 | if (dispatchMode.test(InputTarget::Flags::DISPATCH_AS_OUTSIDE)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3218 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3219 | } else if (dispatchMode.test(InputTarget::Flags::DISPATCH_AS_HOVER_EXIT)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3220 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3221 | } else if (dispatchMode.test(InputTarget::Flags::DISPATCH_AS_HOVER_ENTER)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3222 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3223 | } else if (dispatchMode.test(InputTarget::Flags::DISPATCH_AS_SLIPPERY_EXIT)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3224 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_CANCEL; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3225 | } else if (dispatchMode.test(InputTarget::Flags::DISPATCH_AS_SLIPPERY_ENTER)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3226 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_DOWN; |
| 3227 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3228 | dispatchEntry->resolvedAction = motionEntry.action; |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3229 | dispatchEntry->resolvedEventId = motionEntry.id; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3230 | } |
| 3231 | if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE && |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3232 | !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source, |
| 3233 | motionEntry.displayId)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3234 | if (DEBUG_DISPATCH_CYCLE) { |
| 3235 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: filling in missing hover " |
| 3236 | "enter event", |
| 3237 | connection->getInputChannelName().c_str()); |
| 3238 | } |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3239 | // We keep the 'resolvedEventId' here equal to the original 'motionEntry.id' because |
| 3240 | // this is a one-to-one event conversion. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3241 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 3242 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3243 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3244 | dispatchEntry->resolvedFlags = motionEntry.flags; |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 3245 | if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_CANCEL) { |
| 3246 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_CANCELED; |
| 3247 | } |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3248 | if (dispatchEntry->targetFlags.test(InputTarget::Flags::WINDOW_IS_OBSCURED)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3249 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; |
| 3250 | } |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3251 | if (dispatchEntry->targetFlags.test(InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3252 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 3253 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3254 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3255 | if (!connection->inputState.trackMotion(motionEntry, dispatchEntry->resolvedAction, |
| 3256 | dispatchEntry->resolvedFlags)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3257 | if (DEBUG_DISPATCH_CYCLE) { |
| 3258 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent motion " |
| 3259 | "event", |
| 3260 | connection->getInputChannelName().c_str()); |
| 3261 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3262 | return; // skip the inconsistent event |
| 3263 | } |
| 3264 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3265 | dispatchEntry->resolvedEventId = |
| 3266 | dispatchEntry->resolvedEventId == DEFAULT_RESOLVED_EVENT_ID |
| 3267 | ? mIdGenerator.nextId() |
| 3268 | : motionEntry.id; |
| 3269 | if (ATRACE_ENABLED() && dispatchEntry->resolvedEventId != motionEntry.id) { |
| 3270 | std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32 |
| 3271 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3272 | motionEntry.id, dispatchEntry->resolvedEventId); |
| 3273 | ATRACE_NAME(message.c_str()); |
| 3274 | } |
| 3275 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 3276 | if ((motionEntry.flags & AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) && |
| 3277 | (motionEntry.policyFlags & POLICY_FLAG_TRUSTED)) { |
| 3278 | // Skip reporting pointer down outside focus to the policy. |
| 3279 | break; |
| 3280 | } |
| 3281 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3282 | dispatchPointerDownOutsideFocus(motionEntry.source, dispatchEntry->resolvedAction, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3283 | inputTarget.inputChannel->getConnectionToken()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3284 | |
| 3285 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3286 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3287 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3288 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3289 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3290 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3291 | break; |
| 3292 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3293 | case EventEntry::Type::SENSOR: { |
| 3294 | LOG_ALWAYS_FATAL("SENSOR events should not go to apps via input channel"); |
| 3295 | break; |
| 3296 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3297 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 3298 | case EventEntry::Type::DEVICE_RESET: { |
| 3299 | LOG_ALWAYS_FATAL("%s events should not go to apps", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3300 | ftl::enum_string(newEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3301 | break; |
| 3302 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3303 | } |
| 3304 | |
| 3305 | // Remember that we are waiting for this dispatch to complete. |
| 3306 | if (dispatchEntry->hasForegroundTarget()) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3307 | incrementPendingForegroundDispatches(newEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3308 | } |
| 3309 | |
| 3310 | // Enqueue the dispatch entry. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3311 | connection->outboundQueue.push_back(dispatchEntry.release()); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3312 | traceOutboundQueueLength(*connection); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3313 | } |
| 3314 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3315 | /** |
| 3316 | * This function is purely for debugging. It helps us understand where the user interaction |
| 3317 | * was taking place. For example, if user is touching launcher, we will see a log that user |
| 3318 | * started interacting with launcher. In that example, the event would go to the wallpaper as well. |
| 3319 | * We will see both launcher and wallpaper in that list. |
| 3320 | * Once the interaction with a particular set of connections starts, no new logs will be printed |
| 3321 | * until the set of interacted connections changes. |
| 3322 | * |
| 3323 | * The following items are skipped, to reduce the logspam: |
| 3324 | * ACTION_OUTSIDE: any windows that are receiving ACTION_OUTSIDE are not logged |
| 3325 | * ACTION_UP: any windows that receive ACTION_UP are not logged (for both keys and motions). |
| 3326 | * This includes situations like the soft BACK button key. When the user releases (lifts up the |
| 3327 | * finger) the back button, then navigation bar will inject KEYCODE_BACK with ACTION_UP. |
| 3328 | * Both of those ACTION_UP events would not be logged |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3329 | */ |
| 3330 | void InputDispatcher::updateInteractionTokensLocked(const EventEntry& entry, |
| 3331 | const std::vector<InputTarget>& targets) { |
| 3332 | // Skip ACTION_UP events, and all events other than keys and motions |
| 3333 | if (entry.type == EventEntry::Type::KEY) { |
| 3334 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 3335 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
| 3336 | return; |
| 3337 | } |
| 3338 | } else if (entry.type == EventEntry::Type::MOTION) { |
| 3339 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 3340 | if (motionEntry.action == AMOTION_EVENT_ACTION_UP || |
| 3341 | motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
| 3342 | return; |
| 3343 | } |
| 3344 | } else { |
| 3345 | return; // Not a key or a motion |
| 3346 | } |
| 3347 | |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 3348 | std::unordered_set<sp<IBinder>, StrongPointerHash<IBinder>> newConnectionTokens; |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3349 | std::vector<std::shared_ptr<Connection>> newConnections; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3350 | for (const InputTarget& target : targets) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3351 | if (target.flags.test(InputTarget::Flags::DISPATCH_AS_OUTSIDE)) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3352 | continue; // Skip windows that receive ACTION_OUTSIDE |
| 3353 | } |
| 3354 | |
| 3355 | sp<IBinder> token = target.inputChannel->getConnectionToken(); |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3356 | std::shared_ptr<Connection> connection = getConnectionLocked(token); |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3357 | if (connection == nullptr) { |
| 3358 | continue; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3359 | } |
| 3360 | newConnectionTokens.insert(std::move(token)); |
| 3361 | newConnections.emplace_back(connection); |
| 3362 | } |
| 3363 | if (newConnectionTokens == mInteractionConnectionTokens) { |
| 3364 | return; // no change |
| 3365 | } |
| 3366 | mInteractionConnectionTokens = newConnectionTokens; |
| 3367 | |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3368 | std::string targetList; |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3369 | for (const std::shared_ptr<Connection>& connection : newConnections) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3370 | targetList += connection->getWindowName() + ", "; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3371 | } |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3372 | std::string message = "Interaction with: " + targetList; |
| 3373 | if (targetList.empty()) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3374 | message += "<none>"; |
| 3375 | } |
| 3376 | android_log_event_list(LOGTAG_INPUT_INTERACTION) << message << LOG_ID_EVENTS; |
| 3377 | } |
| 3378 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3379 | void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3380 | const sp<IBinder>& token) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3381 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3382 | uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK; |
| 3383 | if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3384 | return; |
| 3385 | } |
| 3386 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 3387 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3388 | if (focusedToken == token) { |
| 3389 | // ignore since token is focused |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3390 | return; |
| 3391 | } |
| 3392 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3393 | auto command = [this, token]() REQUIRES(mLock) { |
| 3394 | scoped_unlock unlock(mLock); |
| 3395 | mPolicy->onPointerDownOutsideFocus(token); |
| 3396 | }; |
| 3397 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3398 | } |
| 3399 | |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3400 | status_t InputDispatcher::publishMotionEvent(Connection& connection, |
| 3401 | DispatchEntry& dispatchEntry) const { |
| 3402 | const EventEntry& eventEntry = *(dispatchEntry.eventEntry); |
| 3403 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 3404 | |
| 3405 | PointerCoords scaledCoords[MAX_POINTERS]; |
| 3406 | const PointerCoords* usingCoords = motionEntry.pointerCoords; |
| 3407 | |
| 3408 | // Set the X and Y offset and X and Y scale depending on the input source. |
| 3409 | if ((motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) && |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3410 | !(dispatchEntry.targetFlags.test(InputTarget::Flags::ZERO_COORDS))) { |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3411 | float globalScaleFactor = dispatchEntry.globalScaleFactor; |
| 3412 | if (globalScaleFactor != 1.0f) { |
| 3413 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
| 3414 | scaledCoords[i] = motionEntry.pointerCoords[i]; |
| 3415 | // Don't apply window scale here since we don't want scale to affect raw |
| 3416 | // coordinates. The scale will be sent back to the client and applied |
| 3417 | // later when requesting relative coordinates. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 3418 | scaledCoords[i].scale(globalScaleFactor, /*windowXScale=*/1, /*windowYScale=*/1); |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3419 | } |
| 3420 | usingCoords = scaledCoords; |
| 3421 | } |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3422 | } else if (dispatchEntry.targetFlags.test(InputTarget::Flags::ZERO_COORDS)) { |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3423 | // We don't want the dispatch target to know the coordinates |
| 3424 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
| 3425 | scaledCoords[i].clear(); |
| 3426 | } |
| 3427 | usingCoords = scaledCoords; |
| 3428 | } |
| 3429 | |
| 3430 | std::array<uint8_t, 32> hmac = getSignature(motionEntry, dispatchEntry); |
| 3431 | |
| 3432 | // Publish the motion event. |
| 3433 | return connection.inputPublisher |
| 3434 | .publishMotionEvent(dispatchEntry.seq, dispatchEntry.resolvedEventId, |
| 3435 | motionEntry.deviceId, motionEntry.source, motionEntry.displayId, |
| 3436 | std::move(hmac), dispatchEntry.resolvedAction, |
| 3437 | motionEntry.actionButton, dispatchEntry.resolvedFlags, |
| 3438 | motionEntry.edgeFlags, motionEntry.metaState, |
| 3439 | motionEntry.buttonState, motionEntry.classification, |
| 3440 | dispatchEntry.transform, motionEntry.xPrecision, |
| 3441 | motionEntry.yPrecision, motionEntry.xCursorPosition, |
| 3442 | motionEntry.yCursorPosition, dispatchEntry.rawTransform, |
| 3443 | motionEntry.downTime, motionEntry.eventTime, |
| 3444 | motionEntry.pointerCount, motionEntry.pointerProperties, |
| 3445 | usingCoords); |
| 3446 | } |
| 3447 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3448 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3449 | const std::shared_ptr<Connection>& connection) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3450 | if (ATRACE_ENABLED()) { |
| 3451 | std::string message = StringPrintf("startDispatchCycleLocked(inputChannel=%s)", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3452 | connection->getInputChannelName().c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3453 | ATRACE_NAME(message.c_str()); |
| 3454 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3455 | if (DEBUG_DISPATCH_CYCLE) { |
| 3456 | ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str()); |
| 3457 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3458 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3459 | while (connection->status == Connection::Status::NORMAL && !connection->outboundQueue.empty()) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3460 | DispatchEntry* dispatchEntry = connection->outboundQueue.front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3461 | dispatchEntry->deliveryTime = currentTime; |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3462 | const std::chrono::nanoseconds timeout = getDispatchingTimeoutLocked(connection); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3463 | dispatchEntry->timeoutTime = currentTime + timeout.count(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3464 | |
| 3465 | // Publish the event. |
| 3466 | status_t status; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3467 | const EventEntry& eventEntry = *(dispatchEntry->eventEntry); |
| 3468 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3469 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3470 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 3471 | std::array<uint8_t, 32> hmac = getSignature(keyEntry, *dispatchEntry); |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 3472 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 3473 | LOG(DEBUG) << "Publishing " << *dispatchEntry << " to " |
| 3474 | << connection->getInputChannelName(); |
| 3475 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3476 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3477 | // Publish the key event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3478 | status = connection->inputPublisher |
| 3479 | .publishKeyEvent(dispatchEntry->seq, |
| 3480 | dispatchEntry->resolvedEventId, keyEntry.deviceId, |
| 3481 | keyEntry.source, keyEntry.displayId, |
| 3482 | std::move(hmac), dispatchEntry->resolvedAction, |
| 3483 | dispatchEntry->resolvedFlags, keyEntry.keyCode, |
| 3484 | keyEntry.scanCode, keyEntry.metaState, |
| 3485 | keyEntry.repeatCount, keyEntry.downTime, |
| 3486 | keyEntry.eventTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3487 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3488 | } |
| 3489 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3490 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 3491 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 3492 | LOG(DEBUG) << "Publishing " << *dispatchEntry << " to " |
| 3493 | << connection->getInputChannelName(); |
| 3494 | } |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3495 | status = publishMotionEvent(*connection, *dispatchEntry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3496 | break; |
| 3497 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3498 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3499 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3500 | const FocusEntry& focusEntry = static_cast<const FocusEntry&>(eventEntry); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3501 | status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3502 | focusEntry.id, |
Antonio Kantek | 3cfec7b | 2021-11-05 18:26:17 -0700 | [diff] [blame] | 3503 | focusEntry.hasFocus); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3504 | break; |
| 3505 | } |
| 3506 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3507 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 3508 | const TouchModeEntry& touchModeEntry = |
| 3509 | static_cast<const TouchModeEntry&>(eventEntry); |
| 3510 | status = connection->inputPublisher |
| 3511 | .publishTouchModeEvent(dispatchEntry->seq, touchModeEntry.id, |
| 3512 | touchModeEntry.inTouchMode); |
| 3513 | |
| 3514 | break; |
| 3515 | } |
| 3516 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3517 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 3518 | const auto& captureEntry = |
| 3519 | static_cast<const PointerCaptureChangedEntry&>(eventEntry); |
| 3520 | status = connection->inputPublisher |
| 3521 | .publishCaptureEvent(dispatchEntry->seq, captureEntry.id, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 3522 | captureEntry.pointerCaptureRequest.enable); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3523 | break; |
| 3524 | } |
| 3525 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3526 | case EventEntry::Type::DRAG: { |
| 3527 | const DragEntry& dragEntry = static_cast<const DragEntry&>(eventEntry); |
| 3528 | status = connection->inputPublisher.publishDragEvent(dispatchEntry->seq, |
| 3529 | dragEntry.id, dragEntry.x, |
| 3530 | dragEntry.y, |
| 3531 | dragEntry.isExiting); |
| 3532 | break; |
| 3533 | } |
| 3534 | |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3535 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3536 | case EventEntry::Type::DEVICE_RESET: |
| 3537 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3538 | LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3539 | ftl::enum_string(eventEntry.type).c_str()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3540 | return; |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3541 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3542 | } |
| 3543 | |
| 3544 | // Check the result. |
| 3545 | if (status) { |
| 3546 | if (status == WOULD_BLOCK) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3547 | if (connection->waitQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3548 | ALOGE("channel '%s' ~ Could not publish event because the pipe is full. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3549 | "This is unexpected because the wait queue is empty, so the pipe " |
| 3550 | "should be empty and we shouldn't have any problems writing an " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3551 | "event to it, status=%s(%d)", |
| 3552 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3553 | status); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 3554 | abortBrokenDispatchCycleLocked(currentTime, connection, /*notify=*/true); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3555 | } else { |
| 3556 | // Pipe is full and we are waiting for the app to finish process some events |
| 3557 | // before sending more events to it. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3558 | if (DEBUG_DISPATCH_CYCLE) { |
| 3559 | ALOGD("channel '%s' ~ Could not publish event because the pipe is full, " |
| 3560 | "waiting for the application to catch up", |
| 3561 | connection->getInputChannelName().c_str()); |
| 3562 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3563 | } |
| 3564 | } else { |
| 3565 | ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3566 | "status=%s(%d)", |
| 3567 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3568 | status); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 3569 | abortBrokenDispatchCycleLocked(currentTime, connection, /*notify=*/true); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3570 | } |
| 3571 | return; |
| 3572 | } |
| 3573 | |
| 3574 | // Re-enqueue the event on the wait queue. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3575 | connection->outboundQueue.erase(std::remove(connection->outboundQueue.begin(), |
| 3576 | connection->outboundQueue.end(), |
| 3577 | dispatchEntry)); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3578 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3579 | connection->waitQueue.push_back(dispatchEntry); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3580 | if (connection->responsive) { |
| 3581 | mAnrTracker.insert(dispatchEntry->timeoutTime, |
| 3582 | connection->inputChannel->getConnectionToken()); |
| 3583 | } |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3584 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3585 | } |
| 3586 | } |
| 3587 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3588 | std::array<uint8_t, 32> InputDispatcher::sign(const VerifiedInputEvent& event) const { |
| 3589 | size_t size; |
| 3590 | switch (event.type) { |
| 3591 | case VerifiedInputEvent::Type::KEY: { |
| 3592 | size = sizeof(VerifiedKeyEvent); |
| 3593 | break; |
| 3594 | } |
| 3595 | case VerifiedInputEvent::Type::MOTION: { |
| 3596 | size = sizeof(VerifiedMotionEvent); |
| 3597 | break; |
| 3598 | } |
| 3599 | } |
| 3600 | const uint8_t* start = reinterpret_cast<const uint8_t*>(&event); |
| 3601 | return mHmacKeyManager.sign(start, size); |
| 3602 | } |
| 3603 | |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3604 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3605 | const MotionEntry& motionEntry, const DispatchEntry& dispatchEntry) const { |
Siarhei Vishniakou | bf88052 | 2023-05-01 11:03:22 -0700 | [diff] [blame] | 3606 | const int32_t actionMasked = MotionEvent::getActionMasked(dispatchEntry.resolvedAction); |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3607 | if (actionMasked != AMOTION_EVENT_ACTION_UP && actionMasked != AMOTION_EVENT_ACTION_DOWN) { |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3608 | // Only sign events up and down events as the purely move events |
| 3609 | // are tied to their up/down counterparts so signing would be redundant. |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3610 | return INVALID_HMAC; |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3611 | } |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3612 | |
| 3613 | VerifiedMotionEvent verifiedEvent = |
| 3614 | verifiedMotionEventFromMotionEntry(motionEntry, dispatchEntry.rawTransform); |
| 3615 | verifiedEvent.actionMasked = actionMasked; |
| 3616 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS; |
| 3617 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3618 | } |
| 3619 | |
| 3620 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3621 | const KeyEntry& keyEntry, const DispatchEntry& dispatchEntry) const { |
| 3622 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(keyEntry); |
| 3623 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_KEY_EVENT_FLAGS; |
| 3624 | verifiedEvent.action = dispatchEntry.resolvedAction; |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3625 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3626 | } |
| 3627 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3628 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3629 | const std::shared_ptr<Connection>& connection, |
| 3630 | uint32_t seq, bool handled, nsecs_t consumeTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3631 | if (DEBUG_DISPATCH_CYCLE) { |
| 3632 | ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s", |
| 3633 | connection->getInputChannelName().c_str(), seq, toString(handled)); |
| 3634 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3635 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3636 | if (connection->status == Connection::Status::BROKEN || |
| 3637 | connection->status == Connection::Status::ZOMBIE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3638 | return; |
| 3639 | } |
| 3640 | |
| 3641 | // Notify other system components and prepare to start the next dispatch cycle. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3642 | auto command = [this, currentTime, connection, seq, handled, consumeTime]() REQUIRES(mLock) { |
| 3643 | doDispatchCycleFinishedCommand(currentTime, connection, seq, handled, consumeTime); |
| 3644 | }; |
| 3645 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3646 | } |
| 3647 | |
| 3648 | void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3649 | const std::shared_ptr<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3650 | bool notify) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3651 | if (DEBUG_DISPATCH_CYCLE) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 3652 | LOG(DEBUG) << "channel '" << connection->getInputChannelName() << "'~ " << __func__ |
| 3653 | << " - notify=" << toString(notify); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3654 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3655 | |
| 3656 | // Clear the dispatch queues. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3657 | drainDispatchQueue(connection->outboundQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3658 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3659 | drainDispatchQueue(connection->waitQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3660 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3661 | |
| 3662 | // The connection appears to be unrecoverably broken. |
| 3663 | // Ignore already broken or zombie connections. |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3664 | if (connection->status == Connection::Status::NORMAL) { |
| 3665 | connection->status = Connection::Status::BROKEN; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3666 | |
| 3667 | if (notify) { |
| 3668 | // Notify other system components. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3669 | ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", |
| 3670 | connection->getInputChannelName().c_str()); |
| 3671 | |
| 3672 | auto command = [this, connection]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3673 | scoped_unlock unlock(mLock); |
| 3674 | mPolicy->notifyInputChannelBroken(connection->inputChannel->getConnectionToken()); |
| 3675 | }; |
| 3676 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3677 | } |
| 3678 | } |
| 3679 | } |
| 3680 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3681 | void InputDispatcher::drainDispatchQueue(std::deque<DispatchEntry*>& queue) { |
| 3682 | while (!queue.empty()) { |
| 3683 | DispatchEntry* dispatchEntry = queue.front(); |
| 3684 | queue.pop_front(); |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3685 | releaseDispatchEntry(dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3686 | } |
| 3687 | } |
| 3688 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3689 | void InputDispatcher::releaseDispatchEntry(DispatchEntry* dispatchEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3690 | if (dispatchEntry->hasForegroundTarget()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3691 | decrementPendingForegroundDispatches(*(dispatchEntry->eventEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3692 | } |
| 3693 | delete dispatchEntry; |
| 3694 | } |
| 3695 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3696 | int InputDispatcher::handleReceiveCallback(int events, sp<IBinder> connectionToken) { |
| 3697 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3698 | std::shared_ptr<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3699 | if (connection == nullptr) { |
| 3700 | ALOGW("Received looper callback for unknown input channel token %p. events=0x%x", |
| 3701 | connectionToken.get(), events); |
| 3702 | return 0; // remove the callback |
| 3703 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3704 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3705 | bool notify; |
| 3706 | if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { |
| 3707 | if (!(events & ALOOPER_EVENT_INPUT)) { |
| 3708 | ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. " |
| 3709 | "events=0x%x", |
| 3710 | connection->getInputChannelName().c_str(), events); |
| 3711 | return 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3712 | } |
| 3713 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3714 | nsecs_t currentTime = now(); |
| 3715 | bool gotOne = false; |
| 3716 | status_t status = OK; |
| 3717 | for (;;) { |
| 3718 | Result<InputPublisher::ConsumerResponse> result = |
| 3719 | connection->inputPublisher.receiveConsumerResponse(); |
| 3720 | if (!result.ok()) { |
| 3721 | status = result.error().code(); |
| 3722 | break; |
| 3723 | } |
| 3724 | |
| 3725 | if (std::holds_alternative<InputPublisher::Finished>(*result)) { |
| 3726 | const InputPublisher::Finished& finish = |
| 3727 | std::get<InputPublisher::Finished>(*result); |
| 3728 | finishDispatchCycleLocked(currentTime, connection, finish.seq, finish.handled, |
| 3729 | finish.consumeTime); |
| 3730 | } else if (std::holds_alternative<InputPublisher::Timeline>(*result)) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3731 | if (shouldReportMetricsForConnection(*connection)) { |
| 3732 | const InputPublisher::Timeline& timeline = |
| 3733 | std::get<InputPublisher::Timeline>(*result); |
| 3734 | mLatencyTracker |
| 3735 | .trackGraphicsLatency(timeline.inputEventId, |
| 3736 | connection->inputChannel->getConnectionToken(), |
| 3737 | std::move(timeline.graphicsTimeline)); |
| 3738 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3739 | } |
| 3740 | gotOne = true; |
| 3741 | } |
| 3742 | if (gotOne) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3743 | runCommandsLockedInterruptable(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3744 | if (status == WOULD_BLOCK) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3745 | return 1; |
| 3746 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3747 | } |
| 3748 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3749 | notify = status != DEAD_OBJECT || !connection->monitor; |
| 3750 | if (notify) { |
| 3751 | ALOGE("channel '%s' ~ Failed to receive finished signal. status=%s(%d)", |
| 3752 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3753 | status); |
| 3754 | } |
| 3755 | } else { |
| 3756 | // Monitor channels are never explicitly unregistered. |
| 3757 | // We do it automatically when the remote endpoint is closed so don't warn about them. |
| 3758 | const bool stillHaveWindowHandle = |
| 3759 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()) != nullptr; |
| 3760 | notify = !connection->monitor && stillHaveWindowHandle; |
| 3761 | if (notify) { |
| 3762 | ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred. events=0x%x", |
| 3763 | connection->getInputChannelName().c_str(), events); |
| 3764 | } |
| 3765 | } |
| 3766 | |
| 3767 | // Remove the channel. |
| 3768 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), notify); |
| 3769 | return 0; // remove the callback |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3770 | } |
| 3771 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3772 | void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3773 | const CancelationOptions& options) { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3774 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 3775 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3776 | } |
| 3777 | } |
| 3778 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3779 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3780 | const CancelationOptions& options) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 3781 | for (const auto& [_, monitors] : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3782 | for (const Monitor& monitor : monitors) { |
| 3783 | synthesizeCancelationEventsForInputChannelLocked(monitor.inputChannel, options); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3784 | } |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3785 | } |
| 3786 | } |
| 3787 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3788 | void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked( |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 3789 | const std::shared_ptr<InputChannel>& channel, const CancelationOptions& options) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3790 | std::shared_ptr<Connection> connection = getConnectionLocked(channel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3791 | if (connection == nullptr) { |
| 3792 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3793 | } |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3794 | |
| 3795 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3796 | } |
| 3797 | |
| 3798 | void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3799 | const std::shared_ptr<Connection>& connection, const CancelationOptions& options) { |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3800 | if (connection->status == Connection::Status::BROKEN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3801 | return; |
| 3802 | } |
| 3803 | |
| 3804 | nsecs_t currentTime = now(); |
| 3805 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3806 | std::vector<std::unique_ptr<EventEntry>> cancelationEvents = |
Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 3807 | connection->inputState.synthesizeCancelationEvents(currentTime, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3808 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3809 | if (cancelationEvents.empty()) { |
| 3810 | return; |
| 3811 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3812 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 3813 | 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] | 3814 | "with reality: %s, mode=%s.", |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3815 | connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason, |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3816 | ftl::enum_string(options.mode).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3817 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3818 | |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3819 | std::string reason = std::string("reason=").append(options.reason); |
| 3820 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 3821 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 3822 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3823 | InputTarget target; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3824 | sp<WindowInfoHandle> windowHandle = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3825 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3826 | if (windowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3827 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3828 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3829 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3830 | } |
| 3831 | target.inputChannel = connection->inputChannel; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3832 | target.flags = InputTarget::Flags::DISPATCH_AS_IS; |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3833 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 3834 | const bool wasEmpty = connection->outboundQueue.empty(); |
| 3835 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3836 | for (size_t i = 0; i < cancelationEvents.size(); i++) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3837 | std::unique_ptr<EventEntry> cancelationEventEntry = std::move(cancelationEvents[i]); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3838 | switch (cancelationEventEntry->type) { |
| 3839 | case EventEntry::Type::KEY: { |
| 3840 | logOutboundKeyDetails("cancel - ", |
| 3841 | static_cast<const KeyEntry&>(*cancelationEventEntry)); |
| 3842 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3843 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3844 | case EventEntry::Type::MOTION: { |
| 3845 | logOutboundMotionDetails("cancel - ", |
| 3846 | static_cast<const MotionEntry&>(*cancelationEventEntry)); |
| 3847 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3848 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3849 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3850 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3851 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3852 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3853 | LOG_ALWAYS_FATAL("Canceling %s events is not supported", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3854 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3855 | break; |
| 3856 | } |
| 3857 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3858 | case EventEntry::Type::DEVICE_RESET: |
| 3859 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3860 | 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] | 3861 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3862 | break; |
| 3863 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3864 | } |
| 3865 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3866 | enqueueDispatchEntryLocked(connection, std::move(cancelationEventEntry), target, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3867 | InputTarget::Flags::DISPATCH_AS_IS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3868 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3869 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 3870 | // If the outbound queue was previously empty, start the dispatch cycle going. |
| 3871 | if (wasEmpty && !connection->outboundQueue.empty()) { |
| 3872 | startDispatchCycleLocked(currentTime, connection); |
| 3873 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3874 | } |
| 3875 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3876 | void InputDispatcher::synthesizePointerDownEventsForConnectionLocked( |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3877 | const nsecs_t downTime, const std::shared_ptr<Connection>& connection, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 3878 | ftl::Flags<InputTarget::Flags> targetFlags) { |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3879 | if (connection->status == Connection::Status::BROKEN) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3880 | return; |
| 3881 | } |
| 3882 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3883 | std::vector<std::unique_ptr<EventEntry>> downEvents = |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3884 | connection->inputState.synthesizePointerDownEvents(downTime); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3885 | |
| 3886 | if (downEvents.empty()) { |
| 3887 | return; |
| 3888 | } |
| 3889 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3890 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3891 | ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.", |
| 3892 | connection->getInputChannelName().c_str(), downEvents.size()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3893 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3894 | |
| 3895 | InputTarget target; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3896 | sp<WindowInfoHandle> windowHandle = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3897 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3898 | if (windowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3899 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3900 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3901 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3902 | } |
| 3903 | target.inputChannel = connection->inputChannel; |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 3904 | target.flags = targetFlags; |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3905 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 3906 | const bool wasEmpty = connection->outboundQueue.empty(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3907 | for (std::unique_ptr<EventEntry>& downEventEntry : downEvents) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3908 | switch (downEventEntry->type) { |
| 3909 | case EventEntry::Type::MOTION: { |
| 3910 | logOutboundMotionDetails("down - ", |
| 3911 | static_cast<const MotionEntry&>(*downEventEntry)); |
| 3912 | break; |
| 3913 | } |
| 3914 | |
| 3915 | case EventEntry::Type::KEY: |
| 3916 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3917 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3918 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3919 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3920 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3921 | case EventEntry::Type::SENSOR: |
| 3922 | case EventEntry::Type::DRAG: { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3923 | 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] | 3924 | ftl::enum_string(downEventEntry->type).c_str()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3925 | break; |
| 3926 | } |
| 3927 | } |
| 3928 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3929 | enqueueDispatchEntryLocked(connection, std::move(downEventEntry), target, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3930 | InputTarget::Flags::DISPATCH_AS_IS); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3931 | } |
| 3932 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 3933 | // If the outbound queue was previously empty, start the dispatch cycle going. |
| 3934 | if (wasEmpty && !connection->outboundQueue.empty()) { |
| 3935 | startDispatchCycleLocked(downTime, connection); |
| 3936 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3937 | } |
| 3938 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 3939 | void InputDispatcher::synthesizeCancelationEventsForWindowLocked( |
| 3940 | const sp<WindowInfoHandle>& windowHandle, const CancelationOptions& options) { |
| 3941 | if (windowHandle != nullptr) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3942 | std::shared_ptr<Connection> wallpaperConnection = |
| 3943 | getConnectionLocked(windowHandle->getToken()); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 3944 | if (wallpaperConnection != nullptr) { |
| 3945 | synthesizeCancelationEventsForConnectionLocked(wallpaperConnection, options); |
| 3946 | } |
| 3947 | } |
| 3948 | } |
| 3949 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3950 | std::unique_ptr<MotionEntry> InputDispatcher::splitMotionEvent( |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 3951 | const MotionEntry& originalMotionEntry, std::bitset<MAX_POINTER_ID + 1> pointerIds, |
| 3952 | nsecs_t splitDownTime) { |
| 3953 | ALOG_ASSERT(pointerIds.any()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3954 | |
| 3955 | uint32_t splitPointerIndexMap[MAX_POINTERS]; |
| 3956 | PointerProperties splitPointerProperties[MAX_POINTERS]; |
| 3957 | PointerCoords splitPointerCoords[MAX_POINTERS]; |
| 3958 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3959 | uint32_t originalPointerCount = originalMotionEntry.pointerCount; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3960 | uint32_t splitPointerCount = 0; |
| 3961 | |
| 3962 | for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3963 | originalPointerIndex++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3964 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3965 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3966 | uint32_t pointerId = uint32_t(pointerProperties.id); |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 3967 | if (pointerIds.test(pointerId)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3968 | splitPointerIndexMap[splitPointerCount] = originalPointerIndex; |
| 3969 | splitPointerProperties[splitPointerCount].copyFrom(pointerProperties); |
| 3970 | splitPointerCoords[splitPointerCount].copyFrom( |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3971 | originalMotionEntry.pointerCoords[originalPointerIndex]); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3972 | splitPointerCount += 1; |
| 3973 | } |
| 3974 | } |
| 3975 | |
| 3976 | if (splitPointerCount != pointerIds.count()) { |
| 3977 | // This is bad. We are missing some of the pointers that we expected to deliver. |
| 3978 | // Most likely this indicates that we received an ACTION_MOVE events that has |
| 3979 | // different pointer ids than we expected based on the previous ACTION_DOWN |
| 3980 | // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers |
| 3981 | // in this way. |
| 3982 | ALOGW("Dropping split motion event because the pointer count is %d but " |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 3983 | "we expected there to be %zu pointers. This probably means we received " |
Siarhei Vishniakou | 16e4fa0 | 2023-02-16 17:48:56 -0800 | [diff] [blame] | 3984 | "a broken sequence of pointer ids from the input device: %s", |
| 3985 | splitPointerCount, pointerIds.count(), originalMotionEntry.getDescription().c_str()); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3986 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3987 | } |
| 3988 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3989 | int32_t action = originalMotionEntry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3990 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3991 | if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN || |
| 3992 | maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3993 | int32_t originalPointerIndex = getMotionEventActionPointerIndex(action); |
| 3994 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3995 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3996 | uint32_t pointerId = uint32_t(pointerProperties.id); |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 3997 | if (pointerIds.test(pointerId)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3998 | if (pointerIds.count() == 1) { |
| 3999 | // The first/last pointer went down/up. |
| 4000 | action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4001 | ? AMOTION_EVENT_ACTION_DOWN |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4002 | : (originalMotionEntry.flags & AMOTION_EVENT_FLAG_CANCELED) != 0 |
| 4003 | ? AMOTION_EVENT_ACTION_CANCEL |
| 4004 | : AMOTION_EVENT_ACTION_UP; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4005 | } else { |
| 4006 | // A secondary pointer went down/up. |
| 4007 | uint32_t splitPointerIndex = 0; |
| 4008 | while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { |
| 4009 | splitPointerIndex += 1; |
| 4010 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4011 | action = maskedAction | |
| 4012 | (splitPointerIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4013 | } |
| 4014 | } else { |
| 4015 | // An unrelated pointer changed. |
| 4016 | action = AMOTION_EVENT_ACTION_MOVE; |
| 4017 | } |
| 4018 | } |
| 4019 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4020 | if (action == AMOTION_EVENT_ACTION_DOWN) { |
| 4021 | LOG_ALWAYS_FATAL_IF(splitDownTime != originalMotionEntry.eventTime, |
| 4022 | "Split motion event has mismatching downTime and eventTime for " |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4023 | "ACTION_DOWN, motionEntry=%s, splitDownTime=%" PRId64, |
| 4024 | originalMotionEntry.getDescription().c_str(), splitDownTime); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4025 | } |
| 4026 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4027 | int32_t newId = mIdGenerator.nextId(); |
| 4028 | if (ATRACE_ENABLED()) { |
| 4029 | std::string message = StringPrintf("Split MotionEvent(id=0x%" PRIx32 |
| 4030 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 4031 | originalMotionEntry.id, newId); |
| 4032 | ATRACE_NAME(message.c_str()); |
| 4033 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4034 | std::unique_ptr<MotionEntry> splitMotionEntry = |
| 4035 | std::make_unique<MotionEntry>(newId, originalMotionEntry.eventTime, |
| 4036 | originalMotionEntry.deviceId, originalMotionEntry.source, |
| 4037 | originalMotionEntry.displayId, |
| 4038 | originalMotionEntry.policyFlags, action, |
| 4039 | originalMotionEntry.actionButton, |
| 4040 | originalMotionEntry.flags, originalMotionEntry.metaState, |
| 4041 | originalMotionEntry.buttonState, |
| 4042 | originalMotionEntry.classification, |
| 4043 | originalMotionEntry.edgeFlags, |
| 4044 | originalMotionEntry.xPrecision, |
| 4045 | originalMotionEntry.yPrecision, |
| 4046 | originalMotionEntry.xCursorPosition, |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4047 | originalMotionEntry.yCursorPosition, splitDownTime, |
| 4048 | splitPointerCount, splitPointerProperties, |
| 4049 | splitPointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4050 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 4051 | if (originalMotionEntry.injectionState) { |
| 4052 | splitMotionEntry->injectionState = originalMotionEntry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4053 | splitMotionEntry->injectionState->refCount += 1; |
| 4054 | } |
| 4055 | |
| 4056 | return splitMotionEntry; |
| 4057 | } |
| 4058 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4059 | void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4060 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4061 | ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args.eventTime); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4062 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4063 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4064 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4065 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4066 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4067 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4068 | std::unique_ptr<ConfigurationChangedEntry> newEntry = |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4069 | std::make_unique<ConfigurationChangedEntry>(args.id, args.eventTime); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4070 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4071 | } // release lock |
| 4072 | |
| 4073 | if (needWake) { |
| 4074 | mLooper->wake(); |
| 4075 | } |
| 4076 | } |
| 4077 | |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 4078 | /** |
| 4079 | * If one of the meta shortcuts is detected, process them here: |
Vaibhav Devmurari | 34cd5b0 | 2023-02-23 14:51:18 +0000 | [diff] [blame] | 4080 | * Meta + Backspace; Meta + Grave; Meta + Left arrow -> generate BACK |
| 4081 | * Most System shortcuts are handled in PhoneWindowManager.java except 'Back' shortcuts. Unlike |
| 4082 | * Back, other shortcuts DO NOT need to be sent to applications and are fully handled by the system. |
| 4083 | * But for Back key and Back shortcuts, we need to send KEYCODE_BACK to applications which can |
| 4084 | * potentially handle the back key presses. |
| 4085 | * Note: We don't send any Meta based KeyEvents to applications, so we need to convert to a KeyEvent |
| 4086 | * 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] | 4087 | */ |
| 4088 | void InputDispatcher::accelerateMetaShortcuts(const int32_t deviceId, const int32_t action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4089 | int32_t& keyCode, int32_t& metaState) { |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 4090 | if (metaState & AMETA_META_ON && action == AKEY_EVENT_ACTION_DOWN) { |
| 4091 | int32_t newKeyCode = AKEYCODE_UNKNOWN; |
Vaibhav Devmurari | 34cd5b0 | 2023-02-23 14:51:18 +0000 | [diff] [blame] | 4092 | if (keyCode == AKEYCODE_DEL || keyCode == AKEYCODE_GRAVE || keyCode == AKEYCODE_DPAD_LEFT) { |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 4093 | newKeyCode = AKEYCODE_BACK; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 4094 | } |
| 4095 | if (newKeyCode != AKEYCODE_UNKNOWN) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4096 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 4097 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4098 | mReplacedKeys[replacement] = newKeyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 4099 | keyCode = newKeyCode; |
| 4100 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 4101 | } |
| 4102 | } else if (action == AKEY_EVENT_ACTION_UP) { |
| 4103 | // In order to maintain a consistent stream of up and down events, check to see if the key |
| 4104 | // going up is one we've replaced in a down event and haven't yet replaced in an up event, |
| 4105 | // 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] | 4106 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 4107 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4108 | auto replacementIt = mReplacedKeys.find(replacement); |
| 4109 | if (replacementIt != mReplacedKeys.end()) { |
| 4110 | keyCode = replacementIt->second; |
| 4111 | mReplacedKeys.erase(replacementIt); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 4112 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 4113 | } |
| 4114 | } |
| 4115 | } |
| 4116 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4117 | void InputDispatcher::notifyKey(const NotifyKeyArgs& args) { |
Prabir Pradhan | 96282b0 | 2023-02-24 22:36:17 +0000 | [diff] [blame] | 4118 | ALOGD_IF(debugInboundEventDetails(), |
| 4119 | "notifyKey - id=%" PRIx32 ", eventTime=%" PRId64 |
| 4120 | ", deviceId=%d, source=%s, displayId=%" PRId32 |
| 4121 | "policyFlags=0x%x, action=%s, flags=0x%x, keyCode=%s, scanCode=0x%x, metaState=0x%x, " |
| 4122 | "downTime=%" PRId64, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4123 | args.id, args.eventTime, args.deviceId, inputEventSourceToString(args.source).c_str(), |
| 4124 | args.displayId, args.policyFlags, KeyEvent::actionToString(args.action), args.flags, |
| 4125 | KeyEvent::getLabel(args.keyCode), args.scanCode, args.metaState, args.downTime); |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4126 | Result<void> keyCheck = validateKeyEvent(args.action); |
| 4127 | if (!keyCheck.ok()) { |
| 4128 | LOG(ERROR) << "invalid key event: " << keyCheck.error(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4129 | return; |
| 4130 | } |
| 4131 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4132 | uint32_t policyFlags = args.policyFlags; |
| 4133 | int32_t flags = args.flags; |
| 4134 | int32_t metaState = args.metaState; |
Siarhei Vishniakou | 622bd32 | 2018-10-29 18:02:27 -0700 | [diff] [blame] | 4135 | // InputDispatcher tracks and generates key repeats on behalf of |
| 4136 | // whatever notifies it, so repeatCount should always be set to 0 |
| 4137 | constexpr int32_t repeatCount = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4138 | if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { |
| 4139 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 4140 | flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
| 4141 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4142 | if (policyFlags & POLICY_FLAG_FUNCTION) { |
| 4143 | metaState |= AMETA_FUNCTION_ON; |
| 4144 | } |
| 4145 | |
| 4146 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 4147 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4148 | int32_t keyCode = args.keyCode; |
| 4149 | accelerateMetaShortcuts(args.deviceId, args.action, keyCode, metaState); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 4150 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4151 | KeyEvent event; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4152 | event.initialize(args.id, args.deviceId, args.source, args.displayId, INVALID_HMAC, args.action, |
| 4153 | flags, keyCode, args.scanCode, metaState, repeatCount, args.downTime, |
| 4154 | args.eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4155 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4156 | android::base::Timer t; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4157 | mPolicy->interceptKeyBeforeQueueing(&event, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4158 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4159 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4160 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4161 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4162 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4163 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4164 | { // acquire lock |
| 4165 | mLock.lock(); |
| 4166 | |
| 4167 | if (shouldSendKeyToInputFilterLocked(args)) { |
| 4168 | mLock.unlock(); |
| 4169 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4170 | policyFlags |= POLICY_FLAG_FILTERED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4171 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 4172 | return; // event was consumed by the filter |
| 4173 | } |
| 4174 | |
| 4175 | mLock.lock(); |
| 4176 | } |
| 4177 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4178 | std::unique_ptr<KeyEntry> newEntry = |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4179 | std::make_unique<KeyEntry>(args.id, args.eventTime, args.deviceId, args.source, |
| 4180 | args.displayId, policyFlags, args.action, flags, keyCode, |
| 4181 | args.scanCode, metaState, repeatCount, args.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4182 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4183 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4184 | mLock.unlock(); |
| 4185 | } // release lock |
| 4186 | |
| 4187 | if (needWake) { |
| 4188 | mLooper->wake(); |
| 4189 | } |
| 4190 | } |
| 4191 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4192 | bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs& args) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4193 | return mInputFilterEnabled; |
| 4194 | } |
| 4195 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4196 | void InputDispatcher::notifyMotion(const NotifyMotionArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4197 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 96282b0 | 2023-02-24 22:36:17 +0000 | [diff] [blame] | 4198 | ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=%s, " |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4199 | "displayId=%" PRId32 ", policyFlags=0x%x, " |
Siarhei Vishniakou | 6ebd069 | 2022-10-20 15:05:45 -0700 | [diff] [blame] | 4200 | "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] | 4201 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, xCursorPosition=%f, " |
| 4202 | "yCursorPosition=%f, downTime=%" PRId64, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4203 | args.id, args.eventTime, args.deviceId, inputEventSourceToString(args.source).c_str(), |
| 4204 | args.displayId, args.policyFlags, MotionEvent::actionToString(args.action).c_str(), |
| 4205 | args.actionButton, args.flags, args.metaState, args.buttonState, args.edgeFlags, |
| 4206 | args.xPrecision, args.yPrecision, args.xCursorPosition, args.yCursorPosition, |
| 4207 | args.downTime); |
| 4208 | for (uint32_t i = 0; i < args.pointerCount; i++) { |
Prabir Pradhan | 96282b0 | 2023-02-24 22:36:17 +0000 | [diff] [blame] | 4209 | ALOGD(" Pointer %d: id=%d, toolType=%s, x=%f, y=%f, pressure=%f, size=%f, " |
| 4210 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, orientation=%f", |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4211 | i, args.pointerProperties[i].id, |
| 4212 | ftl::enum_string(args.pointerProperties[i].toolType).c_str(), |
| 4213 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 4214 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 4215 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 4216 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 4217 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 4218 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 4219 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 4220 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 4221 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4222 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4223 | } |
Siarhei Vishniakou | 4ca9727 | 2023-03-01 11:31:35 -0800 | [diff] [blame] | 4224 | |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4225 | Result<void> motionCheck = validateMotionEvent(args.action, args.actionButton, |
| 4226 | args.pointerCount, args.pointerProperties); |
| 4227 | if (!motionCheck.ok()) { |
| 4228 | LOG(ERROR) << "Invalid event: " << args.dump() << "; reason: " << motionCheck.error(); |
Siarhei Vishniakou | 4ca9727 | 2023-03-01 11:31:35 -0800 | [diff] [blame] | 4229 | return; |
| 4230 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4231 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4232 | uint32_t policyFlags = args.policyFlags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4233 | policyFlags |= POLICY_FLAG_TRUSTED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4234 | |
| 4235 | android::base::Timer t; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4236 | mPolicy->interceptMotionBeforeQueueing(args.displayId, args.eventTime, policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4237 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4238 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4239 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4240 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4241 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4242 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4243 | { // acquire lock |
| 4244 | mLock.lock(); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 4245 | if (!(policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
| 4246 | // Set the flag anyway if we already have an ongoing gesture. That would allow us to |
| 4247 | // complete the processing of the current stroke. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4248 | const auto touchStateIt = mTouchStatesByDisplay.find(args.displayId); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 4249 | if (touchStateIt != mTouchStatesByDisplay.end()) { |
| 4250 | const TouchState& touchState = touchStateIt->second; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4251 | if (touchState.deviceId == args.deviceId && touchState.isDown()) { |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 4252 | policyFlags |= POLICY_FLAG_PASS_TO_USER; |
| 4253 | } |
| 4254 | } |
| 4255 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4256 | |
| 4257 | if (shouldSendMotionToInputFilterLocked(args)) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4258 | ui::Transform displayTransform; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4259 | if (const auto it = mDisplayInfos.find(args.displayId); it != mDisplayInfos.end()) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4260 | displayTransform = it->second.transform; |
| 4261 | } |
| 4262 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4263 | mLock.unlock(); |
| 4264 | |
| 4265 | MotionEvent event; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4266 | event.initialize(args.id, args.deviceId, args.source, args.displayId, INVALID_HMAC, |
| 4267 | args.action, args.actionButton, args.flags, args.edgeFlags, |
| 4268 | args.metaState, args.buttonState, args.classification, |
| 4269 | displayTransform, args.xPrecision, args.yPrecision, |
| 4270 | args.xCursorPosition, args.yCursorPosition, displayTransform, |
| 4271 | args.downTime, args.eventTime, args.pointerCount, |
| 4272 | args.pointerProperties, args.pointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4273 | |
| 4274 | policyFlags |= POLICY_FLAG_FILTERED; |
| 4275 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 4276 | return; // event was consumed by the filter |
| 4277 | } |
| 4278 | |
| 4279 | mLock.lock(); |
| 4280 | } |
| 4281 | |
| 4282 | // Just enqueue a new motion event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4283 | std::unique_ptr<MotionEntry> newEntry = |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4284 | std::make_unique<MotionEntry>(args.id, args.eventTime, args.deviceId, args.source, |
| 4285 | args.displayId, policyFlags, args.action, |
| 4286 | args.actionButton, args.flags, args.metaState, |
| 4287 | args.buttonState, args.classification, args.edgeFlags, |
| 4288 | args.xPrecision, args.yPrecision, |
| 4289 | args.xCursorPosition, args.yCursorPosition, |
| 4290 | args.downTime, args.pointerCount, |
| 4291 | args.pointerProperties, args.pointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4292 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4293 | if (args.id != android::os::IInputConstants::INVALID_INPUT_EVENT_ID && |
| 4294 | IdGenerator::getSource(args.id) == IdGenerator::Source::INPUT_READER && |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 4295 | !mInputFilterEnabled) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4296 | const bool isDown = args.action == AMOTION_EVENT_ACTION_DOWN; |
| 4297 | mLatencyTracker.trackListener(args.id, isDown, args.eventTime, args.readTime); |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 4298 | } |
| 4299 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4300 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4301 | mLock.unlock(); |
| 4302 | } // release lock |
| 4303 | |
| 4304 | if (needWake) { |
| 4305 | mLooper->wake(); |
| 4306 | } |
| 4307 | } |
| 4308 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4309 | void InputDispatcher::notifySensor(const NotifySensorArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4310 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4311 | ALOGD("notifySensor - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 4312 | " sensorType=%s", |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4313 | args.id, args.eventTime, args.deviceId, args.source, |
| 4314 | ftl::enum_string(args.sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4315 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4316 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4317 | bool needWake = false; |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4318 | { // acquire lock |
| 4319 | mLock.lock(); |
| 4320 | |
| 4321 | // Just enqueue a new sensor event. |
| 4322 | std::unique_ptr<SensorEntry> newEntry = |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4323 | std::make_unique<SensorEntry>(args.id, args.eventTime, args.deviceId, args.source, |
| 4324 | /* policyFlags=*/0, args.hwTimestamp, args.sensorType, |
| 4325 | args.accuracy, args.accuracyChanged, args.values); |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4326 | |
| 4327 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
| 4328 | mLock.unlock(); |
| 4329 | } // release lock |
| 4330 | |
| 4331 | if (needWake) { |
| 4332 | mLooper->wake(); |
| 4333 | } |
| 4334 | } |
| 4335 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4336 | void InputDispatcher::notifyVibratorState(const NotifyVibratorStateArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4337 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4338 | ALOGD("notifyVibratorState - eventTime=%" PRId64 ", device=%d, isOn=%d", args.eventTime, |
| 4339 | args.deviceId, args.isOn); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4340 | } |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4341 | mPolicy->notifyVibratorState(args.deviceId, args.isOn); |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 4342 | } |
| 4343 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4344 | bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs& args) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4345 | return mInputFilterEnabled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4346 | } |
| 4347 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4348 | void InputDispatcher::notifySwitch(const NotifySwitchArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4349 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4350 | ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, " |
| 4351 | "switchMask=0x%08x", |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4352 | args.eventTime, args.policyFlags, args.switchValues, args.switchMask); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4353 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4354 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4355 | uint32_t policyFlags = args.policyFlags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4356 | policyFlags |= POLICY_FLAG_TRUSTED; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4357 | mPolicy->notifySwitch(args.eventTime, args.switchValues, args.switchMask, policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4358 | } |
| 4359 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4360 | void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4361 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4362 | ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args.eventTime, |
| 4363 | args.deviceId); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4364 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4365 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4366 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4367 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4368 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4369 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4370 | std::unique_ptr<DeviceResetEntry> newEntry = |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4371 | std::make_unique<DeviceResetEntry>(args.id, args.eventTime, args.deviceId); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4372 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4373 | } // release lock |
| 4374 | |
| 4375 | if (needWake) { |
| 4376 | mLooper->wake(); |
| 4377 | } |
| 4378 | } |
| 4379 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4380 | void InputDispatcher::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4381 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4382 | ALOGD("notifyPointerCaptureChanged - eventTime=%" PRId64 ", enabled=%s", args.eventTime, |
| 4383 | args.request.enable ? "true" : "false"); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4384 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4385 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4386 | bool needWake = false; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4387 | { // acquire lock |
| 4388 | std::scoped_lock _l(mLock); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4389 | auto entry = |
| 4390 | std::make_unique<PointerCaptureChangedEntry>(args.id, args.eventTime, args.request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4391 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 4392 | } // release lock |
| 4393 | |
| 4394 | if (needWake) { |
| 4395 | mLooper->wake(); |
| 4396 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4397 | } |
| 4398 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4399 | InputEventInjectionResult InputDispatcher::injectInputEvent(const InputEvent* event, |
| 4400 | std::optional<int32_t> targetUid, |
| 4401 | InputEventInjectionSync syncMode, |
| 4402 | std::chrono::milliseconds timeout, |
| 4403 | uint32_t policyFlags) { |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4404 | Result<void> eventValidation = validateInputEvent(*event); |
| 4405 | if (!eventValidation.ok()) { |
| 4406 | LOG(INFO) << "Injection failed: invalid event: " << eventValidation.error(); |
| 4407 | return InputEventInjectionResult::FAILED; |
| 4408 | } |
| 4409 | |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4410 | if (debugInboundEventDetails()) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4411 | LOG(DEBUG) << __func__ << ": targetUid=" << toString(targetUid) |
| 4412 | << ", syncMode=" << ftl::enum_string(syncMode) << ", timeout=" << timeout.count() |
| 4413 | << "ms, policyFlags=0x" << std::hex << policyFlags << std::dec |
| 4414 | << ", event=" << *event; |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4415 | } |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 4416 | 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] | 4417 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4418 | policyFlags |= POLICY_FLAG_INJECTED | POLICY_FLAG_TRUSTED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4419 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4420 | // 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] | 4421 | // that have gone through the InputFilter. If the event passed through the InputFilter, assign |
| 4422 | // the provided device id. If the InputFilter is accessibility, and it modifies or synthesizes |
| 4423 | // the injected event, it is responsible for setting POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY. |
| 4424 | // For those events, we will set FLAG_IS_ACCESSIBILITY_EVENT to allow apps to distinguish them |
| 4425 | // from events that originate from actual hardware. |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4426 | int32_t resolvedDeviceId = VIRTUAL_KEYBOARD_ID; |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4427 | if (policyFlags & POLICY_FLAG_FILTERED) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4428 | resolvedDeviceId = event->getDeviceId(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4429 | } |
| 4430 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4431 | std::queue<std::unique_ptr<EventEntry>> injectedEntries; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4432 | switch (event->getType()) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4433 | case InputEventType::KEY: { |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4434 | const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event); |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4435 | const int32_t action = incomingKey.getAction(); |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4436 | int32_t flags = incomingKey.getFlags(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4437 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4438 | flags |= AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4439 | } |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4440 | int32_t keyCode = incomingKey.getKeyCode(); |
| 4441 | int32_t metaState = incomingKey.getMetaState(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4442 | accelerateMetaShortcuts(resolvedDeviceId, action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4443 | /*byref*/ keyCode, /*byref*/ metaState); |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4444 | KeyEvent keyEvent; |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4445 | keyEvent.initialize(incomingKey.getId(), resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4446 | incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode, |
| 4447 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), |
| 4448 | incomingKey.getDownTime(), incomingKey.getEventTime()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4449 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4450 | if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) { |
| 4451 | policyFlags |= POLICY_FLAG_VIRTUAL; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4452 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4453 | |
| 4454 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 4455 | android::base::Timer t; |
| 4456 | mPolicy->interceptKeyBeforeQueueing(&keyEvent, /*byref*/ policyFlags); |
| 4457 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4458 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
| 4459 | std::to_string(t.duration().count()).c_str()); |
| 4460 | } |
| 4461 | } |
| 4462 | |
| 4463 | mLock.lock(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4464 | std::unique_ptr<KeyEntry> injectedEntry = |
| 4465 | std::make_unique<KeyEntry>(incomingKey.getId(), incomingKey.getEventTime(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4466 | resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4467 | incomingKey.getDisplayId(), policyFlags, action, |
| 4468 | flags, keyCode, incomingKey.getScanCode(), metaState, |
| 4469 | incomingKey.getRepeatCount(), |
| 4470 | incomingKey.getDownTime()); |
| 4471 | injectedEntries.push(std::move(injectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4472 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4473 | } |
| 4474 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4475 | case InputEventType::MOTION: { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4476 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4477 | const bool isPointerEvent = |
| 4478 | isFromSource(event->getSource(), AINPUT_SOURCE_CLASS_POINTER); |
| 4479 | // If a pointer event has no displayId specified, inject it to the default display. |
| 4480 | const uint32_t displayId = isPointerEvent && (event->getDisplayId() == ADISPLAY_ID_NONE) |
| 4481 | ? ADISPLAY_ID_DEFAULT |
| 4482 | : event->getDisplayId(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4483 | int32_t flags = motionEvent.getFlags(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4484 | |
| 4485 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4486 | nsecs_t eventTime = motionEvent.getEventTime(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4487 | android::base::Timer t; |
| 4488 | mPolicy->interceptMotionBeforeQueueing(displayId, eventTime, /*byref*/ policyFlags); |
| 4489 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4490 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
| 4491 | std::to_string(t.duration().count()).c_str()); |
| 4492 | } |
| 4493 | } |
| 4494 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4495 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4496 | flags |= AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4497 | } |
| 4498 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4499 | mLock.lock(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4500 | const nsecs_t* sampleEventTimes = motionEvent.getSampleEventTimes(); |
| 4501 | const PointerCoords* samplePointerCoords = motionEvent.getSamplePointerCoords(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4502 | std::unique_ptr<MotionEntry> injectedEntry = |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4503 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, |
| 4504 | resolvedDeviceId, motionEvent.getSource(), |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4505 | displayId, policyFlags, motionEvent.getAction(), |
| 4506 | motionEvent.getActionButton(), flags, |
| 4507 | motionEvent.getMetaState(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4508 | motionEvent.getButtonState(), |
| 4509 | motionEvent.getClassification(), |
| 4510 | motionEvent.getEdgeFlags(), |
| 4511 | motionEvent.getXPrecision(), |
| 4512 | motionEvent.getYPrecision(), |
| 4513 | motionEvent.getRawXCursorPosition(), |
| 4514 | motionEvent.getRawYCursorPosition(), |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4515 | motionEvent.getDownTime(), |
| 4516 | motionEvent.getPointerCount(), |
| 4517 | motionEvent.getPointerProperties(), |
| 4518 | samplePointerCoords); |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4519 | transformMotionEntryForInjectionLocked(*injectedEntry, motionEvent.getTransform()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4520 | injectedEntries.push(std::move(injectedEntry)); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4521 | for (size_t i = motionEvent.getHistorySize(); i > 0; i--) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4522 | sampleEventTimes += 1; |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4523 | samplePointerCoords += motionEvent.getPointerCount(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4524 | std::unique_ptr<MotionEntry> nextInjectedEntry = |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4525 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, |
| 4526 | resolvedDeviceId, motionEvent.getSource(), |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4527 | displayId, policyFlags, |
| 4528 | motionEvent.getAction(), |
| 4529 | motionEvent.getActionButton(), flags, |
| 4530 | motionEvent.getMetaState(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4531 | motionEvent.getButtonState(), |
| 4532 | motionEvent.getClassification(), |
| 4533 | motionEvent.getEdgeFlags(), |
| 4534 | motionEvent.getXPrecision(), |
| 4535 | motionEvent.getYPrecision(), |
| 4536 | motionEvent.getRawXCursorPosition(), |
| 4537 | motionEvent.getRawYCursorPosition(), |
| 4538 | motionEvent.getDownTime(), |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4539 | motionEvent.getPointerCount(), |
| 4540 | motionEvent.getPointerProperties(), |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4541 | samplePointerCoords); |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4542 | transformMotionEntryForInjectionLocked(*nextInjectedEntry, |
| 4543 | motionEvent.getTransform()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4544 | injectedEntries.push(std::move(nextInjectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4545 | } |
| 4546 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4547 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4548 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4549 | default: |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4550 | LOG(WARNING) << "Cannot inject " << ftl::enum_string(event->getType()) << " events"; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4551 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4552 | } |
| 4553 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4554 | InjectionState* injectionState = new InjectionState(targetUid); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4555 | if (syncMode == InputEventInjectionSync::NONE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4556 | injectionState->injectionIsAsync = true; |
| 4557 | } |
| 4558 | |
| 4559 | injectionState->refCount += 1; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4560 | injectedEntries.back()->injectionState = injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4561 | |
| 4562 | bool needWake = false; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4563 | while (!injectedEntries.empty()) { |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 4564 | if (DEBUG_INJECTION) { |
| 4565 | LOG(DEBUG) << "Injecting " << injectedEntries.front()->getDescription(); |
| 4566 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4567 | needWake |= enqueueInboundEventLocked(std::move(injectedEntries.front())); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4568 | injectedEntries.pop(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4569 | } |
| 4570 | |
| 4571 | mLock.unlock(); |
| 4572 | |
| 4573 | if (needWake) { |
| 4574 | mLooper->wake(); |
| 4575 | } |
| 4576 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4577 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4578 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4579 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4580 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4581 | if (syncMode == InputEventInjectionSync::NONE) { |
| 4582 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4583 | } else { |
| 4584 | for (;;) { |
| 4585 | injectionResult = injectionState->injectionResult; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4586 | if (injectionResult != InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4587 | break; |
| 4588 | } |
| 4589 | |
| 4590 | nsecs_t remainingTimeout = endTime - now(); |
| 4591 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4592 | if (DEBUG_INJECTION) { |
| 4593 | ALOGD("injectInputEvent - Timed out waiting for injection result " |
| 4594 | "to become available."); |
| 4595 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4596 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4597 | break; |
| 4598 | } |
| 4599 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4600 | mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4601 | } |
| 4602 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4603 | if (injectionResult == InputEventInjectionResult::SUCCEEDED && |
| 4604 | syncMode == InputEventInjectionSync::WAIT_FOR_FINISHED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4605 | while (injectionState->pendingForegroundDispatches != 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4606 | if (DEBUG_INJECTION) { |
| 4607 | ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", |
| 4608 | injectionState->pendingForegroundDispatches); |
| 4609 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4610 | nsecs_t remainingTimeout = endTime - now(); |
| 4611 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4612 | if (DEBUG_INJECTION) { |
| 4613 | ALOGD("injectInputEvent - Timed out waiting for pending foreground " |
| 4614 | "dispatches to finish."); |
| 4615 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4616 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4617 | break; |
| 4618 | } |
| 4619 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4620 | mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4621 | } |
| 4622 | } |
| 4623 | } |
| 4624 | |
| 4625 | injectionState->release(); |
| 4626 | } // release lock |
| 4627 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4628 | if (DEBUG_INJECTION) { |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 4629 | LOG(DEBUG) << "injectInputEvent - Finished with result " |
| 4630 | << ftl::enum_string(injectionResult); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4631 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4632 | |
| 4633 | return injectionResult; |
| 4634 | } |
| 4635 | |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4636 | std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4637 | std::array<uint8_t, 32> calculatedHmac; |
| 4638 | std::unique_ptr<VerifiedInputEvent> result; |
| 4639 | switch (event.getType()) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4640 | case InputEventType::KEY: { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4641 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event); |
| 4642 | VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent); |
| 4643 | result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4644 | calculatedHmac = sign(verifiedKeyEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4645 | break; |
| 4646 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4647 | case InputEventType::MOTION: { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4648 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event); |
| 4649 | VerifiedMotionEvent verifiedMotionEvent = |
| 4650 | verifiedMotionEventFromMotionEvent(motionEvent); |
| 4651 | result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4652 | calculatedHmac = sign(verifiedMotionEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4653 | break; |
| 4654 | } |
| 4655 | default: { |
| 4656 | ALOGE("Cannot verify events of type %" PRId32, event.getType()); |
| 4657 | return nullptr; |
| 4658 | } |
| 4659 | } |
| 4660 | if (calculatedHmac == INVALID_HMAC) { |
| 4661 | return nullptr; |
| 4662 | } |
tyiu | 1573a67 | 2023-02-21 22:38:32 +0000 | [diff] [blame] | 4663 | if (0 != CRYPTO_memcmp(calculatedHmac.data(), event.getHmac().data(), calculatedHmac.size())) { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4664 | return nullptr; |
| 4665 | } |
| 4666 | return result; |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4667 | } |
| 4668 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4669 | void InputDispatcher::setInjectionResult(EventEntry& entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4670 | InputEventInjectionResult injectionResult) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4671 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4672 | if (injectionState) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4673 | if (DEBUG_INJECTION) { |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 4674 | LOG(DEBUG) << "Setting input event injection result to " |
| 4675 | << ftl::enum_string(injectionResult); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4676 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4677 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4678 | if (injectionState->injectionIsAsync && !(entry.policyFlags & POLICY_FLAG_FILTERED)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4679 | // Log the outcome since the injector did not wait for the injection result. |
| 4680 | switch (injectionResult) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4681 | case InputEventInjectionResult::SUCCEEDED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4682 | ALOGV("Asynchronous input event injection succeeded."); |
| 4683 | break; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4684 | case InputEventInjectionResult::TARGET_MISMATCH: |
| 4685 | ALOGV("Asynchronous input event injection target mismatch."); |
| 4686 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4687 | case InputEventInjectionResult::FAILED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4688 | ALOGW("Asynchronous input event injection failed."); |
| 4689 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4690 | case InputEventInjectionResult::TIMED_OUT: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4691 | ALOGW("Asynchronous input event injection timed out."); |
| 4692 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4693 | case InputEventInjectionResult::PENDING: |
| 4694 | ALOGE("Setting result to 'PENDING' for asynchronous injection"); |
| 4695 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4696 | } |
| 4697 | } |
| 4698 | |
| 4699 | injectionState->injectionResult = injectionResult; |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4700 | mInjectionResultAvailable.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4701 | } |
| 4702 | } |
| 4703 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4704 | void InputDispatcher::transformMotionEntryForInjectionLocked( |
| 4705 | MotionEntry& entry, const ui::Transform& injectedTransform) const { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4706 | // Input injection works in the logical display coordinate space, but the input pipeline works |
| 4707 | // display space, so we need to transform the injected events accordingly. |
| 4708 | const auto it = mDisplayInfos.find(entry.displayId); |
| 4709 | if (it == mDisplayInfos.end()) return; |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4710 | const auto& transformToDisplay = it->second.transform.inverse() * injectedTransform; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4711 | |
Prabir Pradhan | d9a2ebe | 2022-07-20 19:25:13 +0000 | [diff] [blame] | 4712 | if (entry.xCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION && |
| 4713 | entry.yCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) { |
| 4714 | const vec2 cursor = |
| 4715 | MotionEvent::calculateTransformedXY(entry.source, transformToDisplay, |
| 4716 | {entry.xCursorPosition, entry.yCursorPosition}); |
| 4717 | entry.xCursorPosition = cursor.x; |
| 4718 | entry.yCursorPosition = cursor.y; |
| 4719 | } |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4720 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
Prabir Pradhan | 8e6ce22 | 2022-02-24 09:08:54 -0800 | [diff] [blame] | 4721 | entry.pointerCoords[i] = |
| 4722 | MotionEvent::calculateTransformedCoords(entry.source, transformToDisplay, |
| 4723 | entry.pointerCoords[i]); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4724 | } |
| 4725 | } |
| 4726 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4727 | void InputDispatcher::incrementPendingForegroundDispatches(EventEntry& entry) { |
| 4728 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4729 | if (injectionState) { |
| 4730 | injectionState->pendingForegroundDispatches += 1; |
| 4731 | } |
| 4732 | } |
| 4733 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4734 | void InputDispatcher::decrementPendingForegroundDispatches(EventEntry& entry) { |
| 4735 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4736 | if (injectionState) { |
| 4737 | injectionState->pendingForegroundDispatches -= 1; |
| 4738 | |
| 4739 | if (injectionState->pendingForegroundDispatches == 0) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4740 | mInjectionSyncFinished.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4741 | } |
| 4742 | } |
| 4743 | } |
| 4744 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4745 | const std::vector<sp<WindowInfoHandle>>& InputDispatcher::getWindowHandlesLocked( |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4746 | int32_t displayId) const { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4747 | static const std::vector<sp<WindowInfoHandle>> EMPTY_WINDOW_HANDLES; |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4748 | auto it = mWindowHandlesByDisplay.find(displayId); |
| 4749 | return it != mWindowHandlesByDisplay.end() ? it->second : EMPTY_WINDOW_HANDLES; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4750 | } |
| 4751 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4752 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4753 | const sp<IBinder>& windowHandleToken) const { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4754 | if (windowHandleToken == nullptr) { |
| 4755 | return nullptr; |
| 4756 | } |
| 4757 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4758 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4759 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 4760 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4761 | if (windowHandle->getToken() == windowHandleToken) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4762 | return windowHandle; |
| 4763 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4764 | } |
| 4765 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4766 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4767 | } |
| 4768 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4769 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked(const sp<IBinder>& windowHandleToken, |
| 4770 | int displayId) const { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4771 | if (windowHandleToken == nullptr) { |
| 4772 | return nullptr; |
| 4773 | } |
| 4774 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4775 | for (const sp<WindowInfoHandle>& windowHandle : getWindowHandlesLocked(displayId)) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4776 | if (windowHandle->getToken() == windowHandleToken) { |
| 4777 | return windowHandle; |
| 4778 | } |
| 4779 | } |
| 4780 | return nullptr; |
| 4781 | } |
| 4782 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4783 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
| 4784 | const sp<WindowInfoHandle>& windowHandle) const { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4785 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4786 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 4787 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4788 | if (handle->getId() == windowHandle->getId() && |
| 4789 | handle->getToken() == windowHandle->getToken()) { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4790 | if (windowHandle->getInfo()->displayId != it.first) { |
| 4791 | ALOGE("Found window %s in display %" PRId32 |
| 4792 | ", but it should belong to display %" PRId32, |
| 4793 | windowHandle->getName().c_str(), it.first, |
| 4794 | windowHandle->getInfo()->displayId); |
| 4795 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4796 | return handle; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4797 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4798 | } |
| 4799 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4800 | return nullptr; |
| 4801 | } |
| 4802 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4803 | sp<WindowInfoHandle> InputDispatcher::getFocusedWindowHandleLocked(int displayId) const { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4804 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(displayId); |
| 4805 | return getWindowHandleLocked(focusedToken, displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4806 | } |
| 4807 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4808 | ui::Transform InputDispatcher::getTransformLocked(int32_t displayId) const { |
| 4809 | auto displayInfoIt = mDisplayInfos.find(displayId); |
| 4810 | return displayInfoIt != mDisplayInfos.end() ? displayInfoIt->second.transform |
| 4811 | : kIdentityTransform; |
| 4812 | } |
| 4813 | |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 4814 | bool InputDispatcher::canWindowReceiveMotionLocked(const sp<WindowInfoHandle>& window, |
| 4815 | const MotionEntry& motionEntry) const { |
| 4816 | const WindowInfo& info = *window->getInfo(); |
| 4817 | |
| 4818 | // Skip spy window targets that are not valid for targeted injection. |
| 4819 | if (const auto err = verifyTargetedInjection(window, motionEntry); err) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4820 | return false; |
| 4821 | } |
| 4822 | |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 4823 | if (info.inputConfig.test(WindowInfo::InputConfig::PAUSE_DISPATCHING)) { |
| 4824 | ALOGI("Not sending touch event to %s because it is paused", window->getName().c_str()); |
| 4825 | return false; |
| 4826 | } |
| 4827 | |
| 4828 | if (info.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) { |
| 4829 | ALOGW("Not sending touch gesture to %s because it has config NO_INPUT_CHANNEL", |
| 4830 | window->getName().c_str()); |
| 4831 | return false; |
| 4832 | } |
| 4833 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 4834 | std::shared_ptr<Connection> connection = getConnectionLocked(window->getToken()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4835 | if (connection == nullptr) { |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 4836 | ALOGW("Not sending touch to %s because there's no corresponding connection", |
| 4837 | window->getName().c_str()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4838 | return false; |
| 4839 | } |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 4840 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4841 | if (!connection->responsive) { |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 4842 | 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] | 4843 | return false; |
| 4844 | } |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 4845 | |
| 4846 | // Drop events that can't be trusted due to occlusion |
| 4847 | const auto [x, y] = resolveTouchedPosition(motionEntry); |
| 4848 | TouchOcclusionInfo occlusionInfo = computeTouchOcclusionInfoLocked(window, x, y); |
| 4849 | if (!isTouchTrustedLocked(occlusionInfo)) { |
| 4850 | if (DEBUG_TOUCH_OCCLUSION) { |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 4851 | ALOGD("Stack of obscuring windows during untrusted touch (%.1f, %.1f):", x, y); |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 4852 | for (const auto& log : occlusionInfo.debugInfo) { |
| 4853 | ALOGD("%s", log.c_str()); |
| 4854 | } |
| 4855 | } |
| 4856 | ALOGW("Dropping untrusted touch event due to %s/%d", occlusionInfo.obscuringPackage.c_str(), |
| 4857 | occlusionInfo.obscuringUid); |
| 4858 | return false; |
| 4859 | } |
| 4860 | |
| 4861 | // Drop touch events if requested by input feature |
| 4862 | if (shouldDropInput(motionEntry, window)) { |
| 4863 | return false; |
| 4864 | } |
| 4865 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4866 | return true; |
| 4867 | } |
| 4868 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4869 | std::shared_ptr<InputChannel> InputDispatcher::getInputChannelLocked( |
| 4870 | const sp<IBinder>& token) const { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4871 | auto connectionIt = mConnectionsByToken.find(token); |
| 4872 | if (connectionIt == mConnectionsByToken.end()) { |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4873 | return nullptr; |
| 4874 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4875 | return connectionIt->second->inputChannel; |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4876 | } |
| 4877 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4878 | void InputDispatcher::updateWindowHandlesForDisplayLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4879 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
| 4880 | if (windowInfoHandles.empty()) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4881 | // Remove all handles on a display if there are no windows left. |
| 4882 | mWindowHandlesByDisplay.erase(displayId); |
| 4883 | return; |
| 4884 | } |
| 4885 | |
| 4886 | // Since we compare the pointer of input window handles across window updates, we need |
| 4887 | // 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] | 4888 | const std::vector<sp<WindowInfoHandle>>& oldHandles = getWindowHandlesLocked(displayId); |
| 4889 | std::unordered_map<int32_t /*id*/, sp<WindowInfoHandle>> oldHandlesById; |
| 4890 | for (const sp<WindowInfoHandle>& handle : oldHandles) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4891 | oldHandlesById[handle->getId()] = handle; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4892 | } |
| 4893 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4894 | std::vector<sp<WindowInfoHandle>> newHandles; |
| 4895 | for (const sp<WindowInfoHandle>& handle : windowInfoHandles) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4896 | const WindowInfo* info = handle->getInfo(); |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 4897 | if (getInputChannelLocked(handle->getToken()) == nullptr) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4898 | const bool noInputChannel = |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 4899 | info->inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4900 | const bool canReceiveInput = |
| 4901 | !info->inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE) || |
| 4902 | !info->inputConfig.test(WindowInfo::InputConfig::NOT_FOCUSABLE); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4903 | if (canReceiveInput && !noInputChannel) { |
John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 4904 | ALOGV("Window handle %s has no registered input channel", |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4905 | handle->getName().c_str()); |
Robert Carr | 2984b7a | 2020-04-13 17:06:45 -0700 | [diff] [blame] | 4906 | continue; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4907 | } |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4908 | } |
| 4909 | |
| 4910 | if (info->displayId != displayId) { |
| 4911 | ALOGE("Window %s updated by wrong display %d, should belong to display %d", |
| 4912 | handle->getName().c_str(), displayId, info->displayId); |
| 4913 | continue; |
| 4914 | } |
| 4915 | |
Robert Carr | edd1360 | 2020-04-13 17:24:34 -0700 | [diff] [blame] | 4916 | if ((oldHandlesById.find(handle->getId()) != oldHandlesById.end()) && |
| 4917 | (oldHandlesById.at(handle->getId())->getToken() == handle->getToken())) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4918 | const sp<WindowInfoHandle>& oldHandle = oldHandlesById.at(handle->getId()); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4919 | oldHandle->updateFrom(handle); |
| 4920 | newHandles.push_back(oldHandle); |
| 4921 | } else { |
| 4922 | newHandles.push_back(handle); |
| 4923 | } |
| 4924 | } |
| 4925 | |
| 4926 | // Insert or replace |
| 4927 | mWindowHandlesByDisplay[displayId] = newHandles; |
| 4928 | } |
| 4929 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4930 | void InputDispatcher::setInputWindows( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4931 | const std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>>& handlesPerDisplay) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 4932 | // TODO(b/198444055): Remove setInputWindows from InputDispatcher. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4933 | { // acquire lock |
| 4934 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 4935 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 4936 | setInputWindowsLocked(handles, displayId); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4937 | } |
| 4938 | } |
| 4939 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4940 | mLooper->wake(); |
| 4941 | } |
| 4942 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4943 | /** |
| 4944 | * Called from InputManagerService, update window handle list by displayId that can receive input. |
| 4945 | * A window handle contains information about InputChannel, Touch Region, Types, Focused,... |
| 4946 | * If set an empty list, remove all handles from the specific display. |
| 4947 | * For focused handle, check if need to change and send a cancel event to previous one. |
| 4948 | * For removed handle, check if need to send a cancel event if already in touch. |
| 4949 | */ |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4950 | void InputDispatcher::setInputWindowsLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4951 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4952 | if (DEBUG_FOCUS) { |
| 4953 | std::string windowList; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4954 | for (const sp<WindowInfoHandle>& iwh : windowInfoHandles) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4955 | windowList += iwh->getName() + " "; |
| 4956 | } |
| 4957 | ALOGD("setInputWindows displayId=%" PRId32 " %s", displayId, windowList.c_str()); |
| 4958 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4959 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4960 | // Check preconditions for new input windows |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4961 | for (const sp<WindowInfoHandle>& window : windowInfoHandles) { |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4962 | const WindowInfo& info = *window->getInfo(); |
| 4963 | |
| 4964 | // 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] | 4965 | const bool noInputWindow = info.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4966 | if (noInputWindow && window->getToken() != nullptr) { |
| 4967 | ALOGE("%s has feature NO_INPUT_WINDOW, but a non-null token. Clearing", |
| 4968 | window->getName().c_str()); |
| 4969 | window->releaseChannel(); |
| 4970 | } |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4971 | |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 4972 | // Ensure all spy windows are trusted overlays |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4973 | LOG_ALWAYS_FATAL_IF(info.isSpy() && |
| 4974 | !info.inputConfig.test( |
| 4975 | WindowInfo::InputConfig::TRUSTED_OVERLAY), |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 4976 | "%s has feature SPY, but is not a trusted overlay.", |
| 4977 | window->getName().c_str()); |
| 4978 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4979 | // Ensure all stylus interceptors are trusted overlays |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4980 | LOG_ALWAYS_FATAL_IF(info.interceptsStylus() && |
| 4981 | !info.inputConfig.test( |
| 4982 | WindowInfo::InputConfig::TRUSTED_OVERLAY), |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4983 | "%s has feature INTERCEPTS_STYLUS, but is not a trusted overlay.", |
| 4984 | window->getName().c_str()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4985 | } |
| 4986 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4987 | // Copy old handles for release if they are no longer present. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4988 | const std::vector<sp<WindowInfoHandle>> oldWindowHandles = getWindowHandlesLocked(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4989 | |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4990 | // Save the old windows' orientation by ID before it gets updated. |
| 4991 | std::unordered_map<int32_t, uint32_t> oldWindowOrientations; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4992 | for (const sp<WindowInfoHandle>& handle : oldWindowHandles) { |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4993 | oldWindowOrientations.emplace(handle->getId(), |
| 4994 | handle->getInfo()->transform.getOrientation()); |
| 4995 | } |
| 4996 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4997 | updateWindowHandlesForDisplayLocked(windowInfoHandles, displayId); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4998 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4999 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5000 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5001 | std::optional<FocusResolver::FocusChanges> changes = |
| 5002 | mFocusResolver.setInputWindows(displayId, windowHandles); |
| 5003 | if (changes) { |
| 5004 | onFocusChangedLocked(*changes); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5005 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5006 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5007 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 5008 | mTouchStatesByDisplay.find(displayId); |
| 5009 | if (stateIt != mTouchStatesByDisplay.end()) { |
| 5010 | TouchState& state = stateIt->second; |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5011 | for (size_t i = 0; i < state.windows.size();) { |
| 5012 | TouchedWindow& touchedWindow = state.windows[i]; |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 5013 | if (getWindowHandleLocked(touchedWindow.windowHandle) == nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5014 | if (DEBUG_FOCUS) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5015 | ALOGD("Touched window was removed: %s in display %" PRId32, |
| 5016 | touchedWindow.windowHandle->getName().c_str(), displayId); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5017 | } |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5018 | std::shared_ptr<InputChannel> touchedInputChannel = |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5019 | getInputChannelLocked(touchedWindow.windowHandle->getToken()); |
| 5020 | if (touchedInputChannel != nullptr) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 5021 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5022 | "touched window was removed"); |
| 5023 | synthesizeCancelationEventsForInputChannelLocked(touchedInputChannel, options); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5024 | // Since we are about to drop the touch, cancel the events for the wallpaper as |
| 5025 | // well. |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5026 | if (touchedWindow.targetFlags.test(InputTarget::Flags::FOREGROUND) && |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5027 | touchedWindow.windowHandle->getInfo()->inputConfig.test( |
| 5028 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER)) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5029 | sp<WindowInfoHandle> wallpaper = state.getWallpaperWindow(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5030 | synthesizeCancelationEventsForWindowLocked(wallpaper, options); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5031 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5032 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5033 | state.windows.erase(state.windows.begin() + i); |
| 5034 | } else { |
| 5035 | ++i; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5036 | } |
| 5037 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5038 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5039 | // 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] | 5040 | // could just clear the state here. |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 5041 | if (mDragState && mDragState->dragWindow->getInfo()->displayId == displayId && |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5042 | std::find(windowHandles.begin(), windowHandles.end(), mDragState->dragWindow) == |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5043 | windowHandles.end()) { |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 5044 | ALOGI("Drag window went away: %s", mDragState->dragWindow->getName().c_str()); |
| 5045 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5046 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5047 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5048 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 5049 | |
Prabir Pradhan | 8b89c2f | 2021-07-29 16:30:14 +0000 | [diff] [blame] | 5050 | // Determine if the orientation of any of the input windows have changed, and cancel all |
| 5051 | // pointer events if necessary. |
| 5052 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
| 5053 | const sp<WindowInfoHandle> newWindowHandle = getWindowHandleLocked(oldWindowHandle); |
| 5054 | if (newWindowHandle != nullptr && |
| 5055 | newWindowHandle->getInfo()->transform.getOrientation() != |
| 5056 | oldWindowOrientations[oldWindowHandle->getId()]) { |
| 5057 | std::shared_ptr<InputChannel> inputChannel = |
| 5058 | getInputChannelLocked(newWindowHandle->getToken()); |
| 5059 | if (inputChannel != nullptr) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 5060 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
Prabir Pradhan | 8b89c2f | 2021-07-29 16:30:14 +0000 | [diff] [blame] | 5061 | "touched window's orientation changed"); |
| 5062 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 5063 | } |
| 5064 | } |
| 5065 | } |
| 5066 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5067 | // Release information for windows that are no longer present. |
| 5068 | // This ensures that unused input channels are released promptly. |
| 5069 | // Otherwise, they might stick around until the window handle is destroyed |
| 5070 | // which might not happen until the next GC. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5071 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 5072 | if (getWindowHandleLocked(oldWindowHandle) == nullptr) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5073 | if (DEBUG_FOCUS) { |
| 5074 | ALOGD("Window went away: %s", oldWindowHandle->getName().c_str()); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 5075 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5076 | oldWindowHandle->releaseChannel(); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 5077 | } |
chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 5078 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5079 | } |
| 5080 | |
| 5081 | void InputDispatcher::setFocusedApplication( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5082 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5083 | if (DEBUG_FOCUS) { |
| 5084 | ALOGD("setFocusedApplication displayId=%" PRId32 " %s", displayId, |
| 5085 | inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>"); |
| 5086 | } |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 5087 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5088 | std::scoped_lock _l(mLock); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 5089 | setFocusedApplicationLocked(displayId, inputApplicationHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5090 | } // release lock |
| 5091 | |
| 5092 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5093 | mLooper->wake(); |
| 5094 | } |
| 5095 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 5096 | void InputDispatcher::setFocusedApplicationLocked( |
| 5097 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
| 5098 | std::shared_ptr<InputApplicationHandle> oldFocusedApplicationHandle = |
| 5099 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 5100 | |
| 5101 | if (sharedPointersEqual(oldFocusedApplicationHandle, inputApplicationHandle)) { |
| 5102 | return; // This application is already focused. No need to wake up or change anything. |
| 5103 | } |
| 5104 | |
| 5105 | // Set the new application handle. |
| 5106 | if (inputApplicationHandle != nullptr) { |
| 5107 | mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle; |
| 5108 | } else { |
| 5109 | mFocusedApplicationHandlesByDisplay.erase(displayId); |
| 5110 | } |
| 5111 | |
| 5112 | // No matter what the old focused application was, stop waiting on it because it is |
| 5113 | // no longer focused. |
| 5114 | resetNoFocusedWindowTimeoutLocked(); |
| 5115 | } |
| 5116 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5117 | /** |
| 5118 | * Sets the focused display, which is responsible for receiving focus-dispatched input events where |
| 5119 | * the display not specified. |
| 5120 | * |
| 5121 | * We track any unreleased events for each window. If a window loses the ability to receive the |
| 5122 | * released event, we will send a cancel event to it. So when the focused display is changed, we |
| 5123 | * cancel all the unreleased display-unspecified events for the focused window on the old focused |
| 5124 | * display. The display-specified events won't be affected. |
| 5125 | */ |
| 5126 | void InputDispatcher::setFocusedDisplay(int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5127 | if (DEBUG_FOCUS) { |
| 5128 | ALOGD("setFocusedDisplay displayId=%" PRId32, displayId); |
| 5129 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5130 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5131 | std::scoped_lock _l(mLock); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5132 | |
| 5133 | if (mFocusedDisplayId != displayId) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5134 | sp<IBinder> oldFocusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5135 | mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5136 | if (oldFocusedWindowToken != nullptr) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5137 | std::shared_ptr<InputChannel> inputChannel = |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5138 | getInputChannelLocked(oldFocusedWindowToken); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5139 | if (inputChannel != nullptr) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5140 | CancelationOptions |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 5141 | options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5142 | "The display which contains this window no longer has focus."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5143 | options.displayId = ADISPLAY_ID_NONE; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5144 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
| 5145 | } |
| 5146 | } |
| 5147 | mFocusedDisplayId = displayId; |
| 5148 | |
Chris Ye | 3c2d6f5 | 2020-08-09 10:39:48 -0700 | [diff] [blame] | 5149 | // Find new focused window and validate |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5150 | sp<IBinder> newFocusedWindowToken = mFocusResolver.getFocusedWindowToken(displayId); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5151 | sendFocusChangedCommandLocked(oldFocusedWindowToken, newFocusedWindowToken); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5152 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5153 | if (newFocusedWindowToken == nullptr) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5154 | ALOGW("Focused display #%" PRId32 " does not have a focused window.", displayId); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5155 | if (mFocusResolver.hasFocusedWindowTokens()) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5156 | ALOGE("But another display has a focused window\n%s", |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5157 | mFocusResolver.dumpFocusedWindows().c_str()); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5158 | } |
| 5159 | } |
| 5160 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5161 | } // release lock |
| 5162 | |
| 5163 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5164 | mLooper->wake(); |
| 5165 | } |
| 5166 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5167 | void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5168 | if (DEBUG_FOCUS) { |
| 5169 | ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); |
| 5170 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5171 | |
| 5172 | bool changed; |
| 5173 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5174 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5175 | |
| 5176 | if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { |
| 5177 | if (mDispatchFrozen && !frozen) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5178 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5179 | } |
| 5180 | |
| 5181 | if (mDispatchEnabled && !enabled) { |
| 5182 | resetAndDropEverythingLocked("dispatcher is being disabled"); |
| 5183 | } |
| 5184 | |
| 5185 | mDispatchEnabled = enabled; |
| 5186 | mDispatchFrozen = frozen; |
| 5187 | changed = true; |
| 5188 | } else { |
| 5189 | changed = false; |
| 5190 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5191 | } // release lock |
| 5192 | |
| 5193 | if (changed) { |
| 5194 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5195 | mLooper->wake(); |
| 5196 | } |
| 5197 | } |
| 5198 | |
| 5199 | void InputDispatcher::setInputFilterEnabled(bool enabled) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5200 | if (DEBUG_FOCUS) { |
| 5201 | ALOGD("setInputFilterEnabled: enabled=%d", enabled); |
| 5202 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5203 | |
| 5204 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5205 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5206 | |
| 5207 | if (mInputFilterEnabled == enabled) { |
| 5208 | return; |
| 5209 | } |
| 5210 | |
| 5211 | mInputFilterEnabled = enabled; |
| 5212 | resetAndDropEverythingLocked("input filter is being enabled or disabled"); |
| 5213 | } // release lock |
| 5214 | |
| 5215 | // Wake up poll loop since there might be work to do to drop everything. |
| 5216 | mLooper->wake(); |
| 5217 | } |
| 5218 | |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 5219 | bool InputDispatcher::setInTouchMode(bool inTouchMode, int32_t pid, int32_t uid, bool hasPermission, |
| 5220 | int32_t displayId) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5221 | bool needWake = false; |
| 5222 | { |
| 5223 | std::scoped_lock lock(mLock); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5224 | ALOGD_IF(DEBUG_TOUCH_MODE, |
| 5225 | "Request to change touch mode to %s (calling pid=%d, uid=%d, " |
| 5226 | "hasPermission=%s, target displayId=%d, mTouchModePerDisplay[displayId]=%s)", |
| 5227 | toString(inTouchMode), pid, uid, toString(hasPermission), displayId, |
| 5228 | mTouchModePerDisplay.count(displayId) == 0 |
| 5229 | ? "not set" |
| 5230 | : std::to_string(mTouchModePerDisplay[displayId]).c_str()); |
| 5231 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5232 | auto touchModeIt = mTouchModePerDisplay.find(displayId); |
| 5233 | if (touchModeIt != mTouchModePerDisplay.end() && touchModeIt->second == inTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5234 | return false; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5235 | } |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5236 | if (!hasPermission) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 5237 | if (!focusedWindowIsOwnedByLocked(pid, uid) && |
| 5238 | !recentWindowsAreOwnedByLocked(pid, uid)) { |
| 5239 | ALOGD("Touch mode switch rejected, caller (pid=%d, uid=%d) doesn't own the focused " |
| 5240 | "window nor none of the previously interacted window", |
| 5241 | pid, uid); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5242 | return false; |
| 5243 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5244 | } |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5245 | mTouchModePerDisplay[displayId] = inTouchMode; |
| 5246 | auto entry = std::make_unique<TouchModeEntry>(mIdGenerator.nextId(), now(), inTouchMode, |
| 5247 | displayId); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5248 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 5249 | } // release lock |
| 5250 | |
| 5251 | if (needWake) { |
| 5252 | mLooper->wake(); |
| 5253 | } |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5254 | return true; |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 5255 | } |
| 5256 | |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 5257 | bool InputDispatcher::focusedWindowIsOwnedByLocked(int32_t pid, int32_t uid) { |
| 5258 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
| 5259 | if (focusedToken == nullptr) { |
| 5260 | return false; |
| 5261 | } |
| 5262 | sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(focusedToken); |
| 5263 | return isWindowOwnedBy(windowHandle, pid, uid); |
| 5264 | } |
| 5265 | |
| 5266 | bool InputDispatcher::recentWindowsAreOwnedByLocked(int32_t pid, int32_t uid) { |
| 5267 | return std::find_if(mInteractionConnectionTokens.begin(), mInteractionConnectionTokens.end(), |
| 5268 | [&](const sp<IBinder>& connectionToken) REQUIRES(mLock) { |
| 5269 | const sp<WindowInfoHandle> windowHandle = |
| 5270 | getWindowHandleLocked(connectionToken); |
| 5271 | return isWindowOwnedBy(windowHandle, pid, uid); |
| 5272 | }) != mInteractionConnectionTokens.end(); |
| 5273 | } |
| 5274 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 5275 | void InputDispatcher::setMaximumObscuringOpacityForTouch(float opacity) { |
| 5276 | if (opacity < 0 || opacity > 1) { |
| 5277 | LOG_ALWAYS_FATAL("Maximum obscuring opacity for touch should be >= 0 and <= 1"); |
| 5278 | return; |
| 5279 | } |
| 5280 | |
| 5281 | std::scoped_lock lock(mLock); |
| 5282 | mMaximumObscuringOpacityForTouch = opacity; |
| 5283 | } |
| 5284 | |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5285 | std::tuple<TouchState*, TouchedWindow*, int32_t /*displayId*/> |
| 5286 | InputDispatcher::findTouchStateWindowAndDisplayLocked(const sp<IBinder>& token) { |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5287 | for (auto& [displayId, state] : mTouchStatesByDisplay) { |
| 5288 | for (TouchedWindow& w : state.windows) { |
| 5289 | if (w.windowHandle->getToken() == token) { |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5290 | return std::make_tuple(&state, &w, displayId); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5291 | } |
| 5292 | } |
| 5293 | } |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5294 | return std::make_tuple(nullptr, nullptr, ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5295 | } |
| 5296 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5297 | bool InputDispatcher::transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken, |
| 5298 | bool isDragDrop) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 5299 | if (fromToken == toToken) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5300 | if (DEBUG_FOCUS) { |
| 5301 | ALOGD("Trivial transfer to same window."); |
| 5302 | } |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 5303 | return true; |
| 5304 | } |
| 5305 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5306 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5307 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5308 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5309 | // Find the target touch state and touched window by fromToken. |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5310 | auto [state, touchedWindow, displayId] = findTouchStateWindowAndDisplayLocked(fromToken); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5311 | if (state == nullptr || touchedWindow == nullptr) { |
| 5312 | ALOGD("Focus transfer failed because from window is not being touched."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5313 | return false; |
| 5314 | } |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5315 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5316 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(toToken, displayId); |
| 5317 | if (toWindowHandle == nullptr) { |
| 5318 | ALOGW("Cannot transfer focus because to window not found."); |
| 5319 | return false; |
| 5320 | } |
| 5321 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5322 | if (DEBUG_FOCUS) { |
| 5323 | ALOGD("transferTouchFocus: fromWindowHandle=%s, toWindowHandle=%s", |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5324 | touchedWindow->windowHandle->getName().c_str(), |
| 5325 | toWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5326 | } |
| 5327 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5328 | // Erase old window. |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5329 | ftl::Flags<InputTarget::Flags> oldTargetFlags = touchedWindow->targetFlags; |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 5330 | std::bitset<MAX_POINTER_ID + 1> pointerIds = touchedWindow->pointerIds; |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5331 | sp<WindowInfoHandle> fromWindowHandle = touchedWindow->windowHandle; |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5332 | state->removeWindowByToken(fromToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5333 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5334 | // Add new window. |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 5335 | nsecs_t downTimeInTarget = now(); |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5336 | ftl::Flags<InputTarget::Flags> newTargetFlags = |
| 5337 | oldTargetFlags & (InputTarget::Flags::SPLIT | InputTarget::Flags::DISPATCH_AS_IS); |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 5338 | if (canReceiveForegroundTouches(*toWindowHandle->getInfo())) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5339 | newTargetFlags |= InputTarget::Flags::FOREGROUND; |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 5340 | } |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 5341 | state->addOrUpdateWindow(toWindowHandle, newTargetFlags, pointerIds, downTimeInTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5342 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5343 | // Store the dragging window. |
| 5344 | if (isDragDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 5345 | if (pointerIds.count() != 1) { |
| 5346 | ALOGW("The drag and drop cannot be started when there is no pointer or more than 1" |
| 5347 | " pointer on the window."); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 5348 | return false; |
| 5349 | } |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 5350 | // Track the pointer id for drag window and generate the drag state. |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 5351 | const size_t id = firstMarkedBit(pointerIds); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 5352 | mDragState = std::make_unique<DragState>(toWindowHandle, id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5353 | } |
| 5354 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5355 | // Synthesize cancel for old window and down for new window. |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 5356 | std::shared_ptr<Connection> fromConnection = getConnectionLocked(fromToken); |
| 5357 | std::shared_ptr<Connection> toConnection = getConnectionLocked(toToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5358 | if (fromConnection != nullptr && toConnection != nullptr) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5359 | fromConnection->inputState.mergePointerStateTo(toConnection->inputState); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5360 | CancelationOptions |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 5361 | options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5362 | "transferring touch focus from this window to another window"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5363 | synthesizeCancelationEventsForConnectionLocked(fromConnection, options); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5364 | synthesizePointerDownEventsForConnectionLocked(downTimeInTarget, toConnection, |
| 5365 | newTargetFlags); |
| 5366 | |
| 5367 | // Check if the wallpaper window should deliver the corresponding event. |
| 5368 | transferWallpaperTouch(oldTargetFlags, newTargetFlags, fromWindowHandle, toWindowHandle, |
| 5369 | *state, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5370 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5371 | } // release lock |
| 5372 | |
| 5373 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5374 | mLooper->wake(); |
| 5375 | return true; |
| 5376 | } |
| 5377 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5378 | /** |
| 5379 | * Get the touched foreground window on the given display. |
| 5380 | * Return null if there are no windows touched on that display, or if more than one foreground |
| 5381 | * window is being touched. |
| 5382 | */ |
| 5383 | sp<WindowInfoHandle> InputDispatcher::findTouchedForegroundWindowLocked(int32_t displayId) const { |
| 5384 | auto stateIt = mTouchStatesByDisplay.find(displayId); |
| 5385 | if (stateIt == mTouchStatesByDisplay.end()) { |
| 5386 | ALOGI("No touch state on display %" PRId32, displayId); |
| 5387 | return nullptr; |
| 5388 | } |
| 5389 | |
| 5390 | const TouchState& state = stateIt->second; |
| 5391 | sp<WindowInfoHandle> touchedForegroundWindow; |
| 5392 | // If multiple foreground windows are touched, return nullptr |
| 5393 | for (const TouchedWindow& window : state.windows) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5394 | if (window.targetFlags.test(InputTarget::Flags::FOREGROUND)) { |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5395 | if (touchedForegroundWindow != nullptr) { |
| 5396 | ALOGI("Two or more foreground windows: %s and %s", |
| 5397 | touchedForegroundWindow->getName().c_str(), |
| 5398 | window.windowHandle->getName().c_str()); |
| 5399 | return nullptr; |
| 5400 | } |
| 5401 | touchedForegroundWindow = window.windowHandle; |
| 5402 | } |
| 5403 | } |
| 5404 | return touchedForegroundWindow; |
| 5405 | } |
| 5406 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5407 | // Binder call |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5408 | bool InputDispatcher::transferTouch(const sp<IBinder>& destChannelToken, int32_t displayId) { |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5409 | sp<IBinder> fromToken; |
| 5410 | { // acquire lock |
| 5411 | std::scoped_lock _l(mLock); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5412 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(destChannelToken, displayId); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5413 | if (toWindowHandle == nullptr) { |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5414 | ALOGW("Could not find window associated with token=%p on display %" PRId32, |
| 5415 | destChannelToken.get(), displayId); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5416 | return false; |
| 5417 | } |
| 5418 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5419 | sp<WindowInfoHandle> from = findTouchedForegroundWindowLocked(displayId); |
| 5420 | if (from == nullptr) { |
| 5421 | ALOGE("Could not find a source window in %s for %p", __func__, destChannelToken.get()); |
| 5422 | return false; |
| 5423 | } |
| 5424 | |
| 5425 | fromToken = from->getToken(); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5426 | } // release lock |
| 5427 | |
| 5428 | return transferTouchFocus(fromToken, destChannelToken); |
| 5429 | } |
| 5430 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5431 | void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5432 | if (DEBUG_FOCUS) { |
| 5433 | ALOGD("Resetting and dropping all events (%s).", reason); |
| 5434 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5435 | |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 5436 | CancelationOptions options(CancelationOptions::Mode::CANCEL_ALL_EVENTS, reason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5437 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 5438 | |
| 5439 | resetKeyRepeatLocked(); |
| 5440 | releasePendingEventLocked(); |
| 5441 | drainInboundQueueLocked(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5442 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5443 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5444 | mAnrTracker.clear(); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5445 | mTouchStatesByDisplay.clear(); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5446 | mReplacedKeys.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5447 | } |
| 5448 | |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 5449 | void InputDispatcher::logDispatchStateLocked() const { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5450 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5451 | dumpDispatchStateLocked(dump); |
| 5452 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5453 | std::istringstream stream(dump); |
| 5454 | std::string line; |
| 5455 | |
| 5456 | while (std::getline(stream, line, '\n')) { |
| 5457 | ALOGD("%s", line.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5458 | } |
| 5459 | } |
| 5460 | |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 5461 | std::string InputDispatcher::dumpPointerCaptureStateLocked() const { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5462 | std::string dump; |
| 5463 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5464 | dump += StringPrintf(INDENT "Pointer Capture Requested: %s\n", |
| 5465 | toString(mCurrentPointerCaptureRequest.enable)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5466 | |
| 5467 | std::string windowName = "None"; |
| 5468 | if (mWindowTokenWithPointerCapture) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5469 | const sp<WindowInfoHandle> captureWindowHandle = |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5470 | getWindowHandleLocked(mWindowTokenWithPointerCapture); |
| 5471 | windowName = captureWindowHandle ? captureWindowHandle->getName().c_str() |
| 5472 | : "token has capture without window"; |
| 5473 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5474 | 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] | 5475 | |
| 5476 | return dump; |
| 5477 | } |
| 5478 | |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 5479 | void InputDispatcher::dumpDispatchStateLocked(std::string& dump) const { |
Siarhei Vishniakou | 043a3ec | 2019-05-01 11:30:46 -0700 | [diff] [blame] | 5480 | dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled)); |
| 5481 | dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen)); |
| 5482 | dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5483 | dump += StringPrintf(INDENT "FocusedDisplayId: %" PRId32 "\n", mFocusedDisplayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5484 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5485 | if (!mFocusedApplicationHandlesByDisplay.empty()) { |
| 5486 | dump += StringPrintf(INDENT "FocusedApplications:\n"); |
| 5487 | for (auto& it : mFocusedApplicationHandlesByDisplay) { |
| 5488 | const int32_t displayId = it.first; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5489 | const std::shared_ptr<InputApplicationHandle>& applicationHandle = it.second; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5490 | const std::chrono::duration timeout = |
| 5491 | applicationHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5492 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5493 | ", name='%s', dispatchingTimeout=%" PRId64 "ms\n", |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5494 | displayId, applicationHandle->getName().c_str(), millis(timeout)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5495 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5496 | } else { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5497 | dump += StringPrintf(INDENT "FocusedApplications: <none>\n"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5498 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5499 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5500 | dump += mFocusResolver.dump(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5501 | dump += dumpPointerCaptureStateLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5502 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5503 | if (!mTouchStatesByDisplay.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5504 | dump += StringPrintf(INDENT "TouchStatesByDisplay:\n"); |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5505 | for (const auto& [displayId, state] : mTouchStatesByDisplay) { |
Siarhei Vishniakou | 6e1e987 | 2022-11-08 17:51:35 -0800 | [diff] [blame] | 5506 | std::string touchStateDump = addLinePrefix(state.dump(), INDENT2); |
| 5507 | dump += INDENT2 + std::to_string(displayId) + " : " + touchStateDump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5508 | } |
| 5509 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5510 | dump += INDENT "TouchStates: <no displays touched>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5511 | } |
| 5512 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5513 | if (mDragState) { |
| 5514 | dump += StringPrintf(INDENT "DragState:\n"); |
| 5515 | mDragState->dump(dump, INDENT2); |
| 5516 | } |
| 5517 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5518 | if (!mWindowHandlesByDisplay.empty()) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5519 | for (const auto& [displayId, windowHandles] : mWindowHandlesByDisplay) { |
| 5520 | dump += StringPrintf(INDENT "Display: %" PRId32 "\n", displayId); |
| 5521 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
| 5522 | const auto& displayInfo = it->second; |
| 5523 | dump += StringPrintf(INDENT2 "logicalSize=%dx%d\n", displayInfo.logicalWidth, |
| 5524 | displayInfo.logicalHeight); |
| 5525 | displayInfo.transform.dump(dump, "transform", INDENT4); |
| 5526 | } else { |
| 5527 | dump += INDENT2 "No DisplayInfo found!\n"; |
| 5528 | } |
| 5529 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5530 | if (!windowHandles.empty()) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5531 | dump += INDENT2 "Windows:\n"; |
| 5532 | for (size_t i = 0; i < windowHandles.size(); i++) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5533 | const sp<WindowInfoHandle>& windowHandle = windowHandles[i]; |
| 5534 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5535 | |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5536 | dump += StringPrintf(INDENT3 "%zu: name='%s', id=%" PRId32 ", displayId=%d, " |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5537 | "inputConfig=%s, alpha=%.2f, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5538 | "frame=[%d,%d][%d,%d], globalScale=%f, " |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5539 | "applicationInfo.name=%s, " |
| 5540 | "applicationInfo.token=%s, " |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 5541 | "touchableRegion=", |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5542 | i, windowInfo->name.c_str(), windowInfo->id, |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5543 | windowInfo->displayId, |
| 5544 | windowInfo->inputConfig.string().c_str(), |
| 5545 | windowInfo->alpha, windowInfo->frameLeft, |
| 5546 | windowInfo->frameTop, windowInfo->frameRight, |
| 5547 | windowInfo->frameBottom, windowInfo->globalScaleFactor, |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5548 | windowInfo->applicationInfo.name.c_str(), |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 5549 | binderToString(windowInfo->applicationInfo.token).c_str()); |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 5550 | dump += dumpRegion(windowInfo->touchableRegion); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5551 | dump += StringPrintf(", ownerPid=%d, ownerUid=%d, dispatchingTimeout=%" PRId64 |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5552 | "ms, hasToken=%s, " |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5553 | "touchOcclusionMode=%s\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5554 | windowInfo->ownerPid, windowInfo->ownerUid, |
Bernardo Rufino | c2f1fad | 2020-11-04 17:30:57 +0000 | [diff] [blame] | 5555 | millis(windowInfo->dispatchingTimeout), |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 5556 | binderToString(windowInfo->token).c_str(), |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5557 | toString(windowInfo->touchOcclusionMode).c_str()); |
chaviw | 85b4420 | 2020-07-24 11:46:21 -0700 | [diff] [blame] | 5558 | windowInfo->transform.dump(dump, "transform", INDENT4); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5559 | } |
| 5560 | } else { |
| 5561 | dump += INDENT2 "Windows: <none>\n"; |
| 5562 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5563 | } |
| 5564 | } else { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5565 | dump += INDENT "Displays: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5566 | } |
| 5567 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5568 | if (!mGlobalMonitorsByDisplay.empty()) { |
| 5569 | for (const auto& [displayId, monitors] : mGlobalMonitorsByDisplay) { |
| 5570 | dump += StringPrintf(INDENT "Global monitors on display %d:\n", displayId); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5571 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5572 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5573 | } else { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5574 | dump += INDENT "Global Monitors: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5575 | } |
| 5576 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5577 | const nsecs_t currentTime = now(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5578 | |
| 5579 | // Dump recently dispatched or dropped events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5580 | if (!mRecentQueue.empty()) { |
| 5581 | dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size()); |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 5582 | for (const std::shared_ptr<EventEntry>& entry : mRecentQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5583 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5584 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5585 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5586 | } |
| 5587 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5588 | dump += INDENT "RecentQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5589 | } |
| 5590 | |
| 5591 | // Dump event currently being dispatched. |
| 5592 | if (mPendingEvent) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5593 | dump += INDENT "PendingEvent:\n"; |
| 5594 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5595 | dump += mPendingEvent->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5596 | dump += StringPrintf(", age=%" PRId64 "ms\n", |
| 5597 | ns2ms(currentTime - mPendingEvent->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5598 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5599 | dump += INDENT "PendingEvent: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5600 | } |
| 5601 | |
| 5602 | // Dump inbound events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5603 | if (!mInboundQueue.empty()) { |
| 5604 | dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size()); |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 5605 | for (const std::shared_ptr<EventEntry>& entry : mInboundQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5606 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5607 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5608 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5609 | } |
| 5610 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5611 | dump += INDENT "InboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5612 | } |
| 5613 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5614 | if (!mReplacedKeys.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5615 | dump += INDENT "ReplacedKeys:\n"; |
Michael Wright | 3cec446 | 2022-11-24 22:05:46 +0000 | [diff] [blame] | 5616 | for (const auto& [replacement, newKeyCode] : mReplacedKeys) { |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5617 | dump += StringPrintf(INDENT2 "originalKeyCode=%d, deviceId=%d -> newKeyCode=%d\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5618 | replacement.keyCode, replacement.deviceId, newKeyCode); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5619 | } |
| 5620 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5621 | dump += INDENT "ReplacedKeys: <empty>\n"; |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5622 | } |
| 5623 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5624 | if (!mCommandQueue.empty()) { |
| 5625 | dump += StringPrintf(INDENT "CommandQueue: size=%zu\n", mCommandQueue.size()); |
| 5626 | } else { |
| 5627 | dump += INDENT "CommandQueue: <empty>\n"; |
| 5628 | } |
| 5629 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5630 | if (!mConnectionsByToken.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5631 | dump += INDENT "Connections:\n"; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5632 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5633 | dump += StringPrintf(INDENT2 "%i: channelName='%s', windowName='%s', " |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5634 | "status=%s, monitor=%s, responsive=%s\n", |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5635 | connection->inputChannel->getFd().get(), |
| 5636 | connection->getInputChannelName().c_str(), |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5637 | connection->getWindowName().c_str(), |
| 5638 | ftl::enum_string(connection->status).c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5639 | toString(connection->monitor), toString(connection->responsive)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5640 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5641 | if (!connection->outboundQueue.empty()) { |
| 5642 | dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n", |
| 5643 | connection->outboundQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5644 | dump += dumpQueue(connection->outboundQueue, currentTime); |
| 5645 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5646 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5647 | dump += INDENT3 "OutboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5648 | } |
| 5649 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5650 | if (!connection->waitQueue.empty()) { |
| 5651 | dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n", |
| 5652 | connection->waitQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5653 | dump += dumpQueue(connection->waitQueue, currentTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5654 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5655 | dump += INDENT3 "WaitQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5656 | } |
| 5657 | } |
| 5658 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5659 | dump += INDENT "Connections: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5660 | } |
| 5661 | |
| 5662 | if (isAppSwitchPendingLocked()) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5663 | dump += StringPrintf(INDENT "AppSwitch: pending, due in %" PRId64 "ms\n", |
| 5664 | ns2ms(mAppSwitchDueTime - now())); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5665 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5666 | dump += INDENT "AppSwitch: not pending\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5667 | } |
| 5668 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5669 | if (!mTouchModePerDisplay.empty()) { |
| 5670 | dump += INDENT "TouchModePerDisplay:\n"; |
| 5671 | for (const auto& [displayId, touchMode] : mTouchModePerDisplay) { |
| 5672 | dump += StringPrintf(INDENT2 "Display: %" PRId32 " TouchMode: %s\n", displayId, |
| 5673 | std::to_string(touchMode).c_str()); |
| 5674 | } |
| 5675 | } else { |
| 5676 | dump += INDENT "TouchModePerDisplay: <none>\n"; |
| 5677 | } |
| 5678 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5679 | dump += INDENT "Configuration:\n"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5680 | dump += StringPrintf(INDENT2 "KeyRepeatDelay: %" PRId64 "ms\n", ns2ms(mConfig.keyRepeatDelay)); |
| 5681 | dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n", |
| 5682 | ns2ms(mConfig.keyRepeatTimeout)); |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 5683 | dump += mLatencyTracker.dump(INDENT2); |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 5684 | dump += mLatencyAggregator.dump(INDENT2); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5685 | } |
| 5686 | |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 5687 | void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) const { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5688 | const size_t numMonitors = monitors.size(); |
| 5689 | for (size_t i = 0; i < numMonitors; i++) { |
| 5690 | const Monitor& monitor = monitors[i]; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5691 | const std::shared_ptr<InputChannel>& channel = monitor.inputChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5692 | dump += StringPrintf(INDENT2 "%zu: '%s', ", i, channel->getName().c_str()); |
| 5693 | dump += "\n"; |
| 5694 | } |
| 5695 | } |
| 5696 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5697 | class LooperEventCallback : public LooperCallback { |
| 5698 | public: |
| 5699 | LooperEventCallback(std::function<int(int events)> callback) : mCallback(callback) {} |
| 5700 | int handleEvent(int /*fd*/, int events, void* /*data*/) override { return mCallback(events); } |
| 5701 | |
| 5702 | private: |
| 5703 | std::function<int(int events)> mCallback; |
| 5704 | }; |
| 5705 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5706 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputChannel(const std::string& name) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5707 | if (DEBUG_CHANNEL_CREATION) { |
| 5708 | ALOGD("channel '%s' ~ createInputChannel", name.c_str()); |
| 5709 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5710 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5711 | std::unique_ptr<InputChannel> serverChannel; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5712 | std::unique_ptr<InputChannel> clientChannel; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5713 | status_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5714 | |
| 5715 | if (result) { |
| 5716 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5717 | } |
| 5718 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5719 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5720 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5721 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5722 | int fd = serverChannel->getFd(); |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 5723 | std::shared_ptr<Connection> connection = |
| 5724 | std::make_shared<Connection>(std::move(serverChannel), /*monitor=*/false, |
| 5725 | mIdGenerator); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5726 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5727 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5728 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5729 | } |
| 5730 | mConnectionsByToken.emplace(token, connection); |
| 5731 | |
| 5732 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5733 | this, std::placeholders::_1, token); |
| 5734 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5735 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, sp<LooperEventCallback>::make(callback), |
| 5736 | nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5737 | } // release lock |
| 5738 | |
| 5739 | // Wake the looper because some connections have changed. |
| 5740 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5741 | return clientChannel; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5742 | } |
| 5743 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5744 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputMonitor(int32_t displayId, |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5745 | const std::string& name, |
| 5746 | int32_t pid) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5747 | std::shared_ptr<InputChannel> serverChannel; |
| 5748 | std::unique_ptr<InputChannel> clientChannel; |
| 5749 | status_t result = openInputChannelPair(name, serverChannel, clientChannel); |
| 5750 | if (result) { |
| 5751 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5752 | } |
| 5753 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5754 | { // acquire lock |
| 5755 | std::scoped_lock _l(mLock); |
| 5756 | |
| 5757 | if (displayId < 0) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5758 | return base::Error(BAD_VALUE) << "Attempted to create input monitor with name " << name |
| 5759 | << " without a specified display."; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5760 | } |
| 5761 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 5762 | std::shared_ptr<Connection> connection = |
| 5763 | std::make_shared<Connection>(serverChannel, /*monitor=*/true, mIdGenerator); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5764 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5765 | const int fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5766 | |
| 5767 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5768 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5769 | } |
| 5770 | mConnectionsByToken.emplace(token, connection); |
| 5771 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5772 | this, std::placeholders::_1, token); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5773 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5774 | mGlobalMonitorsByDisplay[displayId].emplace_back(serverChannel, pid); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5775 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5776 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, sp<LooperEventCallback>::make(callback), |
| 5777 | nullptr); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5778 | } |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5779 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5780 | // Wake the looper because some connections have changed. |
| 5781 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5782 | return clientChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5783 | } |
| 5784 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5785 | status_t InputDispatcher::removeInputChannel(const sp<IBinder>& connectionToken) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5786 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5787 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5788 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5789 | status_t status = removeInputChannelLocked(connectionToken, /*notify=*/false); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5790 | if (status) { |
| 5791 | return status; |
| 5792 | } |
| 5793 | } // release lock |
| 5794 | |
| 5795 | // Wake the poll loop because removing the connection may have changed the current |
| 5796 | // synchronization state. |
| 5797 | mLooper->wake(); |
| 5798 | return OK; |
| 5799 | } |
| 5800 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5801 | status_t InputDispatcher::removeInputChannelLocked(const sp<IBinder>& connectionToken, |
| 5802 | bool notify) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 5803 | std::shared_ptr<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5804 | if (connection == nullptr) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5805 | // 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] | 5806 | return BAD_VALUE; |
| 5807 | } |
| 5808 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5809 | removeConnectionLocked(connection); |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 5810 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5811 | if (connection->monitor) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5812 | removeMonitorChannelLocked(connectionToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5813 | } |
| 5814 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5815 | mLooper->removeFd(connection->inputChannel->getFd()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5816 | |
| 5817 | nsecs_t currentTime = now(); |
| 5818 | abortBrokenDispatchCycleLocked(currentTime, connection, notify); |
| 5819 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5820 | connection->status = Connection::Status::ZOMBIE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5821 | return OK; |
| 5822 | } |
| 5823 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5824 | void InputDispatcher::removeMonitorChannelLocked(const sp<IBinder>& connectionToken) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5825 | for (auto it = mGlobalMonitorsByDisplay.begin(); it != mGlobalMonitorsByDisplay.end();) { |
| 5826 | auto& [displayId, monitors] = *it; |
| 5827 | std::erase_if(monitors, [connectionToken](const Monitor& monitor) { |
| 5828 | return monitor.inputChannel->getConnectionToken() == connectionToken; |
| 5829 | }); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5830 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5831 | if (monitors.empty()) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5832 | it = mGlobalMonitorsByDisplay.erase(it); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5833 | } else { |
| 5834 | ++it; |
| 5835 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5836 | } |
| 5837 | } |
| 5838 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5839 | status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5840 | std::scoped_lock _l(mLock); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 5841 | return pilferPointersLocked(token); |
| 5842 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5843 | |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 5844 | status_t InputDispatcher::pilferPointersLocked(const sp<IBinder>& token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5845 | const std::shared_ptr<InputChannel> requestingChannel = getInputChannelLocked(token); |
| 5846 | if (!requestingChannel) { |
| 5847 | ALOGW("Attempted to pilfer pointers from an un-registered channel or invalid token"); |
| 5848 | return BAD_VALUE; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5849 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5850 | |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5851 | auto [statePtr, windowPtr, displayId] = findTouchStateWindowAndDisplayLocked(token); |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 5852 | if (statePtr == nullptr || windowPtr == nullptr || windowPtr->pointerIds.none()) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5853 | ALOGW("Attempted to pilfer points from a channel without any on-going pointer streams." |
| 5854 | " Ignoring."); |
| 5855 | return BAD_VALUE; |
| 5856 | } |
| 5857 | |
| 5858 | TouchState& state = *statePtr; |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 5859 | TouchedWindow& window = *windowPtr; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5860 | // Send cancel events to all the input channels we're stealing from. |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 5861 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5862 | "input channel stole pointer stream"); |
| 5863 | options.deviceId = state.deviceId; |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5864 | options.displayId = displayId; |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 5865 | options.pointerIds = window.pointerIds; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5866 | std::string canceledWindows; |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 5867 | for (const TouchedWindow& w : state.windows) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5868 | const std::shared_ptr<InputChannel> channel = |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 5869 | getInputChannelLocked(w.windowHandle->getToken()); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5870 | if (channel != nullptr && channel->getConnectionToken() != token) { |
| 5871 | synthesizeCancelationEventsForInputChannelLocked(channel, options); |
| 5872 | canceledWindows += canceledWindows.empty() ? "[" : ", "; |
| 5873 | canceledWindows += channel->getName(); |
| 5874 | } |
| 5875 | } |
| 5876 | canceledWindows += canceledWindows.empty() ? "[]" : "]"; |
| 5877 | ALOGI("Channel %s is stealing touch from %s", requestingChannel->getName().c_str(), |
| 5878 | canceledWindows.c_str()); |
| 5879 | |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 5880 | // Prevent the gesture from being sent to any other windows. |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 5881 | // This only blocks relevant pointers to be sent to other windows |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 5882 | window.pilferedPointerIds |= window.pointerIds; |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 5883 | |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 5884 | state.cancelPointersForWindowsExcept(window.pointerIds, token); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5885 | return OK; |
| 5886 | } |
| 5887 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5888 | void InputDispatcher::requestPointerCapture(const sp<IBinder>& windowToken, bool enabled) { |
| 5889 | { // acquire lock |
| 5890 | std::scoped_lock _l(mLock); |
| 5891 | if (DEBUG_FOCUS) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5892 | const sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(windowToken); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5893 | ALOGI("Request to %s Pointer Capture from: %s.", enabled ? "enable" : "disable", |
| 5894 | windowHandle != nullptr ? windowHandle->getName().c_str() |
| 5895 | : "token without window"); |
| 5896 | } |
| 5897 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5898 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5899 | if (focusedToken != windowToken) { |
| 5900 | ALOGW("Ignoring request to %s Pointer Capture: window does not have focus.", |
| 5901 | enabled ? "enable" : "disable"); |
| 5902 | return; |
| 5903 | } |
| 5904 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5905 | if (enabled == mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5906 | ALOGW("Ignoring request to %s Pointer Capture: " |
| 5907 | "window has %s requested pointer capture.", |
| 5908 | enabled ? "enable" : "disable", enabled ? "already" : "not"); |
| 5909 | return; |
| 5910 | } |
| 5911 | |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 5912 | if (enabled) { |
| 5913 | if (std::find(mIneligibleDisplaysForPointerCapture.begin(), |
| 5914 | mIneligibleDisplaysForPointerCapture.end(), |
| 5915 | mFocusedDisplayId) != mIneligibleDisplaysForPointerCapture.end()) { |
| 5916 | ALOGW("Ignoring request to enable Pointer Capture: display is not eligible"); |
| 5917 | return; |
| 5918 | } |
| 5919 | } |
| 5920 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5921 | setPointerCaptureLocked(enabled); |
| 5922 | } // release lock |
| 5923 | |
| 5924 | // Wake the thread to process command entries. |
| 5925 | mLooper->wake(); |
| 5926 | } |
| 5927 | |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 5928 | void InputDispatcher::setDisplayEligibilityForPointerCapture(int32_t displayId, bool isEligible) { |
| 5929 | { // acquire lock |
| 5930 | std::scoped_lock _l(mLock); |
| 5931 | std::erase(mIneligibleDisplaysForPointerCapture, displayId); |
| 5932 | if (!isEligible) { |
| 5933 | mIneligibleDisplaysForPointerCapture.push_back(displayId); |
| 5934 | } |
| 5935 | } // release lock |
| 5936 | } |
| 5937 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5938 | std::optional<int32_t> InputDispatcher::findMonitorPidByTokenLocked(const sp<IBinder>& token) { |
| 5939 | for (const auto& [_, monitors] : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5940 | for (const Monitor& monitor : monitors) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5941 | if (monitor.inputChannel->getConnectionToken() == token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5942 | return monitor.pid; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5943 | } |
| 5944 | } |
| 5945 | } |
| 5946 | return std::nullopt; |
| 5947 | } |
| 5948 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 5949 | std::shared_ptr<Connection> InputDispatcher::getConnectionLocked( |
| 5950 | const sp<IBinder>& inputConnectionToken) const { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 5951 | if (inputConnectionToken == nullptr) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5952 | return nullptr; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 5953 | } |
| 5954 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5955 | for (const auto& [token, connection] : mConnectionsByToken) { |
| 5956 | if (token == inputConnectionToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5957 | return connection; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5958 | } |
| 5959 | } |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 5960 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5961 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5962 | } |
| 5963 | |
Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 5964 | std::string InputDispatcher::getConnectionNameLocked(const sp<IBinder>& connectionToken) const { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 5965 | std::shared_ptr<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 5966 | if (connection == nullptr) { |
| 5967 | return "<nullptr>"; |
| 5968 | } |
| 5969 | return connection->getInputChannelName(); |
| 5970 | } |
| 5971 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 5972 | void InputDispatcher::removeConnectionLocked(const std::shared_ptr<Connection>& connection) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5973 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5974 | mConnectionsByToken.erase(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5975 | } |
| 5976 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5977 | void InputDispatcher::doDispatchCycleFinishedCommand(nsecs_t finishTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 5978 | const std::shared_ptr<Connection>& connection, |
| 5979 | uint32_t seq, bool handled, |
| 5980 | nsecs_t consumeTime) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5981 | // Handle post-event policy actions. |
| 5982 | std::deque<DispatchEntry*>::iterator dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 5983 | if (dispatchEntryIt == connection->waitQueue.end()) { |
| 5984 | return; |
| 5985 | } |
| 5986 | DispatchEntry* dispatchEntry = *dispatchEntryIt; |
| 5987 | const nsecs_t eventDuration = finishTime - dispatchEntry->deliveryTime; |
| 5988 | if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) { |
| 5989 | ALOGI("%s spent %" PRId64 "ms processing %s", connection->getWindowName().c_str(), |
| 5990 | ns2ms(eventDuration), dispatchEntry->eventEntry->getDescription().c_str()); |
| 5991 | } |
| 5992 | if (shouldReportFinishedEvent(*dispatchEntry, *connection)) { |
| 5993 | mLatencyTracker.trackFinishedEvent(dispatchEntry->eventEntry->id, |
| 5994 | connection->inputChannel->getConnectionToken(), |
| 5995 | dispatchEntry->deliveryTime, consumeTime, finishTime); |
| 5996 | } |
| 5997 | |
| 5998 | bool restartEvent; |
| 5999 | if (dispatchEntry->eventEntry->type == EventEntry::Type::KEY) { |
| 6000 | KeyEntry& keyEntry = static_cast<KeyEntry&>(*(dispatchEntry->eventEntry)); |
| 6001 | restartEvent = |
| 6002 | afterKeyEventLockedInterruptable(connection, dispatchEntry, keyEntry, handled); |
| 6003 | } else if (dispatchEntry->eventEntry->type == EventEntry::Type::MOTION) { |
| 6004 | MotionEntry& motionEntry = static_cast<MotionEntry&>(*(dispatchEntry->eventEntry)); |
| 6005 | restartEvent = afterMotionEventLockedInterruptable(connection, dispatchEntry, motionEntry, |
| 6006 | handled); |
| 6007 | } else { |
| 6008 | restartEvent = false; |
| 6009 | } |
| 6010 | |
| 6011 | // Dequeue the event and start the next cycle. |
| 6012 | // Because the lock might have been released, it is possible that the |
| 6013 | // contents of the wait queue to have been drained, so we need to double-check |
| 6014 | // a few things. |
| 6015 | dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 6016 | if (dispatchEntryIt != connection->waitQueue.end()) { |
| 6017 | dispatchEntry = *dispatchEntryIt; |
| 6018 | connection->waitQueue.erase(dispatchEntryIt); |
| 6019 | const sp<IBinder>& connectionToken = connection->inputChannel->getConnectionToken(); |
| 6020 | mAnrTracker.erase(dispatchEntry->timeoutTime, connectionToken); |
| 6021 | if (!connection->responsive) { |
| 6022 | connection->responsive = isConnectionResponsive(*connection); |
| 6023 | if (connection->responsive) { |
| 6024 | // The connection was unresponsive, and now it's responsive. |
| 6025 | processConnectionResponsiveLocked(*connection); |
| 6026 | } |
| 6027 | } |
| 6028 | traceWaitQueueLength(*connection); |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 6029 | if (restartEvent && connection->status == Connection::Status::NORMAL) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6030 | connection->outboundQueue.push_front(dispatchEntry); |
| 6031 | traceOutboundQueueLength(*connection); |
| 6032 | } else { |
| 6033 | releaseDispatchEntry(dispatchEntry); |
| 6034 | } |
| 6035 | } |
| 6036 | |
| 6037 | // Start the next dispatch cycle for this connection. |
| 6038 | startDispatchCycleLocked(now(), connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6039 | } |
| 6040 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6041 | void InputDispatcher::sendFocusChangedCommandLocked(const sp<IBinder>& oldToken, |
| 6042 | const sp<IBinder>& newToken) { |
| 6043 | auto command = [this, oldToken, newToken]() REQUIRES(mLock) { |
| 6044 | scoped_unlock unlock(mLock); |
| 6045 | mPolicy->notifyFocusChanged(oldToken, newToken); |
| 6046 | }; |
| 6047 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6048 | } |
| 6049 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6050 | void InputDispatcher::sendDropWindowCommandLocked(const sp<IBinder>& token, float x, float y) { |
| 6051 | auto command = [this, token, x, y]() REQUIRES(mLock) { |
| 6052 | scoped_unlock unlock(mLock); |
| 6053 | mPolicy->notifyDropWindow(token, x, y); |
| 6054 | }; |
| 6055 | postCommandLocked(std::move(command)); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 6056 | } |
| 6057 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6058 | void InputDispatcher::onAnrLocked(const std::shared_ptr<Connection>& connection) { |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6059 | if (connection == nullptr) { |
| 6060 | LOG_ALWAYS_FATAL("Caller must check for nullness"); |
| 6061 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6062 | // Since we are allowing the policy to extend the timeout, maybe the waitQueue |
| 6063 | // is already healthy again. Don't raise ANR in this situation |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6064 | if (connection->waitQueue.empty()) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6065 | ALOGI("Not raising ANR because the connection %s has recovered", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6066 | connection->inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6067 | return; |
| 6068 | } |
| 6069 | /** |
| 6070 | * The "oldestEntry" is the entry that was first sent to the application. That entry, however, |
| 6071 | * may not be the one that caused the timeout to occur. One possibility is that window timeout |
| 6072 | * has changed. This could cause newer entries to time out before the already dispatched |
| 6073 | * entries. In that situation, the newest entries caused ANR. But in all likelihood, the app |
| 6074 | * processes the events linearly. So providing information about the oldest entry seems to be |
| 6075 | * most useful. |
| 6076 | */ |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6077 | DispatchEntry* oldestEntry = *connection->waitQueue.begin(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6078 | const nsecs_t currentWait = now() - oldestEntry->deliveryTime; |
| 6079 | std::string reason = |
| 6080 | android::base::StringPrintf("%s is not responding. Waited %" PRId64 "ms for %s", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6081 | connection->inputChannel->getName().c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6082 | ns2ms(currentWait), |
| 6083 | oldestEntry->eventEntry->getDescription().c_str()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6084 | sp<IBinder> connectionToken = connection->inputChannel->getConnectionToken(); |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 6085 | updateLastAnrStateLocked(getWindowHandleLocked(connectionToken), reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6086 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6087 | processConnectionUnresponsiveLocked(*connection, std::move(reason)); |
| 6088 | |
| 6089 | // Stop waking up for events on this connection, it is already unresponsive |
| 6090 | cancelEventsForAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6091 | } |
| 6092 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6093 | void InputDispatcher::onAnrLocked(std::shared_ptr<InputApplicationHandle> application) { |
| 6094 | std::string reason = |
| 6095 | StringPrintf("%s does not have a focused window", application->getName().c_str()); |
| 6096 | updateLastAnrStateLocked(*application, reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6097 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6098 | auto command = [this, application = std::move(application)]() REQUIRES(mLock) { |
| 6099 | scoped_unlock unlock(mLock); |
| 6100 | mPolicy->notifyNoFocusedWindowAnr(application); |
| 6101 | }; |
| 6102 | postCommandLocked(std::move(command)); |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 6103 | } |
| 6104 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 6105 | void InputDispatcher::updateLastAnrStateLocked(const sp<WindowInfoHandle>& window, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6106 | const std::string& reason) { |
| 6107 | const std::string windowLabel = getApplicationWindowLabel(nullptr, window); |
| 6108 | updateLastAnrStateLocked(windowLabel, reason); |
| 6109 | } |
| 6110 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6111 | void InputDispatcher::updateLastAnrStateLocked(const InputApplicationHandle& application, |
| 6112 | const std::string& reason) { |
| 6113 | const std::string windowLabel = getApplicationWindowLabel(&application, nullptr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6114 | updateLastAnrStateLocked(windowLabel, reason); |
| 6115 | } |
| 6116 | |
| 6117 | void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel, |
| 6118 | const std::string& reason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6119 | // 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] | 6120 | time_t t = time(nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6121 | struct tm tm; |
| 6122 | localtime_r(&t, &tm); |
| 6123 | char timestr[64]; |
| 6124 | strftime(timestr, sizeof(timestr), "%F %T", &tm); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6125 | mLastAnrState.clear(); |
| 6126 | mLastAnrState += INDENT "ANR:\n"; |
| 6127 | mLastAnrState += StringPrintf(INDENT2 "Time: %s\n", timestr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6128 | mLastAnrState += StringPrintf(INDENT2 "Reason: %s\n", reason.c_str()); |
| 6129 | mLastAnrState += StringPrintf(INDENT2 "Window: %s\n", windowLabel.c_str()); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6130 | dumpDispatchStateLocked(mLastAnrState); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6131 | } |
| 6132 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6133 | void InputDispatcher::doInterceptKeyBeforeDispatchingCommand(const sp<IBinder>& focusedWindowToken, |
| 6134 | KeyEntry& entry) { |
| 6135 | const KeyEvent event = createKeyEvent(entry); |
| 6136 | nsecs_t delay = 0; |
| 6137 | { // release lock |
| 6138 | scoped_unlock unlock(mLock); |
| 6139 | android::base::Timer t; |
| 6140 | delay = mPolicy->interceptKeyBeforeDispatching(focusedWindowToken, &event, |
| 6141 | entry.policyFlags); |
| 6142 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 6143 | ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms", |
| 6144 | std::to_string(t.duration().count()).c_str()); |
| 6145 | } |
| 6146 | } // acquire lock |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6147 | |
| 6148 | if (delay < 0) { |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 6149 | entry.interceptKeyResult = KeyEntry::InterceptKeyResult::SKIP; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6150 | } else if (delay == 0) { |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 6151 | entry.interceptKeyResult = KeyEntry::InterceptKeyResult::CONTINUE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6152 | } else { |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 6153 | entry.interceptKeyResult = KeyEntry::InterceptKeyResult::TRY_AGAIN_LATER; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6154 | entry.interceptKeyWakeupTime = now() + delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6155 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6156 | } |
| 6157 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6158 | void InputDispatcher::sendWindowUnresponsiveCommandLocked(const sp<IBinder>& token, |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6159 | std::optional<int32_t> pid, |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6160 | std::string reason) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6161 | auto command = [this, token, pid, reason = std::move(reason)]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6162 | scoped_unlock unlock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6163 | mPolicy->notifyWindowUnresponsive(token, pid, reason); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6164 | }; |
| 6165 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6166 | } |
| 6167 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6168 | void InputDispatcher::sendWindowResponsiveCommandLocked(const sp<IBinder>& token, |
| 6169 | std::optional<int32_t> pid) { |
| 6170 | auto command = [this, token, pid]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6171 | scoped_unlock unlock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6172 | mPolicy->notifyWindowResponsive(token, pid); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6173 | }; |
| 6174 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6175 | } |
| 6176 | |
| 6177 | /** |
| 6178 | * Tell the policy that a connection has become unresponsive so that it can start ANR. |
| 6179 | * Check whether the connection of interest is a monitor or a window, and add the corresponding |
| 6180 | * command entry to the command queue. |
| 6181 | */ |
| 6182 | void InputDispatcher::processConnectionUnresponsiveLocked(const Connection& connection, |
| 6183 | std::string reason) { |
| 6184 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6185 | std::optional<int32_t> pid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6186 | if (connection.monitor) { |
| 6187 | ALOGW("Monitor %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 6188 | reason.c_str()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6189 | pid = findMonitorPidByTokenLocked(connectionToken); |
| 6190 | } else { |
| 6191 | // The connection is a window |
| 6192 | ALOGW("Window %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 6193 | reason.c_str()); |
| 6194 | const sp<WindowInfoHandle> handle = getWindowHandleLocked(connectionToken); |
| 6195 | if (handle != nullptr) { |
| 6196 | pid = handle->getInfo()->ownerPid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6197 | } |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6198 | } |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6199 | sendWindowUnresponsiveCommandLocked(connectionToken, pid, std::move(reason)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6200 | } |
| 6201 | |
| 6202 | /** |
| 6203 | * Tell the policy that a connection has become responsive so that it can stop ANR. |
| 6204 | */ |
| 6205 | void InputDispatcher::processConnectionResponsiveLocked(const Connection& connection) { |
| 6206 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6207 | std::optional<int32_t> pid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6208 | if (connection.monitor) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6209 | pid = findMonitorPidByTokenLocked(connectionToken); |
| 6210 | } else { |
| 6211 | // The connection is a window |
| 6212 | const sp<WindowInfoHandle> handle = getWindowHandleLocked(connectionToken); |
| 6213 | if (handle != nullptr) { |
| 6214 | pid = handle->getInfo()->ownerPid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6215 | } |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6216 | } |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6217 | sendWindowResponsiveCommandLocked(connectionToken, pid); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6218 | } |
| 6219 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6220 | bool InputDispatcher::afterKeyEventLockedInterruptable( |
| 6221 | const std::shared_ptr<Connection>& connection, DispatchEntry* dispatchEntry, |
| 6222 | KeyEntry& keyEntry, bool handled) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6223 | if (keyEntry.flags & AKEY_EVENT_FLAG_FALLBACK) { |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6224 | if (!handled) { |
| 6225 | // Report the key as unhandled, since the fallback was not handled. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6226 | mReporter->reportUnhandledKey(keyEntry.id); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6227 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6228 | return false; |
| 6229 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6230 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6231 | // Get the fallback key state. |
| 6232 | // Clear it out after dispatching the UP. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6233 | int32_t originalKeyCode = keyEntry.keyCode; |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6234 | std::optional<int32_t> fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6235 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6236 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6237 | } |
| 6238 | |
| 6239 | if (handled || !dispatchEntry->hasForegroundTarget()) { |
| 6240 | // If the application handles the original key for which we previously |
| 6241 | // generated a fallback or if the window is not a foreground window, |
| 6242 | // then cancel the associated fallback key, if any. |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6243 | if (fallbackKeyCode) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6244 | // Dispatch the unhandled key to the policy with the cancel flag. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6245 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6246 | ALOGD("Unhandled key event: Asking policy to cancel fallback action. " |
| 6247 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6248 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, |
| 6249 | keyEntry.policyFlags); |
| 6250 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6251 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6252 | event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6253 | |
| 6254 | mLock.unlock(); |
| 6255 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 6256 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), &event, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6257 | keyEntry.policyFlags, &event); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6258 | |
| 6259 | mLock.lock(); |
| 6260 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6261 | // Cancel the fallback key. |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6262 | if (*fallbackKeyCode != AKEYCODE_UNKNOWN) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 6263 | CancelationOptions options(CancelationOptions::Mode::CANCEL_FALLBACK_EVENTS, |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6264 | "application handled the original non-fallback key " |
| 6265 | "or is no longer a foreground target, " |
| 6266 | "canceling previously dispatched fallback key"); |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6267 | options.keyCode = *fallbackKeyCode; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6268 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6269 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6270 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6271 | } |
| 6272 | } else { |
| 6273 | // If the application did not handle a non-fallback key, first check |
| 6274 | // that we are in a good state to perform unhandled key event processing |
| 6275 | // Then ask the policy what to do with it. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6276 | bool initialDown = keyEntry.action == AKEY_EVENT_ACTION_DOWN && keyEntry.repeatCount == 0; |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6277 | if (!fallbackKeyCode && !initialDown) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6278 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6279 | ALOGD("Unhandled key event: Skipping unhandled key event processing " |
| 6280 | "since this is not an initial down. " |
| 6281 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6282 | originalKeyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 6283 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6284 | return false; |
| 6285 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6286 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6287 | // Dispatch the unhandled key to the policy. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6288 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6289 | ALOGD("Unhandled key event: Asking policy to perform fallback action. " |
| 6290 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6291 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 6292 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6293 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6294 | |
| 6295 | mLock.unlock(); |
| 6296 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 6297 | bool fallback = |
| 6298 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6299 | &event, keyEntry.policyFlags, &event); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6300 | |
| 6301 | mLock.lock(); |
| 6302 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 6303 | if (connection->status != Connection::Status::NORMAL) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6304 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6305 | return false; |
| 6306 | } |
| 6307 | |
| 6308 | // Latch the fallback keycode for this key on an initial down. |
| 6309 | // The fallback keycode cannot change at any other point in the lifecycle. |
| 6310 | if (initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6311 | if (fallback) { |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6312 | *fallbackKeyCode = event.getKeyCode(); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6313 | } else { |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6314 | *fallbackKeyCode = AKEYCODE_UNKNOWN; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6315 | } |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6316 | connection->inputState.setFallbackKey(originalKeyCode, *fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6317 | } |
| 6318 | |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6319 | ALOG_ASSERT(fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6320 | |
| 6321 | // Cancel the fallback key if the policy decides not to send it anymore. |
| 6322 | // We will continue to dispatch the key to the policy but we will no |
| 6323 | // longer dispatch a fallback key to the application. |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6324 | if (*fallbackKeyCode != AKEYCODE_UNKNOWN && |
| 6325 | (!fallback || *fallbackKeyCode != event.getKeyCode())) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6326 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6327 | if (fallback) { |
| 6328 | ALOGD("Unhandled key event: Policy requested to send key %d" |
| 6329 | "as a fallback for %d, but on the DOWN it had requested " |
| 6330 | "to send %d instead. Fallback canceled.", |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6331 | event.getKeyCode(), originalKeyCode, *fallbackKeyCode); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6332 | } else { |
| 6333 | ALOGD("Unhandled key event: Policy did not request fallback for %d, " |
| 6334 | "but on the DOWN it had requested to send %d. " |
| 6335 | "Fallback canceled.", |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6336 | originalKeyCode, *fallbackKeyCode); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6337 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6338 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6339 | |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 6340 | CancelationOptions options(CancelationOptions::Mode::CANCEL_FALLBACK_EVENTS, |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6341 | "canceling fallback, policy no longer desires it"); |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6342 | options.keyCode = *fallbackKeyCode; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6343 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
| 6344 | |
| 6345 | fallback = false; |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6346 | *fallbackKeyCode = AKEYCODE_UNKNOWN; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6347 | if (keyEntry.action != AKEY_EVENT_ACTION_UP) { |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6348 | connection->inputState.setFallbackKey(originalKeyCode, *fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6349 | } |
| 6350 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6351 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6352 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6353 | { |
| 6354 | std::string msg; |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6355 | const std::map<int32_t, int32_t>& fallbackKeys = |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6356 | connection->inputState.getFallbackKeys(); |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6357 | for (const auto& [key, value] : fallbackKeys) { |
| 6358 | msg += StringPrintf(", %d->%d", key, value); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6359 | } |
| 6360 | ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.", |
| 6361 | fallbackKeys.size(), msg.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6362 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6363 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6364 | |
| 6365 | if (fallback) { |
| 6366 | // Restart the dispatch cycle using the fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6367 | keyEntry.eventTime = event.getEventTime(); |
| 6368 | keyEntry.deviceId = event.getDeviceId(); |
| 6369 | keyEntry.source = event.getSource(); |
| 6370 | keyEntry.displayId = event.getDisplayId(); |
| 6371 | keyEntry.flags = event.getFlags() | AKEY_EVENT_FLAG_FALLBACK; |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6372 | keyEntry.keyCode = *fallbackKeyCode; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6373 | keyEntry.scanCode = event.getScanCode(); |
| 6374 | keyEntry.metaState = event.getMetaState(); |
| 6375 | keyEntry.repeatCount = event.getRepeatCount(); |
| 6376 | keyEntry.downTime = event.getDownTime(); |
| 6377 | keyEntry.syntheticRepeat = false; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6378 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6379 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6380 | ALOGD("Unhandled key event: Dispatching fallback key. " |
| 6381 | "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x", |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6382 | originalKeyCode, *fallbackKeyCode, keyEntry.metaState); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6383 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6384 | return true; // restart the event |
| 6385 | } else { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6386 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6387 | ALOGD("Unhandled key event: No fallback key."); |
| 6388 | } |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6389 | |
| 6390 | // Report the key as unhandled, since there is no fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6391 | mReporter->reportUnhandledKey(keyEntry.id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6392 | } |
| 6393 | } |
| 6394 | return false; |
| 6395 | } |
| 6396 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6397 | bool InputDispatcher::afterMotionEventLockedInterruptable( |
| 6398 | const std::shared_ptr<Connection>& connection, DispatchEntry* dispatchEntry, |
| 6399 | MotionEntry& motionEntry, bool handled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6400 | return false; |
| 6401 | } |
| 6402 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6403 | void InputDispatcher::traceInboundQueueLengthLocked() { |
| 6404 | if (ATRACE_ENABLED()) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 6405 | ATRACE_INT("iq", mInboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6406 | } |
| 6407 | } |
| 6408 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6409 | void InputDispatcher::traceOutboundQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6410 | if (ATRACE_ENABLED()) { |
| 6411 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6412 | snprintf(counterName, sizeof(counterName), "oq:%s", connection.getWindowName().c_str()); |
| 6413 | ATRACE_INT(counterName, connection.outboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6414 | } |
| 6415 | } |
| 6416 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6417 | void InputDispatcher::traceWaitQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6418 | if (ATRACE_ENABLED()) { |
| 6419 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6420 | snprintf(counterName, sizeof(counterName), "wq:%s", connection.getWindowName().c_str()); |
| 6421 | ATRACE_INT(counterName, connection.waitQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6422 | } |
| 6423 | } |
| 6424 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6425 | void InputDispatcher::dump(std::string& dump) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6426 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6427 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6428 | dump += "Input Dispatcher State:\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6429 | dumpDispatchStateLocked(dump); |
| 6430 | |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6431 | if (!mLastAnrState.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6432 | dump += "\nInput Dispatcher State at time of last ANR:\n"; |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6433 | dump += mLastAnrState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6434 | } |
| 6435 | } |
| 6436 | |
| 6437 | void InputDispatcher::monitor() { |
| 6438 | // 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] | 6439 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6440 | mLooper->wake(); |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6441 | mDispatcherIsAlive.wait(_l); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6442 | } |
| 6443 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6444 | /** |
| 6445 | * Wake up the dispatcher and wait until it processes all events and commands. |
| 6446 | * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so |
| 6447 | * this method can be safely called from any thread, as long as you've ensured that |
| 6448 | * the work you are interested in completing has already been queued. |
| 6449 | */ |
| 6450 | bool InputDispatcher::waitForIdle() { |
| 6451 | /** |
| 6452 | * Timeout should represent the longest possible time that a device might spend processing |
| 6453 | * events and commands. |
| 6454 | */ |
| 6455 | constexpr std::chrono::duration TIMEOUT = 100ms; |
| 6456 | std::unique_lock lock(mLock); |
| 6457 | mLooper->wake(); |
| 6458 | std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT); |
| 6459 | return result == std::cv_status::no_timeout; |
| 6460 | } |
| 6461 | |
Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 6462 | /** |
| 6463 | * Sets focus to the window identified by the token. This must be called |
| 6464 | * after updating any input window handles. |
| 6465 | * |
| 6466 | * Params: |
| 6467 | * request.token - input channel token used to identify the window that should gain focus. |
| 6468 | * request.focusedToken - the token that the caller expects currently to be focused. If the |
| 6469 | * specified token does not match the currently focused window, this request will be dropped. |
| 6470 | * If the specified focused token matches the currently focused window, the call will succeed. |
| 6471 | * Set this to "null" if this call should succeed no matter what the currently focused token is. |
| 6472 | * request.timestamp - SYSTEM_TIME_MONOTONIC timestamp in nanos set by the client (wm) |
| 6473 | * when requesting the focus change. This determines which request gets |
| 6474 | * precedence if there is a focus change request from another source such as pointer down. |
| 6475 | */ |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6476 | void InputDispatcher::setFocusedWindow(const FocusRequest& request) { |
| 6477 | { // acquire lock |
| 6478 | std::scoped_lock _l(mLock); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6479 | std::optional<FocusResolver::FocusChanges> changes = |
| 6480 | mFocusResolver.setFocusedWindow(request, getWindowHandlesLocked(request.displayId)); |
| 6481 | if (changes) { |
| 6482 | onFocusChangedLocked(*changes); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6483 | } |
| 6484 | } // release lock |
| 6485 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6486 | mLooper->wake(); |
| 6487 | } |
| 6488 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6489 | void InputDispatcher::onFocusChangedLocked(const FocusResolver::FocusChanges& changes) { |
| 6490 | if (changes.oldFocus) { |
| 6491 | std::shared_ptr<InputChannel> focusedInputChannel = getInputChannelLocked(changes.oldFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6492 | if (focusedInputChannel) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 6493 | CancelationOptions options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6494 | "focus left window"); |
| 6495 | synthesizeCancelationEventsForInputChannelLocked(focusedInputChannel, options); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6496 | enqueueFocusEventLocked(changes.oldFocus, /*hasFocus=*/false, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6497 | } |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6498 | } |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6499 | if (changes.newFocus) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6500 | enqueueFocusEventLocked(changes.newFocus, /*hasFocus=*/true, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6501 | } |
| 6502 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6503 | // If a window has pointer capture, then it must have focus. We need to ensure that this |
| 6504 | // contract is upheld when pointer capture is being disabled due to a loss of window focus. |
| 6505 | // If the window loses focus before it loses pointer capture, then the window can be in a state |
| 6506 | // where it has pointer capture but not focus, violating the contract. Therefore we must |
| 6507 | // dispatch the pointer capture event before the focus event. Since focus events are added to |
| 6508 | // the front of the queue (above), we add the pointer capture event to the front of the queue |
| 6509 | // after the focus events are added. This ensures the pointer capture event ends up at the |
| 6510 | // front. |
| 6511 | disablePointerCaptureForcedLocked(); |
| 6512 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6513 | if (mFocusedDisplayId == changes.displayId) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6514 | sendFocusChangedCommandLocked(changes.oldFocus, changes.newFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6515 | } |
| 6516 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6517 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6518 | void InputDispatcher::disablePointerCaptureForcedLocked() { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6519 | if (!mCurrentPointerCaptureRequest.enable && !mWindowTokenWithPointerCapture) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6520 | return; |
| 6521 | } |
| 6522 | |
| 6523 | ALOGD_IF(DEBUG_FOCUS, "Disabling Pointer Capture because the window lost focus."); |
| 6524 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6525 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6526 | setPointerCaptureLocked(false); |
| 6527 | } |
| 6528 | |
| 6529 | if (!mWindowTokenWithPointerCapture) { |
| 6530 | // No need to send capture changes because no window has capture. |
| 6531 | return; |
| 6532 | } |
| 6533 | |
| 6534 | if (mPendingEvent != nullptr) { |
| 6535 | // Move the pending event to the front of the queue. This will give the chance |
| 6536 | // for the pending event to be dropped if it is a captured event. |
| 6537 | mInboundQueue.push_front(mPendingEvent); |
| 6538 | mPendingEvent = nullptr; |
| 6539 | } |
| 6540 | |
| 6541 | auto entry = std::make_unique<PointerCaptureChangedEntry>(mIdGenerator.nextId(), now(), |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6542 | mCurrentPointerCaptureRequest); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6543 | mInboundQueue.push_front(std::move(entry)); |
| 6544 | } |
| 6545 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6546 | void InputDispatcher::setPointerCaptureLocked(bool enable) { |
| 6547 | mCurrentPointerCaptureRequest.enable = enable; |
| 6548 | mCurrentPointerCaptureRequest.seq++; |
| 6549 | auto command = [this, request = mCurrentPointerCaptureRequest]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6550 | scoped_unlock unlock(mLock); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6551 | mPolicy->setPointerCapture(request); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6552 | }; |
| 6553 | postCommandLocked(std::move(command)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6554 | } |
| 6555 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6556 | void InputDispatcher::displayRemoved(int32_t displayId) { |
| 6557 | { // acquire lock |
| 6558 | std::scoped_lock _l(mLock); |
| 6559 | // Set an empty list to remove all handles from the specific display. |
| 6560 | setInputWindowsLocked(/* window handles */ {}, displayId); |
| 6561 | setFocusedApplicationLocked(displayId, nullptr); |
| 6562 | // Call focus resolver to clean up stale requests. This must be called after input windows |
| 6563 | // have been removed for the removed display. |
| 6564 | mFocusResolver.displayRemoved(displayId); |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 6565 | // Reset pointer capture eligibility, regardless of previous state. |
| 6566 | std::erase(mIneligibleDisplaysForPointerCapture, displayId); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 6567 | // Remove the associated touch mode state. |
| 6568 | mTouchModePerDisplay.erase(displayId); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6569 | } // release lock |
| 6570 | |
| 6571 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6572 | mLooper->wake(); |
| 6573 | } |
| 6574 | |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6575 | void InputDispatcher::onWindowInfosChanged(const std::vector<WindowInfo>& windowInfos, |
| 6576 | const std::vector<DisplayInfo>& displayInfos) { |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6577 | // The listener sends the windows as a flattened array. Separate the windows by display for |
| 6578 | // more convenient parsing. |
| 6579 | std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>> handlesPerDisplay; |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6580 | for (const auto& info : windowInfos) { |
| 6581 | handlesPerDisplay.emplace(info.displayId, std::vector<sp<WindowInfoHandle>>()); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6582 | handlesPerDisplay[info.displayId].push_back(sp<WindowInfoHandle>::make(info)); |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6583 | } |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6584 | |
| 6585 | { // acquire lock |
| 6586 | std::scoped_lock _l(mLock); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 6587 | |
| 6588 | // Ensure that we have an entry created for all existing displays so that if a displayId has |
| 6589 | // no windows, we can tell that the windows were removed from the display. |
| 6590 | for (const auto& [displayId, _] : mWindowHandlesByDisplay) { |
| 6591 | handlesPerDisplay[displayId]; |
| 6592 | } |
| 6593 | |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6594 | mDisplayInfos.clear(); |
| 6595 | for (const auto& displayInfo : displayInfos) { |
| 6596 | mDisplayInfos.emplace(displayInfo.displayId, displayInfo); |
| 6597 | } |
| 6598 | |
| 6599 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 6600 | setInputWindowsLocked(handles, displayId); |
| 6601 | } |
| 6602 | } |
| 6603 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6604 | mLooper->wake(); |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6605 | } |
| 6606 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6607 | bool InputDispatcher::shouldDropInput( |
| 6608 | const EventEntry& entry, const sp<android::gui::WindowInfoHandle>& windowHandle) const { |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6609 | if (windowHandle->getInfo()->inputConfig.test(WindowInfo::InputConfig::DROP_INPUT) || |
| 6610 | (windowHandle->getInfo()->inputConfig.test( |
| 6611 | WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED) && |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6612 | isWindowObscuredLocked(windowHandle))) { |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6613 | ALOGW("Dropping %s event targeting %s as requested by the input configuration {%s} on " |
| 6614 | "display %" PRId32 ".", |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6615 | ftl::enum_string(entry.type).c_str(), windowHandle->getName().c_str(), |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6616 | windowHandle->getInfo()->inputConfig.string().c_str(), |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6617 | windowHandle->getInfo()->displayId); |
| 6618 | return true; |
| 6619 | } |
| 6620 | return false; |
| 6621 | } |
| 6622 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 6623 | void InputDispatcher::DispatcherWindowListener::onWindowInfosChanged( |
| 6624 | const std::vector<gui::WindowInfo>& windowInfos, |
| 6625 | const std::vector<DisplayInfo>& displayInfos) { |
| 6626 | mDispatcher.onWindowInfosChanged(windowInfos, displayInfos); |
| 6627 | } |
| 6628 | |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6629 | void InputDispatcher::cancelCurrentTouch() { |
| 6630 | { |
| 6631 | std::scoped_lock _l(mLock); |
| 6632 | ALOGD("Canceling all ongoing pointer gestures on all displays."); |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 6633 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6634 | "cancel current touch"); |
| 6635 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 6636 | |
| 6637 | mTouchStatesByDisplay.clear(); |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6638 | } |
| 6639 | // Wake up poll loop since there might be work to do. |
| 6640 | mLooper->wake(); |
| 6641 | } |
| 6642 | |
Prabir Pradhan | 87112a7 | 2023-04-20 19:13:39 +0000 | [diff] [blame] | 6643 | void InputDispatcher::requestRefreshConfiguration() { |
| 6644 | InputDispatcherConfiguration config; |
| 6645 | mPolicy->getDispatcherConfiguration(&config); |
| 6646 | |
| 6647 | std::scoped_lock _l(mLock); |
| 6648 | mConfig = config; |
| 6649 | } |
| 6650 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6651 | void InputDispatcher::setMonitorDispatchingTimeoutForTest(std::chrono::nanoseconds timeout) { |
| 6652 | std::scoped_lock _l(mLock); |
| 6653 | mMonitorDispatchingTimeout = timeout; |
| 6654 | } |
| 6655 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6656 | void InputDispatcher::slipWallpaperTouch(ftl::Flags<InputTarget::Flags> targetFlags, |
| 6657 | const sp<WindowInfoHandle>& oldWindowHandle, |
| 6658 | const sp<WindowInfoHandle>& newWindowHandle, |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 6659 | TouchState& state, int32_t pointerId, |
Siarhei Vishniakou | bf88052 | 2023-05-01 11:03:22 -0700 | [diff] [blame] | 6660 | std::vector<InputTarget>& targets) const { |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 6661 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
| 6662 | pointerIds.set(pointerId); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6663 | const bool oldHasWallpaper = oldWindowHandle->getInfo()->inputConfig.test( |
| 6664 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER); |
| 6665 | const bool newHasWallpaper = targetFlags.test(InputTarget::Flags::FOREGROUND) && |
| 6666 | newWindowHandle->getInfo()->inputConfig.test( |
| 6667 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER); |
| 6668 | const sp<WindowInfoHandle> oldWallpaper = |
| 6669 | oldHasWallpaper ? state.getWallpaperWindow() : nullptr; |
| 6670 | const sp<WindowInfoHandle> newWallpaper = |
| 6671 | newHasWallpaper ? findWallpaperWindowBelow(newWindowHandle) : nullptr; |
| 6672 | if (oldWallpaper == newWallpaper) { |
| 6673 | return; |
| 6674 | } |
| 6675 | |
| 6676 | if (oldWallpaper != nullptr) { |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 6677 | const TouchedWindow& oldTouchedWindow = state.getTouchedWindow(oldWallpaper); |
| 6678 | addWindowTargetLocked(oldWallpaper, |
| 6679 | oldTouchedWindow.targetFlags | |
| 6680 | InputTarget::Flags::DISPATCH_AS_SLIPPERY_EXIT, |
| 6681 | pointerIds, oldTouchedWindow.firstDownTimeInTarget, targets); |
| 6682 | state.removeTouchedPointerFromWindow(pointerId, oldWallpaper); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6683 | } |
| 6684 | |
| 6685 | if (newWallpaper != nullptr) { |
| 6686 | state.addOrUpdateWindow(newWallpaper, |
| 6687 | InputTarget::Flags::DISPATCH_AS_SLIPPERY_ENTER | |
| 6688 | InputTarget::Flags::WINDOW_IS_OBSCURED | |
| 6689 | InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED, |
| 6690 | pointerIds); |
| 6691 | } |
| 6692 | } |
| 6693 | |
| 6694 | void InputDispatcher::transferWallpaperTouch(ftl::Flags<InputTarget::Flags> oldTargetFlags, |
| 6695 | ftl::Flags<InputTarget::Flags> newTargetFlags, |
| 6696 | const sp<WindowInfoHandle> fromWindowHandle, |
| 6697 | const sp<WindowInfoHandle> toWindowHandle, |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 6698 | TouchState& state, |
| 6699 | std::bitset<MAX_POINTER_ID + 1> pointerIds) { |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6700 | const bool oldHasWallpaper = oldTargetFlags.test(InputTarget::Flags::FOREGROUND) && |
| 6701 | fromWindowHandle->getInfo()->inputConfig.test( |
| 6702 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER); |
| 6703 | const bool newHasWallpaper = newTargetFlags.test(InputTarget::Flags::FOREGROUND) && |
| 6704 | toWindowHandle->getInfo()->inputConfig.test( |
| 6705 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER); |
| 6706 | |
| 6707 | const sp<WindowInfoHandle> oldWallpaper = |
| 6708 | oldHasWallpaper ? state.getWallpaperWindow() : nullptr; |
| 6709 | const sp<WindowInfoHandle> newWallpaper = |
| 6710 | newHasWallpaper ? findWallpaperWindowBelow(toWindowHandle) : nullptr; |
| 6711 | if (oldWallpaper == newWallpaper) { |
| 6712 | return; |
| 6713 | } |
| 6714 | |
| 6715 | if (oldWallpaper != nullptr) { |
| 6716 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
| 6717 | "transferring touch focus to another window"); |
| 6718 | state.removeWindowByToken(oldWallpaper->getToken()); |
| 6719 | synthesizeCancelationEventsForWindowLocked(oldWallpaper, options); |
| 6720 | } |
| 6721 | |
| 6722 | if (newWallpaper != nullptr) { |
| 6723 | nsecs_t downTimeInTarget = now(); |
| 6724 | ftl::Flags<InputTarget::Flags> wallpaperFlags = |
| 6725 | oldTargetFlags & (InputTarget::Flags::SPLIT | InputTarget::Flags::DISPATCH_AS_IS); |
| 6726 | wallpaperFlags |= InputTarget::Flags::WINDOW_IS_OBSCURED | |
| 6727 | InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED; |
| 6728 | state.addOrUpdateWindow(newWallpaper, wallpaperFlags, pointerIds, downTimeInTarget); |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6729 | std::shared_ptr<Connection> wallpaperConnection = |
| 6730 | getConnectionLocked(newWallpaper->getToken()); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6731 | if (wallpaperConnection != nullptr) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6732 | std::shared_ptr<Connection> toConnection = |
| 6733 | getConnectionLocked(toWindowHandle->getToken()); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6734 | toConnection->inputState.mergePointerStateTo(wallpaperConnection->inputState); |
| 6735 | synthesizePointerDownEventsForConnectionLocked(downTimeInTarget, wallpaperConnection, |
| 6736 | wallpaperFlags); |
| 6737 | } |
| 6738 | } |
| 6739 | } |
| 6740 | |
| 6741 | sp<WindowInfoHandle> InputDispatcher::findWallpaperWindowBelow( |
| 6742 | const sp<WindowInfoHandle>& windowHandle) const { |
| 6743 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
| 6744 | getWindowHandlesLocked(windowHandle->getInfo()->displayId); |
| 6745 | bool foundWindow = false; |
| 6746 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
| 6747 | if (!foundWindow && otherHandle != windowHandle) { |
| 6748 | continue; |
| 6749 | } |
| 6750 | if (windowHandle == otherHandle) { |
| 6751 | foundWindow = true; |
| 6752 | continue; |
| 6753 | } |
| 6754 | |
| 6755 | if (otherHandle->getInfo()->inputConfig.test(WindowInfo::InputConfig::IS_WALLPAPER)) { |
| 6756 | return otherHandle; |
| 6757 | } |
| 6758 | } |
| 6759 | return nullptr; |
| 6760 | } |
| 6761 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 6762 | } // namespace android::inputdispatcher |