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 | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 29 | #include <log/log_event_list.h> |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 30 | #if defined(__ANDROID__) |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 31 | #include <gui/SurfaceComposerClient.h> |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 32 | #endif |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 33 | #include <input/InputDevice.h> |
Siarhei Vishniakou | 6e1e987 | 2022-11-08 17:51:35 -0800 | [diff] [blame] | 34 | #include <input/PrintTools.h> |
tyiu | 1573a67 | 2023-02-21 22:38:32 +0000 | [diff] [blame] | 35 | #include <openssl/mem.h> |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 36 | #include <powermanager/PowerManager.h> |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 37 | #include <unistd.h> |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 38 | #include <utils/Trace.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 39 | |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 40 | #include <cerrno> |
| 41 | #include <cinttypes> |
| 42 | #include <climits> |
| 43 | #include <cstddef> |
| 44 | #include <ctime> |
| 45 | #include <queue> |
| 46 | #include <sstream> |
| 47 | |
| 48 | #include "Connection.h" |
Arthur Hung | 1a1007b | 2022-05-11 07:15:01 +0000 | [diff] [blame] | 49 | #include "DebugConfig.h" |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 50 | #include "InputDispatcher.h" |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 51 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 52 | #define INDENT " " |
| 53 | #define INDENT2 " " |
| 54 | #define INDENT3 " " |
| 55 | #define INDENT4 " " |
| 56 | |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 57 | using namespace android::ftl::flag_operators; |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 58 | using android::base::Error; |
Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 59 | using android::base::HwTimeoutMultiplier; |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 60 | using android::base::Result; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 61 | using android::base::StringPrintf; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 62 | using android::gui::DisplayInfo; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 63 | using android::gui::FocusRequest; |
| 64 | using android::gui::TouchOcclusionMode; |
| 65 | using android::gui::WindowInfo; |
| 66 | using android::gui::WindowInfoHandle; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 67 | using android::os::InputEventInjectionResult; |
| 68 | using android::os::InputEventInjectionSync; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 69 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 70 | namespace android::inputdispatcher { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 71 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 72 | namespace { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 73 | // Temporarily releases a held mutex for the lifetime of the instance. |
| 74 | // Named to match std::scoped_lock |
| 75 | class scoped_unlock { |
| 76 | public: |
| 77 | explicit scoped_unlock(std::mutex& mutex) : mMutex(mutex) { mMutex.unlock(); } |
| 78 | ~scoped_unlock() { mMutex.lock(); } |
| 79 | |
| 80 | private: |
| 81 | std::mutex& mMutex; |
| 82 | }; |
| 83 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 84 | // Default input dispatching timeout if there is no focused application or paused window |
| 85 | // from which to determine an appropriate dispatching timeout. |
Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 86 | const std::chrono::duration DEFAULT_INPUT_DISPATCHING_TIMEOUT = std::chrono::milliseconds( |
| 87 | android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS * |
| 88 | HwTimeoutMultiplier()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 89 | |
| 90 | // Amount of time to allow for all pending events to be processed when an app switch |
| 91 | // key is on the way. This is used to preempt input dispatch and drop input events |
| 92 | // 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] | 93 | constexpr nsecs_t APP_SWITCH_TIMEOUT = 500 * 1000000LL; // 0.5sec |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 94 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 95 | const std::chrono::duration STALE_EVENT_TIMEOUT = std::chrono::seconds(10) * HwTimeoutMultiplier(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 96 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 97 | // 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] | 98 | constexpr nsecs_t SLOW_EVENT_PROCESSING_WARNING_TIMEOUT = 2000 * 1000000LL; // 2sec |
| 99 | |
| 100 | // Log a warning when an interception call takes longer than this to process. |
| 101 | constexpr std::chrono::milliseconds SLOW_INTERCEPTION_THRESHOLD = 50ms; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 102 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 103 | // Additional key latency in case a connection is still processing some motion events. |
| 104 | // This will help with the case when a user touched a button that opens a new window, |
| 105 | // and gives us the chance to dispatch the key to this new window. |
| 106 | constexpr std::chrono::nanoseconds KEY_WAITING_FOR_EVENTS_TIMEOUT = 500ms; |
| 107 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 108 | // Number of recent events to keep for debugging purposes. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 109 | constexpr size_t RECENT_QUEUE_MAX_SIZE = 10; |
| 110 | |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 111 | // Event log tags. See EventLogTags.logtags for reference. |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 112 | constexpr int LOGTAG_INPUT_INTERACTION = 62000; |
| 113 | constexpr int LOGTAG_INPUT_FOCUS = 62001; |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 114 | constexpr int LOGTAG_INPUT_CANCEL = 62003; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 115 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 116 | const ui::Transform kIdentityTransform; |
| 117 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 118 | inline nsecs_t now() { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 119 | return systemTime(SYSTEM_TIME_MONOTONIC); |
| 120 | } |
| 121 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 122 | inline const std::string binderToString(const sp<IBinder>& binder) { |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 123 | if (binder == nullptr) { |
| 124 | return "<null>"; |
| 125 | } |
| 126 | return StringPrintf("%p", binder.get()); |
| 127 | } |
| 128 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 129 | static std::string uidString(const gui::Uid& uid) { |
| 130 | return uid.toString(); |
| 131 | } |
| 132 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 133 | inline int32_t getMotionEventActionPointerIndex(int32_t action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 134 | return (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> |
| 135 | AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 136 | } |
| 137 | |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 138 | Result<void> checkKeyAction(int32_t action) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 139 | switch (action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 140 | case AKEY_EVENT_ACTION_DOWN: |
| 141 | case AKEY_EVENT_ACTION_UP: |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 142 | return {}; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 143 | default: |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 144 | return Error() << "Key event has invalid action code " << action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 148 | Result<void> validateKeyEvent(int32_t action) { |
| 149 | return checkKeyAction(action); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 150 | } |
| 151 | |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 152 | Result<void> checkMotionAction(int32_t action, int32_t actionButton, int32_t pointerCount) { |
Siarhei Vishniakou | 2f61bdc | 2022-12-02 08:55:51 -0800 | [diff] [blame] | 153 | switch (MotionEvent::getActionMasked(action)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 154 | case AMOTION_EVENT_ACTION_DOWN: |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 155 | case AMOTION_EVENT_ACTION_UP: { |
| 156 | if (pointerCount != 1) { |
| 157 | return Error() << "invalid pointer count " << pointerCount; |
| 158 | } |
| 159 | return {}; |
| 160 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 161 | case AMOTION_EVENT_ACTION_MOVE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 162 | case AMOTION_EVENT_ACTION_HOVER_ENTER: |
| 163 | case AMOTION_EVENT_ACTION_HOVER_MOVE: |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 164 | case AMOTION_EVENT_ACTION_HOVER_EXIT: { |
| 165 | if (pointerCount < 1) { |
| 166 | return Error() << "invalid pointer count " << pointerCount; |
| 167 | } |
| 168 | return {}; |
| 169 | } |
Siarhei Vishniakou | 2f61bdc | 2022-12-02 08:55:51 -0800 | [diff] [blame] | 170 | case AMOTION_EVENT_ACTION_CANCEL: |
| 171 | case AMOTION_EVENT_ACTION_OUTSIDE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 172 | case AMOTION_EVENT_ACTION_SCROLL: |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 173 | return {}; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 174 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 175 | case AMOTION_EVENT_ACTION_POINTER_UP: { |
Siarhei Vishniakou | 2f61bdc | 2022-12-02 08:55:51 -0800 | [diff] [blame] | 176 | const int32_t index = MotionEvent::getActionIndex(action); |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 177 | if (index < 0) { |
| 178 | return Error() << "invalid index " << index << " for " |
| 179 | << MotionEvent::actionToString(action); |
| 180 | } |
| 181 | if (index >= pointerCount) { |
| 182 | return Error() << "invalid index " << index << " for pointerCount " << pointerCount; |
| 183 | } |
| 184 | if (pointerCount <= 1) { |
| 185 | return Error() << "invalid pointer count " << pointerCount << " for " |
| 186 | << MotionEvent::actionToString(action); |
| 187 | } |
| 188 | return {}; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 189 | } |
| 190 | case AMOTION_EVENT_ACTION_BUTTON_PRESS: |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 191 | case AMOTION_EVENT_ACTION_BUTTON_RELEASE: { |
| 192 | if (actionButton == 0) { |
| 193 | return Error() << "action button should be nonzero for " |
| 194 | << MotionEvent::actionToString(action); |
| 195 | } |
| 196 | return {}; |
| 197 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 198 | default: |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 199 | return Error() << "invalid action " << action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 203 | int64_t millis(std::chrono::nanoseconds t) { |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 204 | return std::chrono::duration_cast<std::chrono::milliseconds>(t).count(); |
| 205 | } |
| 206 | |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 207 | Result<void> validateMotionEvent(int32_t action, int32_t actionButton, size_t pointerCount, |
| 208 | const PointerProperties* pointerProperties) { |
| 209 | Result<void> actionCheck = checkMotionAction(action, actionButton, pointerCount); |
| 210 | if (!actionCheck.ok()) { |
| 211 | return actionCheck; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 212 | } |
| 213 | if (pointerCount < 1 || pointerCount > MAX_POINTERS) { |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 214 | return Error() << "Motion event has invalid pointer count " << pointerCount |
| 215 | << "; value must be between 1 and " << MAX_POINTERS << "."; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 216 | } |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 217 | std::bitset<MAX_POINTER_ID + 1> pointerIdBits; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 218 | for (size_t i = 0; i < pointerCount; i++) { |
| 219 | int32_t id = pointerProperties[i].id; |
| 220 | if (id < 0 || id > MAX_POINTER_ID) { |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 221 | return Error() << "Motion event has invalid pointer id " << id |
| 222 | << "; value must be between 0 and " << MAX_POINTER_ID; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 223 | } |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 224 | if (pointerIdBits.test(id)) { |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 225 | return Error() << "Motion event has duplicate pointer id " << id; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 226 | } |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 227 | pointerIdBits.set(id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 228 | } |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 229 | return {}; |
| 230 | } |
| 231 | |
| 232 | Result<void> validateInputEvent(const InputEvent& event) { |
| 233 | switch (event.getType()) { |
| 234 | case InputEventType::KEY: { |
| 235 | const KeyEvent& key = static_cast<const KeyEvent&>(event); |
| 236 | const int32_t action = key.getAction(); |
| 237 | return validateKeyEvent(action); |
| 238 | } |
| 239 | case InputEventType::MOTION: { |
| 240 | const MotionEvent& motion = static_cast<const MotionEvent&>(event); |
| 241 | const int32_t action = motion.getAction(); |
| 242 | const size_t pointerCount = motion.getPointerCount(); |
| 243 | const PointerProperties* pointerProperties = motion.getPointerProperties(); |
| 244 | const int32_t actionButton = motion.getActionButton(); |
| 245 | return validateMotionEvent(action, actionButton, pointerCount, pointerProperties); |
| 246 | } |
| 247 | default: { |
| 248 | return {}; |
| 249 | } |
| 250 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 251 | } |
| 252 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 253 | std::string dumpRegion(const Region& region) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 254 | if (region.isEmpty()) { |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 255 | return "<empty>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 256 | } |
| 257 | |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 258 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 259 | bool first = true; |
| 260 | Region::const_iterator cur = region.begin(); |
| 261 | Region::const_iterator const tail = region.end(); |
| 262 | while (cur != tail) { |
| 263 | if (first) { |
| 264 | first = false; |
| 265 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 266 | dump += "|"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 267 | } |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 268 | 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] | 269 | cur++; |
| 270 | } |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 271 | return dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 272 | } |
| 273 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 274 | std::string dumpQueue(const std::deque<DispatchEntry*>& queue, nsecs_t currentTime) { |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 275 | constexpr size_t maxEntries = 50; // max events to print |
| 276 | constexpr size_t skipBegin = maxEntries / 2; |
| 277 | const size_t skipEnd = queue.size() - maxEntries / 2; |
| 278 | // skip from maxEntries / 2 ... size() - maxEntries/2 |
| 279 | // only print from 0 .. skipBegin and then from skipEnd .. size() |
| 280 | |
| 281 | std::string dump; |
| 282 | for (size_t i = 0; i < queue.size(); i++) { |
| 283 | const DispatchEntry& entry = *queue[i]; |
| 284 | if (i >= skipBegin && i < skipEnd) { |
| 285 | dump += StringPrintf(INDENT4 "<skipped %zu entries>\n", skipEnd - skipBegin); |
| 286 | i = skipEnd - 1; // it will be incremented to "skipEnd" by 'continue' |
| 287 | continue; |
| 288 | } |
| 289 | dump.append(INDENT4); |
| 290 | dump += entry.eventEntry->getDescription(); |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 291 | dump += StringPrintf(", seq=%" PRIu32 ", targetFlags=%s, resolvedAction=%d, age=%" PRId64 |
| 292 | "ms", |
| 293 | entry.seq, entry.targetFlags.string().c_str(), entry.resolvedAction, |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 294 | ns2ms(currentTime - entry.eventEntry->eventTime)); |
| 295 | if (entry.deliveryTime != 0) { |
| 296 | // This entry was delivered, so add information on how long we've been waiting |
| 297 | dump += StringPrintf(", wait=%" PRId64 "ms", ns2ms(currentTime - entry.deliveryTime)); |
| 298 | } |
| 299 | dump.append("\n"); |
| 300 | } |
| 301 | return dump; |
| 302 | } |
| 303 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 304 | /** |
| 305 | * Find the entry in std::unordered_map by key, and return it. |
| 306 | * If the entry is not found, return a default constructed entry. |
| 307 | * |
| 308 | * Useful when the entries are vectors, since an empty vector will be returned |
| 309 | * if the entry is not found. |
| 310 | * Also useful when the entries are sp<>. If an entry is not found, nullptr is returned. |
| 311 | */ |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 312 | template <typename K, typename V> |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 313 | V getValueByKey(const std::unordered_map<K, V>& map, K key) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 314 | auto it = map.find(key); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 315 | return it != map.end() ? it->second : V{}; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 316 | } |
| 317 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 318 | bool haveSameToken(const sp<WindowInfoHandle>& first, const sp<WindowInfoHandle>& second) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 319 | if (first == second) { |
| 320 | return true; |
| 321 | } |
| 322 | |
| 323 | if (first == nullptr || second == nullptr) { |
| 324 | return false; |
| 325 | } |
| 326 | |
| 327 | return first->getToken() == second->getToken(); |
| 328 | } |
| 329 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 330 | bool haveSameApplicationToken(const WindowInfo* first, const WindowInfo* second) { |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 331 | if (first == nullptr || second == nullptr) { |
| 332 | return false; |
| 333 | } |
| 334 | return first->applicationInfo.token != nullptr && |
| 335 | first->applicationInfo.token == second->applicationInfo.token; |
| 336 | } |
| 337 | |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 338 | template <typename T> |
| 339 | size_t firstMarkedBit(T set) { |
| 340 | // TODO: replace with std::countr_zero from <bit> when that's available |
| 341 | LOG_ALWAYS_FATAL_IF(set.none()); |
| 342 | size_t i = 0; |
| 343 | while (!set.test(i)) { |
| 344 | i++; |
| 345 | } |
| 346 | return i; |
| 347 | } |
| 348 | |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 349 | std::unique_ptr<DispatchEntry> createDispatchEntry( |
| 350 | const InputTarget& inputTarget, std::shared_ptr<EventEntry> eventEntry, |
| 351 | ftl::Flags<InputTarget::Flags> inputTargetFlags) { |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 352 | if (inputTarget.useDefaultPointerTransform()) { |
| 353 | const ui::Transform& transform = inputTarget.getDefaultPointerTransform(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 354 | return std::make_unique<DispatchEntry>(eventEntry, inputTargetFlags, transform, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 355 | inputTarget.displayTransform, |
| 356 | inputTarget.globalScaleFactor); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | ALOG_ASSERT(eventEntry->type == EventEntry::Type::MOTION); |
| 360 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); |
| 361 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 362 | std::vector<PointerCoords> pointerCoords; |
| 363 | pointerCoords.resize(motionEntry.pointerCount); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 364 | |
| 365 | // Use the first pointer information to normalize all other pointers. This could be any pointer |
| 366 | // 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] | 367 | // uses the transform for the normalized pointer. |
| 368 | const ui::Transform& firstPointerTransform = |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 369 | inputTarget.pointerTransforms[firstMarkedBit(inputTarget.pointerIds)]; |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 370 | ui::Transform inverseFirstTransform = firstPointerTransform.inverse(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 371 | |
| 372 | // Iterate through all pointers in the event to normalize against the first. |
| 373 | for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.pointerCount; pointerIndex++) { |
| 374 | const PointerProperties& pointerProperties = motionEntry.pointerProperties[pointerIndex]; |
| 375 | uint32_t pointerId = uint32_t(pointerProperties.id); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 376 | const ui::Transform& currTransform = inputTarget.pointerTransforms[pointerId]; |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 377 | |
| 378 | pointerCoords[pointerIndex].copyFrom(motionEntry.pointerCoords[pointerIndex]); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 379 | // First, apply the current pointer's transform to update the coordinates into |
| 380 | // window space. |
| 381 | pointerCoords[pointerIndex].transform(currTransform); |
| 382 | // Next, apply the inverse transform of the normalized coordinates so the |
| 383 | // current coordinates are transformed into the normalized coordinate space. |
| 384 | pointerCoords[pointerIndex].transform(inverseFirstTransform); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 387 | std::unique_ptr<MotionEntry> combinedMotionEntry = |
| 388 | std::make_unique<MotionEntry>(motionEntry.id, motionEntry.eventTime, |
| 389 | motionEntry.deviceId, motionEntry.source, |
| 390 | motionEntry.displayId, motionEntry.policyFlags, |
| 391 | motionEntry.action, motionEntry.actionButton, |
| 392 | motionEntry.flags, motionEntry.metaState, |
| 393 | motionEntry.buttonState, motionEntry.classification, |
| 394 | motionEntry.edgeFlags, motionEntry.xPrecision, |
| 395 | motionEntry.yPrecision, motionEntry.xCursorPosition, |
| 396 | motionEntry.yCursorPosition, motionEntry.downTime, |
| 397 | motionEntry.pointerCount, motionEntry.pointerProperties, |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 398 | pointerCoords.data()); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 399 | |
| 400 | if (motionEntry.injectionState) { |
| 401 | combinedMotionEntry->injectionState = motionEntry.injectionState; |
| 402 | combinedMotionEntry->injectionState->refCount += 1; |
| 403 | } |
| 404 | |
| 405 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 406 | std::make_unique<DispatchEntry>(std::move(combinedMotionEntry), inputTargetFlags, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 407 | firstPointerTransform, inputTarget.displayTransform, |
| 408 | inputTarget.globalScaleFactor); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 409 | return dispatchEntry; |
| 410 | } |
| 411 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 412 | status_t openInputChannelPair(const std::string& name, std::shared_ptr<InputChannel>& serverChannel, |
| 413 | std::unique_ptr<InputChannel>& clientChannel) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 414 | std::unique_ptr<InputChannel> uniqueServerChannel; |
| 415 | status_t result = InputChannel::openInputChannelPair(name, uniqueServerChannel, clientChannel); |
| 416 | |
| 417 | serverChannel = std::move(uniqueServerChannel); |
| 418 | return result; |
| 419 | } |
| 420 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 421 | template <typename T> |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 422 | 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] | 423 | if (lhs == nullptr && rhs == nullptr) { |
| 424 | return true; |
| 425 | } |
| 426 | if (lhs == nullptr || rhs == nullptr) { |
| 427 | return false; |
| 428 | } |
| 429 | return *lhs == *rhs; |
| 430 | } |
| 431 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 432 | KeyEvent createKeyEvent(const KeyEntry& entry) { |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 433 | KeyEvent event; |
| 434 | event.initialize(entry.id, entry.deviceId, entry.source, entry.displayId, INVALID_HMAC, |
| 435 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, |
| 436 | entry.repeatCount, entry.downTime, entry.eventTime); |
| 437 | return event; |
| 438 | } |
| 439 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 440 | bool shouldReportMetricsForConnection(const Connection& connection) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 441 | // Do not keep track of gesture monitors. They receive every event and would disproportionately |
| 442 | // affect the statistics. |
| 443 | if (connection.monitor) { |
| 444 | return false; |
| 445 | } |
| 446 | // If the connection is experiencing ANR, let's skip it. We have separate ANR metrics |
| 447 | if (!connection.responsive) { |
| 448 | return false; |
| 449 | } |
| 450 | return true; |
| 451 | } |
| 452 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 453 | bool shouldReportFinishedEvent(const DispatchEntry& dispatchEntry, const Connection& connection) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 454 | const EventEntry& eventEntry = *dispatchEntry.eventEntry; |
| 455 | const int32_t& inputEventId = eventEntry.id; |
| 456 | if (inputEventId != dispatchEntry.resolvedEventId) { |
| 457 | // Event was transmuted |
| 458 | return false; |
| 459 | } |
| 460 | if (inputEventId == android::os::IInputConstants::INVALID_INPUT_EVENT_ID) { |
| 461 | return false; |
| 462 | } |
| 463 | // Only track latency for events that originated from hardware |
| 464 | if (eventEntry.isSynthesized()) { |
| 465 | return false; |
| 466 | } |
| 467 | const EventEntry::Type& inputEventEntryType = eventEntry.type; |
| 468 | if (inputEventEntryType == EventEntry::Type::KEY) { |
| 469 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 470 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
| 471 | return false; |
| 472 | } |
| 473 | } else if (inputEventEntryType == EventEntry::Type::MOTION) { |
| 474 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 475 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL || |
| 476 | motionEntry.action == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 477 | return false; |
| 478 | } |
| 479 | } else { |
| 480 | // Not a key or a motion |
| 481 | return false; |
| 482 | } |
| 483 | if (!shouldReportMetricsForConnection(connection)) { |
| 484 | return false; |
| 485 | } |
| 486 | return true; |
| 487 | } |
| 488 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 489 | /** |
| 490 | * Connection is responsive if it has no events in the waitQueue that are older than the |
| 491 | * current time. |
| 492 | */ |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 493 | bool isConnectionResponsive(const Connection& connection) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 494 | const nsecs_t currentTime = now(); |
| 495 | for (const DispatchEntry* entry : connection.waitQueue) { |
| 496 | if (entry->timeoutTime < currentTime) { |
| 497 | return false; |
| 498 | } |
| 499 | } |
| 500 | return true; |
| 501 | } |
| 502 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 503 | // Returns true if the event type passed as argument represents a user activity. |
| 504 | bool isUserActivityEvent(const EventEntry& eventEntry) { |
| 505 | switch (eventEntry.type) { |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 506 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 507 | case EventEntry::Type::DEVICE_RESET: |
| 508 | case EventEntry::Type::DRAG: |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 509 | case EventEntry::Type::FOCUS: |
| 510 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 511 | case EventEntry::Type::SENSOR: |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 512 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 513 | return false; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 514 | case EventEntry::Type::KEY: |
| 515 | case EventEntry::Type::MOTION: |
| 516 | return true; |
| 517 | } |
| 518 | } |
| 519 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 520 | // 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] | 521 | bool windowAcceptsTouchAt(const WindowInfo& windowInfo, int32_t displayId, float x, float y, |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 522 | bool isStylus, const ui::Transform& displayTransform) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 523 | const auto inputConfig = windowInfo.inputConfig; |
| 524 | if (windowInfo.displayId != displayId || |
| 525 | inputConfig.test(WindowInfo::InputConfig::NOT_VISIBLE)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 526 | return false; |
| 527 | } |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 528 | const bool windowCanInterceptTouch = isStylus && windowInfo.interceptsStylus(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 529 | if (inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE) && !windowCanInterceptTouch) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 530 | return false; |
| 531 | } |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 532 | |
| 533 | // Window Manager works in the logical display coordinate space. When it specifies bounds for a |
| 534 | // window as (l, t, r, b), the range of x in [l, r) and y in [t, b) are considered to be inside |
| 535 | // the window. Points on the right and bottom edges should not be inside the window, so we need |
| 536 | // to be careful about performing a hit test when the display is rotated, since the "right" and |
| 537 | // "bottom" of the window will be different in the display (un-rotated) space compared to in the |
| 538 | // logical display in which WM determined the bounds. Perform the hit test in the logical |
| 539 | // display space to ensure these edges are considered correctly in all orientations. |
| 540 | const auto touchableRegion = displayTransform.transform(windowInfo.touchableRegion); |
| 541 | const auto p = displayTransform.transform(x, y); |
| 542 | if (!touchableRegion.contains(std::floor(p.x), std::floor(p.y))) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 543 | return false; |
| 544 | } |
| 545 | return true; |
| 546 | } |
| 547 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 548 | bool isPointerFromStylus(const MotionEntry& entry, int32_t pointerIndex) { |
| 549 | return isFromSource(entry.source, AINPUT_SOURCE_STYLUS) && |
Prabir Pradhan | e562696 | 2022-10-27 20:30:53 +0000 | [diff] [blame] | 550 | isStylusToolType(entry.pointerProperties[pointerIndex].toolType); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 551 | } |
| 552 | |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 553 | // Determines if the given window can be targeted as InputTarget::Flags::FOREGROUND. |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 554 | // Foreground events are only sent to "foreground targetable" windows, but not all gestures sent to |
| 555 | // such window are necessarily targeted with the flag. For example, an event with ACTION_OUTSIDE can |
| 556 | // 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] | 557 | // InputTarget::Flags::FOREGROUND. |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 558 | bool canReceiveForegroundTouches(const WindowInfo& info) { |
| 559 | // A non-touchable window can still receive touch events (e.g. in the case of |
| 560 | // STYLUS_INTERCEPTOR), so prevent such windows from receiving foreground events for touches. |
| 561 | return !info.inputConfig.test(gui::WindowInfo::InputConfig::NOT_TOUCHABLE) && !info.isSpy(); |
| 562 | } |
| 563 | |
Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 564 | bool isWindowOwnedBy(const sp<WindowInfoHandle>& windowHandle, gui::Pid pid, gui::Uid uid) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 565 | if (windowHandle == nullptr) { |
| 566 | return false; |
| 567 | } |
| 568 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 569 | if (pid == windowInfo->ownerPid && uid == windowInfo->ownerUid) { |
| 570 | return true; |
| 571 | } |
| 572 | return false; |
| 573 | } |
| 574 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 575 | // Checks targeted injection using the window's owner's uid. |
| 576 | // Returns an empty string if an entry can be sent to the given window, or an error message if the |
| 577 | // entry is a targeted injection whose uid target doesn't match the window owner. |
| 578 | std::optional<std::string> verifyTargetedInjection(const sp<WindowInfoHandle>& window, |
| 579 | const EventEntry& entry) { |
| 580 | if (entry.injectionState == nullptr || !entry.injectionState->targetUid) { |
| 581 | // The event was not injected, or the injected event does not target a window. |
| 582 | return {}; |
| 583 | } |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 584 | const auto uid = *entry.injectionState->targetUid; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 585 | if (window == nullptr) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 586 | return StringPrintf("No valid window target for injection into uid %s.", |
| 587 | uid.toString().c_str()); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 588 | } |
| 589 | if (entry.injectionState->targetUid != window->getInfo()->ownerUid) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 590 | return StringPrintf("Injected event targeted at uid %s would be dispatched to window '%s' " |
| 591 | "owned by uid %s.", |
| 592 | uid.toString().c_str(), window->getName().c_str(), |
| 593 | window->getInfo()->ownerUid.toString().c_str()); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 594 | } |
| 595 | return {}; |
| 596 | } |
| 597 | |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 598 | std::pair<float, float> resolveTouchedPosition(const MotionEntry& entry) { |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 599 | const bool isFromMouse = isFromSource(entry.source, AINPUT_SOURCE_MOUSE); |
| 600 | // Always dispatch mouse events to cursor position. |
| 601 | if (isFromMouse) { |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 602 | return {entry.xCursorPosition, entry.yCursorPosition}; |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | const int32_t pointerIndex = getMotionEventActionPointerIndex(entry.action); |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 606 | return {entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 607 | entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_Y)}; |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 608 | } |
| 609 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 610 | std::optional<nsecs_t> getDownTime(const EventEntry& eventEntry) { |
| 611 | if (eventEntry.type == EventEntry::Type::KEY) { |
| 612 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 613 | return keyEntry.downTime; |
| 614 | } else if (eventEntry.type == EventEntry::Type::MOTION) { |
| 615 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 616 | return motionEntry.downTime; |
| 617 | } |
| 618 | return std::nullopt; |
| 619 | } |
| 620 | |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 621 | /** |
| 622 | * Compare the old touch state to the new touch state, and generate the corresponding touched |
| 623 | * windows (== input targets). |
| 624 | * If a window had the hovering pointer, but now it doesn't, produce HOVER_EXIT for that window. |
| 625 | * If the pointer just entered the new window, produce HOVER_ENTER. |
| 626 | * For pointers remaining in the window, produce HOVER_MOVE. |
| 627 | */ |
| 628 | std::vector<TouchedWindow> getHoveringWindowsLocked(const TouchState* oldState, |
| 629 | const TouchState& newTouchState, |
| 630 | const MotionEntry& entry) { |
| 631 | std::vector<TouchedWindow> out; |
| 632 | const int32_t maskedAction = MotionEvent::getActionMasked(entry.action); |
| 633 | if (maskedAction != AMOTION_EVENT_ACTION_HOVER_ENTER && |
| 634 | maskedAction != AMOTION_EVENT_ACTION_HOVER_MOVE && |
| 635 | maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 636 | // Not a hover event - don't need to do anything |
| 637 | return out; |
| 638 | } |
| 639 | |
| 640 | // We should consider all hovering pointers here. But for now, just use the first one |
| 641 | const int32_t pointerId = entry.pointerProperties[0].id; |
| 642 | |
| 643 | std::set<sp<WindowInfoHandle>> oldWindows; |
| 644 | if (oldState != nullptr) { |
| 645 | oldWindows = oldState->getWindowsWithHoveringPointer(entry.deviceId, pointerId); |
| 646 | } |
| 647 | |
| 648 | std::set<sp<WindowInfoHandle>> newWindows = |
| 649 | newTouchState.getWindowsWithHoveringPointer(entry.deviceId, pointerId); |
| 650 | |
| 651 | // If the pointer is no longer in the new window set, send HOVER_EXIT. |
| 652 | for (const sp<WindowInfoHandle>& oldWindow : oldWindows) { |
| 653 | if (newWindows.find(oldWindow) == newWindows.end()) { |
| 654 | TouchedWindow touchedWindow; |
| 655 | touchedWindow.windowHandle = oldWindow; |
| 656 | touchedWindow.targetFlags = InputTarget::Flags::DISPATCH_AS_HOVER_EXIT; |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 657 | out.push_back(touchedWindow); |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | for (const sp<WindowInfoHandle>& newWindow : newWindows) { |
| 662 | TouchedWindow touchedWindow; |
| 663 | touchedWindow.windowHandle = newWindow; |
| 664 | if (oldWindows.find(newWindow) == oldWindows.end()) { |
| 665 | // Any windows that have this pointer now, and didn't have it before, should get |
| 666 | // HOVER_ENTER |
| 667 | touchedWindow.targetFlags = InputTarget::Flags::DISPATCH_AS_HOVER_ENTER; |
| 668 | } else { |
| 669 | // This pointer was already sent to the window. Use ACTION_HOVER_MOVE. |
Siarhei Vishniakou | c2eb850 | 2023-04-11 18:33:36 -0700 | [diff] [blame] | 670 | if (CC_UNLIKELY(maskedAction != AMOTION_EVENT_ACTION_HOVER_MOVE)) { |
Daniel Norman | 2f99cdb | 2023-08-02 16:39:45 -0700 | [diff] [blame] | 671 | android::base::LogSeverity severity = android::base::LogSeverity::FATAL; |
| 672 | if (entry.flags & AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT) { |
| 673 | // The Accessibility injected touch exploration event stream |
| 674 | // has known inconsistencies, so log ERROR instead of |
| 675 | // crashing the device with FATAL. |
| 676 | // TODO(b/286037469): Move a11y severity back to FATAL. |
| 677 | severity = android::base::LogSeverity::ERROR; |
| 678 | } |
| 679 | LOG(severity) << "Expected ACTION_HOVER_MOVE instead of " << entry.getDescription(); |
Siarhei Vishniakou | c2eb850 | 2023-04-11 18:33:36 -0700 | [diff] [blame] | 680 | } |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 681 | touchedWindow.targetFlags = InputTarget::Flags::DISPATCH_AS_IS; |
| 682 | } |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 683 | touchedWindow.addHoveringPointer(entry.deviceId, pointerId); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 684 | if (canReceiveForegroundTouches(*newWindow->getInfo())) { |
| 685 | touchedWindow.targetFlags |= InputTarget::Flags::FOREGROUND; |
| 686 | } |
| 687 | out.push_back(touchedWindow); |
| 688 | } |
| 689 | return out; |
| 690 | } |
| 691 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 692 | template <typename T> |
| 693 | std::vector<T>& operator+=(std::vector<T>& left, const std::vector<T>& right) { |
| 694 | left.insert(left.end(), right.begin(), right.end()); |
| 695 | return left; |
| 696 | } |
| 697 | |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 698 | // Filter windows in a TouchState and targets in a vector to remove untrusted windows/targets from |
| 699 | // both. |
| 700 | void filterUntrustedTargets(TouchState& touchState, std::vector<InputTarget>& targets) { |
| 701 | std::erase_if(touchState.windows, [&](const TouchedWindow& window) { |
| 702 | if (!window.windowHandle->getInfo()->inputConfig.test( |
| 703 | WindowInfo::InputConfig::TRUSTED_OVERLAY)) { |
| 704 | // In addition to TouchState, erase this window from the input targets! We don't have a |
| 705 | // good way to do this today except by adding a nested loop. |
| 706 | // TODO(b/282025641): simplify this code once InputTargets are being identified |
| 707 | // separately from TouchedWindows. |
| 708 | std::erase_if(targets, [&](const InputTarget& target) { |
| 709 | return target.inputChannel->getConnectionToken() == window.windowHandle->getToken(); |
| 710 | }); |
| 711 | return true; |
| 712 | } |
| 713 | return false; |
| 714 | }); |
| 715 | } |
| 716 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 717 | } // namespace |
| 718 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 719 | // --- InputDispatcher --- |
| 720 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 721 | InputDispatcher::InputDispatcher(InputDispatcherPolicyInterface& policy) |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 722 | : InputDispatcher(policy, STALE_EVENT_TIMEOUT) {} |
| 723 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 724 | InputDispatcher::InputDispatcher(InputDispatcherPolicyInterface& policy, |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 725 | std::chrono::nanoseconds staleEventTimeout) |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 726 | : mPolicy(policy), |
| 727 | mPendingEvent(nullptr), |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 728 | mLastDropReason(DropReason::NOT_DROPPED), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 729 | mIdGenerator(IdGenerator::Source::INPUT_DISPATCHER), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 730 | mAppSwitchSawKeyDown(false), |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 731 | mAppSwitchDueTime(LLONG_MAX), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 732 | mNextUnblockedEvent(nullptr), |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 733 | mMonitorDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 734 | mDispatchEnabled(false), |
| 735 | mDispatchFrozen(false), |
| 736 | mInputFilterEnabled(false), |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 737 | mMaximumObscuringOpacityForTouch(1.0f), |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 738 | mFocusedDisplayId(ADISPLAY_ID_DEFAULT), |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 739 | mWindowTokenWithPointerCapture(nullptr), |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 740 | mStaleEventTimeout(staleEventTimeout), |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 741 | mLatencyAggregator(), |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 742 | mLatencyTracker(&mLatencyAggregator) { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 743 | mLooper = sp<Looper>::make(false); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 744 | mReporter = createInputReporter(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 745 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 746 | mWindowInfoListener = sp<DispatcherWindowListener>::make(*this); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 747 | #if defined(__ANDROID__) |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 748 | SurfaceComposerClient::getDefault()->addWindowInfosListener(mWindowInfoListener); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 749 | #endif |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 750 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | InputDispatcher::~InputDispatcher() { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 754 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 755 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 756 | resetKeyRepeatLocked(); |
| 757 | releasePendingEventLocked(); |
| 758 | drainInboundQueueLocked(); |
| 759 | mCommandQueue.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 760 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 761 | while (!mConnectionsByToken.empty()) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 762 | std::shared_ptr<Connection> connection = mConnectionsByToken.begin()->second; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 763 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), /*notify=*/false); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 764 | } |
| 765 | } |
| 766 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 767 | status_t InputDispatcher::start() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 768 | if (mThread) { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 769 | return ALREADY_EXISTS; |
| 770 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 771 | mThread = std::make_unique<InputThread>( |
| 772 | "InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); }); |
| 773 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | status_t InputDispatcher::stop() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 777 | if (mThread && mThread->isCallingThread()) { |
| 778 | ALOGE("InputDispatcher cannot be stopped from its own thread!"); |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 779 | return INVALID_OPERATION; |
| 780 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 781 | mThread.reset(); |
| 782 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 783 | } |
| 784 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 785 | void InputDispatcher::dispatchOnce() { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 786 | nsecs_t nextWakeupTime = LLONG_MAX; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 787 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 788 | std::scoped_lock _l(mLock); |
| 789 | mDispatcherIsAlive.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 790 | |
| 791 | // Run a dispatch loop if there are no pending commands. |
| 792 | // The dispatch loop might enqueue commands to run afterwards. |
| 793 | if (!haveCommandsLocked()) { |
| 794 | dispatchOnceInnerLocked(&nextWakeupTime); |
| 795 | } |
| 796 | |
| 797 | // Run all pending commands if there are any. |
| 798 | // 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] | 799 | if (runCommandsLockedInterruptable()) { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 800 | nextWakeupTime = LLONG_MIN; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 801 | } |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 802 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 803 | // If we are still waiting for ack on some events, |
| 804 | // we might have to wake up earlier to check if an app is anr'ing. |
| 805 | const nsecs_t nextAnrCheck = processAnrsLocked(); |
| 806 | nextWakeupTime = std::min(nextWakeupTime, nextAnrCheck); |
| 807 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 808 | // We are about to enter an infinitely long sleep, because we have no commands or |
| 809 | // pending or queued events |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 810 | if (nextWakeupTime == LLONG_MAX) { |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 811 | mDispatcherEnteredIdle.notify_all(); |
| 812 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 813 | } // release lock |
| 814 | |
| 815 | // Wait for callback or timeout or wake. (make sure we round up, not down) |
| 816 | nsecs_t currentTime = now(); |
| 817 | int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime); |
| 818 | mLooper->pollOnce(timeoutMillis); |
| 819 | } |
| 820 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 821 | /** |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 822 | * Raise ANR if there is no focused window. |
| 823 | * Before the ANR is raised, do a final state check: |
| 824 | * 1. The currently focused application must be the same one we are waiting for. |
| 825 | * 2. Ensure we still don't have a focused window. |
| 826 | */ |
| 827 | void InputDispatcher::processNoFocusedWindowAnrLocked() { |
| 828 | // Check if the application that we are waiting for is still focused. |
| 829 | std::shared_ptr<InputApplicationHandle> focusedApplication = |
| 830 | getValueByKey(mFocusedApplicationHandlesByDisplay, mAwaitedApplicationDisplayId); |
| 831 | if (focusedApplication == nullptr || |
| 832 | focusedApplication->getApplicationToken() != |
| 833 | mAwaitedFocusedApplication->getApplicationToken()) { |
| 834 | // Unexpected because we should have reset the ANR timer when focused application changed |
| 835 | ALOGE("Waited for a focused window, but focused application has already changed to %s", |
| 836 | focusedApplication->getName().c_str()); |
| 837 | return; // The focused application has changed. |
| 838 | } |
| 839 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 840 | const sp<WindowInfoHandle>& focusedWindowHandle = |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 841 | getFocusedWindowHandleLocked(mAwaitedApplicationDisplayId); |
| 842 | if (focusedWindowHandle != nullptr) { |
| 843 | return; // We now have a focused window. No need for ANR. |
| 844 | } |
| 845 | onAnrLocked(mAwaitedFocusedApplication); |
| 846 | } |
| 847 | |
| 848 | /** |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 849 | * Check if any of the connections' wait queues have events that are too old. |
| 850 | * If we waited for events to be ack'ed for more than the window timeout, raise an ANR. |
| 851 | * Return the time at which we should wake up next. |
| 852 | */ |
| 853 | nsecs_t InputDispatcher::processAnrsLocked() { |
| 854 | const nsecs_t currentTime = now(); |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 855 | nsecs_t nextAnrCheck = LLONG_MAX; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 856 | // Check if we are waiting for a focused window to appear. Raise ANR if waited too long |
| 857 | if (mNoFocusedWindowTimeoutTime.has_value() && mAwaitedFocusedApplication != nullptr) { |
| 858 | if (currentTime >= *mNoFocusedWindowTimeoutTime) { |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 859 | processNoFocusedWindowAnrLocked(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 860 | mAwaitedFocusedApplication.reset(); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 861 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 862 | return LLONG_MIN; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 863 | } else { |
Siarhei Vishniakou | 38a6d27 | 2020-10-20 20:29:33 -0500 | [diff] [blame] | 864 | // Keep waiting. We will drop the event when mNoFocusedWindowTimeoutTime comes. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 865 | nextAnrCheck = *mNoFocusedWindowTimeoutTime; |
| 866 | } |
| 867 | } |
| 868 | |
| 869 | // Check if any connection ANRs are due |
| 870 | nextAnrCheck = std::min(nextAnrCheck, mAnrTracker.firstTimeout()); |
| 871 | if (currentTime < nextAnrCheck) { // most likely scenario |
| 872 | return nextAnrCheck; // everything is normal. Let's check again at nextAnrCheck |
| 873 | } |
| 874 | |
| 875 | // If we reached here, we have an unresponsive connection. |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 876 | std::shared_ptr<Connection> connection = getConnectionLocked(mAnrTracker.firstToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 877 | if (connection == nullptr) { |
| 878 | ALOGE("Could not find connection for entry %" PRId64, mAnrTracker.firstTimeout()); |
| 879 | return nextAnrCheck; |
| 880 | } |
| 881 | connection->responsive = false; |
| 882 | // Stop waking up for this unresponsive connection |
| 883 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 884 | onAnrLocked(connection); |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 885 | return LLONG_MIN; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 886 | } |
| 887 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 888 | std::chrono::nanoseconds InputDispatcher::getDispatchingTimeoutLocked( |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 889 | const std::shared_ptr<Connection>& connection) { |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 890 | if (connection->monitor) { |
| 891 | return mMonitorDispatchingTimeout; |
| 892 | } |
| 893 | const sp<WindowInfoHandle> window = |
| 894 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 895 | if (window != nullptr) { |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 896 | return window->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 897 | } |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 898 | return DEFAULT_INPUT_DISPATCHING_TIMEOUT; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 899 | } |
| 900 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 901 | void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) { |
| 902 | nsecs_t currentTime = now(); |
| 903 | |
Jeff Brown | dc5992e | 2014-04-11 01:27:26 -0700 | [diff] [blame] | 904 | // Reset the key repeat timer whenever normal dispatch is suspended while the |
| 905 | // device is in a non-interactive state. This is to ensure that we abort a key |
| 906 | // repeat if the device is just coming out of sleep. |
| 907 | if (!mDispatchEnabled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 908 | resetKeyRepeatLocked(); |
| 909 | } |
| 910 | |
| 911 | // If dispatching is frozen, do not process timeouts or try to deliver any new events. |
| 912 | if (mDispatchFrozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 913 | if (DEBUG_FOCUS) { |
| 914 | ALOGD("Dispatch frozen. Waiting some more."); |
| 915 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 916 | return; |
| 917 | } |
| 918 | |
| 919 | // Optimize latency of app switches. |
| 920 | // Essentially we start a short timeout when an app switch key (HOME / ENDCALL) has |
| 921 | // been pressed. When it expires, we preempt dispatch and drop all other pending events. |
| 922 | bool isAppSwitchDue = mAppSwitchDueTime <= currentTime; |
| 923 | if (mAppSwitchDueTime < *nextWakeupTime) { |
| 924 | *nextWakeupTime = mAppSwitchDueTime; |
| 925 | } |
| 926 | |
| 927 | // Ready to start a new event. |
| 928 | // If we don't already have a pending event, go grab one. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 929 | if (!mPendingEvent) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 930 | if (mInboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 931 | if (isAppSwitchDue) { |
| 932 | // The inbound queue is empty so the app switch key we were waiting |
| 933 | // for will never arrive. Stop waiting for it. |
| 934 | resetPendingAppSwitchLocked(false); |
| 935 | isAppSwitchDue = false; |
| 936 | } |
| 937 | |
| 938 | // Synthesize a key repeat if appropriate. |
| 939 | if (mKeyRepeatState.lastKeyEntry) { |
| 940 | if (currentTime >= mKeyRepeatState.nextRepeatTime) { |
| 941 | mPendingEvent = synthesizeKeyRepeatLocked(currentTime); |
| 942 | } else { |
| 943 | if (mKeyRepeatState.nextRepeatTime < *nextWakeupTime) { |
| 944 | *nextWakeupTime = mKeyRepeatState.nextRepeatTime; |
| 945 | } |
| 946 | } |
| 947 | } |
| 948 | |
| 949 | // Nothing to do if there is no pending event. |
| 950 | if (!mPendingEvent) { |
| 951 | return; |
| 952 | } |
| 953 | } else { |
| 954 | // Inbound queue has at least one entry. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 955 | mPendingEvent = mInboundQueue.front(); |
| 956 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 957 | traceInboundQueueLengthLocked(); |
| 958 | } |
| 959 | |
| 960 | // Poke user activity for this event. |
| 961 | if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 962 | pokeUserActivityLocked(*mPendingEvent); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 963 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | // Now we have an event to dispatch. |
| 967 | // 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] | 968 | ALOG_ASSERT(mPendingEvent != nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 969 | bool done = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 970 | DropReason dropReason = DropReason::NOT_DROPPED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 971 | if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 972 | dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 973 | } else if (!mDispatchEnabled) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 974 | dropReason = DropReason::DISABLED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | if (mNextUnblockedEvent == mPendingEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 978 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | switch (mPendingEvent->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 982 | case EventEntry::Type::CONFIGURATION_CHANGED: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 983 | const ConfigurationChangedEntry& typedEntry = |
| 984 | static_cast<const ConfigurationChangedEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 985 | done = dispatchConfigurationChangedLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 986 | dropReason = DropReason::NOT_DROPPED; // configuration changes are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 987 | break; |
| 988 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 989 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 990 | case EventEntry::Type::DEVICE_RESET: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 991 | const DeviceResetEntry& typedEntry = |
| 992 | static_cast<const DeviceResetEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 993 | done = dispatchDeviceResetLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 994 | dropReason = DropReason::NOT_DROPPED; // device resets are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 995 | break; |
| 996 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 997 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 998 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 999 | std::shared_ptr<FocusEntry> typedEntry = |
| 1000 | std::static_pointer_cast<FocusEntry>(mPendingEvent); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1001 | dispatchFocusLocked(currentTime, typedEntry); |
| 1002 | done = true; |
| 1003 | dropReason = DropReason::NOT_DROPPED; // focus events are never dropped |
| 1004 | break; |
| 1005 | } |
| 1006 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1007 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 1008 | const auto typedEntry = std::static_pointer_cast<TouchModeEntry>(mPendingEvent); |
| 1009 | dispatchTouchModeChangeLocked(currentTime, typedEntry); |
| 1010 | done = true; |
| 1011 | dropReason = DropReason::NOT_DROPPED; // touch mode events are never dropped |
| 1012 | break; |
| 1013 | } |
| 1014 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1015 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 1016 | const auto typedEntry = |
| 1017 | std::static_pointer_cast<PointerCaptureChangedEntry>(mPendingEvent); |
| 1018 | dispatchPointerCaptureChangedLocked(currentTime, typedEntry, dropReason); |
| 1019 | done = true; |
| 1020 | break; |
| 1021 | } |
| 1022 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1023 | case EventEntry::Type::DRAG: { |
| 1024 | std::shared_ptr<DragEntry> typedEntry = |
| 1025 | std::static_pointer_cast<DragEntry>(mPendingEvent); |
| 1026 | dispatchDragLocked(currentTime, typedEntry); |
| 1027 | done = true; |
| 1028 | break; |
| 1029 | } |
| 1030 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1031 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1032 | std::shared_ptr<KeyEntry> keyEntry = std::static_pointer_cast<KeyEntry>(mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1033 | if (isAppSwitchDue) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1034 | if (isAppSwitchKeyEvent(*keyEntry)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1035 | resetPendingAppSwitchLocked(true); |
| 1036 | isAppSwitchDue = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1037 | } else if (dropReason == DropReason::NOT_DROPPED) { |
| 1038 | dropReason = DropReason::APP_SWITCH; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1039 | } |
| 1040 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1041 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *keyEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1042 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1043 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1044 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 1045 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1046 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1047 | done = dispatchKeyLocked(currentTime, keyEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1048 | break; |
| 1049 | } |
| 1050 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1051 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1052 | std::shared_ptr<MotionEntry> motionEntry = |
| 1053 | std::static_pointer_cast<MotionEntry>(mPendingEvent); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1054 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 1055 | dropReason = DropReason::APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1056 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1057 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *motionEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1058 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1059 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1060 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
Siarhei Vishniakou | b32ea72 | 2023-12-27 23:31:32 +0000 | [diff] [blame^] | 1061 | if (!isFromSource(motionEntry->source, AINPUT_SOURCE_CLASS_POINTER)) { |
| 1062 | // Only drop events that are focus-dispatched. |
| 1063 | dropReason = DropReason::BLOCKED; |
| 1064 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1065 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1066 | done = dispatchMotionLocked(currentTime, motionEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1067 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1068 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1069 | |
| 1070 | case EventEntry::Type::SENSOR: { |
| 1071 | std::shared_ptr<SensorEntry> sensorEntry = |
| 1072 | std::static_pointer_cast<SensorEntry>(mPendingEvent); |
| 1073 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 1074 | dropReason = DropReason::APP_SWITCH; |
| 1075 | } |
| 1076 | // Sensor timestamps use SYSTEM_TIME_BOOTTIME time base, so we can't use |
| 1077 | // 'currentTime' here, get SYSTEM_TIME_BOOTTIME instead. |
| 1078 | nsecs_t bootTime = systemTime(SYSTEM_TIME_BOOTTIME); |
| 1079 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(bootTime, *sensorEntry)) { |
| 1080 | dropReason = DropReason::STALE; |
| 1081 | } |
| 1082 | dispatchSensorLocked(currentTime, sensorEntry, &dropReason, nextWakeupTime); |
| 1083 | done = true; |
| 1084 | break; |
| 1085 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1086 | } |
| 1087 | |
| 1088 | if (done) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1089 | if (dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1090 | dropInboundEventLocked(*mPendingEvent, dropReason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1091 | } |
Michael Wright | 3a98172 | 2015-06-10 15:26:13 +0100 | [diff] [blame] | 1092 | mLastDropReason = dropReason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1093 | |
| 1094 | releasePendingEventLocked(); |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 1095 | *nextWakeupTime = LLONG_MIN; // force next poll to wake up immediately |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1096 | } |
| 1097 | } |
| 1098 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 1099 | bool InputDispatcher::isStaleEvent(nsecs_t currentTime, const EventEntry& entry) { |
| 1100 | return std::chrono::nanoseconds(currentTime - entry.eventTime) >= mStaleEventTimeout; |
| 1101 | } |
| 1102 | |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1103 | /** |
| 1104 | * Return true if the events preceding this incoming motion event should be dropped |
| 1105 | * Return false otherwise (the default behaviour) |
| 1106 | */ |
| 1107 | bool InputDispatcher::shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1108 | const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN && |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 1109 | isFromSource(motionEntry.source, AINPUT_SOURCE_CLASS_POINTER); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1110 | |
| 1111 | // Optimize case where the current application is unresponsive and the user |
| 1112 | // decides to touch a window in a different application. |
| 1113 | // If the application takes too long to catch up then we drop all events preceding |
| 1114 | // the touch into the other window. |
| 1115 | if (isPointerDownEvent && mAwaitedFocusedApplication != nullptr) { |
Siarhei Vishniakou | 9306c38 | 2022-09-30 15:30:31 -0700 | [diff] [blame] | 1116 | const int32_t displayId = motionEntry.displayId; |
| 1117 | const auto [x, y] = resolveTouchedPosition(motionEntry); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1118 | const bool isStylus = isPointerFromStylus(motionEntry, /*pointerIndex=*/0); |
Siarhei Vishniakou | 9306c38 | 2022-09-30 15:30:31 -0700 | [diff] [blame] | 1119 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1120 | auto [touchedWindowHandle, _] = findTouchedWindowAtLocked(displayId, x, y, isStylus); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1121 | if (touchedWindowHandle != nullptr && |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1122 | touchedWindowHandle->getApplicationToken() != |
| 1123 | mAwaitedFocusedApplication->getApplicationToken()) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1124 | // User touched a different application than the one we are waiting on. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1125 | ALOGI("Pruning input queue because user touched a different application while waiting " |
| 1126 | "for %s", |
| 1127 | mAwaitedFocusedApplication->getName().c_str()); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1128 | return true; |
| 1129 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1130 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1131 | // Alternatively, maybe there's a spy window that could handle this event. |
| 1132 | const std::vector<sp<WindowInfoHandle>> touchedSpies = |
| 1133 | findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus); |
| 1134 | for (const auto& windowHandle : touchedSpies) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 1135 | const std::shared_ptr<Connection> connection = |
| 1136 | getConnectionLocked(windowHandle->getToken()); |
Siarhei Vishniakou | 34ed4d4 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1137 | if (connection != nullptr && connection->responsive) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1138 | // This spy window could take more input. Drop all events preceding this |
| 1139 | // 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] | 1140 | 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] | 1141 | "responsive spy window that may handle the event.", |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1142 | mAwaitedFocusedApplication->getName().c_str()); |
| 1143 | return true; |
| 1144 | } |
| 1145 | } |
| 1146 | } |
| 1147 | |
| 1148 | // Prevent getting stuck: if we have a pending key event, and some motion events that have not |
| 1149 | // yet been processed by some connections, the dispatcher will wait for these motion |
| 1150 | // events to be processed before dispatching the key event. This is because these motion events |
| 1151 | // may cause a new window to be launched, which the user might expect to receive focus. |
| 1152 | // To prevent waiting forever for such events, just send the key to the currently focused window |
| 1153 | if (isPointerDownEvent && mKeyIsWaitingForEventsTimeout) { |
| 1154 | ALOGD("Received a new pointer down event, stop waiting for events to process and " |
| 1155 | "just send the pending key event to the focused window."); |
| 1156 | mKeyIsWaitingForEventsTimeout = now(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1157 | } |
| 1158 | return false; |
| 1159 | } |
| 1160 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1161 | bool InputDispatcher::enqueueInboundEventLocked(std::unique_ptr<EventEntry> newEntry) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1162 | bool needWake = mInboundQueue.empty(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1163 | mInboundQueue.push_back(std::move(newEntry)); |
| 1164 | EventEntry& entry = *(mInboundQueue.back()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1165 | traceInboundQueueLengthLocked(); |
| 1166 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1167 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1168 | case EventEntry::Type::KEY: { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1169 | LOG_ALWAYS_FATAL_IF((entry.policyFlags & POLICY_FLAG_TRUSTED) == 0, |
| 1170 | "Unexpected untrusted event."); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1171 | // Optimize app switch latency. |
| 1172 | // If the application takes too long to catch up then we drop all events preceding |
| 1173 | // the app switch key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1174 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1175 | if (isAppSwitchKeyEvent(keyEntry)) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1176 | if (keyEntry.action == AKEY_EVENT_ACTION_DOWN) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1177 | mAppSwitchSawKeyDown = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1178 | } else if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1179 | if (mAppSwitchSawKeyDown) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1180 | if (DEBUG_APP_SWITCH) { |
| 1181 | ALOGD("App switch is pending!"); |
| 1182 | } |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1183 | mAppSwitchDueTime = keyEntry.eventTime + APP_SWITCH_TIMEOUT; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1184 | mAppSwitchSawKeyDown = false; |
| 1185 | needWake = true; |
| 1186 | } |
| 1187 | } |
| 1188 | } |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 1189 | |
| 1190 | // If a new up event comes in, and the pending event with same key code has been asked |
| 1191 | // to try again later because of the policy. We have to reset the intercept key wake up |
| 1192 | // time for it may have been handled in the policy and could be dropped. |
| 1193 | if (keyEntry.action == AKEY_EVENT_ACTION_UP && mPendingEvent && |
| 1194 | mPendingEvent->type == EventEntry::Type::KEY) { |
| 1195 | KeyEntry& pendingKey = static_cast<KeyEntry&>(*mPendingEvent); |
| 1196 | if (pendingKey.keyCode == keyEntry.keyCode && |
| 1197 | pendingKey.interceptKeyResult == |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1198 | KeyEntry::InterceptKeyResult::TRY_AGAIN_LATER) { |
| 1199 | pendingKey.interceptKeyResult = KeyEntry::InterceptKeyResult::UNKNOWN; |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 1200 | pendingKey.interceptKeyWakeupTime = 0; |
| 1201 | needWake = true; |
| 1202 | } |
| 1203 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1204 | break; |
| 1205 | } |
| 1206 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1207 | case EventEntry::Type::MOTION: { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1208 | LOG_ALWAYS_FATAL_IF((entry.policyFlags & POLICY_FLAG_TRUSTED) == 0, |
| 1209 | "Unexpected untrusted event."); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1210 | if (shouldPruneInboundQueueLocked(static_cast<MotionEntry&>(entry))) { |
| 1211 | mNextUnblockedEvent = mInboundQueue.back(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1212 | needWake = true; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1213 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1214 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1215 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1216 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1217 | LOG_ALWAYS_FATAL("Focus events should be inserted using enqueueFocusEventLocked"); |
| 1218 | break; |
| 1219 | } |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1220 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1221 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1222 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1223 | case EventEntry::Type::SENSOR: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1224 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1225 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1226 | // nothing to do |
| 1227 | break; |
| 1228 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1229 | } |
| 1230 | |
| 1231 | return needWake; |
| 1232 | } |
| 1233 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1234 | void InputDispatcher::addRecentEventLocked(std::shared_ptr<EventEntry> entry) { |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1235 | // Do not store sensor event in recent queue to avoid flooding the queue. |
| 1236 | if (entry->type != EventEntry::Type::SENSOR) { |
| 1237 | mRecentQueue.push_back(entry); |
| 1238 | } |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1239 | if (mRecentQueue.size() > RECENT_QUEUE_MAX_SIZE) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1240 | mRecentQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1241 | } |
| 1242 | } |
| 1243 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1244 | std::pair<sp<WindowInfoHandle>, std::vector<InputTarget>> |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 1245 | InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, float x, float y, bool isStylus, |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1246 | bool ignoreDragWindow) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1247 | // Traverse windows from front to back to find touched window. |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1248 | std::vector<InputTarget> outsideTargets; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1249 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1250 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 1251 | if (ignoreDragWindow && haveSameToken(windowHandle, mDragState->dragWindow)) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1252 | continue; |
| 1253 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1254 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1255 | const WindowInfo& info = *windowHandle->getInfo(); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 1256 | if (!info.isSpy() && |
| 1257 | windowAcceptsTouchAt(info, displayId, x, y, isStylus, getTransformLocked(displayId))) { |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1258 | return {windowHandle, outsideTargets}; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1259 | } |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1260 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1261 | if (info.inputConfig.test(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH)) { |
| 1262 | addWindowTargetLocked(windowHandle, InputTarget::Flags::DISPATCH_AS_OUTSIDE, |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 1263 | /*pointerIds=*/{}, /*firstDownTimeInTarget=*/std::nullopt, |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1264 | outsideTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1265 | } |
| 1266 | } |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1267 | return {nullptr, {}}; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1268 | } |
| 1269 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1270 | std::vector<sp<WindowInfoHandle>> InputDispatcher::findTouchedSpyWindowsAtLocked( |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 1271 | int32_t displayId, float x, float y, bool isStylus) const { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1272 | // Traverse windows from front to back and gather the touched spy windows. |
| 1273 | std::vector<sp<WindowInfoHandle>> spyWindows; |
| 1274 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
| 1275 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 1276 | const WindowInfo& info = *windowHandle->getInfo(); |
| 1277 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 1278 | if (!windowAcceptsTouchAt(info, displayId, x, y, isStylus, getTransformLocked(displayId))) { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1279 | continue; |
| 1280 | } |
| 1281 | if (!info.isSpy()) { |
| 1282 | // The first touched non-spy window was found, so return the spy windows touched so far. |
| 1283 | return spyWindows; |
| 1284 | } |
| 1285 | spyWindows.push_back(windowHandle); |
| 1286 | } |
| 1287 | return spyWindows; |
| 1288 | } |
| 1289 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1290 | void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1291 | const char* reason; |
| 1292 | switch (dropReason) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1293 | case DropReason::POLICY: |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 1294 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1295 | ALOGD("Dropped event because policy consumed it."); |
| 1296 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1297 | reason = "inbound event was dropped because the policy consumed it"; |
| 1298 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1299 | case DropReason::DISABLED: |
| 1300 | if (mLastDropReason != DropReason::DISABLED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1301 | ALOGI("Dropped event because input dispatch is disabled."); |
| 1302 | } |
| 1303 | reason = "inbound event was dropped because input dispatch is disabled"; |
| 1304 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1305 | case DropReason::APP_SWITCH: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1306 | ALOGI("Dropped event because of pending overdue app switch."); |
| 1307 | reason = "inbound event was dropped because of pending overdue app switch"; |
| 1308 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1309 | case DropReason::BLOCKED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1310 | ALOGI("Dropped event because the current application is not responding and the user " |
| 1311 | "has started interacting with a different application."); |
| 1312 | reason = "inbound event was dropped because the current application is not responding " |
| 1313 | "and the user has started interacting with a different application"; |
| 1314 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1315 | case DropReason::STALE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1316 | ALOGI("Dropped event because it is stale."); |
| 1317 | reason = "inbound event was dropped because it is stale"; |
| 1318 | break; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1319 | case DropReason::NO_POINTER_CAPTURE: |
| 1320 | ALOGI("Dropped event because there is no window with Pointer Capture."); |
| 1321 | reason = "inbound event was dropped because there is no window with Pointer Capture"; |
| 1322 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1323 | case DropReason::NOT_DROPPED: { |
| 1324 | LOG_ALWAYS_FATAL("Should not be dropping a NOT_DROPPED event"); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1325 | return; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1326 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1327 | } |
| 1328 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1329 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1330 | case EventEntry::Type::KEY: { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1331 | CancelationOptions options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, reason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1332 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1333 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1334 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1335 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1336 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1337 | if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1338 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, reason); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1339 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1340 | } else { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1341 | CancelationOptions options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, |
| 1342 | reason); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1343 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1344 | } |
| 1345 | break; |
| 1346 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1347 | case EventEntry::Type::SENSOR: { |
| 1348 | break; |
| 1349 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1350 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1351 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1352 | break; |
| 1353 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1354 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1355 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1356 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 1357 | case EventEntry::Type::DEVICE_RESET: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1358 | 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] | 1359 | break; |
| 1360 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1361 | } |
| 1362 | } |
| 1363 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1364 | static bool isAppSwitchKeyCode(int32_t keyCode) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1365 | return keyCode == AKEYCODE_HOME || keyCode == AKEYCODE_ENDCALL || |
| 1366 | keyCode == AKEYCODE_APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1367 | } |
| 1368 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1369 | bool InputDispatcher::isAppSwitchKeyEvent(const KeyEntry& keyEntry) { |
| 1370 | return !(keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) && isAppSwitchKeyCode(keyEntry.keyCode) && |
| 1371 | (keyEntry.policyFlags & POLICY_FLAG_TRUSTED) && |
| 1372 | (keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1373 | } |
| 1374 | |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 1375 | bool InputDispatcher::isAppSwitchPendingLocked() const { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 1376 | return mAppSwitchDueTime != LLONG_MAX; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1377 | } |
| 1378 | |
| 1379 | void InputDispatcher::resetPendingAppSwitchLocked(bool handled) { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 1380 | mAppSwitchDueTime = LLONG_MAX; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1381 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1382 | if (DEBUG_APP_SWITCH) { |
| 1383 | if (handled) { |
| 1384 | ALOGD("App switch has arrived."); |
| 1385 | } else { |
| 1386 | ALOGD("App switch was abandoned."); |
| 1387 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1388 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1389 | } |
| 1390 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1391 | bool InputDispatcher::haveCommandsLocked() const { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1392 | return !mCommandQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1393 | } |
| 1394 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1395 | bool InputDispatcher::runCommandsLockedInterruptable() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1396 | if (mCommandQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1397 | return false; |
| 1398 | } |
| 1399 | |
| 1400 | do { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1401 | auto command = std::move(mCommandQueue.front()); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1402 | mCommandQueue.pop_front(); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1403 | // Commands are run with the lock held, but may release and re-acquire the lock from within. |
| 1404 | command(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1405 | } while (!mCommandQueue.empty()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1406 | return true; |
| 1407 | } |
| 1408 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1409 | void InputDispatcher::postCommandLocked(Command&& command) { |
| 1410 | mCommandQueue.push_back(command); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1411 | } |
| 1412 | |
| 1413 | void InputDispatcher::drainInboundQueueLocked() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1414 | while (!mInboundQueue.empty()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1415 | std::shared_ptr<EventEntry> entry = mInboundQueue.front(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1416 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1417 | releaseInboundEventLocked(entry); |
| 1418 | } |
| 1419 | traceInboundQueueLengthLocked(); |
| 1420 | } |
| 1421 | |
| 1422 | void InputDispatcher::releasePendingEventLocked() { |
| 1423 | if (mPendingEvent) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1424 | releaseInboundEventLocked(mPendingEvent); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1425 | mPendingEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1426 | } |
| 1427 | } |
| 1428 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1429 | void InputDispatcher::releaseInboundEventLocked(std::shared_ptr<EventEntry> entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1430 | InjectionState* injectionState = entry->injectionState; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1431 | if (injectionState && injectionState->injectionResult == InputEventInjectionResult::PENDING) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1432 | if (DEBUG_DISPATCH_CYCLE) { |
| 1433 | ALOGD("Injected inbound event was dropped."); |
| 1434 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1435 | setInjectionResult(*entry, InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1436 | } |
| 1437 | if (entry == mNextUnblockedEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1438 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1439 | } |
| 1440 | addRecentEventLocked(entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1441 | } |
| 1442 | |
| 1443 | void InputDispatcher::resetKeyRepeatLocked() { |
| 1444 | if (mKeyRepeatState.lastKeyEntry) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1445 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1446 | } |
| 1447 | } |
| 1448 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1449 | std::shared_ptr<KeyEntry> InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) { |
| 1450 | std::shared_ptr<KeyEntry> entry = mKeyRepeatState.lastKeyEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1451 | |
Michael Wright | 2e73295 | 2014-09-24 13:26:59 -0700 | [diff] [blame] | 1452 | uint32_t policyFlags = entry->policyFlags & |
| 1453 | (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1454 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1455 | std::shared_ptr<KeyEntry> newEntry = |
| 1456 | std::make_unique<KeyEntry>(mIdGenerator.nextId(), currentTime, entry->deviceId, |
| 1457 | entry->source, entry->displayId, policyFlags, entry->action, |
| 1458 | entry->flags, entry->keyCode, entry->scanCode, |
| 1459 | entry->metaState, entry->repeatCount + 1, entry->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1460 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1461 | newEntry->syntheticRepeat = true; |
| 1462 | mKeyRepeatState.lastKeyEntry = newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1463 | mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1464 | return newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1465 | } |
| 1466 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1467 | bool InputDispatcher::dispatchConfigurationChangedLocked(nsecs_t currentTime, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1468 | const ConfigurationChangedEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1469 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1470 | ALOGD("dispatchConfigurationChanged - eventTime=%" PRId64, entry.eventTime); |
| 1471 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1472 | |
| 1473 | // Reset key repeating in case a keyboard device was added or removed or something. |
| 1474 | resetKeyRepeatLocked(); |
| 1475 | |
| 1476 | // 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] | 1477 | auto command = [this, eventTime = entry.eventTime]() REQUIRES(mLock) { |
| 1478 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 1479 | mPolicy.notifyConfigurationChanged(eventTime); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1480 | }; |
| 1481 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1482 | return true; |
| 1483 | } |
| 1484 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1485 | bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime, |
| 1486 | const DeviceResetEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1487 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1488 | ALOGD("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry.eventTime, |
| 1489 | entry.deviceId); |
| 1490 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1491 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 1492 | // Reset key repeating in case a keyboard device was disabled or enabled. |
| 1493 | if (mKeyRepeatState.lastKeyEntry && mKeyRepeatState.lastKeyEntry->deviceId == entry.deviceId) { |
| 1494 | resetKeyRepeatLocked(); |
| 1495 | } |
| 1496 | |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1497 | CancelationOptions options(CancelationOptions::Mode::CANCEL_ALL_EVENTS, "device was reset"); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1498 | options.deviceId = entry.deviceId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1499 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 1500 | |
| 1501 | // Remove all active pointers from this device |
| 1502 | for (auto& [_, touchState] : mTouchStatesByDisplay) { |
| 1503 | touchState.removeAllPointersForDevice(entry.deviceId); |
| 1504 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1505 | return true; |
| 1506 | } |
| 1507 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1508 | void InputDispatcher::enqueueFocusEventLocked(const sp<IBinder>& windowToken, bool hasFocus, |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1509 | const std::string& reason) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1510 | if (mPendingEvent != nullptr) { |
| 1511 | // Move the pending event to the front of the queue. This will give the chance |
| 1512 | // for the pending event to get dispatched to the newly focused window |
| 1513 | mInboundQueue.push_front(mPendingEvent); |
| 1514 | mPendingEvent = nullptr; |
| 1515 | } |
| 1516 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1517 | std::unique_ptr<FocusEntry> focusEntry = |
| 1518 | std::make_unique<FocusEntry>(mIdGenerator.nextId(), now(), windowToken, hasFocus, |
| 1519 | reason); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1520 | |
| 1521 | // This event should go to the front of the queue, but behind all other focus events |
| 1522 | // Find the last focus event, and insert right after it |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1523 | std::deque<std::shared_ptr<EventEntry>>::reverse_iterator it = |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1524 | std::find_if(mInboundQueue.rbegin(), mInboundQueue.rend(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1525 | [](const std::shared_ptr<EventEntry>& event) { |
| 1526 | return event->type == EventEntry::Type::FOCUS; |
| 1527 | }); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1528 | |
| 1529 | // 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] | 1530 | mInboundQueue.insert(it.base(), std::move(focusEntry)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1531 | } |
| 1532 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1533 | void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime, std::shared_ptr<FocusEntry> entry) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 1534 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1535 | if (channel == nullptr) { |
| 1536 | return; // Window has gone away |
| 1537 | } |
| 1538 | InputTarget target; |
| 1539 | target.inputChannel = channel; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 1540 | target.flags = InputTarget::Flags::DISPATCH_AS_IS; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1541 | entry->dispatchInProgress = true; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1542 | std::string message = std::string("Focus ") + (entry->hasFocus ? "entering " : "leaving ") + |
| 1543 | channel->getName(); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 1544 | std::string reason = std::string("reason=").append(entry->reason); |
| 1545 | android_log_event_list(LOGTAG_INPUT_FOCUS) << message << reason << LOG_ID_EVENTS; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1546 | dispatchEventLocked(currentTime, entry, {target}); |
| 1547 | } |
| 1548 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1549 | void InputDispatcher::dispatchPointerCaptureChangedLocked( |
| 1550 | nsecs_t currentTime, const std::shared_ptr<PointerCaptureChangedEntry>& entry, |
| 1551 | DropReason& dropReason) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1552 | dropReason = DropReason::NOT_DROPPED; |
| 1553 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1554 | const bool haveWindowWithPointerCapture = mWindowTokenWithPointerCapture != nullptr; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1555 | sp<IBinder> token; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1556 | |
| 1557 | if (entry->pointerCaptureRequest.enable) { |
| 1558 | // Enable Pointer Capture. |
| 1559 | if (haveWindowWithPointerCapture && |
| 1560 | (entry->pointerCaptureRequest == mCurrentPointerCaptureRequest)) { |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 1561 | // This can happen if pointer capture is disabled and re-enabled before we notify the |
| 1562 | // app of the state change, so there is no need to notify the app. |
| 1563 | ALOGI("Skipping dispatch of Pointer Capture being enabled: no state change."); |
| 1564 | return; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1565 | } |
| 1566 | if (!mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1567 | // This can happen if a window requests capture and immediately releases capture. |
| 1568 | ALOGW("No window requested Pointer Capture."); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1569 | dropReason = DropReason::NO_POINTER_CAPTURE; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1570 | return; |
| 1571 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1572 | if (entry->pointerCaptureRequest.seq != mCurrentPointerCaptureRequest.seq) { |
| 1573 | ALOGI("Skipping dispatch of Pointer Capture being enabled: sequence number mismatch."); |
| 1574 | return; |
| 1575 | } |
| 1576 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1577 | token = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1578 | LOG_ALWAYS_FATAL_IF(!token, "Cannot find focused window for Pointer Capture."); |
| 1579 | mWindowTokenWithPointerCapture = token; |
| 1580 | } else { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1581 | // Disable Pointer Capture. |
| 1582 | // We do not check if the sequence number matches for requests to disable Pointer Capture |
| 1583 | // for two reasons: |
| 1584 | // 1. Pointer Capture can be disabled by a focus change, which means we can get two entries |
| 1585 | // to disable capture with the same sequence number: one generated by |
| 1586 | // disablePointerCaptureForcedLocked() and another as an acknowledgement of Pointer |
| 1587 | // Capture being disabled in InputReader. |
| 1588 | // 2. We respect any request to disable Pointer Capture generated by InputReader, since the |
| 1589 | // actual Pointer Capture state that affects events being generated by input devices is |
| 1590 | // in InputReader. |
| 1591 | if (!haveWindowWithPointerCapture) { |
| 1592 | // Pointer capture was already forcefully disabled because of focus change. |
| 1593 | dropReason = DropReason::NOT_DROPPED; |
| 1594 | return; |
| 1595 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1596 | token = mWindowTokenWithPointerCapture; |
| 1597 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1598 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1599 | setPointerCaptureLocked(false); |
| 1600 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1601 | } |
| 1602 | |
| 1603 | auto channel = getInputChannelLocked(token); |
| 1604 | if (channel == nullptr) { |
| 1605 | // Window has gone away, clean up Pointer Capture state. |
| 1606 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1607 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1608 | setPointerCaptureLocked(false); |
| 1609 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1610 | return; |
| 1611 | } |
| 1612 | InputTarget target; |
| 1613 | target.inputChannel = channel; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 1614 | target.flags = InputTarget::Flags::DISPATCH_AS_IS; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1615 | entry->dispatchInProgress = true; |
| 1616 | dispatchEventLocked(currentTime, entry, {target}); |
| 1617 | |
| 1618 | dropReason = DropReason::NOT_DROPPED; |
| 1619 | } |
| 1620 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1621 | void InputDispatcher::dispatchTouchModeChangeLocked(nsecs_t currentTime, |
| 1622 | const std::shared_ptr<TouchModeEntry>& entry) { |
| 1623 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 1624 | getWindowHandlesLocked(entry->displayId); |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1625 | if (windowHandles.empty()) { |
| 1626 | return; |
| 1627 | } |
| 1628 | const std::vector<InputTarget> inputTargets = |
| 1629 | getInputTargetsFromWindowHandlesLocked(windowHandles); |
| 1630 | if (inputTargets.empty()) { |
| 1631 | return; |
| 1632 | } |
| 1633 | entry->dispatchInProgress = true; |
| 1634 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1635 | } |
| 1636 | |
| 1637 | std::vector<InputTarget> InputDispatcher::getInputTargetsFromWindowHandlesLocked( |
| 1638 | const std::vector<sp<WindowInfoHandle>>& windowHandles) const { |
| 1639 | std::vector<InputTarget> inputTargets; |
| 1640 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1641 | const sp<IBinder>& token = handle->getToken(); |
| 1642 | if (token == nullptr) { |
| 1643 | continue; |
| 1644 | } |
| 1645 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(token); |
| 1646 | if (channel == nullptr) { |
| 1647 | continue; // Window has gone away |
| 1648 | } |
| 1649 | InputTarget target; |
| 1650 | target.inputChannel = channel; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 1651 | target.flags = InputTarget::Flags::DISPATCH_AS_IS; |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1652 | inputTargets.push_back(target); |
| 1653 | } |
| 1654 | return inputTargets; |
| 1655 | } |
| 1656 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1657 | bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, std::shared_ptr<KeyEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1658 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1659 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1660 | if (!entry->dispatchInProgress) { |
| 1661 | if (entry->repeatCount == 0 && entry->action == AKEY_EVENT_ACTION_DOWN && |
| 1662 | (entry->policyFlags & POLICY_FLAG_TRUSTED) && |
| 1663 | (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { |
| 1664 | if (mKeyRepeatState.lastKeyEntry && |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1665 | mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode && |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1666 | // We have seen two identical key downs in a row which indicates that the device |
| 1667 | // driver is automatically generating key repeats itself. We take note of the |
| 1668 | // repeat here, but we disable our own next key repeat timer since it is clear that |
| 1669 | // we will not need to synthesize key repeats ourselves. |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1670 | mKeyRepeatState.lastKeyEntry->deviceId == entry->deviceId) { |
| 1671 | // Make sure we don't get key down from a different device. If a different |
| 1672 | // device Id has same key pressed down, the new device Id will replace the |
| 1673 | // current one to hold the key repeat with repeat count reset. |
| 1674 | // In the future when got a KEY_UP on the device id, drop it and do not |
| 1675 | // stop the key repeat on current device. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1676 | entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; |
| 1677 | resetKeyRepeatLocked(); |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 1678 | mKeyRepeatState.nextRepeatTime = LLONG_MAX; // don't generate repeats ourselves |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1679 | } else { |
| 1680 | // Not a repeat. Save key down state in case we do see a repeat later. |
| 1681 | resetKeyRepeatLocked(); |
| 1682 | mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; |
| 1683 | } |
| 1684 | mKeyRepeatState.lastKeyEntry = entry; |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1685 | } else if (entry->action == AKEY_EVENT_ACTION_UP && mKeyRepeatState.lastKeyEntry && |
| 1686 | mKeyRepeatState.lastKeyEntry->deviceId != entry->deviceId) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1687 | // 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] | 1688 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1689 | ALOGD("deviceId=%d got KEY_UP as stale", entry->deviceId); |
| 1690 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1691 | } else if (!entry->syntheticRepeat) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1692 | resetKeyRepeatLocked(); |
| 1693 | } |
| 1694 | |
| 1695 | if (entry->repeatCount == 1) { |
| 1696 | entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; |
| 1697 | } else { |
| 1698 | entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; |
| 1699 | } |
| 1700 | |
| 1701 | entry->dispatchInProgress = true; |
| 1702 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1703 | logOutboundKeyDetails("dispatchKey - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1704 | } |
| 1705 | |
| 1706 | // 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] | 1707 | if (entry->interceptKeyResult == KeyEntry::InterceptKeyResult::TRY_AGAIN_LATER) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1708 | if (currentTime < entry->interceptKeyWakeupTime) { |
| 1709 | if (entry->interceptKeyWakeupTime < *nextWakeupTime) { |
| 1710 | *nextWakeupTime = entry->interceptKeyWakeupTime; |
| 1711 | } |
| 1712 | return false; // wait until next wakeup |
| 1713 | } |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1714 | entry->interceptKeyResult = KeyEntry::InterceptKeyResult::UNKNOWN; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1715 | entry->interceptKeyWakeupTime = 0; |
| 1716 | } |
| 1717 | |
| 1718 | // Give the policy a chance to intercept the key. |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1719 | if (entry->interceptKeyResult == KeyEntry::InterceptKeyResult::UNKNOWN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1720 | if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1721 | sp<IBinder> focusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1722 | mFocusResolver.getFocusedWindowToken(getTargetDisplayId(*entry)); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1723 | |
| 1724 | auto command = [this, focusedWindowToken, entry]() REQUIRES(mLock) { |
| 1725 | doInterceptKeyBeforeDispatchingCommand(focusedWindowToken, *entry); |
| 1726 | }; |
| 1727 | postCommandLocked(std::move(command)); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1728 | // Poke user activity for keys not passed to user |
| 1729 | pokeUserActivityLocked(*entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1730 | return false; // wait for the command to run |
| 1731 | } else { |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1732 | entry->interceptKeyResult = KeyEntry::InterceptKeyResult::CONTINUE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1733 | } |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1734 | } else if (entry->interceptKeyResult == KeyEntry::InterceptKeyResult::SKIP) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1735 | if (*dropReason == DropReason::NOT_DROPPED) { |
| 1736 | *dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1737 | } |
| 1738 | } |
| 1739 | |
| 1740 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1741 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1742 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1743 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1744 | : InputEventInjectionResult::FAILED); |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1745 | mReporter->reportDroppedKey(entry->id); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1746 | // Poke user activity for undispatched keys |
| 1747 | pokeUserActivityLocked(*entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1748 | return true; |
| 1749 | } |
| 1750 | |
| 1751 | // Identify targets. |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1752 | InputEventInjectionResult injectionResult; |
| 1753 | sp<WindowInfoHandle> focusedWindow = |
| 1754 | findFocusedWindowTargetLocked(currentTime, *entry, nextWakeupTime, |
| 1755 | /*byref*/ injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1756 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1757 | return false; |
| 1758 | } |
| 1759 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1760 | setInjectionResult(*entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1761 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1762 | return true; |
| 1763 | } |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1764 | LOG_ALWAYS_FATAL_IF(focusedWindow == nullptr); |
| 1765 | |
| 1766 | std::vector<InputTarget> inputTargets; |
| 1767 | addWindowTargetLocked(focusedWindow, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 1768 | InputTarget::Flags::FOREGROUND | InputTarget::Flags::DISPATCH_AS_IS, |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 1769 | /*pointerIds=*/{}, getDownTime(*entry), inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1770 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1771 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1772 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1773 | |
| 1774 | // Dispatch the key. |
| 1775 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1776 | return true; |
| 1777 | } |
| 1778 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1779 | void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1780 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1781 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 ", " |
| 1782 | "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, " |
| 1783 | "metaState=0x%x, repeatCount=%d, downTime=%" PRId64, |
| 1784 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, |
| 1785 | entry.policyFlags, entry.action, entry.flags, entry.keyCode, entry.scanCode, |
| 1786 | entry.metaState, entry.repeatCount, entry.downTime); |
| 1787 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1788 | } |
| 1789 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1790 | void InputDispatcher::dispatchSensorLocked(nsecs_t currentTime, |
| 1791 | const std::shared_ptr<SensorEntry>& entry, |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1792 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1793 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1794 | ALOGD("notifySensorEvent eventTime=%" PRId64 ", hwTimestamp=%" PRId64 ", deviceId=%d, " |
| 1795 | "source=0x%x, sensorType=%s", |
| 1796 | entry->eventTime, entry->hwTimestamp, entry->deviceId, entry->source, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1797 | ftl::enum_string(entry->sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1798 | } |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1799 | auto command = [this, entry]() REQUIRES(mLock) { |
| 1800 | scoped_unlock unlock(mLock); |
| 1801 | |
| 1802 | if (entry->accuracyChanged) { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 1803 | mPolicy.notifySensorAccuracy(entry->deviceId, entry->sensorType, entry->accuracy); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1804 | } |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 1805 | mPolicy.notifySensorEvent(entry->deviceId, entry->sensorType, entry->accuracy, |
| 1806 | entry->hwTimestamp, entry->values); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1807 | }; |
| 1808 | postCommandLocked(std::move(command)); |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1809 | } |
| 1810 | |
| 1811 | bool InputDispatcher::flushSensor(int deviceId, InputDeviceSensorType sensorType) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1812 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1813 | ALOGD("flushSensor deviceId=%d, sensorType=%s", deviceId, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1814 | ftl::enum_string(sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1815 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1816 | { // acquire lock |
| 1817 | std::scoped_lock _l(mLock); |
| 1818 | |
| 1819 | for (auto it = mInboundQueue.begin(); it != mInboundQueue.end(); it++) { |
| 1820 | std::shared_ptr<EventEntry> entry = *it; |
| 1821 | if (entry->type == EventEntry::Type::SENSOR) { |
| 1822 | it = mInboundQueue.erase(it); |
| 1823 | releaseInboundEventLocked(entry); |
| 1824 | } |
| 1825 | } |
| 1826 | } |
| 1827 | return true; |
| 1828 | } |
| 1829 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1830 | bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, std::shared_ptr<MotionEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1831 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1832 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1833 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1834 | if (!entry->dispatchInProgress) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1835 | entry->dispatchInProgress = true; |
| 1836 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1837 | logOutboundMotionDetails("dispatchMotion - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1838 | } |
| 1839 | |
| 1840 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1841 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1842 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1843 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1844 | : InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1845 | return true; |
| 1846 | } |
| 1847 | |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 1848 | const bool isPointerEvent = isFromSource(entry->source, AINPUT_SOURCE_CLASS_POINTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1849 | |
| 1850 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1851 | std::vector<InputTarget> inputTargets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1852 | |
| 1853 | bool conflictingPointerActions = false; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1854 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1855 | if (isPointerEvent) { |
| 1856 | // Pointer event. (eg. touchscreen) |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 1857 | |
| 1858 | if (mDragState && |
| 1859 | (entry->action & AMOTION_EVENT_ACTION_MASK) == AMOTION_EVENT_ACTION_POINTER_DOWN) { |
| 1860 | // If drag and drop ongoing and pointer down occur: pilfer drag window pointers |
| 1861 | pilferPointersLocked(mDragState->dragWindow->getToken()); |
| 1862 | } |
| 1863 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1864 | inputTargets = |
Siarhei Vishniakou | 4fe5739 | 2022-10-25 13:44:30 -0700 | [diff] [blame] | 1865 | findTouchedWindowTargetsLocked(currentTime, *entry, &conflictingPointerActions, |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1866 | /*byref*/ injectionResult); |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 1867 | LOG_ALWAYS_FATAL_IF(injectionResult != InputEventInjectionResult::SUCCEEDED && |
| 1868 | !inputTargets.empty()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1869 | } else { |
| 1870 | // Non touch event. (eg. trackball) |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1871 | sp<WindowInfoHandle> focusedWindow = |
| 1872 | findFocusedWindowTargetLocked(currentTime, *entry, nextWakeupTime, injectionResult); |
| 1873 | if (injectionResult == InputEventInjectionResult::SUCCEEDED) { |
| 1874 | LOG_ALWAYS_FATAL_IF(focusedWindow == nullptr); |
| 1875 | addWindowTargetLocked(focusedWindow, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 1876 | InputTarget::Flags::FOREGROUND | |
| 1877 | InputTarget::Flags::DISPATCH_AS_IS, |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 1878 | /*pointerIds=*/{}, getDownTime(*entry), inputTargets); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1879 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1880 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1881 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1882 | return false; |
| 1883 | } |
| 1884 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1885 | setInjectionResult(*entry, injectionResult); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1886 | if (injectionResult == InputEventInjectionResult::TARGET_MISMATCH) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1887 | return true; |
| 1888 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1889 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1890 | CancelationOptions::Mode mode( |
| 1891 | isPointerEvent ? CancelationOptions::Mode::CANCEL_POINTER_EVENTS |
| 1892 | : CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS); |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1893 | CancelationOptions options(mode, "input event injection failed"); |
| 1894 | synthesizeCancelationEventsForMonitorsLocked(options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1895 | return true; |
| 1896 | } |
| 1897 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1898 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1899 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1900 | |
| 1901 | // Dispatch the motion. |
| 1902 | if (conflictingPointerActions) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1903 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1904 | "conflicting pointer actions"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1905 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1906 | } |
| 1907 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1908 | return true; |
| 1909 | } |
| 1910 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1911 | void InputDispatcher::enqueueDragEventLocked(const sp<WindowInfoHandle>& windowHandle, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 1912 | bool isExiting, const int32_t rawX, |
| 1913 | const int32_t rawY) { |
| 1914 | const vec2 xy = windowHandle->getInfo()->transform.transform(vec2(rawX, rawY)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1915 | std::unique_ptr<DragEntry> dragEntry = |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 1916 | std::make_unique<DragEntry>(mIdGenerator.nextId(), now(), windowHandle->getToken(), |
| 1917 | isExiting, xy.x, xy.y); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1918 | |
| 1919 | enqueueInboundEventLocked(std::move(dragEntry)); |
| 1920 | } |
| 1921 | |
| 1922 | void InputDispatcher::dispatchDragLocked(nsecs_t currentTime, std::shared_ptr<DragEntry> entry) { |
| 1923 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
| 1924 | if (channel == nullptr) { |
| 1925 | return; // Window has gone away |
| 1926 | } |
| 1927 | InputTarget target; |
| 1928 | target.inputChannel = channel; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 1929 | target.flags = InputTarget::Flags::DISPATCH_AS_IS; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1930 | entry->dispatchInProgress = true; |
| 1931 | dispatchEventLocked(currentTime, entry, {target}); |
| 1932 | } |
| 1933 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1934 | void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1935 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 1936 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=%s, displayId=%" PRId32 |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1937 | ", policyFlags=0x%x, " |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1938 | "action=%s, actionButton=0x%x, flags=0x%x, " |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1939 | "metaState=0x%x, buttonState=0x%x," |
| 1940 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64, |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 1941 | prefix, entry.eventTime, entry.deviceId, |
| 1942 | inputEventSourceToString(entry.source).c_str(), entry.displayId, entry.policyFlags, |
| 1943 | MotionEvent::actionToString(entry.action).c_str(), entry.actionButton, entry.flags, |
| 1944 | entry.metaState, entry.buttonState, entry.edgeFlags, entry.xPrecision, |
| 1945 | entry.yPrecision, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1946 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1947 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1948 | ALOGD(" Pointer %d: id=%d, toolType=%s, " |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1949 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 1950 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 1951 | "orientation=%f", |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1952 | i, entry.pointerProperties[i].id, |
| 1953 | ftl::enum_string(entry.pointerProperties[i].toolType).c_str(), |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1954 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 1955 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 1956 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 1957 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 1958 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 1959 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 1960 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 1961 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 1962 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| 1963 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1964 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1965 | } |
| 1966 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1967 | void InputDispatcher::dispatchEventLocked(nsecs_t currentTime, |
| 1968 | std::shared_ptr<EventEntry> eventEntry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1969 | const std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1970 | ATRACE_CALL(); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1971 | if (DEBUG_DISPATCH_CYCLE) { |
| 1972 | ALOGD("dispatchEventToCurrentInputTargets"); |
| 1973 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1974 | |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 1975 | processInteractionsLocked(*eventEntry, inputTargets); |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1976 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1977 | ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true |
| 1978 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1979 | pokeUserActivityLocked(*eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1980 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1981 | for (const InputTarget& inputTarget : inputTargets) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 1982 | std::shared_ptr<Connection> connection = |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 1983 | getConnectionLocked(inputTarget.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 1984 | if (connection != nullptr) { |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 1985 | prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1986 | } else { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1987 | if (DEBUG_FOCUS) { |
| 1988 | ALOGD("Dropping event delivery to target with channel '%s' because it " |
| 1989 | "is no longer registered with the input dispatcher.", |
| 1990 | inputTarget.inputChannel->getName().c_str()); |
| 1991 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1992 | } |
| 1993 | } |
| 1994 | } |
| 1995 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 1996 | void InputDispatcher::cancelEventsForAnrLocked(const std::shared_ptr<Connection>& connection) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1997 | // We will not be breaking any connections here, even if the policy wants us to abort dispatch. |
| 1998 | // If the policy decides to close the app, we will get a channel removal event via |
| 1999 | // unregisterInputChannel, and will clean up the connection that way. We are already not |
| 2000 | // sending new pointers to the connection when it blocked, but focused events will continue to |
| 2001 | // pile up. |
| 2002 | ALOGW("Canceling events for %s because it is unresponsive", |
| 2003 | connection->inputChannel->getName().c_str()); |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 2004 | if (connection->status == Connection::Status::NORMAL) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 2005 | CancelationOptions options(CancelationOptions::Mode::CANCEL_ALL_EVENTS, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2006 | "application not responding"); |
| 2007 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2008 | } |
| 2009 | } |
| 2010 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2011 | void InputDispatcher::resetNoFocusedWindowTimeoutLocked() { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2012 | if (DEBUG_FOCUS) { |
| 2013 | ALOGD("Resetting ANR timeouts."); |
| 2014 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2015 | |
| 2016 | // Reset input target wait timeout. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2017 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2018 | mAwaitedFocusedApplication.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2019 | } |
| 2020 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2021 | /** |
| 2022 | * Get the display id that the given event should go to. If this event specifies a valid display id, |
| 2023 | * then it should be dispatched to that display. Otherwise, the event goes to the focused display. |
| 2024 | * Focused display is the display that the user most recently interacted with. |
| 2025 | */ |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2026 | int32_t InputDispatcher::getTargetDisplayId(const EventEntry& entry) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2027 | int32_t displayId; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2028 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2029 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2030 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 2031 | displayId = keyEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2032 | break; |
| 2033 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2034 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2035 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 2036 | displayId = motionEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2037 | break; |
| 2038 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 2039 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 2040 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2041 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2042 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2043 | case EventEntry::Type::DEVICE_RESET: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2044 | case EventEntry::Type::SENSOR: |
| 2045 | case EventEntry::Type::DRAG: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2046 | 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] | 2047 | return ADISPLAY_ID_NONE; |
| 2048 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2049 | } |
| 2050 | return displayId == ADISPLAY_ID_NONE ? mFocusedDisplayId : displayId; |
| 2051 | } |
| 2052 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2053 | bool InputDispatcher::shouldWaitToSendKeyLocked(nsecs_t currentTime, |
| 2054 | const char* focusedWindowName) { |
| 2055 | if (mAnrTracker.empty()) { |
| 2056 | // already processed all events that we waited for |
| 2057 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 2058 | return false; |
| 2059 | } |
| 2060 | |
| 2061 | if (!mKeyIsWaitingForEventsTimeout.has_value()) { |
| 2062 | // Start the timer |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 2063 | // 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] | 2064 | mKeyIsWaitingForEventsTimeout = currentTime + |
| 2065 | std::chrono::duration_cast<std::chrono::nanoseconds>(KEY_WAITING_FOR_EVENTS_TIMEOUT) |
| 2066 | .count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2067 | return true; |
| 2068 | } |
| 2069 | |
| 2070 | // We still have pending events, and already started the timer |
| 2071 | if (currentTime < *mKeyIsWaitingForEventsTimeout) { |
| 2072 | return true; // Still waiting |
| 2073 | } |
| 2074 | |
| 2075 | // Waited too long, and some connection still hasn't processed all motions |
| 2076 | // Just send the key to the focused window |
| 2077 | ALOGW("Dispatching key to %s even though there are other unprocessed events", |
| 2078 | focusedWindowName); |
| 2079 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 2080 | return false; |
| 2081 | } |
| 2082 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2083 | sp<WindowInfoHandle> InputDispatcher::findFocusedWindowTargetLocked( |
| 2084 | nsecs_t currentTime, const EventEntry& entry, nsecs_t* nextWakeupTime, |
| 2085 | InputEventInjectionResult& outInjectionResult) { |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2086 | outInjectionResult = InputEventInjectionResult::FAILED; // Default result |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2087 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2088 | int32_t displayId = getTargetDisplayId(entry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2089 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2090 | std::shared_ptr<InputApplicationHandle> focusedApplicationHandle = |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2091 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 2092 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2093 | // If there is no currently focused window and no focused application |
| 2094 | // then drop the event. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2095 | if (focusedWindowHandle == nullptr && focusedApplicationHandle == nullptr) { |
| 2096 | ALOGI("Dropping %s event because there is no focused window or focused application in " |
| 2097 | "display %" PRId32 ".", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2098 | ftl::enum_string(entry.type).c_str(), displayId); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2099 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2100 | } |
| 2101 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2102 | // Drop key events if requested by input feature |
| 2103 | if (focusedWindowHandle != nullptr && shouldDropInput(entry, focusedWindowHandle)) { |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2104 | return nullptr; |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2105 | } |
| 2106 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2107 | // Compatibility behavior: raise ANR if there is a focused application, but no focused window. |
| 2108 | // Only start counting when we have a focused event to dispatch. The ANR is canceled if we |
| 2109 | // start interacting with another application via touch (app switch). This code can be removed |
| 2110 | // if the "no focused window ANR" is moved to the policy. Input doesn't know whether |
| 2111 | // an app is expected to have a focused window. |
| 2112 | if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) { |
| 2113 | if (!mNoFocusedWindowTimeoutTime.has_value()) { |
| 2114 | // 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] | 2115 | std::chrono::nanoseconds timeout = focusedApplicationHandle->getDispatchingTimeout( |
| 2116 | DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
| 2117 | mNoFocusedWindowTimeoutTime = currentTime + timeout.count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2118 | mAwaitedFocusedApplication = focusedApplicationHandle; |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 2119 | mAwaitedApplicationDisplayId = displayId; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2120 | ALOGW("Waiting because no window has focus but %s may eventually add a " |
| 2121 | "window when it finishes starting up. Will wait for %" PRId64 "ms", |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 2122 | mAwaitedFocusedApplication->getName().c_str(), millis(timeout)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2123 | *nextWakeupTime = *mNoFocusedWindowTimeoutTime; |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2124 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 2125 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2126 | } else if (currentTime > *mNoFocusedWindowTimeoutTime) { |
| 2127 | // Already raised ANR. Drop the event |
| 2128 | ALOGE("Dropping %s event because there is no focused window", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2129 | ftl::enum_string(entry.type).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2130 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2131 | } else { |
| 2132 | // Still waiting for the focused window |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2133 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 2134 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2135 | } |
| 2136 | } |
| 2137 | |
| 2138 | // we have a valid, non-null focused window |
| 2139 | resetNoFocusedWindowTimeoutLocked(); |
| 2140 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2141 | // Verify targeted injection. |
| 2142 | if (const auto err = verifyTargetedInjection(focusedWindowHandle, entry); err) { |
| 2143 | ALOGW("Dropping injected event: %s", (*err).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2144 | outInjectionResult = InputEventInjectionResult::TARGET_MISMATCH; |
| 2145 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2146 | } |
| 2147 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2148 | if (focusedWindowHandle->getInfo()->inputConfig.test( |
| 2149 | WindowInfo::InputConfig::PAUSE_DISPATCHING)) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2150 | ALOGI("Waiting because %s is paused", focusedWindowHandle->getName().c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2151 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 2152 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2153 | } |
| 2154 | |
| 2155 | // If the event is a key event, then we must wait for all previous events to |
| 2156 | // complete before delivering it because previous events may have the |
| 2157 | // side-effect of transferring focus to a different window and we want to |
| 2158 | // ensure that the following keys are sent to the new window. |
| 2159 | // |
| 2160 | // Suppose the user touches a button in a window then immediately presses "A". |
| 2161 | // If the button causes a pop-up window to appear then we want to ensure that |
| 2162 | // the "A" key is delivered to the new pop-up window. This is because users |
| 2163 | // often anticipate pending UI changes when typing on a keyboard. |
| 2164 | // To obtain this behavior, we must serialize key events with respect to all |
| 2165 | // prior input events. |
| 2166 | if (entry.type == EventEntry::Type::KEY) { |
| 2167 | if (shouldWaitToSendKeyLocked(currentTime, focusedWindowHandle->getName().c_str())) { |
| 2168 | *nextWakeupTime = *mKeyIsWaitingForEventsTimeout; |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2169 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 2170 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2171 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2172 | } |
| 2173 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2174 | outInjectionResult = InputEventInjectionResult::SUCCEEDED; |
| 2175 | return focusedWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2176 | } |
| 2177 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2178 | /** |
| 2179 | * Given a list of monitors, remove the ones we cannot find a connection for, and the ones |
| 2180 | * that are currently unresponsive. |
| 2181 | */ |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2182 | std::vector<Monitor> InputDispatcher::selectResponsiveMonitorsLocked( |
| 2183 | const std::vector<Monitor>& monitors) const { |
| 2184 | std::vector<Monitor> responsiveMonitors; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2185 | std::copy_if(monitors.begin(), monitors.end(), std::back_inserter(responsiveMonitors), |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2186 | [this](const Monitor& monitor) REQUIRES(mLock) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 2187 | std::shared_ptr<Connection> connection = |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2188 | getConnectionLocked(monitor.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2189 | if (connection == nullptr) { |
| 2190 | ALOGE("Could not find connection for monitor %s", |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2191 | monitor.inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2192 | return false; |
| 2193 | } |
| 2194 | if (!connection->responsive) { |
| 2195 | ALOGW("Unresponsive monitor %s will not get the new gesture", |
| 2196 | connection->inputChannel->getName().c_str()); |
| 2197 | return false; |
| 2198 | } |
| 2199 | return true; |
| 2200 | }); |
| 2201 | return responsiveMonitors; |
| 2202 | } |
| 2203 | |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 2204 | /** |
| 2205 | * In general, touch should be always split between windows. Some exceptions: |
| 2206 | * 1. Don't split touch is if we have an active pointer down, and a new pointer is going down that's |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 2207 | * from the same device, *and* the window that's receiving the current pointer does not support |
| 2208 | * split touch. |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 2209 | * 2. Don't split mouse events |
| 2210 | */ |
| 2211 | bool InputDispatcher::shouldSplitTouch(const TouchState& touchState, |
| 2212 | const MotionEntry& entry) const { |
| 2213 | if (isFromSource(entry.source, AINPUT_SOURCE_MOUSE)) { |
| 2214 | // We should never split mouse events |
| 2215 | return false; |
| 2216 | } |
| 2217 | for (const TouchedWindow& touchedWindow : touchState.windows) { |
| 2218 | if (touchedWindow.windowHandle->getInfo()->isSpy()) { |
| 2219 | // Spy windows should not affect whether or not touch is split. |
| 2220 | continue; |
| 2221 | } |
| 2222 | if (touchedWindow.windowHandle->getInfo()->supportsSplitTouch()) { |
| 2223 | continue; |
| 2224 | } |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2225 | if (touchedWindow.windowHandle->getInfo()->inputConfig.test( |
| 2226 | gui::WindowInfo::InputConfig::IS_WALLPAPER)) { |
| 2227 | // Wallpaper window should not affect whether or not touch is split |
| 2228 | continue; |
| 2229 | } |
| 2230 | |
Siarhei Vishniakou | 45504fe | 2023-05-05 16:05:10 -0700 | [diff] [blame] | 2231 | if (touchedWindow.hasTouchingPointers(entry.deviceId)) { |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 2232 | return false; |
| 2233 | } |
| 2234 | } |
| 2235 | return true; |
| 2236 | } |
| 2237 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2238 | std::vector<InputTarget> InputDispatcher::findTouchedWindowTargetsLocked( |
Siarhei Vishniakou | 4fe5739 | 2022-10-25 13:44:30 -0700 | [diff] [blame] | 2239 | nsecs_t currentTime, const MotionEntry& entry, bool* outConflictingPointerActions, |
| 2240 | InputEventInjectionResult& outInjectionResult) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2241 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2242 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2243 | std::vector<InputTarget> targets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2244 | // For security reasons, we defer updating the touch state until we are sure that |
| 2245 | // event injection will be allowed. |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2246 | const int32_t displayId = entry.displayId; |
| 2247 | const int32_t action = entry.action; |
Siarhei Vishniakou | bf88052 | 2023-05-01 11:03:22 -0700 | [diff] [blame] | 2248 | const int32_t maskedAction = MotionEvent::getActionMasked(action); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2249 | |
| 2250 | // 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] | 2251 | outInjectionResult = InputEventInjectionResult::PENDING; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2252 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2253 | // Copy current touch state into tempTouchState. |
| 2254 | // This state will be used to update mTouchStatesByDisplay at the end of this function. |
| 2255 | // 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] | 2256 | const TouchState* oldState = nullptr; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2257 | TouchState tempTouchState; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2258 | if (const auto it = mTouchStatesByDisplay.find(displayId); it != mTouchStatesByDisplay.end()) { |
| 2259 | oldState = &(it->second); |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 2260 | tempTouchState = *oldState; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2261 | } |
| 2262 | |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 2263 | bool isSplit = shouldSplitTouch(tempTouchState, entry); |
Siarhei Vishniakou | 45504fe | 2023-05-05 16:05:10 -0700 | [diff] [blame] | 2264 | bool switchedDevice = false; |
| 2265 | if (oldState != nullptr) { |
| 2266 | std::set<int32_t> oldActiveDevices = oldState->getActiveDeviceIds(); |
| 2267 | const bool anotherDeviceIsActive = |
| 2268 | oldActiveDevices.count(entry.deviceId) == 0 && !oldActiveDevices.empty(); |
| 2269 | switchedDevice |= anotherDeviceIsActive; |
Siarhei Vishniakou | 45504fe | 2023-05-05 16:05:10 -0700 | [diff] [blame] | 2270 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2271 | |
| 2272 | const bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE || |
| 2273 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2274 | maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2275 | // A DOWN could be generated from POINTER_DOWN if the initial pointers did not land into any |
| 2276 | // touchable windows. |
| 2277 | const bool wasDown = oldState != nullptr && oldState->isDown(); |
| 2278 | const bool isDown = (maskedAction == AMOTION_EVENT_ACTION_DOWN) || |
| 2279 | (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN && !wasDown); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2280 | const bool newGesture = isDown || maskedAction == AMOTION_EVENT_ACTION_SCROLL || |
| 2281 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2282 | maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE; |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 2283 | const bool isFromMouse = isFromSource(entry.source, AINPUT_SOURCE_MOUSE); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2284 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2285 | // If pointers are already down, let's finish the current gesture and ignore the new events |
| 2286 | // from another device. However, if the new event is a down event, let's cancel the current |
| 2287 | // touch and let the new one take over. |
| 2288 | if (switchedDevice && wasDown && !isDown) { |
Siarhei Vishniakou | 45504fe | 2023-05-05 16:05:10 -0700 | [diff] [blame] | 2289 | LOG(INFO) << "Dropping event because a pointer for another device " |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2290 | << " is already down in display " << displayId << ": " << entry.getDescription(); |
| 2291 | // TODO(b/211379801): test multiple simultaneous input streams. |
| 2292 | outInjectionResult = InputEventInjectionResult::FAILED; |
| 2293 | return {}; // wrong device |
| 2294 | } |
| 2295 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2296 | if (newGesture) { |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2297 | // If a new gesture is starting, clear the touch state completely. |
| 2298 | tempTouchState.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2299 | isSplit = false; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2300 | } else if (switchedDevice && maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2301 | ALOGI("Dropping move event because a pointer for a different device is already active " |
| 2302 | "in display %" PRId32, |
| 2303 | displayId); |
Siarhei Vishniakou | 16e4fa0 | 2023-02-16 17:48:56 -0800 | [diff] [blame] | 2304 | // TODO(b/211379801): test multiple simultaneous input streams. |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2305 | outInjectionResult = InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2306 | return {}; // wrong device |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2307 | } |
| 2308 | |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2309 | if (isHoverAction) { |
| 2310 | // For hover actions, we will treat 'tempTouchState' as a new state, so let's erase |
| 2311 | // all of the existing hovering pointers and recompute. |
| 2312 | tempTouchState.clearHoveringPointers(); |
| 2313 | } |
| 2314 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2315 | if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { |
| 2316 | /* 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] | 2317 | const auto [x, y] = resolveTouchedPosition(entry); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2318 | const int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2319 | // Outside targets should be added upon first dispatched DOWN event. That means, this should |
| 2320 | // 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] | 2321 | const bool isStylus = isPointerFromStylus(entry, pointerIndex); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2322 | auto [newTouchedWindowHandle, outsideTargets] = |
| 2323 | findTouchedWindowAtLocked(displayId, x, y, isStylus); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2324 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2325 | if (isDown) { |
| 2326 | targets += outsideTargets; |
| 2327 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2328 | // Handle the case where we did not find a window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2329 | if (newTouchedWindowHandle == nullptr) { |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 2330 | 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] | 2331 | // 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] | 2332 | newTouchedWindowHandle = tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2333 | } |
| 2334 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2335 | // Verify targeted injection. |
| 2336 | if (const auto err = verifyTargetedInjection(newTouchedWindowHandle, entry); err) { |
| 2337 | ALOGW("Dropping injected touch event: %s", (*err).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2338 | outInjectionResult = os::InputEventInjectionResult::TARGET_MISMATCH; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2339 | newTouchedWindowHandle = nullptr; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2340 | return {}; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2341 | } |
| 2342 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2343 | // Figure out whether splitting will be allowed for this window. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2344 | if (newTouchedWindowHandle != nullptr) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2345 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
| 2346 | // New window supports splitting, but we should never split mouse events. |
| 2347 | isSplit = !isFromMouse; |
| 2348 | } else if (isSplit) { |
| 2349 | // New window does not support splitting but we have already split events. |
| 2350 | // Ignore the new window. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2351 | newTouchedWindowHandle = nullptr; |
| 2352 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2353 | } else { |
| 2354 | // 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] | 2355 | // be delivered to a new window which supports split touch. Pointers from a mouse device |
| 2356 | // should never be split. |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 2357 | isSplit = !isFromMouse; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2358 | } |
| 2359 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2360 | std::vector<sp<WindowInfoHandle>> newTouchedWindows = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2361 | findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2362 | if (newTouchedWindowHandle != nullptr) { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2363 | // Process the foreground window first so that it is the first to receive the event. |
| 2364 | newTouchedWindows.insert(newTouchedWindows.begin(), newTouchedWindowHandle); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2365 | } |
| 2366 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2367 | if (newTouchedWindows.empty()) { |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 2368 | ALOGI("Dropping event because there is no touchable window at (%.1f, %.1f) on display " |
| 2369 | "%d.", |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2370 | x, y, displayId); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2371 | outInjectionResult = InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2372 | return {}; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2373 | } |
| 2374 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2375 | for (const sp<WindowInfoHandle>& windowHandle : newTouchedWindows) { |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 2376 | if (!canWindowReceiveMotionLocked(windowHandle, entry)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2377 | continue; |
| 2378 | } |
| 2379 | |
Siarhei Vishniakou | b681c20 | 2023-05-01 11:22:33 -0700 | [diff] [blame] | 2380 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2381 | maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) { |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2382 | const int32_t pointerId = entry.pointerProperties[0].id; |
Siarhei Vishniakou | b681c20 | 2023-05-01 11:22:33 -0700 | [diff] [blame] | 2383 | // The "windowHandle" is the target of this hovering pointer. |
| 2384 | tempTouchState.addHoveringPointerToWindow(windowHandle, entry.deviceId, pointerId); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2385 | } |
| 2386 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2387 | // Set target flags. |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2388 | ftl::Flags<InputTarget::Flags> targetFlags = InputTarget::Flags::DISPATCH_AS_IS; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2389 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2390 | if (canReceiveForegroundTouches(*windowHandle->getInfo())) { |
| 2391 | // 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] | 2392 | targetFlags |= InputTarget::Flags::FOREGROUND; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2393 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2394 | |
| 2395 | if (isSplit) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2396 | targetFlags |= InputTarget::Flags::SPLIT; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2397 | } |
| 2398 | if (isWindowObscuredAtPointLocked(windowHandle, x, y)) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2399 | targetFlags |= InputTarget::Flags::WINDOW_IS_OBSCURED; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2400 | } else if (isWindowObscuredLocked(windowHandle)) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2401 | targetFlags |= InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2402 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2403 | |
| 2404 | // Update the temporary touch state. |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 2405 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2406 | if (!isHoverAction) { |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 2407 | pointerIds.set(entry.pointerProperties[pointerIndex].id); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2408 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2409 | |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2410 | const bool isDownOrPointerDown = maskedAction == AMOTION_EVENT_ACTION_DOWN || |
| 2411 | maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN; |
| 2412 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2413 | // TODO(b/211379801): Currently, even if pointerIds are empty (hover case), we would |
| 2414 | // still add a window to the touch state. We should avoid doing that, but some of the |
| 2415 | // later checks ("at least one foreground window") rely on this in order to dispatch |
| 2416 | // the event properly, so that needs to be updated, possibly by looking at InputTargets. |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 2417 | tempTouchState.addOrUpdateWindow(windowHandle, targetFlags, entry.deviceId, pointerIds, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2418 | isDownOrPointerDown |
| 2419 | ? std::make_optional(entry.eventTime) |
| 2420 | : std::nullopt); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2421 | |
| 2422 | // If this is the pointer going down and the touched window has a wallpaper |
| 2423 | // then also add the touched wallpaper windows so they are locked in for the duration |
| 2424 | // of the touch gesture. |
| 2425 | // We do not collect wallpapers during HOVER_MOVE or SCROLL because the wallpaper |
| 2426 | // engine only supports touch events. We would need to add a mechanism similar |
| 2427 | // to View.onGenericMotionEvent to enable wallpapers to handle these events. |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2428 | if (isDownOrPointerDown) { |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2429 | if (targetFlags.test(InputTarget::Flags::FOREGROUND) && |
| 2430 | windowHandle->getInfo()->inputConfig.test( |
| 2431 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER)) { |
| 2432 | sp<WindowInfoHandle> wallpaper = findWallpaperWindowBelow(windowHandle); |
| 2433 | if (wallpaper != nullptr) { |
| 2434 | ftl::Flags<InputTarget::Flags> wallpaperFlags = |
| 2435 | InputTarget::Flags::WINDOW_IS_OBSCURED | |
| 2436 | InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED | |
| 2437 | InputTarget::Flags::DISPATCH_AS_IS; |
| 2438 | if (isSplit) { |
| 2439 | wallpaperFlags |= InputTarget::Flags::SPLIT; |
| 2440 | } |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 2441 | tempTouchState.addOrUpdateWindow(wallpaper, wallpaperFlags, entry.deviceId, |
| 2442 | pointerIds, entry.eventTime); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2443 | } |
| 2444 | } |
| 2445 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2446 | } |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 2447 | |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 2448 | // If a window is already pilfering some pointers, give it this new pointer as well and |
| 2449 | // make it pilfering. This will prevent other non-spy windows from getting this pointer, |
| 2450 | // which is a specific behaviour that we want. |
| 2451 | const int32_t pointerId = entry.pointerProperties[pointerIndex].id; |
| 2452 | for (TouchedWindow& touchedWindow : tempTouchState.windows) { |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 2453 | if (touchedWindow.hasTouchingPointer(entry.deviceId, pointerId) && |
| 2454 | touchedWindow.hasPilferingPointers(entry.deviceId)) { |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 2455 | // This window is already pilfering some pointers, and this new pointer is also |
| 2456 | // going to it. Therefore, take over this pointer and don't give it to anyone |
| 2457 | // else. |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 2458 | touchedWindow.addPilferingPointer(entry.deviceId, pointerId); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 2459 | } |
| 2460 | } |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 2461 | |
| 2462 | // Restrict all pilfered pointers to the pilfering windows. |
| 2463 | tempTouchState.cancelPointersForNonPilferingWindows(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2464 | } else { |
| 2465 | /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ |
| 2466 | |
| 2467 | // If the pointer is not currently down, then ignore the event. |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2468 | if (!tempTouchState.isDown() && maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT) { |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2469 | LOG(INFO) << "Dropping event because the pointer is not down or we previously " |
| 2470 | "dropped the pointer down event in display " |
| 2471 | << displayId << ": " << entry.getDescription(); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2472 | outInjectionResult = InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2473 | return {}; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2474 | } |
| 2475 | |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2476 | // If the pointer is not currently hovering, then ignore the event. |
| 2477 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 2478 | const int32_t pointerId = entry.pointerProperties[0].id; |
| 2479 | if (oldState == nullptr || |
| 2480 | oldState->getWindowsWithHoveringPointer(entry.deviceId, pointerId).empty()) { |
| 2481 | LOG(INFO) << "Dropping event because the hovering pointer is not in any windows in " |
| 2482 | "display " |
| 2483 | << displayId << ": " << entry.getDescription(); |
| 2484 | outInjectionResult = InputEventInjectionResult::FAILED; |
| 2485 | return {}; |
| 2486 | } |
| 2487 | tempTouchState.removeHoveringPointer(entry.deviceId, pointerId); |
| 2488 | } |
| 2489 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2490 | addDragEventLocked(entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2491 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2492 | // Check whether touches should slip outside of the current foreground window. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2493 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.pointerCount == 1 && |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2494 | tempTouchState.isSlippery()) { |
Siarhei Vishniakou | 9306c38 | 2022-09-30 15:30:31 -0700 | [diff] [blame] | 2495 | const auto [x, y] = resolveTouchedPosition(entry); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2496 | const bool isStylus = isPointerFromStylus(entry, /*pointerIndex=*/0); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2497 | sp<WindowInfoHandle> oldTouchedWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2498 | tempTouchState.getFirstForegroundWindowHandle(); |
Siarhei Vishniakou | 0f6558d | 2023-04-21 12:05:13 -0700 | [diff] [blame] | 2499 | LOG_ALWAYS_FATAL_IF(oldTouchedWindowHandle == nullptr); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2500 | auto [newTouchedWindowHandle, _] = findTouchedWindowAtLocked(displayId, x, y, isStylus); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2501 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2502 | // Verify targeted injection. |
| 2503 | if (const auto err = verifyTargetedInjection(newTouchedWindowHandle, entry); err) { |
| 2504 | ALOGW("Dropping injected event: %s", (*err).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2505 | outInjectionResult = os::InputEventInjectionResult::TARGET_MISMATCH; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2506 | return {}; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2507 | } |
| 2508 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2509 | // Drop touch events if requested by input feature |
| 2510 | if (newTouchedWindowHandle != nullptr && |
| 2511 | shouldDropInput(entry, newTouchedWindowHandle)) { |
| 2512 | newTouchedWindowHandle = nullptr; |
| 2513 | } |
| 2514 | |
Siarhei Vishniakou | afa08cc | 2023-05-08 22:35:50 -0700 | [diff] [blame] | 2515 | if (newTouchedWindowHandle != nullptr && |
| 2516 | !haveSameToken(oldTouchedWindowHandle, newTouchedWindowHandle)) { |
Siarhei Vishniakou | 0f6558d | 2023-04-21 12:05:13 -0700 | [diff] [blame] | 2517 | ALOGD("Touch is slipping out of window %s into window %s in display %" PRId32, |
| 2518 | oldTouchedWindowHandle->getName().c_str(), |
| 2519 | newTouchedWindowHandle->getName().c_str(), displayId); |
| 2520 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2521 | // Make a slippery exit from the old window. |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 2522 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2523 | const int32_t pointerId = entry.pointerProperties[0].id; |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 2524 | pointerIds.set(pointerId); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2525 | |
| 2526 | const TouchedWindow& touchedWindow = |
| 2527 | tempTouchState.getTouchedWindow(oldTouchedWindowHandle); |
| 2528 | addWindowTargetLocked(oldTouchedWindowHandle, |
| 2529 | InputTarget::Flags::DISPATCH_AS_SLIPPERY_EXIT, pointerIds, |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 2530 | touchedWindow.getDownTimeInTarget(entry.deviceId), targets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2531 | |
| 2532 | // Make a slippery entrance into the new window. |
| 2533 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
Prabir Pradhan | 713bb3e | 2021-12-20 02:07:40 -0800 | [diff] [blame] | 2534 | isSplit = !isFromMouse; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2535 | } |
| 2536 | |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2537 | ftl::Flags<InputTarget::Flags> targetFlags = |
| 2538 | InputTarget::Flags::DISPATCH_AS_SLIPPERY_ENTER; |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2539 | if (canReceiveForegroundTouches(*newTouchedWindowHandle->getInfo())) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2540 | targetFlags |= InputTarget::Flags::FOREGROUND; |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2541 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2542 | if (isSplit) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2543 | targetFlags |= InputTarget::Flags::SPLIT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2544 | } |
| 2545 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2546 | targetFlags |= InputTarget::Flags::WINDOW_IS_OBSCURED; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2547 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2548 | targetFlags |= InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2549 | } |
| 2550 | |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 2551 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, |
| 2552 | entry.deviceId, pointerIds, entry.eventTime); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2553 | |
| 2554 | // Check if the wallpaper window should deliver the corresponding event. |
| 2555 | slipWallpaperTouch(targetFlags, oldTouchedWindowHandle, newTouchedWindowHandle, |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 2556 | tempTouchState, entry.deviceId, pointerId, targets); |
| 2557 | tempTouchState.removeTouchingPointerFromWindow(entry.deviceId, pointerId, |
| 2558 | oldTouchedWindowHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2559 | } |
| 2560 | } |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 2561 | |
| 2562 | // Update the pointerIds for non-splittable when it received pointer down. |
| 2563 | if (!isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN) { |
| 2564 | // If no split, we suppose all touched windows should receive pointer down. |
| 2565 | const int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
| 2566 | for (size_t i = 0; i < tempTouchState.windows.size(); i++) { |
| 2567 | TouchedWindow& touchedWindow = tempTouchState.windows[i]; |
| 2568 | // Ignore drag window for it should just track one pointer. |
| 2569 | if (mDragState && mDragState->dragWindow == touchedWindow.windowHandle) { |
| 2570 | continue; |
| 2571 | } |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 2572 | touchedWindow.addTouchingPointer(entry.deviceId, |
| 2573 | entry.pointerProperties[pointerIndex].id); |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 2574 | } |
| 2575 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2576 | } |
| 2577 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2578 | // Update dispatching for hover enter and exit. |
Sam Dubey | f886dec | 2023-01-27 13:28:19 +0000 | [diff] [blame] | 2579 | { |
| 2580 | std::vector<TouchedWindow> hoveringWindows = |
| 2581 | getHoveringWindowsLocked(oldState, tempTouchState, entry); |
| 2582 | for (const TouchedWindow& touchedWindow : hoveringWindows) { |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 2583 | std::optional<InputTarget> target = |
| 2584 | createInputTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags, |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 2585 | touchedWindow.getDownTimeInTarget(entry.deviceId)); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 2586 | if (!target) { |
| 2587 | continue; |
| 2588 | } |
| 2589 | // Hardcode to single hovering pointer for now. |
| 2590 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
| 2591 | pointerIds.set(entry.pointerProperties[0].id); |
| 2592 | target->addPointers(pointerIds, touchedWindow.windowHandle->getInfo()->transform); |
| 2593 | targets.push_back(*target); |
Sam Dubey | f886dec | 2023-01-27 13:28:19 +0000 | [diff] [blame] | 2594 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2595 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2596 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2597 | // Ensure that all touched windows are valid for injection. |
| 2598 | if (entry.injectionState != nullptr) { |
| 2599 | std::string errs; |
| 2600 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2601 | const auto err = verifyTargetedInjection(touchedWindow.windowHandle, entry); |
| 2602 | if (err) errs += "\n - " + *err; |
| 2603 | } |
| 2604 | if (!errs.empty()) { |
| 2605 | ALOGW("Dropping targeted injection: At least one touched window is not owned by uid " |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 2606 | "%s:%s", |
| 2607 | entry.injectionState->targetUid->toString().c_str(), errs.c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2608 | outInjectionResult = InputEventInjectionResult::TARGET_MISMATCH; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2609 | return {}; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2610 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2611 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2612 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2613 | // Check whether windows listening for outside touches are owned by the same UID. If the owner |
| 2614 | // 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] | 2615 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2616 | sp<WindowInfoHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2617 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2618 | if (foregroundWindowHandle) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 2619 | const auto foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2620 | for (InputTarget& target : targets) { |
| 2621 | if (target.flags.test(InputTarget::Flags::DISPATCH_AS_OUTSIDE)) { |
| 2622 | sp<WindowInfoHandle> targetWindow = |
| 2623 | getWindowHandleLocked(target.inputChannel->getConnectionToken()); |
| 2624 | if (targetWindow->getInfo()->ownerUid != foregroundWindowUid) { |
| 2625 | target.flags |= InputTarget::Flags::ZERO_COORDS; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2626 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2627 | } |
| 2628 | } |
| 2629 | } |
| 2630 | } |
| 2631 | |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 2632 | // If this is a touchpad navigation gesture, it needs to only be sent to trusted targets, as we |
| 2633 | // only want the system UI to handle these gestures. |
| 2634 | const bool isTouchpadNavGesture = isFromSource(entry.source, AINPUT_SOURCE_MOUSE) && |
| 2635 | entry.classification == MotionClassification::MULTI_FINGER_SWIPE; |
| 2636 | if (isTouchpadNavGesture) { |
| 2637 | filterUntrustedTargets(/* byref */ tempTouchState, /* byref */ targets); |
| 2638 | } |
| 2639 | |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2640 | // Output targets from the touch state. |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2641 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 2642 | if (!touchedWindow.hasTouchingPointers(entry.deviceId) && |
| 2643 | !touchedWindow.hasHoveringPointers(entry.deviceId)) { |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2644 | // Windows with hovering pointers are getting persisted inside TouchState. |
| 2645 | // Do not send this event to those windows. |
| 2646 | continue; |
| 2647 | } |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 2648 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2649 | addWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags, |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 2650 | touchedWindow.getTouchingPointers(entry.deviceId), |
| 2651 | touchedWindow.getDownTimeInTarget(entry.deviceId), targets); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2652 | } |
Sam Dubey | 39d37cf | 2022-12-07 18:05:35 +0000 | [diff] [blame] | 2653 | |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 2654 | // During targeted injection, only allow owned targets to receive events |
| 2655 | std::erase_if(targets, [&](const InputTarget& target) { |
| 2656 | LOG_ALWAYS_FATAL_IF(target.windowHandle == nullptr); |
| 2657 | const auto err = verifyTargetedInjection(target.windowHandle, entry); |
| 2658 | if (err) { |
| 2659 | LOG(WARNING) << "Dropping injected event from " << target.windowHandle->getName() |
| 2660 | << ": " << (*err); |
| 2661 | return true; |
| 2662 | } |
| 2663 | return false; |
| 2664 | }); |
| 2665 | |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2666 | if (targets.empty()) { |
| 2667 | LOG(INFO) << "Dropping event because no targets were found: " << entry.getDescription(); |
| 2668 | outInjectionResult = InputEventInjectionResult::FAILED; |
| 2669 | return {}; |
| 2670 | } |
| 2671 | |
| 2672 | // If we only have windows getting ACTION_OUTSIDE, then drop the event, because there is no |
| 2673 | // window that is actually receiving the entire gesture. |
| 2674 | if (std::all_of(targets.begin(), targets.end(), [](const InputTarget& target) { |
| 2675 | return target.flags.test(InputTarget::Flags::DISPATCH_AS_OUTSIDE); |
| 2676 | })) { |
| 2677 | LOG(INFO) << "Dropping event because all windows would just receive ACTION_OUTSIDE: " |
| 2678 | << entry.getDescription(); |
| 2679 | outInjectionResult = InputEventInjectionResult::FAILED; |
| 2680 | return {}; |
| 2681 | } |
| 2682 | |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2683 | outInjectionResult = InputEventInjectionResult::SUCCEEDED; |
Sam Dubey | f886dec | 2023-01-27 13:28:19 +0000 | [diff] [blame] | 2684 | // Drop the outside or hover touch windows since we will not care about them |
| 2685 | // in the next iteration. |
| 2686 | tempTouchState.filterNonAsIsTouchWindows(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2687 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2688 | // Update final pieces of touch state if the injector had permission. |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2689 | if (switchedDevice) { |
| 2690 | if (DEBUG_FOCUS) { |
| 2691 | ALOGD("Conflicting pointer actions: Switched to a different device."); |
| 2692 | } |
| 2693 | *outConflictingPointerActions = true; |
| 2694 | } |
| 2695 | |
| 2696 | if (isHoverAction) { |
| 2697 | // Started hovering, therefore no longer down. |
Siarhei Vishniakou | 3ad385b | 2022-11-04 10:09:53 -0700 | [diff] [blame] | 2698 | if (oldState && oldState->isDown()) { |
Siarhei Vishniakou | 6e1e987 | 2022-11-08 17:51:35 -0800 | [diff] [blame] | 2699 | ALOGD_IF(DEBUG_FOCUS, |
| 2700 | "Conflicting pointer actions: Hover received while pointer was down."); |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2701 | *outConflictingPointerActions = true; |
| 2702 | } |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2703 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2704 | maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2705 | } |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2706 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP) { |
| 2707 | // Pointer went up. |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 2708 | tempTouchState.removeTouchingPointer(entry.deviceId, entry.pointerProperties[0].id); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2709 | } else if (maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2710 | // All pointers up or canceled. |
| 2711 | tempTouchState.reset(); |
| 2712 | } else if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 2713 | // First pointer went down. |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2714 | if (oldState && (oldState->isDown() || oldState->hasHoveringPointers())) { |
| 2715 | ALOGD("Conflicting pointer actions: Down received while already down or hovering."); |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2716 | *outConflictingPointerActions = true; |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2717 | } |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2718 | } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 2719 | // One pointer went up. |
| 2720 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
| 2721 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2722 | |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2723 | for (size_t i = 0; i < tempTouchState.windows.size();) { |
| 2724 | TouchedWindow& touchedWindow = tempTouchState.windows[i]; |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 2725 | touchedWindow.removeTouchingPointer(entry.deviceId, pointerId); |
| 2726 | if (!touchedWindow.hasTouchingPointers(entry.deviceId)) { |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2727 | tempTouchState.windows.erase(tempTouchState.windows.begin() + i); |
| 2728 | continue; |
| 2729 | } |
| 2730 | i += 1; |
| 2731 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2732 | } |
| 2733 | |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2734 | // Save changes unless the action was scroll in which case the temporary touch |
| 2735 | // state was only valid for this one action. |
| 2736 | if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) { |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 2737 | if (displayId >= 0) { |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2738 | tempTouchState.clearWindowsWithoutPointers(); |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2739 | mTouchStatesByDisplay[displayId] = tempTouchState; |
| 2740 | } else { |
| 2741 | mTouchStatesByDisplay.erase(displayId); |
| 2742 | } |
| 2743 | } |
| 2744 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 2745 | if (tempTouchState.windows.empty()) { |
| 2746 | mTouchStatesByDisplay.erase(displayId); |
| 2747 | } |
| 2748 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2749 | return targets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2750 | } |
| 2751 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2752 | void InputDispatcher::finishDragAndDrop(int32_t displayId, float x, float y) { |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2753 | // Prevent stylus interceptor windows from affecting drag and drop behavior for now, until we |
| 2754 | // have an explicit reason to support it. |
| 2755 | constexpr bool isStylus = false; |
| 2756 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2757 | auto [dropWindow, _] = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2758 | findTouchedWindowAtLocked(displayId, x, y, isStylus, /*ignoreDragWindow=*/true); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2759 | if (dropWindow) { |
| 2760 | vec2 local = dropWindow->getInfo()->transform.transform(x, y); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2761 | sendDropWindowCommandLocked(dropWindow->getToken(), local.x, local.y); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 2762 | } else { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2763 | ALOGW("No window found when drop."); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2764 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2765 | } |
| 2766 | mDragState.reset(); |
| 2767 | } |
| 2768 | |
| 2769 | void InputDispatcher::addDragEventLocked(const MotionEntry& entry) { |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 2770 | if (!mDragState || mDragState->dragWindow->getInfo()->displayId != entry.displayId) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2771 | return; |
| 2772 | } |
| 2773 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2774 | if (!mDragState->isStartDrag) { |
| 2775 | mDragState->isStartDrag = true; |
| 2776 | mDragState->isStylusButtonDownAtStart = |
| 2777 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2778 | } |
| 2779 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2780 | // Find the pointer index by id. |
| 2781 | int32_t pointerIndex = 0; |
| 2782 | for (; static_cast<uint32_t>(pointerIndex) < entry.pointerCount; pointerIndex++) { |
| 2783 | const PointerProperties& pointerProperties = entry.pointerProperties[pointerIndex]; |
| 2784 | if (pointerProperties.id == mDragState->pointerId) { |
| 2785 | break; |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2786 | } |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2787 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2788 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2789 | if (uint32_t(pointerIndex) == entry.pointerCount) { |
| 2790 | 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] | 2791 | } |
| 2792 | |
| 2793 | const int32_t maskedAction = entry.action & AMOTION_EVENT_ACTION_MASK; |
| 2794 | const int32_t x = entry.pointerCoords[pointerIndex].getX(); |
| 2795 | const int32_t y = entry.pointerCoords[pointerIndex].getY(); |
| 2796 | |
| 2797 | switch (maskedAction) { |
| 2798 | case AMOTION_EVENT_ACTION_MOVE: { |
| 2799 | // Handle the special case : stylus button no longer pressed. |
| 2800 | bool isStylusButtonDown = |
| 2801 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2802 | if (mDragState->isStylusButtonDownAtStart && !isStylusButtonDown) { |
| 2803 | finishDragAndDrop(entry.displayId, x, y); |
| 2804 | return; |
| 2805 | } |
| 2806 | |
| 2807 | // Prevent stylus interceptor windows from affecting drag and drop behavior for now, |
| 2808 | // until we have an explicit reason to support it. |
| 2809 | constexpr bool isStylus = false; |
| 2810 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2811 | auto [hoverWindowHandle, _] = findTouchedWindowAtLocked(entry.displayId, x, y, isStylus, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2812 | /*ignoreDragWindow=*/true); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2813 | // enqueue drag exit if needed. |
| 2814 | if (hoverWindowHandle != mDragState->dragHoverWindowHandle && |
| 2815 | !haveSameToken(hoverWindowHandle, mDragState->dragHoverWindowHandle)) { |
| 2816 | if (mDragState->dragHoverWindowHandle != nullptr) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2817 | enqueueDragEventLocked(mDragState->dragHoverWindowHandle, /*isExiting=*/true, x, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2818 | y); |
| 2819 | } |
| 2820 | mDragState->dragHoverWindowHandle = hoverWindowHandle; |
| 2821 | } |
| 2822 | // enqueue drag location if needed. |
| 2823 | if (hoverWindowHandle != nullptr) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2824 | enqueueDragEventLocked(hoverWindowHandle, /*isExiting=*/false, x, y); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2825 | } |
| 2826 | break; |
| 2827 | } |
| 2828 | |
| 2829 | case AMOTION_EVENT_ACTION_POINTER_UP: |
| 2830 | if (getMotionEventActionPointerIndex(entry.action) != pointerIndex) { |
| 2831 | break; |
| 2832 | } |
| 2833 | // The drag pointer is up. |
| 2834 | [[fallthrough]]; |
| 2835 | case AMOTION_EVENT_ACTION_UP: |
| 2836 | finishDragAndDrop(entry.displayId, x, y); |
| 2837 | break; |
| 2838 | case AMOTION_EVENT_ACTION_CANCEL: { |
| 2839 | ALOGD("Receiving cancel when drag and drop."); |
| 2840 | sendDropWindowCommandLocked(nullptr, 0, 0); |
| 2841 | mDragState.reset(); |
| 2842 | break; |
| 2843 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2844 | } |
| 2845 | } |
| 2846 | |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2847 | std::optional<InputTarget> InputDispatcher::createInputTargetLocked( |
| 2848 | const sp<android::gui::WindowInfoHandle>& windowHandle, |
| 2849 | ftl::Flags<InputTarget::Flags> targetFlags, |
| 2850 | std::optional<nsecs_t> firstDownTimeInTarget) const { |
| 2851 | std::shared_ptr<InputChannel> inputChannel = getInputChannelLocked(windowHandle->getToken()); |
| 2852 | if (inputChannel == nullptr) { |
| 2853 | ALOGW("Not creating InputTarget for %s, no input channel", windowHandle->getName().c_str()); |
| 2854 | return {}; |
| 2855 | } |
| 2856 | InputTarget inputTarget; |
| 2857 | inputTarget.inputChannel = inputChannel; |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 2858 | inputTarget.windowHandle = windowHandle; |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2859 | inputTarget.flags = targetFlags; |
| 2860 | inputTarget.globalScaleFactor = windowHandle->getInfo()->globalScaleFactor; |
| 2861 | inputTarget.firstDownTimeInTarget = firstDownTimeInTarget; |
| 2862 | const auto& displayInfoIt = mDisplayInfos.find(windowHandle->getInfo()->displayId); |
| 2863 | if (displayInfoIt != mDisplayInfos.end()) { |
| 2864 | inputTarget.displayTransform = displayInfoIt->second.transform; |
| 2865 | } else { |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 2866 | // DisplayInfo not found for this window on display windowHandle->getInfo()->displayId. |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2867 | // TODO(b/198444055): Make this an error message after 'setInputWindows' API is removed. |
| 2868 | } |
| 2869 | return inputTarget; |
| 2870 | } |
| 2871 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2872 | void InputDispatcher::addWindowTargetLocked(const sp<WindowInfoHandle>& windowHandle, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2873 | ftl::Flags<InputTarget::Flags> targetFlags, |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 2874 | std::bitset<MAX_POINTER_ID + 1> pointerIds, |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2875 | std::optional<nsecs_t> firstDownTimeInTarget, |
Siarhei Vishniakou | f75cddb | 2022-10-25 10:42:16 -0700 | [diff] [blame] | 2876 | std::vector<InputTarget>& inputTargets) const { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2877 | std::vector<InputTarget>::iterator it = |
| 2878 | std::find_if(inputTargets.begin(), inputTargets.end(), |
| 2879 | [&windowHandle](const InputTarget& inputTarget) { |
| 2880 | return inputTarget.inputChannel->getConnectionToken() == |
| 2881 | windowHandle->getToken(); |
| 2882 | }); |
Chavi Weingarten | 97b8eec | 2020-01-09 18:09:08 +0000 | [diff] [blame] | 2883 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2884 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2885 | |
| 2886 | if (it == inputTargets.end()) { |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2887 | std::optional<InputTarget> target = |
| 2888 | createInputTargetLocked(windowHandle, targetFlags, firstDownTimeInTarget); |
| 2889 | if (!target) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2890 | return; |
| 2891 | } |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2892 | inputTargets.push_back(*target); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2893 | it = inputTargets.end() - 1; |
| 2894 | } |
| 2895 | |
| 2896 | ALOG_ASSERT(it->flags == targetFlags); |
| 2897 | ALOG_ASSERT(it->globalScaleFactor == windowInfo->globalScaleFactor); |
| 2898 | |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2899 | it->addPointers(pointerIds, windowInfo->transform); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2900 | } |
| 2901 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2902 | void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2903 | int32_t displayId) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2904 | auto monitorsIt = mGlobalMonitorsByDisplay.find(displayId); |
| 2905 | if (monitorsIt == mGlobalMonitorsByDisplay.end()) return; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2906 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2907 | for (const Monitor& monitor : selectResponsiveMonitorsLocked(monitorsIt->second)) { |
| 2908 | InputTarget target; |
| 2909 | target.inputChannel = monitor.inputChannel; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2910 | target.flags = InputTarget::Flags::DISPATCH_AS_IS; |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2911 | // target.firstDownTimeInTarget is not set for global monitors. It is only required in split |
| 2912 | // touch and global monitoring works as intended even without setting firstDownTimeInTarget |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2913 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
| 2914 | target.displayTransform = it->second.transform; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2915 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2916 | target.setDefaultPointerTransform(target.displayTransform); |
| 2917 | inputTargets.push_back(target); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2918 | } |
| 2919 | } |
| 2920 | |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2921 | /** |
| 2922 | * Indicate whether one window handle should be considered as obscuring |
| 2923 | * another window handle. We only check a few preconditions. Actually |
| 2924 | * checking the bounds is left to the caller. |
| 2925 | */ |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2926 | static bool canBeObscuredBy(const sp<WindowInfoHandle>& windowHandle, |
| 2927 | const sp<WindowInfoHandle>& otherHandle) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2928 | // Compare by token so cloned layers aren't counted |
| 2929 | if (haveSameToken(windowHandle, otherHandle)) { |
| 2930 | return false; |
| 2931 | } |
| 2932 | auto info = windowHandle->getInfo(); |
| 2933 | auto otherInfo = otherHandle->getInfo(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2934 | if (otherInfo->inputConfig.test(WindowInfo::InputConfig::NOT_VISIBLE)) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2935 | return false; |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2936 | } else if (otherInfo->alpha == 0 && |
| 2937 | otherInfo->inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE)) { |
Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame] | 2938 | // Those act as if they were invisible, so we don't need to flag them. |
| 2939 | // We do want to potentially flag touchable windows even if they have 0 |
| 2940 | // opacity, since they can consume touches and alter the effects of the |
| 2941 | // user interaction (eg. apps that rely on |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2942 | // Flags::WINDOW_IS_PARTIALLY_OBSCURED should still be told about those |
Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame] | 2943 | // windows), hence we also check for FLAG_NOT_TOUCHABLE. |
| 2944 | return false; |
Bernardo Rufino | 8007daf | 2020-09-22 09:40:01 +0000 | [diff] [blame] | 2945 | } else if (info->ownerUid == otherInfo->ownerUid) { |
| 2946 | // If ownerUid is the same we don't generate occlusion events as there |
| 2947 | // is no security boundary within an uid. |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2948 | return false; |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2949 | } else if (otherInfo->inputConfig.test(gui::WindowInfo::InputConfig::TRUSTED_OVERLAY)) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2950 | return false; |
| 2951 | } else if (otherInfo->displayId != info->displayId) { |
| 2952 | return false; |
| 2953 | } |
| 2954 | return true; |
| 2955 | } |
| 2956 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2957 | /** |
| 2958 | * Returns touch occlusion information in the form of TouchOcclusionInfo. To check if the touch is |
| 2959 | * untrusted, one should check: |
| 2960 | * |
| 2961 | * 1. If result.hasBlockingOcclusion is true. |
| 2962 | * If it's, it means the touch should be blocked due to a window with occlusion mode of |
| 2963 | * BLOCK_UNTRUSTED. |
| 2964 | * |
| 2965 | * 2. If result.obscuringOpacity > mMaximumObscuringOpacityForTouch. |
| 2966 | * If it is (and 1 is false), then the touch should be blocked because a stack of windows |
| 2967 | * (possibly only one) with occlusion mode of USE_OPACITY from one UID resulted in a composed |
| 2968 | * obscuring opacity above the threshold. Note that if there was no window of occlusion mode |
| 2969 | * USE_OPACITY, result.obscuringOpacity would've been 0 and since |
| 2970 | * mMaximumObscuringOpacityForTouch >= 0, the condition above would never be true. |
| 2971 | * |
| 2972 | * If neither of those is true, then it means the touch can be allowed. |
| 2973 | */ |
| 2974 | InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2975 | const sp<WindowInfoHandle>& windowHandle, int32_t x, int32_t y) const { |
| 2976 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2977 | int32_t displayId = windowInfo->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2978 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2979 | TouchOcclusionInfo info; |
| 2980 | info.hasBlockingOcclusion = false; |
| 2981 | info.obscuringOpacity = 0; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 2982 | info.obscuringUid = gui::Uid::INVALID; |
| 2983 | std::map<gui::Uid, float> opacityByUid; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2984 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2985 | if (windowHandle == otherHandle) { |
| 2986 | break; // All future windows are below us. Exit early. |
| 2987 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2988 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 2989 | if (canBeObscuredBy(windowHandle, otherHandle) && otherInfo->frameContainsPoint(x, y) && |
| 2990 | !haveSameApplicationToken(windowInfo, otherInfo)) { |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2991 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2992 | info.debugInfo.push_back( |
| 2993 | dumpWindowForTouchOcclusion(otherInfo, /* isTouchedWindow */ false)); |
| 2994 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2995 | // canBeObscuredBy() has returned true above, which means this window is untrusted, so |
| 2996 | // we perform the checks below to see if the touch can be propagated or not based on the |
| 2997 | // window's touch occlusion mode |
| 2998 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::BLOCK_UNTRUSTED) { |
| 2999 | info.hasBlockingOcclusion = true; |
| 3000 | info.obscuringUid = otherInfo->ownerUid; |
| 3001 | info.obscuringPackage = otherInfo->packageName; |
| 3002 | break; |
| 3003 | } |
| 3004 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::USE_OPACITY) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3005 | const auto uid = otherInfo->ownerUid; |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3006 | float opacity = |
| 3007 | (opacityByUid.find(uid) == opacityByUid.end()) ? 0 : opacityByUid[uid]; |
| 3008 | // Given windows A and B: |
| 3009 | // opacity(A, B) = 1 - [1 - opacity(A)] * [1 - opacity(B)] |
| 3010 | opacity = 1 - (1 - opacity) * (1 - otherInfo->alpha); |
| 3011 | opacityByUid[uid] = opacity; |
| 3012 | if (opacity > info.obscuringOpacity) { |
| 3013 | info.obscuringOpacity = opacity; |
| 3014 | info.obscuringUid = uid; |
| 3015 | info.obscuringPackage = otherInfo->packageName; |
| 3016 | } |
| 3017 | } |
| 3018 | } |
| 3019 | } |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 3020 | if (DEBUG_TOUCH_OCCLUSION) { |
| 3021 | info.debugInfo.push_back( |
| 3022 | dumpWindowForTouchOcclusion(windowInfo, /* isTouchedWindow */ true)); |
| 3023 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3024 | return info; |
| 3025 | } |
| 3026 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3027 | std::string InputDispatcher::dumpWindowForTouchOcclusion(const WindowInfo* info, |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 3028 | bool isTouchedWindow) const { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3029 | return StringPrintf(INDENT2 "* %spackage=%s/%s, id=%" PRId32 ", mode=%s, alpha=%.2f, " |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 3030 | "frame=[%" PRId32 ",%" PRId32 "][%" PRId32 ",%" PRId32 |
| 3031 | "], touchableRegion=%s, window={%s}, inputConfig={%s}, " |
| 3032 | "hasToken=%s, applicationInfo.name=%s, applicationInfo.token=%s\n", |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 3033 | isTouchedWindow ? "[TOUCHED] " : "", info->packageName.c_str(), |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3034 | info->ownerUid.toString().c_str(), info->id, |
| 3035 | toString(info->touchOcclusionMode).c_str(), info->alpha, info->frameLeft, |
| 3036 | info->frameTop, info->frameRight, info->frameBottom, |
| 3037 | dumpRegion(info->touchableRegion).c_str(), info->name.c_str(), |
| 3038 | info->inputConfig.string().c_str(), toString(info->token != nullptr), |
| 3039 | info->applicationInfo.name.c_str(), |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 3040 | binderToString(info->applicationInfo.token).c_str()); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 3041 | } |
| 3042 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3043 | bool InputDispatcher::isTouchTrustedLocked(const TouchOcclusionInfo& occlusionInfo) const { |
| 3044 | if (occlusionInfo.hasBlockingOcclusion) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3045 | ALOGW("Untrusted touch due to occlusion by %s/%s", occlusionInfo.obscuringPackage.c_str(), |
| 3046 | occlusionInfo.obscuringUid.toString().c_str()); |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3047 | return false; |
| 3048 | } |
| 3049 | if (occlusionInfo.obscuringOpacity > mMaximumObscuringOpacityForTouch) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3050 | ALOGW("Untrusted touch due to occlusion by %s/%s (obscuring opacity = " |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3051 | "%.2f, maximum allowed = %.2f)", |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3052 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid.toString().c_str(), |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3053 | occlusionInfo.obscuringOpacity, mMaximumObscuringOpacityForTouch); |
| 3054 | return false; |
| 3055 | } |
| 3056 | return true; |
| 3057 | } |
| 3058 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3059 | bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3060 | int32_t x, int32_t y) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3061 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3062 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 3063 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3064 | if (windowHandle == otherHandle) { |
| 3065 | break; // All future windows are below us. Exit early. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3066 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3067 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3068 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 3069 | otherInfo->frameContainsPoint(x, y)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3070 | return true; |
| 3071 | } |
| 3072 | } |
| 3073 | return false; |
| 3074 | } |
| 3075 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3076 | bool InputDispatcher::isWindowObscuredLocked(const sp<WindowInfoHandle>& windowHandle) const { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 3077 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3078 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 3079 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 3080 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3081 | if (windowHandle == otherHandle) { |
| 3082 | break; // All future windows are below us. Exit early. |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 3083 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3084 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3085 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 3086 | otherInfo->overlaps(windowInfo)) { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 3087 | return true; |
| 3088 | } |
| 3089 | } |
| 3090 | return false; |
| 3091 | } |
| 3092 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 3093 | std::string InputDispatcher::getApplicationWindowLabel( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3094 | const InputApplicationHandle* applicationHandle, const sp<WindowInfoHandle>& windowHandle) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3095 | if (applicationHandle != nullptr) { |
| 3096 | if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 3097 | return applicationHandle->getName() + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3098 | } else { |
| 3099 | return applicationHandle->getName(); |
| 3100 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3101 | } else if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 3102 | return windowHandle->getInfo()->applicationInfo.name + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3103 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3104 | return "<unknown application or window>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3105 | } |
| 3106 | } |
| 3107 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3108 | void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3109 | if (!isUserActivityEvent(eventEntry)) { |
| 3110 | // 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] | 3111 | return; |
| 3112 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3113 | int32_t displayId = getTargetDisplayId(eventEntry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3114 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 3115 | const WindowInfo* windowDisablingUserActivityInfo = nullptr; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3116 | if (focusedWindowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3117 | const WindowInfo* info = focusedWindowHandle->getInfo(); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 3118 | if (info->inputConfig.test(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY)) { |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 3119 | windowDisablingUserActivityInfo = info; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3120 | } |
| 3121 | } |
| 3122 | |
| 3123 | int32_t eventType = USER_ACTIVITY_EVENT_OTHER; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3124 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3125 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3126 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 3127 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3128 | return; |
| 3129 | } |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 3130 | if (windowDisablingUserActivityInfo != nullptr) { |
| 3131 | if (DEBUG_DISPATCH_CYCLE) { |
| 3132 | ALOGD("Not poking user activity: disabled by window '%s'.", |
| 3133 | windowDisablingUserActivityInfo->name.c_str()); |
| 3134 | } |
| 3135 | return; |
| 3136 | } |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3137 | if (MotionEvent::isTouchEvent(motionEntry.source, motionEntry.action)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3138 | eventType = USER_ACTIVITY_EVENT_TOUCH; |
| 3139 | } |
| 3140 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3141 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3142 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3143 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 3144 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3145 | return; |
| 3146 | } |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 3147 | // If the key code is unknown, we don't consider it user activity |
| 3148 | if (keyEntry.keyCode == AKEYCODE_UNKNOWN) { |
| 3149 | return; |
| 3150 | } |
| 3151 | // Don't inhibit events that were intercepted or are not passed to |
| 3152 | // the apps, like system shortcuts |
| 3153 | if (windowDisablingUserActivityInfo != nullptr && |
| 3154 | keyEntry.interceptKeyResult != KeyEntry::InterceptKeyResult::SKIP && |
| 3155 | keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER) { |
| 3156 | if (DEBUG_DISPATCH_CYCLE) { |
| 3157 | ALOGD("Not poking user activity: disabled by window '%s'.", |
| 3158 | windowDisablingUserActivityInfo->name.c_str()); |
| 3159 | } |
| 3160 | return; |
| 3161 | } |
| 3162 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3163 | eventType = USER_ACTIVITY_EVENT_BUTTON; |
| 3164 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3165 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3166 | default: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3167 | LOG_ALWAYS_FATAL("%s events are not user activity", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3168 | ftl::enum_string(eventEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3169 | break; |
| 3170 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3171 | } |
| 3172 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3173 | auto command = [this, eventTime = eventEntry.eventTime, eventType, displayId]() |
| 3174 | REQUIRES(mLock) { |
| 3175 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 3176 | mPolicy.pokeUserActivity(eventTime, eventType, displayId); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3177 | }; |
| 3178 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3179 | } |
| 3180 | |
| 3181 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3182 | const std::shared_ptr<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3183 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3184 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3185 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3186 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3187 | StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3188 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3189 | ATRACE_NAME(message.c_str()); |
| 3190 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3191 | if (DEBUG_DISPATCH_CYCLE) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3192 | ALOGD("channel '%s' ~ prepareDispatchCycle - flags=%s, " |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 3193 | "globalScaleFactor=%f, pointerIds=%s %s", |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3194 | connection->getInputChannelName().c_str(), inputTarget.flags.string().c_str(), |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 3195 | inputTarget.globalScaleFactor, bitsetToString(inputTarget.pointerIds).c_str(), |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3196 | inputTarget.getPointerInfoString().c_str()); |
| 3197 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3198 | |
| 3199 | // Skip this event if the connection status is not normal. |
| 3200 | // 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] | 3201 | if (connection->status != Connection::Status::NORMAL) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3202 | if (DEBUG_DISPATCH_CYCLE) { |
| 3203 | ALOGD("channel '%s' ~ Dropping event because the channel status is %s", |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3204 | connection->getInputChannelName().c_str(), |
| 3205 | ftl::enum_string(connection->status).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3206 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3207 | return; |
| 3208 | } |
| 3209 | |
| 3210 | // Split a motion event if needed. |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3211 | if (inputTarget.flags.test(InputTarget::Flags::SPLIT)) { |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3212 | LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3213 | "Entry type %s should not have Flags::SPLIT", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3214 | ftl::enum_string(eventEntry->type).c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3215 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3216 | const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry); |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3217 | if (inputTarget.pointerIds.count() != originalMotionEntry.pointerCount) { |
Siarhei Vishniakou | 16e4fa0 | 2023-02-16 17:48:56 -0800 | [diff] [blame] | 3218 | if (!inputTarget.firstDownTimeInTarget.has_value()) { |
| 3219 | logDispatchStateLocked(); |
| 3220 | LOG(FATAL) << "Splitting motion events requires a down time to be set for the " |
| 3221 | "target on connection " |
| 3222 | << connection->getInputChannelName() << " for " |
| 3223 | << originalMotionEntry.getDescription(); |
| 3224 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3225 | std::unique_ptr<MotionEntry> splitMotionEntry = |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3226 | splitMotionEvent(originalMotionEntry, inputTarget.pointerIds, |
| 3227 | inputTarget.firstDownTimeInTarget.value()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3228 | if (!splitMotionEntry) { |
| 3229 | return; // split event was dropped |
| 3230 | } |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3231 | if (splitMotionEntry->action == AMOTION_EVENT_ACTION_CANCEL) { |
| 3232 | std::string reason = std::string("reason=pointer cancel on split window"); |
| 3233 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 3234 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 3235 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3236 | if (DEBUG_FOCUS) { |
| 3237 | ALOGD("channel '%s' ~ Split motion event.", |
| 3238 | connection->getInputChannelName().c_str()); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3239 | logOutboundMotionDetails(" ", *splitMotionEntry); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3240 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3241 | enqueueDispatchEntriesLocked(currentTime, connection, std::move(splitMotionEntry), |
| 3242 | inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3243 | return; |
| 3244 | } |
| 3245 | } |
| 3246 | |
| 3247 | // Not splitting. Enqueue dispatch entries for the event as is. |
| 3248 | enqueueDispatchEntriesLocked(currentTime, connection, eventEntry, inputTarget); |
| 3249 | } |
| 3250 | |
| 3251 | void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3252 | const std::shared_ptr<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3253 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3254 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3255 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3256 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3257 | StringPrintf("enqueueDispatchEntriesLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3258 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3259 | ATRACE_NAME(message.c_str()); |
| 3260 | } |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3261 | LOG_ALWAYS_FATAL_IF(!inputTarget.flags.any(InputTarget::DISPATCH_MASK), |
| 3262 | "No dispatch flags are set for %s", eventEntry->getDescription().c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3263 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 3264 | const bool wasEmpty = connection->outboundQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3265 | |
| 3266 | // Enqueue dispatch entries for the requested modes. |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3267 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3268 | InputTarget::Flags::DISPATCH_AS_HOVER_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3269 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3270 | InputTarget::Flags::DISPATCH_AS_OUTSIDE); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3271 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3272 | InputTarget::Flags::DISPATCH_AS_HOVER_ENTER); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3273 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3274 | InputTarget::Flags::DISPATCH_AS_IS); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3275 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3276 | InputTarget::Flags::DISPATCH_AS_SLIPPERY_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3277 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3278 | InputTarget::Flags::DISPATCH_AS_SLIPPERY_ENTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3279 | |
| 3280 | // If the outbound queue was previously empty, start the dispatch cycle going. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3281 | if (wasEmpty && !connection->outboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3282 | startDispatchCycleLocked(currentTime, connection); |
| 3283 | } |
| 3284 | } |
| 3285 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3286 | void InputDispatcher::enqueueDispatchEntryLocked(const std::shared_ptr<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3287 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3288 | const InputTarget& inputTarget, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3289 | ftl::Flags<InputTarget::Flags> dispatchMode) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3290 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3291 | std::string message = StringPrintf("enqueueDispatchEntry(inputChannel=%s, dispatchMode=%s)", |
| 3292 | connection->getInputChannelName().c_str(), |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3293 | dispatchMode.string().c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3294 | ATRACE_NAME(message.c_str()); |
| 3295 | } |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3296 | ftl::Flags<InputTarget::Flags> inputTargetFlags = inputTarget.flags; |
| 3297 | if (!inputTargetFlags.any(dispatchMode)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3298 | return; |
| 3299 | } |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3300 | |
| 3301 | inputTargetFlags.clear(InputTarget::DISPATCH_MASK); |
| 3302 | inputTargetFlags |= dispatchMode; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3303 | |
| 3304 | // This is a new event. |
| 3305 | // Enqueue a new dispatch entry onto the outbound queue for this connection. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3306 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3307 | createDispatchEntry(inputTarget, eventEntry, inputTargetFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3308 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3309 | // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a |
| 3310 | // different EventEntry than what was passed in. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3311 | EventEntry& newEntry = *(dispatchEntry->eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3312 | // Apply target flags and update the connection's input state. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3313 | switch (newEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3314 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3315 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3316 | dispatchEntry->resolvedEventId = keyEntry.id; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3317 | dispatchEntry->resolvedAction = keyEntry.action; |
| 3318 | dispatchEntry->resolvedFlags = keyEntry.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3319 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3320 | if (!connection->inputState.trackKey(keyEntry, dispatchEntry->resolvedAction, |
| 3321 | dispatchEntry->resolvedFlags)) { |
Siarhei Vishniakou | c94dafe | 2023-05-26 10:24:19 -0700 | [diff] [blame] | 3322 | LOG(WARNING) << "channel " << connection->getInputChannelName() |
| 3323 | << "~ dropping inconsistent event: " << *dispatchEntry; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3324 | return; // skip the inconsistent event |
| 3325 | } |
| 3326 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3327 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3328 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3329 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3330 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3331 | // Assign a default value to dispatchEntry that will never be generated by InputReader, |
| 3332 | // and assign a InputDispatcher value if it doesn't change in the if-else chain below. |
| 3333 | constexpr int32_t DEFAULT_RESOLVED_EVENT_ID = |
| 3334 | static_cast<int32_t>(IdGenerator::Source::OTHER); |
| 3335 | dispatchEntry->resolvedEventId = DEFAULT_RESOLVED_EVENT_ID; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3336 | if (dispatchMode.test(InputTarget::Flags::DISPATCH_AS_OUTSIDE)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3337 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3338 | } else if (dispatchMode.test(InputTarget::Flags::DISPATCH_AS_HOVER_EXIT)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3339 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3340 | } else if (dispatchMode.test(InputTarget::Flags::DISPATCH_AS_HOVER_ENTER)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3341 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3342 | } else if (dispatchMode.test(InputTarget::Flags::DISPATCH_AS_SLIPPERY_EXIT)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3343 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_CANCEL; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3344 | } else if (dispatchMode.test(InputTarget::Flags::DISPATCH_AS_SLIPPERY_ENTER)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3345 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_DOWN; |
| 3346 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3347 | dispatchEntry->resolvedAction = motionEntry.action; |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3348 | dispatchEntry->resolvedEventId = motionEntry.id; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3349 | } |
| 3350 | if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE && |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3351 | !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source, |
| 3352 | motionEntry.displayId)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3353 | if (DEBUG_DISPATCH_CYCLE) { |
| 3354 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: filling in missing hover " |
| 3355 | "enter event", |
| 3356 | connection->getInputChannelName().c_str()); |
| 3357 | } |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3358 | // We keep the 'resolvedEventId' here equal to the original 'motionEntry.id' because |
| 3359 | // this is a one-to-one event conversion. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3360 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 3361 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3362 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3363 | dispatchEntry->resolvedFlags = motionEntry.flags; |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 3364 | if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_CANCEL) { |
| 3365 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_CANCELED; |
| 3366 | } |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3367 | if (dispatchEntry->targetFlags.test(InputTarget::Flags::WINDOW_IS_OBSCURED)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3368 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; |
| 3369 | } |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3370 | if (dispatchEntry->targetFlags.test(InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3371 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 3372 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3373 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3374 | if (!connection->inputState.trackMotion(motionEntry, dispatchEntry->resolvedAction, |
| 3375 | dispatchEntry->resolvedFlags)) { |
Siarhei Vishniakou | c94dafe | 2023-05-26 10:24:19 -0700 | [diff] [blame] | 3376 | LOG(WARNING) << "channel " << connection->getInputChannelName() |
| 3377 | << "~ dropping inconsistent event: " << *dispatchEntry; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3378 | return; // skip the inconsistent event |
| 3379 | } |
| 3380 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3381 | dispatchEntry->resolvedEventId = |
| 3382 | dispatchEntry->resolvedEventId == DEFAULT_RESOLVED_EVENT_ID |
| 3383 | ? mIdGenerator.nextId() |
| 3384 | : motionEntry.id; |
| 3385 | if (ATRACE_ENABLED() && dispatchEntry->resolvedEventId != motionEntry.id) { |
| 3386 | std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32 |
| 3387 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3388 | motionEntry.id, dispatchEntry->resolvedEventId); |
| 3389 | ATRACE_NAME(message.c_str()); |
| 3390 | } |
| 3391 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 3392 | if ((motionEntry.flags & AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) && |
| 3393 | (motionEntry.policyFlags & POLICY_FLAG_TRUSTED)) { |
| 3394 | // Skip reporting pointer down outside focus to the policy. |
| 3395 | break; |
| 3396 | } |
| 3397 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3398 | dispatchPointerDownOutsideFocus(motionEntry.source, dispatchEntry->resolvedAction, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3399 | inputTarget.inputChannel->getConnectionToken()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3400 | |
| 3401 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3402 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3403 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3404 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3405 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3406 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3407 | break; |
| 3408 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3409 | case EventEntry::Type::SENSOR: { |
| 3410 | LOG_ALWAYS_FATAL("SENSOR events should not go to apps via input channel"); |
| 3411 | break; |
| 3412 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3413 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 3414 | case EventEntry::Type::DEVICE_RESET: { |
| 3415 | LOG_ALWAYS_FATAL("%s events should not go to apps", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3416 | ftl::enum_string(newEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3417 | break; |
| 3418 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3419 | } |
| 3420 | |
| 3421 | // Remember that we are waiting for this dispatch to complete. |
| 3422 | if (dispatchEntry->hasForegroundTarget()) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3423 | incrementPendingForegroundDispatches(newEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3424 | } |
| 3425 | |
| 3426 | // Enqueue the dispatch entry. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3427 | connection->outboundQueue.push_back(dispatchEntry.release()); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3428 | traceOutboundQueueLength(*connection); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3429 | } |
| 3430 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3431 | /** |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3432 | * This function is for debugging and metrics collection. It has two roles. |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3433 | * |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3434 | * The first role is to log input interaction with windows, which helps determine what the user was |
| 3435 | * interacting with. For example, if user is touching launcher, we will see an input_interaction log |
| 3436 | * that user started interacting with launcher window, as well as any other window that received |
| 3437 | * that gesture, such as the wallpaper or other spy windows. A new input_interaction is only logged |
| 3438 | * when the set of tokens that received the event changes. It is not logged again as long as the |
| 3439 | * user is interacting with the same windows. |
| 3440 | * |
| 3441 | * The second role is to track input device activity for metrics collection. For each input event, |
| 3442 | * we report the set of UIDs that the input device interacted with to the policy. Unlike for the |
| 3443 | * input_interaction logs, the device interaction is reported even when the set of interaction |
| 3444 | * tokens do not change. |
| 3445 | * |
| 3446 | * For these purposes, we do not count ACTION_OUTSIDE, ACTION_UP and ACTION_CANCEL actions as |
| 3447 | * interaction. This includes up and cancel events for both keys and motions. |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3448 | */ |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3449 | void InputDispatcher::processInteractionsLocked(const EventEntry& entry, |
| 3450 | const std::vector<InputTarget>& targets) { |
| 3451 | int32_t deviceId; |
| 3452 | nsecs_t eventTime; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3453 | // Skip ACTION_UP events, and all events other than keys and motions |
| 3454 | if (entry.type == EventEntry::Type::KEY) { |
| 3455 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 3456 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
| 3457 | return; |
| 3458 | } |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3459 | deviceId = keyEntry.deviceId; |
| 3460 | eventTime = keyEntry.eventTime; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3461 | } else if (entry.type == EventEntry::Type::MOTION) { |
| 3462 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 3463 | if (motionEntry.action == AMOTION_EVENT_ACTION_UP || |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3464 | motionEntry.action == AMOTION_EVENT_ACTION_CANCEL || |
| 3465 | MotionEvent::getActionMasked(motionEntry.action) == AMOTION_EVENT_ACTION_POINTER_UP) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3466 | return; |
| 3467 | } |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3468 | deviceId = motionEntry.deviceId; |
| 3469 | eventTime = motionEntry.eventTime; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3470 | } else { |
| 3471 | return; // Not a key or a motion |
| 3472 | } |
| 3473 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3474 | std::set<gui::Uid> interactionUids; |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 3475 | std::unordered_set<sp<IBinder>, StrongPointerHash<IBinder>> newConnectionTokens; |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3476 | std::vector<std::shared_ptr<Connection>> newConnections; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3477 | for (const InputTarget& target : targets) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3478 | if (target.flags.test(InputTarget::Flags::DISPATCH_AS_OUTSIDE)) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3479 | continue; // Skip windows that receive ACTION_OUTSIDE |
| 3480 | } |
| 3481 | |
| 3482 | sp<IBinder> token = target.inputChannel->getConnectionToken(); |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3483 | std::shared_ptr<Connection> connection = getConnectionLocked(token); |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3484 | if (connection == nullptr) { |
| 3485 | continue; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3486 | } |
| 3487 | newConnectionTokens.insert(std::move(token)); |
| 3488 | newConnections.emplace_back(connection); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3489 | if (target.windowHandle) { |
| 3490 | interactionUids.emplace(target.windowHandle->getInfo()->ownerUid); |
| 3491 | } |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3492 | } |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3493 | |
| 3494 | auto command = [this, deviceId, eventTime, uids = std::move(interactionUids)]() |
| 3495 | REQUIRES(mLock) { |
| 3496 | scoped_unlock unlock(mLock); |
| 3497 | mPolicy.notifyDeviceInteraction(deviceId, eventTime, uids); |
| 3498 | }; |
| 3499 | postCommandLocked(std::move(command)); |
| 3500 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3501 | if (newConnectionTokens == mInteractionConnectionTokens) { |
| 3502 | return; // no change |
| 3503 | } |
| 3504 | mInteractionConnectionTokens = newConnectionTokens; |
| 3505 | |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3506 | std::string targetList; |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3507 | for (const std::shared_ptr<Connection>& connection : newConnections) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3508 | targetList += connection->getWindowName() + ", "; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3509 | } |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3510 | std::string message = "Interaction with: " + targetList; |
| 3511 | if (targetList.empty()) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3512 | message += "<none>"; |
| 3513 | } |
| 3514 | android_log_event_list(LOGTAG_INPUT_INTERACTION) << message << LOG_ID_EVENTS; |
| 3515 | } |
| 3516 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3517 | void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3518 | const sp<IBinder>& token) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3519 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3520 | uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK; |
| 3521 | if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3522 | return; |
| 3523 | } |
| 3524 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 3525 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3526 | if (focusedToken == token) { |
| 3527 | // ignore since token is focused |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3528 | return; |
| 3529 | } |
| 3530 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3531 | auto command = [this, token]() REQUIRES(mLock) { |
| 3532 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 3533 | mPolicy.onPointerDownOutsideFocus(token); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3534 | }; |
| 3535 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3536 | } |
| 3537 | |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3538 | status_t InputDispatcher::publishMotionEvent(Connection& connection, |
| 3539 | DispatchEntry& dispatchEntry) const { |
| 3540 | const EventEntry& eventEntry = *(dispatchEntry.eventEntry); |
| 3541 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 3542 | |
| 3543 | PointerCoords scaledCoords[MAX_POINTERS]; |
| 3544 | const PointerCoords* usingCoords = motionEntry.pointerCoords; |
| 3545 | |
| 3546 | // Set the X and Y offset and X and Y scale depending on the input source. |
| 3547 | if ((motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) && |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3548 | !(dispatchEntry.targetFlags.test(InputTarget::Flags::ZERO_COORDS))) { |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3549 | float globalScaleFactor = dispatchEntry.globalScaleFactor; |
| 3550 | if (globalScaleFactor != 1.0f) { |
| 3551 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
| 3552 | scaledCoords[i] = motionEntry.pointerCoords[i]; |
| 3553 | // Don't apply window scale here since we don't want scale to affect raw |
| 3554 | // coordinates. The scale will be sent back to the client and applied |
| 3555 | // later when requesting relative coordinates. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 3556 | scaledCoords[i].scale(globalScaleFactor, /*windowXScale=*/1, /*windowYScale=*/1); |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3557 | } |
| 3558 | usingCoords = scaledCoords; |
| 3559 | } |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3560 | } else if (dispatchEntry.targetFlags.test(InputTarget::Flags::ZERO_COORDS)) { |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3561 | // We don't want the dispatch target to know the coordinates |
| 3562 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
| 3563 | scaledCoords[i].clear(); |
| 3564 | } |
| 3565 | usingCoords = scaledCoords; |
| 3566 | } |
| 3567 | |
| 3568 | std::array<uint8_t, 32> hmac = getSignature(motionEntry, dispatchEntry); |
| 3569 | |
| 3570 | // Publish the motion event. |
| 3571 | return connection.inputPublisher |
| 3572 | .publishMotionEvent(dispatchEntry.seq, dispatchEntry.resolvedEventId, |
| 3573 | motionEntry.deviceId, motionEntry.source, motionEntry.displayId, |
| 3574 | std::move(hmac), dispatchEntry.resolvedAction, |
| 3575 | motionEntry.actionButton, dispatchEntry.resolvedFlags, |
| 3576 | motionEntry.edgeFlags, motionEntry.metaState, |
| 3577 | motionEntry.buttonState, motionEntry.classification, |
| 3578 | dispatchEntry.transform, motionEntry.xPrecision, |
| 3579 | motionEntry.yPrecision, motionEntry.xCursorPosition, |
| 3580 | motionEntry.yCursorPosition, dispatchEntry.rawTransform, |
| 3581 | motionEntry.downTime, motionEntry.eventTime, |
| 3582 | motionEntry.pointerCount, motionEntry.pointerProperties, |
| 3583 | usingCoords); |
| 3584 | } |
| 3585 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3586 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3587 | const std::shared_ptr<Connection>& connection) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3588 | if (ATRACE_ENABLED()) { |
| 3589 | std::string message = StringPrintf("startDispatchCycleLocked(inputChannel=%s)", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3590 | connection->getInputChannelName().c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3591 | ATRACE_NAME(message.c_str()); |
| 3592 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3593 | if (DEBUG_DISPATCH_CYCLE) { |
| 3594 | ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str()); |
| 3595 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3596 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3597 | while (connection->status == Connection::Status::NORMAL && !connection->outboundQueue.empty()) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3598 | DispatchEntry* dispatchEntry = connection->outboundQueue.front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3599 | dispatchEntry->deliveryTime = currentTime; |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3600 | const std::chrono::nanoseconds timeout = getDispatchingTimeoutLocked(connection); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3601 | dispatchEntry->timeoutTime = currentTime + timeout.count(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3602 | |
| 3603 | // Publish the event. |
| 3604 | status_t status; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3605 | const EventEntry& eventEntry = *(dispatchEntry->eventEntry); |
| 3606 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3607 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3608 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 3609 | std::array<uint8_t, 32> hmac = getSignature(keyEntry, *dispatchEntry); |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 3610 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 3611 | LOG(DEBUG) << "Publishing " << *dispatchEntry << " to " |
| 3612 | << connection->getInputChannelName(); |
| 3613 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3614 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3615 | // Publish the key event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3616 | status = connection->inputPublisher |
| 3617 | .publishKeyEvent(dispatchEntry->seq, |
| 3618 | dispatchEntry->resolvedEventId, keyEntry.deviceId, |
| 3619 | keyEntry.source, keyEntry.displayId, |
| 3620 | std::move(hmac), dispatchEntry->resolvedAction, |
| 3621 | dispatchEntry->resolvedFlags, keyEntry.keyCode, |
| 3622 | keyEntry.scanCode, keyEntry.metaState, |
| 3623 | keyEntry.repeatCount, keyEntry.downTime, |
| 3624 | keyEntry.eventTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3625 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3626 | } |
| 3627 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3628 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 3629 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 3630 | LOG(DEBUG) << "Publishing " << *dispatchEntry << " to " |
| 3631 | << connection->getInputChannelName(); |
| 3632 | } |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3633 | status = publishMotionEvent(*connection, *dispatchEntry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3634 | break; |
| 3635 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3636 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3637 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3638 | const FocusEntry& focusEntry = static_cast<const FocusEntry&>(eventEntry); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3639 | status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3640 | focusEntry.id, |
Antonio Kantek | 3cfec7b | 2021-11-05 18:26:17 -0700 | [diff] [blame] | 3641 | focusEntry.hasFocus); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3642 | break; |
| 3643 | } |
| 3644 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3645 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 3646 | const TouchModeEntry& touchModeEntry = |
| 3647 | static_cast<const TouchModeEntry&>(eventEntry); |
| 3648 | status = connection->inputPublisher |
| 3649 | .publishTouchModeEvent(dispatchEntry->seq, touchModeEntry.id, |
| 3650 | touchModeEntry.inTouchMode); |
| 3651 | |
| 3652 | break; |
| 3653 | } |
| 3654 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3655 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 3656 | const auto& captureEntry = |
| 3657 | static_cast<const PointerCaptureChangedEntry&>(eventEntry); |
| 3658 | status = connection->inputPublisher |
| 3659 | .publishCaptureEvent(dispatchEntry->seq, captureEntry.id, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 3660 | captureEntry.pointerCaptureRequest.enable); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3661 | break; |
| 3662 | } |
| 3663 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3664 | case EventEntry::Type::DRAG: { |
| 3665 | const DragEntry& dragEntry = static_cast<const DragEntry&>(eventEntry); |
| 3666 | status = connection->inputPublisher.publishDragEvent(dispatchEntry->seq, |
| 3667 | dragEntry.id, dragEntry.x, |
| 3668 | dragEntry.y, |
| 3669 | dragEntry.isExiting); |
| 3670 | break; |
| 3671 | } |
| 3672 | |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3673 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3674 | case EventEntry::Type::DEVICE_RESET: |
| 3675 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3676 | LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3677 | ftl::enum_string(eventEntry.type).c_str()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3678 | return; |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3679 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3680 | } |
| 3681 | |
| 3682 | // Check the result. |
| 3683 | if (status) { |
| 3684 | if (status == WOULD_BLOCK) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3685 | if (connection->waitQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3686 | ALOGE("channel '%s' ~ Could not publish event because the pipe is full. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3687 | "This is unexpected because the wait queue is empty, so the pipe " |
| 3688 | "should be empty and we shouldn't have any problems writing an " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3689 | "event to it, status=%s(%d)", |
| 3690 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3691 | status); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 3692 | abortBrokenDispatchCycleLocked(currentTime, connection, /*notify=*/true); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3693 | } else { |
| 3694 | // Pipe is full and we are waiting for the app to finish process some events |
| 3695 | // before sending more events to it. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3696 | if (DEBUG_DISPATCH_CYCLE) { |
| 3697 | ALOGD("channel '%s' ~ Could not publish event because the pipe is full, " |
| 3698 | "waiting for the application to catch up", |
| 3699 | connection->getInputChannelName().c_str()); |
| 3700 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3701 | } |
| 3702 | } else { |
| 3703 | ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3704 | "status=%s(%d)", |
| 3705 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3706 | status); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 3707 | abortBrokenDispatchCycleLocked(currentTime, connection, /*notify=*/true); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3708 | } |
| 3709 | return; |
| 3710 | } |
| 3711 | |
| 3712 | // Re-enqueue the event on the wait queue. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3713 | connection->outboundQueue.erase(std::remove(connection->outboundQueue.begin(), |
| 3714 | connection->outboundQueue.end(), |
| 3715 | dispatchEntry)); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3716 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3717 | connection->waitQueue.push_back(dispatchEntry); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3718 | if (connection->responsive) { |
| 3719 | mAnrTracker.insert(dispatchEntry->timeoutTime, |
| 3720 | connection->inputChannel->getConnectionToken()); |
| 3721 | } |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3722 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3723 | } |
| 3724 | } |
| 3725 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3726 | std::array<uint8_t, 32> InputDispatcher::sign(const VerifiedInputEvent& event) const { |
| 3727 | size_t size; |
| 3728 | switch (event.type) { |
| 3729 | case VerifiedInputEvent::Type::KEY: { |
| 3730 | size = sizeof(VerifiedKeyEvent); |
| 3731 | break; |
| 3732 | } |
| 3733 | case VerifiedInputEvent::Type::MOTION: { |
| 3734 | size = sizeof(VerifiedMotionEvent); |
| 3735 | break; |
| 3736 | } |
| 3737 | } |
| 3738 | const uint8_t* start = reinterpret_cast<const uint8_t*>(&event); |
| 3739 | return mHmacKeyManager.sign(start, size); |
| 3740 | } |
| 3741 | |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3742 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3743 | const MotionEntry& motionEntry, const DispatchEntry& dispatchEntry) const { |
Siarhei Vishniakou | bf88052 | 2023-05-01 11:03:22 -0700 | [diff] [blame] | 3744 | const int32_t actionMasked = MotionEvent::getActionMasked(dispatchEntry.resolvedAction); |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3745 | if (actionMasked != AMOTION_EVENT_ACTION_UP && actionMasked != AMOTION_EVENT_ACTION_DOWN) { |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3746 | // Only sign events up and down events as the purely move events |
| 3747 | // are tied to their up/down counterparts so signing would be redundant. |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3748 | return INVALID_HMAC; |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3749 | } |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3750 | |
| 3751 | VerifiedMotionEvent verifiedEvent = |
| 3752 | verifiedMotionEventFromMotionEntry(motionEntry, dispatchEntry.rawTransform); |
| 3753 | verifiedEvent.actionMasked = actionMasked; |
| 3754 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS; |
| 3755 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3756 | } |
| 3757 | |
| 3758 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3759 | const KeyEntry& keyEntry, const DispatchEntry& dispatchEntry) const { |
| 3760 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(keyEntry); |
| 3761 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_KEY_EVENT_FLAGS; |
| 3762 | verifiedEvent.action = dispatchEntry.resolvedAction; |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3763 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3764 | } |
| 3765 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3766 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3767 | const std::shared_ptr<Connection>& connection, |
| 3768 | uint32_t seq, bool handled, nsecs_t consumeTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3769 | if (DEBUG_DISPATCH_CYCLE) { |
| 3770 | ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s", |
| 3771 | connection->getInputChannelName().c_str(), seq, toString(handled)); |
| 3772 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3773 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3774 | if (connection->status == Connection::Status::BROKEN || |
| 3775 | connection->status == Connection::Status::ZOMBIE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3776 | return; |
| 3777 | } |
| 3778 | |
| 3779 | // Notify other system components and prepare to start the next dispatch cycle. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3780 | auto command = [this, currentTime, connection, seq, handled, consumeTime]() REQUIRES(mLock) { |
| 3781 | doDispatchCycleFinishedCommand(currentTime, connection, seq, handled, consumeTime); |
| 3782 | }; |
| 3783 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3784 | } |
| 3785 | |
| 3786 | void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3787 | const std::shared_ptr<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3788 | bool notify) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3789 | if (DEBUG_DISPATCH_CYCLE) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 3790 | LOG(DEBUG) << "channel '" << connection->getInputChannelName() << "'~ " << __func__ |
| 3791 | << " - notify=" << toString(notify); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3792 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3793 | |
| 3794 | // Clear the dispatch queues. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3795 | drainDispatchQueue(connection->outboundQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3796 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3797 | drainDispatchQueue(connection->waitQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3798 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3799 | |
| 3800 | // The connection appears to be unrecoverably broken. |
| 3801 | // Ignore already broken or zombie connections. |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3802 | if (connection->status == Connection::Status::NORMAL) { |
| 3803 | connection->status = Connection::Status::BROKEN; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3804 | |
| 3805 | if (notify) { |
| 3806 | // Notify other system components. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3807 | ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", |
| 3808 | connection->getInputChannelName().c_str()); |
| 3809 | |
| 3810 | auto command = [this, connection]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3811 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 3812 | mPolicy.notifyInputChannelBroken(connection->inputChannel->getConnectionToken()); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3813 | }; |
| 3814 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3815 | } |
| 3816 | } |
| 3817 | } |
| 3818 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3819 | void InputDispatcher::drainDispatchQueue(std::deque<DispatchEntry*>& queue) { |
| 3820 | while (!queue.empty()) { |
| 3821 | DispatchEntry* dispatchEntry = queue.front(); |
| 3822 | queue.pop_front(); |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3823 | releaseDispatchEntry(dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3824 | } |
| 3825 | } |
| 3826 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3827 | void InputDispatcher::releaseDispatchEntry(DispatchEntry* dispatchEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3828 | if (dispatchEntry->hasForegroundTarget()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3829 | decrementPendingForegroundDispatches(*(dispatchEntry->eventEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3830 | } |
| 3831 | delete dispatchEntry; |
| 3832 | } |
| 3833 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3834 | int InputDispatcher::handleReceiveCallback(int events, sp<IBinder> connectionToken) { |
| 3835 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3836 | std::shared_ptr<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3837 | if (connection == nullptr) { |
| 3838 | ALOGW("Received looper callback for unknown input channel token %p. events=0x%x", |
| 3839 | connectionToken.get(), events); |
| 3840 | return 0; // remove the callback |
| 3841 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3842 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3843 | bool notify; |
| 3844 | if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { |
| 3845 | if (!(events & ALOOPER_EVENT_INPUT)) { |
| 3846 | ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. " |
| 3847 | "events=0x%x", |
| 3848 | connection->getInputChannelName().c_str(), events); |
| 3849 | return 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3850 | } |
| 3851 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3852 | nsecs_t currentTime = now(); |
| 3853 | bool gotOne = false; |
| 3854 | status_t status = OK; |
| 3855 | for (;;) { |
| 3856 | Result<InputPublisher::ConsumerResponse> result = |
| 3857 | connection->inputPublisher.receiveConsumerResponse(); |
| 3858 | if (!result.ok()) { |
| 3859 | status = result.error().code(); |
| 3860 | break; |
| 3861 | } |
| 3862 | |
| 3863 | if (std::holds_alternative<InputPublisher::Finished>(*result)) { |
| 3864 | const InputPublisher::Finished& finish = |
| 3865 | std::get<InputPublisher::Finished>(*result); |
| 3866 | finishDispatchCycleLocked(currentTime, connection, finish.seq, finish.handled, |
| 3867 | finish.consumeTime); |
| 3868 | } else if (std::holds_alternative<InputPublisher::Timeline>(*result)) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3869 | if (shouldReportMetricsForConnection(*connection)) { |
| 3870 | const InputPublisher::Timeline& timeline = |
| 3871 | std::get<InputPublisher::Timeline>(*result); |
| 3872 | mLatencyTracker |
| 3873 | .trackGraphicsLatency(timeline.inputEventId, |
| 3874 | connection->inputChannel->getConnectionToken(), |
| 3875 | std::move(timeline.graphicsTimeline)); |
| 3876 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3877 | } |
| 3878 | gotOne = true; |
| 3879 | } |
| 3880 | if (gotOne) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3881 | runCommandsLockedInterruptable(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3882 | if (status == WOULD_BLOCK) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3883 | return 1; |
| 3884 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3885 | } |
| 3886 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3887 | notify = status != DEAD_OBJECT || !connection->monitor; |
| 3888 | if (notify) { |
| 3889 | ALOGE("channel '%s' ~ Failed to receive finished signal. status=%s(%d)", |
| 3890 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3891 | status); |
| 3892 | } |
| 3893 | } else { |
| 3894 | // Monitor channels are never explicitly unregistered. |
| 3895 | // We do it automatically when the remote endpoint is closed so don't warn about them. |
| 3896 | const bool stillHaveWindowHandle = |
| 3897 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()) != nullptr; |
| 3898 | notify = !connection->monitor && stillHaveWindowHandle; |
| 3899 | if (notify) { |
| 3900 | ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred. events=0x%x", |
| 3901 | connection->getInputChannelName().c_str(), events); |
| 3902 | } |
| 3903 | } |
| 3904 | |
| 3905 | // Remove the channel. |
| 3906 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), notify); |
| 3907 | return 0; // remove the callback |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3908 | } |
| 3909 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3910 | void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3911 | const CancelationOptions& options) { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3912 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 3913 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3914 | } |
| 3915 | } |
| 3916 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3917 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3918 | const CancelationOptions& options) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 3919 | for (const auto& [_, monitors] : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3920 | for (const Monitor& monitor : monitors) { |
| 3921 | synthesizeCancelationEventsForInputChannelLocked(monitor.inputChannel, options); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3922 | } |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3923 | } |
| 3924 | } |
| 3925 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3926 | void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked( |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 3927 | const std::shared_ptr<InputChannel>& channel, const CancelationOptions& options) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3928 | std::shared_ptr<Connection> connection = getConnectionLocked(channel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3929 | if (connection == nullptr) { |
| 3930 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3931 | } |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3932 | |
| 3933 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3934 | } |
| 3935 | |
| 3936 | void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3937 | const std::shared_ptr<Connection>& connection, const CancelationOptions& options) { |
Linnan Li | a4659fc | 2023-07-14 14:36:22 +0800 | [diff] [blame] | 3938 | if ((options.mode == CancelationOptions::Mode::CANCEL_POINTER_EVENTS || |
| 3939 | options.mode == CancelationOptions::Mode::CANCEL_ALL_EVENTS) && |
| 3940 | mDragState && mDragState->dragWindow->getToken() == connection->inputChannel->getToken()) { |
| 3941 | LOG(INFO) << __func__ |
| 3942 | << ": Canceling drag and drop because the pointers for the drag window are being " |
| 3943 | "canceled."; |
| 3944 | sendDropWindowCommandLocked(nullptr, /*x=*/0, /*y=*/0); |
| 3945 | mDragState.reset(); |
| 3946 | } |
| 3947 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3948 | if (connection->status == Connection::Status::BROKEN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3949 | return; |
| 3950 | } |
| 3951 | |
| 3952 | nsecs_t currentTime = now(); |
| 3953 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3954 | std::vector<std::unique_ptr<EventEntry>> cancelationEvents = |
Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 3955 | connection->inputState.synthesizeCancelationEvents(currentTime, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3956 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3957 | if (cancelationEvents.empty()) { |
| 3958 | return; |
| 3959 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3960 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 3961 | 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] | 3962 | "with reality: %s, mode=%s.", |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3963 | connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason, |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3964 | ftl::enum_string(options.mode).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3965 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3966 | |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3967 | std::string reason = std::string("reason=").append(options.reason); |
| 3968 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 3969 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 3970 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3971 | InputTarget target; |
Hu Guo | ca59f11 | 2023-09-17 20:51:08 +0800 | [diff] [blame] | 3972 | sp<WindowInfoHandle> windowHandle; |
| 3973 | if (options.displayId) { |
| 3974 | windowHandle = getWindowHandleLocked(connection->inputChannel->getConnectionToken(), |
| 3975 | options.displayId.value()); |
| 3976 | } else { |
| 3977 | windowHandle = getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3978 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3979 | if (windowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3980 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3981 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3982 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3983 | } |
| 3984 | target.inputChannel = connection->inputChannel; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3985 | target.flags = InputTarget::Flags::DISPATCH_AS_IS; |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3986 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 3987 | const bool wasEmpty = connection->outboundQueue.empty(); |
| 3988 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3989 | for (size_t i = 0; i < cancelationEvents.size(); i++) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3990 | std::unique_ptr<EventEntry> cancelationEventEntry = std::move(cancelationEvents[i]); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3991 | switch (cancelationEventEntry->type) { |
| 3992 | case EventEntry::Type::KEY: { |
| 3993 | logOutboundKeyDetails("cancel - ", |
| 3994 | static_cast<const KeyEntry&>(*cancelationEventEntry)); |
| 3995 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3996 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3997 | case EventEntry::Type::MOTION: { |
| 3998 | logOutboundMotionDetails("cancel - ", |
| 3999 | static_cast<const MotionEntry&>(*cancelationEventEntry)); |
| 4000 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4001 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4002 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 4003 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4004 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 4005 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4006 | LOG_ALWAYS_FATAL("Canceling %s events is not supported", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 4007 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4008 | break; |
| 4009 | } |
| 4010 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4011 | case EventEntry::Type::DEVICE_RESET: |
| 4012 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4013 | 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] | 4014 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4015 | break; |
| 4016 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4017 | } |
| 4018 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4019 | enqueueDispatchEntryLocked(connection, std::move(cancelationEventEntry), target, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 4020 | InputTarget::Flags::DISPATCH_AS_IS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4021 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4022 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 4023 | // If the outbound queue was previously empty, start the dispatch cycle going. |
| 4024 | if (wasEmpty && !connection->outboundQueue.empty()) { |
| 4025 | startDispatchCycleLocked(currentTime, connection); |
| 4026 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4027 | } |
| 4028 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4029 | void InputDispatcher::synthesizePointerDownEventsForConnectionLocked( |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 4030 | const nsecs_t downTime, const std::shared_ptr<Connection>& connection, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4031 | ftl::Flags<InputTarget::Flags> targetFlags) { |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 4032 | if (connection->status == Connection::Status::BROKEN) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4033 | return; |
| 4034 | } |
| 4035 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4036 | std::vector<std::unique_ptr<EventEntry>> downEvents = |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4037 | connection->inputState.synthesizePointerDownEvents(downTime); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4038 | |
| 4039 | if (downEvents.empty()) { |
| 4040 | return; |
| 4041 | } |
| 4042 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4043 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4044 | ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.", |
| 4045 | connection->getInputChannelName().c_str(), downEvents.size()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4046 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4047 | |
| 4048 | InputTarget target; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4049 | sp<WindowInfoHandle> windowHandle = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4050 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 4051 | if (windowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4052 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 4053 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4054 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 4055 | } |
| 4056 | target.inputChannel = connection->inputChannel; |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4057 | target.flags = targetFlags; |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4058 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 4059 | const bool wasEmpty = connection->outboundQueue.empty(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4060 | for (std::unique_ptr<EventEntry>& downEventEntry : downEvents) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4061 | switch (downEventEntry->type) { |
| 4062 | case EventEntry::Type::MOTION: { |
| 4063 | logOutboundMotionDetails("down - ", |
| 4064 | static_cast<const MotionEntry&>(*downEventEntry)); |
| 4065 | break; |
| 4066 | } |
| 4067 | |
| 4068 | case EventEntry::Type::KEY: |
| 4069 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 4070 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4071 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4072 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4073 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4074 | case EventEntry::Type::SENSOR: |
| 4075 | case EventEntry::Type::DRAG: { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4076 | 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] | 4077 | ftl::enum_string(downEventEntry->type).c_str()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4078 | break; |
| 4079 | } |
| 4080 | } |
| 4081 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4082 | enqueueDispatchEntryLocked(connection, std::move(downEventEntry), target, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 4083 | InputTarget::Flags::DISPATCH_AS_IS); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4084 | } |
| 4085 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 4086 | // If the outbound queue was previously empty, start the dispatch cycle going. |
| 4087 | if (wasEmpty && !connection->outboundQueue.empty()) { |
| 4088 | startDispatchCycleLocked(downTime, connection); |
| 4089 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4090 | } |
| 4091 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4092 | void InputDispatcher::synthesizeCancelationEventsForWindowLocked( |
| 4093 | const sp<WindowInfoHandle>& windowHandle, const CancelationOptions& options) { |
| 4094 | if (windowHandle != nullptr) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 4095 | std::shared_ptr<Connection> wallpaperConnection = |
| 4096 | getConnectionLocked(windowHandle->getToken()); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4097 | if (wallpaperConnection != nullptr) { |
| 4098 | synthesizeCancelationEventsForConnectionLocked(wallpaperConnection, options); |
| 4099 | } |
| 4100 | } |
| 4101 | } |
| 4102 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4103 | std::unique_ptr<MotionEntry> InputDispatcher::splitMotionEvent( |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 4104 | const MotionEntry& originalMotionEntry, std::bitset<MAX_POINTER_ID + 1> pointerIds, |
| 4105 | nsecs_t splitDownTime) { |
| 4106 | ALOG_ASSERT(pointerIds.any()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4107 | |
| 4108 | uint32_t splitPointerIndexMap[MAX_POINTERS]; |
| 4109 | PointerProperties splitPointerProperties[MAX_POINTERS]; |
| 4110 | PointerCoords splitPointerCoords[MAX_POINTERS]; |
| 4111 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 4112 | uint32_t originalPointerCount = originalMotionEntry.pointerCount; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4113 | uint32_t splitPointerCount = 0; |
| 4114 | |
| 4115 | for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4116 | originalPointerIndex++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4117 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 4118 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4119 | uint32_t pointerId = uint32_t(pointerProperties.id); |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 4120 | if (pointerIds.test(pointerId)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4121 | splitPointerIndexMap[splitPointerCount] = originalPointerIndex; |
| 4122 | splitPointerProperties[splitPointerCount].copyFrom(pointerProperties); |
| 4123 | splitPointerCoords[splitPointerCount].copyFrom( |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 4124 | originalMotionEntry.pointerCoords[originalPointerIndex]); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4125 | splitPointerCount += 1; |
| 4126 | } |
| 4127 | } |
| 4128 | |
| 4129 | if (splitPointerCount != pointerIds.count()) { |
| 4130 | // This is bad. We are missing some of the pointers that we expected to deliver. |
| 4131 | // Most likely this indicates that we received an ACTION_MOVE events that has |
| 4132 | // different pointer ids than we expected based on the previous ACTION_DOWN |
| 4133 | // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers |
| 4134 | // in this way. |
| 4135 | ALOGW("Dropping split motion event because the pointer count is %d but " |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 4136 | "we expected there to be %zu pointers. This probably means we received " |
Siarhei Vishniakou | 16e4fa0 | 2023-02-16 17:48:56 -0800 | [diff] [blame] | 4137 | "a broken sequence of pointer ids from the input device: %s", |
| 4138 | splitPointerCount, pointerIds.count(), originalMotionEntry.getDescription().c_str()); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4139 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4140 | } |
| 4141 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 4142 | int32_t action = originalMotionEntry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4143 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4144 | if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN || |
| 4145 | maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4146 | int32_t originalPointerIndex = getMotionEventActionPointerIndex(action); |
| 4147 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 4148 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4149 | uint32_t pointerId = uint32_t(pointerProperties.id); |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 4150 | if (pointerIds.test(pointerId)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4151 | if (pointerIds.count() == 1) { |
| 4152 | // The first/last pointer went down/up. |
| 4153 | action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4154 | ? AMOTION_EVENT_ACTION_DOWN |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4155 | : (originalMotionEntry.flags & AMOTION_EVENT_FLAG_CANCELED) != 0 |
| 4156 | ? AMOTION_EVENT_ACTION_CANCEL |
| 4157 | : AMOTION_EVENT_ACTION_UP; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4158 | } else { |
| 4159 | // A secondary pointer went down/up. |
| 4160 | uint32_t splitPointerIndex = 0; |
| 4161 | while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { |
| 4162 | splitPointerIndex += 1; |
| 4163 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4164 | action = maskedAction | |
| 4165 | (splitPointerIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4166 | } |
| 4167 | } else { |
| 4168 | // An unrelated pointer changed. |
| 4169 | action = AMOTION_EVENT_ACTION_MOVE; |
| 4170 | } |
| 4171 | } |
| 4172 | |
Siarhei Vishniakou | 59e302b | 2023-06-05 08:04:53 -0700 | [diff] [blame] | 4173 | if (action == AMOTION_EVENT_ACTION_DOWN && splitDownTime != originalMotionEntry.eventTime) { |
| 4174 | logDispatchStateLocked(); |
| 4175 | LOG_ALWAYS_FATAL("Split motion event has mismatching downTime and eventTime for " |
| 4176 | "ACTION_DOWN, motionEntry=%s, splitDownTime=%" PRId64, |
| 4177 | originalMotionEntry.getDescription().c_str(), splitDownTime); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4178 | } |
| 4179 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4180 | int32_t newId = mIdGenerator.nextId(); |
| 4181 | if (ATRACE_ENABLED()) { |
| 4182 | std::string message = StringPrintf("Split MotionEvent(id=0x%" PRIx32 |
| 4183 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 4184 | originalMotionEntry.id, newId); |
| 4185 | ATRACE_NAME(message.c_str()); |
| 4186 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4187 | std::unique_ptr<MotionEntry> splitMotionEntry = |
| 4188 | std::make_unique<MotionEntry>(newId, originalMotionEntry.eventTime, |
| 4189 | originalMotionEntry.deviceId, originalMotionEntry.source, |
| 4190 | originalMotionEntry.displayId, |
| 4191 | originalMotionEntry.policyFlags, action, |
| 4192 | originalMotionEntry.actionButton, |
| 4193 | originalMotionEntry.flags, originalMotionEntry.metaState, |
| 4194 | originalMotionEntry.buttonState, |
| 4195 | originalMotionEntry.classification, |
| 4196 | originalMotionEntry.edgeFlags, |
| 4197 | originalMotionEntry.xPrecision, |
| 4198 | originalMotionEntry.yPrecision, |
| 4199 | originalMotionEntry.xCursorPosition, |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4200 | originalMotionEntry.yCursorPosition, splitDownTime, |
| 4201 | splitPointerCount, splitPointerProperties, |
| 4202 | splitPointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4203 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 4204 | if (originalMotionEntry.injectionState) { |
| 4205 | splitMotionEntry->injectionState = originalMotionEntry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4206 | splitMotionEntry->injectionState->refCount += 1; |
| 4207 | } |
| 4208 | |
| 4209 | return splitMotionEntry; |
| 4210 | } |
| 4211 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4212 | void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4213 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4214 | ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args.eventTime); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4215 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4216 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4217 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4218 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4219 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4220 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4221 | std::unique_ptr<ConfigurationChangedEntry> newEntry = |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4222 | std::make_unique<ConfigurationChangedEntry>(args.id, args.eventTime); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4223 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4224 | } // release lock |
| 4225 | |
| 4226 | if (needWake) { |
| 4227 | mLooper->wake(); |
| 4228 | } |
| 4229 | } |
| 4230 | |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 4231 | /** |
| 4232 | * If one of the meta shortcuts is detected, process them here: |
Vaibhav Devmurari | 34cd5b0 | 2023-02-23 14:51:18 +0000 | [diff] [blame] | 4233 | * Meta + Backspace; Meta + Grave; Meta + Left arrow -> generate BACK |
| 4234 | * Most System shortcuts are handled in PhoneWindowManager.java except 'Back' shortcuts. Unlike |
| 4235 | * Back, other shortcuts DO NOT need to be sent to applications and are fully handled by the system. |
| 4236 | * But for Back key and Back shortcuts, we need to send KEYCODE_BACK to applications which can |
| 4237 | * potentially handle the back key presses. |
| 4238 | * Note: We don't send any Meta based KeyEvents to applications, so we need to convert to a KeyEvent |
| 4239 | * 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] | 4240 | */ |
| 4241 | void InputDispatcher::accelerateMetaShortcuts(const int32_t deviceId, const int32_t action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4242 | int32_t& keyCode, int32_t& metaState) { |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 4243 | if (metaState & AMETA_META_ON && action == AKEY_EVENT_ACTION_DOWN) { |
| 4244 | int32_t newKeyCode = AKEYCODE_UNKNOWN; |
Vaibhav Devmurari | 34cd5b0 | 2023-02-23 14:51:18 +0000 | [diff] [blame] | 4245 | if (keyCode == AKEYCODE_DEL || keyCode == AKEYCODE_GRAVE || keyCode == AKEYCODE_DPAD_LEFT) { |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 4246 | newKeyCode = AKEYCODE_BACK; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 4247 | } |
| 4248 | if (newKeyCode != AKEYCODE_UNKNOWN) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4249 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 4250 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4251 | mReplacedKeys[replacement] = newKeyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 4252 | keyCode = newKeyCode; |
| 4253 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 4254 | } |
| 4255 | } else if (action == AKEY_EVENT_ACTION_UP) { |
| 4256 | // In order to maintain a consistent stream of up and down events, check to see if the key |
| 4257 | // going up is one we've replaced in a down event and haven't yet replaced in an up event, |
| 4258 | // 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] | 4259 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 4260 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4261 | auto replacementIt = mReplacedKeys.find(replacement); |
| 4262 | if (replacementIt != mReplacedKeys.end()) { |
| 4263 | keyCode = replacementIt->second; |
| 4264 | mReplacedKeys.erase(replacementIt); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 4265 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 4266 | } |
| 4267 | } |
| 4268 | } |
| 4269 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4270 | void InputDispatcher::notifyKey(const NotifyKeyArgs& args) { |
Prabir Pradhan | 96282b0 | 2023-02-24 22:36:17 +0000 | [diff] [blame] | 4271 | ALOGD_IF(debugInboundEventDetails(), |
| 4272 | "notifyKey - id=%" PRIx32 ", eventTime=%" PRId64 |
| 4273 | ", deviceId=%d, source=%s, displayId=%" PRId32 |
| 4274 | "policyFlags=0x%x, action=%s, flags=0x%x, keyCode=%s, scanCode=0x%x, metaState=0x%x, " |
| 4275 | "downTime=%" PRId64, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4276 | args.id, args.eventTime, args.deviceId, inputEventSourceToString(args.source).c_str(), |
| 4277 | args.displayId, args.policyFlags, KeyEvent::actionToString(args.action), args.flags, |
| 4278 | KeyEvent::getLabel(args.keyCode), args.scanCode, args.metaState, args.downTime); |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4279 | Result<void> keyCheck = validateKeyEvent(args.action); |
| 4280 | if (!keyCheck.ok()) { |
| 4281 | LOG(ERROR) << "invalid key event: " << keyCheck.error(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4282 | return; |
| 4283 | } |
| 4284 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4285 | uint32_t policyFlags = args.policyFlags; |
| 4286 | int32_t flags = args.flags; |
| 4287 | int32_t metaState = args.metaState; |
Siarhei Vishniakou | 622bd32 | 2018-10-29 18:02:27 -0700 | [diff] [blame] | 4288 | // InputDispatcher tracks and generates key repeats on behalf of |
| 4289 | // whatever notifies it, so repeatCount should always be set to 0 |
| 4290 | constexpr int32_t repeatCount = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4291 | if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { |
| 4292 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 4293 | flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
| 4294 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4295 | if (policyFlags & POLICY_FLAG_FUNCTION) { |
| 4296 | metaState |= AMETA_FUNCTION_ON; |
| 4297 | } |
| 4298 | |
| 4299 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 4300 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4301 | int32_t keyCode = args.keyCode; |
| 4302 | accelerateMetaShortcuts(args.deviceId, args.action, keyCode, metaState); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 4303 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4304 | KeyEvent event; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4305 | event.initialize(args.id, args.deviceId, args.source, args.displayId, INVALID_HMAC, args.action, |
| 4306 | flags, keyCode, args.scanCode, metaState, repeatCount, args.downTime, |
| 4307 | args.eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4308 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4309 | android::base::Timer t; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4310 | mPolicy.interceptKeyBeforeQueueing(event, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4311 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4312 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4313 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4314 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4315 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4316 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4317 | { // acquire lock |
| 4318 | mLock.lock(); |
| 4319 | |
| 4320 | if (shouldSendKeyToInputFilterLocked(args)) { |
| 4321 | mLock.unlock(); |
| 4322 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4323 | policyFlags |= POLICY_FLAG_FILTERED; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4324 | if (!mPolicy.filterInputEvent(event, policyFlags)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4325 | return; // event was consumed by the filter |
| 4326 | } |
| 4327 | |
| 4328 | mLock.lock(); |
| 4329 | } |
| 4330 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4331 | std::unique_ptr<KeyEntry> newEntry = |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4332 | std::make_unique<KeyEntry>(args.id, args.eventTime, args.deviceId, args.source, |
| 4333 | args.displayId, policyFlags, args.action, flags, keyCode, |
| 4334 | args.scanCode, metaState, repeatCount, args.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4335 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4336 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4337 | mLock.unlock(); |
| 4338 | } // release lock |
| 4339 | |
| 4340 | if (needWake) { |
| 4341 | mLooper->wake(); |
| 4342 | } |
| 4343 | } |
| 4344 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4345 | bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs& args) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4346 | return mInputFilterEnabled; |
| 4347 | } |
| 4348 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4349 | void InputDispatcher::notifyMotion(const NotifyMotionArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4350 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 96282b0 | 2023-02-24 22:36:17 +0000 | [diff] [blame] | 4351 | ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=%s, " |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4352 | "displayId=%" PRId32 ", policyFlags=0x%x, " |
Siarhei Vishniakou | 6ebd069 | 2022-10-20 15:05:45 -0700 | [diff] [blame] | 4353 | "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] | 4354 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, xCursorPosition=%f, " |
| 4355 | "yCursorPosition=%f, downTime=%" PRId64, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4356 | args.id, args.eventTime, args.deviceId, inputEventSourceToString(args.source).c_str(), |
| 4357 | args.displayId, args.policyFlags, MotionEvent::actionToString(args.action).c_str(), |
| 4358 | args.actionButton, args.flags, args.metaState, args.buttonState, args.edgeFlags, |
| 4359 | args.xPrecision, args.yPrecision, args.xCursorPosition, args.yCursorPosition, |
| 4360 | args.downTime); |
Siarhei Vishniakou | 3218fc0 | 2023-06-15 20:41:02 -0700 | [diff] [blame] | 4361 | for (uint32_t i = 0; i < args.getPointerCount(); i++) { |
Prabir Pradhan | 96282b0 | 2023-02-24 22:36:17 +0000 | [diff] [blame] | 4362 | ALOGD(" Pointer %d: id=%d, toolType=%s, x=%f, y=%f, pressure=%f, size=%f, " |
| 4363 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, orientation=%f", |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4364 | i, args.pointerProperties[i].id, |
| 4365 | ftl::enum_string(args.pointerProperties[i].toolType).c_str(), |
| 4366 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 4367 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 4368 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 4369 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 4370 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 4371 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 4372 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 4373 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 4374 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4375 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4376 | } |
Siarhei Vishniakou | 4ca9727 | 2023-03-01 11:31:35 -0800 | [diff] [blame] | 4377 | |
Siarhei Vishniakou | 3218fc0 | 2023-06-15 20:41:02 -0700 | [diff] [blame] | 4378 | Result<void> motionCheck = |
| 4379 | validateMotionEvent(args.action, args.actionButton, args.getPointerCount(), |
| 4380 | args.pointerProperties.data()); |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4381 | if (!motionCheck.ok()) { |
| 4382 | LOG(ERROR) << "Invalid event: " << args.dump() << "; reason: " << motionCheck.error(); |
Siarhei Vishniakou | 4ca9727 | 2023-03-01 11:31:35 -0800 | [diff] [blame] | 4383 | return; |
| 4384 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4385 | |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 4386 | if (DEBUG_VERIFY_EVENTS) { |
| 4387 | auto [it, _] = |
| 4388 | mVerifiersByDisplay.try_emplace(args.displayId, |
| 4389 | StringPrintf("display %" PRId32, args.displayId)); |
| 4390 | Result<void> result = |
Siarhei Vishniakou | 3218fc0 | 2023-06-15 20:41:02 -0700 | [diff] [blame] | 4391 | it->second.processMovement(args.deviceId, args.action, args.getPointerCount(), |
| 4392 | args.pointerProperties.data(), args.pointerCoords.data(), |
| 4393 | args.flags); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 4394 | if (!result.ok()) { |
| 4395 | LOG(FATAL) << "Bad stream: " << result.error() << " caused by " << args.dump(); |
| 4396 | } |
| 4397 | } |
| 4398 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4399 | uint32_t policyFlags = args.policyFlags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4400 | policyFlags |= POLICY_FLAG_TRUSTED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4401 | |
| 4402 | android::base::Timer t; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4403 | mPolicy.interceptMotionBeforeQueueing(args.displayId, args.eventTime, policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4404 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4405 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4406 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4407 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4408 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4409 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4410 | { // acquire lock |
| 4411 | mLock.lock(); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 4412 | if (!(policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
| 4413 | // Set the flag anyway if we already have an ongoing gesture. That would allow us to |
| 4414 | // complete the processing of the current stroke. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4415 | const auto touchStateIt = mTouchStatesByDisplay.find(args.displayId); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 4416 | if (touchStateIt != mTouchStatesByDisplay.end()) { |
| 4417 | const TouchState& touchState = touchStateIt->second; |
Siarhei Vishniakou | 45504fe | 2023-05-05 16:05:10 -0700 | [diff] [blame] | 4418 | if (touchState.hasTouchingPointers(args.deviceId)) { |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 4419 | policyFlags |= POLICY_FLAG_PASS_TO_USER; |
| 4420 | } |
| 4421 | } |
| 4422 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4423 | |
| 4424 | if (shouldSendMotionToInputFilterLocked(args)) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4425 | ui::Transform displayTransform; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4426 | if (const auto it = mDisplayInfos.find(args.displayId); it != mDisplayInfos.end()) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4427 | displayTransform = it->second.transform; |
| 4428 | } |
| 4429 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4430 | mLock.unlock(); |
| 4431 | |
| 4432 | MotionEvent event; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4433 | event.initialize(args.id, args.deviceId, args.source, args.displayId, INVALID_HMAC, |
| 4434 | args.action, args.actionButton, args.flags, args.edgeFlags, |
| 4435 | args.metaState, args.buttonState, args.classification, |
| 4436 | displayTransform, args.xPrecision, args.yPrecision, |
| 4437 | args.xCursorPosition, args.yCursorPosition, displayTransform, |
Siarhei Vishniakou | 3218fc0 | 2023-06-15 20:41:02 -0700 | [diff] [blame] | 4438 | args.downTime, args.eventTime, args.getPointerCount(), |
| 4439 | args.pointerProperties.data(), args.pointerCoords.data()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4440 | |
| 4441 | policyFlags |= POLICY_FLAG_FILTERED; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4442 | if (!mPolicy.filterInputEvent(event, policyFlags)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4443 | return; // event was consumed by the filter |
| 4444 | } |
| 4445 | |
| 4446 | mLock.lock(); |
| 4447 | } |
| 4448 | |
| 4449 | // Just enqueue a new motion event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4450 | std::unique_ptr<MotionEntry> newEntry = |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4451 | std::make_unique<MotionEntry>(args.id, args.eventTime, args.deviceId, args.source, |
| 4452 | args.displayId, policyFlags, args.action, |
| 4453 | args.actionButton, args.flags, args.metaState, |
| 4454 | args.buttonState, args.classification, args.edgeFlags, |
| 4455 | args.xPrecision, args.yPrecision, |
| 4456 | args.xCursorPosition, args.yCursorPosition, |
Siarhei Vishniakou | 3218fc0 | 2023-06-15 20:41:02 -0700 | [diff] [blame] | 4457 | args.downTime, args.getPointerCount(), |
| 4458 | args.pointerProperties.data(), |
| 4459 | args.pointerCoords.data()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4460 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4461 | if (args.id != android::os::IInputConstants::INVALID_INPUT_EVENT_ID && |
| 4462 | IdGenerator::getSource(args.id) == IdGenerator::Source::INPUT_READER && |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 4463 | !mInputFilterEnabled) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4464 | const bool isDown = args.action == AMOTION_EVENT_ACTION_DOWN; |
| 4465 | mLatencyTracker.trackListener(args.id, isDown, args.eventTime, args.readTime); |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 4466 | } |
| 4467 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4468 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4469 | mLock.unlock(); |
| 4470 | } // release lock |
| 4471 | |
| 4472 | if (needWake) { |
| 4473 | mLooper->wake(); |
| 4474 | } |
| 4475 | } |
| 4476 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4477 | void InputDispatcher::notifySensor(const NotifySensorArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4478 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4479 | ALOGD("notifySensor - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 4480 | " sensorType=%s", |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4481 | args.id, args.eventTime, args.deviceId, args.source, |
| 4482 | ftl::enum_string(args.sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4483 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4484 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4485 | bool needWake = false; |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4486 | { // acquire lock |
| 4487 | mLock.lock(); |
| 4488 | |
| 4489 | // Just enqueue a new sensor event. |
| 4490 | std::unique_ptr<SensorEntry> newEntry = |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4491 | std::make_unique<SensorEntry>(args.id, args.eventTime, args.deviceId, args.source, |
| 4492 | /* policyFlags=*/0, args.hwTimestamp, args.sensorType, |
| 4493 | args.accuracy, args.accuracyChanged, args.values); |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4494 | |
| 4495 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
| 4496 | mLock.unlock(); |
| 4497 | } // release lock |
| 4498 | |
| 4499 | if (needWake) { |
| 4500 | mLooper->wake(); |
| 4501 | } |
| 4502 | } |
| 4503 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4504 | void InputDispatcher::notifyVibratorState(const NotifyVibratorStateArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4505 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4506 | ALOGD("notifyVibratorState - eventTime=%" PRId64 ", device=%d, isOn=%d", args.eventTime, |
| 4507 | args.deviceId, args.isOn); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4508 | } |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4509 | mPolicy.notifyVibratorState(args.deviceId, args.isOn); |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 4510 | } |
| 4511 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4512 | bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs& args) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4513 | return mInputFilterEnabled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4514 | } |
| 4515 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4516 | void InputDispatcher::notifySwitch(const NotifySwitchArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4517 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4518 | ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, " |
| 4519 | "switchMask=0x%08x", |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4520 | args.eventTime, args.policyFlags, args.switchValues, args.switchMask); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4521 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4522 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4523 | uint32_t policyFlags = args.policyFlags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4524 | policyFlags |= POLICY_FLAG_TRUSTED; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4525 | mPolicy.notifySwitch(args.eventTime, args.switchValues, args.switchMask, policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4526 | } |
| 4527 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4528 | void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4529 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4530 | ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args.eventTime, |
| 4531 | args.deviceId); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4532 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4533 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4534 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4535 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4536 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4537 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4538 | std::unique_ptr<DeviceResetEntry> newEntry = |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4539 | std::make_unique<DeviceResetEntry>(args.id, args.eventTime, args.deviceId); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4540 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4541 | } // release lock |
| 4542 | |
| 4543 | if (needWake) { |
| 4544 | mLooper->wake(); |
| 4545 | } |
| 4546 | } |
| 4547 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4548 | void InputDispatcher::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4549 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4550 | ALOGD("notifyPointerCaptureChanged - eventTime=%" PRId64 ", enabled=%s", args.eventTime, |
| 4551 | args.request.enable ? "true" : "false"); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4552 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4553 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4554 | bool needWake = false; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4555 | { // acquire lock |
| 4556 | std::scoped_lock _l(mLock); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4557 | auto entry = |
| 4558 | std::make_unique<PointerCaptureChangedEntry>(args.id, args.eventTime, args.request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4559 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 4560 | } // release lock |
| 4561 | |
| 4562 | if (needWake) { |
| 4563 | mLooper->wake(); |
| 4564 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4565 | } |
| 4566 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4567 | InputEventInjectionResult InputDispatcher::injectInputEvent(const InputEvent* event, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 4568 | std::optional<gui::Uid> targetUid, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4569 | InputEventInjectionSync syncMode, |
| 4570 | std::chrono::milliseconds timeout, |
| 4571 | uint32_t policyFlags) { |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4572 | Result<void> eventValidation = validateInputEvent(*event); |
| 4573 | if (!eventValidation.ok()) { |
| 4574 | LOG(INFO) << "Injection failed: invalid event: " << eventValidation.error(); |
| 4575 | return InputEventInjectionResult::FAILED; |
| 4576 | } |
| 4577 | |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4578 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 4579 | LOG(DEBUG) << __func__ << ": targetUid=" << toString(targetUid, &uidString) |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4580 | << ", syncMode=" << ftl::enum_string(syncMode) << ", timeout=" << timeout.count() |
| 4581 | << "ms, policyFlags=0x" << std::hex << policyFlags << std::dec |
| 4582 | << ", event=" << *event; |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4583 | } |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 4584 | 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] | 4585 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4586 | policyFlags |= POLICY_FLAG_INJECTED | POLICY_FLAG_TRUSTED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4587 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4588 | // 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] | 4589 | // that have gone through the InputFilter. If the event passed through the InputFilter, assign |
| 4590 | // the provided device id. If the InputFilter is accessibility, and it modifies or synthesizes |
| 4591 | // the injected event, it is responsible for setting POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY. |
| 4592 | // For those events, we will set FLAG_IS_ACCESSIBILITY_EVENT to allow apps to distinguish them |
| 4593 | // from events that originate from actual hardware. |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4594 | int32_t resolvedDeviceId = VIRTUAL_KEYBOARD_ID; |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4595 | if (policyFlags & POLICY_FLAG_FILTERED) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4596 | resolvedDeviceId = event->getDeviceId(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4597 | } |
| 4598 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4599 | std::queue<std::unique_ptr<EventEntry>> injectedEntries; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4600 | switch (event->getType()) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4601 | case InputEventType::KEY: { |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4602 | const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event); |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4603 | const int32_t action = incomingKey.getAction(); |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4604 | int32_t flags = incomingKey.getFlags(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4605 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4606 | flags |= AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4607 | } |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4608 | int32_t keyCode = incomingKey.getKeyCode(); |
| 4609 | int32_t metaState = incomingKey.getMetaState(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4610 | accelerateMetaShortcuts(resolvedDeviceId, action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4611 | /*byref*/ keyCode, /*byref*/ metaState); |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4612 | KeyEvent keyEvent; |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4613 | keyEvent.initialize(incomingKey.getId(), resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4614 | incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode, |
| 4615 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), |
| 4616 | incomingKey.getDownTime(), incomingKey.getEventTime()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4617 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4618 | if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) { |
| 4619 | policyFlags |= POLICY_FLAG_VIRTUAL; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4620 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4621 | |
| 4622 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 4623 | android::base::Timer t; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4624 | mPolicy.interceptKeyBeforeQueueing(keyEvent, /*byref*/ policyFlags); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4625 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4626 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
| 4627 | std::to_string(t.duration().count()).c_str()); |
| 4628 | } |
| 4629 | } |
| 4630 | |
| 4631 | mLock.lock(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4632 | std::unique_ptr<KeyEntry> injectedEntry = |
| 4633 | std::make_unique<KeyEntry>(incomingKey.getId(), incomingKey.getEventTime(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4634 | resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4635 | incomingKey.getDisplayId(), policyFlags, action, |
| 4636 | flags, keyCode, incomingKey.getScanCode(), metaState, |
| 4637 | incomingKey.getRepeatCount(), |
| 4638 | incomingKey.getDownTime()); |
| 4639 | injectedEntries.push(std::move(injectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4640 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4641 | } |
| 4642 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4643 | case InputEventType::MOTION: { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4644 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4645 | const bool isPointerEvent = |
| 4646 | isFromSource(event->getSource(), AINPUT_SOURCE_CLASS_POINTER); |
| 4647 | // If a pointer event has no displayId specified, inject it to the default display. |
| 4648 | const uint32_t displayId = isPointerEvent && (event->getDisplayId() == ADISPLAY_ID_NONE) |
| 4649 | ? ADISPLAY_ID_DEFAULT |
| 4650 | : event->getDisplayId(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4651 | int32_t flags = motionEvent.getFlags(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4652 | |
| 4653 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4654 | nsecs_t eventTime = motionEvent.getEventTime(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4655 | android::base::Timer t; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4656 | mPolicy.interceptMotionBeforeQueueing(displayId, eventTime, /*byref*/ policyFlags); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4657 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4658 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
| 4659 | std::to_string(t.duration().count()).c_str()); |
| 4660 | } |
| 4661 | } |
| 4662 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4663 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4664 | flags |= AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4665 | } |
| 4666 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4667 | mLock.lock(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4668 | const nsecs_t* sampleEventTimes = motionEvent.getSampleEventTimes(); |
| 4669 | const PointerCoords* samplePointerCoords = motionEvent.getSamplePointerCoords(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4670 | std::unique_ptr<MotionEntry> injectedEntry = |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4671 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, |
| 4672 | resolvedDeviceId, motionEvent.getSource(), |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4673 | displayId, policyFlags, motionEvent.getAction(), |
| 4674 | motionEvent.getActionButton(), flags, |
| 4675 | motionEvent.getMetaState(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4676 | motionEvent.getButtonState(), |
| 4677 | motionEvent.getClassification(), |
| 4678 | motionEvent.getEdgeFlags(), |
| 4679 | motionEvent.getXPrecision(), |
| 4680 | motionEvent.getYPrecision(), |
| 4681 | motionEvent.getRawXCursorPosition(), |
| 4682 | motionEvent.getRawYCursorPosition(), |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4683 | motionEvent.getDownTime(), |
| 4684 | motionEvent.getPointerCount(), |
| 4685 | motionEvent.getPointerProperties(), |
| 4686 | samplePointerCoords); |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4687 | transformMotionEntryForInjectionLocked(*injectedEntry, motionEvent.getTransform()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4688 | injectedEntries.push(std::move(injectedEntry)); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4689 | for (size_t i = motionEvent.getHistorySize(); i > 0; i--) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4690 | sampleEventTimes += 1; |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4691 | samplePointerCoords += motionEvent.getPointerCount(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4692 | std::unique_ptr<MotionEntry> nextInjectedEntry = |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4693 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, |
| 4694 | resolvedDeviceId, motionEvent.getSource(), |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4695 | displayId, policyFlags, |
| 4696 | motionEvent.getAction(), |
| 4697 | motionEvent.getActionButton(), flags, |
| 4698 | motionEvent.getMetaState(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4699 | motionEvent.getButtonState(), |
| 4700 | motionEvent.getClassification(), |
| 4701 | motionEvent.getEdgeFlags(), |
| 4702 | motionEvent.getXPrecision(), |
| 4703 | motionEvent.getYPrecision(), |
| 4704 | motionEvent.getRawXCursorPosition(), |
| 4705 | motionEvent.getRawYCursorPosition(), |
| 4706 | motionEvent.getDownTime(), |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4707 | motionEvent.getPointerCount(), |
| 4708 | motionEvent.getPointerProperties(), |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4709 | samplePointerCoords); |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4710 | transformMotionEntryForInjectionLocked(*nextInjectedEntry, |
| 4711 | motionEvent.getTransform()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4712 | injectedEntries.push(std::move(nextInjectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4713 | } |
| 4714 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4715 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4716 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4717 | default: |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4718 | LOG(WARNING) << "Cannot inject " << ftl::enum_string(event->getType()) << " events"; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4719 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4720 | } |
| 4721 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4722 | InjectionState* injectionState = new InjectionState(targetUid); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4723 | if (syncMode == InputEventInjectionSync::NONE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4724 | injectionState->injectionIsAsync = true; |
| 4725 | } |
| 4726 | |
| 4727 | injectionState->refCount += 1; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4728 | injectedEntries.back()->injectionState = injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4729 | |
| 4730 | bool needWake = false; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4731 | while (!injectedEntries.empty()) { |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 4732 | if (DEBUG_INJECTION) { |
| 4733 | LOG(DEBUG) << "Injecting " << injectedEntries.front()->getDescription(); |
| 4734 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4735 | needWake |= enqueueInboundEventLocked(std::move(injectedEntries.front())); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4736 | injectedEntries.pop(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4737 | } |
| 4738 | |
| 4739 | mLock.unlock(); |
| 4740 | |
| 4741 | if (needWake) { |
| 4742 | mLooper->wake(); |
| 4743 | } |
| 4744 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4745 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4746 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4747 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4748 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4749 | if (syncMode == InputEventInjectionSync::NONE) { |
| 4750 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4751 | } else { |
| 4752 | for (;;) { |
| 4753 | injectionResult = injectionState->injectionResult; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4754 | if (injectionResult != InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4755 | break; |
| 4756 | } |
| 4757 | |
| 4758 | nsecs_t remainingTimeout = endTime - now(); |
| 4759 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4760 | if (DEBUG_INJECTION) { |
| 4761 | ALOGD("injectInputEvent - Timed out waiting for injection result " |
| 4762 | "to become available."); |
| 4763 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4764 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4765 | break; |
| 4766 | } |
| 4767 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4768 | mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4769 | } |
| 4770 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4771 | if (injectionResult == InputEventInjectionResult::SUCCEEDED && |
| 4772 | syncMode == InputEventInjectionSync::WAIT_FOR_FINISHED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4773 | while (injectionState->pendingForegroundDispatches != 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4774 | if (DEBUG_INJECTION) { |
| 4775 | ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", |
| 4776 | injectionState->pendingForegroundDispatches); |
| 4777 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4778 | nsecs_t remainingTimeout = endTime - now(); |
| 4779 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4780 | if (DEBUG_INJECTION) { |
| 4781 | ALOGD("injectInputEvent - Timed out waiting for pending foreground " |
| 4782 | "dispatches to finish."); |
| 4783 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4784 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4785 | break; |
| 4786 | } |
| 4787 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4788 | mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4789 | } |
| 4790 | } |
| 4791 | } |
| 4792 | |
| 4793 | injectionState->release(); |
| 4794 | } // release lock |
| 4795 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4796 | if (DEBUG_INJECTION) { |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 4797 | LOG(DEBUG) << "injectInputEvent - Finished with result " |
| 4798 | << ftl::enum_string(injectionResult); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4799 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4800 | |
| 4801 | return injectionResult; |
| 4802 | } |
| 4803 | |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4804 | std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4805 | std::array<uint8_t, 32> calculatedHmac; |
| 4806 | std::unique_ptr<VerifiedInputEvent> result; |
| 4807 | switch (event.getType()) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4808 | case InputEventType::KEY: { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4809 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event); |
| 4810 | VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent); |
| 4811 | result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4812 | calculatedHmac = sign(verifiedKeyEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4813 | break; |
| 4814 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4815 | case InputEventType::MOTION: { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4816 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event); |
| 4817 | VerifiedMotionEvent verifiedMotionEvent = |
| 4818 | verifiedMotionEventFromMotionEvent(motionEvent); |
| 4819 | result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4820 | calculatedHmac = sign(verifiedMotionEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4821 | break; |
| 4822 | } |
| 4823 | default: { |
| 4824 | ALOGE("Cannot verify events of type %" PRId32, event.getType()); |
| 4825 | return nullptr; |
| 4826 | } |
| 4827 | } |
| 4828 | if (calculatedHmac == INVALID_HMAC) { |
| 4829 | return nullptr; |
| 4830 | } |
tyiu | 1573a67 | 2023-02-21 22:38:32 +0000 | [diff] [blame] | 4831 | if (0 != CRYPTO_memcmp(calculatedHmac.data(), event.getHmac().data(), calculatedHmac.size())) { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4832 | return nullptr; |
| 4833 | } |
| 4834 | return result; |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4835 | } |
| 4836 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4837 | void InputDispatcher::setInjectionResult(EventEntry& entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4838 | InputEventInjectionResult injectionResult) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4839 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4840 | if (injectionState) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4841 | if (DEBUG_INJECTION) { |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 4842 | LOG(DEBUG) << "Setting input event injection result to " |
| 4843 | << ftl::enum_string(injectionResult); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4844 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4845 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4846 | if (injectionState->injectionIsAsync && !(entry.policyFlags & POLICY_FLAG_FILTERED)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4847 | // Log the outcome since the injector did not wait for the injection result. |
| 4848 | switch (injectionResult) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4849 | case InputEventInjectionResult::SUCCEEDED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4850 | ALOGV("Asynchronous input event injection succeeded."); |
| 4851 | break; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4852 | case InputEventInjectionResult::TARGET_MISMATCH: |
| 4853 | ALOGV("Asynchronous input event injection target mismatch."); |
| 4854 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4855 | case InputEventInjectionResult::FAILED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4856 | ALOGW("Asynchronous input event injection failed."); |
| 4857 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4858 | case InputEventInjectionResult::TIMED_OUT: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4859 | ALOGW("Asynchronous input event injection timed out."); |
| 4860 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4861 | case InputEventInjectionResult::PENDING: |
| 4862 | ALOGE("Setting result to 'PENDING' for asynchronous injection"); |
| 4863 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4864 | } |
| 4865 | } |
| 4866 | |
| 4867 | injectionState->injectionResult = injectionResult; |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4868 | mInjectionResultAvailable.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4869 | } |
| 4870 | } |
| 4871 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4872 | void InputDispatcher::transformMotionEntryForInjectionLocked( |
| 4873 | MotionEntry& entry, const ui::Transform& injectedTransform) const { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4874 | // Input injection works in the logical display coordinate space, but the input pipeline works |
| 4875 | // display space, so we need to transform the injected events accordingly. |
| 4876 | const auto it = mDisplayInfos.find(entry.displayId); |
| 4877 | if (it == mDisplayInfos.end()) return; |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4878 | const auto& transformToDisplay = it->second.transform.inverse() * injectedTransform; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4879 | |
Prabir Pradhan | d9a2ebe | 2022-07-20 19:25:13 +0000 | [diff] [blame] | 4880 | if (entry.xCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION && |
| 4881 | entry.yCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) { |
| 4882 | const vec2 cursor = |
| 4883 | MotionEvent::calculateTransformedXY(entry.source, transformToDisplay, |
| 4884 | {entry.xCursorPosition, entry.yCursorPosition}); |
| 4885 | entry.xCursorPosition = cursor.x; |
| 4886 | entry.yCursorPosition = cursor.y; |
| 4887 | } |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4888 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
Prabir Pradhan | 8e6ce22 | 2022-02-24 09:08:54 -0800 | [diff] [blame] | 4889 | entry.pointerCoords[i] = |
| 4890 | MotionEvent::calculateTransformedCoords(entry.source, transformToDisplay, |
| 4891 | entry.pointerCoords[i]); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4892 | } |
| 4893 | } |
| 4894 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4895 | void InputDispatcher::incrementPendingForegroundDispatches(EventEntry& entry) { |
| 4896 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4897 | if (injectionState) { |
| 4898 | injectionState->pendingForegroundDispatches += 1; |
| 4899 | } |
| 4900 | } |
| 4901 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4902 | void InputDispatcher::decrementPendingForegroundDispatches(EventEntry& entry) { |
| 4903 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4904 | if (injectionState) { |
| 4905 | injectionState->pendingForegroundDispatches -= 1; |
| 4906 | |
| 4907 | if (injectionState->pendingForegroundDispatches == 0) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4908 | mInjectionSyncFinished.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4909 | } |
| 4910 | } |
| 4911 | } |
| 4912 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4913 | const std::vector<sp<WindowInfoHandle>>& InputDispatcher::getWindowHandlesLocked( |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4914 | int32_t displayId) const { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4915 | static const std::vector<sp<WindowInfoHandle>> EMPTY_WINDOW_HANDLES; |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4916 | auto it = mWindowHandlesByDisplay.find(displayId); |
| 4917 | return it != mWindowHandlesByDisplay.end() ? it->second : EMPTY_WINDOW_HANDLES; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4918 | } |
| 4919 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4920 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4921 | const sp<IBinder>& windowHandleToken) const { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4922 | if (windowHandleToken == nullptr) { |
| 4923 | return nullptr; |
| 4924 | } |
| 4925 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4926 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4927 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 4928 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4929 | if (windowHandle->getToken() == windowHandleToken) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4930 | return windowHandle; |
| 4931 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4932 | } |
| 4933 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4934 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4935 | } |
| 4936 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4937 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked(const sp<IBinder>& windowHandleToken, |
| 4938 | int displayId) const { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4939 | if (windowHandleToken == nullptr) { |
| 4940 | return nullptr; |
| 4941 | } |
| 4942 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4943 | for (const sp<WindowInfoHandle>& windowHandle : getWindowHandlesLocked(displayId)) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4944 | if (windowHandle->getToken() == windowHandleToken) { |
| 4945 | return windowHandle; |
| 4946 | } |
| 4947 | } |
| 4948 | return nullptr; |
| 4949 | } |
| 4950 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4951 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
| 4952 | const sp<WindowInfoHandle>& windowHandle) const { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4953 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4954 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 4955 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4956 | if (handle->getId() == windowHandle->getId() && |
| 4957 | handle->getToken() == windowHandle->getToken()) { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4958 | if (windowHandle->getInfo()->displayId != it.first) { |
| 4959 | ALOGE("Found window %s in display %" PRId32 |
| 4960 | ", but it should belong to display %" PRId32, |
| 4961 | windowHandle->getName().c_str(), it.first, |
| 4962 | windowHandle->getInfo()->displayId); |
| 4963 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4964 | return handle; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4965 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4966 | } |
| 4967 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4968 | return nullptr; |
| 4969 | } |
| 4970 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4971 | sp<WindowInfoHandle> InputDispatcher::getFocusedWindowHandleLocked(int displayId) const { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4972 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(displayId); |
| 4973 | return getWindowHandleLocked(focusedToken, displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4974 | } |
| 4975 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4976 | ui::Transform InputDispatcher::getTransformLocked(int32_t displayId) const { |
| 4977 | auto displayInfoIt = mDisplayInfos.find(displayId); |
| 4978 | return displayInfoIt != mDisplayInfos.end() ? displayInfoIt->second.transform |
| 4979 | : kIdentityTransform; |
| 4980 | } |
| 4981 | |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 4982 | bool InputDispatcher::canWindowReceiveMotionLocked(const sp<WindowInfoHandle>& window, |
| 4983 | const MotionEntry& motionEntry) const { |
| 4984 | const WindowInfo& info = *window->getInfo(); |
| 4985 | |
| 4986 | // Skip spy window targets that are not valid for targeted injection. |
| 4987 | if (const auto err = verifyTargetedInjection(window, motionEntry); err) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4988 | return false; |
| 4989 | } |
| 4990 | |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 4991 | if (info.inputConfig.test(WindowInfo::InputConfig::PAUSE_DISPATCHING)) { |
| 4992 | ALOGI("Not sending touch event to %s because it is paused", window->getName().c_str()); |
| 4993 | return false; |
| 4994 | } |
| 4995 | |
| 4996 | if (info.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) { |
| 4997 | ALOGW("Not sending touch gesture to %s because it has config NO_INPUT_CHANNEL", |
| 4998 | window->getName().c_str()); |
| 4999 | return false; |
| 5000 | } |
| 5001 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 5002 | std::shared_ptr<Connection> connection = getConnectionLocked(window->getToken()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5003 | if (connection == nullptr) { |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5004 | ALOGW("Not sending touch to %s because there's no corresponding connection", |
| 5005 | window->getName().c_str()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5006 | return false; |
| 5007 | } |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5008 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5009 | if (!connection->responsive) { |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5010 | 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] | 5011 | return false; |
| 5012 | } |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5013 | |
| 5014 | // Drop events that can't be trusted due to occlusion |
| 5015 | const auto [x, y] = resolveTouchedPosition(motionEntry); |
| 5016 | TouchOcclusionInfo occlusionInfo = computeTouchOcclusionInfoLocked(window, x, y); |
| 5017 | if (!isTouchTrustedLocked(occlusionInfo)) { |
| 5018 | if (DEBUG_TOUCH_OCCLUSION) { |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 5019 | ALOGD("Stack of obscuring windows during untrusted touch (%.1f, %.1f):", x, y); |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5020 | for (const auto& log : occlusionInfo.debugInfo) { |
| 5021 | ALOGD("%s", log.c_str()); |
| 5022 | } |
| 5023 | } |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 5024 | ALOGW("Dropping untrusted touch event due to %s/%s", occlusionInfo.obscuringPackage.c_str(), |
| 5025 | occlusionInfo.obscuringUid.toString().c_str()); |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5026 | return false; |
| 5027 | } |
| 5028 | |
| 5029 | // Drop touch events if requested by input feature |
| 5030 | if (shouldDropInput(motionEntry, window)) { |
| 5031 | return false; |
| 5032 | } |
| 5033 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5034 | return true; |
| 5035 | } |
| 5036 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5037 | std::shared_ptr<InputChannel> InputDispatcher::getInputChannelLocked( |
| 5038 | const sp<IBinder>& token) const { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5039 | auto connectionIt = mConnectionsByToken.find(token); |
| 5040 | if (connectionIt == mConnectionsByToken.end()) { |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 5041 | return nullptr; |
| 5042 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5043 | return connectionIt->second->inputChannel; |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 5044 | } |
| 5045 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5046 | void InputDispatcher::updateWindowHandlesForDisplayLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5047 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
| 5048 | if (windowInfoHandles.empty()) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5049 | // Remove all handles on a display if there are no windows left. |
| 5050 | mWindowHandlesByDisplay.erase(displayId); |
| 5051 | return; |
| 5052 | } |
| 5053 | |
| 5054 | // Since we compare the pointer of input window handles across window updates, we need |
| 5055 | // 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] | 5056 | const std::vector<sp<WindowInfoHandle>>& oldHandles = getWindowHandlesLocked(displayId); |
| 5057 | std::unordered_map<int32_t /*id*/, sp<WindowInfoHandle>> oldHandlesById; |
| 5058 | for (const sp<WindowInfoHandle>& handle : oldHandles) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5059 | oldHandlesById[handle->getId()] = handle; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5060 | } |
| 5061 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5062 | std::vector<sp<WindowInfoHandle>> newHandles; |
| 5063 | for (const sp<WindowInfoHandle>& handle : windowInfoHandles) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5064 | const WindowInfo* info = handle->getInfo(); |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 5065 | if (getInputChannelLocked(handle->getToken()) == nullptr) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5066 | const bool noInputChannel = |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 5067 | info->inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5068 | const bool canReceiveInput = |
| 5069 | !info->inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE) || |
| 5070 | !info->inputConfig.test(WindowInfo::InputConfig::NOT_FOCUSABLE); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5071 | if (canReceiveInput && !noInputChannel) { |
John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 5072 | ALOGV("Window handle %s has no registered input channel", |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5073 | handle->getName().c_str()); |
Robert Carr | 2984b7a | 2020-04-13 17:06:45 -0700 | [diff] [blame] | 5074 | continue; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5075 | } |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5076 | } |
| 5077 | |
| 5078 | if (info->displayId != displayId) { |
| 5079 | ALOGE("Window %s updated by wrong display %d, should belong to display %d", |
| 5080 | handle->getName().c_str(), displayId, info->displayId); |
| 5081 | continue; |
| 5082 | } |
| 5083 | |
Robert Carr | edd1360 | 2020-04-13 17:24:34 -0700 | [diff] [blame] | 5084 | if ((oldHandlesById.find(handle->getId()) != oldHandlesById.end()) && |
| 5085 | (oldHandlesById.at(handle->getId())->getToken() == handle->getToken())) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5086 | const sp<WindowInfoHandle>& oldHandle = oldHandlesById.at(handle->getId()); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5087 | oldHandle->updateFrom(handle); |
| 5088 | newHandles.push_back(oldHandle); |
| 5089 | } else { |
| 5090 | newHandles.push_back(handle); |
| 5091 | } |
| 5092 | } |
| 5093 | |
| 5094 | // Insert or replace |
| 5095 | mWindowHandlesByDisplay[displayId] = newHandles; |
| 5096 | } |
| 5097 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5098 | void InputDispatcher::setInputWindows( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5099 | const std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>>& handlesPerDisplay) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5100 | // TODO(b/198444055): Remove setInputWindows from InputDispatcher. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5101 | { // acquire lock |
| 5102 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 5103 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 5104 | setInputWindowsLocked(handles, displayId); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5105 | } |
| 5106 | } |
| 5107 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5108 | mLooper->wake(); |
| 5109 | } |
| 5110 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5111 | /** |
| 5112 | * Called from InputManagerService, update window handle list by displayId that can receive input. |
| 5113 | * A window handle contains information about InputChannel, Touch Region, Types, Focused,... |
| 5114 | * If set an empty list, remove all handles from the specific display. |
| 5115 | * For focused handle, check if need to change and send a cancel event to previous one. |
| 5116 | * For removed handle, check if need to send a cancel event if already in touch. |
| 5117 | */ |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5118 | void InputDispatcher::setInputWindowsLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5119 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5120 | if (DEBUG_FOCUS) { |
| 5121 | std::string windowList; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5122 | for (const sp<WindowInfoHandle>& iwh : windowInfoHandles) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5123 | windowList += iwh->getName() + " "; |
| 5124 | } |
| 5125 | ALOGD("setInputWindows displayId=%" PRId32 " %s", displayId, windowList.c_str()); |
| 5126 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5127 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 5128 | // Check preconditions for new input windows |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5129 | for (const sp<WindowInfoHandle>& window : windowInfoHandles) { |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 5130 | const WindowInfo& info = *window->getInfo(); |
| 5131 | |
| 5132 | // 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] | 5133 | const bool noInputWindow = info.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5134 | if (noInputWindow && window->getToken() != nullptr) { |
| 5135 | ALOGE("%s has feature NO_INPUT_WINDOW, but a non-null token. Clearing", |
| 5136 | window->getName().c_str()); |
| 5137 | window->releaseChannel(); |
| 5138 | } |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 5139 | |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 5140 | // Ensure all spy windows are trusted overlays |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5141 | LOG_ALWAYS_FATAL_IF(info.isSpy() && |
| 5142 | !info.inputConfig.test( |
| 5143 | WindowInfo::InputConfig::TRUSTED_OVERLAY), |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 5144 | "%s has feature SPY, but is not a trusted overlay.", |
| 5145 | window->getName().c_str()); |
| 5146 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 5147 | // Ensure all stylus interceptors are trusted overlays |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5148 | LOG_ALWAYS_FATAL_IF(info.interceptsStylus() && |
| 5149 | !info.inputConfig.test( |
| 5150 | WindowInfo::InputConfig::TRUSTED_OVERLAY), |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 5151 | "%s has feature INTERCEPTS_STYLUS, but is not a trusted overlay.", |
| 5152 | window->getName().c_str()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5153 | } |
| 5154 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5155 | // Copy old handles for release if they are no longer present. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5156 | const std::vector<sp<WindowInfoHandle>> oldWindowHandles = getWindowHandlesLocked(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5157 | |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 5158 | // Save the old windows' orientation by ID before it gets updated. |
| 5159 | std::unordered_map<int32_t, uint32_t> oldWindowOrientations; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5160 | for (const sp<WindowInfoHandle>& handle : oldWindowHandles) { |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 5161 | oldWindowOrientations.emplace(handle->getId(), |
| 5162 | handle->getInfo()->transform.getOrientation()); |
| 5163 | } |
| 5164 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5165 | updateWindowHandlesForDisplayLocked(windowInfoHandles, displayId); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5166 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5167 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5168 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5169 | std::optional<FocusResolver::FocusChanges> changes = |
| 5170 | mFocusResolver.setInputWindows(displayId, windowHandles); |
| 5171 | if (changes) { |
| 5172 | onFocusChangedLocked(*changes); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5173 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5174 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5175 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 5176 | mTouchStatesByDisplay.find(displayId); |
| 5177 | if (stateIt != mTouchStatesByDisplay.end()) { |
| 5178 | TouchState& state = stateIt->second; |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5179 | for (size_t i = 0; i < state.windows.size();) { |
| 5180 | TouchedWindow& touchedWindow = state.windows[i]; |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 5181 | if (getWindowHandleLocked(touchedWindow.windowHandle) == nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5182 | if (DEBUG_FOCUS) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5183 | ALOGD("Touched window was removed: %s in display %" PRId32, |
| 5184 | touchedWindow.windowHandle->getName().c_str(), displayId); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5185 | } |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5186 | std::shared_ptr<InputChannel> touchedInputChannel = |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5187 | getInputChannelLocked(touchedWindow.windowHandle->getToken()); |
| 5188 | if (touchedInputChannel != nullptr) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 5189 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5190 | "touched window was removed"); |
| 5191 | synthesizeCancelationEventsForInputChannelLocked(touchedInputChannel, options); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5192 | // Since we are about to drop the touch, cancel the events for the wallpaper as |
| 5193 | // well. |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5194 | if (touchedWindow.targetFlags.test(InputTarget::Flags::FOREGROUND) && |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5195 | touchedWindow.windowHandle->getInfo()->inputConfig.test( |
| 5196 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER)) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5197 | sp<WindowInfoHandle> wallpaper = state.getWallpaperWindow(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5198 | synthesizeCancelationEventsForWindowLocked(wallpaper, options); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5199 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5200 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5201 | state.windows.erase(state.windows.begin() + i); |
| 5202 | } else { |
| 5203 | ++i; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5204 | } |
| 5205 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5206 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5207 | // 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] | 5208 | // could just clear the state here. |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 5209 | if (mDragState && mDragState->dragWindow->getInfo()->displayId == displayId && |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5210 | std::find(windowHandles.begin(), windowHandles.end(), mDragState->dragWindow) == |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5211 | windowHandles.end()) { |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 5212 | ALOGI("Drag window went away: %s", mDragState->dragWindow->getName().c_str()); |
| 5213 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5214 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5215 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5216 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 5217 | |
Prabir Pradhan | 8b89c2f | 2021-07-29 16:30:14 +0000 | [diff] [blame] | 5218 | // Determine if the orientation of any of the input windows have changed, and cancel all |
| 5219 | // pointer events if necessary. |
| 5220 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
| 5221 | const sp<WindowInfoHandle> newWindowHandle = getWindowHandleLocked(oldWindowHandle); |
| 5222 | if (newWindowHandle != nullptr && |
| 5223 | newWindowHandle->getInfo()->transform.getOrientation() != |
| 5224 | oldWindowOrientations[oldWindowHandle->getId()]) { |
| 5225 | std::shared_ptr<InputChannel> inputChannel = |
| 5226 | getInputChannelLocked(newWindowHandle->getToken()); |
| 5227 | if (inputChannel != nullptr) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 5228 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
Prabir Pradhan | 8b89c2f | 2021-07-29 16:30:14 +0000 | [diff] [blame] | 5229 | "touched window's orientation changed"); |
| 5230 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 5231 | } |
| 5232 | } |
| 5233 | } |
| 5234 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5235 | // Release information for windows that are no longer present. |
| 5236 | // This ensures that unused input channels are released promptly. |
| 5237 | // Otherwise, they might stick around until the window handle is destroyed |
| 5238 | // which might not happen until the next GC. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5239 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 5240 | if (getWindowHandleLocked(oldWindowHandle) == nullptr) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5241 | if (DEBUG_FOCUS) { |
| 5242 | ALOGD("Window went away: %s", oldWindowHandle->getName().c_str()); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 5243 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5244 | oldWindowHandle->releaseChannel(); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 5245 | } |
chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 5246 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5247 | } |
| 5248 | |
| 5249 | void InputDispatcher::setFocusedApplication( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5250 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5251 | if (DEBUG_FOCUS) { |
| 5252 | ALOGD("setFocusedApplication displayId=%" PRId32 " %s", displayId, |
| 5253 | inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>"); |
| 5254 | } |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 5255 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5256 | std::scoped_lock _l(mLock); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 5257 | setFocusedApplicationLocked(displayId, inputApplicationHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5258 | } // release lock |
| 5259 | |
| 5260 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5261 | mLooper->wake(); |
| 5262 | } |
| 5263 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 5264 | void InputDispatcher::setFocusedApplicationLocked( |
| 5265 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
| 5266 | std::shared_ptr<InputApplicationHandle> oldFocusedApplicationHandle = |
| 5267 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 5268 | |
| 5269 | if (sharedPointersEqual(oldFocusedApplicationHandle, inputApplicationHandle)) { |
| 5270 | return; // This application is already focused. No need to wake up or change anything. |
| 5271 | } |
| 5272 | |
| 5273 | // Set the new application handle. |
| 5274 | if (inputApplicationHandle != nullptr) { |
| 5275 | mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle; |
| 5276 | } else { |
| 5277 | mFocusedApplicationHandlesByDisplay.erase(displayId); |
| 5278 | } |
| 5279 | |
| 5280 | // No matter what the old focused application was, stop waiting on it because it is |
| 5281 | // no longer focused. |
| 5282 | resetNoFocusedWindowTimeoutLocked(); |
| 5283 | } |
| 5284 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5285 | /** |
| 5286 | * Sets the focused display, which is responsible for receiving focus-dispatched input events where |
| 5287 | * the display not specified. |
| 5288 | * |
| 5289 | * We track any unreleased events for each window. If a window loses the ability to receive the |
| 5290 | * released event, we will send a cancel event to it. So when the focused display is changed, we |
| 5291 | * cancel all the unreleased display-unspecified events for the focused window on the old focused |
| 5292 | * display. The display-specified events won't be affected. |
| 5293 | */ |
| 5294 | void InputDispatcher::setFocusedDisplay(int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5295 | if (DEBUG_FOCUS) { |
| 5296 | ALOGD("setFocusedDisplay displayId=%" PRId32, displayId); |
| 5297 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5298 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5299 | std::scoped_lock _l(mLock); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5300 | |
| 5301 | if (mFocusedDisplayId != displayId) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5302 | sp<IBinder> oldFocusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5303 | mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5304 | if (oldFocusedWindowToken != nullptr) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5305 | std::shared_ptr<InputChannel> inputChannel = |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5306 | getInputChannelLocked(oldFocusedWindowToken); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5307 | if (inputChannel != nullptr) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5308 | CancelationOptions |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 5309 | options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5310 | "The display which contains this window no longer has focus."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5311 | options.displayId = ADISPLAY_ID_NONE; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5312 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
| 5313 | } |
| 5314 | } |
| 5315 | mFocusedDisplayId = displayId; |
| 5316 | |
Chris Ye | 3c2d6f5 | 2020-08-09 10:39:48 -0700 | [diff] [blame] | 5317 | // Find new focused window and validate |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5318 | sp<IBinder> newFocusedWindowToken = mFocusResolver.getFocusedWindowToken(displayId); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5319 | sendFocusChangedCommandLocked(oldFocusedWindowToken, newFocusedWindowToken); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5320 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5321 | if (newFocusedWindowToken == nullptr) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5322 | ALOGW("Focused display #%" PRId32 " does not have a focused window.", displayId); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5323 | if (mFocusResolver.hasFocusedWindowTokens()) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5324 | ALOGE("But another display has a focused window\n%s", |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5325 | mFocusResolver.dumpFocusedWindows().c_str()); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5326 | } |
| 5327 | } |
| 5328 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5329 | } // release lock |
| 5330 | |
| 5331 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5332 | mLooper->wake(); |
| 5333 | } |
| 5334 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5335 | void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5336 | if (DEBUG_FOCUS) { |
| 5337 | ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); |
| 5338 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5339 | |
| 5340 | bool changed; |
| 5341 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5342 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5343 | |
| 5344 | if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { |
| 5345 | if (mDispatchFrozen && !frozen) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5346 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5347 | } |
| 5348 | |
| 5349 | if (mDispatchEnabled && !enabled) { |
| 5350 | resetAndDropEverythingLocked("dispatcher is being disabled"); |
| 5351 | } |
| 5352 | |
| 5353 | mDispatchEnabled = enabled; |
| 5354 | mDispatchFrozen = frozen; |
| 5355 | changed = true; |
| 5356 | } else { |
| 5357 | changed = false; |
| 5358 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5359 | } // release lock |
| 5360 | |
| 5361 | if (changed) { |
| 5362 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5363 | mLooper->wake(); |
| 5364 | } |
| 5365 | } |
| 5366 | |
| 5367 | void InputDispatcher::setInputFilterEnabled(bool enabled) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5368 | if (DEBUG_FOCUS) { |
| 5369 | ALOGD("setInputFilterEnabled: enabled=%d", enabled); |
| 5370 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5371 | |
| 5372 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5373 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5374 | |
| 5375 | if (mInputFilterEnabled == enabled) { |
| 5376 | return; |
| 5377 | } |
| 5378 | |
| 5379 | mInputFilterEnabled = enabled; |
| 5380 | resetAndDropEverythingLocked("input filter is being enabled or disabled"); |
| 5381 | } // release lock |
| 5382 | |
| 5383 | // Wake up poll loop since there might be work to do to drop everything. |
| 5384 | mLooper->wake(); |
| 5385 | } |
| 5386 | |
Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5387 | bool InputDispatcher::setInTouchMode(bool inTouchMode, gui::Pid pid, gui::Uid uid, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 5388 | bool hasPermission, int32_t displayId) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5389 | bool needWake = false; |
| 5390 | { |
| 5391 | std::scoped_lock lock(mLock); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5392 | ALOGD_IF(DEBUG_TOUCH_MODE, |
Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5393 | "Request to change touch mode to %s (calling pid=%s, uid=%s, " |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5394 | "hasPermission=%s, target displayId=%d, mTouchModePerDisplay[displayId]=%s)", |
Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5395 | toString(inTouchMode), pid.toString().c_str(), uid.toString().c_str(), |
| 5396 | toString(hasPermission), displayId, |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5397 | mTouchModePerDisplay.count(displayId) == 0 |
| 5398 | ? "not set" |
| 5399 | : std::to_string(mTouchModePerDisplay[displayId]).c_str()); |
| 5400 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5401 | auto touchModeIt = mTouchModePerDisplay.find(displayId); |
| 5402 | if (touchModeIt != mTouchModePerDisplay.end() && touchModeIt->second == inTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5403 | return false; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5404 | } |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5405 | if (!hasPermission) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 5406 | if (!focusedWindowIsOwnedByLocked(pid, uid) && |
| 5407 | !recentWindowsAreOwnedByLocked(pid, uid)) { |
Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5408 | ALOGD("Touch mode switch rejected, caller (pid=%s, uid=%s) doesn't own the focused " |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 5409 | "window nor none of the previously interacted window", |
Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5410 | pid.toString().c_str(), uid.toString().c_str()); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5411 | return false; |
| 5412 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5413 | } |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5414 | mTouchModePerDisplay[displayId] = inTouchMode; |
| 5415 | auto entry = std::make_unique<TouchModeEntry>(mIdGenerator.nextId(), now(), inTouchMode, |
| 5416 | displayId); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5417 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 5418 | } // release lock |
| 5419 | |
| 5420 | if (needWake) { |
| 5421 | mLooper->wake(); |
| 5422 | } |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5423 | return true; |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 5424 | } |
| 5425 | |
Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5426 | bool InputDispatcher::focusedWindowIsOwnedByLocked(gui::Pid pid, gui::Uid uid) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 5427 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
| 5428 | if (focusedToken == nullptr) { |
| 5429 | return false; |
| 5430 | } |
| 5431 | sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(focusedToken); |
| 5432 | return isWindowOwnedBy(windowHandle, pid, uid); |
| 5433 | } |
| 5434 | |
Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5435 | bool InputDispatcher::recentWindowsAreOwnedByLocked(gui::Pid pid, gui::Uid uid) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 5436 | return std::find_if(mInteractionConnectionTokens.begin(), mInteractionConnectionTokens.end(), |
| 5437 | [&](const sp<IBinder>& connectionToken) REQUIRES(mLock) { |
| 5438 | const sp<WindowInfoHandle> windowHandle = |
| 5439 | getWindowHandleLocked(connectionToken); |
| 5440 | return isWindowOwnedBy(windowHandle, pid, uid); |
| 5441 | }) != mInteractionConnectionTokens.end(); |
| 5442 | } |
| 5443 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 5444 | void InputDispatcher::setMaximumObscuringOpacityForTouch(float opacity) { |
| 5445 | if (opacity < 0 || opacity > 1) { |
| 5446 | LOG_ALWAYS_FATAL("Maximum obscuring opacity for touch should be >= 0 and <= 1"); |
| 5447 | return; |
| 5448 | } |
| 5449 | |
| 5450 | std::scoped_lock lock(mLock); |
| 5451 | mMaximumObscuringOpacityForTouch = opacity; |
| 5452 | } |
| 5453 | |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5454 | std::tuple<TouchState*, TouchedWindow*, int32_t /*displayId*/> |
| 5455 | InputDispatcher::findTouchStateWindowAndDisplayLocked(const sp<IBinder>& token) { |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5456 | for (auto& [displayId, state] : mTouchStatesByDisplay) { |
| 5457 | for (TouchedWindow& w : state.windows) { |
| 5458 | if (w.windowHandle->getToken() == token) { |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5459 | return std::make_tuple(&state, &w, displayId); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5460 | } |
| 5461 | } |
| 5462 | } |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5463 | return std::make_tuple(nullptr, nullptr, ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5464 | } |
| 5465 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5466 | bool InputDispatcher::transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken, |
| 5467 | bool isDragDrop) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 5468 | if (fromToken == toToken) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5469 | if (DEBUG_FOCUS) { |
| 5470 | ALOGD("Trivial transfer to same window."); |
| 5471 | } |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 5472 | return true; |
| 5473 | } |
| 5474 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5475 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5476 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5477 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5478 | // Find the target touch state and touched window by fromToken. |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5479 | auto [state, touchedWindow, displayId] = findTouchStateWindowAndDisplayLocked(fromToken); |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5480 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5481 | if (state == nullptr || touchedWindow == nullptr) { |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5482 | ALOGD("Touch transfer failed because from window is not being touched."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5483 | return false; |
| 5484 | } |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5485 | std::set<int32_t> deviceIds = touchedWindow->getTouchingDeviceIds(); |
| 5486 | if (deviceIds.size() != 1) { |
| 5487 | LOG(DEBUG) << "Can't transfer touch. Currently touching devices: " << dumpSet(deviceIds) |
| 5488 | << " for window: " << touchedWindow->dump(); |
| 5489 | return false; |
| 5490 | } |
| 5491 | const int32_t deviceId = *deviceIds.begin(); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5492 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5493 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(toToken, displayId); |
| 5494 | if (toWindowHandle == nullptr) { |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5495 | ALOGW("Cannot transfer touch because to window not found."); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5496 | return false; |
| 5497 | } |
| 5498 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5499 | if (DEBUG_FOCUS) { |
| 5500 | ALOGD("transferTouchFocus: fromWindowHandle=%s, toWindowHandle=%s", |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5501 | touchedWindow->windowHandle->getName().c_str(), |
| 5502 | toWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5503 | } |
| 5504 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5505 | // Erase old window. |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5506 | ftl::Flags<InputTarget::Flags> oldTargetFlags = touchedWindow->targetFlags; |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5507 | std::bitset<MAX_POINTER_ID + 1> pointerIds = touchedWindow->getTouchingPointers(deviceId); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5508 | sp<WindowInfoHandle> fromWindowHandle = touchedWindow->windowHandle; |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5509 | state->removeWindowByToken(fromToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5510 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5511 | // Add new window. |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 5512 | nsecs_t downTimeInTarget = now(); |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5513 | ftl::Flags<InputTarget::Flags> newTargetFlags = |
| 5514 | oldTargetFlags & (InputTarget::Flags::SPLIT | InputTarget::Flags::DISPATCH_AS_IS); |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 5515 | if (canReceiveForegroundTouches(*toWindowHandle->getInfo())) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5516 | newTargetFlags |= InputTarget::Flags::FOREGROUND; |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 5517 | } |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5518 | state->addOrUpdateWindow(toWindowHandle, newTargetFlags, deviceId, pointerIds, |
| 5519 | downTimeInTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5520 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5521 | // Store the dragging window. |
| 5522 | if (isDragDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 5523 | if (pointerIds.count() != 1) { |
| 5524 | ALOGW("The drag and drop cannot be started when there is no pointer or more than 1" |
| 5525 | " pointer on the window."); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 5526 | return false; |
| 5527 | } |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 5528 | // Track the pointer id for drag window and generate the drag state. |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 5529 | const size_t id = firstMarkedBit(pointerIds); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 5530 | mDragState = std::make_unique<DragState>(toWindowHandle, id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5531 | } |
| 5532 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5533 | // Synthesize cancel for old window and down for new window. |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 5534 | std::shared_ptr<Connection> fromConnection = getConnectionLocked(fromToken); |
| 5535 | std::shared_ptr<Connection> toConnection = getConnectionLocked(toToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5536 | if (fromConnection != nullptr && toConnection != nullptr) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5537 | fromConnection->inputState.mergePointerStateTo(toConnection->inputState); |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5538 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
| 5539 | "transferring touch from this window to another window"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5540 | synthesizeCancelationEventsForConnectionLocked(fromConnection, options); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5541 | synthesizePointerDownEventsForConnectionLocked(downTimeInTarget, toConnection, |
| 5542 | newTargetFlags); |
| 5543 | |
| 5544 | // Check if the wallpaper window should deliver the corresponding event. |
| 5545 | transferWallpaperTouch(oldTargetFlags, newTargetFlags, fromWindowHandle, toWindowHandle, |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5546 | *state, deviceId, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5547 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5548 | } // release lock |
| 5549 | |
| 5550 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5551 | mLooper->wake(); |
| 5552 | return true; |
| 5553 | } |
| 5554 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5555 | /** |
| 5556 | * Get the touched foreground window on the given display. |
| 5557 | * Return null if there are no windows touched on that display, or if more than one foreground |
| 5558 | * window is being touched. |
| 5559 | */ |
| 5560 | sp<WindowInfoHandle> InputDispatcher::findTouchedForegroundWindowLocked(int32_t displayId) const { |
| 5561 | auto stateIt = mTouchStatesByDisplay.find(displayId); |
| 5562 | if (stateIt == mTouchStatesByDisplay.end()) { |
| 5563 | ALOGI("No touch state on display %" PRId32, displayId); |
| 5564 | return nullptr; |
| 5565 | } |
| 5566 | |
| 5567 | const TouchState& state = stateIt->second; |
| 5568 | sp<WindowInfoHandle> touchedForegroundWindow; |
| 5569 | // If multiple foreground windows are touched, return nullptr |
| 5570 | for (const TouchedWindow& window : state.windows) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5571 | if (window.targetFlags.test(InputTarget::Flags::FOREGROUND)) { |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5572 | if (touchedForegroundWindow != nullptr) { |
| 5573 | ALOGI("Two or more foreground windows: %s and %s", |
| 5574 | touchedForegroundWindow->getName().c_str(), |
| 5575 | window.windowHandle->getName().c_str()); |
| 5576 | return nullptr; |
| 5577 | } |
| 5578 | touchedForegroundWindow = window.windowHandle; |
| 5579 | } |
| 5580 | } |
| 5581 | return touchedForegroundWindow; |
| 5582 | } |
| 5583 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5584 | // Binder call |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5585 | bool InputDispatcher::transferTouch(const sp<IBinder>& destChannelToken, int32_t displayId) { |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5586 | sp<IBinder> fromToken; |
| 5587 | { // acquire lock |
| 5588 | std::scoped_lock _l(mLock); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5589 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(destChannelToken, displayId); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5590 | if (toWindowHandle == nullptr) { |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5591 | ALOGW("Could not find window associated with token=%p on display %" PRId32, |
| 5592 | destChannelToken.get(), displayId); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5593 | return false; |
| 5594 | } |
| 5595 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5596 | sp<WindowInfoHandle> from = findTouchedForegroundWindowLocked(displayId); |
| 5597 | if (from == nullptr) { |
| 5598 | ALOGE("Could not find a source window in %s for %p", __func__, destChannelToken.get()); |
| 5599 | return false; |
| 5600 | } |
| 5601 | |
| 5602 | fromToken = from->getToken(); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5603 | } // release lock |
| 5604 | |
| 5605 | return transferTouchFocus(fromToken, destChannelToken); |
| 5606 | } |
| 5607 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5608 | void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5609 | if (DEBUG_FOCUS) { |
| 5610 | ALOGD("Resetting and dropping all events (%s).", reason); |
| 5611 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5612 | |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 5613 | CancelationOptions options(CancelationOptions::Mode::CANCEL_ALL_EVENTS, reason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5614 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 5615 | |
| 5616 | resetKeyRepeatLocked(); |
| 5617 | releasePendingEventLocked(); |
| 5618 | drainInboundQueueLocked(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5619 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5620 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5621 | mAnrTracker.clear(); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5622 | mTouchStatesByDisplay.clear(); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5623 | mReplacedKeys.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5624 | } |
| 5625 | |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 5626 | void InputDispatcher::logDispatchStateLocked() const { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5627 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5628 | dumpDispatchStateLocked(dump); |
| 5629 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5630 | std::istringstream stream(dump); |
| 5631 | std::string line; |
| 5632 | |
| 5633 | while (std::getline(stream, line, '\n')) { |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 5634 | ALOGI("%s", line.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5635 | } |
| 5636 | } |
| 5637 | |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 5638 | std::string InputDispatcher::dumpPointerCaptureStateLocked() const { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5639 | std::string dump; |
| 5640 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5641 | dump += StringPrintf(INDENT "Pointer Capture Requested: %s\n", |
| 5642 | toString(mCurrentPointerCaptureRequest.enable)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5643 | |
| 5644 | std::string windowName = "None"; |
| 5645 | if (mWindowTokenWithPointerCapture) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5646 | const sp<WindowInfoHandle> captureWindowHandle = |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5647 | getWindowHandleLocked(mWindowTokenWithPointerCapture); |
| 5648 | windowName = captureWindowHandle ? captureWindowHandle->getName().c_str() |
| 5649 | : "token has capture without window"; |
| 5650 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5651 | 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] | 5652 | |
| 5653 | return dump; |
| 5654 | } |
| 5655 | |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 5656 | void InputDispatcher::dumpDispatchStateLocked(std::string& dump) const { |
Siarhei Vishniakou | 043a3ec | 2019-05-01 11:30:46 -0700 | [diff] [blame] | 5657 | dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled)); |
| 5658 | dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen)); |
| 5659 | dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5660 | dump += StringPrintf(INDENT "FocusedDisplayId: %" PRId32 "\n", mFocusedDisplayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5661 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5662 | if (!mFocusedApplicationHandlesByDisplay.empty()) { |
| 5663 | dump += StringPrintf(INDENT "FocusedApplications:\n"); |
| 5664 | for (auto& it : mFocusedApplicationHandlesByDisplay) { |
| 5665 | const int32_t displayId = it.first; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5666 | const std::shared_ptr<InputApplicationHandle>& applicationHandle = it.second; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5667 | const std::chrono::duration timeout = |
| 5668 | applicationHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5669 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5670 | ", name='%s', dispatchingTimeout=%" PRId64 "ms\n", |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5671 | displayId, applicationHandle->getName().c_str(), millis(timeout)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5672 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5673 | } else { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5674 | dump += StringPrintf(INDENT "FocusedApplications: <none>\n"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5675 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5676 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5677 | dump += mFocusResolver.dump(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5678 | dump += dumpPointerCaptureStateLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5679 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5680 | if (!mTouchStatesByDisplay.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5681 | dump += StringPrintf(INDENT "TouchStatesByDisplay:\n"); |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5682 | for (const auto& [displayId, state] : mTouchStatesByDisplay) { |
Siarhei Vishniakou | 6e1e987 | 2022-11-08 17:51:35 -0800 | [diff] [blame] | 5683 | std::string touchStateDump = addLinePrefix(state.dump(), INDENT2); |
| 5684 | dump += INDENT2 + std::to_string(displayId) + " : " + touchStateDump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5685 | } |
| 5686 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5687 | dump += INDENT "TouchStates: <no displays touched>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5688 | } |
| 5689 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5690 | if (mDragState) { |
| 5691 | dump += StringPrintf(INDENT "DragState:\n"); |
| 5692 | mDragState->dump(dump, INDENT2); |
| 5693 | } |
| 5694 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5695 | if (!mWindowHandlesByDisplay.empty()) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5696 | for (const auto& [displayId, windowHandles] : mWindowHandlesByDisplay) { |
| 5697 | dump += StringPrintf(INDENT "Display: %" PRId32 "\n", displayId); |
| 5698 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
| 5699 | const auto& displayInfo = it->second; |
| 5700 | dump += StringPrintf(INDENT2 "logicalSize=%dx%d\n", displayInfo.logicalWidth, |
| 5701 | displayInfo.logicalHeight); |
| 5702 | displayInfo.transform.dump(dump, "transform", INDENT4); |
| 5703 | } else { |
| 5704 | dump += INDENT2 "No DisplayInfo found!\n"; |
| 5705 | } |
| 5706 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5707 | if (!windowHandles.empty()) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5708 | dump += INDENT2 "Windows:\n"; |
| 5709 | for (size_t i = 0; i < windowHandles.size(); i++) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5710 | const sp<WindowInfoHandle>& windowHandle = windowHandles[i]; |
| 5711 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5712 | |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5713 | dump += StringPrintf(INDENT3 "%zu: name='%s', id=%" PRId32 ", displayId=%d, " |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5714 | "inputConfig=%s, alpha=%.2f, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5715 | "frame=[%d,%d][%d,%d], globalScale=%f, " |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5716 | "applicationInfo.name=%s, " |
| 5717 | "applicationInfo.token=%s, " |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 5718 | "touchableRegion=", |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5719 | i, windowInfo->name.c_str(), windowInfo->id, |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5720 | windowInfo->displayId, |
| 5721 | windowInfo->inputConfig.string().c_str(), |
| 5722 | windowInfo->alpha, windowInfo->frameLeft, |
| 5723 | windowInfo->frameTop, windowInfo->frameRight, |
| 5724 | windowInfo->frameBottom, windowInfo->globalScaleFactor, |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5725 | windowInfo->applicationInfo.name.c_str(), |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 5726 | binderToString(windowInfo->applicationInfo.token).c_str()); |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 5727 | dump += dumpRegion(windowInfo->touchableRegion); |
Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5728 | dump += StringPrintf(", ownerPid=%s, ownerUid=%s, dispatchingTimeout=%" PRId64 |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5729 | "ms, hasToken=%s, " |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5730 | "touchOcclusionMode=%s\n", |
Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5731 | windowInfo->ownerPid.toString().c_str(), |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 5732 | windowInfo->ownerUid.toString().c_str(), |
Bernardo Rufino | c2f1fad | 2020-11-04 17:30:57 +0000 | [diff] [blame] | 5733 | millis(windowInfo->dispatchingTimeout), |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 5734 | binderToString(windowInfo->token).c_str(), |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5735 | toString(windowInfo->touchOcclusionMode).c_str()); |
chaviw | 85b4420 | 2020-07-24 11:46:21 -0700 | [diff] [blame] | 5736 | windowInfo->transform.dump(dump, "transform", INDENT4); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5737 | } |
| 5738 | } else { |
| 5739 | dump += INDENT2 "Windows: <none>\n"; |
| 5740 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5741 | } |
| 5742 | } else { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5743 | dump += INDENT "Displays: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5744 | } |
| 5745 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5746 | if (!mGlobalMonitorsByDisplay.empty()) { |
| 5747 | for (const auto& [displayId, monitors] : mGlobalMonitorsByDisplay) { |
| 5748 | dump += StringPrintf(INDENT "Global monitors on display %d:\n", displayId); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5749 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5750 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5751 | } else { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5752 | dump += INDENT "Global Monitors: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5753 | } |
| 5754 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5755 | const nsecs_t currentTime = now(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5756 | |
| 5757 | // Dump recently dispatched or dropped events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5758 | if (!mRecentQueue.empty()) { |
| 5759 | dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size()); |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 5760 | for (const std::shared_ptr<EventEntry>& entry : mRecentQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5761 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5762 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5763 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5764 | } |
| 5765 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5766 | dump += INDENT "RecentQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5767 | } |
| 5768 | |
| 5769 | // Dump event currently being dispatched. |
| 5770 | if (mPendingEvent) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5771 | dump += INDENT "PendingEvent:\n"; |
| 5772 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5773 | dump += mPendingEvent->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5774 | dump += StringPrintf(", age=%" PRId64 "ms\n", |
| 5775 | ns2ms(currentTime - mPendingEvent->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5776 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5777 | dump += INDENT "PendingEvent: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5778 | } |
| 5779 | |
| 5780 | // Dump inbound events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5781 | if (!mInboundQueue.empty()) { |
| 5782 | dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size()); |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 5783 | for (const std::shared_ptr<EventEntry>& entry : mInboundQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5784 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5785 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5786 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5787 | } |
| 5788 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5789 | dump += INDENT "InboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5790 | } |
| 5791 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5792 | if (!mReplacedKeys.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5793 | dump += INDENT "ReplacedKeys:\n"; |
Michael Wright | 3cec446 | 2022-11-24 22:05:46 +0000 | [diff] [blame] | 5794 | for (const auto& [replacement, newKeyCode] : mReplacedKeys) { |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5795 | dump += StringPrintf(INDENT2 "originalKeyCode=%d, deviceId=%d -> newKeyCode=%d\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5796 | replacement.keyCode, replacement.deviceId, newKeyCode); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5797 | } |
| 5798 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5799 | dump += INDENT "ReplacedKeys: <empty>\n"; |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5800 | } |
| 5801 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5802 | if (!mCommandQueue.empty()) { |
| 5803 | dump += StringPrintf(INDENT "CommandQueue: size=%zu\n", mCommandQueue.size()); |
| 5804 | } else { |
| 5805 | dump += INDENT "CommandQueue: <empty>\n"; |
| 5806 | } |
| 5807 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5808 | if (!mConnectionsByToken.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5809 | dump += INDENT "Connections:\n"; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5810 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5811 | dump += StringPrintf(INDENT2 "%i: channelName='%s', windowName='%s', " |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5812 | "status=%s, monitor=%s, responsive=%s\n", |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5813 | connection->inputChannel->getFd().get(), |
| 5814 | connection->getInputChannelName().c_str(), |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5815 | connection->getWindowName().c_str(), |
| 5816 | ftl::enum_string(connection->status).c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5817 | toString(connection->monitor), toString(connection->responsive)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5818 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5819 | if (!connection->outboundQueue.empty()) { |
| 5820 | dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n", |
| 5821 | connection->outboundQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5822 | dump += dumpQueue(connection->outboundQueue, currentTime); |
| 5823 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5824 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5825 | dump += INDENT3 "OutboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5826 | } |
| 5827 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5828 | if (!connection->waitQueue.empty()) { |
| 5829 | dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n", |
| 5830 | connection->waitQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5831 | dump += dumpQueue(connection->waitQueue, currentTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5832 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5833 | dump += INDENT3 "WaitQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5834 | } |
| 5835 | } |
| 5836 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5837 | dump += INDENT "Connections: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5838 | } |
| 5839 | |
| 5840 | if (isAppSwitchPendingLocked()) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5841 | dump += StringPrintf(INDENT "AppSwitch: pending, due in %" PRId64 "ms\n", |
| 5842 | ns2ms(mAppSwitchDueTime - now())); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5843 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5844 | dump += INDENT "AppSwitch: not pending\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5845 | } |
| 5846 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5847 | if (!mTouchModePerDisplay.empty()) { |
| 5848 | dump += INDENT "TouchModePerDisplay:\n"; |
| 5849 | for (const auto& [displayId, touchMode] : mTouchModePerDisplay) { |
| 5850 | dump += StringPrintf(INDENT2 "Display: %" PRId32 " TouchMode: %s\n", displayId, |
| 5851 | std::to_string(touchMode).c_str()); |
| 5852 | } |
| 5853 | } else { |
| 5854 | dump += INDENT "TouchModePerDisplay: <none>\n"; |
| 5855 | } |
| 5856 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5857 | dump += INDENT "Configuration:\n"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5858 | dump += StringPrintf(INDENT2 "KeyRepeatDelay: %" PRId64 "ms\n", ns2ms(mConfig.keyRepeatDelay)); |
| 5859 | dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n", |
| 5860 | ns2ms(mConfig.keyRepeatTimeout)); |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 5861 | dump += mLatencyTracker.dump(INDENT2); |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 5862 | dump += mLatencyAggregator.dump(INDENT2); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5863 | } |
| 5864 | |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 5865 | void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) const { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5866 | const size_t numMonitors = monitors.size(); |
| 5867 | for (size_t i = 0; i < numMonitors; i++) { |
| 5868 | const Monitor& monitor = monitors[i]; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5869 | const std::shared_ptr<InputChannel>& channel = monitor.inputChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5870 | dump += StringPrintf(INDENT2 "%zu: '%s', ", i, channel->getName().c_str()); |
| 5871 | dump += "\n"; |
| 5872 | } |
| 5873 | } |
| 5874 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5875 | class LooperEventCallback : public LooperCallback { |
| 5876 | public: |
| 5877 | LooperEventCallback(std::function<int(int events)> callback) : mCallback(callback) {} |
| 5878 | int handleEvent(int /*fd*/, int events, void* /*data*/) override { return mCallback(events); } |
| 5879 | |
| 5880 | private: |
| 5881 | std::function<int(int events)> mCallback; |
| 5882 | }; |
| 5883 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5884 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputChannel(const std::string& name) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5885 | if (DEBUG_CHANNEL_CREATION) { |
| 5886 | ALOGD("channel '%s' ~ createInputChannel", name.c_str()); |
| 5887 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5888 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5889 | std::unique_ptr<InputChannel> serverChannel; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5890 | std::unique_ptr<InputChannel> clientChannel; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5891 | status_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5892 | |
| 5893 | if (result) { |
| 5894 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5895 | } |
| 5896 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5897 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5898 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5899 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Tomasz Wasilczyk | 3202460 | 2023-11-16 10:17:54 -0800 | [diff] [blame] | 5900 | auto&& fd = serverChannel->getFd(); |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 5901 | std::shared_ptr<Connection> connection = |
| 5902 | std::make_shared<Connection>(std::move(serverChannel), /*monitor=*/false, |
| 5903 | mIdGenerator); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5904 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5905 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5906 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5907 | } |
| 5908 | mConnectionsByToken.emplace(token, connection); |
| 5909 | |
| 5910 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5911 | this, std::placeholders::_1, token); |
| 5912 | |
Tomasz Wasilczyk | 3202460 | 2023-11-16 10:17:54 -0800 | [diff] [blame] | 5913 | mLooper->addFd(fd.get(), 0, ALOOPER_EVENT_INPUT, sp<LooperEventCallback>::make(callback), |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5914 | nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5915 | } // release lock |
| 5916 | |
| 5917 | // Wake the looper because some connections have changed. |
| 5918 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5919 | return clientChannel; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5920 | } |
| 5921 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5922 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputMonitor(int32_t displayId, |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5923 | const std::string& name, |
Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5924 | gui::Pid pid) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5925 | std::shared_ptr<InputChannel> serverChannel; |
| 5926 | std::unique_ptr<InputChannel> clientChannel; |
| 5927 | status_t result = openInputChannelPair(name, serverChannel, clientChannel); |
| 5928 | if (result) { |
| 5929 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5930 | } |
| 5931 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5932 | { // acquire lock |
| 5933 | std::scoped_lock _l(mLock); |
| 5934 | |
| 5935 | if (displayId < 0) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5936 | return base::Error(BAD_VALUE) << "Attempted to create input monitor with name " << name |
| 5937 | << " without a specified display."; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5938 | } |
| 5939 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 5940 | std::shared_ptr<Connection> connection = |
| 5941 | std::make_shared<Connection>(serverChannel, /*monitor=*/true, mIdGenerator); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5942 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Tomasz Wasilczyk | 3202460 | 2023-11-16 10:17:54 -0800 | [diff] [blame] | 5943 | auto&& fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5944 | |
| 5945 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5946 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5947 | } |
| 5948 | mConnectionsByToken.emplace(token, connection); |
| 5949 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5950 | this, std::placeholders::_1, token); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5951 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5952 | mGlobalMonitorsByDisplay[displayId].emplace_back(serverChannel, pid); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5953 | |
Tomasz Wasilczyk | 3202460 | 2023-11-16 10:17:54 -0800 | [diff] [blame] | 5954 | mLooper->addFd(fd.get(), 0, ALOOPER_EVENT_INPUT, sp<LooperEventCallback>::make(callback), |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5955 | nullptr); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5956 | } |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5957 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5958 | // Wake the looper because some connections have changed. |
| 5959 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5960 | return clientChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5961 | } |
| 5962 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5963 | status_t InputDispatcher::removeInputChannel(const sp<IBinder>& connectionToken) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5964 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5965 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5966 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5967 | status_t status = removeInputChannelLocked(connectionToken, /*notify=*/false); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5968 | if (status) { |
| 5969 | return status; |
| 5970 | } |
| 5971 | } // release lock |
| 5972 | |
| 5973 | // Wake the poll loop because removing the connection may have changed the current |
| 5974 | // synchronization state. |
| 5975 | mLooper->wake(); |
| 5976 | return OK; |
| 5977 | } |
| 5978 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5979 | status_t InputDispatcher::removeInputChannelLocked(const sp<IBinder>& connectionToken, |
| 5980 | bool notify) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 5981 | std::shared_ptr<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5982 | if (connection == nullptr) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5983 | // 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] | 5984 | return BAD_VALUE; |
| 5985 | } |
| 5986 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5987 | removeConnectionLocked(connection); |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 5988 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5989 | if (connection->monitor) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5990 | removeMonitorChannelLocked(connectionToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5991 | } |
| 5992 | |
Tomasz Wasilczyk | 3202460 | 2023-11-16 10:17:54 -0800 | [diff] [blame] | 5993 | mLooper->removeFd(connection->inputChannel->getFd().get()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5994 | |
| 5995 | nsecs_t currentTime = now(); |
| 5996 | abortBrokenDispatchCycleLocked(currentTime, connection, notify); |
| 5997 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5998 | connection->status = Connection::Status::ZOMBIE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5999 | return OK; |
| 6000 | } |
| 6001 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 6002 | void InputDispatcher::removeMonitorChannelLocked(const sp<IBinder>& connectionToken) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6003 | for (auto it = mGlobalMonitorsByDisplay.begin(); it != mGlobalMonitorsByDisplay.end();) { |
| 6004 | auto& [displayId, monitors] = *it; |
| 6005 | std::erase_if(monitors, [connectionToken](const Monitor& monitor) { |
| 6006 | return monitor.inputChannel->getConnectionToken() == connectionToken; |
| 6007 | }); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6008 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6009 | if (monitors.empty()) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6010 | it = mGlobalMonitorsByDisplay.erase(it); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6011 | } else { |
| 6012 | ++it; |
| 6013 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6014 | } |
| 6015 | } |
| 6016 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6017 | status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6018 | std::scoped_lock _l(mLock); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 6019 | return pilferPointersLocked(token); |
| 6020 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6021 | |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 6022 | status_t InputDispatcher::pilferPointersLocked(const sp<IBinder>& token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6023 | const std::shared_ptr<InputChannel> requestingChannel = getInputChannelLocked(token); |
| 6024 | if (!requestingChannel) { |
| 6025 | ALOGW("Attempted to pilfer pointers from an un-registered channel or invalid token"); |
| 6026 | return BAD_VALUE; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6027 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6028 | |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 6029 | auto [statePtr, windowPtr, displayId] = findTouchStateWindowAndDisplayLocked(token); |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 6030 | if (statePtr == nullptr || windowPtr == nullptr) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6031 | ALOGW("Attempted to pilfer points from a channel without any on-going pointer streams." |
| 6032 | " Ignoring."); |
| 6033 | return BAD_VALUE; |
| 6034 | } |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 6035 | std::set<int32_t> deviceIds = windowPtr->getTouchingDeviceIds(); |
| 6036 | if (deviceIds.size() != 1) { |
| 6037 | LOG(WARNING) << "Can't pilfer. Currently touching devices: " << dumpSet(deviceIds) |
| 6038 | << " in window: " << windowPtr->dump(); |
| 6039 | return BAD_VALUE; |
| 6040 | } |
| 6041 | const int32_t deviceId = *deviceIds.begin(); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6042 | |
| 6043 | TouchState& state = *statePtr; |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 6044 | TouchedWindow& window = *windowPtr; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6045 | // Send cancel events to all the input channels we're stealing from. |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 6046 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6047 | "input channel stole pointer stream"); |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 6048 | options.deviceId = deviceId; |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 6049 | options.displayId = displayId; |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 6050 | std::bitset<MAX_POINTER_ID + 1> pointerIds = window.getTouchingPointers(deviceId); |
| 6051 | options.pointerIds = pointerIds; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6052 | std::string canceledWindows; |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 6053 | for (const TouchedWindow& w : state.windows) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6054 | const std::shared_ptr<InputChannel> channel = |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 6055 | getInputChannelLocked(w.windowHandle->getToken()); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6056 | if (channel != nullptr && channel->getConnectionToken() != token) { |
| 6057 | synthesizeCancelationEventsForInputChannelLocked(channel, options); |
| 6058 | canceledWindows += canceledWindows.empty() ? "[" : ", "; |
| 6059 | canceledWindows += channel->getName(); |
| 6060 | } |
| 6061 | } |
| 6062 | canceledWindows += canceledWindows.empty() ? "[]" : "]"; |
| 6063 | ALOGI("Channel %s is stealing touch from %s", requestingChannel->getName().c_str(), |
| 6064 | canceledWindows.c_str()); |
| 6065 | |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 6066 | // Prevent the gesture from being sent to any other windows. |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 6067 | // This only blocks relevant pointers to be sent to other windows |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 6068 | window.addPilferingPointers(deviceId, pointerIds); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 6069 | |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 6070 | state.cancelPointersForWindowsExcept(deviceId, pointerIds, token); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6071 | return OK; |
| 6072 | } |
| 6073 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6074 | void InputDispatcher::requestPointerCapture(const sp<IBinder>& windowToken, bool enabled) { |
| 6075 | { // acquire lock |
| 6076 | std::scoped_lock _l(mLock); |
| 6077 | if (DEBUG_FOCUS) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 6078 | const sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(windowToken); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6079 | ALOGI("Request to %s Pointer Capture from: %s.", enabled ? "enable" : "disable", |
| 6080 | windowHandle != nullptr ? windowHandle->getName().c_str() |
| 6081 | : "token without window"); |
| 6082 | } |
| 6083 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6084 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6085 | if (focusedToken != windowToken) { |
| 6086 | ALOGW("Ignoring request to %s Pointer Capture: window does not have focus.", |
| 6087 | enabled ? "enable" : "disable"); |
| 6088 | return; |
| 6089 | } |
| 6090 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6091 | if (enabled == mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6092 | ALOGW("Ignoring request to %s Pointer Capture: " |
| 6093 | "window has %s requested pointer capture.", |
| 6094 | enabled ? "enable" : "disable", enabled ? "already" : "not"); |
| 6095 | return; |
| 6096 | } |
| 6097 | |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 6098 | if (enabled) { |
| 6099 | if (std::find(mIneligibleDisplaysForPointerCapture.begin(), |
| 6100 | mIneligibleDisplaysForPointerCapture.end(), |
| 6101 | mFocusedDisplayId) != mIneligibleDisplaysForPointerCapture.end()) { |
| 6102 | ALOGW("Ignoring request to enable Pointer Capture: display is not eligible"); |
| 6103 | return; |
| 6104 | } |
| 6105 | } |
| 6106 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6107 | setPointerCaptureLocked(enabled); |
| 6108 | } // release lock |
| 6109 | |
| 6110 | // Wake the thread to process command entries. |
| 6111 | mLooper->wake(); |
| 6112 | } |
| 6113 | |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 6114 | void InputDispatcher::setDisplayEligibilityForPointerCapture(int32_t displayId, bool isEligible) { |
| 6115 | { // acquire lock |
| 6116 | std::scoped_lock _l(mLock); |
| 6117 | std::erase(mIneligibleDisplaysForPointerCapture, displayId); |
| 6118 | if (!isEligible) { |
| 6119 | mIneligibleDisplaysForPointerCapture.push_back(displayId); |
| 6120 | } |
| 6121 | } // release lock |
| 6122 | } |
| 6123 | |
Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6124 | std::optional<gui::Pid> InputDispatcher::findMonitorPidByTokenLocked(const sp<IBinder>& token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6125 | for (const auto& [_, monitors] : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6126 | for (const Monitor& monitor : monitors) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 6127 | if (monitor.inputChannel->getConnectionToken() == token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6128 | return monitor.pid; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6129 | } |
| 6130 | } |
| 6131 | } |
| 6132 | return std::nullopt; |
| 6133 | } |
| 6134 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6135 | std::shared_ptr<Connection> InputDispatcher::getConnectionLocked( |
| 6136 | const sp<IBinder>& inputConnectionToken) const { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 6137 | if (inputConnectionToken == nullptr) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 6138 | return nullptr; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 6139 | } |
| 6140 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 6141 | for (const auto& [token, connection] : mConnectionsByToken) { |
| 6142 | if (token == inputConnectionToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 6143 | return connection; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6144 | } |
| 6145 | } |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 6146 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 6147 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6148 | } |
| 6149 | |
Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 6150 | std::string InputDispatcher::getConnectionNameLocked(const sp<IBinder>& connectionToken) const { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6151 | std::shared_ptr<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 6152 | if (connection == nullptr) { |
| 6153 | return "<nullptr>"; |
| 6154 | } |
| 6155 | return connection->getInputChannelName(); |
| 6156 | } |
| 6157 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6158 | void InputDispatcher::removeConnectionLocked(const std::shared_ptr<Connection>& connection) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6159 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 6160 | mConnectionsByToken.erase(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6161 | } |
| 6162 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6163 | void InputDispatcher::doDispatchCycleFinishedCommand(nsecs_t finishTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6164 | const std::shared_ptr<Connection>& connection, |
| 6165 | uint32_t seq, bool handled, |
| 6166 | nsecs_t consumeTime) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6167 | // Handle post-event policy actions. |
| 6168 | std::deque<DispatchEntry*>::iterator dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 6169 | if (dispatchEntryIt == connection->waitQueue.end()) { |
| 6170 | return; |
| 6171 | } |
| 6172 | DispatchEntry* dispatchEntry = *dispatchEntryIt; |
| 6173 | const nsecs_t eventDuration = finishTime - dispatchEntry->deliveryTime; |
| 6174 | if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) { |
| 6175 | ALOGI("%s spent %" PRId64 "ms processing %s", connection->getWindowName().c_str(), |
| 6176 | ns2ms(eventDuration), dispatchEntry->eventEntry->getDescription().c_str()); |
| 6177 | } |
| 6178 | if (shouldReportFinishedEvent(*dispatchEntry, *connection)) { |
| 6179 | mLatencyTracker.trackFinishedEvent(dispatchEntry->eventEntry->id, |
| 6180 | connection->inputChannel->getConnectionToken(), |
| 6181 | dispatchEntry->deliveryTime, consumeTime, finishTime); |
| 6182 | } |
| 6183 | |
| 6184 | bool restartEvent; |
| 6185 | if (dispatchEntry->eventEntry->type == EventEntry::Type::KEY) { |
| 6186 | KeyEntry& keyEntry = static_cast<KeyEntry&>(*(dispatchEntry->eventEntry)); |
| 6187 | restartEvent = |
| 6188 | afterKeyEventLockedInterruptable(connection, dispatchEntry, keyEntry, handled); |
| 6189 | } else if (dispatchEntry->eventEntry->type == EventEntry::Type::MOTION) { |
| 6190 | MotionEntry& motionEntry = static_cast<MotionEntry&>(*(dispatchEntry->eventEntry)); |
| 6191 | restartEvent = afterMotionEventLockedInterruptable(connection, dispatchEntry, motionEntry, |
| 6192 | handled); |
| 6193 | } else { |
| 6194 | restartEvent = false; |
| 6195 | } |
| 6196 | |
| 6197 | // Dequeue the event and start the next cycle. |
| 6198 | // Because the lock might have been released, it is possible that the |
| 6199 | // contents of the wait queue to have been drained, so we need to double-check |
| 6200 | // a few things. |
| 6201 | dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 6202 | if (dispatchEntryIt != connection->waitQueue.end()) { |
| 6203 | dispatchEntry = *dispatchEntryIt; |
| 6204 | connection->waitQueue.erase(dispatchEntryIt); |
| 6205 | const sp<IBinder>& connectionToken = connection->inputChannel->getConnectionToken(); |
| 6206 | mAnrTracker.erase(dispatchEntry->timeoutTime, connectionToken); |
| 6207 | if (!connection->responsive) { |
| 6208 | connection->responsive = isConnectionResponsive(*connection); |
| 6209 | if (connection->responsive) { |
| 6210 | // The connection was unresponsive, and now it's responsive. |
| 6211 | processConnectionResponsiveLocked(*connection); |
| 6212 | } |
| 6213 | } |
| 6214 | traceWaitQueueLength(*connection); |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 6215 | if (restartEvent && connection->status == Connection::Status::NORMAL) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6216 | connection->outboundQueue.push_front(dispatchEntry); |
| 6217 | traceOutboundQueueLength(*connection); |
| 6218 | } else { |
| 6219 | releaseDispatchEntry(dispatchEntry); |
| 6220 | } |
| 6221 | } |
| 6222 | |
| 6223 | // Start the next dispatch cycle for this connection. |
| 6224 | startDispatchCycleLocked(now(), connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6225 | } |
| 6226 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6227 | void InputDispatcher::sendFocusChangedCommandLocked(const sp<IBinder>& oldToken, |
| 6228 | const sp<IBinder>& newToken) { |
| 6229 | auto command = [this, oldToken, newToken]() REQUIRES(mLock) { |
| 6230 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6231 | mPolicy.notifyFocusChanged(oldToken, newToken); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6232 | }; |
| 6233 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6234 | } |
| 6235 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6236 | void InputDispatcher::sendDropWindowCommandLocked(const sp<IBinder>& token, float x, float y) { |
| 6237 | auto command = [this, token, x, y]() REQUIRES(mLock) { |
| 6238 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6239 | mPolicy.notifyDropWindow(token, x, y); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6240 | }; |
| 6241 | postCommandLocked(std::move(command)); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 6242 | } |
| 6243 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6244 | void InputDispatcher::onAnrLocked(const std::shared_ptr<Connection>& connection) { |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6245 | if (connection == nullptr) { |
| 6246 | LOG_ALWAYS_FATAL("Caller must check for nullness"); |
| 6247 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6248 | // Since we are allowing the policy to extend the timeout, maybe the waitQueue |
| 6249 | // is already healthy again. Don't raise ANR in this situation |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6250 | if (connection->waitQueue.empty()) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6251 | ALOGI("Not raising ANR because the connection %s has recovered", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6252 | connection->inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6253 | return; |
| 6254 | } |
| 6255 | /** |
| 6256 | * The "oldestEntry" is the entry that was first sent to the application. That entry, however, |
| 6257 | * may not be the one that caused the timeout to occur. One possibility is that window timeout |
| 6258 | * has changed. This could cause newer entries to time out before the already dispatched |
| 6259 | * entries. In that situation, the newest entries caused ANR. But in all likelihood, the app |
| 6260 | * processes the events linearly. So providing information about the oldest entry seems to be |
| 6261 | * most useful. |
| 6262 | */ |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6263 | DispatchEntry* oldestEntry = *connection->waitQueue.begin(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6264 | const nsecs_t currentWait = now() - oldestEntry->deliveryTime; |
| 6265 | std::string reason = |
| 6266 | android::base::StringPrintf("%s is not responding. Waited %" PRId64 "ms for %s", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6267 | connection->inputChannel->getName().c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6268 | ns2ms(currentWait), |
| 6269 | oldestEntry->eventEntry->getDescription().c_str()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6270 | sp<IBinder> connectionToken = connection->inputChannel->getConnectionToken(); |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 6271 | updateLastAnrStateLocked(getWindowHandleLocked(connectionToken), reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6272 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6273 | processConnectionUnresponsiveLocked(*connection, std::move(reason)); |
| 6274 | |
| 6275 | // Stop waking up for events on this connection, it is already unresponsive |
| 6276 | cancelEventsForAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6277 | } |
| 6278 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6279 | void InputDispatcher::onAnrLocked(std::shared_ptr<InputApplicationHandle> application) { |
| 6280 | std::string reason = |
| 6281 | StringPrintf("%s does not have a focused window", application->getName().c_str()); |
| 6282 | updateLastAnrStateLocked(*application, reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6283 | |
Yabin Cui | 8eb9c55 | 2023-06-08 18:05:07 +0000 | [diff] [blame] | 6284 | auto command = [this, app = std::move(application)]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6285 | scoped_unlock unlock(mLock); |
Yabin Cui | ced952f | 2023-06-09 21:12:51 +0000 | [diff] [blame] | 6286 | mPolicy.notifyNoFocusedWindowAnr(app); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6287 | }; |
| 6288 | postCommandLocked(std::move(command)); |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 6289 | } |
| 6290 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 6291 | void InputDispatcher::updateLastAnrStateLocked(const sp<WindowInfoHandle>& window, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6292 | const std::string& reason) { |
| 6293 | const std::string windowLabel = getApplicationWindowLabel(nullptr, window); |
| 6294 | updateLastAnrStateLocked(windowLabel, reason); |
| 6295 | } |
| 6296 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6297 | void InputDispatcher::updateLastAnrStateLocked(const InputApplicationHandle& application, |
| 6298 | const std::string& reason) { |
| 6299 | const std::string windowLabel = getApplicationWindowLabel(&application, nullptr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6300 | updateLastAnrStateLocked(windowLabel, reason); |
| 6301 | } |
| 6302 | |
| 6303 | void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel, |
| 6304 | const std::string& reason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6305 | // 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] | 6306 | time_t t = time(nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6307 | struct tm tm; |
| 6308 | localtime_r(&t, &tm); |
| 6309 | char timestr[64]; |
| 6310 | strftime(timestr, sizeof(timestr), "%F %T", &tm); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6311 | mLastAnrState.clear(); |
| 6312 | mLastAnrState += INDENT "ANR:\n"; |
| 6313 | mLastAnrState += StringPrintf(INDENT2 "Time: %s\n", timestr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6314 | mLastAnrState += StringPrintf(INDENT2 "Reason: %s\n", reason.c_str()); |
| 6315 | mLastAnrState += StringPrintf(INDENT2 "Window: %s\n", windowLabel.c_str()); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6316 | dumpDispatchStateLocked(mLastAnrState); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6317 | } |
| 6318 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6319 | void InputDispatcher::doInterceptKeyBeforeDispatchingCommand(const sp<IBinder>& focusedWindowToken, |
| 6320 | KeyEntry& entry) { |
| 6321 | const KeyEvent event = createKeyEvent(entry); |
| 6322 | nsecs_t delay = 0; |
| 6323 | { // release lock |
| 6324 | scoped_unlock unlock(mLock); |
| 6325 | android::base::Timer t; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6326 | delay = mPolicy.interceptKeyBeforeDispatching(focusedWindowToken, event, entry.policyFlags); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6327 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 6328 | ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms", |
| 6329 | std::to_string(t.duration().count()).c_str()); |
| 6330 | } |
| 6331 | } // acquire lock |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6332 | |
| 6333 | if (delay < 0) { |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 6334 | entry.interceptKeyResult = KeyEntry::InterceptKeyResult::SKIP; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6335 | } else if (delay == 0) { |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 6336 | entry.interceptKeyResult = KeyEntry::InterceptKeyResult::CONTINUE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6337 | } else { |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 6338 | entry.interceptKeyResult = KeyEntry::InterceptKeyResult::TRY_AGAIN_LATER; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6339 | entry.interceptKeyWakeupTime = now() + delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6340 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6341 | } |
| 6342 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6343 | void InputDispatcher::sendWindowUnresponsiveCommandLocked(const sp<IBinder>& token, |
Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6344 | std::optional<gui::Pid> pid, |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6345 | std::string reason) { |
Yabin Cui | 8eb9c55 | 2023-06-08 18:05:07 +0000 | [diff] [blame] | 6346 | auto command = [this, token, pid, r = std::move(reason)]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6347 | scoped_unlock unlock(mLock); |
Yabin Cui | ced952f | 2023-06-09 21:12:51 +0000 | [diff] [blame] | 6348 | mPolicy.notifyWindowUnresponsive(token, pid, r); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6349 | }; |
| 6350 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6351 | } |
| 6352 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6353 | void InputDispatcher::sendWindowResponsiveCommandLocked(const sp<IBinder>& token, |
Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6354 | std::optional<gui::Pid> pid) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6355 | auto command = [this, token, pid]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6356 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6357 | mPolicy.notifyWindowResponsive(token, pid); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6358 | }; |
| 6359 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6360 | } |
| 6361 | |
| 6362 | /** |
| 6363 | * Tell the policy that a connection has become unresponsive so that it can start ANR. |
| 6364 | * Check whether the connection of interest is a monitor or a window, and add the corresponding |
| 6365 | * command entry to the command queue. |
| 6366 | */ |
| 6367 | void InputDispatcher::processConnectionUnresponsiveLocked(const Connection& connection, |
| 6368 | std::string reason) { |
| 6369 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6370 | std::optional<gui::Pid> pid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6371 | if (connection.monitor) { |
| 6372 | ALOGW("Monitor %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 6373 | reason.c_str()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6374 | pid = findMonitorPidByTokenLocked(connectionToken); |
| 6375 | } else { |
| 6376 | // The connection is a window |
| 6377 | ALOGW("Window %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 6378 | reason.c_str()); |
| 6379 | const sp<WindowInfoHandle> handle = getWindowHandleLocked(connectionToken); |
| 6380 | if (handle != nullptr) { |
| 6381 | pid = handle->getInfo()->ownerPid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6382 | } |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6383 | } |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6384 | sendWindowUnresponsiveCommandLocked(connectionToken, pid, std::move(reason)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6385 | } |
| 6386 | |
| 6387 | /** |
| 6388 | * Tell the policy that a connection has become responsive so that it can stop ANR. |
| 6389 | */ |
| 6390 | void InputDispatcher::processConnectionResponsiveLocked(const Connection& connection) { |
| 6391 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6392 | std::optional<gui::Pid> pid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6393 | if (connection.monitor) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6394 | pid = findMonitorPidByTokenLocked(connectionToken); |
| 6395 | } else { |
| 6396 | // The connection is a window |
| 6397 | const sp<WindowInfoHandle> handle = getWindowHandleLocked(connectionToken); |
| 6398 | if (handle != nullptr) { |
| 6399 | pid = handle->getInfo()->ownerPid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6400 | } |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6401 | } |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6402 | sendWindowResponsiveCommandLocked(connectionToken, pid); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6403 | } |
| 6404 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6405 | bool InputDispatcher::afterKeyEventLockedInterruptable( |
| 6406 | const std::shared_ptr<Connection>& connection, DispatchEntry* dispatchEntry, |
| 6407 | KeyEntry& keyEntry, bool handled) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6408 | if (keyEntry.flags & AKEY_EVENT_FLAG_FALLBACK) { |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6409 | if (!handled) { |
| 6410 | // Report the key as unhandled, since the fallback was not handled. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6411 | mReporter->reportUnhandledKey(keyEntry.id); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6412 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6413 | return false; |
| 6414 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6415 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6416 | // Get the fallback key state. |
| 6417 | // Clear it out after dispatching the UP. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6418 | int32_t originalKeyCode = keyEntry.keyCode; |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6419 | std::optional<int32_t> fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6420 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6421 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6422 | } |
| 6423 | |
| 6424 | if (handled || !dispatchEntry->hasForegroundTarget()) { |
| 6425 | // If the application handles the original key for which we previously |
| 6426 | // generated a fallback or if the window is not a foreground window, |
| 6427 | // then cancel the associated fallback key, if any. |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6428 | if (fallbackKeyCode) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6429 | // Dispatch the unhandled key to the policy with the cancel flag. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6430 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6431 | ALOGD("Unhandled key event: Asking policy to cancel fallback action. " |
| 6432 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6433 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, |
| 6434 | keyEntry.policyFlags); |
| 6435 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6436 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6437 | event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6438 | |
| 6439 | mLock.unlock(); |
| 6440 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6441 | if (const auto unhandledKeyFallback = |
| 6442 | mPolicy.dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), |
| 6443 | event, keyEntry.policyFlags); |
| 6444 | unhandledKeyFallback) { |
| 6445 | event = *unhandledKeyFallback; |
| 6446 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6447 | |
| 6448 | mLock.lock(); |
| 6449 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6450 | // Cancel the fallback key. |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6451 | if (*fallbackKeyCode != AKEYCODE_UNKNOWN) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 6452 | CancelationOptions options(CancelationOptions::Mode::CANCEL_FALLBACK_EVENTS, |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6453 | "application handled the original non-fallback key " |
| 6454 | "or is no longer a foreground target, " |
| 6455 | "canceling previously dispatched fallback key"); |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6456 | options.keyCode = *fallbackKeyCode; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6457 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6458 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6459 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6460 | } |
| 6461 | } else { |
| 6462 | // If the application did not handle a non-fallback key, first check |
| 6463 | // that we are in a good state to perform unhandled key event processing |
| 6464 | // Then ask the policy what to do with it. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6465 | bool initialDown = keyEntry.action == AKEY_EVENT_ACTION_DOWN && keyEntry.repeatCount == 0; |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6466 | if (!fallbackKeyCode && !initialDown) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6467 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6468 | ALOGD("Unhandled key event: Skipping unhandled key event processing " |
| 6469 | "since this is not an initial down. " |
| 6470 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6471 | originalKeyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 6472 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6473 | return false; |
| 6474 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6475 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6476 | // Dispatch the unhandled key to the policy. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6477 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6478 | ALOGD("Unhandled key event: Asking policy to perform fallback action. " |
| 6479 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6480 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 6481 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6482 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6483 | |
| 6484 | mLock.unlock(); |
| 6485 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6486 | bool fallback = false; |
| 6487 | if (auto fb = mPolicy.dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), |
| 6488 | event, keyEntry.policyFlags); |
| 6489 | fb) { |
| 6490 | fallback = true; |
| 6491 | event = *fb; |
| 6492 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6493 | |
| 6494 | mLock.lock(); |
| 6495 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 6496 | if (connection->status != Connection::Status::NORMAL) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6497 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6498 | return false; |
| 6499 | } |
| 6500 | |
| 6501 | // Latch the fallback keycode for this key on an initial down. |
| 6502 | // The fallback keycode cannot change at any other point in the lifecycle. |
| 6503 | if (initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6504 | if (fallback) { |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6505 | *fallbackKeyCode = event.getKeyCode(); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6506 | } else { |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6507 | *fallbackKeyCode = AKEYCODE_UNKNOWN; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6508 | } |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6509 | connection->inputState.setFallbackKey(originalKeyCode, *fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6510 | } |
| 6511 | |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6512 | ALOG_ASSERT(fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6513 | |
| 6514 | // Cancel the fallback key if the policy decides not to send it anymore. |
| 6515 | // We will continue to dispatch the key to the policy but we will no |
| 6516 | // longer dispatch a fallback key to the application. |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6517 | if (*fallbackKeyCode != AKEYCODE_UNKNOWN && |
| 6518 | (!fallback || *fallbackKeyCode != event.getKeyCode())) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6519 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6520 | if (fallback) { |
| 6521 | ALOGD("Unhandled key event: Policy requested to send key %d" |
| 6522 | "as a fallback for %d, but on the DOWN it had requested " |
| 6523 | "to send %d instead. Fallback canceled.", |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6524 | event.getKeyCode(), originalKeyCode, *fallbackKeyCode); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6525 | } else { |
| 6526 | ALOGD("Unhandled key event: Policy did not request fallback for %d, " |
| 6527 | "but on the DOWN it had requested to send %d. " |
| 6528 | "Fallback canceled.", |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6529 | originalKeyCode, *fallbackKeyCode); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6530 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6531 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6532 | |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 6533 | CancelationOptions options(CancelationOptions::Mode::CANCEL_FALLBACK_EVENTS, |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6534 | "canceling fallback, policy no longer desires it"); |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6535 | options.keyCode = *fallbackKeyCode; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6536 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
| 6537 | |
| 6538 | fallback = false; |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6539 | *fallbackKeyCode = AKEYCODE_UNKNOWN; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6540 | if (keyEntry.action != AKEY_EVENT_ACTION_UP) { |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6541 | connection->inputState.setFallbackKey(originalKeyCode, *fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6542 | } |
| 6543 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6544 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6545 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6546 | { |
| 6547 | std::string msg; |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6548 | const std::map<int32_t, int32_t>& fallbackKeys = |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6549 | connection->inputState.getFallbackKeys(); |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6550 | for (const auto& [key, value] : fallbackKeys) { |
| 6551 | msg += StringPrintf(", %d->%d", key, value); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6552 | } |
| 6553 | ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.", |
| 6554 | fallbackKeys.size(), msg.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6555 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6556 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6557 | |
| 6558 | if (fallback) { |
| 6559 | // Restart the dispatch cycle using the fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6560 | keyEntry.eventTime = event.getEventTime(); |
| 6561 | keyEntry.deviceId = event.getDeviceId(); |
| 6562 | keyEntry.source = event.getSource(); |
| 6563 | keyEntry.displayId = event.getDisplayId(); |
| 6564 | keyEntry.flags = event.getFlags() | AKEY_EVENT_FLAG_FALLBACK; |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6565 | keyEntry.keyCode = *fallbackKeyCode; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6566 | keyEntry.scanCode = event.getScanCode(); |
| 6567 | keyEntry.metaState = event.getMetaState(); |
| 6568 | keyEntry.repeatCount = event.getRepeatCount(); |
| 6569 | keyEntry.downTime = event.getDownTime(); |
| 6570 | keyEntry.syntheticRepeat = false; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6571 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6572 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6573 | ALOGD("Unhandled key event: Dispatching fallback key. " |
| 6574 | "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x", |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6575 | originalKeyCode, *fallbackKeyCode, keyEntry.metaState); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6576 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6577 | return true; // restart the event |
| 6578 | } else { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6579 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6580 | ALOGD("Unhandled key event: No fallback key."); |
| 6581 | } |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6582 | |
| 6583 | // Report the key as unhandled, since there is no fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6584 | mReporter->reportUnhandledKey(keyEntry.id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6585 | } |
| 6586 | } |
| 6587 | return false; |
| 6588 | } |
| 6589 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6590 | bool InputDispatcher::afterMotionEventLockedInterruptable( |
| 6591 | const std::shared_ptr<Connection>& connection, DispatchEntry* dispatchEntry, |
| 6592 | MotionEntry& motionEntry, bool handled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6593 | return false; |
| 6594 | } |
| 6595 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6596 | void InputDispatcher::traceInboundQueueLengthLocked() { |
| 6597 | if (ATRACE_ENABLED()) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 6598 | ATRACE_INT("iq", mInboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6599 | } |
| 6600 | } |
| 6601 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6602 | void InputDispatcher::traceOutboundQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6603 | if (ATRACE_ENABLED()) { |
| 6604 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6605 | snprintf(counterName, sizeof(counterName), "oq:%s", connection.getWindowName().c_str()); |
| 6606 | ATRACE_INT(counterName, connection.outboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6607 | } |
| 6608 | } |
| 6609 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6610 | void InputDispatcher::traceWaitQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6611 | if (ATRACE_ENABLED()) { |
| 6612 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6613 | snprintf(counterName, sizeof(counterName), "wq:%s", connection.getWindowName().c_str()); |
| 6614 | ATRACE_INT(counterName, connection.waitQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6615 | } |
| 6616 | } |
| 6617 | |
Siarhei Vishniakou | 5e20f27 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 6618 | void InputDispatcher::dump(std::string& dump) const { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6619 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6620 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6621 | dump += "Input Dispatcher State:\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6622 | dumpDispatchStateLocked(dump); |
| 6623 | |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6624 | if (!mLastAnrState.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6625 | dump += "\nInput Dispatcher State at time of last ANR:\n"; |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6626 | dump += mLastAnrState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6627 | } |
| 6628 | } |
| 6629 | |
| 6630 | void InputDispatcher::monitor() { |
| 6631 | // 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] | 6632 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6633 | mLooper->wake(); |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6634 | mDispatcherIsAlive.wait(_l); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6635 | } |
| 6636 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6637 | /** |
| 6638 | * Wake up the dispatcher and wait until it processes all events and commands. |
| 6639 | * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so |
| 6640 | * this method can be safely called from any thread, as long as you've ensured that |
| 6641 | * the work you are interested in completing has already been queued. |
| 6642 | */ |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 6643 | bool InputDispatcher::waitForIdle() const { |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6644 | /** |
| 6645 | * Timeout should represent the longest possible time that a device might spend processing |
| 6646 | * events and commands. |
| 6647 | */ |
| 6648 | constexpr std::chrono::duration TIMEOUT = 100ms; |
| 6649 | std::unique_lock lock(mLock); |
| 6650 | mLooper->wake(); |
| 6651 | std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT); |
| 6652 | return result == std::cv_status::no_timeout; |
| 6653 | } |
| 6654 | |
Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 6655 | /** |
| 6656 | * Sets focus to the window identified by the token. This must be called |
| 6657 | * after updating any input window handles. |
| 6658 | * |
| 6659 | * Params: |
| 6660 | * request.token - input channel token used to identify the window that should gain focus. |
| 6661 | * request.focusedToken - the token that the caller expects currently to be focused. If the |
| 6662 | * specified token does not match the currently focused window, this request will be dropped. |
| 6663 | * If the specified focused token matches the currently focused window, the call will succeed. |
| 6664 | * Set this to "null" if this call should succeed no matter what the currently focused token is. |
| 6665 | * request.timestamp - SYSTEM_TIME_MONOTONIC timestamp in nanos set by the client (wm) |
| 6666 | * when requesting the focus change. This determines which request gets |
| 6667 | * precedence if there is a focus change request from another source such as pointer down. |
| 6668 | */ |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6669 | void InputDispatcher::setFocusedWindow(const FocusRequest& request) { |
| 6670 | { // acquire lock |
| 6671 | std::scoped_lock _l(mLock); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6672 | std::optional<FocusResolver::FocusChanges> changes = |
| 6673 | mFocusResolver.setFocusedWindow(request, getWindowHandlesLocked(request.displayId)); |
| 6674 | if (changes) { |
| 6675 | onFocusChangedLocked(*changes); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6676 | } |
| 6677 | } // release lock |
| 6678 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6679 | mLooper->wake(); |
| 6680 | } |
| 6681 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6682 | void InputDispatcher::onFocusChangedLocked(const FocusResolver::FocusChanges& changes) { |
| 6683 | if (changes.oldFocus) { |
| 6684 | std::shared_ptr<InputChannel> focusedInputChannel = getInputChannelLocked(changes.oldFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6685 | if (focusedInputChannel) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 6686 | CancelationOptions options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6687 | "focus left window"); |
| 6688 | synthesizeCancelationEventsForInputChannelLocked(focusedInputChannel, options); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6689 | enqueueFocusEventLocked(changes.oldFocus, /*hasFocus=*/false, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6690 | } |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6691 | } |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6692 | if (changes.newFocus) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6693 | enqueueFocusEventLocked(changes.newFocus, /*hasFocus=*/true, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6694 | } |
| 6695 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6696 | // If a window has pointer capture, then it must have focus. We need to ensure that this |
| 6697 | // contract is upheld when pointer capture is being disabled due to a loss of window focus. |
| 6698 | // If the window loses focus before it loses pointer capture, then the window can be in a state |
| 6699 | // where it has pointer capture but not focus, violating the contract. Therefore we must |
| 6700 | // dispatch the pointer capture event before the focus event. Since focus events are added to |
| 6701 | // the front of the queue (above), we add the pointer capture event to the front of the queue |
| 6702 | // after the focus events are added. This ensures the pointer capture event ends up at the |
| 6703 | // front. |
| 6704 | disablePointerCaptureForcedLocked(); |
| 6705 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6706 | if (mFocusedDisplayId == changes.displayId) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6707 | sendFocusChangedCommandLocked(changes.oldFocus, changes.newFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6708 | } |
| 6709 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6710 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6711 | void InputDispatcher::disablePointerCaptureForcedLocked() { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6712 | if (!mCurrentPointerCaptureRequest.enable && !mWindowTokenWithPointerCapture) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6713 | return; |
| 6714 | } |
| 6715 | |
| 6716 | ALOGD_IF(DEBUG_FOCUS, "Disabling Pointer Capture because the window lost focus."); |
| 6717 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6718 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6719 | setPointerCaptureLocked(false); |
| 6720 | } |
| 6721 | |
| 6722 | if (!mWindowTokenWithPointerCapture) { |
| 6723 | // No need to send capture changes because no window has capture. |
| 6724 | return; |
| 6725 | } |
| 6726 | |
| 6727 | if (mPendingEvent != nullptr) { |
| 6728 | // Move the pending event to the front of the queue. This will give the chance |
| 6729 | // for the pending event to be dropped if it is a captured event. |
| 6730 | mInboundQueue.push_front(mPendingEvent); |
| 6731 | mPendingEvent = nullptr; |
| 6732 | } |
| 6733 | |
| 6734 | auto entry = std::make_unique<PointerCaptureChangedEntry>(mIdGenerator.nextId(), now(), |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6735 | mCurrentPointerCaptureRequest); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6736 | mInboundQueue.push_front(std::move(entry)); |
| 6737 | } |
| 6738 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6739 | void InputDispatcher::setPointerCaptureLocked(bool enable) { |
| 6740 | mCurrentPointerCaptureRequest.enable = enable; |
| 6741 | mCurrentPointerCaptureRequest.seq++; |
| 6742 | auto command = [this, request = mCurrentPointerCaptureRequest]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6743 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6744 | mPolicy.setPointerCapture(request); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6745 | }; |
| 6746 | postCommandLocked(std::move(command)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6747 | } |
| 6748 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6749 | void InputDispatcher::displayRemoved(int32_t displayId) { |
| 6750 | { // acquire lock |
| 6751 | std::scoped_lock _l(mLock); |
| 6752 | // Set an empty list to remove all handles from the specific display. |
| 6753 | setInputWindowsLocked(/* window handles */ {}, displayId); |
| 6754 | setFocusedApplicationLocked(displayId, nullptr); |
| 6755 | // Call focus resolver to clean up stale requests. This must be called after input windows |
| 6756 | // have been removed for the removed display. |
| 6757 | mFocusResolver.displayRemoved(displayId); |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 6758 | // Reset pointer capture eligibility, regardless of previous state. |
| 6759 | std::erase(mIneligibleDisplaysForPointerCapture, displayId); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 6760 | // Remove the associated touch mode state. |
| 6761 | mTouchModePerDisplay.erase(displayId); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 6762 | mVerifiersByDisplay.erase(displayId); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6763 | } // release lock |
| 6764 | |
| 6765 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6766 | mLooper->wake(); |
| 6767 | } |
| 6768 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 6769 | void InputDispatcher::onWindowInfosChanged(const gui::WindowInfosUpdate& update) { |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6770 | // The listener sends the windows as a flattened array. Separate the windows by display for |
| 6771 | // more convenient parsing. |
| 6772 | std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>> handlesPerDisplay; |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 6773 | for (const auto& info : update.windowInfos) { |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6774 | handlesPerDisplay.emplace(info.displayId, std::vector<sp<WindowInfoHandle>>()); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6775 | handlesPerDisplay[info.displayId].push_back(sp<WindowInfoHandle>::make(info)); |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6776 | } |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6777 | |
| 6778 | { // acquire lock |
| 6779 | std::scoped_lock _l(mLock); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 6780 | |
| 6781 | // Ensure that we have an entry created for all existing displays so that if a displayId has |
| 6782 | // no windows, we can tell that the windows were removed from the display. |
| 6783 | for (const auto& [displayId, _] : mWindowHandlesByDisplay) { |
| 6784 | handlesPerDisplay[displayId]; |
| 6785 | } |
| 6786 | |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6787 | mDisplayInfos.clear(); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 6788 | for (const auto& displayInfo : update.displayInfos) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6789 | mDisplayInfos.emplace(displayInfo.displayId, displayInfo); |
| 6790 | } |
| 6791 | |
| 6792 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 6793 | setInputWindowsLocked(handles, displayId); |
| 6794 | } |
Patrick Williams | 9464b2c | 2023-05-23 11:22:04 -0500 | [diff] [blame] | 6795 | |
| 6796 | if (update.vsyncId < mWindowInfosVsyncId) { |
| 6797 | ALOGE("Received out of order window infos update. Last update vsync id: %" PRId64 |
| 6798 | ", current update vsync id: %" PRId64, |
| 6799 | mWindowInfosVsyncId, update.vsyncId); |
| 6800 | } |
| 6801 | mWindowInfosVsyncId = update.vsyncId; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6802 | } |
| 6803 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6804 | mLooper->wake(); |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6805 | } |
| 6806 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6807 | bool InputDispatcher::shouldDropInput( |
| 6808 | const EventEntry& entry, const sp<android::gui::WindowInfoHandle>& windowHandle) const { |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6809 | if (windowHandle->getInfo()->inputConfig.test(WindowInfo::InputConfig::DROP_INPUT) || |
| 6810 | (windowHandle->getInfo()->inputConfig.test( |
| 6811 | WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED) && |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6812 | isWindowObscuredLocked(windowHandle))) { |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6813 | ALOGW("Dropping %s event targeting %s as requested by the input configuration {%s} on " |
| 6814 | "display %" PRId32 ".", |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6815 | ftl::enum_string(entry.type).c_str(), windowHandle->getName().c_str(), |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6816 | windowHandle->getInfo()->inputConfig.string().c_str(), |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6817 | windowHandle->getInfo()->displayId); |
| 6818 | return true; |
| 6819 | } |
| 6820 | return false; |
| 6821 | } |
| 6822 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 6823 | void InputDispatcher::DispatcherWindowListener::onWindowInfosChanged( |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 6824 | const gui::WindowInfosUpdate& update) { |
| 6825 | mDispatcher.onWindowInfosChanged(update); |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 6826 | } |
| 6827 | |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6828 | void InputDispatcher::cancelCurrentTouch() { |
| 6829 | { |
| 6830 | std::scoped_lock _l(mLock); |
| 6831 | ALOGD("Canceling all ongoing pointer gestures on all displays."); |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 6832 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6833 | "cancel current touch"); |
| 6834 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 6835 | |
| 6836 | mTouchStatesByDisplay.clear(); |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6837 | } |
| 6838 | // Wake up poll loop since there might be work to do. |
| 6839 | mLooper->wake(); |
| 6840 | } |
| 6841 | |
Prabir Pradhan | 87112a7 | 2023-04-20 19:13:39 +0000 | [diff] [blame] | 6842 | void InputDispatcher::requestRefreshConfiguration() { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6843 | InputDispatcherConfiguration config = mPolicy.getDispatcherConfiguration(); |
Prabir Pradhan | 87112a7 | 2023-04-20 19:13:39 +0000 | [diff] [blame] | 6844 | |
| 6845 | std::scoped_lock _l(mLock); |
| 6846 | mConfig = config; |
| 6847 | } |
| 6848 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6849 | void InputDispatcher::setMonitorDispatchingTimeoutForTest(std::chrono::nanoseconds timeout) { |
| 6850 | std::scoped_lock _l(mLock); |
| 6851 | mMonitorDispatchingTimeout = timeout; |
| 6852 | } |
| 6853 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6854 | void InputDispatcher::slipWallpaperTouch(ftl::Flags<InputTarget::Flags> targetFlags, |
| 6855 | const sp<WindowInfoHandle>& oldWindowHandle, |
| 6856 | const sp<WindowInfoHandle>& newWindowHandle, |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 6857 | TouchState& state, int32_t deviceId, int32_t pointerId, |
Siarhei Vishniakou | bf88052 | 2023-05-01 11:03:22 -0700 | [diff] [blame] | 6858 | std::vector<InputTarget>& targets) const { |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 6859 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
| 6860 | pointerIds.set(pointerId); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6861 | const bool oldHasWallpaper = oldWindowHandle->getInfo()->inputConfig.test( |
| 6862 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER); |
| 6863 | const bool newHasWallpaper = targetFlags.test(InputTarget::Flags::FOREGROUND) && |
| 6864 | newWindowHandle->getInfo()->inputConfig.test( |
| 6865 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER); |
| 6866 | const sp<WindowInfoHandle> oldWallpaper = |
| 6867 | oldHasWallpaper ? state.getWallpaperWindow() : nullptr; |
| 6868 | const sp<WindowInfoHandle> newWallpaper = |
| 6869 | newHasWallpaper ? findWallpaperWindowBelow(newWindowHandle) : nullptr; |
| 6870 | if (oldWallpaper == newWallpaper) { |
| 6871 | return; |
| 6872 | } |
| 6873 | |
| 6874 | if (oldWallpaper != nullptr) { |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 6875 | const TouchedWindow& oldTouchedWindow = state.getTouchedWindow(oldWallpaper); |
| 6876 | addWindowTargetLocked(oldWallpaper, |
| 6877 | oldTouchedWindow.targetFlags | |
| 6878 | InputTarget::Flags::DISPATCH_AS_SLIPPERY_EXIT, |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 6879 | pointerIds, oldTouchedWindow.getDownTimeInTarget(deviceId), targets); |
| 6880 | state.removeTouchingPointerFromWindow(deviceId, pointerId, oldWallpaper); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6881 | } |
| 6882 | |
| 6883 | if (newWallpaper != nullptr) { |
| 6884 | state.addOrUpdateWindow(newWallpaper, |
| 6885 | InputTarget::Flags::DISPATCH_AS_SLIPPERY_ENTER | |
| 6886 | InputTarget::Flags::WINDOW_IS_OBSCURED | |
| 6887 | InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED, |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 6888 | deviceId, pointerIds); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6889 | } |
| 6890 | } |
| 6891 | |
| 6892 | void InputDispatcher::transferWallpaperTouch(ftl::Flags<InputTarget::Flags> oldTargetFlags, |
| 6893 | ftl::Flags<InputTarget::Flags> newTargetFlags, |
| 6894 | const sp<WindowInfoHandle> fromWindowHandle, |
| 6895 | const sp<WindowInfoHandle> toWindowHandle, |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 6896 | TouchState& state, int32_t deviceId, |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 6897 | std::bitset<MAX_POINTER_ID + 1> pointerIds) { |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6898 | const bool oldHasWallpaper = oldTargetFlags.test(InputTarget::Flags::FOREGROUND) && |
| 6899 | fromWindowHandle->getInfo()->inputConfig.test( |
| 6900 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER); |
| 6901 | const bool newHasWallpaper = newTargetFlags.test(InputTarget::Flags::FOREGROUND) && |
| 6902 | toWindowHandle->getInfo()->inputConfig.test( |
| 6903 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER); |
| 6904 | |
| 6905 | const sp<WindowInfoHandle> oldWallpaper = |
| 6906 | oldHasWallpaper ? state.getWallpaperWindow() : nullptr; |
| 6907 | const sp<WindowInfoHandle> newWallpaper = |
| 6908 | newHasWallpaper ? findWallpaperWindowBelow(toWindowHandle) : nullptr; |
| 6909 | if (oldWallpaper == newWallpaper) { |
| 6910 | return; |
| 6911 | } |
| 6912 | |
| 6913 | if (oldWallpaper != nullptr) { |
| 6914 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
| 6915 | "transferring touch focus to another window"); |
| 6916 | state.removeWindowByToken(oldWallpaper->getToken()); |
| 6917 | synthesizeCancelationEventsForWindowLocked(oldWallpaper, options); |
| 6918 | } |
| 6919 | |
| 6920 | if (newWallpaper != nullptr) { |
| 6921 | nsecs_t downTimeInTarget = now(); |
| 6922 | ftl::Flags<InputTarget::Flags> wallpaperFlags = |
| 6923 | oldTargetFlags & (InputTarget::Flags::SPLIT | InputTarget::Flags::DISPATCH_AS_IS); |
| 6924 | wallpaperFlags |= InputTarget::Flags::WINDOW_IS_OBSCURED | |
| 6925 | InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED; |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 6926 | state.addOrUpdateWindow(newWallpaper, wallpaperFlags, deviceId, pointerIds, |
| 6927 | downTimeInTarget); |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6928 | std::shared_ptr<Connection> wallpaperConnection = |
| 6929 | getConnectionLocked(newWallpaper->getToken()); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6930 | if (wallpaperConnection != nullptr) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6931 | std::shared_ptr<Connection> toConnection = |
| 6932 | getConnectionLocked(toWindowHandle->getToken()); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6933 | toConnection->inputState.mergePointerStateTo(wallpaperConnection->inputState); |
| 6934 | synthesizePointerDownEventsForConnectionLocked(downTimeInTarget, wallpaperConnection, |
| 6935 | wallpaperFlags); |
| 6936 | } |
| 6937 | } |
| 6938 | } |
| 6939 | |
| 6940 | sp<WindowInfoHandle> InputDispatcher::findWallpaperWindowBelow( |
| 6941 | const sp<WindowInfoHandle>& windowHandle) const { |
| 6942 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
| 6943 | getWindowHandlesLocked(windowHandle->getInfo()->displayId); |
| 6944 | bool foundWindow = false; |
| 6945 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
| 6946 | if (!foundWindow && otherHandle != windowHandle) { |
| 6947 | continue; |
| 6948 | } |
| 6949 | if (windowHandle == otherHandle) { |
| 6950 | foundWindow = true; |
| 6951 | continue; |
| 6952 | } |
| 6953 | |
| 6954 | if (otherHandle->getInfo()->inputConfig.test(WindowInfo::InputConfig::IS_WALLPAPER)) { |
| 6955 | return otherHandle; |
| 6956 | } |
| 6957 | } |
| 6958 | return nullptr; |
| 6959 | } |
| 6960 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 6961 | } // namespace android::inputdispatcher |