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> |
Ameer Armaly | cff4fa5 | 2023-10-04 23:45:11 +0000 | [diff] [blame] | 28 | #include <com_android_input_flags.h> |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 29 | #include <ftl/enum.h> |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 30 | #include <log/log_event_list.h> |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 31 | #if defined(__ANDROID__) |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 32 | #include <gui/SurfaceComposerClient.h> |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 33 | #endif |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 34 | #include <input/InputDevice.h> |
Siarhei Vishniakou | 6e1e987 | 2022-11-08 17:51:35 -0800 | [diff] [blame] | 35 | #include <input/PrintTools.h> |
Prabir Pradhan | a37bad1 | 2023-08-18 15:55:32 +0000 | [diff] [blame] | 36 | #include <input/TraceTools.h> |
tyiu | 1573a67 | 2023-02-21 22:38:32 +0000 | [diff] [blame] | 37 | #include <openssl/mem.h> |
Prabir Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame] | 38 | #include <private/android_filesystem_config.h> |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 39 | #include <unistd.h> |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 40 | #include <utils/Trace.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 41 | |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 42 | #include <cerrno> |
| 43 | #include <cinttypes> |
| 44 | #include <climits> |
| 45 | #include <cstddef> |
| 46 | #include <ctime> |
| 47 | #include <queue> |
| 48 | #include <sstream> |
| 49 | |
Asmita Poddar | dd9a6cd | 2023-09-26 15:35:12 +0000 | [diff] [blame] | 50 | #include "../InputDeviceMetricsSource.h" |
| 51 | |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 52 | #include "Connection.h" |
Arthur Hung | 1a1007b | 2022-05-11 07:15:01 +0000 | [diff] [blame] | 53 | #include "DebugConfig.h" |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 54 | #include "InputDispatcher.h" |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 55 | #include "trace/InputTracer.h" |
| 56 | #include "trace/InputTracingPerfettoBackend.h" |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 57 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 58 | #define INDENT " " |
| 59 | #define INDENT2 " " |
| 60 | #define INDENT3 " " |
| 61 | #define INDENT4 " " |
| 62 | |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 63 | using namespace android::ftl::flag_operators; |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 64 | using android::base::Error; |
Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 65 | using android::base::HwTimeoutMultiplier; |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 66 | using android::base::Result; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 67 | using android::base::StringPrintf; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 68 | using android::gui::DisplayInfo; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 69 | using android::gui::FocusRequest; |
| 70 | using android::gui::TouchOcclusionMode; |
| 71 | using android::gui::WindowInfo; |
| 72 | using android::gui::WindowInfoHandle; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 73 | using android::os::InputEventInjectionResult; |
| 74 | using android::os::InputEventInjectionSync; |
Ameer Armaly | cff4fa5 | 2023-10-04 23:45:11 +0000 | [diff] [blame] | 75 | namespace input_flags = com::android::input::flags; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 76 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 77 | namespace android::inputdispatcher { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 78 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 79 | namespace { |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 80 | |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 81 | // Input tracing is only available on debuggable builds (userdebug and eng) when the feature |
| 82 | // flag is enabled. When the flag is changed, tracing will only be available after reboot. |
| 83 | bool isInputTracingEnabled() { |
| 84 | static const std::string buildType = base::GetProperty("ro.build.type", "user"); |
| 85 | static const bool isUserdebugOrEng = buildType == "userdebug" || buildType == "eng"; |
| 86 | return input_flags::enable_input_event_tracing() && isUserdebugOrEng; |
| 87 | } |
| 88 | |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 89 | template <class Entry> |
| 90 | void ensureEventTraced(const Entry& entry) { |
| 91 | if (!entry.traceTracker) { |
| 92 | LOG(FATAL) << "Expected event entry to be traced, but it wasn't: " << entry; |
| 93 | } |
| 94 | } |
| 95 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 96 | // Temporarily releases a held mutex for the lifetime of the instance. |
| 97 | // Named to match std::scoped_lock |
| 98 | class scoped_unlock { |
| 99 | public: |
| 100 | explicit scoped_unlock(std::mutex& mutex) : mMutex(mutex) { mMutex.unlock(); } |
| 101 | ~scoped_unlock() { mMutex.lock(); } |
| 102 | |
| 103 | private: |
| 104 | std::mutex& mMutex; |
| 105 | }; |
| 106 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 107 | // Default input dispatching timeout if there is no focused application or paused window |
| 108 | // from which to determine an appropriate dispatching timeout. |
Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 109 | const std::chrono::duration DEFAULT_INPUT_DISPATCHING_TIMEOUT = std::chrono::milliseconds( |
| 110 | android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS * |
| 111 | HwTimeoutMultiplier()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 112 | |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 113 | // The default minimum time gap between two user activity poke events. |
| 114 | const std::chrono::milliseconds DEFAULT_USER_ACTIVITY_POKE_INTERVAL = 100ms; |
| 115 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 116 | const std::chrono::duration STALE_EVENT_TIMEOUT = std::chrono::seconds(10) * HwTimeoutMultiplier(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 117 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 118 | // 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] | 119 | constexpr nsecs_t SLOW_EVENT_PROCESSING_WARNING_TIMEOUT = 2000 * 1000000LL; // 2sec |
| 120 | |
| 121 | // Log a warning when an interception call takes longer than this to process. |
| 122 | constexpr std::chrono::milliseconds SLOW_INTERCEPTION_THRESHOLD = 50ms; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 123 | |
| 124 | // Number of recent events to keep for debugging purposes. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 125 | constexpr size_t RECENT_QUEUE_MAX_SIZE = 10; |
| 126 | |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 127 | // Event log tags. See EventLogTags.logtags for reference. |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 128 | constexpr int LOGTAG_INPUT_INTERACTION = 62000; |
| 129 | constexpr int LOGTAG_INPUT_FOCUS = 62001; |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 130 | constexpr int LOGTAG_INPUT_CANCEL = 62003; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 131 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 132 | const ui::Transform kIdentityTransform; |
| 133 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 134 | inline nsecs_t now() { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 135 | return systemTime(SYSTEM_TIME_MONOTONIC); |
| 136 | } |
| 137 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 138 | inline const std::string binderToString(const sp<IBinder>& binder) { |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 139 | if (binder == nullptr) { |
| 140 | return "<null>"; |
| 141 | } |
| 142 | return StringPrintf("%p", binder.get()); |
| 143 | } |
| 144 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 145 | static std::string uidString(const gui::Uid& uid) { |
| 146 | return uid.toString(); |
| 147 | } |
| 148 | |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 149 | Result<void> checkKeyAction(int32_t action) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 150 | switch (action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 151 | case AKEY_EVENT_ACTION_DOWN: |
| 152 | case AKEY_EVENT_ACTION_UP: |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 153 | return {}; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 154 | default: |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 155 | return Error() << "Key event has invalid action code " << action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 159 | Result<void> validateKeyEvent(int32_t action) { |
| 160 | return checkKeyAction(action); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 161 | } |
| 162 | |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 163 | Result<void> checkMotionAction(int32_t action, int32_t actionButton, int32_t pointerCount) { |
Siarhei Vishniakou | 2f61bdc | 2022-12-02 08:55:51 -0800 | [diff] [blame] | 164 | switch (MotionEvent::getActionMasked(action)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 165 | case AMOTION_EVENT_ACTION_DOWN: |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 166 | case AMOTION_EVENT_ACTION_UP: { |
| 167 | if (pointerCount != 1) { |
| 168 | return Error() << "invalid pointer count " << pointerCount; |
| 169 | } |
| 170 | return {}; |
| 171 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 172 | case AMOTION_EVENT_ACTION_MOVE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 173 | case AMOTION_EVENT_ACTION_HOVER_ENTER: |
| 174 | case AMOTION_EVENT_ACTION_HOVER_MOVE: |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 175 | case AMOTION_EVENT_ACTION_HOVER_EXIT: { |
| 176 | if (pointerCount < 1) { |
| 177 | return Error() << "invalid pointer count " << pointerCount; |
| 178 | } |
| 179 | return {}; |
| 180 | } |
Siarhei Vishniakou | 2f61bdc | 2022-12-02 08:55:51 -0800 | [diff] [blame] | 181 | case AMOTION_EVENT_ACTION_CANCEL: |
| 182 | case AMOTION_EVENT_ACTION_OUTSIDE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 183 | case AMOTION_EVENT_ACTION_SCROLL: |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 184 | return {}; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 185 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 186 | case AMOTION_EVENT_ACTION_POINTER_UP: { |
Siarhei Vishniakou | 2f61bdc | 2022-12-02 08:55:51 -0800 | [diff] [blame] | 187 | const int32_t index = MotionEvent::getActionIndex(action); |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 188 | if (index < 0) { |
| 189 | return Error() << "invalid index " << index << " for " |
| 190 | << MotionEvent::actionToString(action); |
| 191 | } |
| 192 | if (index >= pointerCount) { |
| 193 | return Error() << "invalid index " << index << " for pointerCount " << pointerCount; |
| 194 | } |
| 195 | if (pointerCount <= 1) { |
| 196 | return Error() << "invalid pointer count " << pointerCount << " for " |
| 197 | << MotionEvent::actionToString(action); |
| 198 | } |
| 199 | return {}; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 200 | } |
| 201 | case AMOTION_EVENT_ACTION_BUTTON_PRESS: |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 202 | case AMOTION_EVENT_ACTION_BUTTON_RELEASE: { |
| 203 | if (actionButton == 0) { |
| 204 | return Error() << "action button should be nonzero for " |
| 205 | << MotionEvent::actionToString(action); |
| 206 | } |
| 207 | return {}; |
| 208 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 209 | default: |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 210 | return Error() << "invalid action " << action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 214 | int64_t millis(std::chrono::nanoseconds t) { |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 215 | return std::chrono::duration_cast<std::chrono::milliseconds>(t).count(); |
| 216 | } |
| 217 | |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 218 | Result<void> validateMotionEvent(int32_t action, int32_t actionButton, size_t pointerCount, |
| 219 | const PointerProperties* pointerProperties) { |
| 220 | Result<void> actionCheck = checkMotionAction(action, actionButton, pointerCount); |
| 221 | if (!actionCheck.ok()) { |
| 222 | return actionCheck; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 223 | } |
| 224 | if (pointerCount < 1 || pointerCount > MAX_POINTERS) { |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 225 | return Error() << "Motion event has invalid pointer count " << pointerCount |
| 226 | << "; value must be between 1 and " << MAX_POINTERS << "."; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 227 | } |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 228 | std::bitset<MAX_POINTER_ID + 1> pointerIdBits; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 229 | for (size_t i = 0; i < pointerCount; i++) { |
| 230 | int32_t id = pointerProperties[i].id; |
| 231 | if (id < 0 || id > MAX_POINTER_ID) { |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 232 | return Error() << "Motion event has invalid pointer id " << id |
| 233 | << "; value must be between 0 and " << MAX_POINTER_ID; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 234 | } |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 235 | if (pointerIdBits.test(id)) { |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 236 | return Error() << "Motion event has duplicate pointer id " << id; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 237 | } |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 238 | pointerIdBits.set(id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 239 | } |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 240 | return {}; |
| 241 | } |
| 242 | |
| 243 | Result<void> validateInputEvent(const InputEvent& event) { |
| 244 | switch (event.getType()) { |
| 245 | case InputEventType::KEY: { |
| 246 | const KeyEvent& key = static_cast<const KeyEvent&>(event); |
| 247 | const int32_t action = key.getAction(); |
| 248 | return validateKeyEvent(action); |
| 249 | } |
| 250 | case InputEventType::MOTION: { |
| 251 | const MotionEvent& motion = static_cast<const MotionEvent&>(event); |
| 252 | const int32_t action = motion.getAction(); |
| 253 | const size_t pointerCount = motion.getPointerCount(); |
| 254 | const PointerProperties* pointerProperties = motion.getPointerProperties(); |
| 255 | const int32_t actionButton = motion.getActionButton(); |
| 256 | return validateMotionEvent(action, actionButton, pointerCount, pointerProperties); |
| 257 | } |
| 258 | default: { |
| 259 | return {}; |
| 260 | } |
| 261 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 262 | } |
| 263 | |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 264 | std::bitset<MAX_POINTER_ID + 1> getPointerIds(const std::vector<PointerProperties>& pointers) { |
| 265 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
| 266 | for (const PointerProperties& pointer : pointers) { |
| 267 | pointerIds.set(pointer.id); |
| 268 | } |
| 269 | return pointerIds; |
| 270 | } |
| 271 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 272 | std::string dumpRegion(const Region& region) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 273 | if (region.isEmpty()) { |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 274 | return "<empty>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 275 | } |
| 276 | |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 277 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 278 | bool first = true; |
| 279 | Region::const_iterator cur = region.begin(); |
| 280 | Region::const_iterator const tail = region.end(); |
| 281 | while (cur != tail) { |
| 282 | if (first) { |
| 283 | first = false; |
| 284 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 285 | dump += "|"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 286 | } |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 287 | 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] | 288 | cur++; |
| 289 | } |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 290 | return dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 291 | } |
| 292 | |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 293 | std::string dumpQueue(const std::deque<std::unique_ptr<DispatchEntry>>& queue, |
| 294 | nsecs_t currentTime) { |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 295 | constexpr size_t maxEntries = 50; // max events to print |
| 296 | constexpr size_t skipBegin = maxEntries / 2; |
| 297 | const size_t skipEnd = queue.size() - maxEntries / 2; |
| 298 | // skip from maxEntries / 2 ... size() - maxEntries/2 |
| 299 | // only print from 0 .. skipBegin and then from skipEnd .. size() |
| 300 | |
| 301 | std::string dump; |
| 302 | for (size_t i = 0; i < queue.size(); i++) { |
| 303 | const DispatchEntry& entry = *queue[i]; |
| 304 | if (i >= skipBegin && i < skipEnd) { |
| 305 | dump += StringPrintf(INDENT4 "<skipped %zu entries>\n", skipEnd - skipBegin); |
| 306 | i = skipEnd - 1; // it will be incremented to "skipEnd" by 'continue' |
| 307 | continue; |
| 308 | } |
| 309 | dump.append(INDENT4); |
| 310 | dump += entry.eventEntry->getDescription(); |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 311 | dump += StringPrintf(", seq=%" PRIu32 ", targetFlags=%s, age=%" PRId64 "ms", entry.seq, |
| 312 | entry.targetFlags.string().c_str(), |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 313 | ns2ms(currentTime - entry.eventEntry->eventTime)); |
| 314 | if (entry.deliveryTime != 0) { |
| 315 | // This entry was delivered, so add information on how long we've been waiting |
| 316 | dump += StringPrintf(", wait=%" PRId64 "ms", ns2ms(currentTime - entry.deliveryTime)); |
| 317 | } |
| 318 | dump.append("\n"); |
| 319 | } |
| 320 | return dump; |
| 321 | } |
| 322 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 323 | /** |
| 324 | * Find the entry in std::unordered_map by key, and return it. |
| 325 | * If the entry is not found, return a default constructed entry. |
| 326 | * |
| 327 | * Useful when the entries are vectors, since an empty vector will be returned |
| 328 | * if the entry is not found. |
| 329 | * Also useful when the entries are sp<>. If an entry is not found, nullptr is returned. |
| 330 | */ |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 331 | template <typename K, typename V> |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 332 | V getValueByKey(const std::unordered_map<K, V>& map, K key) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 333 | auto it = map.find(key); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 334 | return it != map.end() ? it->second : V{}; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 335 | } |
| 336 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 337 | bool haveSameToken(const sp<WindowInfoHandle>& first, const sp<WindowInfoHandle>& second) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 338 | if (first == second) { |
| 339 | return true; |
| 340 | } |
| 341 | |
| 342 | if (first == nullptr || second == nullptr) { |
| 343 | return false; |
| 344 | } |
| 345 | |
| 346 | return first->getToken() == second->getToken(); |
| 347 | } |
| 348 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 349 | bool haveSameApplicationToken(const WindowInfo* first, const WindowInfo* second) { |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 350 | if (first == nullptr || second == nullptr) { |
| 351 | return false; |
| 352 | } |
| 353 | return first->applicationInfo.token != nullptr && |
| 354 | first->applicationInfo.token == second->applicationInfo.token; |
| 355 | } |
| 356 | |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 357 | template <typename T> |
| 358 | size_t firstMarkedBit(T set) { |
| 359 | // TODO: replace with std::countr_zero from <bit> when that's available |
| 360 | LOG_ALWAYS_FATAL_IF(set.none()); |
| 361 | size_t i = 0; |
| 362 | while (!set.test(i)) { |
| 363 | i++; |
| 364 | } |
| 365 | return i; |
| 366 | } |
| 367 | |
Prabir Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame] | 368 | std::unique_ptr<DispatchEntry> createDispatchEntry(const InputTarget& inputTarget, |
| 369 | std::shared_ptr<const EventEntry> eventEntry, |
| 370 | ftl::Flags<InputTarget::Flags> inputTargetFlags, |
| 371 | int64_t vsyncId) { |
| 372 | const sp<WindowInfoHandle> win = inputTarget.windowHandle; |
| 373 | const std::optional<int32_t> windowId = |
| 374 | win ? std::make_optional(win->getInfo()->id) : std::nullopt; |
| 375 | // Assume the only targets that are not associated with a window are global monitors, and use |
| 376 | // the system UID for global monitors for tracing purposes. |
| 377 | const gui::Uid uid = win ? win->getInfo()->ownerUid : gui::Uid(AID_SYSTEM); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 378 | if (inputTarget.useDefaultPointerTransform()) { |
| 379 | const ui::Transform& transform = inputTarget.getDefaultPointerTransform(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 380 | return std::make_unique<DispatchEntry>(eventEntry, inputTargetFlags, transform, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 381 | inputTarget.displayTransform, |
Prabir Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame] | 382 | inputTarget.globalScaleFactor, uid, vsyncId, |
| 383 | windowId); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | ALOG_ASSERT(eventEntry->type == EventEntry::Type::MOTION); |
| 387 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); |
| 388 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 389 | std::vector<PointerCoords> pointerCoords; |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 390 | pointerCoords.resize(motionEntry.getPointerCount()); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 391 | |
| 392 | // Use the first pointer information to normalize all other pointers. This could be any pointer |
| 393 | // 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] | 394 | // uses the transform for the normalized pointer. |
| 395 | const ui::Transform& firstPointerTransform = |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 396 | inputTarget.pointerTransforms[firstMarkedBit(inputTarget.pointerIds)]; |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 397 | ui::Transform inverseFirstTransform = firstPointerTransform.inverse(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 398 | |
| 399 | // Iterate through all pointers in the event to normalize against the first. |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 400 | for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.getPointerCount(); pointerIndex++) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 401 | const PointerProperties& pointerProperties = motionEntry.pointerProperties[pointerIndex]; |
| 402 | uint32_t pointerId = uint32_t(pointerProperties.id); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 403 | const ui::Transform& currTransform = inputTarget.pointerTransforms[pointerId]; |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 404 | |
| 405 | pointerCoords[pointerIndex].copyFrom(motionEntry.pointerCoords[pointerIndex]); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 406 | // First, apply the current pointer's transform to update the coordinates into |
| 407 | // window space. |
| 408 | pointerCoords[pointerIndex].transform(currTransform); |
| 409 | // Next, apply the inverse transform of the normalized coordinates so the |
| 410 | // current coordinates are transformed into the normalized coordinate space. |
| 411 | pointerCoords[pointerIndex].transform(inverseFirstTransform); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 414 | std::unique_ptr<MotionEntry> combinedMotionEntry = |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 415 | std::make_unique<MotionEntry>(motionEntry.id, motionEntry.injectionState, |
| 416 | motionEntry.eventTime, motionEntry.deviceId, |
| 417 | motionEntry.source, motionEntry.displayId, |
| 418 | motionEntry.policyFlags, motionEntry.action, |
| 419 | motionEntry.actionButton, motionEntry.flags, |
| 420 | motionEntry.metaState, motionEntry.buttonState, |
| 421 | motionEntry.classification, motionEntry.edgeFlags, |
| 422 | motionEntry.xPrecision, motionEntry.yPrecision, |
| 423 | motionEntry.xCursorPosition, motionEntry.yCursorPosition, |
| 424 | motionEntry.downTime, motionEntry.pointerProperties, |
| 425 | pointerCoords); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 426 | |
| 427 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 428 | std::make_unique<DispatchEntry>(std::move(combinedMotionEntry), inputTargetFlags, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 429 | firstPointerTransform, inputTarget.displayTransform, |
Prabir Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame] | 430 | inputTarget.globalScaleFactor, uid, vsyncId, windowId); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 431 | return dispatchEntry; |
| 432 | } |
| 433 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 434 | template <typename T> |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 435 | 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] | 436 | if (lhs == nullptr && rhs == nullptr) { |
| 437 | return true; |
| 438 | } |
| 439 | if (lhs == nullptr || rhs == nullptr) { |
| 440 | return false; |
| 441 | } |
| 442 | return *lhs == *rhs; |
| 443 | } |
| 444 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 445 | KeyEvent createKeyEvent(const KeyEntry& entry) { |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 446 | KeyEvent event; |
| 447 | event.initialize(entry.id, entry.deviceId, entry.source, entry.displayId, INVALID_HMAC, |
| 448 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, |
| 449 | entry.repeatCount, entry.downTime, entry.eventTime); |
| 450 | return event; |
| 451 | } |
| 452 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 453 | bool shouldReportMetricsForConnection(const Connection& connection) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 454 | // Do not keep track of gesture monitors. They receive every event and would disproportionately |
| 455 | // affect the statistics. |
| 456 | if (connection.monitor) { |
| 457 | return false; |
| 458 | } |
| 459 | // If the connection is experiencing ANR, let's skip it. We have separate ANR metrics |
| 460 | if (!connection.responsive) { |
| 461 | return false; |
| 462 | } |
| 463 | return true; |
| 464 | } |
| 465 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 466 | bool shouldReportFinishedEvent(const DispatchEntry& dispatchEntry, const Connection& connection) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 467 | const EventEntry& eventEntry = *dispatchEntry.eventEntry; |
| 468 | const int32_t& inputEventId = eventEntry.id; |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 469 | if (inputEventId == android::os::IInputConstants::INVALID_INPUT_EVENT_ID) { |
| 470 | return false; |
| 471 | } |
| 472 | // Only track latency for events that originated from hardware |
| 473 | if (eventEntry.isSynthesized()) { |
| 474 | return false; |
| 475 | } |
| 476 | const EventEntry::Type& inputEventEntryType = eventEntry.type; |
| 477 | if (inputEventEntryType == EventEntry::Type::KEY) { |
| 478 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 479 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
| 480 | return false; |
| 481 | } |
| 482 | } else if (inputEventEntryType == EventEntry::Type::MOTION) { |
| 483 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 484 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL || |
| 485 | motionEntry.action == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 486 | return false; |
| 487 | } |
| 488 | } else { |
| 489 | // Not a key or a motion |
| 490 | return false; |
| 491 | } |
| 492 | if (!shouldReportMetricsForConnection(connection)) { |
| 493 | return false; |
| 494 | } |
| 495 | return true; |
| 496 | } |
| 497 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 498 | /** |
| 499 | * Connection is responsive if it has no events in the waitQueue that are older than the |
| 500 | * current time. |
| 501 | */ |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 502 | bool isConnectionResponsive(const Connection& connection) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 503 | const nsecs_t currentTime = now(); |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 504 | for (const auto& dispatchEntry : connection.waitQueue) { |
| 505 | if (dispatchEntry->timeoutTime < currentTime) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 506 | return false; |
| 507 | } |
| 508 | } |
| 509 | return true; |
| 510 | } |
| 511 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 512 | // Returns true if the event type passed as argument represents a user activity. |
| 513 | bool isUserActivityEvent(const EventEntry& eventEntry) { |
| 514 | switch (eventEntry.type) { |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 515 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 516 | case EventEntry::Type::DEVICE_RESET: |
| 517 | case EventEntry::Type::DRAG: |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 518 | case EventEntry::Type::FOCUS: |
| 519 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 520 | case EventEntry::Type::SENSOR: |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 521 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 522 | return false; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 523 | case EventEntry::Type::KEY: |
| 524 | case EventEntry::Type::MOTION: |
| 525 | return true; |
| 526 | } |
| 527 | } |
| 528 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 529 | // 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] | 530 | bool windowAcceptsTouchAt(const WindowInfo& windowInfo, int32_t displayId, float x, float y, |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 531 | bool isStylus, const ui::Transform& displayTransform) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 532 | const auto inputConfig = windowInfo.inputConfig; |
| 533 | if (windowInfo.displayId != displayId || |
| 534 | inputConfig.test(WindowInfo::InputConfig::NOT_VISIBLE)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 535 | return false; |
| 536 | } |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 537 | const bool windowCanInterceptTouch = isStylus && windowInfo.interceptsStylus(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 538 | if (inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE) && !windowCanInterceptTouch) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 539 | return false; |
| 540 | } |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 541 | |
| 542 | // Window Manager works in the logical display coordinate space. When it specifies bounds for a |
| 543 | // window as (l, t, r, b), the range of x in [l, r) and y in [t, b) are considered to be inside |
| 544 | // the window. Points on the right and bottom edges should not be inside the window, so we need |
| 545 | // to be careful about performing a hit test when the display is rotated, since the "right" and |
| 546 | // "bottom" of the window will be different in the display (un-rotated) space compared to in the |
| 547 | // logical display in which WM determined the bounds. Perform the hit test in the logical |
| 548 | // display space to ensure these edges are considered correctly in all orientations. |
| 549 | const auto touchableRegion = displayTransform.transform(windowInfo.touchableRegion); |
| 550 | const auto p = displayTransform.transform(x, y); |
| 551 | if (!touchableRegion.contains(std::floor(p.x), std::floor(p.y))) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 552 | return false; |
| 553 | } |
| 554 | return true; |
| 555 | } |
| 556 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 557 | bool isPointerFromStylus(const MotionEntry& entry, int32_t pointerIndex) { |
| 558 | return isFromSource(entry.source, AINPUT_SOURCE_STYLUS) && |
Prabir Pradhan | e562696 | 2022-10-27 20:30:53 +0000 | [diff] [blame] | 559 | isStylusToolType(entry.pointerProperties[pointerIndex].toolType); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 560 | } |
| 561 | |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 562 | // Determines if the given window can be targeted as InputTarget::Flags::FOREGROUND. |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 563 | // Foreground events are only sent to "foreground targetable" windows, but not all gestures sent to |
| 564 | // such window are necessarily targeted with the flag. For example, an event with ACTION_OUTSIDE can |
| 565 | // 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] | 566 | // InputTarget::Flags::FOREGROUND. |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 567 | bool canReceiveForegroundTouches(const WindowInfo& info) { |
| 568 | // A non-touchable window can still receive touch events (e.g. in the case of |
| 569 | // STYLUS_INTERCEPTOR), so prevent such windows from receiving foreground events for touches. |
| 570 | return !info.inputConfig.test(gui::WindowInfo::InputConfig::NOT_TOUCHABLE) && !info.isSpy(); |
| 571 | } |
| 572 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 573 | bool isWindowOwnedBy(const sp<WindowInfoHandle>& windowHandle, gui::Pid pid, gui::Uid uid) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 574 | if (windowHandle == nullptr) { |
| 575 | return false; |
| 576 | } |
| 577 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 578 | if (pid == windowInfo->ownerPid && uid == windowInfo->ownerUid) { |
| 579 | return true; |
| 580 | } |
| 581 | return false; |
| 582 | } |
| 583 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 584 | // Checks targeted injection using the window's owner's uid. |
| 585 | // Returns an empty string if an entry can be sent to the given window, or an error message if the |
| 586 | // entry is a targeted injection whose uid target doesn't match the window owner. |
| 587 | std::optional<std::string> verifyTargetedInjection(const sp<WindowInfoHandle>& window, |
| 588 | const EventEntry& entry) { |
| 589 | if (entry.injectionState == nullptr || !entry.injectionState->targetUid) { |
| 590 | // The event was not injected, or the injected event does not target a window. |
| 591 | return {}; |
| 592 | } |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 593 | const auto uid = *entry.injectionState->targetUid; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 594 | if (window == nullptr) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 595 | return StringPrintf("No valid window target for injection into uid %s.", |
| 596 | uid.toString().c_str()); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 597 | } |
| 598 | if (entry.injectionState->targetUid != window->getInfo()->ownerUid) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 599 | return StringPrintf("Injected event targeted at uid %s would be dispatched to window '%s' " |
| 600 | "owned by uid %s.", |
| 601 | uid.toString().c_str(), window->getName().c_str(), |
| 602 | window->getInfo()->ownerUid.toString().c_str()); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 603 | } |
| 604 | return {}; |
| 605 | } |
| 606 | |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 607 | std::pair<float, float> resolveTouchedPosition(const MotionEntry& entry) { |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 608 | const bool isFromMouse = isFromSource(entry.source, AINPUT_SOURCE_MOUSE); |
| 609 | // Always dispatch mouse events to cursor position. |
| 610 | if (isFromMouse) { |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 611 | return {entry.xCursorPosition, entry.yCursorPosition}; |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 612 | } |
| 613 | |
Siarhei Vishniakou | 5b9766d | 2023-07-18 14:06:29 -0700 | [diff] [blame] | 614 | const int32_t pointerIndex = MotionEvent::getActionIndex(entry.action); |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 615 | return {entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 616 | entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_Y)}; |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 617 | } |
| 618 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 619 | std::optional<nsecs_t> getDownTime(const EventEntry& eventEntry) { |
| 620 | if (eventEntry.type == EventEntry::Type::KEY) { |
| 621 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 622 | return keyEntry.downTime; |
| 623 | } else if (eventEntry.type == EventEntry::Type::MOTION) { |
| 624 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 625 | return motionEntry.downTime; |
| 626 | } |
| 627 | return std::nullopt; |
| 628 | } |
| 629 | |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 630 | /** |
| 631 | * Compare the old touch state to the new touch state, and generate the corresponding touched |
| 632 | * windows (== input targets). |
| 633 | * If a window had the hovering pointer, but now it doesn't, produce HOVER_EXIT for that window. |
| 634 | * If the pointer just entered the new window, produce HOVER_ENTER. |
| 635 | * For pointers remaining in the window, produce HOVER_MOVE. |
| 636 | */ |
| 637 | std::vector<TouchedWindow> getHoveringWindowsLocked(const TouchState* oldState, |
| 638 | const TouchState& newTouchState, |
| 639 | const MotionEntry& entry) { |
| 640 | std::vector<TouchedWindow> out; |
| 641 | const int32_t maskedAction = MotionEvent::getActionMasked(entry.action); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 642 | |
| 643 | if (maskedAction == AMOTION_EVENT_ACTION_SCROLL) { |
| 644 | // ACTION_SCROLL events should not affect the hovering pointer dispatch |
| 645 | return {}; |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | // We should consider all hovering pointers here. But for now, just use the first one |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 649 | const PointerProperties& pointer = entry.pointerProperties[0]; |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 650 | |
| 651 | std::set<sp<WindowInfoHandle>> oldWindows; |
| 652 | if (oldState != nullptr) { |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 653 | oldWindows = oldState->getWindowsWithHoveringPointer(entry.deviceId, pointer.id); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | std::set<sp<WindowInfoHandle>> newWindows = |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 657 | newTouchState.getWindowsWithHoveringPointer(entry.deviceId, pointer.id); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 658 | |
| 659 | // If the pointer is no longer in the new window set, send HOVER_EXIT. |
| 660 | for (const sp<WindowInfoHandle>& oldWindow : oldWindows) { |
| 661 | if (newWindows.find(oldWindow) == newWindows.end()) { |
| 662 | TouchedWindow touchedWindow; |
| 663 | touchedWindow.windowHandle = oldWindow; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 664 | touchedWindow.dispatchMode = InputTarget::DispatchMode::HOVER_EXIT; |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 665 | out.push_back(touchedWindow); |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | for (const sp<WindowInfoHandle>& newWindow : newWindows) { |
| 670 | TouchedWindow touchedWindow; |
| 671 | touchedWindow.windowHandle = newWindow; |
| 672 | if (oldWindows.find(newWindow) == oldWindows.end()) { |
| 673 | // Any windows that have this pointer now, and didn't have it before, should get |
| 674 | // HOVER_ENTER |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 675 | touchedWindow.dispatchMode = InputTarget::DispatchMode::HOVER_ENTER; |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 676 | } else { |
| 677 | // This pointer was already sent to the window. Use ACTION_HOVER_MOVE. |
Siarhei Vishniakou | c2eb850 | 2023-04-11 18:33:36 -0700 | [diff] [blame] | 678 | if (CC_UNLIKELY(maskedAction != AMOTION_EVENT_ACTION_HOVER_MOVE)) { |
Daniel Norman | 7487dfa | 2023-08-02 16:39:45 -0700 | [diff] [blame] | 679 | android::base::LogSeverity severity = android::base::LogSeverity::FATAL; |
Ameer Armaly | cff4fa5 | 2023-10-04 23:45:11 +0000 | [diff] [blame] | 680 | if (!input_flags::a11y_crash_on_inconsistent_event_stream() && |
| 681 | entry.flags & AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT) { |
Daniel Norman | 7487dfa | 2023-08-02 16:39:45 -0700 | [diff] [blame] | 682 | // The Accessibility injected touch exploration event stream |
| 683 | // has known inconsistencies, so log ERROR instead of |
| 684 | // crashing the device with FATAL. |
Daniel Norman | 7487dfa | 2023-08-02 16:39:45 -0700 | [diff] [blame] | 685 | severity = android::base::LogSeverity::ERROR; |
| 686 | } |
| 687 | LOG(severity) << "Expected ACTION_HOVER_MOVE instead of " << entry.getDescription(); |
Siarhei Vishniakou | c2eb850 | 2023-04-11 18:33:36 -0700 | [diff] [blame] | 688 | } |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 689 | touchedWindow.dispatchMode = InputTarget::DispatchMode::AS_IS; |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 690 | } |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 691 | touchedWindow.addHoveringPointer(entry.deviceId, pointer); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 692 | if (canReceiveForegroundTouches(*newWindow->getInfo())) { |
| 693 | touchedWindow.targetFlags |= InputTarget::Flags::FOREGROUND; |
| 694 | } |
| 695 | out.push_back(touchedWindow); |
| 696 | } |
| 697 | return out; |
| 698 | } |
| 699 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 700 | template <typename T> |
| 701 | std::vector<T>& operator+=(std::vector<T>& left, const std::vector<T>& right) { |
| 702 | left.insert(left.end(), right.begin(), right.end()); |
| 703 | return left; |
| 704 | } |
| 705 | |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 706 | // Filter windows in a TouchState and targets in a vector to remove untrusted windows/targets from |
| 707 | // both. |
| 708 | void filterUntrustedTargets(TouchState& touchState, std::vector<InputTarget>& targets) { |
| 709 | std::erase_if(touchState.windows, [&](const TouchedWindow& window) { |
| 710 | if (!window.windowHandle->getInfo()->inputConfig.test( |
| 711 | WindowInfo::InputConfig::TRUSTED_OVERLAY)) { |
| 712 | // In addition to TouchState, erase this window from the input targets! We don't have a |
| 713 | // good way to do this today except by adding a nested loop. |
| 714 | // TODO(b/282025641): simplify this code once InputTargets are being identified |
| 715 | // separately from TouchedWindows. |
| 716 | std::erase_if(targets, [&](const InputTarget& target) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 717 | return target.connection->getToken() == window.windowHandle->getToken(); |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 718 | }); |
| 719 | return true; |
| 720 | } |
| 721 | return false; |
| 722 | }); |
| 723 | } |
| 724 | |
Siarhei Vishniakou | ce1fd47 | 2023-09-18 18:38:07 -0700 | [diff] [blame] | 725 | /** |
| 726 | * In general, touch should be always split between windows. Some exceptions: |
| 727 | * 1. Don't split touch if all of the below is true: |
| 728 | * (a) we have an active pointer down *and* |
| 729 | * (b) a new pointer is going down that's from the same device *and* |
| 730 | * (c) the window that's receiving the current pointer does not support split touch. |
| 731 | * 2. Don't split mouse events |
| 732 | */ |
| 733 | bool shouldSplitTouch(const TouchState& touchState, const MotionEntry& entry) { |
| 734 | if (isFromSource(entry.source, AINPUT_SOURCE_MOUSE)) { |
| 735 | // We should never split mouse events |
| 736 | return false; |
| 737 | } |
| 738 | for (const TouchedWindow& touchedWindow : touchState.windows) { |
| 739 | if (touchedWindow.windowHandle->getInfo()->isSpy()) { |
| 740 | // Spy windows should not affect whether or not touch is split. |
| 741 | continue; |
| 742 | } |
| 743 | if (touchedWindow.windowHandle->getInfo()->supportsSplitTouch()) { |
| 744 | continue; |
| 745 | } |
| 746 | if (touchedWindow.windowHandle->getInfo()->inputConfig.test( |
| 747 | gui::WindowInfo::InputConfig::IS_WALLPAPER)) { |
| 748 | // Wallpaper window should not affect whether or not touch is split |
| 749 | continue; |
| 750 | } |
| 751 | |
| 752 | if (touchedWindow.hasTouchingPointers(entry.deviceId)) { |
| 753 | return false; |
| 754 | } |
| 755 | } |
| 756 | return true; |
| 757 | } |
| 758 | |
Siarhei Vishniakou | f77f60a | 2023-10-23 17:26:05 -0700 | [diff] [blame] | 759 | /** |
| 760 | * Return true if stylus is currently down anywhere on the specified display, and false otherwise. |
| 761 | */ |
| 762 | bool isStylusActiveInDisplay( |
| 763 | int32_t displayId, |
| 764 | const std::unordered_map<int32_t /*displayId*/, TouchState>& touchStatesByDisplay) { |
| 765 | const auto it = touchStatesByDisplay.find(displayId); |
| 766 | if (it == touchStatesByDisplay.end()) { |
| 767 | return false; |
| 768 | } |
| 769 | const TouchState& state = it->second; |
| 770 | return state.hasActiveStylus(); |
| 771 | } |
| 772 | |
Siarhei Vishniakou | aeed0da | 2024-01-09 08:57:13 -0800 | [diff] [blame] | 773 | Result<void> validateWindowInfosUpdate(const gui::WindowInfosUpdate& update) { |
| 774 | struct HashFunction { |
| 775 | size_t operator()(const WindowInfo& info) const { return info.id; } |
| 776 | }; |
| 777 | |
| 778 | std::unordered_set<WindowInfo, HashFunction> windowSet; |
| 779 | for (const WindowInfo& info : update.windowInfos) { |
| 780 | const auto [_, inserted] = windowSet.insert(info); |
| 781 | if (!inserted) { |
| 782 | return Error() << "Duplicate entry for " << info; |
| 783 | } |
| 784 | } |
| 785 | return {}; |
| 786 | } |
| 787 | |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 788 | int32_t getUserActivityEventType(const EventEntry& eventEntry) { |
| 789 | switch (eventEntry.type) { |
| 790 | case EventEntry::Type::KEY: { |
| 791 | return USER_ACTIVITY_EVENT_BUTTON; |
| 792 | } |
| 793 | case EventEntry::Type::MOTION: { |
| 794 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 795 | if (MotionEvent::isTouchEvent(motionEntry.source, motionEntry.action)) { |
| 796 | return USER_ACTIVITY_EVENT_TOUCH; |
| 797 | } |
| 798 | return USER_ACTIVITY_EVENT_OTHER; |
| 799 | } |
| 800 | default: { |
| 801 | LOG_ALWAYS_FATAL("%s events are not user activity", |
| 802 | ftl::enum_string(eventEntry.type).c_str()); |
| 803 | } |
| 804 | } |
| 805 | } |
| 806 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 807 | std::pair<bool /*cancelPointers*/, bool /*cancelNonPointers*/> expandCancellationMode( |
| 808 | CancelationOptions::Mode mode) { |
| 809 | switch (mode) { |
| 810 | case CancelationOptions::Mode::CANCEL_ALL_EVENTS: |
| 811 | return {true, true}; |
| 812 | case CancelationOptions::Mode::CANCEL_POINTER_EVENTS: |
| 813 | return {true, false}; |
| 814 | case CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS: |
| 815 | return {false, true}; |
| 816 | case CancelationOptions::Mode::CANCEL_FALLBACK_EVENTS: |
| 817 | return {false, true}; |
| 818 | } |
| 819 | } |
| 820 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 821 | } // namespace |
| 822 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 823 | // --- InputDispatcher --- |
| 824 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 825 | InputDispatcher::InputDispatcher(InputDispatcherPolicyInterface& policy) |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 826 | : InputDispatcher(policy, |
| 827 | isInputTracingEnabled() ? std::make_unique<trace::impl::PerfettoBackend>() |
| 828 | : nullptr) {} |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 829 | |
| 830 | InputDispatcher::InputDispatcher(InputDispatcherPolicyInterface& policy, |
| 831 | std::unique_ptr<trace::InputTracingBackendInterface> traceBackend) |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 832 | : mPolicy(policy), |
| 833 | mPendingEvent(nullptr), |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 834 | mLastDropReason(DropReason::NOT_DROPPED), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 835 | mIdGenerator(IdGenerator::Source::INPUT_DISPATCHER), |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 836 | mMinTimeBetweenUserActivityPokes(DEFAULT_USER_ACTIVITY_POKE_INTERVAL), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 837 | mNextUnblockedEvent(nullptr), |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 838 | mMonitorDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 839 | mDispatchEnabled(false), |
| 840 | mDispatchFrozen(false), |
| 841 | mInputFilterEnabled(false), |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 842 | mMaximumObscuringOpacityForTouch(1.0f), |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 843 | mFocusedDisplayId(ADISPLAY_ID_DEFAULT), |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 844 | mWindowTokenWithPointerCapture(nullptr), |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 845 | mLatencyAggregator(), |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 846 | mLatencyTracker(&mLatencyAggregator) { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 847 | mLooper = sp<Looper>::make(false); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 848 | mReporter = createInputReporter(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 849 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 850 | mWindowInfoListener = sp<DispatcherWindowListener>::make(*this); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 851 | #if defined(__ANDROID__) |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 852 | SurfaceComposerClient::getDefault()->addWindowInfosListener(mWindowInfoListener); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 853 | #endif |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 854 | mKeyRepeatState.lastKeyEntry = nullptr; |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 855 | |
| 856 | if (traceBackend) { |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 857 | mTracer = std::make_unique<trace::impl::InputTracer>(std::move(traceBackend)); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 858 | } |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 859 | |
| 860 | mLastUserActivityTimes.fill(0); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | InputDispatcher::~InputDispatcher() { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 864 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 865 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 866 | resetKeyRepeatLocked(); |
| 867 | releasePendingEventLocked(); |
| 868 | drainInboundQueueLocked(); |
| 869 | mCommandQueue.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 870 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 871 | while (!mConnectionsByToken.empty()) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 872 | std::shared_ptr<Connection> connection = mConnectionsByToken.begin()->second; |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 873 | removeInputChannelLocked(connection->getToken(), /*notify=*/false); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 874 | } |
| 875 | } |
| 876 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 877 | status_t InputDispatcher::start() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 878 | if (mThread) { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 879 | return ALREADY_EXISTS; |
| 880 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 881 | mThread = std::make_unique<InputThread>( |
| 882 | "InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); }); |
| 883 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | status_t InputDispatcher::stop() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 887 | if (mThread && mThread->isCallingThread()) { |
| 888 | ALOGE("InputDispatcher cannot be stopped from its own thread!"); |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 889 | return INVALID_OPERATION; |
| 890 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 891 | mThread.reset(); |
| 892 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 893 | } |
| 894 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 895 | void InputDispatcher::dispatchOnce() { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 896 | nsecs_t nextWakeupTime = LLONG_MAX; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 897 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 898 | std::scoped_lock _l(mLock); |
| 899 | mDispatcherIsAlive.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 900 | |
| 901 | // Run a dispatch loop if there are no pending commands. |
| 902 | // The dispatch loop might enqueue commands to run afterwards. |
| 903 | if (!haveCommandsLocked()) { |
Siarhei Vishniakou | 6950596 | 2023-12-28 12:07:04 -0800 | [diff] [blame] | 904 | dispatchOnceInnerLocked(/*byref*/ nextWakeupTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | // Run all pending commands if there are any. |
| 908 | // 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] | 909 | if (runCommandsLockedInterruptable()) { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 910 | nextWakeupTime = LLONG_MIN; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 911 | } |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 912 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 913 | // If we are still waiting for ack on some events, |
| 914 | // we might have to wake up earlier to check if an app is anr'ing. |
| 915 | const nsecs_t nextAnrCheck = processAnrsLocked(); |
| 916 | nextWakeupTime = std::min(nextWakeupTime, nextAnrCheck); |
| 917 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 918 | // We are about to enter an infinitely long sleep, because we have no commands or |
| 919 | // pending or queued events |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 920 | if (nextWakeupTime == LLONG_MAX) { |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 921 | mDispatcherEnteredIdle.notify_all(); |
| 922 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 923 | } // release lock |
| 924 | |
| 925 | // Wait for callback or timeout or wake. (make sure we round up, not down) |
| 926 | nsecs_t currentTime = now(); |
| 927 | int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime); |
| 928 | mLooper->pollOnce(timeoutMillis); |
| 929 | } |
| 930 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 931 | /** |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 932 | * Raise ANR if there is no focused window. |
| 933 | * Before the ANR is raised, do a final state check: |
| 934 | * 1. The currently focused application must be the same one we are waiting for. |
| 935 | * 2. Ensure we still don't have a focused window. |
| 936 | */ |
| 937 | void InputDispatcher::processNoFocusedWindowAnrLocked() { |
| 938 | // Check if the application that we are waiting for is still focused. |
| 939 | std::shared_ptr<InputApplicationHandle> focusedApplication = |
| 940 | getValueByKey(mFocusedApplicationHandlesByDisplay, mAwaitedApplicationDisplayId); |
| 941 | if (focusedApplication == nullptr || |
| 942 | focusedApplication->getApplicationToken() != |
| 943 | mAwaitedFocusedApplication->getApplicationToken()) { |
| 944 | // Unexpected because we should have reset the ANR timer when focused application changed |
| 945 | ALOGE("Waited for a focused window, but focused application has already changed to %s", |
| 946 | focusedApplication->getName().c_str()); |
| 947 | return; // The focused application has changed. |
| 948 | } |
| 949 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 950 | const sp<WindowInfoHandle>& focusedWindowHandle = |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 951 | getFocusedWindowHandleLocked(mAwaitedApplicationDisplayId); |
| 952 | if (focusedWindowHandle != nullptr) { |
| 953 | return; // We now have a focused window. No need for ANR. |
| 954 | } |
| 955 | onAnrLocked(mAwaitedFocusedApplication); |
| 956 | } |
| 957 | |
| 958 | /** |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 959 | * Check if any of the connections' wait queues have events that are too old. |
| 960 | * If we waited for events to be ack'ed for more than the window timeout, raise an ANR. |
| 961 | * Return the time at which we should wake up next. |
| 962 | */ |
| 963 | nsecs_t InputDispatcher::processAnrsLocked() { |
| 964 | const nsecs_t currentTime = now(); |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 965 | nsecs_t nextAnrCheck = LLONG_MAX; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 966 | // Check if we are waiting for a focused window to appear. Raise ANR if waited too long |
| 967 | if (mNoFocusedWindowTimeoutTime.has_value() && mAwaitedFocusedApplication != nullptr) { |
| 968 | if (currentTime >= *mNoFocusedWindowTimeoutTime) { |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 969 | processNoFocusedWindowAnrLocked(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 970 | mAwaitedFocusedApplication.reset(); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 971 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 972 | return LLONG_MIN; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 973 | } else { |
Siarhei Vishniakou | 38a6d27 | 2020-10-20 20:29:33 -0500 | [diff] [blame] | 974 | // Keep waiting. We will drop the event when mNoFocusedWindowTimeoutTime comes. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 975 | nextAnrCheck = *mNoFocusedWindowTimeoutTime; |
| 976 | } |
| 977 | } |
| 978 | |
| 979 | // Check if any connection ANRs are due |
| 980 | nextAnrCheck = std::min(nextAnrCheck, mAnrTracker.firstTimeout()); |
| 981 | if (currentTime < nextAnrCheck) { // most likely scenario |
| 982 | return nextAnrCheck; // everything is normal. Let's check again at nextAnrCheck |
| 983 | } |
| 984 | |
| 985 | // If we reached here, we have an unresponsive connection. |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 986 | std::shared_ptr<Connection> connection = getConnectionLocked(mAnrTracker.firstToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 987 | if (connection == nullptr) { |
| 988 | ALOGE("Could not find connection for entry %" PRId64, mAnrTracker.firstTimeout()); |
| 989 | return nextAnrCheck; |
| 990 | } |
| 991 | connection->responsive = false; |
| 992 | // Stop waking up for this unresponsive connection |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 993 | mAnrTracker.eraseToken(connection->getToken()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 994 | onAnrLocked(connection); |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 995 | return LLONG_MIN; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 996 | } |
| 997 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 998 | std::chrono::nanoseconds InputDispatcher::getDispatchingTimeoutLocked( |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 999 | const std::shared_ptr<Connection>& connection) { |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 1000 | if (connection->monitor) { |
| 1001 | return mMonitorDispatchingTimeout; |
| 1002 | } |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 1003 | const sp<WindowInfoHandle> window = getWindowHandleLocked(connection->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1004 | if (window != nullptr) { |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 1005 | return window->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1006 | } |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 1007 | return DEFAULT_INPUT_DISPATCHING_TIMEOUT; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1008 | } |
| 1009 | |
Siarhei Vishniakou | 6950596 | 2023-12-28 12:07:04 -0800 | [diff] [blame] | 1010 | void InputDispatcher::dispatchOnceInnerLocked(nsecs_t& nextWakeupTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1011 | nsecs_t currentTime = now(); |
| 1012 | |
Jeff Brown | dc5992e | 2014-04-11 01:27:26 -0700 | [diff] [blame] | 1013 | // Reset the key repeat timer whenever normal dispatch is suspended while the |
| 1014 | // device is in a non-interactive state. This is to ensure that we abort a key |
| 1015 | // repeat if the device is just coming out of sleep. |
| 1016 | if (!mDispatchEnabled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1017 | resetKeyRepeatLocked(); |
| 1018 | } |
| 1019 | |
| 1020 | // If dispatching is frozen, do not process timeouts or try to deliver any new events. |
| 1021 | if (mDispatchFrozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1022 | if (DEBUG_FOCUS) { |
| 1023 | ALOGD("Dispatch frozen. Waiting some more."); |
| 1024 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1025 | return; |
| 1026 | } |
| 1027 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1028 | // Ready to start a new event. |
| 1029 | // If we don't already have a pending event, go grab one. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1030 | if (!mPendingEvent) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1031 | if (mInboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1032 | // Synthesize a key repeat if appropriate. |
| 1033 | if (mKeyRepeatState.lastKeyEntry) { |
| 1034 | if (currentTime >= mKeyRepeatState.nextRepeatTime) { |
| 1035 | mPendingEvent = synthesizeKeyRepeatLocked(currentTime); |
| 1036 | } else { |
Siarhei Vishniakou | 6950596 | 2023-12-28 12:07:04 -0800 | [diff] [blame] | 1037 | nextWakeupTime = std::min(nextWakeupTime, mKeyRepeatState.nextRepeatTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1038 | } |
| 1039 | } |
| 1040 | |
| 1041 | // Nothing to do if there is no pending event. |
| 1042 | if (!mPendingEvent) { |
| 1043 | return; |
| 1044 | } |
| 1045 | } else { |
| 1046 | // Inbound queue has at least one entry. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1047 | mPendingEvent = mInboundQueue.front(); |
| 1048 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1049 | traceInboundQueueLengthLocked(); |
| 1050 | } |
| 1051 | |
| 1052 | // Poke user activity for this event. |
| 1053 | if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1054 | pokeUserActivityLocked(*mPendingEvent); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1055 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1056 | } |
| 1057 | |
| 1058 | // Now we have an event to dispatch. |
| 1059 | // 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] | 1060 | ALOG_ASSERT(mPendingEvent != nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1061 | bool done = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1062 | DropReason dropReason = DropReason::NOT_DROPPED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1063 | if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1064 | dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1065 | } else if (!mDispatchEnabled) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1066 | dropReason = DropReason::DISABLED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | if (mNextUnblockedEvent == mPendingEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1070 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1071 | } |
| 1072 | |
| 1073 | switch (mPendingEvent->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1074 | case EventEntry::Type::CONFIGURATION_CHANGED: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1075 | const ConfigurationChangedEntry& typedEntry = |
| 1076 | static_cast<const ConfigurationChangedEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1077 | done = dispatchConfigurationChangedLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1078 | dropReason = DropReason::NOT_DROPPED; // configuration changes are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1079 | break; |
| 1080 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1081 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1082 | case EventEntry::Type::DEVICE_RESET: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1083 | const DeviceResetEntry& typedEntry = |
| 1084 | static_cast<const DeviceResetEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1085 | done = dispatchDeviceResetLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1086 | dropReason = DropReason::NOT_DROPPED; // device resets are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1087 | break; |
| 1088 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1089 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1090 | case EventEntry::Type::FOCUS: { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1091 | std::shared_ptr<const FocusEntry> typedEntry = |
| 1092 | std::static_pointer_cast<const FocusEntry>(mPendingEvent); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1093 | dispatchFocusLocked(currentTime, typedEntry); |
| 1094 | done = true; |
| 1095 | dropReason = DropReason::NOT_DROPPED; // focus events are never dropped |
| 1096 | break; |
| 1097 | } |
| 1098 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1099 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1100 | const auto typedEntry = std::static_pointer_cast<const TouchModeEntry>(mPendingEvent); |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1101 | dispatchTouchModeChangeLocked(currentTime, typedEntry); |
| 1102 | done = true; |
| 1103 | dropReason = DropReason::NOT_DROPPED; // touch mode events are never dropped |
| 1104 | break; |
| 1105 | } |
| 1106 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1107 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 1108 | const auto typedEntry = |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1109 | std::static_pointer_cast<const PointerCaptureChangedEntry>(mPendingEvent); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1110 | dispatchPointerCaptureChangedLocked(currentTime, typedEntry, dropReason); |
| 1111 | done = true; |
| 1112 | break; |
| 1113 | } |
| 1114 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1115 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1116 | std::shared_ptr<const DragEntry> typedEntry = |
| 1117 | std::static_pointer_cast<const DragEntry>(mPendingEvent); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1118 | dispatchDragLocked(currentTime, typedEntry); |
| 1119 | done = true; |
| 1120 | break; |
| 1121 | } |
| 1122 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1123 | case EventEntry::Type::KEY: { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1124 | std::shared_ptr<const KeyEntry> keyEntry = |
| 1125 | std::static_pointer_cast<const KeyEntry>(mPendingEvent); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1126 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *keyEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1127 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1128 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1129 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 1130 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1131 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1132 | done = dispatchKeyLocked(currentTime, keyEntry, &dropReason, nextWakeupTime); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 1133 | if (done && mTracer) { |
| 1134 | ensureEventTraced(*keyEntry); |
| 1135 | mTracer->eventProcessingComplete(*keyEntry->traceTracker); |
| 1136 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1137 | break; |
| 1138 | } |
| 1139 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1140 | case EventEntry::Type::MOTION: { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1141 | std::shared_ptr<const MotionEntry> motionEntry = |
| 1142 | std::static_pointer_cast<const MotionEntry>(mPendingEvent); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1143 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *motionEntry)) { |
Siarhei Vishniakou | 6b71b63 | 2023-10-27 21:34:46 -0700 | [diff] [blame] | 1144 | // The event is stale. However, only drop stale events if there isn't an ongoing |
| 1145 | // gesture. That would allow us to complete the processing of the current stroke. |
| 1146 | const auto touchStateIt = mTouchStatesByDisplay.find(motionEntry->displayId); |
| 1147 | if (touchStateIt != mTouchStatesByDisplay.end()) { |
| 1148 | const TouchState& touchState = touchStateIt->second; |
| 1149 | if (!touchState.hasTouchingPointers(motionEntry->deviceId) && |
| 1150 | !touchState.hasHoveringPointers(motionEntry->deviceId)) { |
| 1151 | dropReason = DropReason::STALE; |
| 1152 | } |
| 1153 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1154 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1155 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
Siarhei Vishniakou | 99e407b | 2023-12-26 18:09:32 -0800 | [diff] [blame] | 1156 | if (!isFromSource(motionEntry->source, AINPUT_SOURCE_CLASS_POINTER)) { |
| 1157 | // Only drop events that are focus-dispatched. |
| 1158 | dropReason = DropReason::BLOCKED; |
| 1159 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1160 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1161 | done = dispatchMotionLocked(currentTime, motionEntry, &dropReason, nextWakeupTime); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 1162 | if (done && mTracer) { |
| 1163 | ensureEventTraced(*motionEntry); |
| 1164 | mTracer->eventProcessingComplete(*motionEntry->traceTracker); |
| 1165 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1166 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1167 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1168 | |
| 1169 | case EventEntry::Type::SENSOR: { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1170 | std::shared_ptr<const SensorEntry> sensorEntry = |
| 1171 | std::static_pointer_cast<const SensorEntry>(mPendingEvent); |
Siarhei Vishniakou | e2404a1 | 2024-01-16 18:38:39 -0800 | [diff] [blame] | 1172 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1173 | // Sensor timestamps use SYSTEM_TIME_BOOTTIME time base, so we can't use |
| 1174 | // 'currentTime' here, get SYSTEM_TIME_BOOTTIME instead. |
| 1175 | nsecs_t bootTime = systemTime(SYSTEM_TIME_BOOTTIME); |
| 1176 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(bootTime, *sensorEntry)) { |
| 1177 | dropReason = DropReason::STALE; |
| 1178 | } |
| 1179 | dispatchSensorLocked(currentTime, sensorEntry, &dropReason, nextWakeupTime); |
| 1180 | done = true; |
| 1181 | break; |
| 1182 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1183 | } |
| 1184 | |
| 1185 | if (done) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1186 | if (dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1187 | dropInboundEventLocked(*mPendingEvent, dropReason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1188 | } |
Michael Wright | 3a98172 | 2015-06-10 15:26:13 +0100 | [diff] [blame] | 1189 | mLastDropReason = dropReason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1190 | |
| 1191 | releasePendingEventLocked(); |
Siarhei Vishniakou | 6950596 | 2023-12-28 12:07:04 -0800 | [diff] [blame] | 1192 | nextWakeupTime = LLONG_MIN; // force next poll to wake up immediately |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1193 | } |
| 1194 | } |
| 1195 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 1196 | bool InputDispatcher::isStaleEvent(nsecs_t currentTime, const EventEntry& entry) { |
Siarhei Vishniakou | a733311 | 2023-10-27 13:33:29 -0700 | [diff] [blame] | 1197 | return mPolicy.isStaleEvent(currentTime, entry.eventTime); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 1198 | } |
| 1199 | |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1200 | /** |
| 1201 | * Return true if the events preceding this incoming motion event should be dropped |
| 1202 | * Return false otherwise (the default behaviour) |
| 1203 | */ |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 1204 | bool InputDispatcher::shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) const { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1205 | const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN && |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 1206 | isFromSource(motionEntry.source, AINPUT_SOURCE_CLASS_POINTER); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1207 | |
| 1208 | // Optimize case where the current application is unresponsive and the user |
| 1209 | // decides to touch a window in a different application. |
| 1210 | // If the application takes too long to catch up then we drop all events preceding |
| 1211 | // the touch into the other window. |
| 1212 | if (isPointerDownEvent && mAwaitedFocusedApplication != nullptr) { |
Siarhei Vishniakou | 9306c38 | 2022-09-30 15:30:31 -0700 | [diff] [blame] | 1213 | const int32_t displayId = motionEntry.displayId; |
| 1214 | const auto [x, y] = resolveTouchedPosition(motionEntry); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1215 | const bool isStylus = isPointerFromStylus(motionEntry, /*pointerIndex=*/0); |
Siarhei Vishniakou | 9306c38 | 2022-09-30 15:30:31 -0700 | [diff] [blame] | 1216 | |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 1217 | sp<WindowInfoHandle> touchedWindowHandle = |
| 1218 | findTouchedWindowAtLocked(displayId, x, y, isStylus); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1219 | if (touchedWindowHandle != nullptr && |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1220 | touchedWindowHandle->getApplicationToken() != |
| 1221 | mAwaitedFocusedApplication->getApplicationToken()) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1222 | // User touched a different application than the one we are waiting on. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1223 | ALOGI("Pruning input queue because user touched a different application while waiting " |
| 1224 | "for %s", |
| 1225 | mAwaitedFocusedApplication->getName().c_str()); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1226 | return true; |
| 1227 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1228 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1229 | // Alternatively, maybe there's a spy window that could handle this event. |
| 1230 | const std::vector<sp<WindowInfoHandle>> touchedSpies = |
| 1231 | findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus); |
| 1232 | for (const auto& windowHandle : touchedSpies) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 1233 | const std::shared_ptr<Connection> connection = |
| 1234 | getConnectionLocked(windowHandle->getToken()); |
Siarhei Vishniakou | 34ed4d4 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1235 | if (connection != nullptr && connection->responsive) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1236 | // This spy window could take more input. Drop all events preceding this |
| 1237 | // 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] | 1238 | 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] | 1239 | "responsive spy window that may handle the event.", |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1240 | mAwaitedFocusedApplication->getName().c_str()); |
| 1241 | return true; |
| 1242 | } |
| 1243 | } |
| 1244 | } |
| 1245 | |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1246 | return false; |
| 1247 | } |
| 1248 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1249 | bool InputDispatcher::enqueueInboundEventLocked(std::unique_ptr<EventEntry> newEntry) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1250 | bool needWake = mInboundQueue.empty(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1251 | mInboundQueue.push_back(std::move(newEntry)); |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1252 | const EventEntry& entry = *(mInboundQueue.back()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1253 | traceInboundQueueLengthLocked(); |
| 1254 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1255 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1256 | case EventEntry::Type::KEY: { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1257 | LOG_ALWAYS_FATAL_IF((entry.policyFlags & POLICY_FLAG_TRUSTED) == 0, |
| 1258 | "Unexpected untrusted event."); |
Siarhei Vishniakou | e2404a1 | 2024-01-16 18:38:39 -0800 | [diff] [blame] | 1259 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1260 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 1261 | if (mTracer) { |
| 1262 | ensureEventTraced(keyEntry); |
| 1263 | } |
Siarhei Vishniakou | 6520a58 | 2023-10-27 21:53:45 -0700 | [diff] [blame] | 1264 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 1265 | // If a new up event comes in, and the pending event with same key code has been asked |
| 1266 | // to try again later because of the policy. We have to reset the intercept key wake up |
| 1267 | // time for it may have been handled in the policy and could be dropped. |
| 1268 | if (keyEntry.action == AKEY_EVENT_ACTION_UP && mPendingEvent && |
| 1269 | mPendingEvent->type == EventEntry::Type::KEY) { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1270 | const KeyEntry& pendingKey = static_cast<const KeyEntry&>(*mPendingEvent); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 1271 | if (pendingKey.keyCode == keyEntry.keyCode && |
| 1272 | pendingKey.interceptKeyResult == |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1273 | KeyEntry::InterceptKeyResult::TRY_AGAIN_LATER) { |
| 1274 | pendingKey.interceptKeyResult = KeyEntry::InterceptKeyResult::UNKNOWN; |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 1275 | pendingKey.interceptKeyWakeupTime = 0; |
| 1276 | needWake = true; |
| 1277 | } |
| 1278 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1279 | break; |
| 1280 | } |
| 1281 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1282 | case EventEntry::Type::MOTION: { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1283 | LOG_ALWAYS_FATAL_IF((entry.policyFlags & POLICY_FLAG_TRUSTED) == 0, |
| 1284 | "Unexpected untrusted event."); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 1285 | const auto& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1286 | if (mTracer) { |
| 1287 | ensureEventTraced(motionEntry); |
| 1288 | } |
| 1289 | if (shouldPruneInboundQueueLocked(motionEntry)) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1290 | mNextUnblockedEvent = mInboundQueue.back(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1291 | needWake = true; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1292 | } |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 1293 | |
| 1294 | const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN && |
| 1295 | isFromSource(motionEntry.source, AINPUT_SOURCE_CLASS_POINTER); |
| 1296 | if (isPointerDownEvent && mKeyIsWaitingForEventsTimeout) { |
| 1297 | // Prevent waiting too long for unprocessed events: if we have a pending key event, |
| 1298 | // and some other events have not yet been processed, the dispatcher will wait for |
| 1299 | // these events to be processed before dispatching the key event. This is because |
| 1300 | // the unprocessed events may cause the focus to change (for example, by launching a |
| 1301 | // new window or tapping a different window). To prevent waiting too long, we force |
| 1302 | // the key to be sent to the currently focused window when a new tap comes in. |
| 1303 | ALOGD("Received a new pointer down event, stop waiting for events to process and " |
| 1304 | "just send the pending key event to the currently focused window."); |
| 1305 | mKeyIsWaitingForEventsTimeout = now(); |
| 1306 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1307 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1308 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1309 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1310 | LOG_ALWAYS_FATAL("Focus events should be inserted using enqueueFocusEventLocked"); |
| 1311 | break; |
| 1312 | } |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1313 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1314 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1315 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1316 | case EventEntry::Type::SENSOR: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1317 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1318 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1319 | // nothing to do |
| 1320 | break; |
| 1321 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1322 | } |
| 1323 | |
| 1324 | return needWake; |
| 1325 | } |
| 1326 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1327 | void InputDispatcher::addRecentEventLocked(std::shared_ptr<const EventEntry> entry) { |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1328 | // Do not store sensor event in recent queue to avoid flooding the queue. |
| 1329 | if (entry->type != EventEntry::Type::SENSOR) { |
| 1330 | mRecentQueue.push_back(entry); |
| 1331 | } |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1332 | if (mRecentQueue.size() > RECENT_QUEUE_MAX_SIZE) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1333 | mRecentQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1334 | } |
| 1335 | } |
| 1336 | |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 1337 | sp<WindowInfoHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, float x, float y, |
| 1338 | bool isStylus, |
| 1339 | bool ignoreDragWindow) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1340 | // Traverse windows from front to back to find touched window. |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1341 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1342 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 1343 | if (ignoreDragWindow && haveSameToken(windowHandle, mDragState->dragWindow)) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1344 | continue; |
| 1345 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1346 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1347 | const WindowInfo& info = *windowHandle->getInfo(); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 1348 | if (!info.isSpy() && |
| 1349 | windowAcceptsTouchAt(info, displayId, x, y, isStylus, getTransformLocked(displayId))) { |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 1350 | return windowHandle; |
| 1351 | } |
| 1352 | } |
| 1353 | return nullptr; |
| 1354 | } |
| 1355 | |
| 1356 | std::vector<InputTarget> InputDispatcher::findOutsideTargetsLocked( |
Siarhei Vishniakou | 70f3d8c | 2023-09-19 15:36:52 -0700 | [diff] [blame] | 1357 | int32_t displayId, const sp<WindowInfoHandle>& touchedWindow, int32_t pointerId) const { |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 1358 | if (touchedWindow == nullptr) { |
| 1359 | return {}; |
| 1360 | } |
| 1361 | // Traverse windows from front to back until we encounter the touched window. |
| 1362 | std::vector<InputTarget> outsideTargets; |
| 1363 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
| 1364 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 1365 | if (windowHandle == touchedWindow) { |
| 1366 | // Stop iterating once we found a touched window. Any WATCH_OUTSIDE_TOUCH window |
| 1367 | // below the touched window will not get ACTION_OUTSIDE event. |
| 1368 | return outsideTargets; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1369 | } |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1370 | |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 1371 | const WindowInfo& info = *windowHandle->getInfo(); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1372 | if (info.inputConfig.test(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH)) { |
Siarhei Vishniakou | 70f3d8c | 2023-09-19 15:36:52 -0700 | [diff] [blame] | 1373 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
| 1374 | pointerIds.set(pointerId); |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 1375 | addPointerWindowTargetLocked(windowHandle, InputTarget::DispatchMode::OUTSIDE, |
| 1376 | ftl::Flags<InputTarget::Flags>(), pointerIds, |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 1377 | /*firstDownTimeInTarget=*/std::nullopt, outsideTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1378 | } |
| 1379 | } |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 1380 | return outsideTargets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1381 | } |
| 1382 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1383 | std::vector<sp<WindowInfoHandle>> InputDispatcher::findTouchedSpyWindowsAtLocked( |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 1384 | int32_t displayId, float x, float y, bool isStylus) const { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1385 | // Traverse windows from front to back and gather the touched spy windows. |
| 1386 | std::vector<sp<WindowInfoHandle>> spyWindows; |
| 1387 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
| 1388 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 1389 | const WindowInfo& info = *windowHandle->getInfo(); |
| 1390 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 1391 | if (!windowAcceptsTouchAt(info, displayId, x, y, isStylus, getTransformLocked(displayId))) { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1392 | continue; |
| 1393 | } |
| 1394 | if (!info.isSpy()) { |
| 1395 | // The first touched non-spy window was found, so return the spy windows touched so far. |
| 1396 | return spyWindows; |
| 1397 | } |
| 1398 | spyWindows.push_back(windowHandle); |
| 1399 | } |
| 1400 | return spyWindows; |
| 1401 | } |
| 1402 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1403 | void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1404 | const char* reason; |
| 1405 | switch (dropReason) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1406 | case DropReason::POLICY: |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 1407 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1408 | ALOGD("Dropped event because policy consumed it."); |
| 1409 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1410 | reason = "inbound event was dropped because the policy consumed it"; |
| 1411 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1412 | case DropReason::DISABLED: |
| 1413 | if (mLastDropReason != DropReason::DISABLED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1414 | ALOGI("Dropped event because input dispatch is disabled."); |
| 1415 | } |
| 1416 | reason = "inbound event was dropped because input dispatch is disabled"; |
| 1417 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1418 | case DropReason::BLOCKED: |
Siarhei Vishniakou | 99e407b | 2023-12-26 18:09:32 -0800 | [diff] [blame] | 1419 | LOG(INFO) << "Dropping because the current application is not responding and the user " |
| 1420 | "has started interacting with a different application: " |
| 1421 | << entry.getDescription(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1422 | reason = "inbound event was dropped because the current application is not responding " |
| 1423 | "and the user has started interacting with a different application"; |
| 1424 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1425 | case DropReason::STALE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1426 | ALOGI("Dropped event because it is stale."); |
| 1427 | reason = "inbound event was dropped because it is stale"; |
| 1428 | break; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1429 | case DropReason::NO_POINTER_CAPTURE: |
| 1430 | ALOGI("Dropped event because there is no window with Pointer Capture."); |
| 1431 | reason = "inbound event was dropped because there is no window with Pointer Capture"; |
| 1432 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1433 | case DropReason::NOT_DROPPED: { |
| 1434 | LOG_ALWAYS_FATAL("Should not be dropping a NOT_DROPPED event"); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1435 | return; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1436 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1437 | } |
| 1438 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1439 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1440 | case EventEntry::Type::KEY: { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1441 | CancelationOptions options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, reason); |
Hu Guo | 3cfa738 | 2023-11-15 09:50:04 +0000 | [diff] [blame] | 1442 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 1443 | options.displayId = keyEntry.displayId; |
| 1444 | options.deviceId = keyEntry.deviceId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1445 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1446 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1447 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1448 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1449 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1450 | if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1451 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, reason); |
Hu Guo | 3cfa738 | 2023-11-15 09:50:04 +0000 | [diff] [blame] | 1452 | options.displayId = motionEntry.displayId; |
| 1453 | options.deviceId = motionEntry.deviceId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1454 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1455 | } else { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1456 | CancelationOptions options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, |
| 1457 | reason); |
Hu Guo | 3cfa738 | 2023-11-15 09:50:04 +0000 | [diff] [blame] | 1458 | options.displayId = motionEntry.displayId; |
| 1459 | options.deviceId = motionEntry.deviceId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1460 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1461 | } |
| 1462 | break; |
| 1463 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1464 | case EventEntry::Type::SENSOR: { |
| 1465 | break; |
| 1466 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1467 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1468 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1469 | break; |
| 1470 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1471 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1472 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1473 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 1474 | case EventEntry::Type::DEVICE_RESET: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1475 | 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] | 1476 | break; |
| 1477 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1478 | } |
| 1479 | } |
| 1480 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1481 | bool InputDispatcher::haveCommandsLocked() const { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1482 | return !mCommandQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1483 | } |
| 1484 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1485 | bool InputDispatcher::runCommandsLockedInterruptable() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1486 | if (mCommandQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1487 | return false; |
| 1488 | } |
| 1489 | |
| 1490 | do { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1491 | auto command = std::move(mCommandQueue.front()); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1492 | mCommandQueue.pop_front(); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1493 | // Commands are run with the lock held, but may release and re-acquire the lock from within. |
| 1494 | command(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1495 | } while (!mCommandQueue.empty()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1496 | return true; |
| 1497 | } |
| 1498 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1499 | void InputDispatcher::postCommandLocked(Command&& command) { |
| 1500 | mCommandQueue.push_back(command); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1501 | } |
| 1502 | |
| 1503 | void InputDispatcher::drainInboundQueueLocked() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1504 | while (!mInboundQueue.empty()) { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1505 | std::shared_ptr<const EventEntry> entry = mInboundQueue.front(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1506 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1507 | releaseInboundEventLocked(entry); |
| 1508 | } |
| 1509 | traceInboundQueueLengthLocked(); |
| 1510 | } |
| 1511 | |
| 1512 | void InputDispatcher::releasePendingEventLocked() { |
| 1513 | if (mPendingEvent) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1514 | releaseInboundEventLocked(mPendingEvent); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1515 | mPendingEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1516 | } |
| 1517 | } |
| 1518 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1519 | void InputDispatcher::releaseInboundEventLocked(std::shared_ptr<const EventEntry> entry) { |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 1520 | const std::shared_ptr<InjectionState>& injectionState = entry->injectionState; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1521 | if (injectionState && injectionState->injectionResult == InputEventInjectionResult::PENDING) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1522 | if (DEBUG_DISPATCH_CYCLE) { |
| 1523 | ALOGD("Injected inbound event was dropped."); |
| 1524 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1525 | setInjectionResult(*entry, InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1526 | } |
| 1527 | if (entry == mNextUnblockedEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1528 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1529 | } |
| 1530 | addRecentEventLocked(entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1531 | } |
| 1532 | |
| 1533 | void InputDispatcher::resetKeyRepeatLocked() { |
| 1534 | if (mKeyRepeatState.lastKeyEntry) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1535 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1536 | } |
| 1537 | } |
| 1538 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1539 | std::shared_ptr<KeyEntry> InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1540 | std::shared_ptr<const KeyEntry> entry = mKeyRepeatState.lastKeyEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1541 | |
Michael Wright | 2e73295 | 2014-09-24 13:26:59 -0700 | [diff] [blame] | 1542 | uint32_t policyFlags = entry->policyFlags & |
| 1543 | (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1544 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1545 | std::shared_ptr<KeyEntry> newEntry = |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 1546 | std::make_unique<KeyEntry>(mIdGenerator.nextId(), /*injectionState=*/nullptr, |
| 1547 | currentTime, entry->deviceId, entry->source, |
| 1548 | entry->displayId, policyFlags, entry->action, entry->flags, |
| 1549 | entry->keyCode, entry->scanCode, entry->metaState, |
| 1550 | entry->repeatCount + 1, entry->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1551 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1552 | newEntry->syntheticRepeat = true; |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 1553 | if (mTracer) { |
| 1554 | newEntry->traceTracker = mTracer->traceInboundEvent(*newEntry); |
| 1555 | } |
| 1556 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1557 | mKeyRepeatState.lastKeyEntry = newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1558 | mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1559 | return newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1560 | } |
| 1561 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1562 | bool InputDispatcher::dispatchConfigurationChangedLocked(nsecs_t currentTime, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1563 | const ConfigurationChangedEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1564 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1565 | ALOGD("dispatchConfigurationChanged - eventTime=%" PRId64, entry.eventTime); |
| 1566 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1567 | |
| 1568 | // Reset key repeating in case a keyboard device was added or removed or something. |
| 1569 | resetKeyRepeatLocked(); |
| 1570 | |
| 1571 | // 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] | 1572 | auto command = [this, eventTime = entry.eventTime]() REQUIRES(mLock) { |
| 1573 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 1574 | mPolicy.notifyConfigurationChanged(eventTime); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1575 | }; |
| 1576 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1577 | return true; |
| 1578 | } |
| 1579 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1580 | bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime, |
| 1581 | const DeviceResetEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1582 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1583 | ALOGD("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry.eventTime, |
| 1584 | entry.deviceId); |
| 1585 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1586 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 1587 | // Reset key repeating in case a keyboard device was disabled or enabled. |
| 1588 | if (mKeyRepeatState.lastKeyEntry && mKeyRepeatState.lastKeyEntry->deviceId == entry.deviceId) { |
| 1589 | resetKeyRepeatLocked(); |
| 1590 | } |
| 1591 | |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1592 | CancelationOptions options(CancelationOptions::Mode::CANCEL_ALL_EVENTS, "device was reset"); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1593 | options.deviceId = entry.deviceId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1594 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 1595 | |
| 1596 | // Remove all active pointers from this device |
| 1597 | for (auto& [_, touchState] : mTouchStatesByDisplay) { |
| 1598 | touchState.removeAllPointersForDevice(entry.deviceId); |
| 1599 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1600 | return true; |
| 1601 | } |
| 1602 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1603 | void InputDispatcher::enqueueFocusEventLocked(const sp<IBinder>& windowToken, bool hasFocus, |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1604 | const std::string& reason) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1605 | if (mPendingEvent != nullptr) { |
| 1606 | // Move the pending event to the front of the queue. This will give the chance |
| 1607 | // for the pending event to get dispatched to the newly focused window |
| 1608 | mInboundQueue.push_front(mPendingEvent); |
| 1609 | mPendingEvent = nullptr; |
| 1610 | } |
| 1611 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1612 | std::unique_ptr<FocusEntry> focusEntry = |
| 1613 | std::make_unique<FocusEntry>(mIdGenerator.nextId(), now(), windowToken, hasFocus, |
| 1614 | reason); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1615 | |
| 1616 | // This event should go to the front of the queue, but behind all other focus events |
| 1617 | // Find the last focus event, and insert right after it |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1618 | auto it = std::find_if(mInboundQueue.rbegin(), mInboundQueue.rend(), |
| 1619 | [](const std::shared_ptr<const EventEntry>& event) { |
| 1620 | return event->type == EventEntry::Type::FOCUS; |
| 1621 | }); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1622 | |
| 1623 | // 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] | 1624 | mInboundQueue.insert(it.base(), std::move(focusEntry)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1625 | } |
| 1626 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1627 | void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime, |
| 1628 | std::shared_ptr<const FocusEntry> entry) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 1629 | std::shared_ptr<Connection> connection = getConnectionLocked(entry->connectionToken); |
| 1630 | if (connection == nullptr) { |
| 1631 | return; // Connection has gone away |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1632 | } |
| 1633 | InputTarget target; |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 1634 | target.connection = connection; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1635 | entry->dispatchInProgress = true; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1636 | std::string message = std::string("Focus ") + (entry->hasFocus ? "entering " : "leaving ") + |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 1637 | connection->getInputChannelName(); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 1638 | std::string reason = std::string("reason=").append(entry->reason); |
| 1639 | android_log_event_list(LOGTAG_INPUT_FOCUS) << message << reason << LOG_ID_EVENTS; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1640 | dispatchEventLocked(currentTime, entry, {target}); |
| 1641 | } |
| 1642 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1643 | void InputDispatcher::dispatchPointerCaptureChangedLocked( |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1644 | nsecs_t currentTime, const std::shared_ptr<const PointerCaptureChangedEntry>& entry, |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1645 | DropReason& dropReason) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1646 | dropReason = DropReason::NOT_DROPPED; |
| 1647 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1648 | const bool haveWindowWithPointerCapture = mWindowTokenWithPointerCapture != nullptr; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1649 | sp<IBinder> token; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1650 | |
| 1651 | if (entry->pointerCaptureRequest.enable) { |
| 1652 | // Enable Pointer Capture. |
| 1653 | if (haveWindowWithPointerCapture && |
| 1654 | (entry->pointerCaptureRequest == mCurrentPointerCaptureRequest)) { |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 1655 | // This can happen if pointer capture is disabled and re-enabled before we notify the |
| 1656 | // app of the state change, so there is no need to notify the app. |
| 1657 | ALOGI("Skipping dispatch of Pointer Capture being enabled: no state change."); |
| 1658 | return; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1659 | } |
| 1660 | if (!mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1661 | // This can happen if a window requests capture and immediately releases capture. |
| 1662 | ALOGW("No window requested Pointer Capture."); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1663 | dropReason = DropReason::NO_POINTER_CAPTURE; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1664 | return; |
| 1665 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1666 | if (entry->pointerCaptureRequest.seq != mCurrentPointerCaptureRequest.seq) { |
| 1667 | ALOGI("Skipping dispatch of Pointer Capture being enabled: sequence number mismatch."); |
| 1668 | return; |
| 1669 | } |
| 1670 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1671 | token = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1672 | LOG_ALWAYS_FATAL_IF(!token, "Cannot find focused window for Pointer Capture."); |
| 1673 | mWindowTokenWithPointerCapture = token; |
| 1674 | } else { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1675 | // Disable Pointer Capture. |
| 1676 | // We do not check if the sequence number matches for requests to disable Pointer Capture |
| 1677 | // for two reasons: |
| 1678 | // 1. Pointer Capture can be disabled by a focus change, which means we can get two entries |
| 1679 | // to disable capture with the same sequence number: one generated by |
| 1680 | // disablePointerCaptureForcedLocked() and another as an acknowledgement of Pointer |
| 1681 | // Capture being disabled in InputReader. |
| 1682 | // 2. We respect any request to disable Pointer Capture generated by InputReader, since the |
| 1683 | // actual Pointer Capture state that affects events being generated by input devices is |
| 1684 | // in InputReader. |
| 1685 | if (!haveWindowWithPointerCapture) { |
| 1686 | // Pointer capture was already forcefully disabled because of focus change. |
| 1687 | dropReason = DropReason::NOT_DROPPED; |
| 1688 | return; |
| 1689 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1690 | token = mWindowTokenWithPointerCapture; |
| 1691 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1692 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1693 | setPointerCaptureLocked(false); |
| 1694 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1695 | } |
| 1696 | |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 1697 | auto connection = getConnectionLocked(token); |
| 1698 | if (connection == nullptr) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1699 | // Window has gone away, clean up Pointer Capture state. |
| 1700 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1701 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1702 | setPointerCaptureLocked(false); |
| 1703 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1704 | return; |
| 1705 | } |
| 1706 | InputTarget target; |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 1707 | target.connection = connection; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1708 | entry->dispatchInProgress = true; |
| 1709 | dispatchEventLocked(currentTime, entry, {target}); |
| 1710 | |
| 1711 | dropReason = DropReason::NOT_DROPPED; |
| 1712 | } |
| 1713 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1714 | void InputDispatcher::dispatchTouchModeChangeLocked( |
| 1715 | nsecs_t currentTime, const std::shared_ptr<const TouchModeEntry>& entry) { |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1716 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 1717 | getWindowHandlesLocked(entry->displayId); |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1718 | if (windowHandles.empty()) { |
| 1719 | return; |
| 1720 | } |
| 1721 | const std::vector<InputTarget> inputTargets = |
| 1722 | getInputTargetsFromWindowHandlesLocked(windowHandles); |
| 1723 | if (inputTargets.empty()) { |
| 1724 | return; |
| 1725 | } |
| 1726 | entry->dispatchInProgress = true; |
| 1727 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1728 | } |
| 1729 | |
| 1730 | std::vector<InputTarget> InputDispatcher::getInputTargetsFromWindowHandlesLocked( |
| 1731 | const std::vector<sp<WindowInfoHandle>>& windowHandles) const { |
| 1732 | std::vector<InputTarget> inputTargets; |
| 1733 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1734 | const sp<IBinder>& token = handle->getToken(); |
| 1735 | if (token == nullptr) { |
| 1736 | continue; |
| 1737 | } |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 1738 | std::shared_ptr<Connection> connection = getConnectionLocked(token); |
| 1739 | if (connection == nullptr) { |
| 1740 | continue; // Connection has gone away |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1741 | } |
| 1742 | InputTarget target; |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 1743 | target.connection = connection; |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1744 | inputTargets.push_back(target); |
| 1745 | } |
| 1746 | return inputTargets; |
| 1747 | } |
| 1748 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1749 | bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, std::shared_ptr<const KeyEntry> entry, |
Siarhei Vishniakou | 6950596 | 2023-12-28 12:07:04 -0800 | [diff] [blame] | 1750 | DropReason* dropReason, nsecs_t& nextWakeupTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1751 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1752 | if (!entry->dispatchInProgress) { |
| 1753 | if (entry->repeatCount == 0 && entry->action == AKEY_EVENT_ACTION_DOWN && |
| 1754 | (entry->policyFlags & POLICY_FLAG_TRUSTED) && |
| 1755 | (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { |
| 1756 | if (mKeyRepeatState.lastKeyEntry && |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1757 | mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode && |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1758 | // We have seen two identical key downs in a row which indicates that the device |
| 1759 | // driver is automatically generating key repeats itself. We take note of the |
| 1760 | // repeat here, but we disable our own next key repeat timer since it is clear that |
| 1761 | // we will not need to synthesize key repeats ourselves. |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1762 | mKeyRepeatState.lastKeyEntry->deviceId == entry->deviceId) { |
| 1763 | // Make sure we don't get key down from a different device. If a different |
| 1764 | // device Id has same key pressed down, the new device Id will replace the |
| 1765 | // current one to hold the key repeat with repeat count reset. |
| 1766 | // In the future when got a KEY_UP on the device id, drop it and do not |
| 1767 | // stop the key repeat on current device. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1768 | entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; |
| 1769 | resetKeyRepeatLocked(); |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 1770 | mKeyRepeatState.nextRepeatTime = LLONG_MAX; // don't generate repeats ourselves |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1771 | } else { |
| 1772 | // Not a repeat. Save key down state in case we do see a repeat later. |
| 1773 | resetKeyRepeatLocked(); |
| 1774 | mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; |
| 1775 | } |
| 1776 | mKeyRepeatState.lastKeyEntry = entry; |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1777 | } else if (entry->action == AKEY_EVENT_ACTION_UP && mKeyRepeatState.lastKeyEntry && |
| 1778 | mKeyRepeatState.lastKeyEntry->deviceId != entry->deviceId) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1779 | // 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] | 1780 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1781 | ALOGD("deviceId=%d got KEY_UP as stale", entry->deviceId); |
| 1782 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1783 | } else if (!entry->syntheticRepeat) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1784 | resetKeyRepeatLocked(); |
| 1785 | } |
| 1786 | |
| 1787 | if (entry->repeatCount == 1) { |
| 1788 | entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; |
| 1789 | } else { |
| 1790 | entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; |
| 1791 | } |
| 1792 | |
| 1793 | entry->dispatchInProgress = true; |
| 1794 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1795 | logOutboundKeyDetails("dispatchKey - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1796 | } |
| 1797 | |
| 1798 | // 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] | 1799 | if (entry->interceptKeyResult == KeyEntry::InterceptKeyResult::TRY_AGAIN_LATER) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1800 | if (currentTime < entry->interceptKeyWakeupTime) { |
Siarhei Vishniakou | 6950596 | 2023-12-28 12:07:04 -0800 | [diff] [blame] | 1801 | nextWakeupTime = std::min(nextWakeupTime, entry->interceptKeyWakeupTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1802 | return false; // wait until next wakeup |
| 1803 | } |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1804 | entry->interceptKeyResult = KeyEntry::InterceptKeyResult::UNKNOWN; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1805 | entry->interceptKeyWakeupTime = 0; |
| 1806 | } |
| 1807 | |
| 1808 | // Give the policy a chance to intercept the key. |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1809 | if (entry->interceptKeyResult == KeyEntry::InterceptKeyResult::UNKNOWN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1810 | if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1811 | sp<IBinder> focusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1812 | mFocusResolver.getFocusedWindowToken(getTargetDisplayId(*entry)); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1813 | |
| 1814 | auto command = [this, focusedWindowToken, entry]() REQUIRES(mLock) { |
| 1815 | doInterceptKeyBeforeDispatchingCommand(focusedWindowToken, *entry); |
| 1816 | }; |
| 1817 | postCommandLocked(std::move(command)); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1818 | // Poke user activity for keys not passed to user |
| 1819 | pokeUserActivityLocked(*entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1820 | return false; // wait for the command to run |
| 1821 | } else { |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1822 | entry->interceptKeyResult = KeyEntry::InterceptKeyResult::CONTINUE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1823 | } |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1824 | } else if (entry->interceptKeyResult == KeyEntry::InterceptKeyResult::SKIP) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1825 | if (*dropReason == DropReason::NOT_DROPPED) { |
| 1826 | *dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1827 | } |
| 1828 | } |
| 1829 | |
| 1830 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1831 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1832 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1833 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1834 | : InputEventInjectionResult::FAILED); |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1835 | mReporter->reportDroppedKey(entry->id); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1836 | // Poke user activity for undispatched keys |
| 1837 | pokeUserActivityLocked(*entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1838 | return true; |
| 1839 | } |
| 1840 | |
| 1841 | // Identify targets. |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1842 | InputEventInjectionResult injectionResult; |
| 1843 | sp<WindowInfoHandle> focusedWindow = |
| 1844 | findFocusedWindowTargetLocked(currentTime, *entry, nextWakeupTime, |
| 1845 | /*byref*/ injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1846 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1847 | return false; |
| 1848 | } |
| 1849 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1850 | setInjectionResult(*entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1851 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1852 | return true; |
| 1853 | } |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1854 | LOG_ALWAYS_FATAL_IF(focusedWindow == nullptr); |
| 1855 | |
| 1856 | std::vector<InputTarget> inputTargets; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 1857 | addWindowTargetLocked(focusedWindow, InputTarget::DispatchMode::AS_IS, |
| 1858 | InputTarget::Flags::FOREGROUND, getDownTime(*entry), inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1859 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1860 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1861 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1862 | |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 1863 | if (mTracer) { |
| 1864 | ensureEventTraced(*entry); |
| 1865 | for (const auto& target : inputTargets) { |
| 1866 | mTracer->dispatchToTargetHint(*entry->traceTracker, target); |
| 1867 | } |
| 1868 | } |
| 1869 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1870 | // Dispatch the key. |
| 1871 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1872 | return true; |
| 1873 | } |
| 1874 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1875 | void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1876 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1877 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 ", " |
| 1878 | "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, " |
| 1879 | "metaState=0x%x, repeatCount=%d, downTime=%" PRId64, |
| 1880 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, |
| 1881 | entry.policyFlags, entry.action, entry.flags, entry.keyCode, entry.scanCode, |
| 1882 | entry.metaState, entry.repeatCount, entry.downTime); |
| 1883 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1884 | } |
| 1885 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1886 | void InputDispatcher::dispatchSensorLocked(nsecs_t currentTime, |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1887 | const std::shared_ptr<const SensorEntry>& entry, |
Siarhei Vishniakou | 6950596 | 2023-12-28 12:07:04 -0800 | [diff] [blame] | 1888 | DropReason* dropReason, nsecs_t& nextWakeupTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1889 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1890 | ALOGD("notifySensorEvent eventTime=%" PRId64 ", hwTimestamp=%" PRId64 ", deviceId=%d, " |
| 1891 | "source=0x%x, sensorType=%s", |
| 1892 | entry->eventTime, entry->hwTimestamp, entry->deviceId, entry->source, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1893 | ftl::enum_string(entry->sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1894 | } |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1895 | auto command = [this, entry]() REQUIRES(mLock) { |
| 1896 | scoped_unlock unlock(mLock); |
| 1897 | |
| 1898 | if (entry->accuracyChanged) { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 1899 | mPolicy.notifySensorAccuracy(entry->deviceId, entry->sensorType, entry->accuracy); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1900 | } |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 1901 | mPolicy.notifySensorEvent(entry->deviceId, entry->sensorType, entry->accuracy, |
| 1902 | entry->hwTimestamp, entry->values); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1903 | }; |
| 1904 | postCommandLocked(std::move(command)); |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1905 | } |
| 1906 | |
| 1907 | bool InputDispatcher::flushSensor(int deviceId, InputDeviceSensorType sensorType) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1908 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1909 | ALOGD("flushSensor deviceId=%d, sensorType=%s", deviceId, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1910 | ftl::enum_string(sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1911 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1912 | { // acquire lock |
| 1913 | std::scoped_lock _l(mLock); |
| 1914 | |
| 1915 | for (auto it = mInboundQueue.begin(); it != mInboundQueue.end(); it++) { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1916 | std::shared_ptr<const EventEntry> entry = *it; |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1917 | if (entry->type == EventEntry::Type::SENSOR) { |
| 1918 | it = mInboundQueue.erase(it); |
| 1919 | releaseInboundEventLocked(entry); |
| 1920 | } |
| 1921 | } |
| 1922 | } |
| 1923 | return true; |
| 1924 | } |
| 1925 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1926 | bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, |
| 1927 | std::shared_ptr<const MotionEntry> entry, |
Siarhei Vishniakou | 6950596 | 2023-12-28 12:07:04 -0800 | [diff] [blame] | 1928 | DropReason* dropReason, nsecs_t& nextWakeupTime) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1929 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1930 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1931 | if (!entry->dispatchInProgress) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1932 | entry->dispatchInProgress = true; |
| 1933 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1934 | logOutboundMotionDetails("dispatchMotion - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1935 | } |
| 1936 | |
| 1937 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1938 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1939 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1940 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1941 | : InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1942 | return true; |
| 1943 | } |
| 1944 | |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 1945 | const bool isPointerEvent = isFromSource(entry->source, AINPUT_SOURCE_CLASS_POINTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1946 | |
| 1947 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1948 | std::vector<InputTarget> inputTargets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1949 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1950 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1951 | if (isPointerEvent) { |
| 1952 | // Pointer event. (eg. touchscreen) |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 1953 | |
| 1954 | if (mDragState && |
| 1955 | (entry->action & AMOTION_EVENT_ACTION_MASK) == AMOTION_EVENT_ACTION_POINTER_DOWN) { |
| 1956 | // If drag and drop ongoing and pointer down occur: pilfer drag window pointers |
| 1957 | pilferPointersLocked(mDragState->dragWindow->getToken()); |
| 1958 | } |
| 1959 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1960 | inputTargets = |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 1961 | findTouchedWindowTargetsLocked(currentTime, *entry, /*byref*/ injectionResult); |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 1962 | LOG_ALWAYS_FATAL_IF(injectionResult != InputEventInjectionResult::SUCCEEDED && |
| 1963 | !inputTargets.empty()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1964 | } else { |
| 1965 | // Non touch event. (eg. trackball) |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1966 | sp<WindowInfoHandle> focusedWindow = |
| 1967 | findFocusedWindowTargetLocked(currentTime, *entry, nextWakeupTime, injectionResult); |
| 1968 | if (injectionResult == InputEventInjectionResult::SUCCEEDED) { |
| 1969 | LOG_ALWAYS_FATAL_IF(focusedWindow == nullptr); |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 1970 | addWindowTargetLocked(focusedWindow, InputTarget::DispatchMode::AS_IS, |
| 1971 | InputTarget::Flags::FOREGROUND, getDownTime(*entry), |
| 1972 | inputTargets); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1973 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1974 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1975 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1976 | return false; |
| 1977 | } |
| 1978 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1979 | setInjectionResult(*entry, injectionResult); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1980 | if (injectionResult == InputEventInjectionResult::TARGET_MISMATCH) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1981 | return true; |
| 1982 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1983 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1984 | CancelationOptions::Mode mode( |
| 1985 | isPointerEvent ? CancelationOptions::Mode::CANCEL_POINTER_EVENTS |
| 1986 | : CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS); |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1987 | CancelationOptions options(mode, "input event injection failed"); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 1988 | options.displayId = entry->displayId; |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1989 | synthesizeCancelationEventsForMonitorsLocked(options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1990 | return true; |
| 1991 | } |
| 1992 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1993 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1994 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1995 | |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 1996 | if (mTracer) { |
| 1997 | ensureEventTraced(*entry); |
| 1998 | for (const auto& target : inputTargets) { |
| 1999 | mTracer->dispatchToTargetHint(*entry->traceTracker, target); |
| 2000 | } |
| 2001 | } |
| 2002 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2003 | // Dispatch the motion. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2004 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 2005 | return true; |
| 2006 | } |
| 2007 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2008 | void InputDispatcher::enqueueDragEventLocked(const sp<WindowInfoHandle>& windowHandle, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2009 | bool isExiting, const int32_t rawX, |
| 2010 | const int32_t rawY) { |
| 2011 | const vec2 xy = windowHandle->getInfo()->transform.transform(vec2(rawX, rawY)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2012 | std::unique_ptr<DragEntry> dragEntry = |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2013 | std::make_unique<DragEntry>(mIdGenerator.nextId(), now(), windowHandle->getToken(), |
| 2014 | isExiting, xy.x, xy.y); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2015 | |
| 2016 | enqueueInboundEventLocked(std::move(dragEntry)); |
| 2017 | } |
| 2018 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 2019 | void InputDispatcher::dispatchDragLocked(nsecs_t currentTime, |
| 2020 | std::shared_ptr<const DragEntry> entry) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 2021 | std::shared_ptr<Connection> connection = getConnectionLocked(entry->connectionToken); |
| 2022 | if (connection == nullptr) { |
| 2023 | return; // Connection has gone away |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2024 | } |
| 2025 | InputTarget target; |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 2026 | target.connection = connection; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2027 | entry->dispatchInProgress = true; |
| 2028 | dispatchEventLocked(currentTime, entry, {target}); |
| 2029 | } |
| 2030 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2031 | void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2032 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 2033 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=%s, displayId=%" PRId32 |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2034 | ", policyFlags=0x%x, " |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2035 | "action=%s, actionButton=0x%x, flags=0x%x, " |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2036 | "metaState=0x%x, buttonState=0x%x," |
| 2037 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64, |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 2038 | prefix, entry.eventTime, entry.deviceId, |
| 2039 | inputEventSourceToString(entry.source).c_str(), entry.displayId, entry.policyFlags, |
| 2040 | MotionEvent::actionToString(entry.action).c_str(), entry.actionButton, entry.flags, |
| 2041 | entry.metaState, entry.buttonState, entry.edgeFlags, entry.xPrecision, |
| 2042 | entry.yPrecision, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2043 | |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 2044 | for (uint32_t i = 0; i < entry.getPointerCount(); i++) { |
Siarhei Vishniakou | 09a8fe4 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2045 | ALOGD(" Pointer %d: id=%d, toolType=%s, " |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2046 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 2047 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 2048 | "orientation=%f", |
Siarhei Vishniakou | 09a8fe4 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2049 | i, entry.pointerProperties[i].id, |
| 2050 | ftl::enum_string(entry.pointerProperties[i].toolType).c_str(), |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2051 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 2052 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 2053 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 2054 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 2055 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 2056 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 2057 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 2058 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 2059 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| 2060 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2061 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2062 | } |
| 2063 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2064 | void InputDispatcher::dispatchEventLocked(nsecs_t currentTime, |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 2065 | std::shared_ptr<const EventEntry> eventEntry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2066 | const std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2067 | ATRACE_CALL(); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2068 | if (DEBUG_DISPATCH_CYCLE) { |
| 2069 | ALOGD("dispatchEventToCurrentInputTargets"); |
| 2070 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2071 | |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 2072 | processInteractionsLocked(*eventEntry, inputTargets); |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 2073 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2074 | ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true |
| 2075 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2076 | pokeUserActivityLocked(*eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2077 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2078 | for (const InputTarget& inputTarget : inputTargets) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 2079 | std::shared_ptr<Connection> connection = inputTarget.connection; |
| 2080 | prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2081 | } |
| 2082 | } |
| 2083 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 2084 | void InputDispatcher::cancelEventsForAnrLocked(const std::shared_ptr<Connection>& connection) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2085 | // We will not be breaking any connections here, even if the policy wants us to abort dispatch. |
| 2086 | // If the policy decides to close the app, we will get a channel removal event via |
| 2087 | // unregisterInputChannel, and will clean up the connection that way. We are already not |
| 2088 | // sending new pointers to the connection when it blocked, but focused events will continue to |
| 2089 | // pile up. |
| 2090 | ALOGW("Canceling events for %s because it is unresponsive", |
Siarhei Vishniakou | 10bfdc9 | 2024-02-01 11:12:57 -0800 | [diff] [blame] | 2091 | connection->getInputChannelName().c_str()); |
Prabir Pradhan | fc36472 | 2024-02-08 17:51:20 +0000 | [diff] [blame] | 2092 | if (connection->status != Connection::Status::NORMAL) { |
| 2093 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2094 | } |
Prabir Pradhan | fc36472 | 2024-02-08 17:51:20 +0000 | [diff] [blame] | 2095 | CancelationOptions options(CancelationOptions::Mode::CANCEL_ALL_EVENTS, |
| 2096 | "application not responding"); |
| 2097 | |
| 2098 | sp<WindowInfoHandle> windowHandle; |
| 2099 | if (!connection->monitor) { |
| 2100 | windowHandle = getWindowHandleLocked(connection->getToken()); |
| 2101 | if (windowHandle == nullptr) { |
| 2102 | // The window that is receiving this ANR was removed, so there is no need to generate |
| 2103 | // cancellations, because the cancellations would have already been generated when |
| 2104 | // the window was removed. |
| 2105 | return; |
| 2106 | } |
| 2107 | } |
| 2108 | synthesizeCancelationEventsForConnectionLocked(connection, options, windowHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2109 | } |
| 2110 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2111 | void InputDispatcher::resetNoFocusedWindowTimeoutLocked() { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2112 | if (DEBUG_FOCUS) { |
| 2113 | ALOGD("Resetting ANR timeouts."); |
| 2114 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2115 | |
| 2116 | // Reset input target wait timeout. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2117 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2118 | mAwaitedFocusedApplication.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2119 | } |
| 2120 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2121 | /** |
| 2122 | * Get the display id that the given event should go to. If this event specifies a valid display id, |
| 2123 | * then it should be dispatched to that display. Otherwise, the event goes to the focused display. |
| 2124 | * Focused display is the display that the user most recently interacted with. |
| 2125 | */ |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2126 | int32_t InputDispatcher::getTargetDisplayId(const EventEntry& entry) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2127 | int32_t displayId; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2128 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2129 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2130 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 2131 | displayId = keyEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2132 | break; |
| 2133 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2134 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2135 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 2136 | displayId = motionEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2137 | break; |
| 2138 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 2139 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 2140 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2141 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2142 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2143 | case EventEntry::Type::DEVICE_RESET: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2144 | case EventEntry::Type::SENSOR: |
| 2145 | case EventEntry::Type::DRAG: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2146 | 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] | 2147 | return ADISPLAY_ID_NONE; |
| 2148 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2149 | } |
| 2150 | return displayId == ADISPLAY_ID_NONE ? mFocusedDisplayId : displayId; |
| 2151 | } |
| 2152 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2153 | bool InputDispatcher::shouldWaitToSendKeyLocked(nsecs_t currentTime, |
| 2154 | const char* focusedWindowName) { |
| 2155 | if (mAnrTracker.empty()) { |
| 2156 | // already processed all events that we waited for |
| 2157 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 2158 | return false; |
| 2159 | } |
| 2160 | |
| 2161 | if (!mKeyIsWaitingForEventsTimeout.has_value()) { |
| 2162 | // Start the timer |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 2163 | // 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] | 2164 | mKeyIsWaitingForEventsTimeout = currentTime + |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 2165 | std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 2166 | mPolicy.getKeyWaitingForEventsTimeout()) |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 2167 | .count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2168 | return true; |
| 2169 | } |
| 2170 | |
| 2171 | // We still have pending events, and already started the timer |
| 2172 | if (currentTime < *mKeyIsWaitingForEventsTimeout) { |
| 2173 | return true; // Still waiting |
| 2174 | } |
| 2175 | |
| 2176 | // Waited too long, and some connection still hasn't processed all motions |
| 2177 | // Just send the key to the focused window |
| 2178 | ALOGW("Dispatching key to %s even though there are other unprocessed events", |
| 2179 | focusedWindowName); |
| 2180 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 2181 | return false; |
| 2182 | } |
| 2183 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2184 | sp<WindowInfoHandle> InputDispatcher::findFocusedWindowTargetLocked( |
Siarhei Vishniakou | 6950596 | 2023-12-28 12:07:04 -0800 | [diff] [blame] | 2185 | nsecs_t currentTime, const EventEntry& entry, nsecs_t& nextWakeupTime, |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2186 | InputEventInjectionResult& outInjectionResult) { |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2187 | outInjectionResult = InputEventInjectionResult::FAILED; // Default result |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2188 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2189 | int32_t displayId = getTargetDisplayId(entry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2190 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2191 | std::shared_ptr<InputApplicationHandle> focusedApplicationHandle = |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2192 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 2193 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2194 | // If there is no currently focused window and no focused application |
| 2195 | // then drop the event. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2196 | if (focusedWindowHandle == nullptr && focusedApplicationHandle == nullptr) { |
| 2197 | ALOGI("Dropping %s event because there is no focused window or focused application in " |
| 2198 | "display %" PRId32 ".", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2199 | ftl::enum_string(entry.type).c_str(), displayId); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2200 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2201 | } |
| 2202 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2203 | // Drop key events if requested by input feature |
| 2204 | if (focusedWindowHandle != nullptr && shouldDropInput(entry, focusedWindowHandle)) { |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2205 | return nullptr; |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2206 | } |
| 2207 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2208 | // Compatibility behavior: raise ANR if there is a focused application, but no focused window. |
| 2209 | // Only start counting when we have a focused event to dispatch. The ANR is canceled if we |
| 2210 | // start interacting with another application via touch (app switch). This code can be removed |
| 2211 | // if the "no focused window ANR" is moved to the policy. Input doesn't know whether |
| 2212 | // an app is expected to have a focused window. |
| 2213 | if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) { |
| 2214 | if (!mNoFocusedWindowTimeoutTime.has_value()) { |
| 2215 | // 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] | 2216 | std::chrono::nanoseconds timeout = focusedApplicationHandle->getDispatchingTimeout( |
| 2217 | DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
| 2218 | mNoFocusedWindowTimeoutTime = currentTime + timeout.count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2219 | mAwaitedFocusedApplication = focusedApplicationHandle; |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 2220 | mAwaitedApplicationDisplayId = displayId; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2221 | ALOGW("Waiting because no window has focus but %s may eventually add a " |
| 2222 | "window when it finishes starting up. Will wait for %" PRId64 "ms", |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 2223 | mAwaitedFocusedApplication->getName().c_str(), millis(timeout)); |
Siarhei Vishniakou | 6950596 | 2023-12-28 12:07:04 -0800 | [diff] [blame] | 2224 | nextWakeupTime = std::min(nextWakeupTime, *mNoFocusedWindowTimeoutTime); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2225 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 2226 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2227 | } else if (currentTime > *mNoFocusedWindowTimeoutTime) { |
| 2228 | // Already raised ANR. Drop the event |
| 2229 | ALOGE("Dropping %s event because there is no focused window", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2230 | ftl::enum_string(entry.type).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2231 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2232 | } else { |
| 2233 | // Still waiting for the focused window |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2234 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 2235 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2236 | } |
| 2237 | } |
| 2238 | |
| 2239 | // we have a valid, non-null focused window |
| 2240 | resetNoFocusedWindowTimeoutLocked(); |
| 2241 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2242 | // Verify targeted injection. |
| 2243 | if (const auto err = verifyTargetedInjection(focusedWindowHandle, entry); err) { |
| 2244 | ALOGW("Dropping injected event: %s", (*err).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2245 | outInjectionResult = InputEventInjectionResult::TARGET_MISMATCH; |
| 2246 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2247 | } |
| 2248 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2249 | if (focusedWindowHandle->getInfo()->inputConfig.test( |
| 2250 | WindowInfo::InputConfig::PAUSE_DISPATCHING)) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2251 | ALOGI("Waiting because %s is paused", focusedWindowHandle->getName().c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2252 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 2253 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2254 | } |
| 2255 | |
| 2256 | // If the event is a key event, then we must wait for all previous events to |
| 2257 | // complete before delivering it because previous events may have the |
| 2258 | // side-effect of transferring focus to a different window and we want to |
| 2259 | // ensure that the following keys are sent to the new window. |
| 2260 | // |
| 2261 | // Suppose the user touches a button in a window then immediately presses "A". |
| 2262 | // If the button causes a pop-up window to appear then we want to ensure that |
| 2263 | // the "A" key is delivered to the new pop-up window. This is because users |
| 2264 | // often anticipate pending UI changes when typing on a keyboard. |
| 2265 | // To obtain this behavior, we must serialize key events with respect to all |
| 2266 | // prior input events. |
| 2267 | if (entry.type == EventEntry::Type::KEY) { |
| 2268 | if (shouldWaitToSendKeyLocked(currentTime, focusedWindowHandle->getName().c_str())) { |
Siarhei Vishniakou | 6950596 | 2023-12-28 12:07:04 -0800 | [diff] [blame] | 2269 | nextWakeupTime = std::min(nextWakeupTime, *mKeyIsWaitingForEventsTimeout); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2270 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 2271 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2272 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2273 | } |
| 2274 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2275 | outInjectionResult = InputEventInjectionResult::SUCCEEDED; |
| 2276 | return focusedWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2277 | } |
| 2278 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2279 | /** |
| 2280 | * Given a list of monitors, remove the ones we cannot find a connection for, and the ones |
| 2281 | * that are currently unresponsive. |
| 2282 | */ |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2283 | std::vector<Monitor> InputDispatcher::selectResponsiveMonitorsLocked( |
| 2284 | const std::vector<Monitor>& monitors) const { |
| 2285 | std::vector<Monitor> responsiveMonitors; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2286 | std::copy_if(monitors.begin(), monitors.end(), std::back_inserter(responsiveMonitors), |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 2287 | [](const Monitor& monitor) REQUIRES(mLock) { |
| 2288 | std::shared_ptr<Connection> connection = monitor.connection; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2289 | if (!connection->responsive) { |
| 2290 | ALOGW("Unresponsive monitor %s will not get the new gesture", |
Siarhei Vishniakou | 10bfdc9 | 2024-02-01 11:12:57 -0800 | [diff] [blame] | 2291 | connection->getInputChannelName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2292 | return false; |
| 2293 | } |
| 2294 | return true; |
| 2295 | }); |
| 2296 | return responsiveMonitors; |
| 2297 | } |
| 2298 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2299 | std::vector<InputTarget> InputDispatcher::findTouchedWindowTargetsLocked( |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2300 | nsecs_t currentTime, const MotionEntry& entry, |
Siarhei Vishniakou | 4fe5739 | 2022-10-25 13:44:30 -0700 | [diff] [blame] | 2301 | InputEventInjectionResult& outInjectionResult) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2302 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2303 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2304 | std::vector<InputTarget> targets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2305 | // For security reasons, we defer updating the touch state until we are sure that |
| 2306 | // event injection will be allowed. |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2307 | const int32_t displayId = entry.displayId; |
| 2308 | const int32_t action = entry.action; |
Siarhei Vishniakou | bf88052 | 2023-05-01 11:03:22 -0700 | [diff] [blame] | 2309 | const int32_t maskedAction = MotionEvent::getActionMasked(action); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2310 | |
| 2311 | // 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] | 2312 | outInjectionResult = InputEventInjectionResult::PENDING; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2313 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2314 | // Copy current touch state into tempTouchState. |
| 2315 | // This state will be used to update mTouchStatesByDisplay at the end of this function. |
| 2316 | // 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] | 2317 | const TouchState* oldState = nullptr; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2318 | TouchState tempTouchState; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2319 | if (const auto it = mTouchStatesByDisplay.find(displayId); it != mTouchStatesByDisplay.end()) { |
| 2320 | oldState = &(it->second); |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 2321 | tempTouchState = *oldState; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2322 | } |
| 2323 | |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 2324 | bool isSplit = shouldSplitTouch(tempTouchState, entry); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2325 | |
| 2326 | const bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE || |
| 2327 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2328 | maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2329 | // A DOWN could be generated from POINTER_DOWN if the initial pointers did not land into any |
| 2330 | // touchable windows. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2331 | const bool wasDown = oldState != nullptr && oldState->isDown(entry.deviceId); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2332 | const bool isDown = (maskedAction == AMOTION_EVENT_ACTION_DOWN) || |
| 2333 | (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN && !wasDown); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2334 | const bool newGesture = isDown || maskedAction == AMOTION_EVENT_ACTION_SCROLL || |
| 2335 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2336 | maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE; |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 2337 | const bool isFromMouse = isFromSource(entry.source, AINPUT_SOURCE_MOUSE); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2338 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2339 | if (newGesture) { |
| 2340 | isSplit = false; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2341 | } |
| 2342 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2343 | if (isDown && tempTouchState.hasHoveringPointers(entry.deviceId)) { |
| 2344 | // Compatibility behaviour: ACTION_DOWN causes HOVER_EXIT to get generated. |
| 2345 | tempTouchState.clearHoveringPointers(entry.deviceId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2346 | } |
| 2347 | |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2348 | if (isHoverAction) { |
Siarhei Vishniakou | 7be50c9 | 2023-11-17 17:09:08 -0800 | [diff] [blame] | 2349 | if (wasDown) { |
| 2350 | // Started hovering, but the device is already down: reject the hover event |
| 2351 | LOG(ERROR) << "Got hover event " << entry.getDescription() |
| 2352 | << " but the device is already down " << oldState->dump(); |
| 2353 | outInjectionResult = InputEventInjectionResult::FAILED; |
| 2354 | return {}; |
| 2355 | } |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2356 | // For hover actions, we will treat 'tempTouchState' as a new state, so let's erase |
| 2357 | // all of the existing hovering pointers and recompute. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2358 | tempTouchState.clearHoveringPointers(entry.deviceId); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2359 | } |
| 2360 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2361 | if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { |
| 2362 | /* 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] | 2363 | const auto [x, y] = resolveTouchedPosition(entry); |
Siarhei Vishniakou | 5b9766d | 2023-07-18 14:06:29 -0700 | [diff] [blame] | 2364 | const int32_t pointerIndex = MotionEvent::getActionIndex(action); |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2365 | const PointerProperties& pointer = entry.pointerProperties[pointerIndex]; |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2366 | // Outside targets should be added upon first dispatched DOWN event. That means, this should |
| 2367 | // 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] | 2368 | const bool isStylus = isPointerFromStylus(entry, pointerIndex); |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 2369 | sp<WindowInfoHandle> newTouchedWindowHandle = |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2370 | findTouchedWindowAtLocked(displayId, x, y, isStylus); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2371 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2372 | if (isDown) { |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2373 | targets += findOutsideTargetsLocked(displayId, newTouchedWindowHandle, pointer.id); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2374 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2375 | // Handle the case where we did not find a window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2376 | if (newTouchedWindowHandle == nullptr) { |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 2377 | 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] | 2378 | // 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] | 2379 | newTouchedWindowHandle = tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2380 | } |
| 2381 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2382 | // Verify targeted injection. |
| 2383 | if (const auto err = verifyTargetedInjection(newTouchedWindowHandle, entry); err) { |
| 2384 | ALOGW("Dropping injected touch event: %s", (*err).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2385 | outInjectionResult = os::InputEventInjectionResult::TARGET_MISMATCH; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2386 | newTouchedWindowHandle = nullptr; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2387 | return {}; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2388 | } |
| 2389 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2390 | // Figure out whether splitting will be allowed for this window. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2391 | if (newTouchedWindowHandle != nullptr) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2392 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
| 2393 | // New window supports splitting, but we should never split mouse events. |
| 2394 | isSplit = !isFromMouse; |
| 2395 | } else if (isSplit) { |
| 2396 | // New window does not support splitting but we have already split events. |
| 2397 | // Ignore the new window. |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame] | 2398 | LOG(INFO) << "Skipping " << newTouchedWindowHandle->getName() |
| 2399 | << " because it doesn't support split touch"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2400 | newTouchedWindowHandle = nullptr; |
| 2401 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2402 | } else { |
| 2403 | // 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] | 2404 | // be delivered to a new window which supports split touch. Pointers from a mouse device |
| 2405 | // should never be split. |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 2406 | isSplit = !isFromMouse; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2407 | } |
| 2408 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2409 | std::vector<sp<WindowInfoHandle>> newTouchedWindows = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2410 | findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2411 | if (newTouchedWindowHandle != nullptr) { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2412 | // Process the foreground window first so that it is the first to receive the event. |
| 2413 | newTouchedWindows.insert(newTouchedWindows.begin(), newTouchedWindowHandle); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2414 | } |
| 2415 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2416 | if (newTouchedWindows.empty()) { |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 2417 | ALOGI("Dropping event because there is no touchable window at (%.1f, %.1f) on display " |
| 2418 | "%d.", |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2419 | x, y, displayId); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2420 | outInjectionResult = InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2421 | return {}; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2422 | } |
| 2423 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2424 | for (const sp<WindowInfoHandle>& windowHandle : newTouchedWindows) { |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 2425 | if (!canWindowReceiveMotionLocked(windowHandle, entry)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2426 | continue; |
| 2427 | } |
| 2428 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2429 | if (isHoverAction) { |
Siarhei Vishniakou | b681c20 | 2023-05-01 11:22:33 -0700 | [diff] [blame] | 2430 | // The "windowHandle" is the target of this hovering pointer. |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2431 | tempTouchState.addHoveringPointerToWindow(windowHandle, entry.deviceId, pointer); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2432 | } |
| 2433 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2434 | // Set target flags. |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2435 | ftl::Flags<InputTarget::Flags> targetFlags; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2436 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2437 | if (canReceiveForegroundTouches(*windowHandle->getInfo())) { |
| 2438 | // 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] | 2439 | targetFlags |= InputTarget::Flags::FOREGROUND; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2440 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2441 | |
| 2442 | if (isSplit) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2443 | targetFlags |= InputTarget::Flags::SPLIT; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2444 | } |
| 2445 | if (isWindowObscuredAtPointLocked(windowHandle, x, y)) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2446 | targetFlags |= InputTarget::Flags::WINDOW_IS_OBSCURED; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2447 | } else if (isWindowObscuredLocked(windowHandle)) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2448 | targetFlags |= InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2449 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2450 | |
| 2451 | // Update the temporary touch state. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2452 | |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2453 | if (!isHoverAction) { |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2454 | const bool isDownOrPointerDown = maskedAction == AMOTION_EVENT_ACTION_DOWN || |
| 2455 | maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2456 | tempTouchState.addOrUpdateWindow(windowHandle, InputTarget::DispatchMode::AS_IS, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2457 | targetFlags, entry.deviceId, {pointer}, |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2458 | isDownOrPointerDown |
| 2459 | ? std::make_optional(entry.eventTime) |
| 2460 | : std::nullopt); |
| 2461 | // If this is the pointer going down and the touched window has a wallpaper |
| 2462 | // then also add the touched wallpaper windows so they are locked in for the |
| 2463 | // duration of the touch gesture. We do not collect wallpapers during HOVER_MOVE or |
| 2464 | // SCROLL because the wallpaper engine only supports touch events. We would need to |
| 2465 | // add a mechanism similar to View.onGenericMotionEvent to enable wallpapers to |
| 2466 | // handle these events. |
| 2467 | if (isDownOrPointerDown && targetFlags.test(InputTarget::Flags::FOREGROUND) && |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2468 | windowHandle->getInfo()->inputConfig.test( |
| 2469 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER)) { |
| 2470 | sp<WindowInfoHandle> wallpaper = findWallpaperWindowBelow(windowHandle); |
| 2471 | if (wallpaper != nullptr) { |
| 2472 | ftl::Flags<InputTarget::Flags> wallpaperFlags = |
| 2473 | InputTarget::Flags::WINDOW_IS_OBSCURED | |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2474 | InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED; |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2475 | if (isSplit) { |
| 2476 | wallpaperFlags |= InputTarget::Flags::SPLIT; |
| 2477 | } |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2478 | tempTouchState.addOrUpdateWindow(wallpaper, |
| 2479 | InputTarget::DispatchMode::AS_IS, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2480 | wallpaperFlags, entry.deviceId, {pointer}, |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2481 | entry.eventTime); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2482 | } |
| 2483 | } |
| 2484 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2485 | } |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 2486 | |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 2487 | // If a window is already pilfering some pointers, give it this new pointer as well and |
| 2488 | // make it pilfering. This will prevent other non-spy windows from getting this pointer, |
| 2489 | // which is a specific behaviour that we want. |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 2490 | for (TouchedWindow& touchedWindow : tempTouchState.windows) { |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2491 | if (touchedWindow.hasTouchingPointer(entry.deviceId, pointer.id) && |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 2492 | touchedWindow.hasPilferingPointers(entry.deviceId)) { |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 2493 | // This window is already pilfering some pointers, and this new pointer is also |
| 2494 | // going to it. Therefore, take over this pointer and don't give it to anyone |
| 2495 | // else. |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2496 | touchedWindow.addPilferingPointer(entry.deviceId, pointer.id); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 2497 | } |
| 2498 | } |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 2499 | |
| 2500 | // Restrict all pilfered pointers to the pilfering windows. |
| 2501 | tempTouchState.cancelPointersForNonPilferingWindows(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2502 | } else { |
| 2503 | /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ |
| 2504 | |
| 2505 | // If the pointer is not currently down, then ignore the event. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2506 | if (!tempTouchState.isDown(entry.deviceId) && |
| 2507 | maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT) { |
Siarhei Vishniakou | 31dd155 | 2023-10-30 18:46:10 -0700 | [diff] [blame] | 2508 | if (DEBUG_DROPPED_EVENTS_VERBOSE) { |
| 2509 | LOG(INFO) << "Dropping event because the pointer for device " << entry.deviceId |
| 2510 | << " is not down or we previously dropped the pointer down event in " |
| 2511 | << "display " << displayId << ": " << entry.getDescription(); |
| 2512 | } |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2513 | outInjectionResult = InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2514 | return {}; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2515 | } |
| 2516 | |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2517 | // If the pointer is not currently hovering, then ignore the event. |
| 2518 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 2519 | const int32_t pointerId = entry.pointerProperties[0].id; |
| 2520 | if (oldState == nullptr || |
| 2521 | oldState->getWindowsWithHoveringPointer(entry.deviceId, pointerId).empty()) { |
| 2522 | LOG(INFO) << "Dropping event because the hovering pointer is not in any windows in " |
| 2523 | "display " |
| 2524 | << displayId << ": " << entry.getDescription(); |
| 2525 | outInjectionResult = InputEventInjectionResult::FAILED; |
| 2526 | return {}; |
| 2527 | } |
| 2528 | tempTouchState.removeHoveringPointer(entry.deviceId, pointerId); |
| 2529 | } |
| 2530 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2531 | addDragEventLocked(entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2532 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2533 | // Check whether touches should slip outside of the current foreground window. |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 2534 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.getPointerCount() == 1 && |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2535 | tempTouchState.isSlippery()) { |
Siarhei Vishniakou | 9306c38 | 2022-09-30 15:30:31 -0700 | [diff] [blame] | 2536 | const auto [x, y] = resolveTouchedPosition(entry); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2537 | const bool isStylus = isPointerFromStylus(entry, /*pointerIndex=*/0); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2538 | sp<WindowInfoHandle> oldTouchedWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2539 | tempTouchState.getFirstForegroundWindowHandle(); |
Siarhei Vishniakou | 0f6558d | 2023-04-21 12:05:13 -0700 | [diff] [blame] | 2540 | LOG_ALWAYS_FATAL_IF(oldTouchedWindowHandle == nullptr); |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 2541 | sp<WindowInfoHandle> newTouchedWindowHandle = |
| 2542 | findTouchedWindowAtLocked(displayId, x, y, isStylus); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2543 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2544 | // Verify targeted injection. |
| 2545 | if (const auto err = verifyTargetedInjection(newTouchedWindowHandle, entry); err) { |
| 2546 | ALOGW("Dropping injected event: %s", (*err).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2547 | outInjectionResult = os::InputEventInjectionResult::TARGET_MISMATCH; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2548 | return {}; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2549 | } |
| 2550 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2551 | // Drop touch events if requested by input feature |
| 2552 | if (newTouchedWindowHandle != nullptr && |
| 2553 | shouldDropInput(entry, newTouchedWindowHandle)) { |
| 2554 | newTouchedWindowHandle = nullptr; |
| 2555 | } |
| 2556 | |
Siarhei Vishniakou | afa08cc | 2023-05-08 22:35:50 -0700 | [diff] [blame] | 2557 | if (newTouchedWindowHandle != nullptr && |
| 2558 | !haveSameToken(oldTouchedWindowHandle, newTouchedWindowHandle)) { |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2559 | ALOGI("Touch is slipping out of window %s into window %s in display %" PRId32, |
Siarhei Vishniakou | 0f6558d | 2023-04-21 12:05:13 -0700 | [diff] [blame] | 2560 | oldTouchedWindowHandle->getName().c_str(), |
| 2561 | newTouchedWindowHandle->getName().c_str(), displayId); |
| 2562 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2563 | // Make a slippery exit from the old window. |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 2564 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2565 | const PointerProperties& pointer = entry.pointerProperties[0]; |
| 2566 | pointerIds.set(pointer.id); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2567 | |
| 2568 | const TouchedWindow& touchedWindow = |
| 2569 | tempTouchState.getTouchedWindow(oldTouchedWindowHandle); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2570 | addPointerWindowTargetLocked(oldTouchedWindowHandle, |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2571 | InputTarget::DispatchMode::SLIPPERY_EXIT, |
| 2572 | ftl::Flags<InputTarget::Flags>(), pointerIds, |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2573 | touchedWindow.getDownTimeInTarget(entry.deviceId), |
| 2574 | targets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2575 | |
| 2576 | // Make a slippery entrance into the new window. |
| 2577 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
Prabir Pradhan | 713bb3e | 2021-12-20 02:07:40 -0800 | [diff] [blame] | 2578 | isSplit = !isFromMouse; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2579 | } |
| 2580 | |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2581 | ftl::Flags<InputTarget::Flags> targetFlags; |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2582 | if (canReceiveForegroundTouches(*newTouchedWindowHandle->getInfo())) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2583 | targetFlags |= InputTarget::Flags::FOREGROUND; |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2584 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2585 | if (isSplit) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2586 | targetFlags |= InputTarget::Flags::SPLIT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2587 | } |
| 2588 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2589 | targetFlags |= InputTarget::Flags::WINDOW_IS_OBSCURED; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2590 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2591 | targetFlags |= InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2592 | } |
| 2593 | |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2594 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, |
| 2595 | InputTarget::DispatchMode::SLIPPERY_ENTER, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2596 | targetFlags, entry.deviceId, {pointer}, |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2597 | entry.eventTime); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2598 | |
| 2599 | // Check if the wallpaper window should deliver the corresponding event. |
| 2600 | slipWallpaperTouch(targetFlags, oldTouchedWindowHandle, newTouchedWindowHandle, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2601 | tempTouchState, entry.deviceId, pointer, targets); |
| 2602 | tempTouchState.removeTouchingPointerFromWindow(entry.deviceId, pointer.id, |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 2603 | oldTouchedWindowHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2604 | } |
| 2605 | } |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 2606 | |
| 2607 | // Update the pointerIds for non-splittable when it received pointer down. |
| 2608 | if (!isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN) { |
| 2609 | // If no split, we suppose all touched windows should receive pointer down. |
Siarhei Vishniakou | 5b9766d | 2023-07-18 14:06:29 -0700 | [diff] [blame] | 2610 | const int32_t pointerIndex = MotionEvent::getActionIndex(action); |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2611 | std::vector<PointerProperties> touchingPointers{entry.pointerProperties[pointerIndex]}; |
| 2612 | for (TouchedWindow& touchedWindow : tempTouchState.windows) { |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 2613 | // Ignore drag window for it should just track one pointer. |
| 2614 | if (mDragState && mDragState->dragWindow == touchedWindow.windowHandle) { |
| 2615 | continue; |
| 2616 | } |
Siarhei Vishniakou | 5b9766d | 2023-07-18 14:06:29 -0700 | [diff] [blame] | 2617 | touchedWindow.addTouchingPointers(entry.deviceId, touchingPointers); |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 2618 | } |
| 2619 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2620 | } |
| 2621 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2622 | // Update dispatching for hover enter and exit. |
Sam Dubey | f886dec | 2023-01-27 13:28:19 +0000 | [diff] [blame] | 2623 | { |
| 2624 | std::vector<TouchedWindow> hoveringWindows = |
| 2625 | getHoveringWindowsLocked(oldState, tempTouchState, entry); |
| 2626 | for (const TouchedWindow& touchedWindow : hoveringWindows) { |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 2627 | std::optional<InputTarget> target = |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2628 | createInputTargetLocked(touchedWindow.windowHandle, touchedWindow.dispatchMode, |
| 2629 | touchedWindow.targetFlags, |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 2630 | touchedWindow.getDownTimeInTarget(entry.deviceId)); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 2631 | if (!target) { |
| 2632 | continue; |
| 2633 | } |
| 2634 | // Hardcode to single hovering pointer for now. |
| 2635 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
| 2636 | pointerIds.set(entry.pointerProperties[0].id); |
| 2637 | target->addPointers(pointerIds, touchedWindow.windowHandle->getInfo()->transform); |
| 2638 | targets.push_back(*target); |
Sam Dubey | f886dec | 2023-01-27 13:28:19 +0000 | [diff] [blame] | 2639 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2640 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2641 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2642 | // Ensure that all touched windows are valid for injection. |
| 2643 | if (entry.injectionState != nullptr) { |
| 2644 | std::string errs; |
| 2645 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2646 | const auto err = verifyTargetedInjection(touchedWindow.windowHandle, entry); |
| 2647 | if (err) errs += "\n - " + *err; |
| 2648 | } |
| 2649 | if (!errs.empty()) { |
| 2650 | 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] | 2651 | "%s:%s", |
| 2652 | entry.injectionState->targetUid->toString().c_str(), errs.c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2653 | outInjectionResult = InputEventInjectionResult::TARGET_MISMATCH; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2654 | return {}; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2655 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2656 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2657 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2658 | // Check whether windows listening for outside touches are owned by the same UID. If the owner |
| 2659 | // 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] | 2660 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2661 | sp<WindowInfoHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2662 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2663 | if (foregroundWindowHandle) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 2664 | const auto foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2665 | for (InputTarget& target : targets) { |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2666 | if (target.dispatchMode == InputTarget::DispatchMode::OUTSIDE) { |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2667 | sp<WindowInfoHandle> targetWindow = |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 2668 | getWindowHandleLocked(target.connection->getToken()); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2669 | if (targetWindow->getInfo()->ownerUid != foregroundWindowUid) { |
| 2670 | target.flags |= InputTarget::Flags::ZERO_COORDS; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2671 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2672 | } |
| 2673 | } |
| 2674 | } |
| 2675 | } |
| 2676 | |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 2677 | // If this is a touchpad navigation gesture, it needs to only be sent to trusted targets, as we |
| 2678 | // only want the system UI to handle these gestures. |
| 2679 | const bool isTouchpadNavGesture = isFromSource(entry.source, AINPUT_SOURCE_MOUSE) && |
| 2680 | entry.classification == MotionClassification::MULTI_FINGER_SWIPE; |
| 2681 | if (isTouchpadNavGesture) { |
| 2682 | filterUntrustedTargets(/* byref */ tempTouchState, /* byref */ targets); |
| 2683 | } |
| 2684 | |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2685 | // Output targets from the touch state. |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2686 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2687 | std::vector<PointerProperties> touchingPointers = |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2688 | touchedWindow.getTouchingPointers(entry.deviceId); |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2689 | if (touchingPointers.empty()) { |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2690 | continue; |
| 2691 | } |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2692 | addPointerWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.dispatchMode, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2693 | touchedWindow.targetFlags, getPointerIds(touchingPointers), |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2694 | touchedWindow.getDownTimeInTarget(entry.deviceId), targets); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2695 | } |
Sam Dubey | 39d37cf | 2022-12-07 18:05:35 +0000 | [diff] [blame] | 2696 | |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 2697 | // During targeted injection, only allow owned targets to receive events |
| 2698 | std::erase_if(targets, [&](const InputTarget& target) { |
| 2699 | LOG_ALWAYS_FATAL_IF(target.windowHandle == nullptr); |
| 2700 | const auto err = verifyTargetedInjection(target.windowHandle, entry); |
| 2701 | if (err) { |
| 2702 | LOG(WARNING) << "Dropping injected event from " << target.windowHandle->getName() |
| 2703 | << ": " << (*err); |
| 2704 | return true; |
| 2705 | } |
| 2706 | return false; |
| 2707 | }); |
| 2708 | |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2709 | if (targets.empty()) { |
| 2710 | LOG(INFO) << "Dropping event because no targets were found: " << entry.getDescription(); |
| 2711 | outInjectionResult = InputEventInjectionResult::FAILED; |
| 2712 | return {}; |
| 2713 | } |
| 2714 | |
| 2715 | // If we only have windows getting ACTION_OUTSIDE, then drop the event, because there is no |
| 2716 | // window that is actually receiving the entire gesture. |
| 2717 | if (std::all_of(targets.begin(), targets.end(), [](const InputTarget& target) { |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2718 | return target.dispatchMode == InputTarget::DispatchMode::OUTSIDE; |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2719 | })) { |
| 2720 | LOG(INFO) << "Dropping event because all windows would just receive ACTION_OUTSIDE: " |
| 2721 | << entry.getDescription(); |
| 2722 | outInjectionResult = InputEventInjectionResult::FAILED; |
| 2723 | return {}; |
| 2724 | } |
| 2725 | |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2726 | outInjectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2727 | |
Prabir Pradhan | 502a725 | 2023-12-01 16:11:24 +0000 | [diff] [blame] | 2728 | // Now that we have generated all of the input targets for this event, reset the dispatch |
| 2729 | // mode for all touched window to AS_IS. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2730 | for (TouchedWindow& touchedWindow : tempTouchState.windows) { |
Prabir Pradhan | 502a725 | 2023-12-01 16:11:24 +0000 | [diff] [blame] | 2731 | touchedWindow.dispatchMode = InputTarget::DispatchMode::AS_IS; |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2732 | } |
| 2733 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2734 | // Update final pieces of touch state if the injector had permission. |
Siarhei Vishniakou | 7be50c9 | 2023-11-17 17:09:08 -0800 | [diff] [blame] | 2735 | if (maskedAction == AMOTION_EVENT_ACTION_UP) { |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2736 | // Pointer went up. |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 2737 | tempTouchState.removeTouchingPointer(entry.deviceId, entry.pointerProperties[0].id); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2738 | } else if (maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2739 | // All pointers up or canceled. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2740 | tempTouchState.removeAllPointersForDevice(entry.deviceId); |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2741 | } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 2742 | // One pointer went up. |
Siarhei Vishniakou | 5b9766d | 2023-07-18 14:06:29 -0700 | [diff] [blame] | 2743 | const int32_t pointerIndex = MotionEvent::getActionIndex(action); |
| 2744 | const uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
| 2745 | tempTouchState.removeTouchingPointer(entry.deviceId, pointerId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2746 | } |
| 2747 | |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2748 | // Save changes unless the action was scroll in which case the temporary touch |
| 2749 | // state was only valid for this one action. |
| 2750 | if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) { |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 2751 | if (displayId >= 0) { |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2752 | tempTouchState.clearWindowsWithoutPointers(); |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2753 | mTouchStatesByDisplay[displayId] = tempTouchState; |
| 2754 | } else { |
| 2755 | mTouchStatesByDisplay.erase(displayId); |
| 2756 | } |
| 2757 | } |
| 2758 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 2759 | if (tempTouchState.windows.empty()) { |
| 2760 | mTouchStatesByDisplay.erase(displayId); |
| 2761 | } |
| 2762 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2763 | return targets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2764 | } |
| 2765 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2766 | void InputDispatcher::finishDragAndDrop(int32_t displayId, float x, float y) { |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2767 | // Prevent stylus interceptor windows from affecting drag and drop behavior for now, until we |
| 2768 | // have an explicit reason to support it. |
| 2769 | constexpr bool isStylus = false; |
| 2770 | |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 2771 | sp<WindowInfoHandle> dropWindow = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2772 | findTouchedWindowAtLocked(displayId, x, y, isStylus, /*ignoreDragWindow=*/true); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2773 | if (dropWindow) { |
| 2774 | vec2 local = dropWindow->getInfo()->transform.transform(x, y); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2775 | sendDropWindowCommandLocked(dropWindow->getToken(), local.x, local.y); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 2776 | } else { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2777 | ALOGW("No window found when drop."); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2778 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2779 | } |
| 2780 | mDragState.reset(); |
| 2781 | } |
| 2782 | |
| 2783 | void InputDispatcher::addDragEventLocked(const MotionEntry& entry) { |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 2784 | if (!mDragState || mDragState->dragWindow->getInfo()->displayId != entry.displayId) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2785 | return; |
| 2786 | } |
| 2787 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2788 | if (!mDragState->isStartDrag) { |
| 2789 | mDragState->isStartDrag = true; |
| 2790 | mDragState->isStylusButtonDownAtStart = |
| 2791 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2792 | } |
| 2793 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2794 | // Find the pointer index by id. |
| 2795 | int32_t pointerIndex = 0; |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 2796 | for (; static_cast<uint32_t>(pointerIndex) < entry.getPointerCount(); pointerIndex++) { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2797 | const PointerProperties& pointerProperties = entry.pointerProperties[pointerIndex]; |
| 2798 | if (pointerProperties.id == mDragState->pointerId) { |
| 2799 | break; |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2800 | } |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2801 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2802 | |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 2803 | if (uint32_t(pointerIndex) == entry.getPointerCount()) { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2804 | 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] | 2805 | } |
| 2806 | |
| 2807 | const int32_t maskedAction = entry.action & AMOTION_EVENT_ACTION_MASK; |
| 2808 | const int32_t x = entry.pointerCoords[pointerIndex].getX(); |
| 2809 | const int32_t y = entry.pointerCoords[pointerIndex].getY(); |
| 2810 | |
| 2811 | switch (maskedAction) { |
| 2812 | case AMOTION_EVENT_ACTION_MOVE: { |
| 2813 | // Handle the special case : stylus button no longer pressed. |
| 2814 | bool isStylusButtonDown = |
| 2815 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2816 | if (mDragState->isStylusButtonDownAtStart && !isStylusButtonDown) { |
| 2817 | finishDragAndDrop(entry.displayId, x, y); |
| 2818 | return; |
| 2819 | } |
| 2820 | |
| 2821 | // Prevent stylus interceptor windows from affecting drag and drop behavior for now, |
| 2822 | // until we have an explicit reason to support it. |
| 2823 | constexpr bool isStylus = false; |
| 2824 | |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 2825 | sp<WindowInfoHandle> hoverWindowHandle = |
| 2826 | findTouchedWindowAtLocked(entry.displayId, x, y, isStylus, |
| 2827 | /*ignoreDragWindow=*/true); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2828 | // enqueue drag exit if needed. |
| 2829 | if (hoverWindowHandle != mDragState->dragHoverWindowHandle && |
| 2830 | !haveSameToken(hoverWindowHandle, mDragState->dragHoverWindowHandle)) { |
| 2831 | if (mDragState->dragHoverWindowHandle != nullptr) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2832 | enqueueDragEventLocked(mDragState->dragHoverWindowHandle, /*isExiting=*/true, x, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2833 | y); |
| 2834 | } |
| 2835 | mDragState->dragHoverWindowHandle = hoverWindowHandle; |
| 2836 | } |
| 2837 | // enqueue drag location if needed. |
| 2838 | if (hoverWindowHandle != nullptr) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2839 | enqueueDragEventLocked(hoverWindowHandle, /*isExiting=*/false, x, y); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2840 | } |
| 2841 | break; |
| 2842 | } |
| 2843 | |
| 2844 | case AMOTION_EVENT_ACTION_POINTER_UP: |
Siarhei Vishniakou | 5b9766d | 2023-07-18 14:06:29 -0700 | [diff] [blame] | 2845 | if (MotionEvent::getActionIndex(entry.action) != pointerIndex) { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2846 | break; |
| 2847 | } |
| 2848 | // The drag pointer is up. |
| 2849 | [[fallthrough]]; |
| 2850 | case AMOTION_EVENT_ACTION_UP: |
| 2851 | finishDragAndDrop(entry.displayId, x, y); |
| 2852 | break; |
| 2853 | case AMOTION_EVENT_ACTION_CANCEL: { |
| 2854 | ALOGD("Receiving cancel when drag and drop."); |
| 2855 | sendDropWindowCommandLocked(nullptr, 0, 0); |
| 2856 | mDragState.reset(); |
| 2857 | break; |
| 2858 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2859 | } |
| 2860 | } |
| 2861 | |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2862 | std::optional<InputTarget> InputDispatcher::createInputTargetLocked( |
| 2863 | const sp<android::gui::WindowInfoHandle>& windowHandle, |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2864 | InputTarget::DispatchMode dispatchMode, ftl::Flags<InputTarget::Flags> targetFlags, |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2865 | std::optional<nsecs_t> firstDownTimeInTarget) const { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 2866 | std::shared_ptr<Connection> connection = getConnectionLocked(windowHandle->getToken()); |
| 2867 | if (connection == nullptr) { |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2868 | ALOGW("Not creating InputTarget for %s, no input channel", windowHandle->getName().c_str()); |
| 2869 | return {}; |
| 2870 | } |
| 2871 | InputTarget inputTarget; |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 2872 | inputTarget.connection = connection; |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 2873 | inputTarget.windowHandle = windowHandle; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2874 | inputTarget.dispatchMode = dispatchMode; |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2875 | inputTarget.flags = targetFlags; |
| 2876 | inputTarget.globalScaleFactor = windowHandle->getInfo()->globalScaleFactor; |
| 2877 | inputTarget.firstDownTimeInTarget = firstDownTimeInTarget; |
| 2878 | const auto& displayInfoIt = mDisplayInfos.find(windowHandle->getInfo()->displayId); |
| 2879 | if (displayInfoIt != mDisplayInfos.end()) { |
| 2880 | inputTarget.displayTransform = displayInfoIt->second.transform; |
| 2881 | } else { |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 2882 | // DisplayInfo not found for this window on display windowHandle->getInfo()->displayId. |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2883 | // TODO(b/198444055): Make this an error message after 'setInputWindows' API is removed. |
| 2884 | } |
| 2885 | return inputTarget; |
| 2886 | } |
| 2887 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2888 | void InputDispatcher::addWindowTargetLocked(const sp<WindowInfoHandle>& windowHandle, |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2889 | InputTarget::DispatchMode dispatchMode, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2890 | ftl::Flags<InputTarget::Flags> targetFlags, |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2891 | std::optional<nsecs_t> firstDownTimeInTarget, |
Siarhei Vishniakou | f75cddb | 2022-10-25 10:42:16 -0700 | [diff] [blame] | 2892 | std::vector<InputTarget>& inputTargets) const { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2893 | std::vector<InputTarget>::iterator it = |
| 2894 | std::find_if(inputTargets.begin(), inputTargets.end(), |
| 2895 | [&windowHandle](const InputTarget& inputTarget) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 2896 | return inputTarget.connection->getToken() == windowHandle->getToken(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2897 | }); |
Chavi Weingarten | 97b8eec | 2020-01-09 18:09:08 +0000 | [diff] [blame] | 2898 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2899 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2900 | |
| 2901 | if (it == inputTargets.end()) { |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2902 | std::optional<InputTarget> target = |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2903 | createInputTargetLocked(windowHandle, dispatchMode, targetFlags, |
| 2904 | firstDownTimeInTarget); |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2905 | if (!target) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2906 | return; |
| 2907 | } |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2908 | inputTargets.push_back(*target); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2909 | it = inputTargets.end() - 1; |
| 2910 | } |
| 2911 | |
Siarhei Vishniakou | 23d73fb | 2023-10-29 13:27:46 -0700 | [diff] [blame] | 2912 | if (it->flags != targetFlags) { |
| 2913 | LOG(ERROR) << "Flags don't match! targetFlags=" << targetFlags.string() << ", it=" << *it; |
| 2914 | } |
| 2915 | if (it->globalScaleFactor != windowInfo->globalScaleFactor) { |
| 2916 | LOG(ERROR) << "Mismatch! it->globalScaleFactor=" << it->globalScaleFactor |
| 2917 | << ", windowInfo->globalScaleFactor=" << windowInfo->globalScaleFactor; |
| 2918 | } |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2919 | } |
| 2920 | |
| 2921 | void InputDispatcher::addPointerWindowTargetLocked( |
| 2922 | const sp<android::gui::WindowInfoHandle>& windowHandle, |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2923 | InputTarget::DispatchMode dispatchMode, ftl::Flags<InputTarget::Flags> targetFlags, |
| 2924 | std::bitset<MAX_POINTER_ID + 1> pointerIds, std::optional<nsecs_t> firstDownTimeInTarget, |
| 2925 | std::vector<InputTarget>& inputTargets) const REQUIRES(mLock) { |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2926 | if (pointerIds.none()) { |
| 2927 | for (const auto& target : inputTargets) { |
| 2928 | LOG(INFO) << "Target: " << target; |
| 2929 | } |
| 2930 | LOG(FATAL) << "No pointers specified for " << windowHandle->getName(); |
| 2931 | return; |
| 2932 | } |
| 2933 | std::vector<InputTarget>::iterator it = |
| 2934 | std::find_if(inputTargets.begin(), inputTargets.end(), |
| 2935 | [&windowHandle](const InputTarget& inputTarget) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 2936 | return inputTarget.connection->getToken() == windowHandle->getToken(); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2937 | }); |
| 2938 | |
| 2939 | // This is a hack, because the actual entry could potentially be an ACTION_DOWN event that |
| 2940 | // causes a HOVER_EXIT to be generated. That means that the same entry of ACTION_DOWN would |
| 2941 | // have DISPATCH_AS_HOVER_EXIT and DISPATCH_AS_IS. And therefore, we have to create separate |
| 2942 | // input targets for hovering pointers and for touching pointers. |
| 2943 | // If we picked an existing input target above, but it's for HOVER_EXIT - let's use a new |
| 2944 | // target instead. |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2945 | if (it != inputTargets.end() && it->dispatchMode == InputTarget::DispatchMode::HOVER_EXIT) { |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2946 | // Force the code below to create a new input target |
| 2947 | it = inputTargets.end(); |
| 2948 | } |
| 2949 | |
| 2950 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 2951 | |
| 2952 | if (it == inputTargets.end()) { |
| 2953 | std::optional<InputTarget> target = |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2954 | createInputTargetLocked(windowHandle, dispatchMode, targetFlags, |
| 2955 | firstDownTimeInTarget); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2956 | if (!target) { |
| 2957 | return; |
| 2958 | } |
| 2959 | inputTargets.push_back(*target); |
| 2960 | it = inputTargets.end() - 1; |
| 2961 | } |
| 2962 | |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2963 | if (it->dispatchMode != dispatchMode) { |
| 2964 | LOG(ERROR) << __func__ << ": DispatchMode doesn't match! ignoring new mode=" |
| 2965 | << ftl::enum_string(dispatchMode) << ", it=" << *it; |
| 2966 | } |
Siarhei Vishniakou | 4bd0b7c | 2023-10-27 00:51:14 -0700 | [diff] [blame] | 2967 | if (it->flags != targetFlags) { |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2968 | LOG(ERROR) << __func__ << ": Flags don't match! new targetFlags=" << targetFlags.string() |
| 2969 | << ", it=" << *it; |
Siarhei Vishniakou | 4bd0b7c | 2023-10-27 00:51:14 -0700 | [diff] [blame] | 2970 | } |
Siarhei Vishniakou | 23d73fb | 2023-10-29 13:27:46 -0700 | [diff] [blame] | 2971 | if (it->globalScaleFactor != windowInfo->globalScaleFactor) { |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2972 | LOG(ERROR) << __func__ << ": Mismatch! it->globalScaleFactor=" << it->globalScaleFactor |
Siarhei Vishniakou | 23d73fb | 2023-10-29 13:27:46 -0700 | [diff] [blame] | 2973 | << ", windowInfo->globalScaleFactor=" << windowInfo->globalScaleFactor; |
| 2974 | } |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2975 | |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2976 | it->addPointers(pointerIds, windowInfo->transform); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2977 | } |
| 2978 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2979 | void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2980 | int32_t displayId) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2981 | auto monitorsIt = mGlobalMonitorsByDisplay.find(displayId); |
| 2982 | if (monitorsIt == mGlobalMonitorsByDisplay.end()) return; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2983 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2984 | for (const Monitor& monitor : selectResponsiveMonitorsLocked(monitorsIt->second)) { |
| 2985 | InputTarget target; |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 2986 | target.connection = monitor.connection; |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2987 | // target.firstDownTimeInTarget is not set for global monitors. It is only required in split |
| 2988 | // touch and global monitoring works as intended even without setting firstDownTimeInTarget |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2989 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
| 2990 | target.displayTransform = it->second.transform; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2991 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2992 | target.setDefaultPointerTransform(target.displayTransform); |
| 2993 | inputTargets.push_back(target); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2994 | } |
| 2995 | } |
| 2996 | |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2997 | /** |
| 2998 | * Indicate whether one window handle should be considered as obscuring |
| 2999 | * another window handle. We only check a few preconditions. Actually |
| 3000 | * checking the bounds is left to the caller. |
| 3001 | */ |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3002 | static bool canBeObscuredBy(const sp<WindowInfoHandle>& windowHandle, |
| 3003 | const sp<WindowInfoHandle>& otherHandle) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3004 | // Compare by token so cloned layers aren't counted |
| 3005 | if (haveSameToken(windowHandle, otherHandle)) { |
| 3006 | return false; |
| 3007 | } |
| 3008 | auto info = windowHandle->getInfo(); |
| 3009 | auto otherInfo = otherHandle->getInfo(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 3010 | if (otherInfo->inputConfig.test(WindowInfo::InputConfig::NOT_VISIBLE)) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3011 | return false; |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 3012 | } else if (otherInfo->alpha == 0 && |
| 3013 | otherInfo->inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE)) { |
Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame] | 3014 | // Those act as if they were invisible, so we don't need to flag them. |
| 3015 | // We do want to potentially flag touchable windows even if they have 0 |
| 3016 | // opacity, since they can consume touches and alter the effects of the |
| 3017 | // user interaction (eg. apps that rely on |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3018 | // Flags::WINDOW_IS_PARTIALLY_OBSCURED should still be told about those |
Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame] | 3019 | // windows), hence we also check for FLAG_NOT_TOUCHABLE. |
| 3020 | return false; |
Bernardo Rufino | 8007daf | 2020-09-22 09:40:01 +0000 | [diff] [blame] | 3021 | } else if (info->ownerUid == otherInfo->ownerUid) { |
| 3022 | // If ownerUid is the same we don't generate occlusion events as there |
| 3023 | // is no security boundary within an uid. |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3024 | return false; |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 3025 | } else if (otherInfo->inputConfig.test(gui::WindowInfo::InputConfig::TRUSTED_OVERLAY)) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3026 | return false; |
| 3027 | } else if (otherInfo->displayId != info->displayId) { |
| 3028 | return false; |
| 3029 | } |
| 3030 | return true; |
| 3031 | } |
| 3032 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3033 | /** |
| 3034 | * Returns touch occlusion information in the form of TouchOcclusionInfo. To check if the touch is |
| 3035 | * untrusted, one should check: |
| 3036 | * |
| 3037 | * 1. If result.hasBlockingOcclusion is true. |
| 3038 | * If it's, it means the touch should be blocked due to a window with occlusion mode of |
| 3039 | * BLOCK_UNTRUSTED. |
| 3040 | * |
| 3041 | * 2. If result.obscuringOpacity > mMaximumObscuringOpacityForTouch. |
| 3042 | * If it is (and 1 is false), then the touch should be blocked because a stack of windows |
| 3043 | * (possibly only one) with occlusion mode of USE_OPACITY from one UID resulted in a composed |
| 3044 | * obscuring opacity above the threshold. Note that if there was no window of occlusion mode |
| 3045 | * USE_OPACITY, result.obscuringOpacity would've been 0 and since |
| 3046 | * mMaximumObscuringOpacityForTouch >= 0, the condition above would never be true. |
| 3047 | * |
| 3048 | * If neither of those is true, then it means the touch can be allowed. |
| 3049 | */ |
| 3050 | InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3051 | const sp<WindowInfoHandle>& windowHandle, int32_t x, int32_t y) const { |
| 3052 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 3053 | int32_t displayId = windowInfo->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3054 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3055 | TouchOcclusionInfo info; |
| 3056 | info.hasBlockingOcclusion = false; |
| 3057 | info.obscuringOpacity = 0; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3058 | info.obscuringUid = gui::Uid::INVALID; |
| 3059 | std::map<gui::Uid, float> opacityByUid; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3060 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3061 | if (windowHandle == otherHandle) { |
| 3062 | break; // All future windows are below us. Exit early. |
| 3063 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3064 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 3065 | if (canBeObscuredBy(windowHandle, otherHandle) && otherInfo->frameContainsPoint(x, y) && |
| 3066 | !haveSameApplicationToken(windowInfo, otherInfo)) { |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 3067 | if (DEBUG_TOUCH_OCCLUSION) { |
| 3068 | info.debugInfo.push_back( |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 3069 | dumpWindowForTouchOcclusion(otherInfo, /*isTouchedWindow=*/false)); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 3070 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3071 | // canBeObscuredBy() has returned true above, which means this window is untrusted, so |
| 3072 | // we perform the checks below to see if the touch can be propagated or not based on the |
| 3073 | // window's touch occlusion mode |
| 3074 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::BLOCK_UNTRUSTED) { |
| 3075 | info.hasBlockingOcclusion = true; |
| 3076 | info.obscuringUid = otherInfo->ownerUid; |
| 3077 | info.obscuringPackage = otherInfo->packageName; |
| 3078 | break; |
| 3079 | } |
| 3080 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::USE_OPACITY) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3081 | const auto uid = otherInfo->ownerUid; |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3082 | float opacity = |
| 3083 | (opacityByUid.find(uid) == opacityByUid.end()) ? 0 : opacityByUid[uid]; |
| 3084 | // Given windows A and B: |
| 3085 | // opacity(A, B) = 1 - [1 - opacity(A)] * [1 - opacity(B)] |
| 3086 | opacity = 1 - (1 - opacity) * (1 - otherInfo->alpha); |
| 3087 | opacityByUid[uid] = opacity; |
| 3088 | if (opacity > info.obscuringOpacity) { |
| 3089 | info.obscuringOpacity = opacity; |
| 3090 | info.obscuringUid = uid; |
| 3091 | info.obscuringPackage = otherInfo->packageName; |
| 3092 | } |
| 3093 | } |
| 3094 | } |
| 3095 | } |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 3096 | if (DEBUG_TOUCH_OCCLUSION) { |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 3097 | info.debugInfo.push_back(dumpWindowForTouchOcclusion(windowInfo, /*isTouchedWindow=*/true)); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 3098 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3099 | return info; |
| 3100 | } |
| 3101 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3102 | std::string InputDispatcher::dumpWindowForTouchOcclusion(const WindowInfo* info, |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 3103 | bool isTouchedWindow) const { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3104 | return StringPrintf(INDENT2 "* %spackage=%s/%s, id=%" PRId32 ", mode=%s, alpha=%.2f, " |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 3105 | "frame=[%" PRId32 ",%" PRId32 "][%" PRId32 ",%" PRId32 |
| 3106 | "], touchableRegion=%s, window={%s}, inputConfig={%s}, " |
| 3107 | "hasToken=%s, applicationInfo.name=%s, applicationInfo.token=%s\n", |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 3108 | isTouchedWindow ? "[TOUCHED] " : "", info->packageName.c_str(), |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3109 | info->ownerUid.toString().c_str(), info->id, |
Chavi Weingarten | 7f01919 | 2023-08-08 20:39:01 +0000 | [diff] [blame] | 3110 | toString(info->touchOcclusionMode).c_str(), info->alpha, info->frame.left, |
| 3111 | info->frame.top, info->frame.right, info->frame.bottom, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3112 | dumpRegion(info->touchableRegion).c_str(), info->name.c_str(), |
| 3113 | info->inputConfig.string().c_str(), toString(info->token != nullptr), |
| 3114 | info->applicationInfo.name.c_str(), |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 3115 | binderToString(info->applicationInfo.token).c_str()); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 3116 | } |
| 3117 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3118 | bool InputDispatcher::isTouchTrustedLocked(const TouchOcclusionInfo& occlusionInfo) const { |
| 3119 | if (occlusionInfo.hasBlockingOcclusion) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3120 | ALOGW("Untrusted touch due to occlusion by %s/%s", occlusionInfo.obscuringPackage.c_str(), |
| 3121 | occlusionInfo.obscuringUid.toString().c_str()); |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3122 | return false; |
| 3123 | } |
| 3124 | if (occlusionInfo.obscuringOpacity > mMaximumObscuringOpacityForTouch) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3125 | ALOGW("Untrusted touch due to occlusion by %s/%s (obscuring opacity = " |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3126 | "%.2f, maximum allowed = %.2f)", |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3127 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid.toString().c_str(), |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3128 | occlusionInfo.obscuringOpacity, mMaximumObscuringOpacityForTouch); |
| 3129 | return false; |
| 3130 | } |
| 3131 | return true; |
| 3132 | } |
| 3133 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3134 | bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3135 | int32_t x, int32_t y) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3136 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3137 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 3138 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3139 | if (windowHandle == otherHandle) { |
| 3140 | break; // All future windows are below us. Exit early. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3141 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3142 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3143 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 3144 | otherInfo->frameContainsPoint(x, y)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3145 | return true; |
| 3146 | } |
| 3147 | } |
| 3148 | return false; |
| 3149 | } |
| 3150 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3151 | bool InputDispatcher::isWindowObscuredLocked(const sp<WindowInfoHandle>& windowHandle) const { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 3152 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3153 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 3154 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 3155 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3156 | if (windowHandle == otherHandle) { |
| 3157 | break; // All future windows are below us. Exit early. |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 3158 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3159 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3160 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 3161 | otherInfo->overlaps(windowInfo)) { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 3162 | return true; |
| 3163 | } |
| 3164 | } |
| 3165 | return false; |
| 3166 | } |
| 3167 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 3168 | std::string InputDispatcher::getApplicationWindowLabel( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3169 | const InputApplicationHandle* applicationHandle, const sp<WindowInfoHandle>& windowHandle) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3170 | if (applicationHandle != nullptr) { |
| 3171 | if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 3172 | return applicationHandle->getName() + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3173 | } else { |
| 3174 | return applicationHandle->getName(); |
| 3175 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3176 | } else if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 3177 | return windowHandle->getInfo()->applicationInfo.name + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3178 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3179 | return "<unknown application or window>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3180 | } |
| 3181 | } |
| 3182 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3183 | void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3184 | if (!isUserActivityEvent(eventEntry)) { |
| 3185 | // 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] | 3186 | return; |
| 3187 | } |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 3188 | |
| 3189 | const int32_t eventType = getUserActivityEventType(eventEntry); |
| 3190 | if (input_flags::rate_limit_user_activity_poke_in_dispatcher()) { |
| 3191 | // Note that we're directly getting the time diff between the current event and the previous |
| 3192 | // event. This is assuming that the first user event always happens at a timestamp that is |
| 3193 | // greater than `mMinTimeBetweenUserActivityPokes` (otherwise, the first user event will |
| 3194 | // wrongly be dropped). In real life, `mMinTimeBetweenUserActivityPokes` is a much smaller |
| 3195 | // value than the potential first user activity event time, so this is ok. |
| 3196 | std::chrono::nanoseconds timeSinceLastEvent = |
| 3197 | std::chrono::nanoseconds(eventEntry.eventTime - mLastUserActivityTimes[eventType]); |
| 3198 | if (timeSinceLastEvent < mMinTimeBetweenUserActivityPokes) { |
| 3199 | return; |
| 3200 | } |
| 3201 | } |
| 3202 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3203 | int32_t displayId = getTargetDisplayId(eventEntry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3204 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 3205 | const WindowInfo* windowDisablingUserActivityInfo = nullptr; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3206 | if (focusedWindowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3207 | const WindowInfo* info = focusedWindowHandle->getInfo(); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 3208 | if (info->inputConfig.test(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY)) { |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 3209 | windowDisablingUserActivityInfo = info; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3210 | } |
| 3211 | } |
| 3212 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3213 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3214 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3215 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 3216 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3217 | return; |
| 3218 | } |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 3219 | if (windowDisablingUserActivityInfo != nullptr) { |
| 3220 | if (DEBUG_DISPATCH_CYCLE) { |
| 3221 | ALOGD("Not poking user activity: disabled by window '%s'.", |
| 3222 | windowDisablingUserActivityInfo->name.c_str()); |
| 3223 | } |
| 3224 | return; |
| 3225 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3226 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3227 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3228 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3229 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 3230 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3231 | return; |
| 3232 | } |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 3233 | // If the key code is unknown, we don't consider it user activity |
| 3234 | if (keyEntry.keyCode == AKEYCODE_UNKNOWN) { |
| 3235 | return; |
| 3236 | } |
| 3237 | // Don't inhibit events that were intercepted or are not passed to |
| 3238 | // the apps, like system shortcuts |
| 3239 | if (windowDisablingUserActivityInfo != nullptr && |
| 3240 | keyEntry.interceptKeyResult != KeyEntry::InterceptKeyResult::SKIP && |
| 3241 | keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER) { |
| 3242 | if (DEBUG_DISPATCH_CYCLE) { |
| 3243 | ALOGD("Not poking user activity: disabled by window '%s'.", |
| 3244 | windowDisablingUserActivityInfo->name.c_str()); |
| 3245 | } |
| 3246 | return; |
| 3247 | } |
| 3248 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3249 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3250 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3251 | default: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3252 | LOG_ALWAYS_FATAL("%s events are not user activity", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3253 | ftl::enum_string(eventEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3254 | break; |
| 3255 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3256 | } |
| 3257 | |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 3258 | mLastUserActivityTimes[eventType] = eventEntry.eventTime; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3259 | auto command = [this, eventTime = eventEntry.eventTime, eventType, displayId]() |
| 3260 | REQUIRES(mLock) { |
| 3261 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 3262 | mPolicy.pokeUserActivity(eventTime, eventType, displayId); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3263 | }; |
| 3264 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3265 | } |
| 3266 | |
| 3267 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3268 | const std::shared_ptr<Connection>& connection, |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 3269 | std::shared_ptr<const EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3270 | const InputTarget& inputTarget) { |
Prabir Pradhan | 2dac8b8 | 2023-09-06 01:11:51 +0000 | [diff] [blame] | 3271 | ATRACE_NAME_IF(ATRACE_ENABLED(), |
| 3272 | StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
| 3273 | connection->getInputChannelName().c_str(), eventEntry->id)); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3274 | if (DEBUG_DISPATCH_CYCLE) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3275 | ALOGD("channel '%s' ~ prepareDispatchCycle - flags=%s, " |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 3276 | "globalScaleFactor=%f, pointerIds=%s %s", |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3277 | connection->getInputChannelName().c_str(), inputTarget.flags.string().c_str(), |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 3278 | inputTarget.globalScaleFactor, bitsetToString(inputTarget.pointerIds).c_str(), |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3279 | inputTarget.getPointerInfoString().c_str()); |
| 3280 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3281 | |
| 3282 | // Skip this event if the connection status is not normal. |
| 3283 | // 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] | 3284 | if (connection->status != Connection::Status::NORMAL) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3285 | if (DEBUG_DISPATCH_CYCLE) { |
| 3286 | ALOGD("channel '%s' ~ Dropping event because the channel status is %s", |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3287 | connection->getInputChannelName().c_str(), |
| 3288 | ftl::enum_string(connection->status).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3289 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3290 | return; |
| 3291 | } |
| 3292 | |
| 3293 | // Split a motion event if needed. |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3294 | if (inputTarget.flags.test(InputTarget::Flags::SPLIT)) { |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3295 | LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3296 | "Entry type %s should not have Flags::SPLIT", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3297 | ftl::enum_string(eventEntry->type).c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3298 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3299 | const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry); |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 3300 | if (inputTarget.pointerIds.count() != originalMotionEntry.getPointerCount()) { |
Siarhei Vishniakou | 16e4fa0 | 2023-02-16 17:48:56 -0800 | [diff] [blame] | 3301 | if (!inputTarget.firstDownTimeInTarget.has_value()) { |
| 3302 | logDispatchStateLocked(); |
| 3303 | LOG(FATAL) << "Splitting motion events requires a down time to be set for the " |
| 3304 | "target on connection " |
| 3305 | << connection->getInputChannelName() << " for " |
| 3306 | << originalMotionEntry.getDescription(); |
| 3307 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3308 | std::unique_ptr<MotionEntry> splitMotionEntry = |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3309 | splitMotionEvent(originalMotionEntry, inputTarget.pointerIds, |
| 3310 | inputTarget.firstDownTimeInTarget.value()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3311 | if (!splitMotionEntry) { |
| 3312 | return; // split event was dropped |
| 3313 | } |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3314 | if (splitMotionEntry->action == AMOTION_EVENT_ACTION_CANCEL) { |
| 3315 | std::string reason = std::string("reason=pointer cancel on split window"); |
| 3316 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 3317 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 3318 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3319 | if (DEBUG_FOCUS) { |
| 3320 | ALOGD("channel '%s' ~ Split motion event.", |
| 3321 | connection->getInputChannelName().c_str()); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3322 | logOutboundMotionDetails(" ", *splitMotionEntry); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3323 | } |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3324 | enqueueDispatchEntryAndStartDispatchCycleLocked(currentTime, connection, |
| 3325 | std::move(splitMotionEntry), |
| 3326 | inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3327 | return; |
| 3328 | } |
| 3329 | } |
| 3330 | |
| 3331 | // Not splitting. Enqueue dispatch entries for the event as is. |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3332 | enqueueDispatchEntryAndStartDispatchCycleLocked(currentTime, connection, eventEntry, |
| 3333 | inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3334 | } |
| 3335 | |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3336 | void InputDispatcher::enqueueDispatchEntryAndStartDispatchCycleLocked( |
| 3337 | nsecs_t currentTime, const std::shared_ptr<Connection>& connection, |
| 3338 | std::shared_ptr<const EventEntry> eventEntry, const InputTarget& inputTarget) { |
Prabir Pradhan | 2dac8b8 | 2023-09-06 01:11:51 +0000 | [diff] [blame] | 3339 | ATRACE_NAME_IF(ATRACE_ENABLED(), |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3340 | StringPrintf("enqueueDispatchEntryAndStartDispatchCycleLocked(inputChannel=%s, " |
| 3341 | "id=0x%" PRIx32 ")", |
Prabir Pradhan | 2dac8b8 | 2023-09-06 01:11:51 +0000 | [diff] [blame] | 3342 | connection->getInputChannelName().c_str(), eventEntry->id)); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3343 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 3344 | const bool wasEmpty = connection->outboundQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3345 | |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3346 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3347 | |
| 3348 | // If the outbound queue was previously empty, start the dispatch cycle going. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3349 | if (wasEmpty && !connection->outboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3350 | startDispatchCycleLocked(currentTime, connection); |
| 3351 | } |
| 3352 | } |
| 3353 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3354 | void InputDispatcher::enqueueDispatchEntryLocked(const std::shared_ptr<Connection>& connection, |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 3355 | std::shared_ptr<const EventEntry> eventEntry, |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3356 | const InputTarget& inputTarget) { |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 3357 | const bool isKeyOrMotion = eventEntry->type == EventEntry::Type::KEY || |
| 3358 | eventEntry->type == EventEntry::Type::MOTION; |
| 3359 | if (isKeyOrMotion && !inputTarget.windowHandle && !connection->monitor) { |
| 3360 | LOG(FATAL) << "All InputTargets for non-monitors must be associated with a window; target: " |
| 3361 | << inputTarget << " connection: " << connection->getInputChannelName() |
| 3362 | << " entry: " << eventEntry->getDescription(); |
| 3363 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3364 | // This is a new event. |
| 3365 | // Enqueue a new dispatch entry onto the outbound queue for this connection. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3366 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Prabir Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame] | 3367 | createDispatchEntry(inputTarget, eventEntry, inputTarget.flags, mWindowInfosVsyncId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3368 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3369 | // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a |
| 3370 | // different EventEntry than what was passed in. |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3371 | eventEntry = dispatchEntry->eventEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3372 | // Apply target flags and update the connection's input state. |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3373 | switch (eventEntry->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3374 | case EventEntry::Type::KEY: { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3375 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(*eventEntry); |
| 3376 | if (!connection->inputState.trackKey(keyEntry, keyEntry.flags)) { |
Siarhei Vishniakou | c94dafe | 2023-05-26 10:24:19 -0700 | [diff] [blame] | 3377 | LOG(WARNING) << "channel " << connection->getInputChannelName() |
| 3378 | << "~ dropping inconsistent event: " << *dispatchEntry; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3379 | return; // skip the inconsistent event |
| 3380 | } |
| 3381 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3382 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3383 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3384 | case EventEntry::Type::MOTION: { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3385 | std::shared_ptr<const MotionEntry> resolvedMotion = |
| 3386 | std::static_pointer_cast<const MotionEntry>(eventEntry); |
| 3387 | { |
| 3388 | // Determine the resolved motion entry. |
| 3389 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); |
| 3390 | int32_t resolvedAction = motionEntry.action; |
| 3391 | int32_t resolvedFlags = motionEntry.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3392 | |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3393 | if (inputTarget.dispatchMode == InputTarget::DispatchMode::OUTSIDE) { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3394 | resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3395 | } else if (inputTarget.dispatchMode == InputTarget::DispatchMode::HOVER_EXIT) { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3396 | resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3397 | } else if (inputTarget.dispatchMode == InputTarget::DispatchMode::HOVER_ENTER) { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3398 | resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3399 | } else if (inputTarget.dispatchMode == InputTarget::DispatchMode::SLIPPERY_EXIT) { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3400 | resolvedAction = AMOTION_EVENT_ACTION_CANCEL; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3401 | } else if (inputTarget.dispatchMode == InputTarget::DispatchMode::SLIPPERY_ENTER) { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3402 | resolvedAction = AMOTION_EVENT_ACTION_DOWN; |
| 3403 | } |
| 3404 | if (resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE && |
| 3405 | !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source, |
| 3406 | motionEntry.displayId)) { |
| 3407 | if (DEBUG_DISPATCH_CYCLE) { |
| 3408 | LOG(DEBUG) << "channel '" << connection->getInputChannelName().c_str() |
| 3409 | << "' ~ enqueueDispatchEntryLocked: filling in missing hover " |
| 3410 | "enter event"; |
| 3411 | } |
| 3412 | resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 3413 | } |
| 3414 | |
| 3415 | if (resolvedAction == AMOTION_EVENT_ACTION_CANCEL) { |
| 3416 | resolvedFlags |= AMOTION_EVENT_FLAG_CANCELED; |
| 3417 | } |
| 3418 | if (dispatchEntry->targetFlags.test(InputTarget::Flags::WINDOW_IS_OBSCURED)) { |
| 3419 | resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; |
| 3420 | } |
| 3421 | if (dispatchEntry->targetFlags.test( |
| 3422 | InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED)) { |
| 3423 | resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 3424 | } |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 3425 | if (dispatchEntry->targetFlags.test(InputTarget::Flags::NO_FOCUS_CHANGE)) { |
| 3426 | resolvedFlags |= AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE; |
| 3427 | } |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3428 | |
| 3429 | dispatchEntry->resolvedFlags = resolvedFlags; |
| 3430 | if (resolvedAction != motionEntry.action) { |
Siarhei Vishniakou | e9ef6bc | 2023-12-21 19:47:20 -0800 | [diff] [blame] | 3431 | std::optional<std::vector<PointerProperties>> usingProperties; |
| 3432 | std::optional<std::vector<PointerCoords>> usingCoords; |
| 3433 | if (resolvedAction == AMOTION_EVENT_ACTION_HOVER_EXIT || |
| 3434 | resolvedAction == AMOTION_EVENT_ACTION_CANCEL) { |
| 3435 | // This is a HOVER_EXIT or an ACTION_CANCEL event that was synthesized by |
| 3436 | // the dispatcher, and therefore the coordinates of this event are currently |
| 3437 | // incorrect. These events should use the coordinates of the last dispatched |
| 3438 | // ACTION_MOVE or HOVER_MOVE. We need to query InputState to get this data. |
| 3439 | const bool hovering = resolvedAction == AMOTION_EVENT_ACTION_HOVER_EXIT; |
| 3440 | std::optional<std::pair<std::vector<PointerProperties>, |
| 3441 | std::vector<PointerCoords>>> |
| 3442 | pointerInfo = |
| 3443 | connection->inputState.getPointersOfLastEvent(motionEntry, |
| 3444 | hovering); |
| 3445 | if (pointerInfo) { |
| 3446 | usingProperties = pointerInfo->first; |
| 3447 | usingCoords = pointerInfo->second; |
| 3448 | } |
| 3449 | } |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3450 | // Generate a new MotionEntry with a new eventId using the resolved action and |
| 3451 | // flags. |
Siarhei Vishniakou | e9ef6bc | 2023-12-21 19:47:20 -0800 | [diff] [blame] | 3452 | resolvedMotion = std::make_shared< |
| 3453 | MotionEntry>(mIdGenerator.nextId(), motionEntry.injectionState, |
| 3454 | motionEntry.eventTime, motionEntry.deviceId, |
| 3455 | motionEntry.source, motionEntry.displayId, |
| 3456 | motionEntry.policyFlags, resolvedAction, |
| 3457 | motionEntry.actionButton, resolvedFlags, |
| 3458 | motionEntry.metaState, motionEntry.buttonState, |
| 3459 | motionEntry.classification, motionEntry.edgeFlags, |
| 3460 | motionEntry.xPrecision, motionEntry.yPrecision, |
| 3461 | motionEntry.xCursorPosition, motionEntry.yCursorPosition, |
| 3462 | motionEntry.downTime, |
| 3463 | usingProperties.value_or(motionEntry.pointerProperties), |
| 3464 | usingCoords.value_or(motionEntry.pointerCoords)); |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3465 | if (ATRACE_ENABLED()) { |
| 3466 | std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32 |
| 3467 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3468 | motionEntry.id, resolvedMotion->id); |
| 3469 | ATRACE_NAME(message.c_str()); |
| 3470 | } |
| 3471 | |
| 3472 | // Set the resolved motion entry in the DispatchEntry. |
| 3473 | dispatchEntry->eventEntry = resolvedMotion; |
| 3474 | eventEntry = resolvedMotion; |
| 3475 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3476 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3477 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3478 | // Check if we need to cancel any of the ongoing gestures. We don't support multiple |
| 3479 | // devices being active at the same time in the same window, so if a new device is |
| 3480 | // active, cancel the gesture from the old device. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3481 | std::unique_ptr<EventEntry> cancelEvent = |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3482 | connection->inputState.cancelConflictingInputStream(*resolvedMotion); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3483 | if (cancelEvent != nullptr) { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3484 | LOG(INFO) << "Canceling pointers for device " << resolvedMotion->deviceId << " in " |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3485 | << connection->getInputChannelName() << " with event " |
| 3486 | << cancelEvent->getDescription(); |
| 3487 | std::unique_ptr<DispatchEntry> cancelDispatchEntry = |
| 3488 | createDispatchEntry(inputTarget, std::move(cancelEvent), |
Prabir Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame] | 3489 | ftl::Flags<InputTarget::Flags>(), mWindowInfosVsyncId); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3490 | |
| 3491 | // Send these cancel events to the queue before sending the event from the new |
| 3492 | // device. |
| 3493 | connection->outboundQueue.emplace_back(std::move(cancelDispatchEntry)); |
| 3494 | } |
| 3495 | |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3496 | if (!connection->inputState.trackMotion(*resolvedMotion, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3497 | dispatchEntry->resolvedFlags)) { |
Siarhei Vishniakou | c94dafe | 2023-05-26 10:24:19 -0700 | [diff] [blame] | 3498 | LOG(WARNING) << "channel " << connection->getInputChannelName() |
| 3499 | << "~ dropping inconsistent event: " << *dispatchEntry; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3500 | return; // skip the inconsistent event |
| 3501 | } |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 3502 | if ((dispatchEntry->resolvedFlags & AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) && |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3503 | (resolvedMotion->policyFlags & POLICY_FLAG_TRUSTED)) { |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 3504 | // Skip reporting pointer down outside focus to the policy. |
| 3505 | break; |
| 3506 | } |
| 3507 | |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3508 | dispatchPointerDownOutsideFocus(resolvedMotion->source, resolvedMotion->action, |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 3509 | inputTarget.connection->getToken()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3510 | |
| 3511 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3512 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3513 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3514 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3515 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3516 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3517 | break; |
| 3518 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3519 | case EventEntry::Type::SENSOR: { |
| 3520 | LOG_ALWAYS_FATAL("SENSOR events should not go to apps via input channel"); |
| 3521 | break; |
| 3522 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3523 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 3524 | case EventEntry::Type::DEVICE_RESET: { |
| 3525 | LOG_ALWAYS_FATAL("%s events should not go to apps", |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3526 | ftl::enum_string(eventEntry->type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3527 | break; |
| 3528 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3529 | } |
| 3530 | |
| 3531 | // Remember that we are waiting for this dispatch to complete. |
| 3532 | if (dispatchEntry->hasForegroundTarget()) { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3533 | incrementPendingForegroundDispatches(*eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3534 | } |
| 3535 | |
| 3536 | // Enqueue the dispatch entry. |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 3537 | connection->outboundQueue.emplace_back(std::move(dispatchEntry)); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3538 | traceOutboundQueueLength(*connection); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3539 | } |
| 3540 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3541 | /** |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3542 | * This function is for debugging and metrics collection. It has two roles. |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3543 | * |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3544 | * The first role is to log input interaction with windows, which helps determine what the user was |
| 3545 | * interacting with. For example, if user is touching launcher, we will see an input_interaction log |
| 3546 | * that user started interacting with launcher window, as well as any other window that received |
| 3547 | * that gesture, such as the wallpaper or other spy windows. A new input_interaction is only logged |
| 3548 | * when the set of tokens that received the event changes. It is not logged again as long as the |
| 3549 | * user is interacting with the same windows. |
| 3550 | * |
| 3551 | * The second role is to track input device activity for metrics collection. For each input event, |
| 3552 | * we report the set of UIDs that the input device interacted with to the policy. Unlike for the |
| 3553 | * input_interaction logs, the device interaction is reported even when the set of interaction |
| 3554 | * tokens do not change. |
| 3555 | * |
| 3556 | * For these purposes, we do not count ACTION_OUTSIDE, ACTION_UP and ACTION_CANCEL actions as |
| 3557 | * interaction. This includes up and cancel events for both keys and motions. |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3558 | */ |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3559 | void InputDispatcher::processInteractionsLocked(const EventEntry& entry, |
| 3560 | const std::vector<InputTarget>& targets) { |
| 3561 | int32_t deviceId; |
| 3562 | nsecs_t eventTime; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3563 | // Skip ACTION_UP events, and all events other than keys and motions |
| 3564 | if (entry.type == EventEntry::Type::KEY) { |
| 3565 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 3566 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
| 3567 | return; |
| 3568 | } |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3569 | deviceId = keyEntry.deviceId; |
| 3570 | eventTime = keyEntry.eventTime; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3571 | } else if (entry.type == EventEntry::Type::MOTION) { |
| 3572 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 3573 | if (motionEntry.action == AMOTION_EVENT_ACTION_UP || |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3574 | motionEntry.action == AMOTION_EVENT_ACTION_CANCEL || |
| 3575 | MotionEvent::getActionMasked(motionEntry.action) == AMOTION_EVENT_ACTION_POINTER_UP) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3576 | return; |
| 3577 | } |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3578 | deviceId = motionEntry.deviceId; |
| 3579 | eventTime = motionEntry.eventTime; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3580 | } else { |
| 3581 | return; // Not a key or a motion |
| 3582 | } |
| 3583 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3584 | std::set<gui::Uid> interactionUids; |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 3585 | std::unordered_set<sp<IBinder>, StrongPointerHash<IBinder>> newConnectionTokens; |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3586 | std::vector<std::shared_ptr<Connection>> newConnections; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3587 | for (const InputTarget& target : targets) { |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3588 | if (target.dispatchMode == InputTarget::DispatchMode::OUTSIDE) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3589 | continue; // Skip windows that receive ACTION_OUTSIDE |
| 3590 | } |
| 3591 | |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 3592 | sp<IBinder> token = target.connection->getToken(); |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3593 | newConnectionTokens.insert(std::move(token)); |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 3594 | newConnections.emplace_back(target.connection); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3595 | if (target.windowHandle) { |
| 3596 | interactionUids.emplace(target.windowHandle->getInfo()->ownerUid); |
| 3597 | } |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3598 | } |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3599 | |
| 3600 | auto command = [this, deviceId, eventTime, uids = std::move(interactionUids)]() |
| 3601 | REQUIRES(mLock) { |
| 3602 | scoped_unlock unlock(mLock); |
| 3603 | mPolicy.notifyDeviceInteraction(deviceId, eventTime, uids); |
| 3604 | }; |
| 3605 | postCommandLocked(std::move(command)); |
| 3606 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3607 | if (newConnectionTokens == mInteractionConnectionTokens) { |
| 3608 | return; // no change |
| 3609 | } |
| 3610 | mInteractionConnectionTokens = newConnectionTokens; |
| 3611 | |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3612 | std::string targetList; |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3613 | for (const std::shared_ptr<Connection>& connection : newConnections) { |
Siarhei Vishniakou | 10bfdc9 | 2024-02-01 11:12:57 -0800 | [diff] [blame] | 3614 | targetList += connection->getInputChannelName() + ", "; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3615 | } |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3616 | std::string message = "Interaction with: " + targetList; |
| 3617 | if (targetList.empty()) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3618 | message += "<none>"; |
| 3619 | } |
| 3620 | android_log_event_list(LOGTAG_INPUT_INTERACTION) << message << LOG_ID_EVENTS; |
| 3621 | } |
| 3622 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3623 | void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3624 | const sp<IBinder>& token) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3625 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3626 | uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK; |
| 3627 | if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3628 | return; |
| 3629 | } |
| 3630 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 3631 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3632 | if (focusedToken == token) { |
| 3633 | // ignore since token is focused |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3634 | return; |
| 3635 | } |
| 3636 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3637 | auto command = [this, token]() REQUIRES(mLock) { |
| 3638 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 3639 | mPolicy.onPointerDownOutsideFocus(token); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3640 | }; |
| 3641 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3642 | } |
| 3643 | |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3644 | status_t InputDispatcher::publishMotionEvent(Connection& connection, |
| 3645 | DispatchEntry& dispatchEntry) const { |
| 3646 | const EventEntry& eventEntry = *(dispatchEntry.eventEntry); |
| 3647 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 3648 | |
| 3649 | PointerCoords scaledCoords[MAX_POINTERS]; |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 3650 | const PointerCoords* usingCoords = motionEntry.pointerCoords.data(); |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3651 | |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 3652 | // TODO(b/316355518): Do not modify coords before dispatch. |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3653 | // Set the X and Y offset and X and Y scale depending on the input source. |
| 3654 | if ((motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) && |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3655 | !(dispatchEntry.targetFlags.test(InputTarget::Flags::ZERO_COORDS))) { |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3656 | float globalScaleFactor = dispatchEntry.globalScaleFactor; |
| 3657 | if (globalScaleFactor != 1.0f) { |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 3658 | for (uint32_t i = 0; i < motionEntry.getPointerCount(); i++) { |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3659 | scaledCoords[i] = motionEntry.pointerCoords[i]; |
| 3660 | // Don't apply window scale here since we don't want scale to affect raw |
| 3661 | // coordinates. The scale will be sent back to the client and applied |
| 3662 | // later when requesting relative coordinates. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 3663 | scaledCoords[i].scale(globalScaleFactor, /*windowXScale=*/1, /*windowYScale=*/1); |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3664 | } |
| 3665 | usingCoords = scaledCoords; |
| 3666 | } |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3667 | } else if (dispatchEntry.targetFlags.test(InputTarget::Flags::ZERO_COORDS)) { |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3668 | // We don't want the dispatch target to know the coordinates |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 3669 | for (uint32_t i = 0; i < motionEntry.getPointerCount(); i++) { |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3670 | scaledCoords[i].clear(); |
| 3671 | } |
| 3672 | usingCoords = scaledCoords; |
| 3673 | } |
| 3674 | |
| 3675 | std::array<uint8_t, 32> hmac = getSignature(motionEntry, dispatchEntry); |
| 3676 | |
| 3677 | // Publish the motion event. |
| 3678 | return connection.inputPublisher |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3679 | .publishMotionEvent(dispatchEntry.seq, motionEntry.id, motionEntry.deviceId, |
| 3680 | motionEntry.source, motionEntry.displayId, std::move(hmac), |
| 3681 | motionEntry.action, motionEntry.actionButton, |
| 3682 | dispatchEntry.resolvedFlags, motionEntry.edgeFlags, |
| 3683 | motionEntry.metaState, motionEntry.buttonState, |
| 3684 | motionEntry.classification, dispatchEntry.transform, |
| 3685 | motionEntry.xPrecision, motionEntry.yPrecision, |
| 3686 | motionEntry.xCursorPosition, motionEntry.yCursorPosition, |
| 3687 | dispatchEntry.rawTransform, motionEntry.downTime, |
| 3688 | motionEntry.eventTime, motionEntry.getPointerCount(), |
| 3689 | motionEntry.pointerProperties.data(), usingCoords); |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3690 | } |
| 3691 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3692 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3693 | const std::shared_ptr<Connection>& connection) { |
Prabir Pradhan | 2dac8b8 | 2023-09-06 01:11:51 +0000 | [diff] [blame] | 3694 | ATRACE_NAME_IF(ATRACE_ENABLED(), |
| 3695 | StringPrintf("startDispatchCycleLocked(inputChannel=%s)", |
| 3696 | connection->getInputChannelName().c_str())); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3697 | if (DEBUG_DISPATCH_CYCLE) { |
| 3698 | ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str()); |
| 3699 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3700 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3701 | while (connection->status == Connection::Status::NORMAL && !connection->outboundQueue.empty()) { |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 3702 | std::unique_ptr<DispatchEntry>& dispatchEntry = connection->outboundQueue.front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3703 | dispatchEntry->deliveryTime = currentTime; |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3704 | const std::chrono::nanoseconds timeout = getDispatchingTimeoutLocked(connection); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3705 | dispatchEntry->timeoutTime = currentTime + timeout.count(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3706 | |
| 3707 | // Publish the event. |
| 3708 | status_t status; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3709 | const EventEntry& eventEntry = *(dispatchEntry->eventEntry); |
| 3710 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3711 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3712 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 3713 | std::array<uint8_t, 32> hmac = getSignature(keyEntry, *dispatchEntry); |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 3714 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
Siarhei Vishniakou | 827d1ac | 2023-07-21 16:37:51 -0700 | [diff] [blame] | 3715 | LOG(INFO) << "Publishing " << *dispatchEntry << " to " |
| 3716 | << connection->getInputChannelName(); |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 3717 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3718 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3719 | // Publish the key event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3720 | status = connection->inputPublisher |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3721 | .publishKeyEvent(dispatchEntry->seq, keyEntry.id, |
| 3722 | keyEntry.deviceId, keyEntry.source, |
| 3723 | keyEntry.displayId, std::move(hmac), |
| 3724 | keyEntry.action, dispatchEntry->resolvedFlags, |
| 3725 | keyEntry.keyCode, keyEntry.scanCode, |
| 3726 | keyEntry.metaState, keyEntry.repeatCount, |
| 3727 | keyEntry.downTime, keyEntry.eventTime); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 3728 | if (mTracer) { |
| 3729 | mTracer->traceEventDispatch(*dispatchEntry, keyEntry.traceTracker.get()); |
| 3730 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3731 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3732 | } |
| 3733 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3734 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 3735 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
Siarhei Vishniakou | 827d1ac | 2023-07-21 16:37:51 -0700 | [diff] [blame] | 3736 | LOG(INFO) << "Publishing " << *dispatchEntry << " to " |
| 3737 | << connection->getInputChannelName(); |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 3738 | } |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 3739 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3740 | status = publishMotionEvent(*connection, *dispatchEntry); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 3741 | if (mTracer) { |
| 3742 | mTracer->traceEventDispatch(*dispatchEntry, motionEntry.traceTracker.get()); |
| 3743 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3744 | break; |
| 3745 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3746 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3747 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3748 | const FocusEntry& focusEntry = static_cast<const FocusEntry&>(eventEntry); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3749 | status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3750 | focusEntry.id, |
Antonio Kantek | 3cfec7b | 2021-11-05 18:26:17 -0700 | [diff] [blame] | 3751 | focusEntry.hasFocus); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3752 | break; |
| 3753 | } |
| 3754 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3755 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 3756 | const TouchModeEntry& touchModeEntry = |
| 3757 | static_cast<const TouchModeEntry&>(eventEntry); |
| 3758 | status = connection->inputPublisher |
| 3759 | .publishTouchModeEvent(dispatchEntry->seq, touchModeEntry.id, |
| 3760 | touchModeEntry.inTouchMode); |
| 3761 | |
| 3762 | break; |
| 3763 | } |
| 3764 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3765 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 3766 | const auto& captureEntry = |
| 3767 | static_cast<const PointerCaptureChangedEntry&>(eventEntry); |
| 3768 | status = connection->inputPublisher |
| 3769 | .publishCaptureEvent(dispatchEntry->seq, captureEntry.id, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 3770 | captureEntry.pointerCaptureRequest.enable); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3771 | break; |
| 3772 | } |
| 3773 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3774 | case EventEntry::Type::DRAG: { |
| 3775 | const DragEntry& dragEntry = static_cast<const DragEntry&>(eventEntry); |
| 3776 | status = connection->inputPublisher.publishDragEvent(dispatchEntry->seq, |
| 3777 | dragEntry.id, dragEntry.x, |
| 3778 | dragEntry.y, |
| 3779 | dragEntry.isExiting); |
| 3780 | break; |
| 3781 | } |
| 3782 | |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3783 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3784 | case EventEntry::Type::DEVICE_RESET: |
| 3785 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3786 | LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3787 | ftl::enum_string(eventEntry.type).c_str()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3788 | return; |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3789 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3790 | } |
| 3791 | |
| 3792 | // Check the result. |
| 3793 | if (status) { |
| 3794 | if (status == WOULD_BLOCK) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3795 | if (connection->waitQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3796 | ALOGE("channel '%s' ~ Could not publish event because the pipe is full. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3797 | "This is unexpected because the wait queue is empty, so the pipe " |
| 3798 | "should be empty and we shouldn't have any problems writing an " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3799 | "event to it, status=%s(%d)", |
| 3800 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3801 | status); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 3802 | abortBrokenDispatchCycleLocked(currentTime, connection, /*notify=*/true); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3803 | } else { |
| 3804 | // Pipe is full and we are waiting for the app to finish process some events |
| 3805 | // before sending more events to it. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3806 | if (DEBUG_DISPATCH_CYCLE) { |
| 3807 | ALOGD("channel '%s' ~ Could not publish event because the pipe is full, " |
| 3808 | "waiting for the application to catch up", |
| 3809 | connection->getInputChannelName().c_str()); |
| 3810 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3811 | } |
| 3812 | } else { |
| 3813 | ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3814 | "status=%s(%d)", |
| 3815 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3816 | status); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 3817 | abortBrokenDispatchCycleLocked(currentTime, connection, /*notify=*/true); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3818 | } |
| 3819 | return; |
| 3820 | } |
| 3821 | |
| 3822 | // Re-enqueue the event on the wait queue. |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 3823 | const nsecs_t timeoutTime = dispatchEntry->timeoutTime; |
| 3824 | connection->waitQueue.emplace_back(std::move(dispatchEntry)); |
| 3825 | connection->outboundQueue.erase(connection->outboundQueue.begin()); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3826 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3827 | if (connection->responsive) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 3828 | mAnrTracker.insert(timeoutTime, connection->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3829 | } |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3830 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3831 | } |
| 3832 | } |
| 3833 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3834 | std::array<uint8_t, 32> InputDispatcher::sign(const VerifiedInputEvent& event) const { |
| 3835 | size_t size; |
| 3836 | switch (event.type) { |
| 3837 | case VerifiedInputEvent::Type::KEY: { |
| 3838 | size = sizeof(VerifiedKeyEvent); |
| 3839 | break; |
| 3840 | } |
| 3841 | case VerifiedInputEvent::Type::MOTION: { |
| 3842 | size = sizeof(VerifiedMotionEvent); |
| 3843 | break; |
| 3844 | } |
| 3845 | } |
| 3846 | const uint8_t* start = reinterpret_cast<const uint8_t*>(&event); |
| 3847 | return mHmacKeyManager.sign(start, size); |
| 3848 | } |
| 3849 | |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3850 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3851 | const MotionEntry& motionEntry, const DispatchEntry& dispatchEntry) const { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3852 | const int32_t actionMasked = MotionEvent::getActionMasked(motionEntry.action); |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3853 | if (actionMasked != AMOTION_EVENT_ACTION_UP && actionMasked != AMOTION_EVENT_ACTION_DOWN) { |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3854 | // Only sign events up and down events as the purely move events |
| 3855 | // are tied to their up/down counterparts so signing would be redundant. |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3856 | return INVALID_HMAC; |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3857 | } |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3858 | |
| 3859 | VerifiedMotionEvent verifiedEvent = |
| 3860 | verifiedMotionEventFromMotionEntry(motionEntry, dispatchEntry.rawTransform); |
| 3861 | verifiedEvent.actionMasked = actionMasked; |
| 3862 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS; |
| 3863 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3864 | } |
| 3865 | |
| 3866 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3867 | const KeyEntry& keyEntry, const DispatchEntry& dispatchEntry) const { |
| 3868 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(keyEntry); |
| 3869 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_KEY_EVENT_FLAGS; |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3870 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3871 | } |
| 3872 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3873 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3874 | const std::shared_ptr<Connection>& connection, |
| 3875 | uint32_t seq, bool handled, nsecs_t consumeTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3876 | if (DEBUG_DISPATCH_CYCLE) { |
| 3877 | ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s", |
| 3878 | connection->getInputChannelName().c_str(), seq, toString(handled)); |
| 3879 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3880 | |
Prabir Pradhan | 98ca4a2 | 2024-01-09 23:51:50 +0000 | [diff] [blame] | 3881 | if (connection->status != Connection::Status::NORMAL) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3882 | return; |
| 3883 | } |
| 3884 | |
| 3885 | // Notify other system components and prepare to start the next dispatch cycle. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3886 | auto command = [this, currentTime, connection, seq, handled, consumeTime]() REQUIRES(mLock) { |
| 3887 | doDispatchCycleFinishedCommand(currentTime, connection, seq, handled, consumeTime); |
| 3888 | }; |
| 3889 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3890 | } |
| 3891 | |
| 3892 | void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3893 | const std::shared_ptr<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3894 | bool notify) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3895 | if (DEBUG_DISPATCH_CYCLE) { |
Siarhei Vishniakou | 827d1ac | 2023-07-21 16:37:51 -0700 | [diff] [blame] | 3896 | LOG(INFO) << "channel '" << connection->getInputChannelName() << "'~ " << __func__ |
| 3897 | << " - notify=" << toString(notify); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3898 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3899 | |
| 3900 | // Clear the dispatch queues. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3901 | drainDispatchQueue(connection->outboundQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3902 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3903 | drainDispatchQueue(connection->waitQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3904 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3905 | |
| 3906 | // The connection appears to be unrecoverably broken. |
| 3907 | // Ignore already broken or zombie connections. |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3908 | if (connection->status == Connection::Status::NORMAL) { |
| 3909 | connection->status = Connection::Status::BROKEN; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3910 | |
| 3911 | if (notify) { |
| 3912 | // Notify other system components. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3913 | ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", |
| 3914 | connection->getInputChannelName().c_str()); |
| 3915 | |
| 3916 | auto command = [this, connection]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3917 | scoped_unlock unlock(mLock); |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 3918 | mPolicy.notifyInputChannelBroken(connection->getToken()); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3919 | }; |
| 3920 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3921 | } |
| 3922 | } |
| 3923 | } |
| 3924 | |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 3925 | void InputDispatcher::drainDispatchQueue(std::deque<std::unique_ptr<DispatchEntry>>& queue) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3926 | while (!queue.empty()) { |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 3927 | releaseDispatchEntry(std::move(queue.front())); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3928 | queue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3929 | } |
| 3930 | } |
| 3931 | |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 3932 | void InputDispatcher::releaseDispatchEntry(std::unique_ptr<DispatchEntry> dispatchEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3933 | if (dispatchEntry->hasForegroundTarget()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3934 | decrementPendingForegroundDispatches(*(dispatchEntry->eventEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3935 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3936 | } |
| 3937 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3938 | int InputDispatcher::handleReceiveCallback(int events, sp<IBinder> connectionToken) { |
| 3939 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3940 | std::shared_ptr<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3941 | if (connection == nullptr) { |
| 3942 | ALOGW("Received looper callback for unknown input channel token %p. events=0x%x", |
| 3943 | connectionToken.get(), events); |
| 3944 | return 0; // remove the callback |
| 3945 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3946 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3947 | bool notify; |
| 3948 | if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { |
| 3949 | if (!(events & ALOOPER_EVENT_INPUT)) { |
| 3950 | ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. " |
| 3951 | "events=0x%x", |
| 3952 | connection->getInputChannelName().c_str(), events); |
| 3953 | return 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3954 | } |
| 3955 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3956 | nsecs_t currentTime = now(); |
| 3957 | bool gotOne = false; |
| 3958 | status_t status = OK; |
| 3959 | for (;;) { |
| 3960 | Result<InputPublisher::ConsumerResponse> result = |
| 3961 | connection->inputPublisher.receiveConsumerResponse(); |
| 3962 | if (!result.ok()) { |
| 3963 | status = result.error().code(); |
| 3964 | break; |
| 3965 | } |
| 3966 | |
| 3967 | if (std::holds_alternative<InputPublisher::Finished>(*result)) { |
| 3968 | const InputPublisher::Finished& finish = |
| 3969 | std::get<InputPublisher::Finished>(*result); |
| 3970 | finishDispatchCycleLocked(currentTime, connection, finish.seq, finish.handled, |
| 3971 | finish.consumeTime); |
| 3972 | } else if (std::holds_alternative<InputPublisher::Timeline>(*result)) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3973 | if (shouldReportMetricsForConnection(*connection)) { |
| 3974 | const InputPublisher::Timeline& timeline = |
| 3975 | std::get<InputPublisher::Timeline>(*result); |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 3976 | mLatencyTracker.trackGraphicsLatency(timeline.inputEventId, |
| 3977 | connection->getToken(), |
| 3978 | std::move(timeline.graphicsTimeline)); |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3979 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3980 | } |
| 3981 | gotOne = true; |
| 3982 | } |
| 3983 | if (gotOne) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3984 | runCommandsLockedInterruptable(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3985 | if (status == WOULD_BLOCK) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3986 | return 1; |
| 3987 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3988 | } |
| 3989 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3990 | notify = status != DEAD_OBJECT || !connection->monitor; |
| 3991 | if (notify) { |
| 3992 | ALOGE("channel '%s' ~ Failed to receive finished signal. status=%s(%d)", |
| 3993 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3994 | status); |
| 3995 | } |
| 3996 | } else { |
| 3997 | // Monitor channels are never explicitly unregistered. |
| 3998 | // We do it automatically when the remote endpoint is closed so don't warn about them. |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 3999 | const bool stillHaveWindowHandle = getWindowHandleLocked(connection->getToken()) != nullptr; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4000 | notify = !connection->monitor && stillHaveWindowHandle; |
| 4001 | if (notify) { |
| 4002 | ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred. events=0x%x", |
| 4003 | connection->getInputChannelName().c_str(), events); |
| 4004 | } |
| 4005 | } |
| 4006 | |
| 4007 | // Remove the channel. |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 4008 | removeInputChannelLocked(connection->getToken(), notify); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4009 | return 0; // remove the callback |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4010 | } |
| 4011 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4012 | void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4013 | const CancelationOptions& options) { |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 4014 | // Cancel windows (i.e. non-monitors). |
| 4015 | // A channel must have at least one window to receive any input. If a window was removed, the |
| 4016 | // event streams directed to the window will already have been canceled during window removal. |
| 4017 | // So there is no need to generate cancellations for connections without any windows. |
| 4018 | const auto [cancelPointers, cancelNonPointers] = expandCancellationMode(options.mode); |
| 4019 | // Generate cancellations for touched windows first. This is to avoid generating cancellations |
| 4020 | // through a non-touched window if there are more than one window for an input channel. |
| 4021 | if (cancelPointers) { |
| 4022 | for (const auto& [displayId, touchState] : mTouchStatesByDisplay) { |
| 4023 | if (options.displayId.has_value() && options.displayId != displayId) { |
| 4024 | continue; |
| 4025 | } |
| 4026 | for (const auto& touchedWindow : touchState.windows) { |
| 4027 | synthesizeCancelationEventsForWindowLocked(touchedWindow.windowHandle, options); |
| 4028 | } |
| 4029 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4030 | } |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 4031 | // Follow up by generating cancellations for all windows, because we don't explicitly track |
| 4032 | // the windows that have an ongoing focus event stream. |
| 4033 | if (cancelNonPointers) { |
| 4034 | for (const auto& [_, handles] : mWindowHandlesByDisplay) { |
| 4035 | for (const auto& windowHandle : handles) { |
| 4036 | synthesizeCancelationEventsForWindowLocked(windowHandle, options); |
| 4037 | } |
| 4038 | } |
| 4039 | } |
| 4040 | |
| 4041 | // Cancel monitors. |
| 4042 | synthesizeCancelationEventsForMonitorsLocked(options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4043 | } |
| 4044 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4045 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 4046 | const CancelationOptions& options) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 4047 | for (const auto& [_, monitors] : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4048 | for (const Monitor& monitor : monitors) { |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 4049 | synthesizeCancelationEventsForConnectionLocked(monitor.connection, options, |
| 4050 | /*window=*/nullptr); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4051 | } |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 4052 | } |
| 4053 | } |
| 4054 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 4055 | void InputDispatcher::synthesizeCancelationEventsForWindowLocked( |
| 4056 | const sp<WindowInfoHandle>& windowHandle, const CancelationOptions& options, |
| 4057 | const std::shared_ptr<Connection>& connection) { |
| 4058 | if (windowHandle == nullptr) { |
| 4059 | LOG(FATAL) << __func__ << ": Window handle must not be null"; |
| 4060 | } |
| 4061 | if (connection) { |
| 4062 | // The connection can be optionally provided to avoid multiple lookups. |
| 4063 | if (windowHandle->getToken() != connection->getToken()) { |
| 4064 | LOG(FATAL) << __func__ |
| 4065 | << ": Wrong connection provided for window: " << windowHandle->getName(); |
| 4066 | } |
| 4067 | } |
| 4068 | |
| 4069 | std::shared_ptr<Connection> resolvedConnection = |
| 4070 | connection ? connection : getConnectionLocked(windowHandle->getToken()); |
| 4071 | if (!resolvedConnection) { |
| 4072 | LOG(DEBUG) << __func__ << "No connection found for window: " << windowHandle->getName(); |
| 4073 | return; |
| 4074 | } |
| 4075 | synthesizeCancelationEventsForConnectionLocked(resolvedConnection, options, windowHandle); |
| 4076 | } |
| 4077 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4078 | void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 4079 | const std::shared_ptr<Connection>& connection, const CancelationOptions& options, |
| 4080 | const sp<WindowInfoHandle>& window) { |
| 4081 | if (!connection->monitor && window == nullptr) { |
| 4082 | LOG(FATAL) << __func__ |
| 4083 | << ": Cannot send event to non-monitor channel without a window - channel: " |
| 4084 | << connection->getInputChannelName(); |
| 4085 | } |
Prabir Pradhan | b13da8f | 2024-01-09 23:10:13 +0000 | [diff] [blame] | 4086 | if (connection->status != Connection::Status::NORMAL) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4087 | return; |
| 4088 | } |
| 4089 | |
| 4090 | nsecs_t currentTime = now(); |
| 4091 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4092 | std::vector<std::unique_ptr<EventEntry>> cancelationEvents = |
Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 4093 | connection->inputState.synthesizeCancelationEvents(currentTime, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4094 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4095 | if (cancelationEvents.empty()) { |
| 4096 | return; |
| 4097 | } |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 4098 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4099 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 4100 | 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] | 4101 | "with reality: %s, mode=%s.", |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4102 | connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason, |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 4103 | ftl::enum_string(options.mode).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4104 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4105 | |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 4106 | std::string reason = std::string("reason=").append(options.reason); |
| 4107 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 4108 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 4109 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 4110 | const bool wasEmpty = connection->outboundQueue.empty(); |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 4111 | // The target to use if we don't find a window associated with the channel. |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 4112 | const InputTarget fallbackTarget{.connection = connection}; |
| 4113 | const auto& token = connection->getToken(); |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 4114 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4115 | for (size_t i = 0; i < cancelationEvents.size(); i++) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4116 | std::unique_ptr<EventEntry> cancelationEventEntry = std::move(cancelationEvents[i]); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4117 | std::vector<InputTarget> targets{}; |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4118 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4119 | switch (cancelationEventEntry->type) { |
| 4120 | case EventEntry::Type::KEY: { |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4121 | const auto& keyEntry = static_cast<const KeyEntry&>(*cancelationEventEntry); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 4122 | if (window) { |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 4123 | addWindowTargetLocked(window, InputTarget::DispatchMode::AS_IS, |
| 4124 | /*targetFlags=*/{}, keyEntry.downTime, targets); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4125 | } else { |
| 4126 | targets.emplace_back(fallbackTarget); |
| 4127 | } |
| 4128 | logOutboundKeyDetails("cancel - ", keyEntry); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4129 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4130 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4131 | case EventEntry::Type::MOTION: { |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4132 | const auto& motionEntry = static_cast<const MotionEntry&>(*cancelationEventEntry); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 4133 | if (window) { |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4134 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4135 | for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.getPointerCount(); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4136 | pointerIndex++) { |
| 4137 | pointerIds.set(motionEntry.pointerProperties[pointerIndex].id); |
| 4138 | } |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 4139 | if (mDragState && mDragState->dragWindow->getToken() == token && |
| 4140 | pointerIds.test(mDragState->pointerId)) { |
| 4141 | LOG(INFO) << __func__ |
| 4142 | << ": Canceling drag and drop because the pointers for the drag " |
| 4143 | "window are being canceled."; |
| 4144 | sendDropWindowCommandLocked(nullptr, /*x=*/0, /*y=*/0); |
| 4145 | mDragState.reset(); |
| 4146 | } |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 4147 | addPointerWindowTargetLocked(window, InputTarget::DispatchMode::AS_IS, |
| 4148 | ftl::Flags<InputTarget::Flags>(), pointerIds, |
| 4149 | motionEntry.downTime, targets); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4150 | } else { |
| 4151 | targets.emplace_back(fallbackTarget); |
| 4152 | const auto it = mDisplayInfos.find(motionEntry.displayId); |
| 4153 | if (it != mDisplayInfos.end()) { |
| 4154 | targets.back().displayTransform = it->second.transform; |
| 4155 | targets.back().setDefaultPointerTransform(it->second.transform); |
| 4156 | } |
| 4157 | } |
| 4158 | logOutboundMotionDetails("cancel - ", motionEntry); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4159 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4160 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4161 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 4162 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4163 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 4164 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4165 | LOG_ALWAYS_FATAL("Canceling %s events is not supported", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 4166 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4167 | break; |
| 4168 | } |
| 4169 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4170 | case EventEntry::Type::DEVICE_RESET: |
| 4171 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4172 | 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] | 4173 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4174 | break; |
| 4175 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4176 | } |
| 4177 | |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4178 | if (targets.size() != 1) LOG(FATAL) << __func__ << ": InputTarget not created"; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 4179 | enqueueDispatchEntryLocked(connection, std::move(cancelationEventEntry), targets[0]); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4180 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4181 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 4182 | // If the outbound queue was previously empty, start the dispatch cycle going. |
| 4183 | if (wasEmpty && !connection->outboundQueue.empty()) { |
| 4184 | startDispatchCycleLocked(currentTime, connection); |
| 4185 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4186 | } |
| 4187 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4188 | void InputDispatcher::synthesizePointerDownEventsForConnectionLocked( |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 4189 | const nsecs_t downTime, const std::shared_ptr<Connection>& connection, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4190 | ftl::Flags<InputTarget::Flags> targetFlags) { |
Prabir Pradhan | 98ca4a2 | 2024-01-09 23:51:50 +0000 | [diff] [blame] | 4191 | if (connection->status != Connection::Status::NORMAL) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4192 | return; |
| 4193 | } |
| 4194 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4195 | std::vector<std::unique_ptr<EventEntry>> downEvents = |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4196 | connection->inputState.synthesizePointerDownEvents(downTime); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4197 | |
| 4198 | if (downEvents.empty()) { |
| 4199 | return; |
| 4200 | } |
| 4201 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4202 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4203 | ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.", |
| 4204 | connection->getInputChannelName().c_str(), downEvents.size()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4205 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4206 | |
Prabir Pradhan | feca057 | 2024-02-06 00:43:11 +0000 | [diff] [blame] | 4207 | const auto [_, touchedWindowState, displayId] = |
| 4208 | findTouchStateWindowAndDisplayLocked(connection->getToken()); |
| 4209 | if (touchedWindowState == nullptr) { |
| 4210 | LOG(FATAL) << __func__ << ": Touch state is out of sync: No touched window for token"; |
| 4211 | } |
| 4212 | const auto& windowHandle = touchedWindowState->windowHandle; |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4213 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 4214 | const bool wasEmpty = connection->outboundQueue.empty(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4215 | for (std::unique_ptr<EventEntry>& downEventEntry : downEvents) { |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 4216 | std::vector<InputTarget> targets{}; |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4217 | switch (downEventEntry->type) { |
| 4218 | case EventEntry::Type::MOTION: { |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 4219 | const auto& motionEntry = static_cast<const MotionEntry&>(*downEventEntry); |
| 4220 | if (windowHandle != nullptr) { |
| 4221 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4222 | for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.getPointerCount(); |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 4223 | pointerIndex++) { |
| 4224 | pointerIds.set(motionEntry.pointerProperties[pointerIndex].id); |
| 4225 | } |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 4226 | addPointerWindowTargetLocked(windowHandle, InputTarget::DispatchMode::AS_IS, |
| 4227 | targetFlags, pointerIds, motionEntry.downTime, |
| 4228 | targets); |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 4229 | } else { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 4230 | targets.emplace_back( |
| 4231 | InputTarget{.connection = connection, .flags = targetFlags}); |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 4232 | const auto it = mDisplayInfos.find(motionEntry.displayId); |
| 4233 | if (it != mDisplayInfos.end()) { |
| 4234 | targets.back().displayTransform = it->second.transform; |
| 4235 | targets.back().setDefaultPointerTransform(it->second.transform); |
| 4236 | } |
| 4237 | } |
| 4238 | logOutboundMotionDetails("down - ", motionEntry); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4239 | break; |
| 4240 | } |
| 4241 | |
| 4242 | case EventEntry::Type::KEY: |
| 4243 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 4244 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4245 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4246 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4247 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4248 | case EventEntry::Type::SENSOR: |
| 4249 | case EventEntry::Type::DRAG: { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4250 | 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] | 4251 | ftl::enum_string(downEventEntry->type).c_str()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4252 | break; |
| 4253 | } |
| 4254 | } |
| 4255 | |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 4256 | if (targets.size() != 1) LOG(FATAL) << __func__ << ": InputTarget not created"; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 4257 | enqueueDispatchEntryLocked(connection, std::move(downEventEntry), targets[0]); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4258 | } |
| 4259 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 4260 | // If the outbound queue was previously empty, start the dispatch cycle going. |
| 4261 | if (wasEmpty && !connection->outboundQueue.empty()) { |
| 4262 | startDispatchCycleLocked(downTime, connection); |
| 4263 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4264 | } |
| 4265 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4266 | std::unique_ptr<MotionEntry> InputDispatcher::splitMotionEvent( |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 4267 | const MotionEntry& originalMotionEntry, std::bitset<MAX_POINTER_ID + 1> pointerIds, |
| 4268 | nsecs_t splitDownTime) { |
| 4269 | ALOG_ASSERT(pointerIds.any()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4270 | |
| 4271 | uint32_t splitPointerIndexMap[MAX_POINTERS]; |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4272 | std::vector<PointerProperties> splitPointerProperties; |
| 4273 | std::vector<PointerCoords> splitPointerCoords; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4274 | |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4275 | uint32_t originalPointerCount = originalMotionEntry.getPointerCount(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4276 | uint32_t splitPointerCount = 0; |
| 4277 | |
| 4278 | for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4279 | originalPointerIndex++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4280 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 4281 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4282 | uint32_t pointerId = uint32_t(pointerProperties.id); |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 4283 | if (pointerIds.test(pointerId)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4284 | splitPointerIndexMap[splitPointerCount] = originalPointerIndex; |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4285 | splitPointerProperties.push_back(pointerProperties); |
| 4286 | splitPointerCoords.push_back(originalMotionEntry.pointerCoords[originalPointerIndex]); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4287 | splitPointerCount += 1; |
| 4288 | } |
| 4289 | } |
| 4290 | |
| 4291 | if (splitPointerCount != pointerIds.count()) { |
| 4292 | // This is bad. We are missing some of the pointers that we expected to deliver. |
| 4293 | // Most likely this indicates that we received an ACTION_MOVE events that has |
| 4294 | // different pointer ids than we expected based on the previous ACTION_DOWN |
| 4295 | // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers |
| 4296 | // in this way. |
| 4297 | ALOGW("Dropping split motion event because the pointer count is %d but " |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 4298 | "we expected there to be %zu pointers. This probably means we received " |
Siarhei Vishniakou | 16e4fa0 | 2023-02-16 17:48:56 -0800 | [diff] [blame] | 4299 | "a broken sequence of pointer ids from the input device: %s", |
| 4300 | splitPointerCount, pointerIds.count(), originalMotionEntry.getDescription().c_str()); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4301 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4302 | } |
| 4303 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 4304 | int32_t action = originalMotionEntry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4305 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4306 | if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN || |
| 4307 | maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
Siarhei Vishniakou | 5b9766d | 2023-07-18 14:06:29 -0700 | [diff] [blame] | 4308 | int32_t originalPointerIndex = MotionEvent::getActionIndex(action); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4309 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 4310 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4311 | uint32_t pointerId = uint32_t(pointerProperties.id); |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 4312 | if (pointerIds.test(pointerId)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4313 | if (pointerIds.count() == 1) { |
| 4314 | // The first/last pointer went down/up. |
| 4315 | action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4316 | ? AMOTION_EVENT_ACTION_DOWN |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4317 | : (originalMotionEntry.flags & AMOTION_EVENT_FLAG_CANCELED) != 0 |
| 4318 | ? AMOTION_EVENT_ACTION_CANCEL |
| 4319 | : AMOTION_EVENT_ACTION_UP; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4320 | } else { |
| 4321 | // A secondary pointer went down/up. |
| 4322 | uint32_t splitPointerIndex = 0; |
| 4323 | while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { |
| 4324 | splitPointerIndex += 1; |
| 4325 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4326 | action = maskedAction | |
| 4327 | (splitPointerIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4328 | } |
| 4329 | } else { |
| 4330 | // An unrelated pointer changed. |
| 4331 | action = AMOTION_EVENT_ACTION_MOVE; |
| 4332 | } |
| 4333 | } |
| 4334 | |
Siarhei Vishniakou | 59e302b | 2023-06-05 08:04:53 -0700 | [diff] [blame] | 4335 | if (action == AMOTION_EVENT_ACTION_DOWN && splitDownTime != originalMotionEntry.eventTime) { |
| 4336 | logDispatchStateLocked(); |
| 4337 | LOG_ALWAYS_FATAL("Split motion event has mismatching downTime and eventTime for " |
| 4338 | "ACTION_DOWN, motionEntry=%s, splitDownTime=%" PRId64, |
| 4339 | originalMotionEntry.getDescription().c_str(), splitDownTime); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4340 | } |
| 4341 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4342 | int32_t newId = mIdGenerator.nextId(); |
Prabir Pradhan | 2dac8b8 | 2023-09-06 01:11:51 +0000 | [diff] [blame] | 4343 | ATRACE_NAME_IF(ATRACE_ENABLED(), |
| 4344 | StringPrintf("Split MotionEvent(id=0x%" PRIx32 ") to MotionEvent(id=0x%" PRIx32 |
| 4345 | ").", |
| 4346 | originalMotionEntry.id, newId)); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4347 | std::unique_ptr<MotionEntry> splitMotionEntry = |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4348 | std::make_unique<MotionEntry>(newId, originalMotionEntry.injectionState, |
| 4349 | originalMotionEntry.eventTime, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4350 | originalMotionEntry.deviceId, originalMotionEntry.source, |
| 4351 | originalMotionEntry.displayId, |
| 4352 | originalMotionEntry.policyFlags, action, |
| 4353 | originalMotionEntry.actionButton, |
| 4354 | originalMotionEntry.flags, originalMotionEntry.metaState, |
| 4355 | originalMotionEntry.buttonState, |
| 4356 | originalMotionEntry.classification, |
| 4357 | originalMotionEntry.edgeFlags, |
| 4358 | originalMotionEntry.xPrecision, |
| 4359 | originalMotionEntry.yPrecision, |
| 4360 | originalMotionEntry.xCursorPosition, |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4361 | originalMotionEntry.yCursorPosition, splitDownTime, |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4362 | splitPointerProperties, splitPointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4363 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4364 | return splitMotionEntry; |
| 4365 | } |
| 4366 | |
Asmita Poddar | dd9a6cd | 2023-09-26 15:35:12 +0000 | [diff] [blame] | 4367 | void InputDispatcher::notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs& args) { |
| 4368 | std::scoped_lock _l(mLock); |
| 4369 | mLatencyTracker.setInputDevices(args.inputDeviceInfos); |
| 4370 | } |
| 4371 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4372 | void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4373 | if (debugInboundEventDetails()) { |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4374 | ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args.eventTime); |
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 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4377 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4378 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4379 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4380 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4381 | std::unique_ptr<ConfigurationChangedEntry> newEntry = |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4382 | std::make_unique<ConfigurationChangedEntry>(args.id, args.eventTime); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4383 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4384 | } // release lock |
| 4385 | |
| 4386 | if (needWake) { |
| 4387 | mLooper->wake(); |
| 4388 | } |
| 4389 | } |
| 4390 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4391 | void InputDispatcher::notifyKey(const NotifyKeyArgs& args) { |
Prabir Pradhan | 96282b0 | 2023-02-24 22:36:17 +0000 | [diff] [blame] | 4392 | ALOGD_IF(debugInboundEventDetails(), |
| 4393 | "notifyKey - id=%" PRIx32 ", eventTime=%" PRId64 |
| 4394 | ", deviceId=%d, source=%s, displayId=%" PRId32 |
| 4395 | "policyFlags=0x%x, action=%s, flags=0x%x, keyCode=%s, scanCode=0x%x, metaState=0x%x, " |
| 4396 | "downTime=%" PRId64, |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4397 | args.id, args.eventTime, args.deviceId, inputEventSourceToString(args.source).c_str(), |
| 4398 | args.displayId, args.policyFlags, KeyEvent::actionToString(args.action), args.flags, |
| 4399 | KeyEvent::getLabel(args.keyCode), args.scanCode, args.metaState, args.downTime); |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4400 | Result<void> keyCheck = validateKeyEvent(args.action); |
| 4401 | if (!keyCheck.ok()) { |
| 4402 | LOG(ERROR) << "invalid key event: " << keyCheck.error(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4403 | return; |
| 4404 | } |
| 4405 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4406 | uint32_t policyFlags = args.policyFlags; |
| 4407 | int32_t flags = args.flags; |
| 4408 | int32_t metaState = args.metaState; |
Siarhei Vishniakou | 622bd32 | 2018-10-29 18:02:27 -0700 | [diff] [blame] | 4409 | // InputDispatcher tracks and generates key repeats on behalf of |
| 4410 | // whatever notifies it, so repeatCount should always be set to 0 |
| 4411 | constexpr int32_t repeatCount = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4412 | if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { |
| 4413 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 4414 | flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
| 4415 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4416 | if (policyFlags & POLICY_FLAG_FUNCTION) { |
| 4417 | metaState |= AMETA_FUNCTION_ON; |
| 4418 | } |
| 4419 | |
| 4420 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 4421 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4422 | int32_t keyCode = args.keyCode; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4423 | KeyEvent event; |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4424 | event.initialize(args.id, args.deviceId, args.source, args.displayId, INVALID_HMAC, args.action, |
| 4425 | flags, keyCode, args.scanCode, metaState, repeatCount, args.downTime, |
| 4426 | args.eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4427 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4428 | android::base::Timer t; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4429 | mPolicy.interceptKeyBeforeQueueing(event, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4430 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4431 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4432 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4433 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4434 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4435 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4436 | { // acquire lock |
| 4437 | mLock.lock(); |
| 4438 | |
| 4439 | if (shouldSendKeyToInputFilterLocked(args)) { |
| 4440 | mLock.unlock(); |
| 4441 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4442 | policyFlags |= POLICY_FLAG_FILTERED; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4443 | if (!mPolicy.filterInputEvent(event, policyFlags)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4444 | return; // event was consumed by the filter |
| 4445 | } |
| 4446 | |
| 4447 | mLock.lock(); |
| 4448 | } |
| 4449 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4450 | std::unique_ptr<KeyEntry> newEntry = |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4451 | std::make_unique<KeyEntry>(args.id, /*injectionState=*/nullptr, args.eventTime, |
| 4452 | args.deviceId, args.source, args.displayId, policyFlags, |
| 4453 | args.action, flags, keyCode, args.scanCode, metaState, |
| 4454 | repeatCount, args.downTime); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 4455 | if (mTracer) { |
| 4456 | newEntry->traceTracker = mTracer->traceInboundEvent(*newEntry); |
| 4457 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4458 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4459 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4460 | mLock.unlock(); |
| 4461 | } // release lock |
| 4462 | |
| 4463 | if (needWake) { |
| 4464 | mLooper->wake(); |
| 4465 | } |
| 4466 | } |
| 4467 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4468 | bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs& args) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4469 | return mInputFilterEnabled; |
| 4470 | } |
| 4471 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4472 | void InputDispatcher::notifyMotion(const NotifyMotionArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4473 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 96282b0 | 2023-02-24 22:36:17 +0000 | [diff] [blame] | 4474 | ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=%s, " |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4475 | "displayId=%" PRId32 ", policyFlags=0x%x, " |
Siarhei Vishniakou | 6ebd069 | 2022-10-20 15:05:45 -0700 | [diff] [blame] | 4476 | "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] | 4477 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, xCursorPosition=%f, " |
| 4478 | "yCursorPosition=%f, downTime=%" PRId64, |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4479 | args.id, args.eventTime, args.deviceId, inputEventSourceToString(args.source).c_str(), |
| 4480 | args.displayId, args.policyFlags, MotionEvent::actionToString(args.action).c_str(), |
| 4481 | args.actionButton, args.flags, args.metaState, args.buttonState, args.edgeFlags, |
| 4482 | args.xPrecision, args.yPrecision, args.xCursorPosition, args.yCursorPosition, |
| 4483 | args.downTime); |
Siarhei Vishniakou | 3218fc0 | 2023-06-15 20:41:02 -0700 | [diff] [blame] | 4484 | for (uint32_t i = 0; i < args.getPointerCount(); i++) { |
Prabir Pradhan | 96282b0 | 2023-02-24 22:36:17 +0000 | [diff] [blame] | 4485 | ALOGD(" Pointer %d: id=%d, toolType=%s, x=%f, y=%f, pressure=%f, size=%f, " |
| 4486 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, orientation=%f", |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4487 | i, args.pointerProperties[i].id, |
| 4488 | ftl::enum_string(args.pointerProperties[i].toolType).c_str(), |
| 4489 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 4490 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 4491 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 4492 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 4493 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 4494 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 4495 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 4496 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 4497 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4498 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4499 | } |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4500 | |
Siarhei Vishniakou | 3218fc0 | 2023-06-15 20:41:02 -0700 | [diff] [blame] | 4501 | Result<void> motionCheck = |
| 4502 | validateMotionEvent(args.action, args.actionButton, args.getPointerCount(), |
| 4503 | args.pointerProperties.data()); |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4504 | if (!motionCheck.ok()) { |
| 4505 | LOG(FATAL) << "Invalid event: " << args.dump() << "; reason: " << motionCheck.error(); |
| 4506 | return; |
| 4507 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4508 | |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 4509 | if (DEBUG_VERIFY_EVENTS) { |
| 4510 | auto [it, _] = |
| 4511 | mVerifiersByDisplay.try_emplace(args.displayId, |
| 4512 | StringPrintf("display %" PRId32, args.displayId)); |
| 4513 | Result<void> result = |
Siarhei Vishniakou | 2d151ac | 2023-09-19 13:30:24 -0700 | [diff] [blame] | 4514 | it->second.processMovement(args.deviceId, args.source, args.action, |
| 4515 | args.getPointerCount(), args.pointerProperties.data(), |
| 4516 | args.pointerCoords.data(), args.flags); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 4517 | if (!result.ok()) { |
| 4518 | LOG(FATAL) << "Bad stream: " << result.error() << " caused by " << args.dump(); |
| 4519 | } |
| 4520 | } |
| 4521 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4522 | uint32_t policyFlags = args.policyFlags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4523 | policyFlags |= POLICY_FLAG_TRUSTED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4524 | |
| 4525 | android::base::Timer t; |
Yeabkal Wubshit | 88a9041 | 2023-12-21 18:23:04 -0800 | [diff] [blame] | 4526 | mPolicy.interceptMotionBeforeQueueing(args.displayId, args.source, args.action, args.eventTime, |
| 4527 | policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4528 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4529 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4530 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4531 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4532 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4533 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4534 | { // acquire lock |
| 4535 | mLock.lock(); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 4536 | if (!(policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
| 4537 | // Set the flag anyway if we already have an ongoing gesture. That would allow us to |
| 4538 | // complete the processing of the current stroke. |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4539 | const auto touchStateIt = mTouchStatesByDisplay.find(args.displayId); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 4540 | if (touchStateIt != mTouchStatesByDisplay.end()) { |
| 4541 | const TouchState& touchState = touchStateIt->second; |
Linnan Li | 907ae73 | 2023-09-05 17:14:21 +0800 | [diff] [blame] | 4542 | if (touchState.hasTouchingPointers(args.deviceId) || |
| 4543 | touchState.hasHoveringPointers(args.deviceId)) { |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 4544 | policyFlags |= POLICY_FLAG_PASS_TO_USER; |
| 4545 | } |
| 4546 | } |
| 4547 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4548 | |
| 4549 | if (shouldSendMotionToInputFilterLocked(args)) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4550 | ui::Transform displayTransform; |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4551 | if (const auto it = mDisplayInfos.find(args.displayId); it != mDisplayInfos.end()) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4552 | displayTransform = it->second.transform; |
| 4553 | } |
| 4554 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4555 | mLock.unlock(); |
| 4556 | |
| 4557 | MotionEvent event; |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4558 | event.initialize(args.id, args.deviceId, args.source, args.displayId, INVALID_HMAC, |
| 4559 | args.action, args.actionButton, args.flags, args.edgeFlags, |
| 4560 | args.metaState, args.buttonState, args.classification, |
| 4561 | displayTransform, args.xPrecision, args.yPrecision, |
| 4562 | args.xCursorPosition, args.yCursorPosition, displayTransform, |
Siarhei Vishniakou | 3218fc0 | 2023-06-15 20:41:02 -0700 | [diff] [blame] | 4563 | args.downTime, args.eventTime, args.getPointerCount(), |
| 4564 | args.pointerProperties.data(), args.pointerCoords.data()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4565 | |
| 4566 | policyFlags |= POLICY_FLAG_FILTERED; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4567 | if (!mPolicy.filterInputEvent(event, policyFlags)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4568 | return; // event was consumed by the filter |
| 4569 | } |
| 4570 | |
| 4571 | mLock.lock(); |
| 4572 | } |
| 4573 | |
| 4574 | // Just enqueue a new motion event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4575 | std::unique_ptr<MotionEntry> newEntry = |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4576 | std::make_unique<MotionEntry>(args.id, /*injectionState=*/nullptr, args.eventTime, |
| 4577 | args.deviceId, args.source, args.displayId, |
| 4578 | policyFlags, args.action, args.actionButton, |
| 4579 | args.flags, args.metaState, args.buttonState, |
| 4580 | args.classification, args.edgeFlags, args.xPrecision, |
| 4581 | args.yPrecision, args.xCursorPosition, |
| 4582 | args.yCursorPosition, args.downTime, |
| 4583 | args.pointerProperties, args.pointerCoords); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 4584 | if (mTracer) { |
| 4585 | newEntry->traceTracker = mTracer->traceInboundEvent(*newEntry); |
| 4586 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4587 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4588 | if (args.id != android::os::IInputConstants::INVALID_INPUT_EVENT_ID && |
| 4589 | IdGenerator::getSource(args.id) == IdGenerator::Source::INPUT_READER && |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 4590 | !mInputFilterEnabled) { |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4591 | const bool isDown = args.action == AMOTION_EVENT_ACTION_DOWN; |
Asmita Poddar | dd9a6cd | 2023-09-26 15:35:12 +0000 | [diff] [blame] | 4592 | std::set<InputDeviceUsageSource> sources = getUsageSourcesForMotionArgs(args); |
| 4593 | mLatencyTracker.trackListener(args.id, isDown, args.eventTime, args.readTime, |
| 4594 | args.deviceId, sources); |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 4595 | } |
| 4596 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4597 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4598 | mLock.unlock(); |
| 4599 | } // release lock |
| 4600 | |
| 4601 | if (needWake) { |
| 4602 | mLooper->wake(); |
| 4603 | } |
| 4604 | } |
| 4605 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4606 | void InputDispatcher::notifySensor(const NotifySensorArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4607 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4608 | ALOGD("notifySensor - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 4609 | " sensorType=%s", |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4610 | args.id, args.eventTime, args.deviceId, args.source, |
| 4611 | ftl::enum_string(args.sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4612 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4613 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4614 | bool needWake = false; |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4615 | { // acquire lock |
| 4616 | mLock.lock(); |
| 4617 | |
| 4618 | // Just enqueue a new sensor event. |
| 4619 | std::unique_ptr<SensorEntry> newEntry = |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4620 | std::make_unique<SensorEntry>(args.id, args.eventTime, args.deviceId, args.source, |
| 4621 | /* policyFlags=*/0, args.hwTimestamp, args.sensorType, |
| 4622 | args.accuracy, args.accuracyChanged, args.values); |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4623 | |
| 4624 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
| 4625 | mLock.unlock(); |
| 4626 | } // release lock |
| 4627 | |
| 4628 | if (needWake) { |
| 4629 | mLooper->wake(); |
| 4630 | } |
| 4631 | } |
| 4632 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4633 | void InputDispatcher::notifyVibratorState(const NotifyVibratorStateArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4634 | if (debugInboundEventDetails()) { |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4635 | ALOGD("notifyVibratorState - eventTime=%" PRId64 ", device=%d, isOn=%d", args.eventTime, |
| 4636 | args.deviceId, args.isOn); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4637 | } |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4638 | mPolicy.notifyVibratorState(args.deviceId, args.isOn); |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 4639 | } |
| 4640 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4641 | bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs& args) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4642 | return mInputFilterEnabled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4643 | } |
| 4644 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4645 | void InputDispatcher::notifySwitch(const NotifySwitchArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4646 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4647 | ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, " |
| 4648 | "switchMask=0x%08x", |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4649 | args.eventTime, args.policyFlags, args.switchValues, args.switchMask); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4650 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4651 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4652 | uint32_t policyFlags = args.policyFlags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4653 | policyFlags |= POLICY_FLAG_TRUSTED; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4654 | mPolicy.notifySwitch(args.eventTime, args.switchValues, args.switchMask, policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4655 | } |
| 4656 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4657 | void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4658 | if (debugInboundEventDetails()) { |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4659 | ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args.eventTime, |
| 4660 | args.deviceId); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4661 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4662 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4663 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4664 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4665 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4666 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4667 | std::unique_ptr<DeviceResetEntry> newEntry = |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4668 | std::make_unique<DeviceResetEntry>(args.id, args.eventTime, args.deviceId); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4669 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Siarhei Vishniakou | 1160ecd | 2023-06-28 15:57:47 -0700 | [diff] [blame] | 4670 | |
| 4671 | for (auto& [_, verifier] : mVerifiersByDisplay) { |
| 4672 | verifier.resetDevice(args.deviceId); |
| 4673 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4674 | } // release lock |
| 4675 | |
| 4676 | if (needWake) { |
| 4677 | mLooper->wake(); |
| 4678 | } |
| 4679 | } |
| 4680 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4681 | void InputDispatcher::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4682 | if (debugInboundEventDetails()) { |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4683 | ALOGD("notifyPointerCaptureChanged - eventTime=%" PRId64 ", enabled=%s", args.eventTime, |
| 4684 | args.request.enable ? "true" : "false"); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4685 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4686 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4687 | bool needWake = false; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4688 | { // acquire lock |
| 4689 | std::scoped_lock _l(mLock); |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4690 | auto entry = |
| 4691 | std::make_unique<PointerCaptureChangedEntry>(args.id, args.eventTime, args.request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4692 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 4693 | } // release lock |
| 4694 | |
| 4695 | if (needWake) { |
| 4696 | mLooper->wake(); |
| 4697 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4698 | } |
| 4699 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4700 | InputEventInjectionResult InputDispatcher::injectInputEvent(const InputEvent* event, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 4701 | std::optional<gui::Uid> targetUid, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4702 | InputEventInjectionSync syncMode, |
| 4703 | std::chrono::milliseconds timeout, |
| 4704 | uint32_t policyFlags) { |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4705 | Result<void> eventValidation = validateInputEvent(*event); |
| 4706 | if (!eventValidation.ok()) { |
| 4707 | LOG(INFO) << "Injection failed: invalid event: " << eventValidation.error(); |
| 4708 | return InputEventInjectionResult::FAILED; |
| 4709 | } |
| 4710 | |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4711 | if (debugInboundEventDetails()) { |
Siarhei Vishniakou | 827d1ac | 2023-07-21 16:37:51 -0700 | [diff] [blame] | 4712 | LOG(INFO) << __func__ << ": targetUid=" << toString(targetUid, &uidString) |
| 4713 | << ", syncMode=" << ftl::enum_string(syncMode) << ", timeout=" << timeout.count() |
| 4714 | << "ms, policyFlags=0x" << std::hex << policyFlags << std::dec |
| 4715 | << ", event=" << *event; |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4716 | } |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 4717 | 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] | 4718 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4719 | policyFlags |= POLICY_FLAG_INJECTED | POLICY_FLAG_TRUSTED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4720 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4721 | // 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] | 4722 | // that have gone through the InputFilter. If the event passed through the InputFilter, assign |
| 4723 | // the provided device id. If the InputFilter is accessibility, and it modifies or synthesizes |
| 4724 | // the injected event, it is responsible for setting POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY. |
| 4725 | // For those events, we will set FLAG_IS_ACCESSIBILITY_EVENT to allow apps to distinguish them |
| 4726 | // from events that originate from actual hardware. |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 4727 | DeviceId resolvedDeviceId = VIRTUAL_KEYBOARD_ID; |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4728 | if (policyFlags & POLICY_FLAG_FILTERED) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4729 | resolvedDeviceId = event->getDeviceId(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4730 | } |
| 4731 | |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4732 | const bool isAsync = syncMode == InputEventInjectionSync::NONE; |
| 4733 | auto injectionState = std::make_shared<InjectionState>(targetUid, isAsync); |
| 4734 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4735 | std::queue<std::unique_ptr<EventEntry>> injectedEntries; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4736 | switch (event->getType()) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4737 | case InputEventType::KEY: { |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4738 | const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event); |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4739 | const int32_t action = incomingKey.getAction(); |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4740 | int32_t flags = incomingKey.getFlags(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4741 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4742 | flags |= AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4743 | } |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4744 | int32_t keyCode = incomingKey.getKeyCode(); |
| 4745 | int32_t metaState = incomingKey.getMetaState(); |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4746 | KeyEvent keyEvent; |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4747 | keyEvent.initialize(incomingKey.getId(), resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4748 | incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode, |
| 4749 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), |
| 4750 | incomingKey.getDownTime(), incomingKey.getEventTime()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4751 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4752 | if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) { |
| 4753 | policyFlags |= POLICY_FLAG_VIRTUAL; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4754 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4755 | |
| 4756 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 4757 | android::base::Timer t; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4758 | mPolicy.interceptKeyBeforeQueueing(keyEvent, /*byref*/ policyFlags); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4759 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4760 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
| 4761 | std::to_string(t.duration().count()).c_str()); |
| 4762 | } |
| 4763 | } |
| 4764 | |
| 4765 | mLock.lock(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4766 | std::unique_ptr<KeyEntry> injectedEntry = |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4767 | std::make_unique<KeyEntry>(incomingKey.getId(), injectionState, |
| 4768 | incomingKey.getEventTime(), resolvedDeviceId, |
| 4769 | incomingKey.getSource(), incomingKey.getDisplayId(), |
| 4770 | policyFlags, action, flags, keyCode, |
| 4771 | incomingKey.getScanCode(), metaState, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4772 | incomingKey.getRepeatCount(), |
| 4773 | incomingKey.getDownTime()); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 4774 | if (mTracer) { |
| 4775 | injectedEntry->traceTracker = mTracer->traceInboundEvent(*injectedEntry); |
| 4776 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4777 | injectedEntries.push(std::move(injectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4778 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4779 | } |
| 4780 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4781 | case InputEventType::MOTION: { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4782 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4783 | const bool isPointerEvent = |
| 4784 | isFromSource(event->getSource(), AINPUT_SOURCE_CLASS_POINTER); |
| 4785 | // If a pointer event has no displayId specified, inject it to the default display. |
| 4786 | const uint32_t displayId = isPointerEvent && (event->getDisplayId() == ADISPLAY_ID_NONE) |
| 4787 | ? ADISPLAY_ID_DEFAULT |
| 4788 | : event->getDisplayId(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4789 | int32_t flags = motionEvent.getFlags(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4790 | |
| 4791 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4792 | nsecs_t eventTime = motionEvent.getEventTime(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4793 | android::base::Timer t; |
Yeabkal Wubshit | 88a9041 | 2023-12-21 18:23:04 -0800 | [diff] [blame] | 4794 | mPolicy.interceptMotionBeforeQueueing(displayId, motionEvent.getSource(), |
| 4795 | motionEvent.getAction(), eventTime, |
| 4796 | /*byref*/ policyFlags); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4797 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4798 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
| 4799 | std::to_string(t.duration().count()).c_str()); |
| 4800 | } |
| 4801 | } |
| 4802 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4803 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4804 | flags |= AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4805 | } |
| 4806 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4807 | mLock.lock(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4808 | const nsecs_t* sampleEventTimes = motionEvent.getSampleEventTimes(); |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4809 | const size_t pointerCount = motionEvent.getPointerCount(); |
| 4810 | const std::vector<PointerProperties> |
| 4811 | pointerProperties(motionEvent.getPointerProperties(), |
| 4812 | motionEvent.getPointerProperties() + pointerCount); |
| 4813 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4814 | const PointerCoords* samplePointerCoords = motionEvent.getSamplePointerCoords(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4815 | std::unique_ptr<MotionEntry> injectedEntry = |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4816 | std::make_unique<MotionEntry>(motionEvent.getId(), injectionState, |
| 4817 | *sampleEventTimes, resolvedDeviceId, |
| 4818 | motionEvent.getSource(), displayId, policyFlags, |
| 4819 | motionEvent.getAction(), |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4820 | motionEvent.getActionButton(), flags, |
| 4821 | motionEvent.getMetaState(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4822 | motionEvent.getButtonState(), |
| 4823 | motionEvent.getClassification(), |
| 4824 | motionEvent.getEdgeFlags(), |
| 4825 | motionEvent.getXPrecision(), |
| 4826 | motionEvent.getYPrecision(), |
| 4827 | motionEvent.getRawXCursorPosition(), |
| 4828 | motionEvent.getRawYCursorPosition(), |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4829 | motionEvent.getDownTime(), pointerProperties, |
| 4830 | std::vector<PointerCoords>(samplePointerCoords, |
| 4831 | samplePointerCoords + |
| 4832 | pointerCount)); |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4833 | transformMotionEntryForInjectionLocked(*injectedEntry, motionEvent.getTransform()); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 4834 | if (mTracer) { |
| 4835 | injectedEntry->traceTracker = mTracer->traceInboundEvent(*injectedEntry); |
| 4836 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4837 | injectedEntries.push(std::move(injectedEntry)); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4838 | for (size_t i = motionEvent.getHistorySize(); i > 0; i--) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4839 | sampleEventTimes += 1; |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4840 | samplePointerCoords += motionEvent.getPointerCount(); |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4841 | std::unique_ptr<MotionEntry> nextInjectedEntry = std::make_unique< |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4842 | MotionEntry>(motionEvent.getId(), injectionState, *sampleEventTimes, |
| 4843 | resolvedDeviceId, motionEvent.getSource(), displayId, |
| 4844 | policyFlags, motionEvent.getAction(), |
| 4845 | motionEvent.getActionButton(), flags, |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4846 | motionEvent.getMetaState(), motionEvent.getButtonState(), |
| 4847 | motionEvent.getClassification(), motionEvent.getEdgeFlags(), |
| 4848 | motionEvent.getXPrecision(), motionEvent.getYPrecision(), |
| 4849 | motionEvent.getRawXCursorPosition(), |
| 4850 | motionEvent.getRawYCursorPosition(), motionEvent.getDownTime(), |
| 4851 | pointerProperties, |
| 4852 | std::vector<PointerCoords>(samplePointerCoords, |
| 4853 | samplePointerCoords + |
| 4854 | pointerCount)); |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4855 | transformMotionEntryForInjectionLocked(*nextInjectedEntry, |
| 4856 | motionEvent.getTransform()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4857 | injectedEntries.push(std::move(nextInjectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4858 | } |
| 4859 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4860 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4861 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4862 | default: |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4863 | LOG(WARNING) << "Cannot inject " << ftl::enum_string(event->getType()) << " events"; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4864 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4865 | } |
| 4866 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4867 | bool needWake = false; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4868 | while (!injectedEntries.empty()) { |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 4869 | if (DEBUG_INJECTION) { |
Siarhei Vishniakou | 827d1ac | 2023-07-21 16:37:51 -0700 | [diff] [blame] | 4870 | LOG(INFO) << "Injecting " << injectedEntries.front()->getDescription(); |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 4871 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4872 | needWake |= enqueueInboundEventLocked(std::move(injectedEntries.front())); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4873 | injectedEntries.pop(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4874 | } |
| 4875 | |
| 4876 | mLock.unlock(); |
| 4877 | |
| 4878 | if (needWake) { |
| 4879 | mLooper->wake(); |
| 4880 | } |
| 4881 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4882 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4883 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4884 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4885 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4886 | if (syncMode == InputEventInjectionSync::NONE) { |
| 4887 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4888 | } else { |
| 4889 | for (;;) { |
| 4890 | injectionResult = injectionState->injectionResult; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4891 | if (injectionResult != InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4892 | break; |
| 4893 | } |
| 4894 | |
| 4895 | nsecs_t remainingTimeout = endTime - now(); |
| 4896 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4897 | if (DEBUG_INJECTION) { |
| 4898 | ALOGD("injectInputEvent - Timed out waiting for injection result " |
| 4899 | "to become available."); |
| 4900 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4901 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4902 | break; |
| 4903 | } |
| 4904 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4905 | mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4906 | } |
| 4907 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4908 | if (injectionResult == InputEventInjectionResult::SUCCEEDED && |
| 4909 | syncMode == InputEventInjectionSync::WAIT_FOR_FINISHED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4910 | while (injectionState->pendingForegroundDispatches != 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4911 | if (DEBUG_INJECTION) { |
| 4912 | ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", |
| 4913 | injectionState->pendingForegroundDispatches); |
| 4914 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4915 | nsecs_t remainingTimeout = endTime - now(); |
| 4916 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4917 | if (DEBUG_INJECTION) { |
| 4918 | ALOGD("injectInputEvent - Timed out waiting for pending foreground " |
| 4919 | "dispatches to finish."); |
| 4920 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4921 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4922 | break; |
| 4923 | } |
| 4924 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4925 | mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4926 | } |
| 4927 | } |
| 4928 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4929 | } // release lock |
| 4930 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4931 | if (DEBUG_INJECTION) { |
Siarhei Vishniakou | 827d1ac | 2023-07-21 16:37:51 -0700 | [diff] [blame] | 4932 | LOG(INFO) << "injectInputEvent - Finished with result " |
| 4933 | << ftl::enum_string(injectionResult); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4934 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4935 | |
| 4936 | return injectionResult; |
| 4937 | } |
| 4938 | |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4939 | std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4940 | std::array<uint8_t, 32> calculatedHmac; |
| 4941 | std::unique_ptr<VerifiedInputEvent> result; |
| 4942 | switch (event.getType()) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4943 | case InputEventType::KEY: { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4944 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event); |
| 4945 | VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent); |
| 4946 | result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4947 | calculatedHmac = sign(verifiedKeyEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4948 | break; |
| 4949 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4950 | case InputEventType::MOTION: { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4951 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event); |
| 4952 | VerifiedMotionEvent verifiedMotionEvent = |
| 4953 | verifiedMotionEventFromMotionEvent(motionEvent); |
| 4954 | result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4955 | calculatedHmac = sign(verifiedMotionEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4956 | break; |
| 4957 | } |
| 4958 | default: { |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 4959 | LOG(ERROR) << "Cannot verify events of type " << ftl::enum_string(event.getType()); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4960 | return nullptr; |
| 4961 | } |
| 4962 | } |
| 4963 | if (calculatedHmac == INVALID_HMAC) { |
| 4964 | return nullptr; |
| 4965 | } |
tyiu | 1573a67 | 2023-02-21 22:38:32 +0000 | [diff] [blame] | 4966 | if (0 != CRYPTO_memcmp(calculatedHmac.data(), event.getHmac().data(), calculatedHmac.size())) { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4967 | return nullptr; |
| 4968 | } |
| 4969 | return result; |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4970 | } |
| 4971 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 4972 | void InputDispatcher::setInjectionResult(const EventEntry& entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4973 | InputEventInjectionResult injectionResult) { |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4974 | if (!entry.injectionState) { |
| 4975 | // Not an injected event. |
| 4976 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4977 | } |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4978 | |
| 4979 | InjectionState& injectionState = *entry.injectionState; |
| 4980 | if (DEBUG_INJECTION) { |
| 4981 | LOG(INFO) << "Setting input event injection result to " |
| 4982 | << ftl::enum_string(injectionResult); |
| 4983 | } |
| 4984 | |
| 4985 | if (injectionState.injectionIsAsync && !(entry.policyFlags & POLICY_FLAG_FILTERED)) { |
| 4986 | // Log the outcome since the injector did not wait for the injection result. |
| 4987 | switch (injectionResult) { |
| 4988 | case InputEventInjectionResult::SUCCEEDED: |
| 4989 | ALOGV("Asynchronous input event injection succeeded."); |
| 4990 | break; |
| 4991 | case InputEventInjectionResult::TARGET_MISMATCH: |
| 4992 | ALOGV("Asynchronous input event injection target mismatch."); |
| 4993 | break; |
| 4994 | case InputEventInjectionResult::FAILED: |
| 4995 | ALOGW("Asynchronous input event injection failed."); |
| 4996 | break; |
| 4997 | case InputEventInjectionResult::TIMED_OUT: |
| 4998 | ALOGW("Asynchronous input event injection timed out."); |
| 4999 | break; |
| 5000 | case InputEventInjectionResult::PENDING: |
| 5001 | ALOGE("Setting result to 'PENDING' for asynchronous injection"); |
| 5002 | break; |
| 5003 | } |
| 5004 | } |
| 5005 | |
| 5006 | injectionState.injectionResult = injectionResult; |
| 5007 | mInjectionResultAvailable.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5008 | } |
| 5009 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 5010 | void InputDispatcher::transformMotionEntryForInjectionLocked( |
| 5011 | MotionEntry& entry, const ui::Transform& injectedTransform) const { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 5012 | // Input injection works in the logical display coordinate space, but the input pipeline works |
| 5013 | // display space, so we need to transform the injected events accordingly. |
| 5014 | const auto it = mDisplayInfos.find(entry.displayId); |
| 5015 | if (it == mDisplayInfos.end()) return; |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 5016 | const auto& transformToDisplay = it->second.transform.inverse() * injectedTransform; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 5017 | |
Prabir Pradhan | d9a2ebe | 2022-07-20 19:25:13 +0000 | [diff] [blame] | 5018 | if (entry.xCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION && |
| 5019 | entry.yCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) { |
| 5020 | const vec2 cursor = |
| 5021 | MotionEvent::calculateTransformedXY(entry.source, transformToDisplay, |
| 5022 | {entry.xCursorPosition, entry.yCursorPosition}); |
| 5023 | entry.xCursorPosition = cursor.x; |
| 5024 | entry.yCursorPosition = cursor.y; |
| 5025 | } |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 5026 | for (uint32_t i = 0; i < entry.getPointerCount(); i++) { |
Prabir Pradhan | 8e6ce22 | 2022-02-24 09:08:54 -0800 | [diff] [blame] | 5027 | entry.pointerCoords[i] = |
| 5028 | MotionEvent::calculateTransformedCoords(entry.source, transformToDisplay, |
| 5029 | entry.pointerCoords[i]); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 5030 | } |
| 5031 | } |
| 5032 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 5033 | void InputDispatcher::incrementPendingForegroundDispatches(const EventEntry& entry) { |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 5034 | if (entry.injectionState) { |
| 5035 | entry.injectionState->pendingForegroundDispatches += 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5036 | } |
| 5037 | } |
| 5038 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 5039 | void InputDispatcher::decrementPendingForegroundDispatches(const EventEntry& entry) { |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 5040 | if (entry.injectionState) { |
| 5041 | entry.injectionState->pendingForegroundDispatches -= 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5042 | |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 5043 | if (entry.injectionState->pendingForegroundDispatches == 0) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5044 | mInjectionSyncFinished.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5045 | } |
| 5046 | } |
| 5047 | } |
| 5048 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5049 | const std::vector<sp<WindowInfoHandle>>& InputDispatcher::getWindowHandlesLocked( |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5050 | int32_t displayId) const { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5051 | static const std::vector<sp<WindowInfoHandle>> EMPTY_WINDOW_HANDLES; |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5052 | auto it = mWindowHandlesByDisplay.find(displayId); |
| 5053 | return it != mWindowHandlesByDisplay.end() ? it->second : EMPTY_WINDOW_HANDLES; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5054 | } |
| 5055 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5056 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 5057 | const sp<IBinder>& windowHandleToken, std::optional<int32_t> displayId) const { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 5058 | if (windowHandleToken == nullptr) { |
| 5059 | return nullptr; |
| 5060 | } |
| 5061 | |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 5062 | if (!displayId) { |
| 5063 | // Look through all displays. |
| 5064 | for (auto& it : mWindowHandlesByDisplay) { |
| 5065 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 5066 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 5067 | if (windowHandle->getToken() == windowHandleToken) { |
| 5068 | return windowHandle; |
| 5069 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5070 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5071 | } |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5072 | return nullptr; |
| 5073 | } |
| 5074 | |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 5075 | // Only look through the requested display. |
| 5076 | for (const sp<WindowInfoHandle>& windowHandle : getWindowHandlesLocked(*displayId)) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5077 | if (windowHandle->getToken() == windowHandleToken) { |
| 5078 | return windowHandle; |
| 5079 | } |
| 5080 | } |
| 5081 | return nullptr; |
| 5082 | } |
| 5083 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5084 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
| 5085 | const sp<WindowInfoHandle>& windowHandle) const { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 5086 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5087 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 5088 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 5089 | if (handle->getId() == windowHandle->getId() && |
| 5090 | handle->getToken() == windowHandle->getToken()) { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 5091 | if (windowHandle->getInfo()->displayId != it.first) { |
| 5092 | ALOGE("Found window %s in display %" PRId32 |
| 5093 | ", but it should belong to display %" PRId32, |
| 5094 | windowHandle->getName().c_str(), it.first, |
| 5095 | windowHandle->getInfo()->displayId); |
| 5096 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 5097 | return handle; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5098 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5099 | } |
| 5100 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 5101 | return nullptr; |
| 5102 | } |
| 5103 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5104 | sp<WindowInfoHandle> InputDispatcher::getFocusedWindowHandleLocked(int displayId) const { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 5105 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(displayId); |
| 5106 | return getWindowHandleLocked(focusedToken, displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5107 | } |
| 5108 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5109 | ui::Transform InputDispatcher::getTransformLocked(int32_t displayId) const { |
| 5110 | auto displayInfoIt = mDisplayInfos.find(displayId); |
| 5111 | return displayInfoIt != mDisplayInfos.end() ? displayInfoIt->second.transform |
| 5112 | : kIdentityTransform; |
| 5113 | } |
| 5114 | |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5115 | bool InputDispatcher::canWindowReceiveMotionLocked(const sp<WindowInfoHandle>& window, |
| 5116 | const MotionEntry& motionEntry) const { |
| 5117 | const WindowInfo& info = *window->getInfo(); |
| 5118 | |
| 5119 | // Skip spy window targets that are not valid for targeted injection. |
| 5120 | if (const auto err = verifyTargetedInjection(window, motionEntry); err) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5121 | return false; |
| 5122 | } |
| 5123 | |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5124 | if (info.inputConfig.test(WindowInfo::InputConfig::PAUSE_DISPATCHING)) { |
| 5125 | ALOGI("Not sending touch event to %s because it is paused", window->getName().c_str()); |
| 5126 | return false; |
| 5127 | } |
| 5128 | |
| 5129 | if (info.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) { |
| 5130 | ALOGW("Not sending touch gesture to %s because it has config NO_INPUT_CHANNEL", |
| 5131 | window->getName().c_str()); |
| 5132 | return false; |
| 5133 | } |
| 5134 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 5135 | std::shared_ptr<Connection> connection = getConnectionLocked(window->getToken()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5136 | if (connection == nullptr) { |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5137 | ALOGW("Not sending touch to %s because there's no corresponding connection", |
| 5138 | window->getName().c_str()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5139 | return false; |
| 5140 | } |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5141 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5142 | if (!connection->responsive) { |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5143 | 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] | 5144 | return false; |
| 5145 | } |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5146 | |
| 5147 | // Drop events that can't be trusted due to occlusion |
| 5148 | const auto [x, y] = resolveTouchedPosition(motionEntry); |
| 5149 | TouchOcclusionInfo occlusionInfo = computeTouchOcclusionInfoLocked(window, x, y); |
| 5150 | if (!isTouchTrustedLocked(occlusionInfo)) { |
| 5151 | if (DEBUG_TOUCH_OCCLUSION) { |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 5152 | ALOGD("Stack of obscuring windows during untrusted touch (%.1f, %.1f):", x, y); |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5153 | for (const auto& log : occlusionInfo.debugInfo) { |
| 5154 | ALOGD("%s", log.c_str()); |
| 5155 | } |
| 5156 | } |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 5157 | ALOGW("Dropping untrusted touch event due to %s/%s", occlusionInfo.obscuringPackage.c_str(), |
| 5158 | occlusionInfo.obscuringUid.toString().c_str()); |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5159 | return false; |
| 5160 | } |
| 5161 | |
| 5162 | // Drop touch events if requested by input feature |
| 5163 | if (shouldDropInput(motionEntry, window)) { |
| 5164 | return false; |
| 5165 | } |
| 5166 | |
Siarhei Vishniakou | f77f60a | 2023-10-23 17:26:05 -0700 | [diff] [blame] | 5167 | // Ignore touches if stylus is down anywhere on screen |
| 5168 | if (info.inputConfig.test(WindowInfo::InputConfig::GLOBAL_STYLUS_BLOCKS_TOUCH) && |
| 5169 | isStylusActiveInDisplay(info.displayId, mTouchStatesByDisplay)) { |
| 5170 | LOG(INFO) << "Dropping touch from " << window->getName() << " because stylus is active"; |
| 5171 | return false; |
| 5172 | } |
| 5173 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5174 | return true; |
| 5175 | } |
| 5176 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5177 | void InputDispatcher::updateWindowHandlesForDisplayLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5178 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
| 5179 | if (windowInfoHandles.empty()) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5180 | // Remove all handles on a display if there are no windows left. |
| 5181 | mWindowHandlesByDisplay.erase(displayId); |
| 5182 | return; |
| 5183 | } |
| 5184 | |
| 5185 | // Since we compare the pointer of input window handles across window updates, we need |
| 5186 | // 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] | 5187 | const std::vector<sp<WindowInfoHandle>>& oldHandles = getWindowHandlesLocked(displayId); |
| 5188 | std::unordered_map<int32_t /*id*/, sp<WindowInfoHandle>> oldHandlesById; |
| 5189 | for (const sp<WindowInfoHandle>& handle : oldHandles) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5190 | oldHandlesById[handle->getId()] = handle; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5191 | } |
| 5192 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5193 | std::vector<sp<WindowInfoHandle>> newHandles; |
| 5194 | for (const sp<WindowInfoHandle>& handle : windowInfoHandles) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5195 | const WindowInfo* info = handle->getInfo(); |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 5196 | if (getConnectionLocked(handle->getToken()) == nullptr) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5197 | const bool noInputChannel = |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 5198 | info->inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5199 | const bool canReceiveInput = |
| 5200 | !info->inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE) || |
| 5201 | !info->inputConfig.test(WindowInfo::InputConfig::NOT_FOCUSABLE); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5202 | if (canReceiveInput && !noInputChannel) { |
John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 5203 | ALOGV("Window handle %s has no registered input channel", |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5204 | handle->getName().c_str()); |
Robert Carr | 2984b7a | 2020-04-13 17:06:45 -0700 | [diff] [blame] | 5205 | continue; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5206 | } |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5207 | } |
| 5208 | |
| 5209 | if (info->displayId != displayId) { |
| 5210 | ALOGE("Window %s updated by wrong display %d, should belong to display %d", |
| 5211 | handle->getName().c_str(), displayId, info->displayId); |
| 5212 | continue; |
| 5213 | } |
| 5214 | |
Robert Carr | edd1360 | 2020-04-13 17:24:34 -0700 | [diff] [blame] | 5215 | if ((oldHandlesById.find(handle->getId()) != oldHandlesById.end()) && |
| 5216 | (oldHandlesById.at(handle->getId())->getToken() == handle->getToken())) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5217 | const sp<WindowInfoHandle>& oldHandle = oldHandlesById.at(handle->getId()); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5218 | oldHandle->updateFrom(handle); |
| 5219 | newHandles.push_back(oldHandle); |
| 5220 | } else { |
| 5221 | newHandles.push_back(handle); |
| 5222 | } |
| 5223 | } |
| 5224 | |
| 5225 | // Insert or replace |
| 5226 | mWindowHandlesByDisplay[displayId] = newHandles; |
| 5227 | } |
| 5228 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5229 | /** |
| 5230 | * Called from InputManagerService, update window handle list by displayId that can receive input. |
| 5231 | * A window handle contains information about InputChannel, Touch Region, Types, Focused,... |
| 5232 | * If set an empty list, remove all handles from the specific display. |
| 5233 | * For focused handle, check if need to change and send a cancel event to previous one. |
| 5234 | * For removed handle, check if need to send a cancel event if already in touch. |
| 5235 | */ |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5236 | void InputDispatcher::setInputWindowsLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5237 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5238 | if (DEBUG_FOCUS) { |
| 5239 | std::string windowList; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5240 | for (const sp<WindowInfoHandle>& iwh : windowInfoHandles) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5241 | windowList += iwh->getName() + " "; |
| 5242 | } |
Siarhei Vishniakou | 366fb5b | 2023-12-06 11:23:41 -0800 | [diff] [blame] | 5243 | LOG(INFO) << "setInputWindows displayId=" << displayId << " " << windowList; |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5244 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5245 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 5246 | // Check preconditions for new input windows |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5247 | for (const sp<WindowInfoHandle>& window : windowInfoHandles) { |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 5248 | const WindowInfo& info = *window->getInfo(); |
| 5249 | |
| 5250 | // 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] | 5251 | const bool noInputWindow = info.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5252 | if (noInputWindow && window->getToken() != nullptr) { |
| 5253 | ALOGE("%s has feature NO_INPUT_WINDOW, but a non-null token. Clearing", |
| 5254 | window->getName().c_str()); |
| 5255 | window->releaseChannel(); |
| 5256 | } |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 5257 | |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 5258 | // Ensure all spy windows are trusted overlays |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5259 | LOG_ALWAYS_FATAL_IF(info.isSpy() && |
| 5260 | !info.inputConfig.test( |
| 5261 | WindowInfo::InputConfig::TRUSTED_OVERLAY), |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 5262 | "%s has feature SPY, but is not a trusted overlay.", |
| 5263 | window->getName().c_str()); |
| 5264 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 5265 | // Ensure all stylus interceptors are trusted overlays |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5266 | LOG_ALWAYS_FATAL_IF(info.interceptsStylus() && |
| 5267 | !info.inputConfig.test( |
| 5268 | WindowInfo::InputConfig::TRUSTED_OVERLAY), |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 5269 | "%s has feature INTERCEPTS_STYLUS, but is not a trusted overlay.", |
| 5270 | window->getName().c_str()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5271 | } |
| 5272 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5273 | // Copy old handles for release if they are no longer present. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5274 | const std::vector<sp<WindowInfoHandle>> oldWindowHandles = getWindowHandlesLocked(displayId); |
Prabir Pradhan | 4b9b1a1 | 2024-02-02 00:16:17 +0000 | [diff] [blame] | 5275 | const sp<WindowInfoHandle> removedFocusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5276 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5277 | updateWindowHandlesForDisplayLocked(windowInfoHandles, displayId); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5278 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5279 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5280 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5281 | std::optional<FocusResolver::FocusChanges> changes = |
| 5282 | mFocusResolver.setInputWindows(displayId, windowHandles); |
| 5283 | if (changes) { |
Prabir Pradhan | 4b9b1a1 | 2024-02-02 00:16:17 +0000 | [diff] [blame] | 5284 | onFocusChangedLocked(*changes, removedFocusedWindowHandle); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5285 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5286 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5287 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 5288 | mTouchStatesByDisplay.find(displayId); |
| 5289 | if (stateIt != mTouchStatesByDisplay.end()) { |
| 5290 | TouchState& state = stateIt->second; |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5291 | for (size_t i = 0; i < state.windows.size();) { |
| 5292 | TouchedWindow& touchedWindow = state.windows[i]; |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 5293 | if (getWindowHandleLocked(touchedWindow.windowHandle) == nullptr) { |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 5294 | LOG(INFO) << "Touched window was removed: " << touchedWindow.windowHandle->getName() |
| 5295 | << " in display %" << displayId; |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 5296 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
| 5297 | "touched window was removed"); |
| 5298 | synthesizeCancelationEventsForWindowLocked(touchedWindow.windowHandle, options); |
| 5299 | // Since we are about to drop the touch, cancel the events for the wallpaper as |
| 5300 | // well. |
| 5301 | if (touchedWindow.targetFlags.test(InputTarget::Flags::FOREGROUND) && |
| 5302 | touchedWindow.windowHandle->getInfo()->inputConfig.test( |
| 5303 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER)) { |
| 5304 | if (const auto& ww = state.getWallpaperWindow(); ww) { |
| 5305 | synthesizeCancelationEventsForWindowLocked(ww, options); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5306 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5307 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5308 | state.windows.erase(state.windows.begin() + i); |
| 5309 | } else { |
| 5310 | ++i; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5311 | } |
| 5312 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5313 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5314 | // 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] | 5315 | // could just clear the state here. |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 5316 | if (mDragState && mDragState->dragWindow->getInfo()->displayId == displayId && |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5317 | std::find(windowHandles.begin(), windowHandles.end(), mDragState->dragWindow) == |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5318 | windowHandles.end()) { |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 5319 | ALOGI("Drag window went away: %s", mDragState->dragWindow->getName().c_str()); |
| 5320 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5321 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5322 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5323 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 5324 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5325 | // Release information for windows that are no longer present. |
| 5326 | // This ensures that unused input channels are released promptly. |
| 5327 | // Otherwise, they might stick around until the window handle is destroyed |
| 5328 | // which might not happen until the next GC. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5329 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 5330 | if (getWindowHandleLocked(oldWindowHandle) == nullptr) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5331 | if (DEBUG_FOCUS) { |
| 5332 | ALOGD("Window went away: %s", oldWindowHandle->getName().c_str()); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 5333 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5334 | oldWindowHandle->releaseChannel(); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 5335 | } |
chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 5336 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5337 | } |
| 5338 | |
| 5339 | void InputDispatcher::setFocusedApplication( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5340 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5341 | if (DEBUG_FOCUS) { |
| 5342 | ALOGD("setFocusedApplication displayId=%" PRId32 " %s", displayId, |
| 5343 | inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>"); |
| 5344 | } |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 5345 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5346 | std::scoped_lock _l(mLock); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 5347 | setFocusedApplicationLocked(displayId, inputApplicationHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5348 | } // release lock |
| 5349 | |
| 5350 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5351 | mLooper->wake(); |
| 5352 | } |
| 5353 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 5354 | void InputDispatcher::setFocusedApplicationLocked( |
| 5355 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
| 5356 | std::shared_ptr<InputApplicationHandle> oldFocusedApplicationHandle = |
| 5357 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 5358 | |
| 5359 | if (sharedPointersEqual(oldFocusedApplicationHandle, inputApplicationHandle)) { |
| 5360 | return; // This application is already focused. No need to wake up or change anything. |
| 5361 | } |
| 5362 | |
| 5363 | // Set the new application handle. |
| 5364 | if (inputApplicationHandle != nullptr) { |
| 5365 | mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle; |
| 5366 | } else { |
| 5367 | mFocusedApplicationHandlesByDisplay.erase(displayId); |
| 5368 | } |
| 5369 | |
| 5370 | // No matter what the old focused application was, stop waiting on it because it is |
| 5371 | // no longer focused. |
| 5372 | resetNoFocusedWindowTimeoutLocked(); |
| 5373 | } |
| 5374 | |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 5375 | void InputDispatcher::setMinTimeBetweenUserActivityPokes(std::chrono::milliseconds interval) { |
| 5376 | if (interval.count() < 0) { |
| 5377 | LOG_ALWAYS_FATAL("Minimum time between user activity pokes should be >= 0"); |
| 5378 | } |
| 5379 | std::scoped_lock _l(mLock); |
| 5380 | mMinTimeBetweenUserActivityPokes = interval; |
| 5381 | } |
| 5382 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5383 | /** |
| 5384 | * Sets the focused display, which is responsible for receiving focus-dispatched input events where |
| 5385 | * the display not specified. |
| 5386 | * |
| 5387 | * We track any unreleased events for each window. If a window loses the ability to receive the |
| 5388 | * released event, we will send a cancel event to it. So when the focused display is changed, we |
| 5389 | * cancel all the unreleased display-unspecified events for the focused window on the old focused |
| 5390 | * display. The display-specified events won't be affected. |
| 5391 | */ |
| 5392 | void InputDispatcher::setFocusedDisplay(int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5393 | if (DEBUG_FOCUS) { |
| 5394 | ALOGD("setFocusedDisplay displayId=%" PRId32, displayId); |
| 5395 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5396 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5397 | std::scoped_lock _l(mLock); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5398 | |
| 5399 | if (mFocusedDisplayId != displayId) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5400 | sp<IBinder> oldFocusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5401 | mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5402 | if (oldFocusedWindowToken != nullptr) { |
Prabir Pradhan | 4b9b1a1 | 2024-02-02 00:16:17 +0000 | [diff] [blame] | 5403 | const auto windowHandle = |
| 5404 | getWindowHandleLocked(oldFocusedWindowToken, mFocusedDisplayId); |
| 5405 | if (windowHandle == nullptr) { |
| 5406 | LOG(FATAL) << __func__ << ": Previously focused token did not have a window"; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5407 | } |
Prabir Pradhan | 4b9b1a1 | 2024-02-02 00:16:17 +0000 | [diff] [blame] | 5408 | CancelationOptions |
| 5409 | options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, |
| 5410 | "The display which contains this window no longer has focus."); |
| 5411 | options.displayId = ADISPLAY_ID_NONE; |
| 5412 | synthesizeCancelationEventsForWindowLocked(windowHandle, options); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5413 | } |
| 5414 | mFocusedDisplayId = displayId; |
| 5415 | |
Chris Ye | 3c2d6f5 | 2020-08-09 10:39:48 -0700 | [diff] [blame] | 5416 | // Find new focused window and validate |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5417 | sp<IBinder> newFocusedWindowToken = mFocusResolver.getFocusedWindowToken(displayId); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5418 | sendFocusChangedCommandLocked(oldFocusedWindowToken, newFocusedWindowToken); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5419 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5420 | if (newFocusedWindowToken == nullptr) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5421 | ALOGW("Focused display #%" PRId32 " does not have a focused window.", displayId); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5422 | if (mFocusResolver.hasFocusedWindowTokens()) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5423 | ALOGE("But another display has a focused window\n%s", |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5424 | mFocusResolver.dumpFocusedWindows().c_str()); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5425 | } |
| 5426 | } |
| 5427 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5428 | } // release lock |
| 5429 | |
| 5430 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5431 | mLooper->wake(); |
| 5432 | } |
| 5433 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5434 | void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5435 | if (DEBUG_FOCUS) { |
| 5436 | ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); |
| 5437 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5438 | |
| 5439 | bool changed; |
| 5440 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5441 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5442 | |
| 5443 | if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { |
| 5444 | if (mDispatchFrozen && !frozen) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5445 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5446 | } |
| 5447 | |
| 5448 | if (mDispatchEnabled && !enabled) { |
| 5449 | resetAndDropEverythingLocked("dispatcher is being disabled"); |
| 5450 | } |
| 5451 | |
| 5452 | mDispatchEnabled = enabled; |
| 5453 | mDispatchFrozen = frozen; |
| 5454 | changed = true; |
| 5455 | } else { |
| 5456 | changed = false; |
| 5457 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5458 | } // release lock |
| 5459 | |
| 5460 | if (changed) { |
| 5461 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5462 | mLooper->wake(); |
| 5463 | } |
| 5464 | } |
| 5465 | |
| 5466 | void InputDispatcher::setInputFilterEnabled(bool enabled) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5467 | if (DEBUG_FOCUS) { |
| 5468 | ALOGD("setInputFilterEnabled: enabled=%d", enabled); |
| 5469 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5470 | |
| 5471 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5472 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5473 | |
| 5474 | if (mInputFilterEnabled == enabled) { |
| 5475 | return; |
| 5476 | } |
| 5477 | |
| 5478 | mInputFilterEnabled = enabled; |
| 5479 | resetAndDropEverythingLocked("input filter is being enabled or disabled"); |
| 5480 | } // release lock |
| 5481 | |
| 5482 | // Wake up poll loop since there might be work to do to drop everything. |
| 5483 | mLooper->wake(); |
| 5484 | } |
| 5485 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5486 | bool InputDispatcher::setInTouchMode(bool inTouchMode, gui::Pid pid, gui::Uid uid, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 5487 | bool hasPermission, int32_t displayId) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5488 | bool needWake = false; |
| 5489 | { |
| 5490 | std::scoped_lock lock(mLock); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5491 | ALOGD_IF(DEBUG_TOUCH_MODE, |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5492 | "Request to change touch mode to %s (calling pid=%s, uid=%s, " |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5493 | "hasPermission=%s, target displayId=%d, mTouchModePerDisplay[displayId]=%s)", |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5494 | toString(inTouchMode), pid.toString().c_str(), uid.toString().c_str(), |
| 5495 | toString(hasPermission), displayId, |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5496 | mTouchModePerDisplay.count(displayId) == 0 |
| 5497 | ? "not set" |
| 5498 | : std::to_string(mTouchModePerDisplay[displayId]).c_str()); |
| 5499 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5500 | auto touchModeIt = mTouchModePerDisplay.find(displayId); |
| 5501 | if (touchModeIt != mTouchModePerDisplay.end() && touchModeIt->second == inTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5502 | return false; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5503 | } |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5504 | if (!hasPermission) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 5505 | if (!focusedWindowIsOwnedByLocked(pid, uid) && |
| 5506 | !recentWindowsAreOwnedByLocked(pid, uid)) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5507 | 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] | 5508 | "window nor none of the previously interacted window", |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5509 | pid.toString().c_str(), uid.toString().c_str()); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5510 | return false; |
| 5511 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5512 | } |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5513 | mTouchModePerDisplay[displayId] = inTouchMode; |
| 5514 | auto entry = std::make_unique<TouchModeEntry>(mIdGenerator.nextId(), now(), inTouchMode, |
| 5515 | displayId); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5516 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 5517 | } // release lock |
| 5518 | |
| 5519 | if (needWake) { |
| 5520 | mLooper->wake(); |
| 5521 | } |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5522 | return true; |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 5523 | } |
| 5524 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5525 | bool InputDispatcher::focusedWindowIsOwnedByLocked(gui::Pid pid, gui::Uid uid) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 5526 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
| 5527 | if (focusedToken == nullptr) { |
| 5528 | return false; |
| 5529 | } |
| 5530 | sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(focusedToken); |
| 5531 | return isWindowOwnedBy(windowHandle, pid, uid); |
| 5532 | } |
| 5533 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5534 | bool InputDispatcher::recentWindowsAreOwnedByLocked(gui::Pid pid, gui::Uid uid) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 5535 | return std::find_if(mInteractionConnectionTokens.begin(), mInteractionConnectionTokens.end(), |
| 5536 | [&](const sp<IBinder>& connectionToken) REQUIRES(mLock) { |
| 5537 | const sp<WindowInfoHandle> windowHandle = |
| 5538 | getWindowHandleLocked(connectionToken); |
| 5539 | return isWindowOwnedBy(windowHandle, pid, uid); |
| 5540 | }) != mInteractionConnectionTokens.end(); |
| 5541 | } |
| 5542 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 5543 | void InputDispatcher::setMaximumObscuringOpacityForTouch(float opacity) { |
| 5544 | if (opacity < 0 || opacity > 1) { |
| 5545 | LOG_ALWAYS_FATAL("Maximum obscuring opacity for touch should be >= 0 and <= 1"); |
| 5546 | return; |
| 5547 | } |
| 5548 | |
| 5549 | std::scoped_lock lock(mLock); |
| 5550 | mMaximumObscuringOpacityForTouch = opacity; |
| 5551 | } |
| 5552 | |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5553 | std::tuple<TouchState*, TouchedWindow*, int32_t /*displayId*/> |
| 5554 | InputDispatcher::findTouchStateWindowAndDisplayLocked(const sp<IBinder>& token) { |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5555 | for (auto& [displayId, state] : mTouchStatesByDisplay) { |
| 5556 | for (TouchedWindow& w : state.windows) { |
| 5557 | if (w.windowHandle->getToken() == token) { |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5558 | return std::make_tuple(&state, &w, displayId); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5559 | } |
| 5560 | } |
| 5561 | } |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5562 | return std::make_tuple(nullptr, nullptr, ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5563 | } |
| 5564 | |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame^] | 5565 | bool InputDispatcher::transferTouchGesture(const sp<IBinder>& fromToken, const sp<IBinder>& toToken, |
| 5566 | bool isDragDrop) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 5567 | if (fromToken == toToken) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5568 | if (DEBUG_FOCUS) { |
| 5569 | ALOGD("Trivial transfer to same window."); |
| 5570 | } |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 5571 | return true; |
| 5572 | } |
| 5573 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5574 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5575 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5576 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5577 | // Find the target touch state and touched window by fromToken. |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5578 | auto [state, touchedWindow, displayId] = findTouchStateWindowAndDisplayLocked(fromToken); |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5579 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5580 | if (state == nullptr || touchedWindow == nullptr) { |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5581 | ALOGD("Touch transfer failed because from window is not being touched."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5582 | return false; |
| 5583 | } |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5584 | std::set<int32_t> deviceIds = touchedWindow->getTouchingDeviceIds(); |
| 5585 | if (deviceIds.size() != 1) { |
Siarhei Vishniakou | 827d1ac | 2023-07-21 16:37:51 -0700 | [diff] [blame] | 5586 | LOG(INFO) << "Can't transfer touch. Currently touching devices: " << dumpSet(deviceIds) |
| 5587 | << " for window: " << touchedWindow->dump(); |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5588 | return false; |
| 5589 | } |
| 5590 | const int32_t deviceId = *deviceIds.begin(); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5591 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 5592 | const sp<WindowInfoHandle> fromWindowHandle = touchedWindow->windowHandle; |
| 5593 | const sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(toToken, displayId); |
| 5594 | if (!toWindowHandle) { |
| 5595 | ALOGW("Cannot transfer touch because the transfer target window was not found."); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5596 | return false; |
| 5597 | } |
| 5598 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5599 | if (DEBUG_FOCUS) { |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame^] | 5600 | ALOGD("%s: fromWindowHandle=%s, toWindowHandle=%s", __func__, |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5601 | touchedWindow->windowHandle->getName().c_str(), |
| 5602 | toWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5603 | } |
| 5604 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5605 | // Erase old window. |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5606 | ftl::Flags<InputTarget::Flags> oldTargetFlags = touchedWindow->targetFlags; |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 5607 | std::vector<PointerProperties> pointers = touchedWindow->getTouchingPointers(deviceId); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5608 | state->removeWindowByToken(fromToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5609 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5610 | // Add new window. |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 5611 | nsecs_t downTimeInTarget = now(); |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5612 | ftl::Flags<InputTarget::Flags> newTargetFlags = |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 5613 | oldTargetFlags & (InputTarget::Flags::SPLIT); |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 5614 | if (canReceiveForegroundTouches(*toWindowHandle->getInfo())) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5615 | newTargetFlags |= InputTarget::Flags::FOREGROUND; |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 5616 | } |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 5617 | // Transferring touch focus using this API should not effect the focused window. |
| 5618 | newTargetFlags |= InputTarget::Flags::NO_FOCUS_CHANGE; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 5619 | state->addOrUpdateWindow(toWindowHandle, InputTarget::DispatchMode::AS_IS, newTargetFlags, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 5620 | deviceId, pointers, downTimeInTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5621 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5622 | // Store the dragging window. |
| 5623 | if (isDragDrop) { |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 5624 | if (pointers.size() != 1) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 5625 | ALOGW("The drag and drop cannot be started when there is no pointer or more than 1" |
| 5626 | " pointer on the window."); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 5627 | return false; |
| 5628 | } |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 5629 | // Track the pointer id for drag window and generate the drag state. |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 5630 | const size_t id = pointers.begin()->id; |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 5631 | mDragState = std::make_unique<DragState>(toWindowHandle, id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5632 | } |
| 5633 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5634 | // Synthesize cancel for old window and down for new window. |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 5635 | std::shared_ptr<Connection> fromConnection = getConnectionLocked(fromToken); |
| 5636 | std::shared_ptr<Connection> toConnection = getConnectionLocked(toToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5637 | if (fromConnection != nullptr && toConnection != nullptr) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5638 | fromConnection->inputState.mergePointerStateTo(toConnection->inputState); |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5639 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
| 5640 | "transferring touch from this window to another window"); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 5641 | synthesizeCancelationEventsForWindowLocked(fromWindowHandle, options, fromConnection); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5642 | synthesizePointerDownEventsForConnectionLocked(downTimeInTarget, toConnection, |
| 5643 | newTargetFlags); |
| 5644 | |
| 5645 | // Check if the wallpaper window should deliver the corresponding event. |
| 5646 | transferWallpaperTouch(oldTargetFlags, newTargetFlags, fromWindowHandle, toWindowHandle, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 5647 | *state, deviceId, pointers); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5648 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5649 | } // release lock |
| 5650 | |
| 5651 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5652 | mLooper->wake(); |
| 5653 | return true; |
| 5654 | } |
| 5655 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5656 | /** |
| 5657 | * Get the touched foreground window on the given display. |
| 5658 | * Return null if there are no windows touched on that display, or if more than one foreground |
| 5659 | * window is being touched. |
| 5660 | */ |
| 5661 | sp<WindowInfoHandle> InputDispatcher::findTouchedForegroundWindowLocked(int32_t displayId) const { |
| 5662 | auto stateIt = mTouchStatesByDisplay.find(displayId); |
| 5663 | if (stateIt == mTouchStatesByDisplay.end()) { |
| 5664 | ALOGI("No touch state on display %" PRId32, displayId); |
| 5665 | return nullptr; |
| 5666 | } |
| 5667 | |
| 5668 | const TouchState& state = stateIt->second; |
| 5669 | sp<WindowInfoHandle> touchedForegroundWindow; |
| 5670 | // If multiple foreground windows are touched, return nullptr |
| 5671 | for (const TouchedWindow& window : state.windows) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5672 | if (window.targetFlags.test(InputTarget::Flags::FOREGROUND)) { |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5673 | if (touchedForegroundWindow != nullptr) { |
| 5674 | ALOGI("Two or more foreground windows: %s and %s", |
| 5675 | touchedForegroundWindow->getName().c_str(), |
| 5676 | window.windowHandle->getName().c_str()); |
| 5677 | return nullptr; |
| 5678 | } |
| 5679 | touchedForegroundWindow = window.windowHandle; |
| 5680 | } |
| 5681 | } |
| 5682 | return touchedForegroundWindow; |
| 5683 | } |
| 5684 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5685 | // Binder call |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame^] | 5686 | bool InputDispatcher::transferTouchOnDisplay(const sp<IBinder>& destChannelToken, |
| 5687 | int32_t displayId) { |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5688 | sp<IBinder> fromToken; |
| 5689 | { // acquire lock |
| 5690 | std::scoped_lock _l(mLock); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5691 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(destChannelToken, displayId); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5692 | if (toWindowHandle == nullptr) { |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5693 | ALOGW("Could not find window associated with token=%p on display %" PRId32, |
| 5694 | destChannelToken.get(), displayId); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5695 | return false; |
| 5696 | } |
| 5697 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5698 | sp<WindowInfoHandle> from = findTouchedForegroundWindowLocked(displayId); |
| 5699 | if (from == nullptr) { |
| 5700 | ALOGE("Could not find a source window in %s for %p", __func__, destChannelToken.get()); |
| 5701 | return false; |
| 5702 | } |
| 5703 | |
| 5704 | fromToken = from->getToken(); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5705 | } // release lock |
| 5706 | |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame^] | 5707 | return transferTouchGesture(fromToken, destChannelToken); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5708 | } |
| 5709 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5710 | void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5711 | if (DEBUG_FOCUS) { |
| 5712 | ALOGD("Resetting and dropping all events (%s).", reason); |
| 5713 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5714 | |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 5715 | CancelationOptions options(CancelationOptions::Mode::CANCEL_ALL_EVENTS, reason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5716 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 5717 | |
| 5718 | resetKeyRepeatLocked(); |
| 5719 | releasePendingEventLocked(); |
| 5720 | drainInboundQueueLocked(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5721 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5722 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5723 | mAnrTracker.clear(); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5724 | mTouchStatesByDisplay.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5725 | } |
| 5726 | |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 5727 | void InputDispatcher::logDispatchStateLocked() const { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5728 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5729 | dumpDispatchStateLocked(dump); |
| 5730 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5731 | std::istringstream stream(dump); |
| 5732 | std::string line; |
| 5733 | |
| 5734 | while (std::getline(stream, line, '\n')) { |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 5735 | ALOGI("%s", line.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5736 | } |
| 5737 | } |
| 5738 | |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 5739 | std::string InputDispatcher::dumpPointerCaptureStateLocked() const { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5740 | std::string dump; |
| 5741 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5742 | dump += StringPrintf(INDENT "Pointer Capture Requested: %s\n", |
| 5743 | toString(mCurrentPointerCaptureRequest.enable)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5744 | |
| 5745 | std::string windowName = "None"; |
| 5746 | if (mWindowTokenWithPointerCapture) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5747 | const sp<WindowInfoHandle> captureWindowHandle = |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5748 | getWindowHandleLocked(mWindowTokenWithPointerCapture); |
| 5749 | windowName = captureWindowHandle ? captureWindowHandle->getName().c_str() |
| 5750 | : "token has capture without window"; |
| 5751 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5752 | 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] | 5753 | |
| 5754 | return dump; |
| 5755 | } |
| 5756 | |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 5757 | void InputDispatcher::dumpDispatchStateLocked(std::string& dump) const { |
Siarhei Vishniakou | 043a3ec | 2019-05-01 11:30:46 -0700 | [diff] [blame] | 5758 | dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled)); |
| 5759 | dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen)); |
| 5760 | dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5761 | dump += StringPrintf(INDENT "FocusedDisplayId: %" PRId32 "\n", mFocusedDisplayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5762 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5763 | if (!mFocusedApplicationHandlesByDisplay.empty()) { |
| 5764 | dump += StringPrintf(INDENT "FocusedApplications:\n"); |
| 5765 | for (auto& it : mFocusedApplicationHandlesByDisplay) { |
| 5766 | const int32_t displayId = it.first; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5767 | const std::shared_ptr<InputApplicationHandle>& applicationHandle = it.second; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5768 | const std::chrono::duration timeout = |
| 5769 | applicationHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5770 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5771 | ", name='%s', dispatchingTimeout=%" PRId64 "ms\n", |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5772 | displayId, applicationHandle->getName().c_str(), millis(timeout)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5773 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5774 | } else { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5775 | dump += StringPrintf(INDENT "FocusedApplications: <none>\n"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5776 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5777 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5778 | dump += mFocusResolver.dump(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5779 | dump += dumpPointerCaptureStateLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5780 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5781 | if (!mTouchStatesByDisplay.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5782 | dump += StringPrintf(INDENT "TouchStatesByDisplay:\n"); |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5783 | for (const auto& [displayId, state] : mTouchStatesByDisplay) { |
Siarhei Vishniakou | 6e1e987 | 2022-11-08 17:51:35 -0800 | [diff] [blame] | 5784 | std::string touchStateDump = addLinePrefix(state.dump(), INDENT2); |
| 5785 | dump += INDENT2 + std::to_string(displayId) + " : " + touchStateDump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5786 | } |
| 5787 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5788 | dump += INDENT "TouchStates: <no displays touched>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5789 | } |
| 5790 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5791 | if (mDragState) { |
| 5792 | dump += StringPrintf(INDENT "DragState:\n"); |
| 5793 | mDragState->dump(dump, INDENT2); |
| 5794 | } |
| 5795 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5796 | if (!mWindowHandlesByDisplay.empty()) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5797 | for (const auto& [displayId, windowHandles] : mWindowHandlesByDisplay) { |
| 5798 | dump += StringPrintf(INDENT "Display: %" PRId32 "\n", displayId); |
| 5799 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
| 5800 | const auto& displayInfo = it->second; |
| 5801 | dump += StringPrintf(INDENT2 "logicalSize=%dx%d\n", displayInfo.logicalWidth, |
| 5802 | displayInfo.logicalHeight); |
| 5803 | displayInfo.transform.dump(dump, "transform", INDENT4); |
| 5804 | } else { |
| 5805 | dump += INDENT2 "No DisplayInfo found!\n"; |
| 5806 | } |
| 5807 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5808 | if (!windowHandles.empty()) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5809 | dump += INDENT2 "Windows:\n"; |
| 5810 | for (size_t i = 0; i < windowHandles.size(); i++) { |
Siarhei Vishniakou | 366fb5b | 2023-12-06 11:23:41 -0800 | [diff] [blame] | 5811 | dump += StringPrintf(INDENT3 "%zu: %s", i, |
| 5812 | streamableToString(*windowHandles[i]).c_str()); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5813 | } |
| 5814 | } else { |
| 5815 | dump += INDENT2 "Windows: <none>\n"; |
| 5816 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5817 | } |
| 5818 | } else { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5819 | dump += INDENT "Displays: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5820 | } |
| 5821 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5822 | if (!mGlobalMonitorsByDisplay.empty()) { |
| 5823 | for (const auto& [displayId, monitors] : mGlobalMonitorsByDisplay) { |
| 5824 | dump += StringPrintf(INDENT "Global monitors on display %d:\n", displayId); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5825 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5826 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5827 | } else { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5828 | dump += INDENT "Global Monitors: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5829 | } |
| 5830 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5831 | const nsecs_t currentTime = now(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5832 | |
| 5833 | // Dump recently dispatched or dropped events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5834 | if (!mRecentQueue.empty()) { |
| 5835 | dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size()); |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 5836 | for (const std::shared_ptr<const EventEntry>& entry : mRecentQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5837 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5838 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5839 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5840 | } |
| 5841 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5842 | dump += INDENT "RecentQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5843 | } |
| 5844 | |
| 5845 | // Dump event currently being dispatched. |
| 5846 | if (mPendingEvent) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5847 | dump += INDENT "PendingEvent:\n"; |
| 5848 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5849 | dump += mPendingEvent->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5850 | dump += StringPrintf(", age=%" PRId64 "ms\n", |
| 5851 | ns2ms(currentTime - mPendingEvent->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5852 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5853 | dump += INDENT "PendingEvent: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5854 | } |
| 5855 | |
| 5856 | // Dump inbound events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5857 | if (!mInboundQueue.empty()) { |
| 5858 | dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size()); |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 5859 | for (const std::shared_ptr<const EventEntry>& entry : mInboundQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5860 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5861 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5862 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5863 | } |
| 5864 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5865 | dump += INDENT "InboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5866 | } |
| 5867 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5868 | if (!mCommandQueue.empty()) { |
| 5869 | dump += StringPrintf(INDENT "CommandQueue: size=%zu\n", mCommandQueue.size()); |
| 5870 | } else { |
| 5871 | dump += INDENT "CommandQueue: <empty>\n"; |
| 5872 | } |
| 5873 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5874 | if (!mConnectionsByToken.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5875 | dump += INDENT "Connections:\n"; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5876 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 10bfdc9 | 2024-02-01 11:12:57 -0800 | [diff] [blame] | 5877 | dump += StringPrintf(INDENT2 "%i: channelName='%s', " |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5878 | "status=%s, monitor=%s, responsive=%s\n", |
Siarhei Vishniakou | 7b9f4f5 | 2024-02-02 13:07:16 -0800 | [diff] [blame] | 5879 | connection->inputPublisher.getChannel().getFd(), |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5880 | connection->getInputChannelName().c_str(), |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5881 | ftl::enum_string(connection->status).c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5882 | toString(connection->monitor), toString(connection->responsive)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5883 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5884 | if (!connection->outboundQueue.empty()) { |
| 5885 | dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n", |
| 5886 | connection->outboundQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5887 | dump += dumpQueue(connection->outboundQueue, currentTime); |
| 5888 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5889 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5890 | dump += INDENT3 "OutboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5891 | } |
| 5892 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5893 | if (!connection->waitQueue.empty()) { |
| 5894 | dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n", |
| 5895 | connection->waitQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5896 | dump += dumpQueue(connection->waitQueue, currentTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5897 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5898 | dump += INDENT3 "WaitQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5899 | } |
Siarhei Vishniakou | 366fb5b | 2023-12-06 11:23:41 -0800 | [diff] [blame] | 5900 | std::string inputStateDump = streamableToString(connection->inputState); |
| 5901 | if (!inputStateDump.empty()) { |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 5902 | dump += INDENT3 "InputState: "; |
Siarhei Vishniakou | 366fb5b | 2023-12-06 11:23:41 -0800 | [diff] [blame] | 5903 | dump += inputStateDump + "\n"; |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 5904 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5905 | } |
| 5906 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5907 | dump += INDENT "Connections: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5908 | } |
| 5909 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5910 | if (!mTouchModePerDisplay.empty()) { |
| 5911 | dump += INDENT "TouchModePerDisplay:\n"; |
| 5912 | for (const auto& [displayId, touchMode] : mTouchModePerDisplay) { |
| 5913 | dump += StringPrintf(INDENT2 "Display: %" PRId32 " TouchMode: %s\n", displayId, |
| 5914 | std::to_string(touchMode).c_str()); |
| 5915 | } |
| 5916 | } else { |
| 5917 | dump += INDENT "TouchModePerDisplay: <none>\n"; |
| 5918 | } |
| 5919 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5920 | dump += INDENT "Configuration:\n"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5921 | dump += StringPrintf(INDENT2 "KeyRepeatDelay: %" PRId64 "ms\n", ns2ms(mConfig.keyRepeatDelay)); |
| 5922 | dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n", |
| 5923 | ns2ms(mConfig.keyRepeatTimeout)); |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 5924 | dump += mLatencyTracker.dump(INDENT2); |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 5925 | dump += mLatencyAggregator.dump(INDENT2); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 5926 | dump += INDENT "InputTracer: "; |
| 5927 | dump += mTracer == nullptr ? "Disabled" : "Enabled"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5928 | } |
| 5929 | |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 5930 | void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) const { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5931 | const size_t numMonitors = monitors.size(); |
| 5932 | for (size_t i = 0; i < numMonitors; i++) { |
| 5933 | const Monitor& monitor = monitors[i]; |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 5934 | const std::shared_ptr<Connection>& connection = monitor.connection; |
| 5935 | dump += StringPrintf(INDENT2 "%zu: '%s', ", i, connection->getInputChannelName().c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5936 | dump += "\n"; |
| 5937 | } |
| 5938 | } |
| 5939 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5940 | class LooperEventCallback : public LooperCallback { |
| 5941 | public: |
| 5942 | LooperEventCallback(std::function<int(int events)> callback) : mCallback(callback) {} |
| 5943 | int handleEvent(int /*fd*/, int events, void* /*data*/) override { return mCallback(events); } |
| 5944 | |
| 5945 | private: |
| 5946 | std::function<int(int events)> mCallback; |
| 5947 | }; |
| 5948 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5949 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputChannel(const std::string& name) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5950 | if (DEBUG_CHANNEL_CREATION) { |
| 5951 | ALOGD("channel '%s' ~ createInputChannel", name.c_str()); |
| 5952 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5953 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5954 | std::unique_ptr<InputChannel> serverChannel; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5955 | std::unique_ptr<InputChannel> clientChannel; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5956 | status_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5957 | |
| 5958 | if (result) { |
| 5959 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5960 | } |
| 5961 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5962 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5963 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5964 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Siarhei Vishniakou | 8d66013 | 2024-01-11 16:48:44 -0800 | [diff] [blame] | 5965 | const int fd = serverChannel->getFd(); |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 5966 | std::shared_ptr<Connection> connection = |
| 5967 | std::make_shared<Connection>(std::move(serverChannel), /*monitor=*/false, |
| 5968 | mIdGenerator); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5969 | |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 5970 | auto [_, inserted] = mConnectionsByToken.try_emplace(token, connection); |
| 5971 | if (!inserted) { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5972 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5973 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5974 | |
| 5975 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5976 | this, std::placeholders::_1, token); |
| 5977 | |
Siarhei Vishniakou | 8d66013 | 2024-01-11 16:48:44 -0800 | [diff] [blame] | 5978 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, sp<LooperEventCallback>::make(callback), |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5979 | nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5980 | } // release lock |
| 5981 | |
| 5982 | // Wake the looper because some connections have changed. |
| 5983 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5984 | return clientChannel; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5985 | } |
| 5986 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5987 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputMonitor(int32_t displayId, |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5988 | const std::string& name, |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5989 | gui::Pid pid) { |
Siarhei Vishniakou | 7b9f4f5 | 2024-02-02 13:07:16 -0800 | [diff] [blame] | 5990 | std::unique_ptr<InputChannel> serverChannel; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5991 | std::unique_ptr<InputChannel> clientChannel; |
Siarhei Vishniakou | 7b9f4f5 | 2024-02-02 13:07:16 -0800 | [diff] [blame] | 5992 | status_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5993 | if (result) { |
| 5994 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5995 | } |
| 5996 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5997 | { // acquire lock |
| 5998 | std::scoped_lock _l(mLock); |
| 5999 | |
| 6000 | if (displayId < 0) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 6001 | return base::Error(BAD_VALUE) << "Attempted to create input monitor with name " << name |
| 6002 | << " without a specified display."; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6003 | } |
| 6004 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 6005 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Siarhei Vishniakou | 8d66013 | 2024-01-11 16:48:44 -0800 | [diff] [blame] | 6006 | const int fd = serverChannel->getFd(); |
Siarhei Vishniakou | 7b9f4f5 | 2024-02-02 13:07:16 -0800 | [diff] [blame] | 6007 | std::shared_ptr<Connection> connection = |
| 6008 | std::make_shared<Connection>(std::move(serverChannel), /*monitor=*/true, |
| 6009 | mIdGenerator); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 6010 | |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6011 | auto [_, inserted] = mConnectionsByToken.emplace(token, connection); |
| 6012 | if (!inserted) { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 6013 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 6014 | } |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6015 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 6016 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 6017 | this, std::placeholders::_1, token); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6018 | |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6019 | mGlobalMonitorsByDisplay[displayId].emplace_back(connection, pid); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6020 | |
Siarhei Vishniakou | 8d66013 | 2024-01-11 16:48:44 -0800 | [diff] [blame] | 6021 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, sp<LooperEventCallback>::make(callback), |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6022 | nullptr); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6023 | } |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 6024 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6025 | // Wake the looper because some connections have changed. |
| 6026 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 6027 | return clientChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6028 | } |
| 6029 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 6030 | status_t InputDispatcher::removeInputChannel(const sp<IBinder>& connectionToken) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6031 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6032 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6033 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6034 | status_t status = removeInputChannelLocked(connectionToken, /*notify=*/false); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6035 | if (status) { |
| 6036 | return status; |
| 6037 | } |
| 6038 | } // release lock |
| 6039 | |
| 6040 | // Wake the poll loop because removing the connection may have changed the current |
| 6041 | // synchronization state. |
| 6042 | mLooper->wake(); |
| 6043 | return OK; |
| 6044 | } |
| 6045 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 6046 | status_t InputDispatcher::removeInputChannelLocked(const sp<IBinder>& connectionToken, |
| 6047 | bool notify) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6048 | std::shared_ptr<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 6049 | if (connection == nullptr) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 6050 | // 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] | 6051 | return BAD_VALUE; |
| 6052 | } |
| 6053 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6054 | removeConnectionLocked(connection); |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 6055 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6056 | if (connection->monitor) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 6057 | removeMonitorChannelLocked(connectionToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6058 | } |
| 6059 | |
Siarhei Vishniakou | 7b9f4f5 | 2024-02-02 13:07:16 -0800 | [diff] [blame] | 6060 | mLooper->removeFd(connection->inputPublisher.getChannel().getFd()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6061 | |
| 6062 | nsecs_t currentTime = now(); |
| 6063 | abortBrokenDispatchCycleLocked(currentTime, connection, notify); |
| 6064 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 6065 | connection->status = Connection::Status::ZOMBIE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6066 | return OK; |
| 6067 | } |
| 6068 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 6069 | void InputDispatcher::removeMonitorChannelLocked(const sp<IBinder>& connectionToken) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6070 | for (auto it = mGlobalMonitorsByDisplay.begin(); it != mGlobalMonitorsByDisplay.end();) { |
| 6071 | auto& [displayId, monitors] = *it; |
| 6072 | std::erase_if(monitors, [connectionToken](const Monitor& monitor) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6073 | return monitor.connection->getToken() == connectionToken; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6074 | }); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6075 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6076 | if (monitors.empty()) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6077 | it = mGlobalMonitorsByDisplay.erase(it); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6078 | } else { |
| 6079 | ++it; |
| 6080 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6081 | } |
| 6082 | } |
| 6083 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6084 | status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6085 | std::scoped_lock _l(mLock); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 6086 | return pilferPointersLocked(token); |
| 6087 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6088 | |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 6089 | status_t InputDispatcher::pilferPointersLocked(const sp<IBinder>& token) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6090 | const std::shared_ptr<Connection> requestingConnection = getConnectionLocked(token); |
| 6091 | if (!requestingConnection) { |
Siarhei Vishniakou | 8384e0d | 2023-09-18 18:48:27 -0700 | [diff] [blame] | 6092 | LOG(WARNING) |
| 6093 | << "Attempted to pilfer pointers from an un-registered channel or invalid token"; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6094 | return BAD_VALUE; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6095 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6096 | |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 6097 | auto [statePtr, windowPtr, displayId] = findTouchStateWindowAndDisplayLocked(token); |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 6098 | if (statePtr == nullptr || windowPtr == nullptr) { |
Siarhei Vishniakou | 8384e0d | 2023-09-18 18:48:27 -0700 | [diff] [blame] | 6099 | LOG(WARNING) |
| 6100 | << "Attempted to pilfer points from a channel without any on-going pointer streams." |
| 6101 | " Ignoring."; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6102 | return BAD_VALUE; |
| 6103 | } |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 6104 | std::set<int32_t> deviceIds = windowPtr->getTouchingDeviceIds(); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 6105 | if (deviceIds.empty()) { |
| 6106 | LOG(WARNING) << "Can't pilfer: no touching devices in window: " << windowPtr->dump(); |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 6107 | return BAD_VALUE; |
| 6108 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6109 | |
Siarhei Vishniakou | 8384e0d | 2023-09-18 18:48:27 -0700 | [diff] [blame] | 6110 | for (const DeviceId deviceId : deviceIds) { |
| 6111 | TouchState& state = *statePtr; |
| 6112 | TouchedWindow& window = *windowPtr; |
| 6113 | // Send cancel events to all the input channels we're stealing from. |
| 6114 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
| 6115 | "input channel stole pointer stream"); |
| 6116 | options.deviceId = deviceId; |
| 6117 | options.displayId = displayId; |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 6118 | std::vector<PointerProperties> pointers = window.getTouchingPointers(deviceId); |
| 6119 | std::bitset<MAX_POINTER_ID + 1> pointerIds = getPointerIds(pointers); |
Siarhei Vishniakou | 8384e0d | 2023-09-18 18:48:27 -0700 | [diff] [blame] | 6120 | options.pointerIds = pointerIds; |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 6121 | |
Siarhei Vishniakou | 8384e0d | 2023-09-18 18:48:27 -0700 | [diff] [blame] | 6122 | std::string canceledWindows; |
| 6123 | for (const TouchedWindow& w : state.windows) { |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 6124 | if (w.windowHandle->getToken() != token) { |
| 6125 | synthesizeCancelationEventsForWindowLocked(w.windowHandle, options); |
Siarhei Vishniakou | 8384e0d | 2023-09-18 18:48:27 -0700 | [diff] [blame] | 6126 | canceledWindows += canceledWindows.empty() ? "[" : ", "; |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 6127 | canceledWindows += w.windowHandle->getName(); |
Siarhei Vishniakou | 8384e0d | 2023-09-18 18:48:27 -0700 | [diff] [blame] | 6128 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6129 | } |
Siarhei Vishniakou | 8384e0d | 2023-09-18 18:48:27 -0700 | [diff] [blame] | 6130 | canceledWindows += canceledWindows.empty() ? "[]" : "]"; |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6131 | LOG(INFO) << "Channel " << requestingConnection->getInputChannelName() |
Siarhei Vishniakou | 8384e0d | 2023-09-18 18:48:27 -0700 | [diff] [blame] | 6132 | << " is stealing input gesture for device " << deviceId << " from " |
| 6133 | << canceledWindows; |
| 6134 | |
| 6135 | // Prevent the gesture from being sent to any other windows. |
| 6136 | // This only blocks relevant pointers to be sent to other windows |
| 6137 | window.addPilferingPointers(deviceId, pointerIds); |
| 6138 | |
| 6139 | state.cancelPointersForWindowsExcept(deviceId, pointerIds, token); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6140 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6141 | return OK; |
| 6142 | } |
| 6143 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6144 | void InputDispatcher::requestPointerCapture(const sp<IBinder>& windowToken, bool enabled) { |
| 6145 | { // acquire lock |
| 6146 | std::scoped_lock _l(mLock); |
| 6147 | if (DEBUG_FOCUS) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 6148 | const sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(windowToken); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6149 | ALOGI("Request to %s Pointer Capture from: %s.", enabled ? "enable" : "disable", |
| 6150 | windowHandle != nullptr ? windowHandle->getName().c_str() |
| 6151 | : "token without window"); |
| 6152 | } |
| 6153 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6154 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6155 | if (focusedToken != windowToken) { |
| 6156 | ALOGW("Ignoring request to %s Pointer Capture: window does not have focus.", |
| 6157 | enabled ? "enable" : "disable"); |
| 6158 | return; |
| 6159 | } |
| 6160 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6161 | if (enabled == mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6162 | ALOGW("Ignoring request to %s Pointer Capture: " |
| 6163 | "window has %s requested pointer capture.", |
| 6164 | enabled ? "enable" : "disable", enabled ? "already" : "not"); |
| 6165 | return; |
| 6166 | } |
| 6167 | |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 6168 | if (enabled) { |
| 6169 | if (std::find(mIneligibleDisplaysForPointerCapture.begin(), |
| 6170 | mIneligibleDisplaysForPointerCapture.end(), |
| 6171 | mFocusedDisplayId) != mIneligibleDisplaysForPointerCapture.end()) { |
| 6172 | ALOGW("Ignoring request to enable Pointer Capture: display is not eligible"); |
| 6173 | return; |
| 6174 | } |
| 6175 | } |
| 6176 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6177 | setPointerCaptureLocked(enabled); |
| 6178 | } // release lock |
| 6179 | |
| 6180 | // Wake the thread to process command entries. |
| 6181 | mLooper->wake(); |
| 6182 | } |
| 6183 | |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 6184 | void InputDispatcher::setDisplayEligibilityForPointerCapture(int32_t displayId, bool isEligible) { |
| 6185 | { // acquire lock |
| 6186 | std::scoped_lock _l(mLock); |
| 6187 | std::erase(mIneligibleDisplaysForPointerCapture, displayId); |
| 6188 | if (!isEligible) { |
| 6189 | mIneligibleDisplaysForPointerCapture.push_back(displayId); |
| 6190 | } |
| 6191 | } // release lock |
| 6192 | } |
| 6193 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6194 | std::optional<gui::Pid> InputDispatcher::findMonitorPidByTokenLocked(const sp<IBinder>& token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6195 | for (const auto& [_, monitors] : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6196 | for (const Monitor& monitor : monitors) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6197 | if (monitor.connection->getToken() == token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6198 | return monitor.pid; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6199 | } |
| 6200 | } |
| 6201 | } |
| 6202 | return std::nullopt; |
| 6203 | } |
| 6204 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6205 | std::shared_ptr<Connection> InputDispatcher::getConnectionLocked( |
| 6206 | const sp<IBinder>& inputConnectionToken) const { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 6207 | if (inputConnectionToken == nullptr) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 6208 | return nullptr; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 6209 | } |
| 6210 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 6211 | for (const auto& [token, connection] : mConnectionsByToken) { |
| 6212 | if (token == inputConnectionToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 6213 | return connection; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6214 | } |
| 6215 | } |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 6216 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 6217 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6218 | } |
| 6219 | |
Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 6220 | std::string InputDispatcher::getConnectionNameLocked(const sp<IBinder>& connectionToken) const { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6221 | std::shared_ptr<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 6222 | if (connection == nullptr) { |
| 6223 | return "<nullptr>"; |
| 6224 | } |
| 6225 | return connection->getInputChannelName(); |
| 6226 | } |
| 6227 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6228 | void InputDispatcher::removeConnectionLocked(const std::shared_ptr<Connection>& connection) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6229 | mAnrTracker.eraseToken(connection->getToken()); |
| 6230 | mConnectionsByToken.erase(connection->getToken()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6231 | } |
| 6232 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6233 | void InputDispatcher::doDispatchCycleFinishedCommand(nsecs_t finishTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6234 | const std::shared_ptr<Connection>& connection, |
| 6235 | uint32_t seq, bool handled, |
| 6236 | nsecs_t consumeTime) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6237 | // Handle post-event policy actions. |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 6238 | std::unique_ptr<const KeyEntry> fallbackKeyEntry; |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6239 | |
| 6240 | { // Start critical section |
| 6241 | auto dispatchEntryIt = |
| 6242 | std::find_if(connection->waitQueue.begin(), connection->waitQueue.end(), |
| 6243 | [seq](auto& e) { return e->seq == seq; }); |
| 6244 | if (dispatchEntryIt == connection->waitQueue.end()) { |
| 6245 | return; |
| 6246 | } |
| 6247 | |
| 6248 | DispatchEntry& dispatchEntry = **dispatchEntryIt; |
| 6249 | |
| 6250 | const nsecs_t eventDuration = finishTime - dispatchEntry.deliveryTime; |
| 6251 | if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) { |
Siarhei Vishniakou | 10bfdc9 | 2024-02-01 11:12:57 -0800 | [diff] [blame] | 6252 | ALOGI("%s spent %" PRId64 "ms processing %s", connection->getInputChannelName().c_str(), |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6253 | ns2ms(eventDuration), dispatchEntry.eventEntry->getDescription().c_str()); |
| 6254 | } |
| 6255 | if (shouldReportFinishedEvent(dispatchEntry, *connection)) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6256 | mLatencyTracker.trackFinishedEvent(dispatchEntry.eventEntry->id, connection->getToken(), |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6257 | dispatchEntry.deliveryTime, consumeTime, finishTime); |
| 6258 | } |
| 6259 | |
| 6260 | if (dispatchEntry.eventEntry->type == EventEntry::Type::KEY) { |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6261 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(*(dispatchEntry.eventEntry)); |
| 6262 | fallbackKeyEntry = |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6263 | afterKeyEventLockedInterruptable(connection, dispatchEntry, keyEntry, handled); |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6264 | } |
| 6265 | } // End critical section: The -LockedInterruptable methods may have released the lock. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6266 | |
| 6267 | // Dequeue the event and start the next cycle. |
| 6268 | // Because the lock might have been released, it is possible that the |
| 6269 | // contents of the wait queue to have been drained, so we need to double-check |
| 6270 | // a few things. |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6271 | auto entryIt = std::find_if(connection->waitQueue.begin(), connection->waitQueue.end(), |
| 6272 | [seq](auto& e) { return e->seq == seq; }); |
| 6273 | if (entryIt != connection->waitQueue.end()) { |
| 6274 | std::unique_ptr<DispatchEntry> dispatchEntry = std::move(*entryIt); |
| 6275 | connection->waitQueue.erase(entryIt); |
| 6276 | |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6277 | const sp<IBinder>& connectionToken = connection->getToken(); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6278 | mAnrTracker.erase(dispatchEntry->timeoutTime, connectionToken); |
| 6279 | if (!connection->responsive) { |
| 6280 | connection->responsive = isConnectionResponsive(*connection); |
| 6281 | if (connection->responsive) { |
| 6282 | // The connection was unresponsive, and now it's responsive. |
| 6283 | processConnectionResponsiveLocked(*connection); |
| 6284 | } |
| 6285 | } |
| 6286 | traceWaitQueueLength(*connection); |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6287 | if (fallbackKeyEntry && connection->status == Connection::Status::NORMAL) { |
Prabir Pradhan | fa2c69f | 2024-02-01 20:31:34 +0000 | [diff] [blame] | 6288 | const auto windowHandle = getWindowHandleLocked(connection->getToken()); |
| 6289 | // Only dispatch fallbacks if there is a window for the connection. |
| 6290 | if (windowHandle != nullptr) { |
| 6291 | const auto inputTarget = |
| 6292 | createInputTargetLocked(windowHandle, InputTarget::DispatchMode::AS_IS, |
| 6293 | dispatchEntry->targetFlags, |
| 6294 | fallbackKeyEntry->downTime); |
| 6295 | if (inputTarget.has_value()) { |
| 6296 | enqueueDispatchEntryLocked(connection, std::move(fallbackKeyEntry), |
| 6297 | *inputTarget); |
| 6298 | } |
| 6299 | } |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6300 | } |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6301 | releaseDispatchEntry(std::move(dispatchEntry)); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6302 | } |
| 6303 | |
| 6304 | // Start the next dispatch cycle for this connection. |
| 6305 | startDispatchCycleLocked(now(), connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6306 | } |
| 6307 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6308 | void InputDispatcher::sendFocusChangedCommandLocked(const sp<IBinder>& oldToken, |
| 6309 | const sp<IBinder>& newToken) { |
| 6310 | auto command = [this, oldToken, newToken]() REQUIRES(mLock) { |
| 6311 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6312 | mPolicy.notifyFocusChanged(oldToken, newToken); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6313 | }; |
| 6314 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6315 | } |
| 6316 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6317 | void InputDispatcher::sendDropWindowCommandLocked(const sp<IBinder>& token, float x, float y) { |
| 6318 | auto command = [this, token, x, y]() REQUIRES(mLock) { |
| 6319 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6320 | mPolicy.notifyDropWindow(token, x, y); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6321 | }; |
| 6322 | postCommandLocked(std::move(command)); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 6323 | } |
| 6324 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6325 | void InputDispatcher::onAnrLocked(const std::shared_ptr<Connection>& connection) { |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6326 | if (connection == nullptr) { |
| 6327 | LOG_ALWAYS_FATAL("Caller must check for nullness"); |
| 6328 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6329 | // Since we are allowing the policy to extend the timeout, maybe the waitQueue |
| 6330 | // is already healthy again. Don't raise ANR in this situation |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6331 | if (connection->waitQueue.empty()) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6332 | ALOGI("Not raising ANR because the connection %s has recovered", |
Siarhei Vishniakou | 10bfdc9 | 2024-02-01 11:12:57 -0800 | [diff] [blame] | 6333 | connection->getInputChannelName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6334 | return; |
| 6335 | } |
| 6336 | /** |
| 6337 | * The "oldestEntry" is the entry that was first sent to the application. That entry, however, |
| 6338 | * may not be the one that caused the timeout to occur. One possibility is that window timeout |
| 6339 | * has changed. This could cause newer entries to time out before the already dispatched |
| 6340 | * entries. In that situation, the newest entries caused ANR. But in all likelihood, the app |
| 6341 | * processes the events linearly. So providing information about the oldest entry seems to be |
| 6342 | * most useful. |
| 6343 | */ |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6344 | DispatchEntry& oldestEntry = *connection->waitQueue.front(); |
| 6345 | const nsecs_t currentWait = now() - oldestEntry.deliveryTime; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6346 | std::string reason = |
| 6347 | android::base::StringPrintf("%s is not responding. Waited %" PRId64 "ms for %s", |
Siarhei Vishniakou | 10bfdc9 | 2024-02-01 11:12:57 -0800 | [diff] [blame] | 6348 | connection->getInputChannelName().c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6349 | ns2ms(currentWait), |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6350 | oldestEntry.eventEntry->getDescription().c_str()); |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6351 | sp<IBinder> connectionToken = connection->getToken(); |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 6352 | updateLastAnrStateLocked(getWindowHandleLocked(connectionToken), reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6353 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6354 | processConnectionUnresponsiveLocked(*connection, std::move(reason)); |
| 6355 | |
| 6356 | // Stop waking up for events on this connection, it is already unresponsive |
| 6357 | cancelEventsForAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6358 | } |
| 6359 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6360 | void InputDispatcher::onAnrLocked(std::shared_ptr<InputApplicationHandle> application) { |
| 6361 | std::string reason = |
| 6362 | StringPrintf("%s does not have a focused window", application->getName().c_str()); |
| 6363 | updateLastAnrStateLocked(*application, reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6364 | |
Yabin Cui | 8eb9c55 | 2023-06-08 18:05:07 +0000 | [diff] [blame] | 6365 | auto command = [this, app = std::move(application)]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6366 | scoped_unlock unlock(mLock); |
Yabin Cui | ced952f | 2023-06-09 21:12:51 +0000 | [diff] [blame] | 6367 | mPolicy.notifyNoFocusedWindowAnr(app); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6368 | }; |
| 6369 | postCommandLocked(std::move(command)); |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 6370 | } |
| 6371 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 6372 | void InputDispatcher::updateLastAnrStateLocked(const sp<WindowInfoHandle>& window, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6373 | const std::string& reason) { |
| 6374 | const std::string windowLabel = getApplicationWindowLabel(nullptr, window); |
| 6375 | updateLastAnrStateLocked(windowLabel, reason); |
| 6376 | } |
| 6377 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6378 | void InputDispatcher::updateLastAnrStateLocked(const InputApplicationHandle& application, |
| 6379 | const std::string& reason) { |
| 6380 | const std::string windowLabel = getApplicationWindowLabel(&application, nullptr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6381 | updateLastAnrStateLocked(windowLabel, reason); |
| 6382 | } |
| 6383 | |
| 6384 | void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel, |
| 6385 | const std::string& reason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6386 | // 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] | 6387 | time_t t = time(nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6388 | struct tm tm; |
| 6389 | localtime_r(&t, &tm); |
| 6390 | char timestr[64]; |
| 6391 | strftime(timestr, sizeof(timestr), "%F %T", &tm); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6392 | mLastAnrState.clear(); |
| 6393 | mLastAnrState += INDENT "ANR:\n"; |
| 6394 | mLastAnrState += StringPrintf(INDENT2 "Time: %s\n", timestr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6395 | mLastAnrState += StringPrintf(INDENT2 "Reason: %s\n", reason.c_str()); |
| 6396 | mLastAnrState += StringPrintf(INDENT2 "Window: %s\n", windowLabel.c_str()); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6397 | dumpDispatchStateLocked(mLastAnrState); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6398 | } |
| 6399 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6400 | void InputDispatcher::doInterceptKeyBeforeDispatchingCommand(const sp<IBinder>& focusedWindowToken, |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 6401 | const KeyEntry& entry) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6402 | const KeyEvent event = createKeyEvent(entry); |
| 6403 | nsecs_t delay = 0; |
| 6404 | { // release lock |
| 6405 | scoped_unlock unlock(mLock); |
| 6406 | android::base::Timer t; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6407 | delay = mPolicy.interceptKeyBeforeDispatching(focusedWindowToken, event, entry.policyFlags); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6408 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 6409 | ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms", |
| 6410 | std::to_string(t.duration().count()).c_str()); |
| 6411 | } |
| 6412 | } // acquire lock |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6413 | |
| 6414 | if (delay < 0) { |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 6415 | entry.interceptKeyResult = KeyEntry::InterceptKeyResult::SKIP; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6416 | } else if (delay == 0) { |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 6417 | entry.interceptKeyResult = KeyEntry::InterceptKeyResult::CONTINUE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6418 | } else { |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 6419 | entry.interceptKeyResult = KeyEntry::InterceptKeyResult::TRY_AGAIN_LATER; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6420 | entry.interceptKeyWakeupTime = now() + delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6421 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6422 | } |
| 6423 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6424 | void InputDispatcher::sendWindowUnresponsiveCommandLocked(const sp<IBinder>& token, |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6425 | std::optional<gui::Pid> pid, |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6426 | std::string reason) { |
Yabin Cui | 8eb9c55 | 2023-06-08 18:05:07 +0000 | [diff] [blame] | 6427 | auto command = [this, token, pid, r = std::move(reason)]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6428 | scoped_unlock unlock(mLock); |
Yabin Cui | ced952f | 2023-06-09 21:12:51 +0000 | [diff] [blame] | 6429 | mPolicy.notifyWindowUnresponsive(token, pid, r); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6430 | }; |
| 6431 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6432 | } |
| 6433 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6434 | void InputDispatcher::sendWindowResponsiveCommandLocked(const sp<IBinder>& token, |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6435 | std::optional<gui::Pid> pid) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6436 | auto command = [this, token, pid]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6437 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6438 | mPolicy.notifyWindowResponsive(token, pid); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6439 | }; |
| 6440 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6441 | } |
| 6442 | |
| 6443 | /** |
| 6444 | * Tell the policy that a connection has become unresponsive so that it can start ANR. |
| 6445 | * Check whether the connection of interest is a monitor or a window, and add the corresponding |
| 6446 | * command entry to the command queue. |
| 6447 | */ |
| 6448 | void InputDispatcher::processConnectionUnresponsiveLocked(const Connection& connection, |
| 6449 | std::string reason) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6450 | const sp<IBinder>& connectionToken = connection.getToken(); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6451 | std::optional<gui::Pid> pid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6452 | if (connection.monitor) { |
Siarhei Vishniakou | 10bfdc9 | 2024-02-01 11:12:57 -0800 | [diff] [blame] | 6453 | ALOGW("Monitor %s is unresponsive: %s", connection.getInputChannelName().c_str(), |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6454 | reason.c_str()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6455 | pid = findMonitorPidByTokenLocked(connectionToken); |
| 6456 | } else { |
| 6457 | // The connection is a window |
Siarhei Vishniakou | 10bfdc9 | 2024-02-01 11:12:57 -0800 | [diff] [blame] | 6458 | ALOGW("Window %s is unresponsive: %s", connection.getInputChannelName().c_str(), |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6459 | reason.c_str()); |
| 6460 | const sp<WindowInfoHandle> handle = getWindowHandleLocked(connectionToken); |
| 6461 | if (handle != nullptr) { |
| 6462 | pid = handle->getInfo()->ownerPid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6463 | } |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6464 | } |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6465 | sendWindowUnresponsiveCommandLocked(connectionToken, pid, std::move(reason)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6466 | } |
| 6467 | |
| 6468 | /** |
| 6469 | * Tell the policy that a connection has become responsive so that it can stop ANR. |
| 6470 | */ |
| 6471 | void InputDispatcher::processConnectionResponsiveLocked(const Connection& connection) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6472 | const sp<IBinder>& connectionToken = connection.getToken(); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6473 | std::optional<gui::Pid> pid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6474 | if (connection.monitor) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6475 | pid = findMonitorPidByTokenLocked(connectionToken); |
| 6476 | } else { |
| 6477 | // The connection is a window |
| 6478 | const sp<WindowInfoHandle> handle = getWindowHandleLocked(connectionToken); |
| 6479 | if (handle != nullptr) { |
| 6480 | pid = handle->getInfo()->ownerPid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6481 | } |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6482 | } |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6483 | sendWindowResponsiveCommandLocked(connectionToken, pid); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6484 | } |
| 6485 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 6486 | std::unique_ptr<const KeyEntry> InputDispatcher::afterKeyEventLockedInterruptable( |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6487 | const std::shared_ptr<Connection>& connection, DispatchEntry& dispatchEntry, |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6488 | const KeyEntry& keyEntry, bool handled) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6489 | if (keyEntry.flags & AKEY_EVENT_FLAG_FALLBACK) { |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6490 | if (!handled) { |
| 6491 | // Report the key as unhandled, since the fallback was not handled. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6492 | mReporter->reportUnhandledKey(keyEntry.id); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6493 | } |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6494 | return {}; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6495 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6496 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6497 | // Get the fallback key state. |
| 6498 | // Clear it out after dispatching the UP. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6499 | int32_t originalKeyCode = keyEntry.keyCode; |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6500 | std::optional<int32_t> fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6501 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6502 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6503 | } |
| 6504 | |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6505 | if (handled || !dispatchEntry.hasForegroundTarget()) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6506 | // If the application handles the original key for which we previously |
| 6507 | // generated a fallback or if the window is not a foreground window, |
| 6508 | // then cancel the associated fallback key, if any. |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6509 | if (fallbackKeyCode) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6510 | // Dispatch the unhandled key to the policy with the cancel flag. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6511 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6512 | ALOGD("Unhandled key event: Asking policy to cancel fallback action. " |
| 6513 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6514 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, |
| 6515 | keyEntry.policyFlags); |
| 6516 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6517 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6518 | event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6519 | |
| 6520 | mLock.unlock(); |
| 6521 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6522 | if (const auto unhandledKeyFallback = |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6523 | mPolicy.dispatchUnhandledKey(connection->getToken(), event, |
| 6524 | keyEntry.policyFlags); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6525 | unhandledKeyFallback) { |
| 6526 | event = *unhandledKeyFallback; |
| 6527 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6528 | |
| 6529 | mLock.lock(); |
| 6530 | |
Prabir Pradhan | fa2c69f | 2024-02-01 20:31:34 +0000 | [diff] [blame] | 6531 | // Cancel the fallback key, but only if we still have a window for the channel. |
| 6532 | // It could have been removed during the policy call. |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6533 | if (*fallbackKeyCode != AKEYCODE_UNKNOWN) { |
Prabir Pradhan | fa2c69f | 2024-02-01 20:31:34 +0000 | [diff] [blame] | 6534 | const auto windowHandle = getWindowHandleLocked(connection->getToken()); |
| 6535 | if (windowHandle != nullptr) { |
| 6536 | CancelationOptions options(CancelationOptions::Mode::CANCEL_FALLBACK_EVENTS, |
| 6537 | "application handled the original non-fallback key " |
| 6538 | "or is no longer a foreground target, " |
| 6539 | "canceling previously dispatched fallback key"); |
| 6540 | options.keyCode = *fallbackKeyCode; |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 6541 | synthesizeCancelationEventsForWindowLocked(windowHandle, options, connection); |
Prabir Pradhan | fa2c69f | 2024-02-01 20:31:34 +0000 | [diff] [blame] | 6542 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6543 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6544 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6545 | } |
| 6546 | } else { |
| 6547 | // If the application did not handle a non-fallback key, first check |
| 6548 | // that we are in a good state to perform unhandled key event processing |
| 6549 | // Then ask the policy what to do with it. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6550 | bool initialDown = keyEntry.action == AKEY_EVENT_ACTION_DOWN && keyEntry.repeatCount == 0; |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6551 | if (!fallbackKeyCode && !initialDown) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6552 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6553 | ALOGD("Unhandled key event: Skipping unhandled key event processing " |
| 6554 | "since this is not an initial down. " |
| 6555 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6556 | originalKeyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 6557 | } |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6558 | return {}; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6559 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6560 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6561 | // Dispatch the unhandled key to the policy. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6562 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6563 | ALOGD("Unhandled key event: Asking policy to perform fallback action. " |
| 6564 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6565 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 6566 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6567 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6568 | |
| 6569 | mLock.unlock(); |
| 6570 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6571 | bool fallback = false; |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6572 | if (auto fb = mPolicy.dispatchUnhandledKey(connection->getToken(), event, |
| 6573 | keyEntry.policyFlags); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6574 | fb) { |
| 6575 | fallback = true; |
| 6576 | event = *fb; |
| 6577 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6578 | |
| 6579 | mLock.lock(); |
| 6580 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 6581 | if (connection->status != Connection::Status::NORMAL) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6582 | connection->inputState.removeFallbackKey(originalKeyCode); |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6583 | return {}; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6584 | } |
| 6585 | |
| 6586 | // Latch the fallback keycode for this key on an initial down. |
| 6587 | // The fallback keycode cannot change at any other point in the lifecycle. |
| 6588 | if (initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6589 | if (fallback) { |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6590 | *fallbackKeyCode = event.getKeyCode(); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6591 | } else { |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6592 | *fallbackKeyCode = AKEYCODE_UNKNOWN; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6593 | } |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6594 | connection->inputState.setFallbackKey(originalKeyCode, *fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6595 | } |
| 6596 | |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6597 | ALOG_ASSERT(fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6598 | |
| 6599 | // Cancel the fallback key if the policy decides not to send it anymore. |
| 6600 | // We will continue to dispatch the key to the policy but we will no |
| 6601 | // longer dispatch a fallback key to the application. |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6602 | if (*fallbackKeyCode != AKEYCODE_UNKNOWN && |
| 6603 | (!fallback || *fallbackKeyCode != event.getKeyCode())) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6604 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6605 | if (fallback) { |
| 6606 | ALOGD("Unhandled key event: Policy requested to send key %d" |
| 6607 | "as a fallback for %d, but on the DOWN it had requested " |
| 6608 | "to send %d instead. Fallback canceled.", |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6609 | event.getKeyCode(), originalKeyCode, *fallbackKeyCode); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6610 | } else { |
| 6611 | ALOGD("Unhandled key event: Policy did not request fallback for %d, " |
| 6612 | "but on the DOWN it had requested to send %d. " |
| 6613 | "Fallback canceled.", |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6614 | originalKeyCode, *fallbackKeyCode); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6615 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6616 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6617 | |
Prabir Pradhan | fa2c69f | 2024-02-01 20:31:34 +0000 | [diff] [blame] | 6618 | const auto windowHandle = getWindowHandleLocked(connection->getToken()); |
| 6619 | if (windowHandle != nullptr) { |
| 6620 | CancelationOptions options(CancelationOptions::Mode::CANCEL_FALLBACK_EVENTS, |
| 6621 | "canceling fallback, policy no longer desires it"); |
| 6622 | options.keyCode = *fallbackKeyCode; |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 6623 | synthesizeCancelationEventsForWindowLocked(windowHandle, options, connection); |
Prabir Pradhan | fa2c69f | 2024-02-01 20:31:34 +0000 | [diff] [blame] | 6624 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6625 | |
| 6626 | fallback = false; |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6627 | *fallbackKeyCode = AKEYCODE_UNKNOWN; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6628 | if (keyEntry.action != AKEY_EVENT_ACTION_UP) { |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6629 | connection->inputState.setFallbackKey(originalKeyCode, *fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6630 | } |
| 6631 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6632 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6633 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6634 | { |
| 6635 | std::string msg; |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6636 | const std::map<int32_t, int32_t>& fallbackKeys = |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6637 | connection->inputState.getFallbackKeys(); |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6638 | for (const auto& [key, value] : fallbackKeys) { |
| 6639 | msg += StringPrintf(", %d->%d", key, value); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6640 | } |
| 6641 | ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.", |
| 6642 | fallbackKeys.size(), msg.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6643 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6644 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6645 | |
| 6646 | if (fallback) { |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6647 | // Return the fallback key that we want dispatched to the channel. |
| 6648 | std::unique_ptr<KeyEntry> newEntry = |
| 6649 | std::make_unique<KeyEntry>(mIdGenerator.nextId(), keyEntry.injectionState, |
| 6650 | event.getEventTime(), event.getDeviceId(), |
| 6651 | event.getSource(), event.getDisplayId(), |
| 6652 | keyEntry.policyFlags, keyEntry.action, |
| 6653 | event.getFlags() | AKEY_EVENT_FLAG_FALLBACK, |
| 6654 | *fallbackKeyCode, event.getScanCode(), |
| 6655 | event.getMetaState(), event.getRepeatCount(), |
| 6656 | event.getDownTime()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6657 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6658 | ALOGD("Unhandled key event: Dispatching fallback key. " |
| 6659 | "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x", |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6660 | originalKeyCode, *fallbackKeyCode, keyEntry.metaState); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6661 | } |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6662 | return newEntry; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6663 | } else { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6664 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6665 | ALOGD("Unhandled key event: No fallback key."); |
| 6666 | } |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6667 | |
| 6668 | // Report the key as unhandled, since there is no fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6669 | mReporter->reportUnhandledKey(keyEntry.id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6670 | } |
| 6671 | } |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6672 | return {}; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6673 | } |
| 6674 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6675 | void InputDispatcher::traceInboundQueueLengthLocked() { |
| 6676 | if (ATRACE_ENABLED()) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 6677 | ATRACE_INT("iq", mInboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6678 | } |
| 6679 | } |
| 6680 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6681 | void InputDispatcher::traceOutboundQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6682 | if (ATRACE_ENABLED()) { |
| 6683 | char counterName[40]; |
Siarhei Vishniakou | 10bfdc9 | 2024-02-01 11:12:57 -0800 | [diff] [blame] | 6684 | snprintf(counterName, sizeof(counterName), "oq:%s", |
| 6685 | connection.getInputChannelName().c_str()); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6686 | ATRACE_INT(counterName, connection.outboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6687 | } |
| 6688 | } |
| 6689 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6690 | void InputDispatcher::traceWaitQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6691 | if (ATRACE_ENABLED()) { |
| 6692 | char counterName[40]; |
Siarhei Vishniakou | 10bfdc9 | 2024-02-01 11:12:57 -0800 | [diff] [blame] | 6693 | snprintf(counterName, sizeof(counterName), "wq:%s", |
| 6694 | connection.getInputChannelName().c_str()); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6695 | ATRACE_INT(counterName, connection.waitQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6696 | } |
| 6697 | } |
| 6698 | |
Siarhei Vishniakou | 5e20f27 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 6699 | void InputDispatcher::dump(std::string& dump) const { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6700 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6701 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6702 | dump += "Input Dispatcher State:\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6703 | dumpDispatchStateLocked(dump); |
| 6704 | |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6705 | if (!mLastAnrState.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6706 | dump += "\nInput Dispatcher State at time of last ANR:\n"; |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6707 | dump += mLastAnrState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6708 | } |
| 6709 | } |
| 6710 | |
| 6711 | void InputDispatcher::monitor() { |
| 6712 | // 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] | 6713 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6714 | mLooper->wake(); |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6715 | mDispatcherIsAlive.wait(_l); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6716 | } |
| 6717 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6718 | /** |
| 6719 | * Wake up the dispatcher and wait until it processes all events and commands. |
| 6720 | * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so |
| 6721 | * this method can be safely called from any thread, as long as you've ensured that |
| 6722 | * the work you are interested in completing has already been queued. |
| 6723 | */ |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 6724 | bool InputDispatcher::waitForIdle() const { |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6725 | /** |
| 6726 | * Timeout should represent the longest possible time that a device might spend processing |
| 6727 | * events and commands. |
| 6728 | */ |
| 6729 | constexpr std::chrono::duration TIMEOUT = 100ms; |
| 6730 | std::unique_lock lock(mLock); |
| 6731 | mLooper->wake(); |
| 6732 | std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT); |
| 6733 | return result == std::cv_status::no_timeout; |
| 6734 | } |
| 6735 | |
Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 6736 | /** |
| 6737 | * Sets focus to the window identified by the token. This must be called |
| 6738 | * after updating any input window handles. |
| 6739 | * |
| 6740 | * Params: |
| 6741 | * request.token - input channel token used to identify the window that should gain focus. |
| 6742 | * request.focusedToken - the token that the caller expects currently to be focused. If the |
| 6743 | * specified token does not match the currently focused window, this request will be dropped. |
| 6744 | * If the specified focused token matches the currently focused window, the call will succeed. |
| 6745 | * Set this to "null" if this call should succeed no matter what the currently focused token is. |
| 6746 | * request.timestamp - SYSTEM_TIME_MONOTONIC timestamp in nanos set by the client (wm) |
| 6747 | * when requesting the focus change. This determines which request gets |
| 6748 | * precedence if there is a focus change request from another source such as pointer down. |
| 6749 | */ |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6750 | void InputDispatcher::setFocusedWindow(const FocusRequest& request) { |
| 6751 | { // acquire lock |
| 6752 | std::scoped_lock _l(mLock); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6753 | std::optional<FocusResolver::FocusChanges> changes = |
| 6754 | mFocusResolver.setFocusedWindow(request, getWindowHandlesLocked(request.displayId)); |
| 6755 | if (changes) { |
| 6756 | onFocusChangedLocked(*changes); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6757 | } |
| 6758 | } // release lock |
| 6759 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6760 | mLooper->wake(); |
| 6761 | } |
| 6762 | |
Prabir Pradhan | 4b9b1a1 | 2024-02-02 00:16:17 +0000 | [diff] [blame] | 6763 | void InputDispatcher::onFocusChangedLocked(const FocusResolver::FocusChanges& changes, |
| 6764 | const sp<WindowInfoHandle> removedFocusedWindowHandle) { |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6765 | if (changes.oldFocus) { |
Prabir Pradhan | 4b9b1a1 | 2024-02-02 00:16:17 +0000 | [diff] [blame] | 6766 | const auto resolvedWindow = removedFocusedWindowHandle != nullptr |
| 6767 | ? removedFocusedWindowHandle |
| 6768 | : getWindowHandleLocked(changes.oldFocus, changes.displayId); |
| 6769 | if (resolvedWindow == nullptr) { |
| 6770 | LOG(FATAL) << __func__ << ": Previously focused token did not have a window"; |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6771 | } |
Prabir Pradhan | 4b9b1a1 | 2024-02-02 00:16:17 +0000 | [diff] [blame] | 6772 | CancelationOptions options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, |
| 6773 | "focus left window"); |
| 6774 | synthesizeCancelationEventsForWindowLocked(resolvedWindow, options); |
| 6775 | enqueueFocusEventLocked(changes.oldFocus, /*hasFocus=*/false, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6776 | } |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6777 | if (changes.newFocus) { |
Siarhei Vishniakou | c033dfb | 2023-10-03 10:45:16 -0700 | [diff] [blame] | 6778 | resetNoFocusedWindowTimeoutLocked(); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6779 | enqueueFocusEventLocked(changes.newFocus, /*hasFocus=*/true, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6780 | } |
| 6781 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6782 | // If a window has pointer capture, then it must have focus. We need to ensure that this |
| 6783 | // contract is upheld when pointer capture is being disabled due to a loss of window focus. |
| 6784 | // If the window loses focus before it loses pointer capture, then the window can be in a state |
| 6785 | // where it has pointer capture but not focus, violating the contract. Therefore we must |
| 6786 | // dispatch the pointer capture event before the focus event. Since focus events are added to |
| 6787 | // the front of the queue (above), we add the pointer capture event to the front of the queue |
| 6788 | // after the focus events are added. This ensures the pointer capture event ends up at the |
| 6789 | // front. |
| 6790 | disablePointerCaptureForcedLocked(); |
| 6791 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6792 | if (mFocusedDisplayId == changes.displayId) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6793 | sendFocusChangedCommandLocked(changes.oldFocus, changes.newFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6794 | } |
| 6795 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6796 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6797 | void InputDispatcher::disablePointerCaptureForcedLocked() { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6798 | if (!mCurrentPointerCaptureRequest.enable && !mWindowTokenWithPointerCapture) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6799 | return; |
| 6800 | } |
| 6801 | |
| 6802 | ALOGD_IF(DEBUG_FOCUS, "Disabling Pointer Capture because the window lost focus."); |
| 6803 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6804 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6805 | setPointerCaptureLocked(false); |
| 6806 | } |
| 6807 | |
| 6808 | if (!mWindowTokenWithPointerCapture) { |
| 6809 | // No need to send capture changes because no window has capture. |
| 6810 | return; |
| 6811 | } |
| 6812 | |
| 6813 | if (mPendingEvent != nullptr) { |
| 6814 | // Move the pending event to the front of the queue. This will give the chance |
| 6815 | // for the pending event to be dropped if it is a captured event. |
| 6816 | mInboundQueue.push_front(mPendingEvent); |
| 6817 | mPendingEvent = nullptr; |
| 6818 | } |
| 6819 | |
| 6820 | auto entry = std::make_unique<PointerCaptureChangedEntry>(mIdGenerator.nextId(), now(), |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6821 | mCurrentPointerCaptureRequest); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6822 | mInboundQueue.push_front(std::move(entry)); |
| 6823 | } |
| 6824 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6825 | void InputDispatcher::setPointerCaptureLocked(bool enable) { |
| 6826 | mCurrentPointerCaptureRequest.enable = enable; |
| 6827 | mCurrentPointerCaptureRequest.seq++; |
| 6828 | auto command = [this, request = mCurrentPointerCaptureRequest]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6829 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6830 | mPolicy.setPointerCapture(request); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6831 | }; |
| 6832 | postCommandLocked(std::move(command)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6833 | } |
| 6834 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6835 | void InputDispatcher::displayRemoved(int32_t displayId) { |
| 6836 | { // acquire lock |
| 6837 | std::scoped_lock _l(mLock); |
| 6838 | // Set an empty list to remove all handles from the specific display. |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 6839 | setInputWindowsLocked(/*windowInfoHandles=*/{}, displayId); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6840 | setFocusedApplicationLocked(displayId, nullptr); |
| 6841 | // Call focus resolver to clean up stale requests. This must be called after input windows |
| 6842 | // have been removed for the removed display. |
| 6843 | mFocusResolver.displayRemoved(displayId); |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 6844 | // Reset pointer capture eligibility, regardless of previous state. |
| 6845 | std::erase(mIneligibleDisplaysForPointerCapture, displayId); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 6846 | // Remove the associated touch mode state. |
| 6847 | mTouchModePerDisplay.erase(displayId); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 6848 | mVerifiersByDisplay.erase(displayId); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6849 | } // release lock |
| 6850 | |
| 6851 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6852 | mLooper->wake(); |
| 6853 | } |
| 6854 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 6855 | void InputDispatcher::onWindowInfosChanged(const gui::WindowInfosUpdate& update) { |
Siarhei Vishniakou | aeed0da | 2024-01-09 08:57:13 -0800 | [diff] [blame] | 6856 | if (auto result = validateWindowInfosUpdate(update); !result.ok()) { |
| 6857 | { |
| 6858 | // acquire lock |
| 6859 | std::scoped_lock _l(mLock); |
| 6860 | logDispatchStateLocked(); |
| 6861 | } |
| 6862 | LOG_ALWAYS_FATAL("Incorrect WindowInfosUpdate provided: %s", |
| 6863 | result.error().message().c_str()); |
| 6864 | }; |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6865 | // The listener sends the windows as a flattened array. Separate the windows by display for |
| 6866 | // more convenient parsing. |
| 6867 | std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>> handlesPerDisplay; |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 6868 | for (const auto& info : update.windowInfos) { |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6869 | handlesPerDisplay.emplace(info.displayId, std::vector<sp<WindowInfoHandle>>()); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6870 | handlesPerDisplay[info.displayId].push_back(sp<WindowInfoHandle>::make(info)); |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6871 | } |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6872 | |
| 6873 | { // acquire lock |
| 6874 | std::scoped_lock _l(mLock); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 6875 | |
| 6876 | // Ensure that we have an entry created for all existing displays so that if a displayId has |
| 6877 | // no windows, we can tell that the windows were removed from the display. |
| 6878 | for (const auto& [displayId, _] : mWindowHandlesByDisplay) { |
| 6879 | handlesPerDisplay[displayId]; |
| 6880 | } |
| 6881 | |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6882 | mDisplayInfos.clear(); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 6883 | for (const auto& displayInfo : update.displayInfos) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6884 | mDisplayInfos.emplace(displayInfo.displayId, displayInfo); |
| 6885 | } |
| 6886 | |
| 6887 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 6888 | setInputWindowsLocked(handles, displayId); |
| 6889 | } |
Patrick Williams | 9464b2c | 2023-05-23 11:22:04 -0500 | [diff] [blame] | 6890 | |
| 6891 | if (update.vsyncId < mWindowInfosVsyncId) { |
| 6892 | ALOGE("Received out of order window infos update. Last update vsync id: %" PRId64 |
| 6893 | ", current update vsync id: %" PRId64, |
| 6894 | mWindowInfosVsyncId, update.vsyncId); |
| 6895 | } |
| 6896 | mWindowInfosVsyncId = update.vsyncId; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6897 | } |
| 6898 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6899 | mLooper->wake(); |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6900 | } |
| 6901 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6902 | bool InputDispatcher::shouldDropInput( |
| 6903 | const EventEntry& entry, const sp<android::gui::WindowInfoHandle>& windowHandle) const { |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6904 | if (windowHandle->getInfo()->inputConfig.test(WindowInfo::InputConfig::DROP_INPUT) || |
| 6905 | (windowHandle->getInfo()->inputConfig.test( |
| 6906 | WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED) && |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6907 | isWindowObscuredLocked(windowHandle))) { |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6908 | ALOGW("Dropping %s event targeting %s as requested by the input configuration {%s} on " |
| 6909 | "display %" PRId32 ".", |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6910 | ftl::enum_string(entry.type).c_str(), windowHandle->getName().c_str(), |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6911 | windowHandle->getInfo()->inputConfig.string().c_str(), |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6912 | windowHandle->getInfo()->displayId); |
| 6913 | return true; |
| 6914 | } |
| 6915 | return false; |
| 6916 | } |
| 6917 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 6918 | void InputDispatcher::DispatcherWindowListener::onWindowInfosChanged( |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 6919 | const gui::WindowInfosUpdate& update) { |
| 6920 | mDispatcher.onWindowInfosChanged(update); |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 6921 | } |
| 6922 | |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6923 | void InputDispatcher::cancelCurrentTouch() { |
| 6924 | { |
| 6925 | std::scoped_lock _l(mLock); |
| 6926 | ALOGD("Canceling all ongoing pointer gestures on all displays."); |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 6927 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6928 | "cancel current touch"); |
| 6929 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 6930 | |
| 6931 | mTouchStatesByDisplay.clear(); |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6932 | } |
| 6933 | // Wake up poll loop since there might be work to do. |
| 6934 | mLooper->wake(); |
| 6935 | } |
| 6936 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6937 | void InputDispatcher::setMonitorDispatchingTimeoutForTest(std::chrono::nanoseconds timeout) { |
| 6938 | std::scoped_lock _l(mLock); |
| 6939 | mMonitorDispatchingTimeout = timeout; |
| 6940 | } |
| 6941 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6942 | void InputDispatcher::slipWallpaperTouch(ftl::Flags<InputTarget::Flags> targetFlags, |
| 6943 | const sp<WindowInfoHandle>& oldWindowHandle, |
| 6944 | const sp<WindowInfoHandle>& newWindowHandle, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 6945 | TouchState& state, int32_t deviceId, |
| 6946 | const PointerProperties& pointerProperties, |
Siarhei Vishniakou | bf88052 | 2023-05-01 11:03:22 -0700 | [diff] [blame] | 6947 | std::vector<InputTarget>& targets) const { |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 6948 | std::vector<PointerProperties> pointers{pointerProperties}; |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6949 | const bool oldHasWallpaper = oldWindowHandle->getInfo()->inputConfig.test( |
| 6950 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER); |
| 6951 | const bool newHasWallpaper = targetFlags.test(InputTarget::Flags::FOREGROUND) && |
| 6952 | newWindowHandle->getInfo()->inputConfig.test( |
| 6953 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER); |
| 6954 | const sp<WindowInfoHandle> oldWallpaper = |
| 6955 | oldHasWallpaper ? state.getWallpaperWindow() : nullptr; |
| 6956 | const sp<WindowInfoHandle> newWallpaper = |
| 6957 | newHasWallpaper ? findWallpaperWindowBelow(newWindowHandle) : nullptr; |
| 6958 | if (oldWallpaper == newWallpaper) { |
| 6959 | return; |
| 6960 | } |
| 6961 | |
| 6962 | if (oldWallpaper != nullptr) { |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 6963 | const TouchedWindow& oldTouchedWindow = state.getTouchedWindow(oldWallpaper); |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 6964 | addPointerWindowTargetLocked(oldWallpaper, InputTarget::DispatchMode::SLIPPERY_EXIT, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 6965 | oldTouchedWindow.targetFlags, getPointerIds(pointers), |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 6966 | oldTouchedWindow.getDownTimeInTarget(deviceId), targets); |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 6967 | state.removeTouchingPointerFromWindow(deviceId, pointerProperties.id, oldWallpaper); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6968 | } |
| 6969 | |
| 6970 | if (newWallpaper != nullptr) { |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 6971 | state.addOrUpdateWindow(newWallpaper, InputTarget::DispatchMode::SLIPPERY_ENTER, |
| 6972 | InputTarget::Flags::WINDOW_IS_OBSCURED | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6973 | InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 6974 | deviceId, pointers); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6975 | } |
| 6976 | } |
| 6977 | |
| 6978 | void InputDispatcher::transferWallpaperTouch(ftl::Flags<InputTarget::Flags> oldTargetFlags, |
| 6979 | ftl::Flags<InputTarget::Flags> newTargetFlags, |
| 6980 | const sp<WindowInfoHandle> fromWindowHandle, |
| 6981 | const sp<WindowInfoHandle> toWindowHandle, |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 6982 | TouchState& state, int32_t deviceId, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 6983 | const std::vector<PointerProperties>& pointers) { |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6984 | const bool oldHasWallpaper = oldTargetFlags.test(InputTarget::Flags::FOREGROUND) && |
| 6985 | fromWindowHandle->getInfo()->inputConfig.test( |
| 6986 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER); |
| 6987 | const bool newHasWallpaper = newTargetFlags.test(InputTarget::Flags::FOREGROUND) && |
| 6988 | toWindowHandle->getInfo()->inputConfig.test( |
| 6989 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER); |
| 6990 | |
| 6991 | const sp<WindowInfoHandle> oldWallpaper = |
| 6992 | oldHasWallpaper ? state.getWallpaperWindow() : nullptr; |
| 6993 | const sp<WindowInfoHandle> newWallpaper = |
| 6994 | newHasWallpaper ? findWallpaperWindowBelow(toWindowHandle) : nullptr; |
| 6995 | if (oldWallpaper == newWallpaper) { |
| 6996 | return; |
| 6997 | } |
| 6998 | |
| 6999 | if (oldWallpaper != nullptr) { |
| 7000 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
| 7001 | "transferring touch focus to another window"); |
| 7002 | state.removeWindowByToken(oldWallpaper->getToken()); |
| 7003 | synthesizeCancelationEventsForWindowLocked(oldWallpaper, options); |
| 7004 | } |
| 7005 | |
| 7006 | if (newWallpaper != nullptr) { |
| 7007 | nsecs_t downTimeInTarget = now(); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 7008 | ftl::Flags<InputTarget::Flags> wallpaperFlags = newTargetFlags; |
| 7009 | wallpaperFlags |= oldTargetFlags & InputTarget::Flags::SPLIT; |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 7010 | wallpaperFlags |= InputTarget::Flags::WINDOW_IS_OBSCURED | |
| 7011 | InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 7012 | state.addOrUpdateWindow(newWallpaper, InputTarget::DispatchMode::AS_IS, wallpaperFlags, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 7013 | deviceId, pointers, downTimeInTarget); |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 7014 | std::shared_ptr<Connection> wallpaperConnection = |
| 7015 | getConnectionLocked(newWallpaper->getToken()); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 7016 | if (wallpaperConnection != nullptr) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 7017 | std::shared_ptr<Connection> toConnection = |
| 7018 | getConnectionLocked(toWindowHandle->getToken()); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 7019 | toConnection->inputState.mergePointerStateTo(wallpaperConnection->inputState); |
| 7020 | synthesizePointerDownEventsForConnectionLocked(downTimeInTarget, wallpaperConnection, |
| 7021 | wallpaperFlags); |
| 7022 | } |
| 7023 | } |
| 7024 | } |
| 7025 | |
| 7026 | sp<WindowInfoHandle> InputDispatcher::findWallpaperWindowBelow( |
| 7027 | const sp<WindowInfoHandle>& windowHandle) const { |
| 7028 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
| 7029 | getWindowHandlesLocked(windowHandle->getInfo()->displayId); |
| 7030 | bool foundWindow = false; |
| 7031 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
| 7032 | if (!foundWindow && otherHandle != windowHandle) { |
| 7033 | continue; |
| 7034 | } |
| 7035 | if (windowHandle == otherHandle) { |
| 7036 | foundWindow = true; |
| 7037 | continue; |
| 7038 | } |
| 7039 | |
| 7040 | if (otherHandle->getInfo()->inputConfig.test(WindowInfo::InputConfig::IS_WALLPAPER)) { |
| 7041 | return otherHandle; |
| 7042 | } |
| 7043 | } |
| 7044 | return nullptr; |
| 7045 | } |
| 7046 | |
Siarhei Vishniakou | fa2a049 | 2023-11-14 13:13:18 -0800 | [diff] [blame] | 7047 | void InputDispatcher::setKeyRepeatConfiguration(std::chrono::nanoseconds timeout, |
| 7048 | std::chrono::nanoseconds delay) { |
Nergi Rahardi | 730cf3c | 2023-04-13 12:41:17 +0900 | [diff] [blame] | 7049 | std::scoped_lock _l(mLock); |
| 7050 | |
Siarhei Vishniakou | fa2a049 | 2023-11-14 13:13:18 -0800 | [diff] [blame] | 7051 | mConfig.keyRepeatTimeout = timeout.count(); |
| 7052 | mConfig.keyRepeatDelay = delay.count(); |
Nergi Rahardi | 730cf3c | 2023-04-13 12:41:17 +0900 | [diff] [blame] | 7053 | } |
| 7054 | |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 7055 | bool InputDispatcher::isPointerInWindow(const sp<android::IBinder>& token, int32_t displayId, |
| 7056 | DeviceId deviceId, int32_t pointerId) { |
| 7057 | std::scoped_lock _l(mLock); |
| 7058 | auto touchStateIt = mTouchStatesByDisplay.find(displayId); |
| 7059 | if (touchStateIt == mTouchStatesByDisplay.end()) { |
| 7060 | return false; |
| 7061 | } |
| 7062 | for (const TouchedWindow& window : touchStateIt->second.windows) { |
| 7063 | if (window.windowHandle->getToken() == token && |
| 7064 | (window.hasTouchingPointer(deviceId, pointerId) || |
| 7065 | window.hasHoveringPointer(deviceId, pointerId))) { |
| 7066 | return true; |
| 7067 | } |
| 7068 | } |
| 7069 | return false; |
| 7070 | } |
| 7071 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 7072 | } // namespace android::inputdispatcher |