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" |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 57 | #include "trace/ThreadedBackend.h" |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 58 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 59 | #define INDENT " " |
| 60 | #define INDENT2 " " |
| 61 | #define INDENT3 " " |
| 62 | #define INDENT4 " " |
| 63 | |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 64 | using namespace android::ftl::flag_operators; |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 65 | using android::base::Error; |
Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 66 | using android::base::HwTimeoutMultiplier; |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 67 | using android::base::Result; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 68 | using android::base::StringPrintf; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 69 | using android::gui::DisplayInfo; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 70 | using android::gui::FocusRequest; |
| 71 | using android::gui::TouchOcclusionMode; |
| 72 | using android::gui::WindowInfo; |
| 73 | using android::gui::WindowInfoHandle; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 74 | using android::os::InputEventInjectionResult; |
| 75 | using android::os::InputEventInjectionSync; |
Ameer Armaly | cff4fa5 | 2023-10-04 23:45:11 +0000 | [diff] [blame] | 76 | namespace input_flags = com::android::input::flags; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 77 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 78 | namespace android::inputdispatcher { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 79 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 80 | namespace { |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 81 | |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 82 | // Input tracing is only available on debuggable builds (userdebug and eng) when the feature |
| 83 | // flag is enabled. When the flag is changed, tracing will only be available after reboot. |
| 84 | bool isInputTracingEnabled() { |
| 85 | static const std::string buildType = base::GetProperty("ro.build.type", "user"); |
| 86 | static const bool isUserdebugOrEng = buildType == "userdebug" || buildType == "eng"; |
| 87 | return input_flags::enable_input_event_tracing() && isUserdebugOrEng; |
| 88 | } |
| 89 | |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 90 | // Create the input tracing backend that writes to perfetto from a single thread. |
| 91 | std::unique_ptr<trace::InputTracingBackendInterface> createInputTracingBackendIfEnabled() { |
| 92 | if (!isInputTracingEnabled()) { |
| 93 | return nullptr; |
| 94 | } |
| 95 | return std::make_unique<trace::impl::ThreadedBackend<trace::impl::PerfettoBackend>>( |
| 96 | trace::impl::PerfettoBackend()); |
| 97 | } |
| 98 | |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 99 | template <class Entry> |
| 100 | void ensureEventTraced(const Entry& entry) { |
| 101 | if (!entry.traceTracker) { |
| 102 | LOG(FATAL) << "Expected event entry to be traced, but it wasn't: " << entry; |
| 103 | } |
| 104 | } |
| 105 | |
Prabir Pradhan | 052fb0b | 2024-02-23 21:03:12 +0000 | [diff] [blame^] | 106 | // Helper to get a trace tracker from a traced key or motion entry. |
| 107 | const std::unique_ptr<trace::EventTrackerInterface>& getTraceTracker(const EventEntry& entry) { |
| 108 | switch (entry.type) { |
| 109 | case EventEntry::Type::MOTION: { |
| 110 | const auto& motion = static_cast<const MotionEntry&>(entry); |
| 111 | ensureEventTraced(motion); |
| 112 | return motion.traceTracker; |
| 113 | } |
| 114 | case EventEntry::Type::KEY: { |
| 115 | const auto& key = static_cast<const KeyEntry&>(entry); |
| 116 | ensureEventTraced(key); |
| 117 | return key.traceTracker; |
| 118 | } |
| 119 | default: { |
| 120 | const static std::unique_ptr<trace::EventTrackerInterface> kNullTracker; |
| 121 | return kNullTracker; |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 126 | // Temporarily releases a held mutex for the lifetime of the instance. |
| 127 | // Named to match std::scoped_lock |
| 128 | class scoped_unlock { |
| 129 | public: |
| 130 | explicit scoped_unlock(std::mutex& mutex) : mMutex(mutex) { mMutex.unlock(); } |
| 131 | ~scoped_unlock() { mMutex.lock(); } |
| 132 | |
| 133 | private: |
| 134 | std::mutex& mMutex; |
| 135 | }; |
| 136 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 137 | // Default input dispatching timeout if there is no focused application or paused window |
| 138 | // from which to determine an appropriate dispatching timeout. |
Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 139 | const std::chrono::duration DEFAULT_INPUT_DISPATCHING_TIMEOUT = std::chrono::milliseconds( |
| 140 | android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS * |
| 141 | HwTimeoutMultiplier()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 142 | |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 143 | // The default minimum time gap between two user activity poke events. |
| 144 | const std::chrono::milliseconds DEFAULT_USER_ACTIVITY_POKE_INTERVAL = 100ms; |
| 145 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 146 | const std::chrono::duration STALE_EVENT_TIMEOUT = std::chrono::seconds(10) * HwTimeoutMultiplier(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 147 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 148 | // 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] | 149 | constexpr nsecs_t SLOW_EVENT_PROCESSING_WARNING_TIMEOUT = 2000 * 1000000LL; // 2sec |
| 150 | |
| 151 | // Log a warning when an interception call takes longer than this to process. |
| 152 | constexpr std::chrono::milliseconds SLOW_INTERCEPTION_THRESHOLD = 50ms; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 153 | |
| 154 | // Number of recent events to keep for debugging purposes. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 155 | constexpr size_t RECENT_QUEUE_MAX_SIZE = 10; |
| 156 | |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 157 | // Event log tags. See EventLogTags.logtags for reference. |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 158 | constexpr int LOGTAG_INPUT_INTERACTION = 62000; |
| 159 | constexpr int LOGTAG_INPUT_FOCUS = 62001; |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 160 | constexpr int LOGTAG_INPUT_CANCEL = 62003; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 161 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 162 | const ui::Transform kIdentityTransform; |
| 163 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 164 | inline nsecs_t now() { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 165 | return systemTime(SYSTEM_TIME_MONOTONIC); |
| 166 | } |
| 167 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 168 | inline const std::string binderToString(const sp<IBinder>& binder) { |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 169 | if (binder == nullptr) { |
| 170 | return "<null>"; |
| 171 | } |
| 172 | return StringPrintf("%p", binder.get()); |
| 173 | } |
| 174 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 175 | static std::string uidString(const gui::Uid& uid) { |
| 176 | return uid.toString(); |
| 177 | } |
| 178 | |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 179 | Result<void> checkKeyAction(int32_t action) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 180 | switch (action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 181 | case AKEY_EVENT_ACTION_DOWN: |
| 182 | case AKEY_EVENT_ACTION_UP: |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 183 | return {}; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 184 | default: |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 185 | return Error() << "Key event has invalid action code " << action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 189 | Result<void> validateKeyEvent(int32_t action) { |
| 190 | return checkKeyAction(action); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 191 | } |
| 192 | |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 193 | Result<void> checkMotionAction(int32_t action, int32_t actionButton, int32_t pointerCount) { |
Siarhei Vishniakou | 2f61bdc | 2022-12-02 08:55:51 -0800 | [diff] [blame] | 194 | switch (MotionEvent::getActionMasked(action)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 195 | case AMOTION_EVENT_ACTION_DOWN: |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 196 | case AMOTION_EVENT_ACTION_UP: { |
| 197 | if (pointerCount != 1) { |
| 198 | return Error() << "invalid pointer count " << pointerCount; |
| 199 | } |
| 200 | return {}; |
| 201 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 202 | case AMOTION_EVENT_ACTION_MOVE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 203 | case AMOTION_EVENT_ACTION_HOVER_ENTER: |
| 204 | case AMOTION_EVENT_ACTION_HOVER_MOVE: |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 205 | case AMOTION_EVENT_ACTION_HOVER_EXIT: { |
| 206 | if (pointerCount < 1) { |
| 207 | return Error() << "invalid pointer count " << pointerCount; |
| 208 | } |
| 209 | return {}; |
| 210 | } |
Siarhei Vishniakou | 2f61bdc | 2022-12-02 08:55:51 -0800 | [diff] [blame] | 211 | case AMOTION_EVENT_ACTION_CANCEL: |
| 212 | case AMOTION_EVENT_ACTION_OUTSIDE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 213 | case AMOTION_EVENT_ACTION_SCROLL: |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 214 | return {}; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 215 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 216 | case AMOTION_EVENT_ACTION_POINTER_UP: { |
Siarhei Vishniakou | 2f61bdc | 2022-12-02 08:55:51 -0800 | [diff] [blame] | 217 | const int32_t index = MotionEvent::getActionIndex(action); |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 218 | if (index < 0) { |
| 219 | return Error() << "invalid index " << index << " for " |
| 220 | << MotionEvent::actionToString(action); |
| 221 | } |
| 222 | if (index >= pointerCount) { |
| 223 | return Error() << "invalid index " << index << " for pointerCount " << pointerCount; |
| 224 | } |
| 225 | if (pointerCount <= 1) { |
| 226 | return Error() << "invalid pointer count " << pointerCount << " for " |
| 227 | << MotionEvent::actionToString(action); |
| 228 | } |
| 229 | return {}; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 230 | } |
| 231 | case AMOTION_EVENT_ACTION_BUTTON_PRESS: |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 232 | case AMOTION_EVENT_ACTION_BUTTON_RELEASE: { |
| 233 | if (actionButton == 0) { |
| 234 | return Error() << "action button should be nonzero for " |
| 235 | << MotionEvent::actionToString(action); |
| 236 | } |
| 237 | return {}; |
| 238 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 239 | default: |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 240 | return Error() << "invalid action " << action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 244 | int64_t millis(std::chrono::nanoseconds t) { |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 245 | return std::chrono::duration_cast<std::chrono::milliseconds>(t).count(); |
| 246 | } |
| 247 | |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 248 | Result<void> validateMotionEvent(int32_t action, int32_t actionButton, size_t pointerCount, |
| 249 | const PointerProperties* pointerProperties) { |
| 250 | Result<void> actionCheck = checkMotionAction(action, actionButton, pointerCount); |
| 251 | if (!actionCheck.ok()) { |
| 252 | return actionCheck; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 253 | } |
| 254 | if (pointerCount < 1 || pointerCount > MAX_POINTERS) { |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 255 | return Error() << "Motion event has invalid pointer count " << pointerCount |
| 256 | << "; value must be between 1 and " << MAX_POINTERS << "."; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 257 | } |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 258 | std::bitset<MAX_POINTER_ID + 1> pointerIdBits; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 259 | for (size_t i = 0; i < pointerCount; i++) { |
| 260 | int32_t id = pointerProperties[i].id; |
| 261 | if (id < 0 || id > MAX_POINTER_ID) { |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 262 | return Error() << "Motion event has invalid pointer id " << id |
| 263 | << "; value must be between 0 and " << MAX_POINTER_ID; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 264 | } |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 265 | if (pointerIdBits.test(id)) { |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 266 | return Error() << "Motion event has duplicate pointer id " << id; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 267 | } |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 268 | pointerIdBits.set(id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 269 | } |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 270 | return {}; |
| 271 | } |
| 272 | |
| 273 | Result<void> validateInputEvent(const InputEvent& event) { |
| 274 | switch (event.getType()) { |
| 275 | case InputEventType::KEY: { |
| 276 | const KeyEvent& key = static_cast<const KeyEvent&>(event); |
| 277 | const int32_t action = key.getAction(); |
| 278 | return validateKeyEvent(action); |
| 279 | } |
| 280 | case InputEventType::MOTION: { |
| 281 | const MotionEvent& motion = static_cast<const MotionEvent&>(event); |
| 282 | const int32_t action = motion.getAction(); |
| 283 | const size_t pointerCount = motion.getPointerCount(); |
| 284 | const PointerProperties* pointerProperties = motion.getPointerProperties(); |
| 285 | const int32_t actionButton = motion.getActionButton(); |
| 286 | return validateMotionEvent(action, actionButton, pointerCount, pointerProperties); |
| 287 | } |
| 288 | default: { |
| 289 | return {}; |
| 290 | } |
| 291 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 292 | } |
| 293 | |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 294 | std::bitset<MAX_POINTER_ID + 1> getPointerIds(const std::vector<PointerProperties>& pointers) { |
| 295 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
| 296 | for (const PointerProperties& pointer : pointers) { |
| 297 | pointerIds.set(pointer.id); |
| 298 | } |
| 299 | return pointerIds; |
| 300 | } |
| 301 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 302 | std::string dumpRegion(const Region& region) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 303 | if (region.isEmpty()) { |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 304 | return "<empty>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 305 | } |
| 306 | |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 307 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 308 | bool first = true; |
| 309 | Region::const_iterator cur = region.begin(); |
| 310 | Region::const_iterator const tail = region.end(); |
| 311 | while (cur != tail) { |
| 312 | if (first) { |
| 313 | first = false; |
| 314 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 315 | dump += "|"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 316 | } |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 317 | 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] | 318 | cur++; |
| 319 | } |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 320 | return dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 321 | } |
| 322 | |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 323 | std::string dumpQueue(const std::deque<std::unique_ptr<DispatchEntry>>& queue, |
| 324 | nsecs_t currentTime) { |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 325 | constexpr size_t maxEntries = 50; // max events to print |
| 326 | constexpr size_t skipBegin = maxEntries / 2; |
| 327 | const size_t skipEnd = queue.size() - maxEntries / 2; |
| 328 | // skip from maxEntries / 2 ... size() - maxEntries/2 |
| 329 | // only print from 0 .. skipBegin and then from skipEnd .. size() |
| 330 | |
| 331 | std::string dump; |
| 332 | for (size_t i = 0; i < queue.size(); i++) { |
| 333 | const DispatchEntry& entry = *queue[i]; |
| 334 | if (i >= skipBegin && i < skipEnd) { |
| 335 | dump += StringPrintf(INDENT4 "<skipped %zu entries>\n", skipEnd - skipBegin); |
| 336 | i = skipEnd - 1; // it will be incremented to "skipEnd" by 'continue' |
| 337 | continue; |
| 338 | } |
| 339 | dump.append(INDENT4); |
| 340 | dump += entry.eventEntry->getDescription(); |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 341 | dump += StringPrintf(", seq=%" PRIu32 ", targetFlags=%s, age=%" PRId64 "ms", entry.seq, |
| 342 | entry.targetFlags.string().c_str(), |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 343 | ns2ms(currentTime - entry.eventEntry->eventTime)); |
| 344 | if (entry.deliveryTime != 0) { |
| 345 | // This entry was delivered, so add information on how long we've been waiting |
| 346 | dump += StringPrintf(", wait=%" PRId64 "ms", ns2ms(currentTime - entry.deliveryTime)); |
| 347 | } |
| 348 | dump.append("\n"); |
| 349 | } |
| 350 | return dump; |
| 351 | } |
| 352 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 353 | /** |
| 354 | * Find the entry in std::unordered_map by key, and return it. |
| 355 | * If the entry is not found, return a default constructed entry. |
| 356 | * |
| 357 | * Useful when the entries are vectors, since an empty vector will be returned |
| 358 | * if the entry is not found. |
| 359 | * Also useful when the entries are sp<>. If an entry is not found, nullptr is returned. |
| 360 | */ |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 361 | template <typename K, typename V> |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 362 | V getValueByKey(const std::unordered_map<K, V>& map, K key) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 363 | auto it = map.find(key); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 364 | return it != map.end() ? it->second : V{}; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 365 | } |
| 366 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 367 | bool haveSameToken(const sp<WindowInfoHandle>& first, const sp<WindowInfoHandle>& second) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 368 | if (first == second) { |
| 369 | return true; |
| 370 | } |
| 371 | |
| 372 | if (first == nullptr || second == nullptr) { |
| 373 | return false; |
| 374 | } |
| 375 | |
| 376 | return first->getToken() == second->getToken(); |
| 377 | } |
| 378 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 379 | bool haveSameApplicationToken(const WindowInfo* first, const WindowInfo* second) { |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 380 | if (first == nullptr || second == nullptr) { |
| 381 | return false; |
| 382 | } |
| 383 | return first->applicationInfo.token != nullptr && |
| 384 | first->applicationInfo.token == second->applicationInfo.token; |
| 385 | } |
| 386 | |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 387 | template <typename T> |
| 388 | size_t firstMarkedBit(T set) { |
| 389 | // TODO: replace with std::countr_zero from <bit> when that's available |
| 390 | LOG_ALWAYS_FATAL_IF(set.none()); |
| 391 | size_t i = 0; |
| 392 | while (!set.test(i)) { |
| 393 | i++; |
| 394 | } |
| 395 | return i; |
| 396 | } |
| 397 | |
Prabir Pradhan | c88b1fa | 2024-01-23 21:48:03 +0000 | [diff] [blame] | 398 | std::unique_ptr<DispatchEntry> createDispatchEntry(const IdGenerator& idGenerator, |
| 399 | const InputTarget& inputTarget, |
Prabir Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame] | 400 | std::shared_ptr<const EventEntry> eventEntry, |
| 401 | ftl::Flags<InputTarget::Flags> inputTargetFlags, |
| 402 | int64_t vsyncId) { |
Prabir Pradhan | c88b1fa | 2024-01-23 21:48:03 +0000 | [diff] [blame] | 403 | const bool zeroCoords = inputTargetFlags.test(InputTarget::Flags::ZERO_COORDS); |
Prabir Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame] | 404 | const sp<WindowInfoHandle> win = inputTarget.windowHandle; |
| 405 | const std::optional<int32_t> windowId = |
| 406 | win ? std::make_optional(win->getInfo()->id) : std::nullopt; |
| 407 | // Assume the only targets that are not associated with a window are global monitors, and use |
| 408 | // the system UID for global monitors for tracing purposes. |
| 409 | const gui::Uid uid = win ? win->getInfo()->ownerUid : gui::Uid(AID_SYSTEM); |
Prabir Pradhan | c88b1fa | 2024-01-23 21:48:03 +0000 | [diff] [blame] | 410 | |
| 411 | if (inputTarget.useDefaultPointerTransform() && !zeroCoords) { |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 412 | const ui::Transform& transform = inputTarget.getDefaultPointerTransform(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 413 | return std::make_unique<DispatchEntry>(eventEntry, inputTargetFlags, transform, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 414 | inputTarget.displayTransform, |
Prabir Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame] | 415 | inputTarget.globalScaleFactor, uid, vsyncId, |
| 416 | windowId); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | ALOG_ASSERT(eventEntry->type == EventEntry::Type::MOTION); |
| 420 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); |
| 421 | |
Prabir Pradhan | c88b1fa | 2024-01-23 21:48:03 +0000 | [diff] [blame] | 422 | std::vector<PointerCoords> pointerCoords{motionEntry.getPointerCount()}; |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 423 | |
Prabir Pradhan | c88b1fa | 2024-01-23 21:48:03 +0000 | [diff] [blame] | 424 | const ui::Transform* transform = &kIdentityTransform; |
| 425 | const ui::Transform* displayTransform = &kIdentityTransform; |
| 426 | if (zeroCoords) { |
| 427 | std::for_each(pointerCoords.begin(), pointerCoords.end(), [](auto& pc) { pc.clear(); }); |
| 428 | } else { |
| 429 | // Use the first pointer information to normalize all other pointers. This could be any |
| 430 | // pointer as long as all other pointers are normalized to the same value and the final |
| 431 | // DispatchEntry uses the transform for the normalized pointer. |
| 432 | transform = |
| 433 | &inputTarget.getTransformForPointer(firstMarkedBit(inputTarget.getPointerIds())); |
| 434 | const ui::Transform inverseTransform = transform->inverse(); |
| 435 | displayTransform = &inputTarget.displayTransform; |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 436 | |
Prabir Pradhan | c88b1fa | 2024-01-23 21:48:03 +0000 | [diff] [blame] | 437 | // Iterate through all pointers in the event to normalize against the first. |
| 438 | for (size_t i = 0; i < motionEntry.getPointerCount(); i++) { |
| 439 | PointerCoords& newCoords = pointerCoords[i]; |
| 440 | const auto pointerId = motionEntry.pointerProperties[i].id; |
| 441 | const ui::Transform& currTransform = inputTarget.getTransformForPointer(pointerId); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 442 | |
Prabir Pradhan | c88b1fa | 2024-01-23 21:48:03 +0000 | [diff] [blame] | 443 | newCoords.copyFrom(motionEntry.pointerCoords[i]); |
| 444 | // First, apply the current pointer's transform to update the coordinates into |
| 445 | // window space. |
| 446 | newCoords.transform(currTransform); |
| 447 | // Next, apply the inverse transform of the normalized coordinates so the |
| 448 | // current coordinates are transformed into the normalized coordinate space. |
| 449 | newCoords.transform(inverseTransform); |
| 450 | } |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 453 | std::unique_ptr<MotionEntry> combinedMotionEntry = |
Prabir Pradhan | c88b1fa | 2024-01-23 21:48:03 +0000 | [diff] [blame] | 454 | std::make_unique<MotionEntry>(idGenerator.nextId(), motionEntry.injectionState, |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 455 | motionEntry.eventTime, motionEntry.deviceId, |
| 456 | motionEntry.source, motionEntry.displayId, |
| 457 | motionEntry.policyFlags, motionEntry.action, |
| 458 | motionEntry.actionButton, motionEntry.flags, |
| 459 | motionEntry.metaState, motionEntry.buttonState, |
| 460 | motionEntry.classification, motionEntry.edgeFlags, |
| 461 | motionEntry.xPrecision, motionEntry.yPrecision, |
| 462 | motionEntry.xCursorPosition, motionEntry.yCursorPosition, |
| 463 | motionEntry.downTime, motionEntry.pointerProperties, |
| 464 | pointerCoords); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 465 | |
| 466 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 467 | std::make_unique<DispatchEntry>(std::move(combinedMotionEntry), inputTargetFlags, |
Prabir Pradhan | c88b1fa | 2024-01-23 21:48:03 +0000 | [diff] [blame] | 468 | *transform, *displayTransform, |
Prabir Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame] | 469 | inputTarget.globalScaleFactor, uid, vsyncId, windowId); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 470 | return dispatchEntry; |
| 471 | } |
| 472 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 473 | template <typename T> |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 474 | 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] | 475 | if (lhs == nullptr && rhs == nullptr) { |
| 476 | return true; |
| 477 | } |
| 478 | if (lhs == nullptr || rhs == nullptr) { |
| 479 | return false; |
| 480 | } |
| 481 | return *lhs == *rhs; |
| 482 | } |
| 483 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 484 | KeyEvent createKeyEvent(const KeyEntry& entry) { |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 485 | KeyEvent event; |
| 486 | event.initialize(entry.id, entry.deviceId, entry.source, entry.displayId, INVALID_HMAC, |
| 487 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, |
| 488 | entry.repeatCount, entry.downTime, entry.eventTime); |
| 489 | return event; |
| 490 | } |
| 491 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 492 | bool shouldReportMetricsForConnection(const Connection& connection) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 493 | // Do not keep track of gesture monitors. They receive every event and would disproportionately |
| 494 | // affect the statistics. |
| 495 | if (connection.monitor) { |
| 496 | return false; |
| 497 | } |
| 498 | // If the connection is experiencing ANR, let's skip it. We have separate ANR metrics |
| 499 | if (!connection.responsive) { |
| 500 | return false; |
| 501 | } |
| 502 | return true; |
| 503 | } |
| 504 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 505 | bool shouldReportFinishedEvent(const DispatchEntry& dispatchEntry, const Connection& connection) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 506 | const EventEntry& eventEntry = *dispatchEntry.eventEntry; |
| 507 | const int32_t& inputEventId = eventEntry.id; |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 508 | if (inputEventId == android::os::IInputConstants::INVALID_INPUT_EVENT_ID) { |
| 509 | return false; |
| 510 | } |
| 511 | // Only track latency for events that originated from hardware |
| 512 | if (eventEntry.isSynthesized()) { |
| 513 | return false; |
| 514 | } |
| 515 | const EventEntry::Type& inputEventEntryType = eventEntry.type; |
| 516 | if (inputEventEntryType == EventEntry::Type::KEY) { |
| 517 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 518 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
| 519 | return false; |
| 520 | } |
| 521 | } else if (inputEventEntryType == EventEntry::Type::MOTION) { |
| 522 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 523 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL || |
| 524 | motionEntry.action == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 525 | return false; |
| 526 | } |
| 527 | } else { |
| 528 | // Not a key or a motion |
| 529 | return false; |
| 530 | } |
| 531 | if (!shouldReportMetricsForConnection(connection)) { |
| 532 | return false; |
| 533 | } |
| 534 | return true; |
| 535 | } |
| 536 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 537 | /** |
| 538 | * Connection is responsive if it has no events in the waitQueue that are older than the |
| 539 | * current time. |
| 540 | */ |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 541 | bool isConnectionResponsive(const Connection& connection) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 542 | const nsecs_t currentTime = now(); |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 543 | for (const auto& dispatchEntry : connection.waitQueue) { |
| 544 | if (dispatchEntry->timeoutTime < currentTime) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 545 | return false; |
| 546 | } |
| 547 | } |
| 548 | return true; |
| 549 | } |
| 550 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 551 | // Returns true if the event type passed as argument represents a user activity. |
| 552 | bool isUserActivityEvent(const EventEntry& eventEntry) { |
| 553 | switch (eventEntry.type) { |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 554 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 555 | case EventEntry::Type::DEVICE_RESET: |
| 556 | case EventEntry::Type::DRAG: |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 557 | case EventEntry::Type::FOCUS: |
| 558 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 559 | case EventEntry::Type::SENSOR: |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 560 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 561 | return false; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 562 | case EventEntry::Type::KEY: |
| 563 | case EventEntry::Type::MOTION: |
| 564 | return true; |
| 565 | } |
| 566 | } |
| 567 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 568 | // 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] | 569 | bool windowAcceptsTouchAt(const WindowInfo& windowInfo, int32_t displayId, float x, float y, |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 570 | bool isStylus, const ui::Transform& displayTransform) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 571 | const auto inputConfig = windowInfo.inputConfig; |
| 572 | if (windowInfo.displayId != displayId || |
| 573 | inputConfig.test(WindowInfo::InputConfig::NOT_VISIBLE)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 574 | return false; |
| 575 | } |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 576 | const bool windowCanInterceptTouch = isStylus && windowInfo.interceptsStylus(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 577 | if (inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE) && !windowCanInterceptTouch) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 578 | return false; |
| 579 | } |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 580 | |
| 581 | // Window Manager works in the logical display coordinate space. When it specifies bounds for a |
| 582 | // window as (l, t, r, b), the range of x in [l, r) and y in [t, b) are considered to be inside |
| 583 | // the window. Points on the right and bottom edges should not be inside the window, so we need |
| 584 | // to be careful about performing a hit test when the display is rotated, since the "right" and |
| 585 | // "bottom" of the window will be different in the display (un-rotated) space compared to in the |
| 586 | // logical display in which WM determined the bounds. Perform the hit test in the logical |
| 587 | // display space to ensure these edges are considered correctly in all orientations. |
| 588 | const auto touchableRegion = displayTransform.transform(windowInfo.touchableRegion); |
| 589 | const auto p = displayTransform.transform(x, y); |
| 590 | if (!touchableRegion.contains(std::floor(p.x), std::floor(p.y))) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 591 | return false; |
| 592 | } |
| 593 | return true; |
| 594 | } |
| 595 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 596 | bool isPointerFromStylus(const MotionEntry& entry, int32_t pointerIndex) { |
| 597 | return isFromSource(entry.source, AINPUT_SOURCE_STYLUS) && |
Prabir Pradhan | e562696 | 2022-10-27 20:30:53 +0000 | [diff] [blame] | 598 | isStylusToolType(entry.pointerProperties[pointerIndex].toolType); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 599 | } |
| 600 | |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 601 | // Determines if the given window can be targeted as InputTarget::Flags::FOREGROUND. |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 602 | // Foreground events are only sent to "foreground targetable" windows, but not all gestures sent to |
| 603 | // such window are necessarily targeted with the flag. For example, an event with ACTION_OUTSIDE can |
| 604 | // 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] | 605 | // InputTarget::Flags::FOREGROUND. |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 606 | bool canReceiveForegroundTouches(const WindowInfo& info) { |
| 607 | // A non-touchable window can still receive touch events (e.g. in the case of |
| 608 | // STYLUS_INTERCEPTOR), so prevent such windows from receiving foreground events for touches. |
| 609 | return !info.inputConfig.test(gui::WindowInfo::InputConfig::NOT_TOUCHABLE) && !info.isSpy(); |
| 610 | } |
| 611 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 612 | bool isWindowOwnedBy(const sp<WindowInfoHandle>& windowHandle, gui::Pid pid, gui::Uid uid) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 613 | if (windowHandle == nullptr) { |
| 614 | return false; |
| 615 | } |
| 616 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 617 | if (pid == windowInfo->ownerPid && uid == windowInfo->ownerUid) { |
| 618 | return true; |
| 619 | } |
| 620 | return false; |
| 621 | } |
| 622 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 623 | // Checks targeted injection using the window's owner's uid. |
| 624 | // Returns an empty string if an entry can be sent to the given window, or an error message if the |
| 625 | // entry is a targeted injection whose uid target doesn't match the window owner. |
| 626 | std::optional<std::string> verifyTargetedInjection(const sp<WindowInfoHandle>& window, |
| 627 | const EventEntry& entry) { |
| 628 | if (entry.injectionState == nullptr || !entry.injectionState->targetUid) { |
| 629 | // The event was not injected, or the injected event does not target a window. |
| 630 | return {}; |
| 631 | } |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 632 | const auto uid = *entry.injectionState->targetUid; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 633 | if (window == nullptr) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 634 | return StringPrintf("No valid window target for injection into uid %s.", |
| 635 | uid.toString().c_str()); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 636 | } |
| 637 | if (entry.injectionState->targetUid != window->getInfo()->ownerUid) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 638 | return StringPrintf("Injected event targeted at uid %s would be dispatched to window '%s' " |
| 639 | "owned by uid %s.", |
| 640 | uid.toString().c_str(), window->getName().c_str(), |
| 641 | window->getInfo()->ownerUid.toString().c_str()); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 642 | } |
| 643 | return {}; |
| 644 | } |
| 645 | |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 646 | std::pair<float, float> resolveTouchedPosition(const MotionEntry& entry) { |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 647 | const bool isFromMouse = isFromSource(entry.source, AINPUT_SOURCE_MOUSE); |
| 648 | // Always dispatch mouse events to cursor position. |
| 649 | if (isFromMouse) { |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 650 | return {entry.xCursorPosition, entry.yCursorPosition}; |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 651 | } |
| 652 | |
Siarhei Vishniakou | 5b9766d | 2023-07-18 14:06:29 -0700 | [diff] [blame] | 653 | const int32_t pointerIndex = MotionEvent::getActionIndex(entry.action); |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 654 | return {entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 655 | entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_Y)}; |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 656 | } |
| 657 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 658 | std::optional<nsecs_t> getDownTime(const EventEntry& eventEntry) { |
| 659 | if (eventEntry.type == EventEntry::Type::KEY) { |
| 660 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 661 | return keyEntry.downTime; |
| 662 | } else if (eventEntry.type == EventEntry::Type::MOTION) { |
| 663 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 664 | return motionEntry.downTime; |
| 665 | } |
| 666 | return std::nullopt; |
| 667 | } |
| 668 | |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 669 | /** |
| 670 | * Compare the old touch state to the new touch state, and generate the corresponding touched |
| 671 | * windows (== input targets). |
| 672 | * If a window had the hovering pointer, but now it doesn't, produce HOVER_EXIT for that window. |
| 673 | * If the pointer just entered the new window, produce HOVER_ENTER. |
| 674 | * For pointers remaining in the window, produce HOVER_MOVE. |
| 675 | */ |
| 676 | std::vector<TouchedWindow> getHoveringWindowsLocked(const TouchState* oldState, |
| 677 | const TouchState& newTouchState, |
| 678 | const MotionEntry& entry) { |
| 679 | std::vector<TouchedWindow> out; |
| 680 | const int32_t maskedAction = MotionEvent::getActionMasked(entry.action); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 681 | |
| 682 | if (maskedAction == AMOTION_EVENT_ACTION_SCROLL) { |
| 683 | // ACTION_SCROLL events should not affect the hovering pointer dispatch |
| 684 | return {}; |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | // 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] | 688 | const PointerProperties& pointer = entry.pointerProperties[0]; |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 689 | |
| 690 | std::set<sp<WindowInfoHandle>> oldWindows; |
| 691 | if (oldState != nullptr) { |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 692 | oldWindows = oldState->getWindowsWithHoveringPointer(entry.deviceId, pointer.id); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | std::set<sp<WindowInfoHandle>> newWindows = |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 696 | newTouchState.getWindowsWithHoveringPointer(entry.deviceId, pointer.id); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 697 | |
| 698 | // If the pointer is no longer in the new window set, send HOVER_EXIT. |
| 699 | for (const sp<WindowInfoHandle>& oldWindow : oldWindows) { |
| 700 | if (newWindows.find(oldWindow) == newWindows.end()) { |
| 701 | TouchedWindow touchedWindow; |
| 702 | touchedWindow.windowHandle = oldWindow; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 703 | touchedWindow.dispatchMode = InputTarget::DispatchMode::HOVER_EXIT; |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 704 | out.push_back(touchedWindow); |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | for (const sp<WindowInfoHandle>& newWindow : newWindows) { |
| 709 | TouchedWindow touchedWindow; |
| 710 | touchedWindow.windowHandle = newWindow; |
| 711 | if (oldWindows.find(newWindow) == oldWindows.end()) { |
| 712 | // Any windows that have this pointer now, and didn't have it before, should get |
| 713 | // HOVER_ENTER |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 714 | touchedWindow.dispatchMode = InputTarget::DispatchMode::HOVER_ENTER; |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 715 | } else { |
| 716 | // This pointer was already sent to the window. Use ACTION_HOVER_MOVE. |
Siarhei Vishniakou | c2eb850 | 2023-04-11 18:33:36 -0700 | [diff] [blame] | 717 | if (CC_UNLIKELY(maskedAction != AMOTION_EVENT_ACTION_HOVER_MOVE)) { |
Daniel Norman | 7487dfa | 2023-08-02 16:39:45 -0700 | [diff] [blame] | 718 | android::base::LogSeverity severity = android::base::LogSeverity::FATAL; |
Ameer Armaly | cff4fa5 | 2023-10-04 23:45:11 +0000 | [diff] [blame] | 719 | if (!input_flags::a11y_crash_on_inconsistent_event_stream() && |
| 720 | entry.flags & AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT) { |
Daniel Norman | 7487dfa | 2023-08-02 16:39:45 -0700 | [diff] [blame] | 721 | // The Accessibility injected touch exploration event stream |
| 722 | // has known inconsistencies, so log ERROR instead of |
| 723 | // crashing the device with FATAL. |
Daniel Norman | 7487dfa | 2023-08-02 16:39:45 -0700 | [diff] [blame] | 724 | severity = android::base::LogSeverity::ERROR; |
| 725 | } |
| 726 | LOG(severity) << "Expected ACTION_HOVER_MOVE instead of " << entry.getDescription(); |
Siarhei Vishniakou | c2eb850 | 2023-04-11 18:33:36 -0700 | [diff] [blame] | 727 | } |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 728 | touchedWindow.dispatchMode = InputTarget::DispatchMode::AS_IS; |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 729 | } |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 730 | touchedWindow.addHoveringPointer(entry.deviceId, pointer); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 731 | if (canReceiveForegroundTouches(*newWindow->getInfo())) { |
| 732 | touchedWindow.targetFlags |= InputTarget::Flags::FOREGROUND; |
| 733 | } |
| 734 | out.push_back(touchedWindow); |
| 735 | } |
| 736 | return out; |
| 737 | } |
| 738 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 739 | template <typename T> |
| 740 | std::vector<T>& operator+=(std::vector<T>& left, const std::vector<T>& right) { |
| 741 | left.insert(left.end(), right.begin(), right.end()); |
| 742 | return left; |
| 743 | } |
| 744 | |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 745 | // Filter windows in a TouchState and targets in a vector to remove untrusted windows/targets from |
| 746 | // both. |
| 747 | void filterUntrustedTargets(TouchState& touchState, std::vector<InputTarget>& targets) { |
| 748 | std::erase_if(touchState.windows, [&](const TouchedWindow& window) { |
| 749 | if (!window.windowHandle->getInfo()->inputConfig.test( |
| 750 | WindowInfo::InputConfig::TRUSTED_OVERLAY)) { |
| 751 | // In addition to TouchState, erase this window from the input targets! We don't have a |
| 752 | // good way to do this today except by adding a nested loop. |
| 753 | // TODO(b/282025641): simplify this code once InputTargets are being identified |
| 754 | // separately from TouchedWindows. |
| 755 | std::erase_if(targets, [&](const InputTarget& target) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 756 | return target.connection->getToken() == window.windowHandle->getToken(); |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 757 | }); |
| 758 | return true; |
| 759 | } |
| 760 | return false; |
| 761 | }); |
| 762 | } |
| 763 | |
Siarhei Vishniakou | ce1fd47 | 2023-09-18 18:38:07 -0700 | [diff] [blame] | 764 | /** |
| 765 | * In general, touch should be always split between windows. Some exceptions: |
| 766 | * 1. Don't split touch if all of the below is true: |
| 767 | * (a) we have an active pointer down *and* |
| 768 | * (b) a new pointer is going down that's from the same device *and* |
| 769 | * (c) the window that's receiving the current pointer does not support split touch. |
| 770 | * 2. Don't split mouse events |
| 771 | */ |
| 772 | bool shouldSplitTouch(const TouchState& touchState, const MotionEntry& entry) { |
| 773 | if (isFromSource(entry.source, AINPUT_SOURCE_MOUSE)) { |
| 774 | // We should never split mouse events |
| 775 | return false; |
| 776 | } |
| 777 | for (const TouchedWindow& touchedWindow : touchState.windows) { |
| 778 | if (touchedWindow.windowHandle->getInfo()->isSpy()) { |
| 779 | // Spy windows should not affect whether or not touch is split. |
| 780 | continue; |
| 781 | } |
| 782 | if (touchedWindow.windowHandle->getInfo()->supportsSplitTouch()) { |
| 783 | continue; |
| 784 | } |
| 785 | if (touchedWindow.windowHandle->getInfo()->inputConfig.test( |
| 786 | gui::WindowInfo::InputConfig::IS_WALLPAPER)) { |
| 787 | // Wallpaper window should not affect whether or not touch is split |
| 788 | continue; |
| 789 | } |
| 790 | |
| 791 | if (touchedWindow.hasTouchingPointers(entry.deviceId)) { |
| 792 | return false; |
| 793 | } |
| 794 | } |
| 795 | return true; |
| 796 | } |
| 797 | |
Siarhei Vishniakou | f77f60a | 2023-10-23 17:26:05 -0700 | [diff] [blame] | 798 | /** |
| 799 | * Return true if stylus is currently down anywhere on the specified display, and false otherwise. |
| 800 | */ |
| 801 | bool isStylusActiveInDisplay( |
| 802 | int32_t displayId, |
| 803 | const std::unordered_map<int32_t /*displayId*/, TouchState>& touchStatesByDisplay) { |
| 804 | const auto it = touchStatesByDisplay.find(displayId); |
| 805 | if (it == touchStatesByDisplay.end()) { |
| 806 | return false; |
| 807 | } |
| 808 | const TouchState& state = it->second; |
| 809 | return state.hasActiveStylus(); |
| 810 | } |
| 811 | |
Siarhei Vishniakou | aeed0da | 2024-01-09 08:57:13 -0800 | [diff] [blame] | 812 | Result<void> validateWindowInfosUpdate(const gui::WindowInfosUpdate& update) { |
| 813 | struct HashFunction { |
| 814 | size_t operator()(const WindowInfo& info) const { return info.id; } |
| 815 | }; |
| 816 | |
| 817 | std::unordered_set<WindowInfo, HashFunction> windowSet; |
| 818 | for (const WindowInfo& info : update.windowInfos) { |
| 819 | const auto [_, inserted] = windowSet.insert(info); |
| 820 | if (!inserted) { |
| 821 | return Error() << "Duplicate entry for " << info; |
| 822 | } |
| 823 | } |
| 824 | return {}; |
| 825 | } |
| 826 | |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 827 | int32_t getUserActivityEventType(const EventEntry& eventEntry) { |
| 828 | switch (eventEntry.type) { |
| 829 | case EventEntry::Type::KEY: { |
| 830 | return USER_ACTIVITY_EVENT_BUTTON; |
| 831 | } |
| 832 | case EventEntry::Type::MOTION: { |
| 833 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 834 | if (MotionEvent::isTouchEvent(motionEntry.source, motionEntry.action)) { |
| 835 | return USER_ACTIVITY_EVENT_TOUCH; |
| 836 | } |
| 837 | return USER_ACTIVITY_EVENT_OTHER; |
| 838 | } |
| 839 | default: { |
| 840 | LOG_ALWAYS_FATAL("%s events are not user activity", |
| 841 | ftl::enum_string(eventEntry.type).c_str()); |
| 842 | } |
| 843 | } |
| 844 | } |
| 845 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 846 | std::pair<bool /*cancelPointers*/, bool /*cancelNonPointers*/> expandCancellationMode( |
| 847 | CancelationOptions::Mode mode) { |
| 848 | switch (mode) { |
| 849 | case CancelationOptions::Mode::CANCEL_ALL_EVENTS: |
| 850 | return {true, true}; |
| 851 | case CancelationOptions::Mode::CANCEL_POINTER_EVENTS: |
| 852 | return {true, false}; |
| 853 | case CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS: |
| 854 | return {false, true}; |
| 855 | case CancelationOptions::Mode::CANCEL_FALLBACK_EVENTS: |
| 856 | return {false, true}; |
| 857 | } |
| 858 | } |
| 859 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 860 | } // namespace |
| 861 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 862 | // --- InputDispatcher --- |
| 863 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 864 | InputDispatcher::InputDispatcher(InputDispatcherPolicyInterface& policy) |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 865 | : InputDispatcher(policy, createInputTracingBackendIfEnabled()) {} |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 866 | |
| 867 | InputDispatcher::InputDispatcher(InputDispatcherPolicyInterface& policy, |
| 868 | std::unique_ptr<trace::InputTracingBackendInterface> traceBackend) |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 869 | : mPolicy(policy), |
| 870 | mPendingEvent(nullptr), |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 871 | mLastDropReason(DropReason::NOT_DROPPED), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 872 | mIdGenerator(IdGenerator::Source::INPUT_DISPATCHER), |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 873 | mMinTimeBetweenUserActivityPokes(DEFAULT_USER_ACTIVITY_POKE_INTERVAL), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 874 | mNextUnblockedEvent(nullptr), |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 875 | mMonitorDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 876 | mDispatchEnabled(false), |
| 877 | mDispatchFrozen(false), |
| 878 | mInputFilterEnabled(false), |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 879 | mMaximumObscuringOpacityForTouch(1.0f), |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 880 | mFocusedDisplayId(ADISPLAY_ID_DEFAULT), |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 881 | mWindowTokenWithPointerCapture(nullptr), |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 882 | mLatencyAggregator(), |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 883 | mLatencyTracker(&mLatencyAggregator) { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 884 | mLooper = sp<Looper>::make(false); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 885 | mReporter = createInputReporter(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 886 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 887 | mWindowInfoListener = sp<DispatcherWindowListener>::make(*this); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 888 | #if defined(__ANDROID__) |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 889 | SurfaceComposerClient::getDefault()->addWindowInfosListener(mWindowInfoListener); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 890 | #endif |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 891 | mKeyRepeatState.lastKeyEntry = nullptr; |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 892 | |
| 893 | if (traceBackend) { |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 894 | mTracer = std::make_unique<trace::impl::InputTracer>(std::move(traceBackend)); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 895 | } |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 896 | |
| 897 | mLastUserActivityTimes.fill(0); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | InputDispatcher::~InputDispatcher() { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 901 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 902 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 903 | resetKeyRepeatLocked(); |
| 904 | releasePendingEventLocked(); |
| 905 | drainInboundQueueLocked(); |
| 906 | mCommandQueue.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 907 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 908 | while (!mConnectionsByToken.empty()) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 909 | std::shared_ptr<Connection> connection = mConnectionsByToken.begin()->second; |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 910 | removeInputChannelLocked(connection->getToken(), /*notify=*/false); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 911 | } |
| 912 | } |
| 913 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 914 | status_t InputDispatcher::start() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 915 | if (mThread) { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 916 | return ALREADY_EXISTS; |
| 917 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 918 | mThread = std::make_unique<InputThread>( |
| 919 | "InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); }); |
| 920 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | status_t InputDispatcher::stop() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 924 | if (mThread && mThread->isCallingThread()) { |
| 925 | ALOGE("InputDispatcher cannot be stopped from its own thread!"); |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 926 | return INVALID_OPERATION; |
| 927 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 928 | mThread.reset(); |
| 929 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 930 | } |
| 931 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 932 | void InputDispatcher::dispatchOnce() { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 933 | nsecs_t nextWakeupTime = LLONG_MAX; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 934 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 935 | std::scoped_lock _l(mLock); |
| 936 | mDispatcherIsAlive.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 937 | |
| 938 | // Run a dispatch loop if there are no pending commands. |
| 939 | // The dispatch loop might enqueue commands to run afterwards. |
| 940 | if (!haveCommandsLocked()) { |
Siarhei Vishniakou | 6950596 | 2023-12-28 12:07:04 -0800 | [diff] [blame] | 941 | dispatchOnceInnerLocked(/*byref*/ nextWakeupTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | // Run all pending commands if there are any. |
| 945 | // 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] | 946 | if (runCommandsLockedInterruptable()) { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 947 | nextWakeupTime = LLONG_MIN; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 948 | } |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 949 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 950 | // If we are still waiting for ack on some events, |
| 951 | // we might have to wake up earlier to check if an app is anr'ing. |
| 952 | const nsecs_t nextAnrCheck = processAnrsLocked(); |
| 953 | nextWakeupTime = std::min(nextWakeupTime, nextAnrCheck); |
| 954 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 955 | // We are about to enter an infinitely long sleep, because we have no commands or |
| 956 | // pending or queued events |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 957 | if (nextWakeupTime == LLONG_MAX) { |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 958 | mDispatcherEnteredIdle.notify_all(); |
| 959 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 960 | } // release lock |
| 961 | |
| 962 | // Wait for callback or timeout or wake. (make sure we round up, not down) |
| 963 | nsecs_t currentTime = now(); |
| 964 | int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime); |
| 965 | mLooper->pollOnce(timeoutMillis); |
| 966 | } |
| 967 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 968 | /** |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 969 | * Raise ANR if there is no focused window. |
| 970 | * Before the ANR is raised, do a final state check: |
| 971 | * 1. The currently focused application must be the same one we are waiting for. |
| 972 | * 2. Ensure we still don't have a focused window. |
| 973 | */ |
| 974 | void InputDispatcher::processNoFocusedWindowAnrLocked() { |
| 975 | // Check if the application that we are waiting for is still focused. |
| 976 | std::shared_ptr<InputApplicationHandle> focusedApplication = |
| 977 | getValueByKey(mFocusedApplicationHandlesByDisplay, mAwaitedApplicationDisplayId); |
| 978 | if (focusedApplication == nullptr || |
| 979 | focusedApplication->getApplicationToken() != |
| 980 | mAwaitedFocusedApplication->getApplicationToken()) { |
| 981 | // Unexpected because we should have reset the ANR timer when focused application changed |
| 982 | ALOGE("Waited for a focused window, but focused application has already changed to %s", |
| 983 | focusedApplication->getName().c_str()); |
| 984 | return; // The focused application has changed. |
| 985 | } |
| 986 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 987 | const sp<WindowInfoHandle>& focusedWindowHandle = |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 988 | getFocusedWindowHandleLocked(mAwaitedApplicationDisplayId); |
| 989 | if (focusedWindowHandle != nullptr) { |
| 990 | return; // We now have a focused window. No need for ANR. |
| 991 | } |
| 992 | onAnrLocked(mAwaitedFocusedApplication); |
| 993 | } |
| 994 | |
| 995 | /** |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 996 | * Check if any of the connections' wait queues have events that are too old. |
| 997 | * If we waited for events to be ack'ed for more than the window timeout, raise an ANR. |
| 998 | * Return the time at which we should wake up next. |
| 999 | */ |
| 1000 | nsecs_t InputDispatcher::processAnrsLocked() { |
| 1001 | const nsecs_t currentTime = now(); |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 1002 | nsecs_t nextAnrCheck = LLONG_MAX; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1003 | // Check if we are waiting for a focused window to appear. Raise ANR if waited too long |
| 1004 | if (mNoFocusedWindowTimeoutTime.has_value() && mAwaitedFocusedApplication != nullptr) { |
| 1005 | if (currentTime >= *mNoFocusedWindowTimeoutTime) { |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 1006 | processNoFocusedWindowAnrLocked(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1007 | mAwaitedFocusedApplication.reset(); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 1008 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 1009 | return LLONG_MIN; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1010 | } else { |
Siarhei Vishniakou | 38a6d27 | 2020-10-20 20:29:33 -0500 | [diff] [blame] | 1011 | // Keep waiting. We will drop the event when mNoFocusedWindowTimeoutTime comes. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1012 | nextAnrCheck = *mNoFocusedWindowTimeoutTime; |
| 1013 | } |
| 1014 | } |
| 1015 | |
| 1016 | // Check if any connection ANRs are due |
| 1017 | nextAnrCheck = std::min(nextAnrCheck, mAnrTracker.firstTimeout()); |
| 1018 | if (currentTime < nextAnrCheck) { // most likely scenario |
| 1019 | return nextAnrCheck; // everything is normal. Let's check again at nextAnrCheck |
| 1020 | } |
| 1021 | |
| 1022 | // If we reached here, we have an unresponsive connection. |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 1023 | std::shared_ptr<Connection> connection = getConnectionLocked(mAnrTracker.firstToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1024 | if (connection == nullptr) { |
| 1025 | ALOGE("Could not find connection for entry %" PRId64, mAnrTracker.firstTimeout()); |
| 1026 | return nextAnrCheck; |
| 1027 | } |
| 1028 | connection->responsive = false; |
| 1029 | // Stop waking up for this unresponsive connection |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 1030 | mAnrTracker.eraseToken(connection->getToken()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 1031 | onAnrLocked(connection); |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 1032 | return LLONG_MIN; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1033 | } |
| 1034 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 1035 | std::chrono::nanoseconds InputDispatcher::getDispatchingTimeoutLocked( |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 1036 | const std::shared_ptr<Connection>& connection) { |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 1037 | if (connection->monitor) { |
| 1038 | return mMonitorDispatchingTimeout; |
| 1039 | } |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 1040 | const sp<WindowInfoHandle> window = getWindowHandleLocked(connection->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1041 | if (window != nullptr) { |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 1042 | return window->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1043 | } |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 1044 | return DEFAULT_INPUT_DISPATCHING_TIMEOUT; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1045 | } |
| 1046 | |
Siarhei Vishniakou | 6950596 | 2023-12-28 12:07:04 -0800 | [diff] [blame] | 1047 | void InputDispatcher::dispatchOnceInnerLocked(nsecs_t& nextWakeupTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1048 | nsecs_t currentTime = now(); |
| 1049 | |
Jeff Brown | dc5992e | 2014-04-11 01:27:26 -0700 | [diff] [blame] | 1050 | // Reset the key repeat timer whenever normal dispatch is suspended while the |
| 1051 | // device is in a non-interactive state. This is to ensure that we abort a key |
| 1052 | // repeat if the device is just coming out of sleep. |
| 1053 | if (!mDispatchEnabled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1054 | resetKeyRepeatLocked(); |
| 1055 | } |
| 1056 | |
| 1057 | // If dispatching is frozen, do not process timeouts or try to deliver any new events. |
| 1058 | if (mDispatchFrozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1059 | if (DEBUG_FOCUS) { |
| 1060 | ALOGD("Dispatch frozen. Waiting some more."); |
| 1061 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1062 | return; |
| 1063 | } |
| 1064 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1065 | // Ready to start a new event. |
| 1066 | // If we don't already have a pending event, go grab one. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1067 | if (!mPendingEvent) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1068 | if (mInboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1069 | // Synthesize a key repeat if appropriate. |
| 1070 | if (mKeyRepeatState.lastKeyEntry) { |
| 1071 | if (currentTime >= mKeyRepeatState.nextRepeatTime) { |
| 1072 | mPendingEvent = synthesizeKeyRepeatLocked(currentTime); |
| 1073 | } else { |
Siarhei Vishniakou | 6950596 | 2023-12-28 12:07:04 -0800 | [diff] [blame] | 1074 | nextWakeupTime = std::min(nextWakeupTime, mKeyRepeatState.nextRepeatTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1075 | } |
| 1076 | } |
| 1077 | |
| 1078 | // Nothing to do if there is no pending event. |
| 1079 | if (!mPendingEvent) { |
| 1080 | return; |
| 1081 | } |
| 1082 | } else { |
| 1083 | // Inbound queue has at least one entry. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1084 | mPendingEvent = mInboundQueue.front(); |
| 1085 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1086 | traceInboundQueueLengthLocked(); |
| 1087 | } |
| 1088 | |
| 1089 | // Poke user activity for this event. |
| 1090 | if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1091 | pokeUserActivityLocked(*mPendingEvent); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1092 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | // Now we have an event to dispatch. |
| 1096 | // 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] | 1097 | ALOG_ASSERT(mPendingEvent != nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1098 | bool done = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1099 | DropReason dropReason = DropReason::NOT_DROPPED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1100 | if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1101 | dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1102 | } else if (!mDispatchEnabled) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1103 | dropReason = DropReason::DISABLED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1104 | } |
| 1105 | |
| 1106 | if (mNextUnblockedEvent == mPendingEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1107 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1108 | } |
| 1109 | |
| 1110 | switch (mPendingEvent->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1111 | case EventEntry::Type::CONFIGURATION_CHANGED: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1112 | const ConfigurationChangedEntry& typedEntry = |
| 1113 | static_cast<const ConfigurationChangedEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1114 | done = dispatchConfigurationChangedLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1115 | dropReason = DropReason::NOT_DROPPED; // configuration changes are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1116 | break; |
| 1117 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1118 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1119 | case EventEntry::Type::DEVICE_RESET: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1120 | const DeviceResetEntry& typedEntry = |
| 1121 | static_cast<const DeviceResetEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1122 | done = dispatchDeviceResetLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1123 | dropReason = DropReason::NOT_DROPPED; // device resets are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1124 | break; |
| 1125 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1126 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1127 | case EventEntry::Type::FOCUS: { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1128 | std::shared_ptr<const FocusEntry> typedEntry = |
| 1129 | std::static_pointer_cast<const FocusEntry>(mPendingEvent); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1130 | dispatchFocusLocked(currentTime, typedEntry); |
| 1131 | done = true; |
| 1132 | dropReason = DropReason::NOT_DROPPED; // focus events are never dropped |
| 1133 | break; |
| 1134 | } |
| 1135 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1136 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1137 | const auto typedEntry = std::static_pointer_cast<const TouchModeEntry>(mPendingEvent); |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1138 | dispatchTouchModeChangeLocked(currentTime, typedEntry); |
| 1139 | done = true; |
| 1140 | dropReason = DropReason::NOT_DROPPED; // touch mode events are never dropped |
| 1141 | break; |
| 1142 | } |
| 1143 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1144 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 1145 | const auto typedEntry = |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1146 | std::static_pointer_cast<const PointerCaptureChangedEntry>(mPendingEvent); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1147 | dispatchPointerCaptureChangedLocked(currentTime, typedEntry, dropReason); |
| 1148 | done = true; |
| 1149 | break; |
| 1150 | } |
| 1151 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1152 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1153 | std::shared_ptr<const DragEntry> typedEntry = |
| 1154 | std::static_pointer_cast<const DragEntry>(mPendingEvent); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1155 | dispatchDragLocked(currentTime, typedEntry); |
| 1156 | done = true; |
| 1157 | break; |
| 1158 | } |
| 1159 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1160 | case EventEntry::Type::KEY: { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1161 | std::shared_ptr<const KeyEntry> keyEntry = |
| 1162 | std::static_pointer_cast<const KeyEntry>(mPendingEvent); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1163 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *keyEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1164 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1165 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1166 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 1167 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1168 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1169 | done = dispatchKeyLocked(currentTime, keyEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1170 | break; |
| 1171 | } |
| 1172 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1173 | case EventEntry::Type::MOTION: { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1174 | std::shared_ptr<const MotionEntry> motionEntry = |
| 1175 | std::static_pointer_cast<const MotionEntry>(mPendingEvent); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1176 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *motionEntry)) { |
Siarhei Vishniakou | 6b71b63 | 2023-10-27 21:34:46 -0700 | [diff] [blame] | 1177 | // The event is stale. However, only drop stale events if there isn't an ongoing |
| 1178 | // gesture. That would allow us to complete the processing of the current stroke. |
| 1179 | const auto touchStateIt = mTouchStatesByDisplay.find(motionEntry->displayId); |
| 1180 | if (touchStateIt != mTouchStatesByDisplay.end()) { |
| 1181 | const TouchState& touchState = touchStateIt->second; |
| 1182 | if (!touchState.hasTouchingPointers(motionEntry->deviceId) && |
| 1183 | !touchState.hasHoveringPointers(motionEntry->deviceId)) { |
| 1184 | dropReason = DropReason::STALE; |
| 1185 | } |
| 1186 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1187 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1188 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
Siarhei Vishniakou | 99e407b | 2023-12-26 18:09:32 -0800 | [diff] [blame] | 1189 | if (!isFromSource(motionEntry->source, AINPUT_SOURCE_CLASS_POINTER)) { |
| 1190 | // Only drop events that are focus-dispatched. |
| 1191 | dropReason = DropReason::BLOCKED; |
| 1192 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1193 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1194 | done = dispatchMotionLocked(currentTime, motionEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1195 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1196 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1197 | |
| 1198 | case EventEntry::Type::SENSOR: { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1199 | std::shared_ptr<const SensorEntry> sensorEntry = |
| 1200 | std::static_pointer_cast<const SensorEntry>(mPendingEvent); |
Siarhei Vishniakou | e2404a1 | 2024-01-16 18:38:39 -0800 | [diff] [blame] | 1201 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1202 | // Sensor timestamps use SYSTEM_TIME_BOOTTIME time base, so we can't use |
| 1203 | // 'currentTime' here, get SYSTEM_TIME_BOOTTIME instead. |
| 1204 | nsecs_t bootTime = systemTime(SYSTEM_TIME_BOOTTIME); |
| 1205 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(bootTime, *sensorEntry)) { |
| 1206 | dropReason = DropReason::STALE; |
| 1207 | } |
| 1208 | dispatchSensorLocked(currentTime, sensorEntry, &dropReason, nextWakeupTime); |
| 1209 | done = true; |
| 1210 | break; |
| 1211 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1212 | } |
| 1213 | |
| 1214 | if (done) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1215 | if (dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1216 | dropInboundEventLocked(*mPendingEvent, dropReason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1217 | } |
Michael Wright | 3a98172 | 2015-06-10 15:26:13 +0100 | [diff] [blame] | 1218 | mLastDropReason = dropReason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1219 | |
Prabir Pradhan | 052fb0b | 2024-02-23 21:03:12 +0000 | [diff] [blame^] | 1220 | if (mTracer) { |
| 1221 | if (auto& traceTracker = getTraceTracker(*mPendingEvent); traceTracker != nullptr) { |
| 1222 | mTracer->eventProcessingComplete(*traceTracker); |
| 1223 | } |
| 1224 | } |
| 1225 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1226 | releasePendingEventLocked(); |
Siarhei Vishniakou | 6950596 | 2023-12-28 12:07:04 -0800 | [diff] [blame] | 1227 | nextWakeupTime = LLONG_MIN; // force next poll to wake up immediately |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1228 | } |
| 1229 | } |
| 1230 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 1231 | bool InputDispatcher::isStaleEvent(nsecs_t currentTime, const EventEntry& entry) { |
Siarhei Vishniakou | a733311 | 2023-10-27 13:33:29 -0700 | [diff] [blame] | 1232 | return mPolicy.isStaleEvent(currentTime, entry.eventTime); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 1233 | } |
| 1234 | |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1235 | /** |
| 1236 | * Return true if the events preceding this incoming motion event should be dropped |
| 1237 | * Return false otherwise (the default behaviour) |
| 1238 | */ |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 1239 | bool InputDispatcher::shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) const { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1240 | const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN && |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 1241 | isFromSource(motionEntry.source, AINPUT_SOURCE_CLASS_POINTER); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1242 | |
| 1243 | // Optimize case where the current application is unresponsive and the user |
| 1244 | // decides to touch a window in a different application. |
| 1245 | // If the application takes too long to catch up then we drop all events preceding |
| 1246 | // the touch into the other window. |
| 1247 | if (isPointerDownEvent && mAwaitedFocusedApplication != nullptr) { |
Siarhei Vishniakou | 9306c38 | 2022-09-30 15:30:31 -0700 | [diff] [blame] | 1248 | const int32_t displayId = motionEntry.displayId; |
| 1249 | const auto [x, y] = resolveTouchedPosition(motionEntry); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1250 | const bool isStylus = isPointerFromStylus(motionEntry, /*pointerIndex=*/0); |
Siarhei Vishniakou | 9306c38 | 2022-09-30 15:30:31 -0700 | [diff] [blame] | 1251 | |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 1252 | sp<WindowInfoHandle> touchedWindowHandle = |
| 1253 | findTouchedWindowAtLocked(displayId, x, y, isStylus); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1254 | if (touchedWindowHandle != nullptr && |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1255 | touchedWindowHandle->getApplicationToken() != |
| 1256 | mAwaitedFocusedApplication->getApplicationToken()) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1257 | // User touched a different application than the one we are waiting on. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1258 | ALOGI("Pruning input queue because user touched a different application while waiting " |
| 1259 | "for %s", |
| 1260 | mAwaitedFocusedApplication->getName().c_str()); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1261 | return true; |
| 1262 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1263 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1264 | // Alternatively, maybe there's a spy window that could handle this event. |
| 1265 | const std::vector<sp<WindowInfoHandle>> touchedSpies = |
| 1266 | findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus); |
| 1267 | for (const auto& windowHandle : touchedSpies) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 1268 | const std::shared_ptr<Connection> connection = |
| 1269 | getConnectionLocked(windowHandle->getToken()); |
Siarhei Vishniakou | 34ed4d4 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1270 | if (connection != nullptr && connection->responsive) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1271 | // This spy window could take more input. Drop all events preceding this |
| 1272 | // 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] | 1273 | 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] | 1274 | "responsive spy window that may handle the event.", |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1275 | mAwaitedFocusedApplication->getName().c_str()); |
| 1276 | return true; |
| 1277 | } |
| 1278 | } |
| 1279 | } |
| 1280 | |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1281 | return false; |
| 1282 | } |
| 1283 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1284 | bool InputDispatcher::enqueueInboundEventLocked(std::unique_ptr<EventEntry> newEntry) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1285 | bool needWake = mInboundQueue.empty(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1286 | mInboundQueue.push_back(std::move(newEntry)); |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1287 | const EventEntry& entry = *(mInboundQueue.back()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1288 | traceInboundQueueLengthLocked(); |
| 1289 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1290 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1291 | case EventEntry::Type::KEY: { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1292 | LOG_ALWAYS_FATAL_IF((entry.policyFlags & POLICY_FLAG_TRUSTED) == 0, |
| 1293 | "Unexpected untrusted event."); |
Siarhei Vishniakou | e2404a1 | 2024-01-16 18:38:39 -0800 | [diff] [blame] | 1294 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1295 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 1296 | if (mTracer) { |
| 1297 | ensureEventTraced(keyEntry); |
| 1298 | } |
Siarhei Vishniakou | 6520a58 | 2023-10-27 21:53:45 -0700 | [diff] [blame] | 1299 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 1300 | // If a new up event comes in, and the pending event with same key code has been asked |
| 1301 | // to try again later because of the policy. We have to reset the intercept key wake up |
| 1302 | // time for it may have been handled in the policy and could be dropped. |
| 1303 | if (keyEntry.action == AKEY_EVENT_ACTION_UP && mPendingEvent && |
| 1304 | mPendingEvent->type == EventEntry::Type::KEY) { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1305 | const KeyEntry& pendingKey = static_cast<const KeyEntry&>(*mPendingEvent); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 1306 | if (pendingKey.keyCode == keyEntry.keyCode && |
| 1307 | pendingKey.interceptKeyResult == |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1308 | KeyEntry::InterceptKeyResult::TRY_AGAIN_LATER) { |
| 1309 | pendingKey.interceptKeyResult = KeyEntry::InterceptKeyResult::UNKNOWN; |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 1310 | pendingKey.interceptKeyWakeupTime = 0; |
| 1311 | needWake = true; |
| 1312 | } |
| 1313 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1314 | break; |
| 1315 | } |
| 1316 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1317 | case EventEntry::Type::MOTION: { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1318 | LOG_ALWAYS_FATAL_IF((entry.policyFlags & POLICY_FLAG_TRUSTED) == 0, |
| 1319 | "Unexpected untrusted event."); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 1320 | const auto& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1321 | if (mTracer) { |
| 1322 | ensureEventTraced(motionEntry); |
| 1323 | } |
| 1324 | if (shouldPruneInboundQueueLocked(motionEntry)) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1325 | mNextUnblockedEvent = mInboundQueue.back(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1326 | needWake = true; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1327 | } |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 1328 | |
| 1329 | const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN && |
| 1330 | isFromSource(motionEntry.source, AINPUT_SOURCE_CLASS_POINTER); |
| 1331 | if (isPointerDownEvent && mKeyIsWaitingForEventsTimeout) { |
| 1332 | // Prevent waiting too long for unprocessed events: if we have a pending key event, |
| 1333 | // and some other events have not yet been processed, the dispatcher will wait for |
| 1334 | // these events to be processed before dispatching the key event. This is because |
| 1335 | // the unprocessed events may cause the focus to change (for example, by launching a |
| 1336 | // new window or tapping a different window). To prevent waiting too long, we force |
| 1337 | // the key to be sent to the currently focused window when a new tap comes in. |
| 1338 | ALOGD("Received a new pointer down event, stop waiting for events to process and " |
| 1339 | "just send the pending key event to the currently focused window."); |
| 1340 | mKeyIsWaitingForEventsTimeout = now(); |
Linnan Li | 563916a | 2024-02-23 16:08:33 +0000 | [diff] [blame] | 1341 | needWake = true; |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 1342 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1343 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1344 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1345 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1346 | LOG_ALWAYS_FATAL("Focus events should be inserted using enqueueFocusEventLocked"); |
| 1347 | break; |
| 1348 | } |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1349 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1350 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1351 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1352 | case EventEntry::Type::SENSOR: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1353 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1354 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1355 | // nothing to do |
| 1356 | break; |
| 1357 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1358 | } |
| 1359 | |
| 1360 | return needWake; |
| 1361 | } |
| 1362 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1363 | void InputDispatcher::addRecentEventLocked(std::shared_ptr<const EventEntry> entry) { |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1364 | // Do not store sensor event in recent queue to avoid flooding the queue. |
| 1365 | if (entry->type != EventEntry::Type::SENSOR) { |
| 1366 | mRecentQueue.push_back(entry); |
| 1367 | } |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1368 | if (mRecentQueue.size() > RECENT_QUEUE_MAX_SIZE) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1369 | mRecentQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1370 | } |
| 1371 | } |
| 1372 | |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 1373 | sp<WindowInfoHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, float x, float y, |
| 1374 | bool isStylus, |
| 1375 | bool ignoreDragWindow) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1376 | // Traverse windows from front to back to find touched window. |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1377 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1378 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 1379 | if (ignoreDragWindow && haveSameToken(windowHandle, mDragState->dragWindow)) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1380 | continue; |
| 1381 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1382 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1383 | const WindowInfo& info = *windowHandle->getInfo(); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 1384 | if (!info.isSpy() && |
| 1385 | windowAcceptsTouchAt(info, displayId, x, y, isStylus, getTransformLocked(displayId))) { |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 1386 | return windowHandle; |
| 1387 | } |
| 1388 | } |
| 1389 | return nullptr; |
| 1390 | } |
| 1391 | |
| 1392 | std::vector<InputTarget> InputDispatcher::findOutsideTargetsLocked( |
Siarhei Vishniakou | 70f3d8c | 2023-09-19 15:36:52 -0700 | [diff] [blame] | 1393 | int32_t displayId, const sp<WindowInfoHandle>& touchedWindow, int32_t pointerId) const { |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 1394 | if (touchedWindow == nullptr) { |
| 1395 | return {}; |
| 1396 | } |
| 1397 | // Traverse windows from front to back until we encounter the touched window. |
| 1398 | std::vector<InputTarget> outsideTargets; |
| 1399 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
| 1400 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 1401 | if (windowHandle == touchedWindow) { |
| 1402 | // Stop iterating once we found a touched window. Any WATCH_OUTSIDE_TOUCH window |
| 1403 | // below the touched window will not get ACTION_OUTSIDE event. |
| 1404 | return outsideTargets; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1405 | } |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1406 | |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 1407 | const WindowInfo& info = *windowHandle->getInfo(); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1408 | if (info.inputConfig.test(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH)) { |
Siarhei Vishniakou | 70f3d8c | 2023-09-19 15:36:52 -0700 | [diff] [blame] | 1409 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
| 1410 | pointerIds.set(pointerId); |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 1411 | addPointerWindowTargetLocked(windowHandle, InputTarget::DispatchMode::OUTSIDE, |
| 1412 | ftl::Flags<InputTarget::Flags>(), pointerIds, |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 1413 | /*firstDownTimeInTarget=*/std::nullopt, outsideTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1414 | } |
| 1415 | } |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 1416 | return outsideTargets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1417 | } |
| 1418 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1419 | std::vector<sp<WindowInfoHandle>> InputDispatcher::findTouchedSpyWindowsAtLocked( |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 1420 | int32_t displayId, float x, float y, bool isStylus) const { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1421 | // Traverse windows from front to back and gather the touched spy windows. |
| 1422 | std::vector<sp<WindowInfoHandle>> spyWindows; |
| 1423 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
| 1424 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 1425 | const WindowInfo& info = *windowHandle->getInfo(); |
| 1426 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 1427 | if (!windowAcceptsTouchAt(info, displayId, x, y, isStylus, getTransformLocked(displayId))) { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1428 | continue; |
| 1429 | } |
| 1430 | if (!info.isSpy()) { |
| 1431 | // The first touched non-spy window was found, so return the spy windows touched so far. |
| 1432 | return spyWindows; |
| 1433 | } |
| 1434 | spyWindows.push_back(windowHandle); |
| 1435 | } |
| 1436 | return spyWindows; |
| 1437 | } |
| 1438 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1439 | void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1440 | const char* reason; |
| 1441 | switch (dropReason) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1442 | case DropReason::POLICY: |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 1443 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1444 | ALOGD("Dropped event because policy consumed it."); |
| 1445 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1446 | reason = "inbound event was dropped because the policy consumed it"; |
| 1447 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1448 | case DropReason::DISABLED: |
| 1449 | if (mLastDropReason != DropReason::DISABLED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1450 | ALOGI("Dropped event because input dispatch is disabled."); |
| 1451 | } |
| 1452 | reason = "inbound event was dropped because input dispatch is disabled"; |
| 1453 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1454 | case DropReason::BLOCKED: |
Siarhei Vishniakou | 99e407b | 2023-12-26 18:09:32 -0800 | [diff] [blame] | 1455 | LOG(INFO) << "Dropping because the current application is not responding and the user " |
| 1456 | "has started interacting with a different application: " |
| 1457 | << entry.getDescription(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1458 | reason = "inbound event was dropped because the current application is not responding " |
| 1459 | "and the user has started interacting with a different application"; |
| 1460 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1461 | case DropReason::STALE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1462 | ALOGI("Dropped event because it is stale."); |
| 1463 | reason = "inbound event was dropped because it is stale"; |
| 1464 | break; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1465 | case DropReason::NO_POINTER_CAPTURE: |
| 1466 | ALOGI("Dropped event because there is no window with Pointer Capture."); |
| 1467 | reason = "inbound event was dropped because there is no window with Pointer Capture"; |
| 1468 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1469 | case DropReason::NOT_DROPPED: { |
| 1470 | LOG_ALWAYS_FATAL("Should not be dropping a NOT_DROPPED event"); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1471 | return; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1472 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1473 | } |
| 1474 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1475 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1476 | case EventEntry::Type::KEY: { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1477 | CancelationOptions options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, reason); |
Hu Guo | 3cfa738 | 2023-11-15 09:50:04 +0000 | [diff] [blame] | 1478 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 1479 | options.displayId = keyEntry.displayId; |
| 1480 | options.deviceId = keyEntry.deviceId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1481 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1482 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1483 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1484 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1485 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1486 | if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1487 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, reason); |
Hu Guo | 3cfa738 | 2023-11-15 09:50:04 +0000 | [diff] [blame] | 1488 | options.displayId = motionEntry.displayId; |
| 1489 | options.deviceId = motionEntry.deviceId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1490 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1491 | } else { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1492 | CancelationOptions options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, |
| 1493 | reason); |
Hu Guo | 3cfa738 | 2023-11-15 09:50:04 +0000 | [diff] [blame] | 1494 | options.displayId = motionEntry.displayId; |
| 1495 | options.deviceId = motionEntry.deviceId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1496 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1497 | } |
| 1498 | break; |
| 1499 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1500 | case EventEntry::Type::SENSOR: { |
| 1501 | break; |
| 1502 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1503 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1504 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1505 | break; |
| 1506 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1507 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1508 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1509 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 1510 | case EventEntry::Type::DEVICE_RESET: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1511 | 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] | 1512 | break; |
| 1513 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1514 | } |
| 1515 | } |
| 1516 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1517 | bool InputDispatcher::haveCommandsLocked() const { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1518 | return !mCommandQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1519 | } |
| 1520 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1521 | bool InputDispatcher::runCommandsLockedInterruptable() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1522 | if (mCommandQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1523 | return false; |
| 1524 | } |
| 1525 | |
| 1526 | do { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1527 | auto command = std::move(mCommandQueue.front()); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1528 | mCommandQueue.pop_front(); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1529 | // Commands are run with the lock held, but may release and re-acquire the lock from within. |
| 1530 | command(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1531 | } while (!mCommandQueue.empty()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1532 | return true; |
| 1533 | } |
| 1534 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1535 | void InputDispatcher::postCommandLocked(Command&& command) { |
| 1536 | mCommandQueue.push_back(command); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1537 | } |
| 1538 | |
| 1539 | void InputDispatcher::drainInboundQueueLocked() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1540 | while (!mInboundQueue.empty()) { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1541 | std::shared_ptr<const EventEntry> entry = mInboundQueue.front(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1542 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1543 | releaseInboundEventLocked(entry); |
| 1544 | } |
| 1545 | traceInboundQueueLengthLocked(); |
| 1546 | } |
| 1547 | |
| 1548 | void InputDispatcher::releasePendingEventLocked() { |
| 1549 | if (mPendingEvent) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1550 | releaseInboundEventLocked(mPendingEvent); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1551 | mPendingEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1552 | } |
| 1553 | } |
| 1554 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1555 | void InputDispatcher::releaseInboundEventLocked(std::shared_ptr<const EventEntry> entry) { |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 1556 | const std::shared_ptr<InjectionState>& injectionState = entry->injectionState; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1557 | if (injectionState && injectionState->injectionResult == InputEventInjectionResult::PENDING) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1558 | if (DEBUG_DISPATCH_CYCLE) { |
| 1559 | ALOGD("Injected inbound event was dropped."); |
| 1560 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1561 | setInjectionResult(*entry, InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1562 | } |
| 1563 | if (entry == mNextUnblockedEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1564 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1565 | } |
| 1566 | addRecentEventLocked(entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1567 | } |
| 1568 | |
| 1569 | void InputDispatcher::resetKeyRepeatLocked() { |
| 1570 | if (mKeyRepeatState.lastKeyEntry) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1571 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1572 | } |
| 1573 | } |
| 1574 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1575 | std::shared_ptr<KeyEntry> InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1576 | std::shared_ptr<const KeyEntry> entry = mKeyRepeatState.lastKeyEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1577 | |
Michael Wright | 2e73295 | 2014-09-24 13:26:59 -0700 | [diff] [blame] | 1578 | uint32_t policyFlags = entry->policyFlags & |
| 1579 | (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1580 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1581 | std::shared_ptr<KeyEntry> newEntry = |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 1582 | std::make_unique<KeyEntry>(mIdGenerator.nextId(), /*injectionState=*/nullptr, |
| 1583 | currentTime, entry->deviceId, entry->source, |
| 1584 | entry->displayId, policyFlags, entry->action, entry->flags, |
| 1585 | entry->keyCode, entry->scanCode, entry->metaState, |
| 1586 | entry->repeatCount + 1, entry->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1587 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1588 | newEntry->syntheticRepeat = true; |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 1589 | if (mTracer) { |
| 1590 | newEntry->traceTracker = mTracer->traceInboundEvent(*newEntry); |
| 1591 | } |
| 1592 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1593 | mKeyRepeatState.lastKeyEntry = newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1594 | mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1595 | return newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1596 | } |
| 1597 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1598 | bool InputDispatcher::dispatchConfigurationChangedLocked(nsecs_t currentTime, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1599 | const ConfigurationChangedEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1600 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1601 | ALOGD("dispatchConfigurationChanged - eventTime=%" PRId64, entry.eventTime); |
| 1602 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1603 | |
| 1604 | // Reset key repeating in case a keyboard device was added or removed or something. |
| 1605 | resetKeyRepeatLocked(); |
| 1606 | |
| 1607 | // 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] | 1608 | auto command = [this, eventTime = entry.eventTime]() REQUIRES(mLock) { |
| 1609 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 1610 | mPolicy.notifyConfigurationChanged(eventTime); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1611 | }; |
| 1612 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1613 | return true; |
| 1614 | } |
| 1615 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1616 | bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime, |
| 1617 | const DeviceResetEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1618 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1619 | ALOGD("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry.eventTime, |
| 1620 | entry.deviceId); |
| 1621 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1622 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 1623 | // Reset key repeating in case a keyboard device was disabled or enabled. |
| 1624 | if (mKeyRepeatState.lastKeyEntry && mKeyRepeatState.lastKeyEntry->deviceId == entry.deviceId) { |
| 1625 | resetKeyRepeatLocked(); |
| 1626 | } |
| 1627 | |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1628 | CancelationOptions options(CancelationOptions::Mode::CANCEL_ALL_EVENTS, "device was reset"); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1629 | options.deviceId = entry.deviceId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1630 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 1631 | |
| 1632 | // Remove all active pointers from this device |
| 1633 | for (auto& [_, touchState] : mTouchStatesByDisplay) { |
| 1634 | touchState.removeAllPointersForDevice(entry.deviceId); |
| 1635 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1636 | return true; |
| 1637 | } |
| 1638 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1639 | void InputDispatcher::enqueueFocusEventLocked(const sp<IBinder>& windowToken, bool hasFocus, |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1640 | const std::string& reason) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1641 | if (mPendingEvent != nullptr) { |
| 1642 | // Move the pending event to the front of the queue. This will give the chance |
| 1643 | // for the pending event to get dispatched to the newly focused window |
| 1644 | mInboundQueue.push_front(mPendingEvent); |
| 1645 | mPendingEvent = nullptr; |
| 1646 | } |
| 1647 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1648 | std::unique_ptr<FocusEntry> focusEntry = |
| 1649 | std::make_unique<FocusEntry>(mIdGenerator.nextId(), now(), windowToken, hasFocus, |
| 1650 | reason); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1651 | |
| 1652 | // This event should go to the front of the queue, but behind all other focus events |
| 1653 | // Find the last focus event, and insert right after it |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1654 | auto it = std::find_if(mInboundQueue.rbegin(), mInboundQueue.rend(), |
| 1655 | [](const std::shared_ptr<const EventEntry>& event) { |
| 1656 | return event->type == EventEntry::Type::FOCUS; |
| 1657 | }); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1658 | |
| 1659 | // 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] | 1660 | mInboundQueue.insert(it.base(), std::move(focusEntry)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1661 | } |
| 1662 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1663 | void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime, |
| 1664 | std::shared_ptr<const FocusEntry> entry) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 1665 | std::shared_ptr<Connection> connection = getConnectionLocked(entry->connectionToken); |
| 1666 | if (connection == nullptr) { |
| 1667 | return; // Connection has gone away |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1668 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1669 | entry->dispatchInProgress = true; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1670 | std::string message = std::string("Focus ") + (entry->hasFocus ? "entering " : "leaving ") + |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 1671 | connection->getInputChannelName(); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 1672 | std::string reason = std::string("reason=").append(entry->reason); |
| 1673 | android_log_event_list(LOGTAG_INPUT_FOCUS) << message << reason << LOG_ID_EVENTS; |
Prabir Pradhan | c32a411 | 2024-01-23 23:20:37 +0000 | [diff] [blame] | 1674 | dispatchEventLocked(currentTime, entry, {{connection}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1675 | } |
| 1676 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1677 | void InputDispatcher::dispatchPointerCaptureChangedLocked( |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1678 | nsecs_t currentTime, const std::shared_ptr<const PointerCaptureChangedEntry>& entry, |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1679 | DropReason& dropReason) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1680 | dropReason = DropReason::NOT_DROPPED; |
| 1681 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1682 | const bool haveWindowWithPointerCapture = mWindowTokenWithPointerCapture != nullptr; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1683 | sp<IBinder> token; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1684 | |
| 1685 | if (entry->pointerCaptureRequest.enable) { |
| 1686 | // Enable Pointer Capture. |
| 1687 | if (haveWindowWithPointerCapture && |
| 1688 | (entry->pointerCaptureRequest == mCurrentPointerCaptureRequest)) { |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 1689 | // This can happen if pointer capture is disabled and re-enabled before we notify the |
| 1690 | // app of the state change, so there is no need to notify the app. |
| 1691 | ALOGI("Skipping dispatch of Pointer Capture being enabled: no state change."); |
| 1692 | return; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1693 | } |
| 1694 | if (!mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1695 | // This can happen if a window requests capture and immediately releases capture. |
| 1696 | ALOGW("No window requested Pointer Capture."); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1697 | dropReason = DropReason::NO_POINTER_CAPTURE; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1698 | return; |
| 1699 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1700 | if (entry->pointerCaptureRequest.seq != mCurrentPointerCaptureRequest.seq) { |
| 1701 | ALOGI("Skipping dispatch of Pointer Capture being enabled: sequence number mismatch."); |
| 1702 | return; |
| 1703 | } |
| 1704 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1705 | token = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1706 | LOG_ALWAYS_FATAL_IF(!token, "Cannot find focused window for Pointer Capture."); |
| 1707 | mWindowTokenWithPointerCapture = token; |
| 1708 | } else { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1709 | // Disable Pointer Capture. |
| 1710 | // We do not check if the sequence number matches for requests to disable Pointer Capture |
| 1711 | // for two reasons: |
| 1712 | // 1. Pointer Capture can be disabled by a focus change, which means we can get two entries |
| 1713 | // to disable capture with the same sequence number: one generated by |
| 1714 | // disablePointerCaptureForcedLocked() and another as an acknowledgement of Pointer |
| 1715 | // Capture being disabled in InputReader. |
| 1716 | // 2. We respect any request to disable Pointer Capture generated by InputReader, since the |
| 1717 | // actual Pointer Capture state that affects events being generated by input devices is |
| 1718 | // in InputReader. |
| 1719 | if (!haveWindowWithPointerCapture) { |
| 1720 | // Pointer capture was already forcefully disabled because of focus change. |
| 1721 | dropReason = DropReason::NOT_DROPPED; |
| 1722 | return; |
| 1723 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1724 | token = mWindowTokenWithPointerCapture; |
| 1725 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1726 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1727 | setPointerCaptureLocked(false); |
| 1728 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1729 | } |
| 1730 | |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 1731 | auto connection = getConnectionLocked(token); |
| 1732 | if (connection == nullptr) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1733 | // Window has gone away, clean up Pointer Capture state. |
| 1734 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1735 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1736 | setPointerCaptureLocked(false); |
| 1737 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1738 | return; |
| 1739 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1740 | entry->dispatchInProgress = true; |
Prabir Pradhan | c32a411 | 2024-01-23 23:20:37 +0000 | [diff] [blame] | 1741 | dispatchEventLocked(currentTime, entry, {{connection}}); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1742 | |
| 1743 | dropReason = DropReason::NOT_DROPPED; |
| 1744 | } |
| 1745 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1746 | void InputDispatcher::dispatchTouchModeChangeLocked( |
| 1747 | nsecs_t currentTime, const std::shared_ptr<const TouchModeEntry>& entry) { |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1748 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 1749 | getWindowHandlesLocked(entry->displayId); |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1750 | if (windowHandles.empty()) { |
| 1751 | return; |
| 1752 | } |
| 1753 | const std::vector<InputTarget> inputTargets = |
| 1754 | getInputTargetsFromWindowHandlesLocked(windowHandles); |
| 1755 | if (inputTargets.empty()) { |
| 1756 | return; |
| 1757 | } |
| 1758 | entry->dispatchInProgress = true; |
| 1759 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1760 | } |
| 1761 | |
| 1762 | std::vector<InputTarget> InputDispatcher::getInputTargetsFromWindowHandlesLocked( |
| 1763 | const std::vector<sp<WindowInfoHandle>>& windowHandles) const { |
| 1764 | std::vector<InputTarget> inputTargets; |
| 1765 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1766 | const sp<IBinder>& token = handle->getToken(); |
| 1767 | if (token == nullptr) { |
| 1768 | continue; |
| 1769 | } |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 1770 | std::shared_ptr<Connection> connection = getConnectionLocked(token); |
| 1771 | if (connection == nullptr) { |
| 1772 | continue; // Connection has gone away |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1773 | } |
Prabir Pradhan | c32a411 | 2024-01-23 23:20:37 +0000 | [diff] [blame] | 1774 | inputTargets.emplace_back(connection); |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1775 | } |
| 1776 | return inputTargets; |
| 1777 | } |
| 1778 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1779 | bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, std::shared_ptr<const KeyEntry> entry, |
Siarhei Vishniakou | 6950596 | 2023-12-28 12:07:04 -0800 | [diff] [blame] | 1780 | DropReason* dropReason, nsecs_t& nextWakeupTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1781 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1782 | if (!entry->dispatchInProgress) { |
| 1783 | if (entry->repeatCount == 0 && entry->action == AKEY_EVENT_ACTION_DOWN && |
| 1784 | (entry->policyFlags & POLICY_FLAG_TRUSTED) && |
| 1785 | (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { |
| 1786 | if (mKeyRepeatState.lastKeyEntry && |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1787 | mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode && |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1788 | // We have seen two identical key downs in a row which indicates that the device |
| 1789 | // driver is automatically generating key repeats itself. We take note of the |
| 1790 | // repeat here, but we disable our own next key repeat timer since it is clear that |
| 1791 | // we will not need to synthesize key repeats ourselves. |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1792 | mKeyRepeatState.lastKeyEntry->deviceId == entry->deviceId) { |
| 1793 | // Make sure we don't get key down from a different device. If a different |
| 1794 | // device Id has same key pressed down, the new device Id will replace the |
| 1795 | // current one to hold the key repeat with repeat count reset. |
| 1796 | // In the future when got a KEY_UP on the device id, drop it and do not |
| 1797 | // stop the key repeat on current device. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1798 | entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; |
| 1799 | resetKeyRepeatLocked(); |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 1800 | mKeyRepeatState.nextRepeatTime = LLONG_MAX; // don't generate repeats ourselves |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1801 | } else { |
| 1802 | // Not a repeat. Save key down state in case we do see a repeat later. |
| 1803 | resetKeyRepeatLocked(); |
| 1804 | mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; |
| 1805 | } |
| 1806 | mKeyRepeatState.lastKeyEntry = entry; |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1807 | } else if (entry->action == AKEY_EVENT_ACTION_UP && mKeyRepeatState.lastKeyEntry && |
| 1808 | mKeyRepeatState.lastKeyEntry->deviceId != entry->deviceId) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1809 | // 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] | 1810 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1811 | ALOGD("deviceId=%d got KEY_UP as stale", entry->deviceId); |
| 1812 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1813 | } else if (!entry->syntheticRepeat) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1814 | resetKeyRepeatLocked(); |
| 1815 | } |
| 1816 | |
| 1817 | if (entry->repeatCount == 1) { |
| 1818 | entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; |
| 1819 | } else { |
| 1820 | entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; |
| 1821 | } |
| 1822 | |
| 1823 | entry->dispatchInProgress = true; |
| 1824 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1825 | logOutboundKeyDetails("dispatchKey - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1826 | } |
| 1827 | |
| 1828 | // 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] | 1829 | if (entry->interceptKeyResult == KeyEntry::InterceptKeyResult::TRY_AGAIN_LATER) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1830 | if (currentTime < entry->interceptKeyWakeupTime) { |
Siarhei Vishniakou | 6950596 | 2023-12-28 12:07:04 -0800 | [diff] [blame] | 1831 | nextWakeupTime = std::min(nextWakeupTime, entry->interceptKeyWakeupTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1832 | return false; // wait until next wakeup |
| 1833 | } |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1834 | entry->interceptKeyResult = KeyEntry::InterceptKeyResult::UNKNOWN; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1835 | entry->interceptKeyWakeupTime = 0; |
| 1836 | } |
| 1837 | |
| 1838 | // Give the policy a chance to intercept the key. |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1839 | if (entry->interceptKeyResult == KeyEntry::InterceptKeyResult::UNKNOWN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1840 | if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1841 | sp<IBinder> focusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1842 | mFocusResolver.getFocusedWindowToken(getTargetDisplayId(*entry)); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1843 | |
| 1844 | auto command = [this, focusedWindowToken, entry]() REQUIRES(mLock) { |
| 1845 | doInterceptKeyBeforeDispatchingCommand(focusedWindowToken, *entry); |
| 1846 | }; |
| 1847 | postCommandLocked(std::move(command)); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1848 | // Poke user activity for keys not passed to user |
| 1849 | pokeUserActivityLocked(*entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1850 | return false; // wait for the command to run |
| 1851 | } else { |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1852 | entry->interceptKeyResult = KeyEntry::InterceptKeyResult::CONTINUE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1853 | } |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1854 | } else if (entry->interceptKeyResult == KeyEntry::InterceptKeyResult::SKIP) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1855 | if (*dropReason == DropReason::NOT_DROPPED) { |
| 1856 | *dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1857 | } |
| 1858 | } |
| 1859 | |
| 1860 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1861 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1862 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1863 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1864 | : InputEventInjectionResult::FAILED); |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1865 | mReporter->reportDroppedKey(entry->id); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1866 | // Poke user activity for undispatched keys |
| 1867 | pokeUserActivityLocked(*entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1868 | return true; |
| 1869 | } |
| 1870 | |
| 1871 | // Identify targets. |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1872 | InputEventInjectionResult injectionResult; |
| 1873 | sp<WindowInfoHandle> focusedWindow = |
| 1874 | findFocusedWindowTargetLocked(currentTime, *entry, nextWakeupTime, |
| 1875 | /*byref*/ injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1876 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1877 | return false; |
| 1878 | } |
| 1879 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1880 | setInjectionResult(*entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1881 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1882 | return true; |
| 1883 | } |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1884 | LOG_ALWAYS_FATAL_IF(focusedWindow == nullptr); |
| 1885 | |
| 1886 | std::vector<InputTarget> inputTargets; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 1887 | addWindowTargetLocked(focusedWindow, InputTarget::DispatchMode::AS_IS, |
| 1888 | InputTarget::Flags::FOREGROUND, getDownTime(*entry), inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1889 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1890 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1891 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1892 | |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 1893 | if (mTracer) { |
| 1894 | ensureEventTraced(*entry); |
| 1895 | for (const auto& target : inputTargets) { |
| 1896 | mTracer->dispatchToTargetHint(*entry->traceTracker, target); |
| 1897 | } |
| 1898 | } |
| 1899 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1900 | // Dispatch the key. |
| 1901 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1902 | return true; |
| 1903 | } |
| 1904 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1905 | void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1906 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1907 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 ", " |
| 1908 | "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, " |
| 1909 | "metaState=0x%x, repeatCount=%d, downTime=%" PRId64, |
| 1910 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, |
| 1911 | entry.policyFlags, entry.action, entry.flags, entry.keyCode, entry.scanCode, |
| 1912 | entry.metaState, entry.repeatCount, entry.downTime); |
| 1913 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1914 | } |
| 1915 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1916 | void InputDispatcher::dispatchSensorLocked(nsecs_t currentTime, |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1917 | const std::shared_ptr<const SensorEntry>& entry, |
Siarhei Vishniakou | 6950596 | 2023-12-28 12:07:04 -0800 | [diff] [blame] | 1918 | DropReason* dropReason, nsecs_t& nextWakeupTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1919 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1920 | ALOGD("notifySensorEvent eventTime=%" PRId64 ", hwTimestamp=%" PRId64 ", deviceId=%d, " |
| 1921 | "source=0x%x, sensorType=%s", |
| 1922 | entry->eventTime, entry->hwTimestamp, entry->deviceId, entry->source, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1923 | ftl::enum_string(entry->sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1924 | } |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1925 | auto command = [this, entry]() REQUIRES(mLock) { |
| 1926 | scoped_unlock unlock(mLock); |
| 1927 | |
| 1928 | if (entry->accuracyChanged) { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 1929 | mPolicy.notifySensorAccuracy(entry->deviceId, entry->sensorType, entry->accuracy); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1930 | } |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 1931 | mPolicy.notifySensorEvent(entry->deviceId, entry->sensorType, entry->accuracy, |
| 1932 | entry->hwTimestamp, entry->values); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1933 | }; |
| 1934 | postCommandLocked(std::move(command)); |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1935 | } |
| 1936 | |
| 1937 | bool InputDispatcher::flushSensor(int deviceId, InputDeviceSensorType sensorType) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1938 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1939 | ALOGD("flushSensor deviceId=%d, sensorType=%s", deviceId, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1940 | ftl::enum_string(sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1941 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1942 | { // acquire lock |
| 1943 | std::scoped_lock _l(mLock); |
| 1944 | |
| 1945 | for (auto it = mInboundQueue.begin(); it != mInboundQueue.end(); it++) { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1946 | std::shared_ptr<const EventEntry> entry = *it; |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1947 | if (entry->type == EventEntry::Type::SENSOR) { |
| 1948 | it = mInboundQueue.erase(it); |
| 1949 | releaseInboundEventLocked(entry); |
| 1950 | } |
| 1951 | } |
| 1952 | } |
| 1953 | return true; |
| 1954 | } |
| 1955 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1956 | bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, |
| 1957 | std::shared_ptr<const MotionEntry> entry, |
Siarhei Vishniakou | 6950596 | 2023-12-28 12:07:04 -0800 | [diff] [blame] | 1958 | DropReason* dropReason, nsecs_t& nextWakeupTime) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1959 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1960 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1961 | if (!entry->dispatchInProgress) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1962 | entry->dispatchInProgress = true; |
| 1963 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1964 | logOutboundMotionDetails("dispatchMotion - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1965 | } |
| 1966 | |
| 1967 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1968 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1969 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1970 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1971 | : InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1972 | return true; |
| 1973 | } |
| 1974 | |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 1975 | const bool isPointerEvent = isFromSource(entry->source, AINPUT_SOURCE_CLASS_POINTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1976 | |
| 1977 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1978 | std::vector<InputTarget> inputTargets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1979 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1980 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1981 | if (isPointerEvent) { |
| 1982 | // Pointer event. (eg. touchscreen) |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 1983 | |
| 1984 | if (mDragState && |
| 1985 | (entry->action & AMOTION_EVENT_ACTION_MASK) == AMOTION_EVENT_ACTION_POINTER_DOWN) { |
| 1986 | // If drag and drop ongoing and pointer down occur: pilfer drag window pointers |
| 1987 | pilferPointersLocked(mDragState->dragWindow->getToken()); |
| 1988 | } |
| 1989 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1990 | inputTargets = |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 1991 | findTouchedWindowTargetsLocked(currentTime, *entry, /*byref*/ injectionResult); |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 1992 | LOG_ALWAYS_FATAL_IF(injectionResult != InputEventInjectionResult::SUCCEEDED && |
| 1993 | !inputTargets.empty()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1994 | } else { |
| 1995 | // Non touch event. (eg. trackball) |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1996 | sp<WindowInfoHandle> focusedWindow = |
| 1997 | findFocusedWindowTargetLocked(currentTime, *entry, nextWakeupTime, injectionResult); |
| 1998 | if (injectionResult == InputEventInjectionResult::SUCCEEDED) { |
| 1999 | LOG_ALWAYS_FATAL_IF(focusedWindow == nullptr); |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2000 | addWindowTargetLocked(focusedWindow, InputTarget::DispatchMode::AS_IS, |
| 2001 | InputTarget::Flags::FOREGROUND, getDownTime(*entry), |
| 2002 | inputTargets); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2003 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2004 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2005 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2006 | return false; |
| 2007 | } |
| 2008 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2009 | setInjectionResult(*entry, injectionResult); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2010 | if (injectionResult == InputEventInjectionResult::TARGET_MISMATCH) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2011 | return true; |
| 2012 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2013 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 2014 | CancelationOptions::Mode mode( |
| 2015 | isPointerEvent ? CancelationOptions::Mode::CANCEL_POINTER_EVENTS |
| 2016 | : CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS); |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2017 | CancelationOptions options(mode, "input event injection failed"); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 2018 | options.displayId = entry->displayId; |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2019 | synthesizeCancelationEventsForMonitorsLocked(options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2020 | return true; |
| 2021 | } |
| 2022 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2023 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2024 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2025 | |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 2026 | if (mTracer) { |
| 2027 | ensureEventTraced(*entry); |
| 2028 | for (const auto& target : inputTargets) { |
| 2029 | mTracer->dispatchToTargetHint(*entry->traceTracker, target); |
| 2030 | } |
| 2031 | } |
| 2032 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2033 | // Dispatch the motion. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2034 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 2035 | return true; |
| 2036 | } |
| 2037 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2038 | void InputDispatcher::enqueueDragEventLocked(const sp<WindowInfoHandle>& windowHandle, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2039 | bool isExiting, const int32_t rawX, |
| 2040 | const int32_t rawY) { |
| 2041 | const vec2 xy = windowHandle->getInfo()->transform.transform(vec2(rawX, rawY)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2042 | std::unique_ptr<DragEntry> dragEntry = |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2043 | std::make_unique<DragEntry>(mIdGenerator.nextId(), now(), windowHandle->getToken(), |
| 2044 | isExiting, xy.x, xy.y); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2045 | |
| 2046 | enqueueInboundEventLocked(std::move(dragEntry)); |
| 2047 | } |
| 2048 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 2049 | void InputDispatcher::dispatchDragLocked(nsecs_t currentTime, |
| 2050 | std::shared_ptr<const DragEntry> entry) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 2051 | std::shared_ptr<Connection> connection = getConnectionLocked(entry->connectionToken); |
| 2052 | if (connection == nullptr) { |
| 2053 | return; // Connection has gone away |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2054 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2055 | entry->dispatchInProgress = true; |
Prabir Pradhan | c32a411 | 2024-01-23 23:20:37 +0000 | [diff] [blame] | 2056 | dispatchEventLocked(currentTime, entry, {{connection}}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2057 | } |
| 2058 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2059 | void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2060 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 2061 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=%s, displayId=%" PRId32 |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2062 | ", policyFlags=0x%x, " |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2063 | "action=%s, actionButton=0x%x, flags=0x%x, " |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2064 | "metaState=0x%x, buttonState=0x%x," |
| 2065 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64, |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 2066 | prefix, entry.eventTime, entry.deviceId, |
| 2067 | inputEventSourceToString(entry.source).c_str(), entry.displayId, entry.policyFlags, |
| 2068 | MotionEvent::actionToString(entry.action).c_str(), entry.actionButton, entry.flags, |
| 2069 | entry.metaState, entry.buttonState, entry.edgeFlags, entry.xPrecision, |
| 2070 | entry.yPrecision, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2071 | |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 2072 | for (uint32_t i = 0; i < entry.getPointerCount(); i++) { |
Siarhei Vishniakou | 09a8fe4 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2073 | ALOGD(" Pointer %d: id=%d, toolType=%s, " |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2074 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 2075 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 2076 | "orientation=%f", |
Siarhei Vishniakou | 09a8fe4 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2077 | i, entry.pointerProperties[i].id, |
| 2078 | ftl::enum_string(entry.pointerProperties[i].toolType).c_str(), |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2079 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 2080 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 2081 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 2082 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 2083 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 2084 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 2085 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 2086 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 2087 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| 2088 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2089 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2090 | } |
| 2091 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2092 | void InputDispatcher::dispatchEventLocked(nsecs_t currentTime, |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 2093 | std::shared_ptr<const EventEntry> eventEntry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2094 | const std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2095 | ATRACE_CALL(); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2096 | if (DEBUG_DISPATCH_CYCLE) { |
| 2097 | ALOGD("dispatchEventToCurrentInputTargets"); |
| 2098 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2099 | |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 2100 | processInteractionsLocked(*eventEntry, inputTargets); |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 2101 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2102 | ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true |
| 2103 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2104 | pokeUserActivityLocked(*eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2105 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2106 | for (const InputTarget& inputTarget : inputTargets) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 2107 | std::shared_ptr<Connection> connection = inputTarget.connection; |
| 2108 | prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2109 | } |
| 2110 | } |
| 2111 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 2112 | void InputDispatcher::cancelEventsForAnrLocked(const std::shared_ptr<Connection>& connection) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2113 | // We will not be breaking any connections here, even if the policy wants us to abort dispatch. |
| 2114 | // If the policy decides to close the app, we will get a channel removal event via |
| 2115 | // unregisterInputChannel, and will clean up the connection that way. We are already not |
| 2116 | // sending new pointers to the connection when it blocked, but focused events will continue to |
| 2117 | // pile up. |
| 2118 | ALOGW("Canceling events for %s because it is unresponsive", |
Siarhei Vishniakou | 10bfdc9 | 2024-02-01 11:12:57 -0800 | [diff] [blame] | 2119 | connection->getInputChannelName().c_str()); |
Prabir Pradhan | fc36472 | 2024-02-08 17:51:20 +0000 | [diff] [blame] | 2120 | if (connection->status != Connection::Status::NORMAL) { |
| 2121 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2122 | } |
Prabir Pradhan | fc36472 | 2024-02-08 17:51:20 +0000 | [diff] [blame] | 2123 | CancelationOptions options(CancelationOptions::Mode::CANCEL_ALL_EVENTS, |
| 2124 | "application not responding"); |
| 2125 | |
| 2126 | sp<WindowInfoHandle> windowHandle; |
| 2127 | if (!connection->monitor) { |
| 2128 | windowHandle = getWindowHandleLocked(connection->getToken()); |
| 2129 | if (windowHandle == nullptr) { |
| 2130 | // The window that is receiving this ANR was removed, so there is no need to generate |
| 2131 | // cancellations, because the cancellations would have already been generated when |
| 2132 | // the window was removed. |
| 2133 | return; |
| 2134 | } |
| 2135 | } |
| 2136 | synthesizeCancelationEventsForConnectionLocked(connection, options, windowHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2137 | } |
| 2138 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2139 | void InputDispatcher::resetNoFocusedWindowTimeoutLocked() { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2140 | if (DEBUG_FOCUS) { |
| 2141 | ALOGD("Resetting ANR timeouts."); |
| 2142 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2143 | |
| 2144 | // Reset input target wait timeout. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2145 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2146 | mAwaitedFocusedApplication.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2147 | } |
| 2148 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2149 | /** |
| 2150 | * Get the display id that the given event should go to. If this event specifies a valid display id, |
| 2151 | * then it should be dispatched to that display. Otherwise, the event goes to the focused display. |
| 2152 | * Focused display is the display that the user most recently interacted with. |
| 2153 | */ |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2154 | int32_t InputDispatcher::getTargetDisplayId(const EventEntry& entry) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2155 | int32_t displayId; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2156 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2157 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2158 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 2159 | displayId = keyEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2160 | break; |
| 2161 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2162 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2163 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 2164 | displayId = motionEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2165 | break; |
| 2166 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 2167 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 2168 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2169 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2170 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2171 | case EventEntry::Type::DEVICE_RESET: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2172 | case EventEntry::Type::SENSOR: |
| 2173 | case EventEntry::Type::DRAG: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2174 | 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] | 2175 | return ADISPLAY_ID_NONE; |
| 2176 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2177 | } |
| 2178 | return displayId == ADISPLAY_ID_NONE ? mFocusedDisplayId : displayId; |
| 2179 | } |
| 2180 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2181 | bool InputDispatcher::shouldWaitToSendKeyLocked(nsecs_t currentTime, |
| 2182 | const char* focusedWindowName) { |
| 2183 | if (mAnrTracker.empty()) { |
| 2184 | // already processed all events that we waited for |
| 2185 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 2186 | return false; |
| 2187 | } |
| 2188 | |
| 2189 | if (!mKeyIsWaitingForEventsTimeout.has_value()) { |
| 2190 | // Start the timer |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 2191 | // 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] | 2192 | mKeyIsWaitingForEventsTimeout = currentTime + |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 2193 | std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 2194 | mPolicy.getKeyWaitingForEventsTimeout()) |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 2195 | .count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2196 | return true; |
| 2197 | } |
| 2198 | |
| 2199 | // We still have pending events, and already started the timer |
| 2200 | if (currentTime < *mKeyIsWaitingForEventsTimeout) { |
| 2201 | return true; // Still waiting |
| 2202 | } |
| 2203 | |
| 2204 | // Waited too long, and some connection still hasn't processed all motions |
| 2205 | // Just send the key to the focused window |
| 2206 | ALOGW("Dispatching key to %s even though there are other unprocessed events", |
| 2207 | focusedWindowName); |
| 2208 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 2209 | return false; |
| 2210 | } |
| 2211 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2212 | sp<WindowInfoHandle> InputDispatcher::findFocusedWindowTargetLocked( |
Siarhei Vishniakou | 6950596 | 2023-12-28 12:07:04 -0800 | [diff] [blame] | 2213 | nsecs_t currentTime, const EventEntry& entry, nsecs_t& nextWakeupTime, |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2214 | InputEventInjectionResult& outInjectionResult) { |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2215 | outInjectionResult = InputEventInjectionResult::FAILED; // Default result |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2216 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2217 | int32_t displayId = getTargetDisplayId(entry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2218 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2219 | std::shared_ptr<InputApplicationHandle> focusedApplicationHandle = |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2220 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 2221 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2222 | // If there is no currently focused window and no focused application |
| 2223 | // then drop the event. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2224 | if (focusedWindowHandle == nullptr && focusedApplicationHandle == nullptr) { |
| 2225 | ALOGI("Dropping %s event because there is no focused window or focused application in " |
| 2226 | "display %" PRId32 ".", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2227 | ftl::enum_string(entry.type).c_str(), displayId); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2228 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2229 | } |
| 2230 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2231 | // Drop key events if requested by input feature |
| 2232 | if (focusedWindowHandle != nullptr && shouldDropInput(entry, focusedWindowHandle)) { |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2233 | return nullptr; |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2234 | } |
| 2235 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2236 | // Compatibility behavior: raise ANR if there is a focused application, but no focused window. |
| 2237 | // Only start counting when we have a focused event to dispatch. The ANR is canceled if we |
| 2238 | // start interacting with another application via touch (app switch). This code can be removed |
| 2239 | // if the "no focused window ANR" is moved to the policy. Input doesn't know whether |
| 2240 | // an app is expected to have a focused window. |
| 2241 | if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) { |
| 2242 | if (!mNoFocusedWindowTimeoutTime.has_value()) { |
| 2243 | // 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] | 2244 | std::chrono::nanoseconds timeout = focusedApplicationHandle->getDispatchingTimeout( |
| 2245 | DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
| 2246 | mNoFocusedWindowTimeoutTime = currentTime + timeout.count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2247 | mAwaitedFocusedApplication = focusedApplicationHandle; |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 2248 | mAwaitedApplicationDisplayId = displayId; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2249 | ALOGW("Waiting because no window has focus but %s may eventually add a " |
| 2250 | "window when it finishes starting up. Will wait for %" PRId64 "ms", |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 2251 | mAwaitedFocusedApplication->getName().c_str(), millis(timeout)); |
Siarhei Vishniakou | 6950596 | 2023-12-28 12:07:04 -0800 | [diff] [blame] | 2252 | nextWakeupTime = std::min(nextWakeupTime, *mNoFocusedWindowTimeoutTime); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2253 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 2254 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2255 | } else if (currentTime > *mNoFocusedWindowTimeoutTime) { |
| 2256 | // Already raised ANR. Drop the event |
| 2257 | ALOGE("Dropping %s event because there is no focused window", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2258 | ftl::enum_string(entry.type).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2259 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2260 | } else { |
| 2261 | // Still waiting for the focused window |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2262 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 2263 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2264 | } |
| 2265 | } |
| 2266 | |
| 2267 | // we have a valid, non-null focused window |
| 2268 | resetNoFocusedWindowTimeoutLocked(); |
| 2269 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2270 | // Verify targeted injection. |
| 2271 | if (const auto err = verifyTargetedInjection(focusedWindowHandle, entry); err) { |
| 2272 | ALOGW("Dropping injected event: %s", (*err).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2273 | outInjectionResult = InputEventInjectionResult::TARGET_MISMATCH; |
| 2274 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2275 | } |
| 2276 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2277 | if (focusedWindowHandle->getInfo()->inputConfig.test( |
| 2278 | WindowInfo::InputConfig::PAUSE_DISPATCHING)) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2279 | ALOGI("Waiting because %s is paused", focusedWindowHandle->getName().c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2280 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 2281 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2282 | } |
| 2283 | |
| 2284 | // If the event is a key event, then we must wait for all previous events to |
| 2285 | // complete before delivering it because previous events may have the |
| 2286 | // side-effect of transferring focus to a different window and we want to |
| 2287 | // ensure that the following keys are sent to the new window. |
| 2288 | // |
| 2289 | // Suppose the user touches a button in a window then immediately presses "A". |
| 2290 | // If the button causes a pop-up window to appear then we want to ensure that |
| 2291 | // the "A" key is delivered to the new pop-up window. This is because users |
| 2292 | // often anticipate pending UI changes when typing on a keyboard. |
| 2293 | // To obtain this behavior, we must serialize key events with respect to all |
| 2294 | // prior input events. |
| 2295 | if (entry.type == EventEntry::Type::KEY) { |
| 2296 | if (shouldWaitToSendKeyLocked(currentTime, focusedWindowHandle->getName().c_str())) { |
Siarhei Vishniakou | 6950596 | 2023-12-28 12:07:04 -0800 | [diff] [blame] | 2297 | nextWakeupTime = std::min(nextWakeupTime, *mKeyIsWaitingForEventsTimeout); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2298 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 2299 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2300 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2301 | } |
| 2302 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2303 | outInjectionResult = InputEventInjectionResult::SUCCEEDED; |
| 2304 | return focusedWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2305 | } |
| 2306 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2307 | /** |
| 2308 | * Given a list of monitors, remove the ones we cannot find a connection for, and the ones |
| 2309 | * that are currently unresponsive. |
| 2310 | */ |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2311 | std::vector<Monitor> InputDispatcher::selectResponsiveMonitorsLocked( |
| 2312 | const std::vector<Monitor>& monitors) const { |
| 2313 | std::vector<Monitor> responsiveMonitors; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2314 | std::copy_if(monitors.begin(), monitors.end(), std::back_inserter(responsiveMonitors), |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 2315 | [](const Monitor& monitor) REQUIRES(mLock) { |
| 2316 | std::shared_ptr<Connection> connection = monitor.connection; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2317 | if (!connection->responsive) { |
| 2318 | ALOGW("Unresponsive monitor %s will not get the new gesture", |
Siarhei Vishniakou | 10bfdc9 | 2024-02-01 11:12:57 -0800 | [diff] [blame] | 2319 | connection->getInputChannelName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2320 | return false; |
| 2321 | } |
| 2322 | return true; |
| 2323 | }); |
| 2324 | return responsiveMonitors; |
| 2325 | } |
| 2326 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2327 | std::vector<InputTarget> InputDispatcher::findTouchedWindowTargetsLocked( |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2328 | nsecs_t currentTime, const MotionEntry& entry, |
Siarhei Vishniakou | 4fe5739 | 2022-10-25 13:44:30 -0700 | [diff] [blame] | 2329 | InputEventInjectionResult& outInjectionResult) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2330 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2331 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2332 | std::vector<InputTarget> targets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2333 | // For security reasons, we defer updating the touch state until we are sure that |
| 2334 | // event injection will be allowed. |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2335 | const int32_t displayId = entry.displayId; |
| 2336 | const int32_t action = entry.action; |
Siarhei Vishniakou | bf88052 | 2023-05-01 11:03:22 -0700 | [diff] [blame] | 2337 | const int32_t maskedAction = MotionEvent::getActionMasked(action); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2338 | |
| 2339 | // 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] | 2340 | outInjectionResult = InputEventInjectionResult::PENDING; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2341 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2342 | // Copy current touch state into tempTouchState. |
| 2343 | // This state will be used to update mTouchStatesByDisplay at the end of this function. |
| 2344 | // 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] | 2345 | const TouchState* oldState = nullptr; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2346 | TouchState tempTouchState; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2347 | if (const auto it = mTouchStatesByDisplay.find(displayId); it != mTouchStatesByDisplay.end()) { |
| 2348 | oldState = &(it->second); |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 2349 | tempTouchState = *oldState; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2350 | } |
| 2351 | |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 2352 | bool isSplit = shouldSplitTouch(tempTouchState, entry); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2353 | |
| 2354 | const bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE || |
| 2355 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2356 | maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2357 | // A DOWN could be generated from POINTER_DOWN if the initial pointers did not land into any |
| 2358 | // touchable windows. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2359 | const bool wasDown = oldState != nullptr && oldState->isDown(entry.deviceId); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2360 | const bool isDown = (maskedAction == AMOTION_EVENT_ACTION_DOWN) || |
| 2361 | (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN && !wasDown); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2362 | const bool newGesture = isDown || maskedAction == AMOTION_EVENT_ACTION_SCROLL || |
| 2363 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2364 | maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE; |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 2365 | const bool isFromMouse = isFromSource(entry.source, AINPUT_SOURCE_MOUSE); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2366 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2367 | if (newGesture) { |
| 2368 | isSplit = false; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2369 | } |
| 2370 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2371 | if (isDown && tempTouchState.hasHoveringPointers(entry.deviceId)) { |
| 2372 | // Compatibility behaviour: ACTION_DOWN causes HOVER_EXIT to get generated. |
| 2373 | tempTouchState.clearHoveringPointers(entry.deviceId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2374 | } |
| 2375 | |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2376 | if (isHoverAction) { |
Siarhei Vishniakou | 7be50c9 | 2023-11-17 17:09:08 -0800 | [diff] [blame] | 2377 | if (wasDown) { |
| 2378 | // Started hovering, but the device is already down: reject the hover event |
| 2379 | LOG(ERROR) << "Got hover event " << entry.getDescription() |
| 2380 | << " but the device is already down " << oldState->dump(); |
| 2381 | outInjectionResult = InputEventInjectionResult::FAILED; |
| 2382 | return {}; |
| 2383 | } |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2384 | // For hover actions, we will treat 'tempTouchState' as a new state, so let's erase |
| 2385 | // all of the existing hovering pointers and recompute. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2386 | tempTouchState.clearHoveringPointers(entry.deviceId); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2387 | } |
| 2388 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2389 | if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { |
| 2390 | /* 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] | 2391 | const auto [x, y] = resolveTouchedPosition(entry); |
Siarhei Vishniakou | 5b9766d | 2023-07-18 14:06:29 -0700 | [diff] [blame] | 2392 | const int32_t pointerIndex = MotionEvent::getActionIndex(action); |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2393 | const PointerProperties& pointer = entry.pointerProperties[pointerIndex]; |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2394 | // Outside targets should be added upon first dispatched DOWN event. That means, this should |
| 2395 | // 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] | 2396 | const bool isStylus = isPointerFromStylus(entry, pointerIndex); |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 2397 | sp<WindowInfoHandle> newTouchedWindowHandle = |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2398 | findTouchedWindowAtLocked(displayId, x, y, isStylus); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2399 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2400 | if (isDown) { |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2401 | targets += findOutsideTargetsLocked(displayId, newTouchedWindowHandle, pointer.id); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2402 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2403 | // Handle the case where we did not find a window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2404 | if (newTouchedWindowHandle == nullptr) { |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 2405 | 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] | 2406 | // 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] | 2407 | newTouchedWindowHandle = tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2408 | } |
| 2409 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2410 | // Verify targeted injection. |
| 2411 | if (const auto err = verifyTargetedInjection(newTouchedWindowHandle, entry); err) { |
| 2412 | ALOGW("Dropping injected touch event: %s", (*err).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2413 | outInjectionResult = os::InputEventInjectionResult::TARGET_MISMATCH; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2414 | newTouchedWindowHandle = nullptr; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2415 | return {}; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2416 | } |
| 2417 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2418 | // Figure out whether splitting will be allowed for this window. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2419 | if (newTouchedWindowHandle != nullptr) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2420 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
| 2421 | // New window supports splitting, but we should never split mouse events. |
| 2422 | isSplit = !isFromMouse; |
| 2423 | } else if (isSplit) { |
| 2424 | // New window does not support splitting but we have already split events. |
| 2425 | // Ignore the new window. |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame] | 2426 | LOG(INFO) << "Skipping " << newTouchedWindowHandle->getName() |
| 2427 | << " because it doesn't support split touch"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2428 | newTouchedWindowHandle = nullptr; |
| 2429 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2430 | } else { |
| 2431 | // 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] | 2432 | // be delivered to a new window which supports split touch. Pointers from a mouse device |
| 2433 | // should never be split. |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 2434 | isSplit = !isFromMouse; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2435 | } |
| 2436 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2437 | std::vector<sp<WindowInfoHandle>> newTouchedWindows = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2438 | findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2439 | if (newTouchedWindowHandle != nullptr) { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2440 | // Process the foreground window first so that it is the first to receive the event. |
| 2441 | newTouchedWindows.insert(newTouchedWindows.begin(), newTouchedWindowHandle); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2442 | } |
| 2443 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2444 | if (newTouchedWindows.empty()) { |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 2445 | ALOGI("Dropping event because there is no touchable window at (%.1f, %.1f) on display " |
| 2446 | "%d.", |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2447 | x, y, displayId); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2448 | outInjectionResult = InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2449 | return {}; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2450 | } |
| 2451 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2452 | for (const sp<WindowInfoHandle>& windowHandle : newTouchedWindows) { |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 2453 | if (!canWindowReceiveMotionLocked(windowHandle, entry)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2454 | continue; |
| 2455 | } |
| 2456 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2457 | if (isHoverAction) { |
Siarhei Vishniakou | b681c20 | 2023-05-01 11:22:33 -0700 | [diff] [blame] | 2458 | // The "windowHandle" is the target of this hovering pointer. |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2459 | tempTouchState.addHoveringPointerToWindow(windowHandle, entry.deviceId, pointer); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2460 | } |
| 2461 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2462 | // Set target flags. |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2463 | ftl::Flags<InputTarget::Flags> targetFlags; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2464 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2465 | if (canReceiveForegroundTouches(*windowHandle->getInfo())) { |
| 2466 | // 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] | 2467 | targetFlags |= InputTarget::Flags::FOREGROUND; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2468 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2469 | |
| 2470 | if (isSplit) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2471 | targetFlags |= InputTarget::Flags::SPLIT; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2472 | } |
| 2473 | if (isWindowObscuredAtPointLocked(windowHandle, x, y)) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2474 | targetFlags |= InputTarget::Flags::WINDOW_IS_OBSCURED; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2475 | } else if (isWindowObscuredLocked(windowHandle)) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2476 | targetFlags |= InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2477 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2478 | |
| 2479 | // Update the temporary touch state. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2480 | |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2481 | if (!isHoverAction) { |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2482 | const bool isDownOrPointerDown = maskedAction == AMOTION_EVENT_ACTION_DOWN || |
| 2483 | maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2484 | tempTouchState.addOrUpdateWindow(windowHandle, InputTarget::DispatchMode::AS_IS, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2485 | targetFlags, entry.deviceId, {pointer}, |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2486 | isDownOrPointerDown |
| 2487 | ? std::make_optional(entry.eventTime) |
| 2488 | : std::nullopt); |
| 2489 | // If this is the pointer going down and the touched window has a wallpaper |
| 2490 | // then also add the touched wallpaper windows so they are locked in for the |
| 2491 | // duration of the touch gesture. We do not collect wallpapers during HOVER_MOVE or |
| 2492 | // SCROLL because the wallpaper engine only supports touch events. We would need to |
| 2493 | // add a mechanism similar to View.onGenericMotionEvent to enable wallpapers to |
| 2494 | // handle these events. |
| 2495 | if (isDownOrPointerDown && targetFlags.test(InputTarget::Flags::FOREGROUND) && |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2496 | windowHandle->getInfo()->inputConfig.test( |
| 2497 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER)) { |
| 2498 | sp<WindowInfoHandle> wallpaper = findWallpaperWindowBelow(windowHandle); |
| 2499 | if (wallpaper != nullptr) { |
| 2500 | ftl::Flags<InputTarget::Flags> wallpaperFlags = |
| 2501 | InputTarget::Flags::WINDOW_IS_OBSCURED | |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2502 | InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED; |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2503 | if (isSplit) { |
| 2504 | wallpaperFlags |= InputTarget::Flags::SPLIT; |
| 2505 | } |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2506 | tempTouchState.addOrUpdateWindow(wallpaper, |
| 2507 | InputTarget::DispatchMode::AS_IS, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2508 | wallpaperFlags, entry.deviceId, {pointer}, |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2509 | entry.eventTime); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2510 | } |
| 2511 | } |
| 2512 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2513 | } |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 2514 | |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 2515 | // If a window is already pilfering some pointers, give it this new pointer as well and |
| 2516 | // make it pilfering. This will prevent other non-spy windows from getting this pointer, |
| 2517 | // which is a specific behaviour that we want. |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 2518 | for (TouchedWindow& touchedWindow : tempTouchState.windows) { |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2519 | if (touchedWindow.hasTouchingPointer(entry.deviceId, pointer.id) && |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 2520 | touchedWindow.hasPilferingPointers(entry.deviceId)) { |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 2521 | // This window is already pilfering some pointers, and this new pointer is also |
| 2522 | // going to it. Therefore, take over this pointer and don't give it to anyone |
| 2523 | // else. |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2524 | touchedWindow.addPilferingPointer(entry.deviceId, pointer.id); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 2525 | } |
| 2526 | } |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 2527 | |
| 2528 | // Restrict all pilfered pointers to the pilfering windows. |
| 2529 | tempTouchState.cancelPointersForNonPilferingWindows(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2530 | } else { |
| 2531 | /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ |
| 2532 | |
| 2533 | // If the pointer is not currently down, then ignore the event. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2534 | if (!tempTouchState.isDown(entry.deviceId) && |
| 2535 | maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT) { |
Siarhei Vishniakou | 31dd155 | 2023-10-30 18:46:10 -0700 | [diff] [blame] | 2536 | if (DEBUG_DROPPED_EVENTS_VERBOSE) { |
| 2537 | LOG(INFO) << "Dropping event because the pointer for device " << entry.deviceId |
| 2538 | << " is not down or we previously dropped the pointer down event in " |
| 2539 | << "display " << displayId << ": " << entry.getDescription(); |
| 2540 | } |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2541 | outInjectionResult = InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2542 | return {}; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2543 | } |
| 2544 | |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2545 | // If the pointer is not currently hovering, then ignore the event. |
| 2546 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 2547 | const int32_t pointerId = entry.pointerProperties[0].id; |
| 2548 | if (oldState == nullptr || |
| 2549 | oldState->getWindowsWithHoveringPointer(entry.deviceId, pointerId).empty()) { |
| 2550 | LOG(INFO) << "Dropping event because the hovering pointer is not in any windows in " |
| 2551 | "display " |
| 2552 | << displayId << ": " << entry.getDescription(); |
| 2553 | outInjectionResult = InputEventInjectionResult::FAILED; |
| 2554 | return {}; |
| 2555 | } |
| 2556 | tempTouchState.removeHoveringPointer(entry.deviceId, pointerId); |
| 2557 | } |
| 2558 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2559 | addDragEventLocked(entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2560 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2561 | // Check whether touches should slip outside of the current foreground window. |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 2562 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.getPointerCount() == 1 && |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2563 | tempTouchState.isSlippery()) { |
Siarhei Vishniakou | 9306c38 | 2022-09-30 15:30:31 -0700 | [diff] [blame] | 2564 | const auto [x, y] = resolveTouchedPosition(entry); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2565 | const bool isStylus = isPointerFromStylus(entry, /*pointerIndex=*/0); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2566 | sp<WindowInfoHandle> oldTouchedWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2567 | tempTouchState.getFirstForegroundWindowHandle(); |
Siarhei Vishniakou | 0f6558d | 2023-04-21 12:05:13 -0700 | [diff] [blame] | 2568 | LOG_ALWAYS_FATAL_IF(oldTouchedWindowHandle == nullptr); |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 2569 | sp<WindowInfoHandle> newTouchedWindowHandle = |
| 2570 | findTouchedWindowAtLocked(displayId, x, y, isStylus); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2571 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2572 | // Verify targeted injection. |
| 2573 | if (const auto err = verifyTargetedInjection(newTouchedWindowHandle, entry); err) { |
| 2574 | ALOGW("Dropping injected event: %s", (*err).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2575 | outInjectionResult = os::InputEventInjectionResult::TARGET_MISMATCH; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2576 | return {}; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2577 | } |
| 2578 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2579 | // Drop touch events if requested by input feature |
| 2580 | if (newTouchedWindowHandle != nullptr && |
| 2581 | shouldDropInput(entry, newTouchedWindowHandle)) { |
| 2582 | newTouchedWindowHandle = nullptr; |
| 2583 | } |
| 2584 | |
Siarhei Vishniakou | afa08cc | 2023-05-08 22:35:50 -0700 | [diff] [blame] | 2585 | if (newTouchedWindowHandle != nullptr && |
| 2586 | !haveSameToken(oldTouchedWindowHandle, newTouchedWindowHandle)) { |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2587 | 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] | 2588 | oldTouchedWindowHandle->getName().c_str(), |
| 2589 | newTouchedWindowHandle->getName().c_str(), displayId); |
| 2590 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2591 | // Make a slippery exit from the old window. |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 2592 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2593 | const PointerProperties& pointer = entry.pointerProperties[0]; |
| 2594 | pointerIds.set(pointer.id); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2595 | |
| 2596 | const TouchedWindow& touchedWindow = |
| 2597 | tempTouchState.getTouchedWindow(oldTouchedWindowHandle); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2598 | addPointerWindowTargetLocked(oldTouchedWindowHandle, |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2599 | InputTarget::DispatchMode::SLIPPERY_EXIT, |
| 2600 | ftl::Flags<InputTarget::Flags>(), pointerIds, |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2601 | touchedWindow.getDownTimeInTarget(entry.deviceId), |
| 2602 | targets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2603 | |
| 2604 | // Make a slippery entrance into the new window. |
| 2605 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
Prabir Pradhan | 713bb3e | 2021-12-20 02:07:40 -0800 | [diff] [blame] | 2606 | isSplit = !isFromMouse; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2607 | } |
| 2608 | |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2609 | ftl::Flags<InputTarget::Flags> targetFlags; |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2610 | if (canReceiveForegroundTouches(*newTouchedWindowHandle->getInfo())) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2611 | targetFlags |= InputTarget::Flags::FOREGROUND; |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2612 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2613 | if (isSplit) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2614 | targetFlags |= InputTarget::Flags::SPLIT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2615 | } |
| 2616 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2617 | targetFlags |= InputTarget::Flags::WINDOW_IS_OBSCURED; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2618 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2619 | targetFlags |= InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2620 | } |
| 2621 | |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2622 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, |
| 2623 | InputTarget::DispatchMode::SLIPPERY_ENTER, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2624 | targetFlags, entry.deviceId, {pointer}, |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2625 | entry.eventTime); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2626 | |
| 2627 | // Check if the wallpaper window should deliver the corresponding event. |
| 2628 | slipWallpaperTouch(targetFlags, oldTouchedWindowHandle, newTouchedWindowHandle, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2629 | tempTouchState, entry.deviceId, pointer, targets); |
| 2630 | tempTouchState.removeTouchingPointerFromWindow(entry.deviceId, pointer.id, |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 2631 | oldTouchedWindowHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2632 | } |
| 2633 | } |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 2634 | |
| 2635 | // Update the pointerIds for non-splittable when it received pointer down. |
| 2636 | if (!isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN) { |
| 2637 | // If no split, we suppose all touched windows should receive pointer down. |
Siarhei Vishniakou | 5b9766d | 2023-07-18 14:06:29 -0700 | [diff] [blame] | 2638 | const int32_t pointerIndex = MotionEvent::getActionIndex(action); |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2639 | std::vector<PointerProperties> touchingPointers{entry.pointerProperties[pointerIndex]}; |
| 2640 | for (TouchedWindow& touchedWindow : tempTouchState.windows) { |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 2641 | // Ignore drag window for it should just track one pointer. |
| 2642 | if (mDragState && mDragState->dragWindow == touchedWindow.windowHandle) { |
| 2643 | continue; |
| 2644 | } |
Siarhei Vishniakou | 5b9766d | 2023-07-18 14:06:29 -0700 | [diff] [blame] | 2645 | touchedWindow.addTouchingPointers(entry.deviceId, touchingPointers); |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 2646 | } |
| 2647 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2648 | } |
| 2649 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2650 | // Update dispatching for hover enter and exit. |
Sam Dubey | f886dec | 2023-01-27 13:28:19 +0000 | [diff] [blame] | 2651 | { |
| 2652 | std::vector<TouchedWindow> hoveringWindows = |
| 2653 | getHoveringWindowsLocked(oldState, tempTouchState, entry); |
| 2654 | for (const TouchedWindow& touchedWindow : hoveringWindows) { |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 2655 | std::optional<InputTarget> target = |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2656 | createInputTargetLocked(touchedWindow.windowHandle, touchedWindow.dispatchMode, |
| 2657 | touchedWindow.targetFlags, |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 2658 | touchedWindow.getDownTimeInTarget(entry.deviceId)); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 2659 | if (!target) { |
| 2660 | continue; |
| 2661 | } |
| 2662 | // Hardcode to single hovering pointer for now. |
| 2663 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
| 2664 | pointerIds.set(entry.pointerProperties[0].id); |
| 2665 | target->addPointers(pointerIds, touchedWindow.windowHandle->getInfo()->transform); |
| 2666 | targets.push_back(*target); |
Sam Dubey | f886dec | 2023-01-27 13:28:19 +0000 | [diff] [blame] | 2667 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2668 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2669 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2670 | // Ensure that all touched windows are valid for injection. |
| 2671 | if (entry.injectionState != nullptr) { |
| 2672 | std::string errs; |
| 2673 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2674 | const auto err = verifyTargetedInjection(touchedWindow.windowHandle, entry); |
| 2675 | if (err) errs += "\n - " + *err; |
| 2676 | } |
| 2677 | if (!errs.empty()) { |
| 2678 | 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] | 2679 | "%s:%s", |
| 2680 | entry.injectionState->targetUid->toString().c_str(), errs.c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2681 | outInjectionResult = InputEventInjectionResult::TARGET_MISMATCH; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2682 | return {}; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2683 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2684 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2685 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2686 | // Check whether windows listening for outside touches are owned by the same UID. If the owner |
| 2687 | // 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] | 2688 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2689 | sp<WindowInfoHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2690 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2691 | if (foregroundWindowHandle) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 2692 | const auto foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2693 | for (InputTarget& target : targets) { |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2694 | if (target.dispatchMode == InputTarget::DispatchMode::OUTSIDE) { |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2695 | sp<WindowInfoHandle> targetWindow = |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 2696 | getWindowHandleLocked(target.connection->getToken()); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2697 | if (targetWindow->getInfo()->ownerUid != foregroundWindowUid) { |
| 2698 | target.flags |= InputTarget::Flags::ZERO_COORDS; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2699 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2700 | } |
| 2701 | } |
| 2702 | } |
| 2703 | } |
| 2704 | |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 2705 | // If this is a touchpad navigation gesture, it needs to only be sent to trusted targets, as we |
| 2706 | // only want the system UI to handle these gestures. |
| 2707 | const bool isTouchpadNavGesture = isFromSource(entry.source, AINPUT_SOURCE_MOUSE) && |
| 2708 | entry.classification == MotionClassification::MULTI_FINGER_SWIPE; |
| 2709 | if (isTouchpadNavGesture) { |
| 2710 | filterUntrustedTargets(/* byref */ tempTouchState, /* byref */ targets); |
| 2711 | } |
| 2712 | |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2713 | // Output targets from the touch state. |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2714 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2715 | std::vector<PointerProperties> touchingPointers = |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2716 | touchedWindow.getTouchingPointers(entry.deviceId); |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2717 | if (touchingPointers.empty()) { |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2718 | continue; |
| 2719 | } |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2720 | addPointerWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.dispatchMode, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2721 | touchedWindow.targetFlags, getPointerIds(touchingPointers), |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2722 | touchedWindow.getDownTimeInTarget(entry.deviceId), targets); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2723 | } |
Sam Dubey | 39d37cf | 2022-12-07 18:05:35 +0000 | [diff] [blame] | 2724 | |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 2725 | // During targeted injection, only allow owned targets to receive events |
| 2726 | std::erase_if(targets, [&](const InputTarget& target) { |
| 2727 | LOG_ALWAYS_FATAL_IF(target.windowHandle == nullptr); |
| 2728 | const auto err = verifyTargetedInjection(target.windowHandle, entry); |
| 2729 | if (err) { |
| 2730 | LOG(WARNING) << "Dropping injected event from " << target.windowHandle->getName() |
| 2731 | << ": " << (*err); |
| 2732 | return true; |
| 2733 | } |
| 2734 | return false; |
| 2735 | }); |
| 2736 | |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2737 | if (targets.empty()) { |
| 2738 | LOG(INFO) << "Dropping event because no targets were found: " << entry.getDescription(); |
| 2739 | outInjectionResult = InputEventInjectionResult::FAILED; |
| 2740 | return {}; |
| 2741 | } |
| 2742 | |
| 2743 | // If we only have windows getting ACTION_OUTSIDE, then drop the event, because there is no |
| 2744 | // window that is actually receiving the entire gesture. |
| 2745 | if (std::all_of(targets.begin(), targets.end(), [](const InputTarget& target) { |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2746 | return target.dispatchMode == InputTarget::DispatchMode::OUTSIDE; |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2747 | })) { |
| 2748 | LOG(INFO) << "Dropping event because all windows would just receive ACTION_OUTSIDE: " |
| 2749 | << entry.getDescription(); |
| 2750 | outInjectionResult = InputEventInjectionResult::FAILED; |
| 2751 | return {}; |
| 2752 | } |
| 2753 | |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2754 | outInjectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2755 | |
Prabir Pradhan | 502a725 | 2023-12-01 16:11:24 +0000 | [diff] [blame] | 2756 | // Now that we have generated all of the input targets for this event, reset the dispatch |
| 2757 | // mode for all touched window to AS_IS. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2758 | for (TouchedWindow& touchedWindow : tempTouchState.windows) { |
Prabir Pradhan | 502a725 | 2023-12-01 16:11:24 +0000 | [diff] [blame] | 2759 | touchedWindow.dispatchMode = InputTarget::DispatchMode::AS_IS; |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2760 | } |
| 2761 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2762 | // Update final pieces of touch state if the injector had permission. |
Siarhei Vishniakou | 7be50c9 | 2023-11-17 17:09:08 -0800 | [diff] [blame] | 2763 | if (maskedAction == AMOTION_EVENT_ACTION_UP) { |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2764 | // Pointer went up. |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 2765 | tempTouchState.removeTouchingPointer(entry.deviceId, entry.pointerProperties[0].id); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2766 | } else if (maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2767 | // All pointers up or canceled. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2768 | tempTouchState.removeAllPointersForDevice(entry.deviceId); |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2769 | } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 2770 | // One pointer went up. |
Siarhei Vishniakou | 5b9766d | 2023-07-18 14:06:29 -0700 | [diff] [blame] | 2771 | const int32_t pointerIndex = MotionEvent::getActionIndex(action); |
| 2772 | const uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
| 2773 | tempTouchState.removeTouchingPointer(entry.deviceId, pointerId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2774 | } |
| 2775 | |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2776 | // Save changes unless the action was scroll in which case the temporary touch |
| 2777 | // state was only valid for this one action. |
| 2778 | if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) { |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 2779 | if (displayId >= 0) { |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2780 | tempTouchState.clearWindowsWithoutPointers(); |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2781 | mTouchStatesByDisplay[displayId] = tempTouchState; |
| 2782 | } else { |
| 2783 | mTouchStatesByDisplay.erase(displayId); |
| 2784 | } |
| 2785 | } |
| 2786 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 2787 | if (tempTouchState.windows.empty()) { |
| 2788 | mTouchStatesByDisplay.erase(displayId); |
| 2789 | } |
| 2790 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2791 | return targets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2792 | } |
| 2793 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2794 | void InputDispatcher::finishDragAndDrop(int32_t displayId, float x, float y) { |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2795 | // Prevent stylus interceptor windows from affecting drag and drop behavior for now, until we |
| 2796 | // have an explicit reason to support it. |
| 2797 | constexpr bool isStylus = false; |
| 2798 | |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 2799 | sp<WindowInfoHandle> dropWindow = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2800 | findTouchedWindowAtLocked(displayId, x, y, isStylus, /*ignoreDragWindow=*/true); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2801 | if (dropWindow) { |
| 2802 | vec2 local = dropWindow->getInfo()->transform.transform(x, y); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2803 | sendDropWindowCommandLocked(dropWindow->getToken(), local.x, local.y); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 2804 | } else { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2805 | ALOGW("No window found when drop."); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2806 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2807 | } |
| 2808 | mDragState.reset(); |
| 2809 | } |
| 2810 | |
| 2811 | void InputDispatcher::addDragEventLocked(const MotionEntry& entry) { |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 2812 | if (!mDragState || mDragState->dragWindow->getInfo()->displayId != entry.displayId) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2813 | return; |
| 2814 | } |
| 2815 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2816 | if (!mDragState->isStartDrag) { |
| 2817 | mDragState->isStartDrag = true; |
| 2818 | mDragState->isStylusButtonDownAtStart = |
| 2819 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2820 | } |
| 2821 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2822 | // Find the pointer index by id. |
| 2823 | int32_t pointerIndex = 0; |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 2824 | for (; static_cast<uint32_t>(pointerIndex) < entry.getPointerCount(); pointerIndex++) { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2825 | const PointerProperties& pointerProperties = entry.pointerProperties[pointerIndex]; |
| 2826 | if (pointerProperties.id == mDragState->pointerId) { |
| 2827 | break; |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2828 | } |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2829 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2830 | |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 2831 | if (uint32_t(pointerIndex) == entry.getPointerCount()) { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2832 | 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] | 2833 | } |
| 2834 | |
| 2835 | const int32_t maskedAction = entry.action & AMOTION_EVENT_ACTION_MASK; |
| 2836 | const int32_t x = entry.pointerCoords[pointerIndex].getX(); |
| 2837 | const int32_t y = entry.pointerCoords[pointerIndex].getY(); |
| 2838 | |
| 2839 | switch (maskedAction) { |
| 2840 | case AMOTION_EVENT_ACTION_MOVE: { |
| 2841 | // Handle the special case : stylus button no longer pressed. |
| 2842 | bool isStylusButtonDown = |
| 2843 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2844 | if (mDragState->isStylusButtonDownAtStart && !isStylusButtonDown) { |
| 2845 | finishDragAndDrop(entry.displayId, x, y); |
| 2846 | return; |
| 2847 | } |
| 2848 | |
| 2849 | // Prevent stylus interceptor windows from affecting drag and drop behavior for now, |
| 2850 | // until we have an explicit reason to support it. |
| 2851 | constexpr bool isStylus = false; |
| 2852 | |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 2853 | sp<WindowInfoHandle> hoverWindowHandle = |
| 2854 | findTouchedWindowAtLocked(entry.displayId, x, y, isStylus, |
| 2855 | /*ignoreDragWindow=*/true); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2856 | // enqueue drag exit if needed. |
| 2857 | if (hoverWindowHandle != mDragState->dragHoverWindowHandle && |
| 2858 | !haveSameToken(hoverWindowHandle, mDragState->dragHoverWindowHandle)) { |
| 2859 | if (mDragState->dragHoverWindowHandle != nullptr) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2860 | enqueueDragEventLocked(mDragState->dragHoverWindowHandle, /*isExiting=*/true, x, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2861 | y); |
| 2862 | } |
| 2863 | mDragState->dragHoverWindowHandle = hoverWindowHandle; |
| 2864 | } |
| 2865 | // enqueue drag location if needed. |
| 2866 | if (hoverWindowHandle != nullptr) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2867 | enqueueDragEventLocked(hoverWindowHandle, /*isExiting=*/false, x, y); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2868 | } |
| 2869 | break; |
| 2870 | } |
| 2871 | |
| 2872 | case AMOTION_EVENT_ACTION_POINTER_UP: |
Siarhei Vishniakou | 5b9766d | 2023-07-18 14:06:29 -0700 | [diff] [blame] | 2873 | if (MotionEvent::getActionIndex(entry.action) != pointerIndex) { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2874 | break; |
| 2875 | } |
| 2876 | // The drag pointer is up. |
| 2877 | [[fallthrough]]; |
| 2878 | case AMOTION_EVENT_ACTION_UP: |
| 2879 | finishDragAndDrop(entry.displayId, x, y); |
| 2880 | break; |
| 2881 | case AMOTION_EVENT_ACTION_CANCEL: { |
| 2882 | ALOGD("Receiving cancel when drag and drop."); |
| 2883 | sendDropWindowCommandLocked(nullptr, 0, 0); |
| 2884 | mDragState.reset(); |
| 2885 | break; |
| 2886 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2887 | } |
| 2888 | } |
| 2889 | |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2890 | std::optional<InputTarget> InputDispatcher::createInputTargetLocked( |
| 2891 | const sp<android::gui::WindowInfoHandle>& windowHandle, |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2892 | InputTarget::DispatchMode dispatchMode, ftl::Flags<InputTarget::Flags> targetFlags, |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2893 | std::optional<nsecs_t> firstDownTimeInTarget) const { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 2894 | std::shared_ptr<Connection> connection = getConnectionLocked(windowHandle->getToken()); |
| 2895 | if (connection == nullptr) { |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2896 | ALOGW("Not creating InputTarget for %s, no input channel", windowHandle->getName().c_str()); |
| 2897 | return {}; |
| 2898 | } |
Prabir Pradhan | c32a411 | 2024-01-23 23:20:37 +0000 | [diff] [blame] | 2899 | InputTarget inputTarget{connection}; |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 2900 | inputTarget.windowHandle = windowHandle; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2901 | inputTarget.dispatchMode = dispatchMode; |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2902 | inputTarget.flags = targetFlags; |
| 2903 | inputTarget.globalScaleFactor = windowHandle->getInfo()->globalScaleFactor; |
| 2904 | inputTarget.firstDownTimeInTarget = firstDownTimeInTarget; |
| 2905 | const auto& displayInfoIt = mDisplayInfos.find(windowHandle->getInfo()->displayId); |
| 2906 | if (displayInfoIt != mDisplayInfos.end()) { |
| 2907 | inputTarget.displayTransform = displayInfoIt->second.transform; |
| 2908 | } else { |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 2909 | // DisplayInfo not found for this window on display windowHandle->getInfo()->displayId. |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2910 | // TODO(b/198444055): Make this an error message after 'setInputWindows' API is removed. |
| 2911 | } |
| 2912 | return inputTarget; |
| 2913 | } |
| 2914 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2915 | void InputDispatcher::addWindowTargetLocked(const sp<WindowInfoHandle>& windowHandle, |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2916 | InputTarget::DispatchMode dispatchMode, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2917 | ftl::Flags<InputTarget::Flags> targetFlags, |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2918 | std::optional<nsecs_t> firstDownTimeInTarget, |
Siarhei Vishniakou | f75cddb | 2022-10-25 10:42:16 -0700 | [diff] [blame] | 2919 | std::vector<InputTarget>& inputTargets) const { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2920 | std::vector<InputTarget>::iterator it = |
| 2921 | std::find_if(inputTargets.begin(), inputTargets.end(), |
| 2922 | [&windowHandle](const InputTarget& inputTarget) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 2923 | return inputTarget.connection->getToken() == windowHandle->getToken(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2924 | }); |
Chavi Weingarten | 97b8eec | 2020-01-09 18:09:08 +0000 | [diff] [blame] | 2925 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2926 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2927 | |
| 2928 | if (it == inputTargets.end()) { |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2929 | std::optional<InputTarget> target = |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2930 | createInputTargetLocked(windowHandle, dispatchMode, targetFlags, |
| 2931 | firstDownTimeInTarget); |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2932 | if (!target) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2933 | return; |
| 2934 | } |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2935 | inputTargets.push_back(*target); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2936 | it = inputTargets.end() - 1; |
| 2937 | } |
| 2938 | |
Siarhei Vishniakou | 23d73fb | 2023-10-29 13:27:46 -0700 | [diff] [blame] | 2939 | if (it->flags != targetFlags) { |
| 2940 | LOG(ERROR) << "Flags don't match! targetFlags=" << targetFlags.string() << ", it=" << *it; |
| 2941 | } |
| 2942 | if (it->globalScaleFactor != windowInfo->globalScaleFactor) { |
| 2943 | LOG(ERROR) << "Mismatch! it->globalScaleFactor=" << it->globalScaleFactor |
| 2944 | << ", windowInfo->globalScaleFactor=" << windowInfo->globalScaleFactor; |
| 2945 | } |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2946 | } |
| 2947 | |
| 2948 | void InputDispatcher::addPointerWindowTargetLocked( |
| 2949 | const sp<android::gui::WindowInfoHandle>& windowHandle, |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2950 | InputTarget::DispatchMode dispatchMode, ftl::Flags<InputTarget::Flags> targetFlags, |
| 2951 | std::bitset<MAX_POINTER_ID + 1> pointerIds, std::optional<nsecs_t> firstDownTimeInTarget, |
| 2952 | std::vector<InputTarget>& inputTargets) const REQUIRES(mLock) { |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2953 | if (pointerIds.none()) { |
| 2954 | for (const auto& target : inputTargets) { |
| 2955 | LOG(INFO) << "Target: " << target; |
| 2956 | } |
| 2957 | LOG(FATAL) << "No pointers specified for " << windowHandle->getName(); |
| 2958 | return; |
| 2959 | } |
| 2960 | std::vector<InputTarget>::iterator it = |
| 2961 | std::find_if(inputTargets.begin(), inputTargets.end(), |
| 2962 | [&windowHandle](const InputTarget& inputTarget) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 2963 | return inputTarget.connection->getToken() == windowHandle->getToken(); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2964 | }); |
| 2965 | |
| 2966 | // This is a hack, because the actual entry could potentially be an ACTION_DOWN event that |
| 2967 | // causes a HOVER_EXIT to be generated. That means that the same entry of ACTION_DOWN would |
| 2968 | // have DISPATCH_AS_HOVER_EXIT and DISPATCH_AS_IS. And therefore, we have to create separate |
| 2969 | // input targets for hovering pointers and for touching pointers. |
| 2970 | // If we picked an existing input target above, but it's for HOVER_EXIT - let's use a new |
| 2971 | // target instead. |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2972 | if (it != inputTargets.end() && it->dispatchMode == InputTarget::DispatchMode::HOVER_EXIT) { |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2973 | // Force the code below to create a new input target |
| 2974 | it = inputTargets.end(); |
| 2975 | } |
| 2976 | |
| 2977 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 2978 | |
| 2979 | if (it == inputTargets.end()) { |
| 2980 | std::optional<InputTarget> target = |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2981 | createInputTargetLocked(windowHandle, dispatchMode, targetFlags, |
| 2982 | firstDownTimeInTarget); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2983 | if (!target) { |
| 2984 | return; |
| 2985 | } |
| 2986 | inputTargets.push_back(*target); |
| 2987 | it = inputTargets.end() - 1; |
| 2988 | } |
| 2989 | |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2990 | if (it->dispatchMode != dispatchMode) { |
| 2991 | LOG(ERROR) << __func__ << ": DispatchMode doesn't match! ignoring new mode=" |
| 2992 | << ftl::enum_string(dispatchMode) << ", it=" << *it; |
| 2993 | } |
Siarhei Vishniakou | 4bd0b7c | 2023-10-27 00:51:14 -0700 | [diff] [blame] | 2994 | if (it->flags != targetFlags) { |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2995 | LOG(ERROR) << __func__ << ": Flags don't match! new targetFlags=" << targetFlags.string() |
| 2996 | << ", it=" << *it; |
Siarhei Vishniakou | 4bd0b7c | 2023-10-27 00:51:14 -0700 | [diff] [blame] | 2997 | } |
Siarhei Vishniakou | 23d73fb | 2023-10-29 13:27:46 -0700 | [diff] [blame] | 2998 | if (it->globalScaleFactor != windowInfo->globalScaleFactor) { |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2999 | LOG(ERROR) << __func__ << ": Mismatch! it->globalScaleFactor=" << it->globalScaleFactor |
Siarhei Vishniakou | 23d73fb | 2023-10-29 13:27:46 -0700 | [diff] [blame] | 3000 | << ", windowInfo->globalScaleFactor=" << windowInfo->globalScaleFactor; |
| 3001 | } |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3002 | |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3003 | it->addPointers(pointerIds, windowInfo->transform); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3004 | } |
| 3005 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3006 | void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 3007 | int32_t displayId) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 3008 | auto monitorsIt = mGlobalMonitorsByDisplay.find(displayId); |
| 3009 | if (monitorsIt == mGlobalMonitorsByDisplay.end()) return; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3010 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 3011 | for (const Monitor& monitor : selectResponsiveMonitorsLocked(monitorsIt->second)) { |
Prabir Pradhan | c32a411 | 2024-01-23 23:20:37 +0000 | [diff] [blame] | 3012 | InputTarget target{monitor.connection}; |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3013 | // target.firstDownTimeInTarget is not set for global monitors. It is only required in split |
| 3014 | // touch and global monitoring works as intended even without setting firstDownTimeInTarget |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 3015 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
| 3016 | target.displayTransform = it->second.transform; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3017 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 3018 | target.setDefaultPointerTransform(target.displayTransform); |
| 3019 | inputTargets.push_back(target); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3020 | } |
| 3021 | } |
| 3022 | |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3023 | /** |
| 3024 | * Indicate whether one window handle should be considered as obscuring |
| 3025 | * another window handle. We only check a few preconditions. Actually |
| 3026 | * checking the bounds is left to the caller. |
| 3027 | */ |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3028 | static bool canBeObscuredBy(const sp<WindowInfoHandle>& windowHandle, |
| 3029 | const sp<WindowInfoHandle>& otherHandle) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3030 | // Compare by token so cloned layers aren't counted |
| 3031 | if (haveSameToken(windowHandle, otherHandle)) { |
| 3032 | return false; |
| 3033 | } |
| 3034 | auto info = windowHandle->getInfo(); |
| 3035 | auto otherInfo = otherHandle->getInfo(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 3036 | if (otherInfo->inputConfig.test(WindowInfo::InputConfig::NOT_VISIBLE)) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3037 | return false; |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 3038 | } else if (otherInfo->alpha == 0 && |
| 3039 | otherInfo->inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE)) { |
Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame] | 3040 | // Those act as if they were invisible, so we don't need to flag them. |
| 3041 | // We do want to potentially flag touchable windows even if they have 0 |
| 3042 | // opacity, since they can consume touches and alter the effects of the |
| 3043 | // user interaction (eg. apps that rely on |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3044 | // Flags::WINDOW_IS_PARTIALLY_OBSCURED should still be told about those |
Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame] | 3045 | // windows), hence we also check for FLAG_NOT_TOUCHABLE. |
| 3046 | return false; |
Bernardo Rufino | 8007daf | 2020-09-22 09:40:01 +0000 | [diff] [blame] | 3047 | } else if (info->ownerUid == otherInfo->ownerUid) { |
| 3048 | // If ownerUid is the same we don't generate occlusion events as there |
| 3049 | // is no security boundary within an uid. |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3050 | return false; |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 3051 | } else if (otherInfo->inputConfig.test(gui::WindowInfo::InputConfig::TRUSTED_OVERLAY)) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3052 | return false; |
| 3053 | } else if (otherInfo->displayId != info->displayId) { |
| 3054 | return false; |
| 3055 | } |
| 3056 | return true; |
| 3057 | } |
| 3058 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3059 | /** |
| 3060 | * Returns touch occlusion information in the form of TouchOcclusionInfo. To check if the touch is |
| 3061 | * untrusted, one should check: |
| 3062 | * |
| 3063 | * 1. If result.hasBlockingOcclusion is true. |
| 3064 | * If it's, it means the touch should be blocked due to a window with occlusion mode of |
| 3065 | * BLOCK_UNTRUSTED. |
| 3066 | * |
| 3067 | * 2. If result.obscuringOpacity > mMaximumObscuringOpacityForTouch. |
| 3068 | * If it is (and 1 is false), then the touch should be blocked because a stack of windows |
| 3069 | * (possibly only one) with occlusion mode of USE_OPACITY from one UID resulted in a composed |
| 3070 | * obscuring opacity above the threshold. Note that if there was no window of occlusion mode |
| 3071 | * USE_OPACITY, result.obscuringOpacity would've been 0 and since |
| 3072 | * mMaximumObscuringOpacityForTouch >= 0, the condition above would never be true. |
| 3073 | * |
| 3074 | * If neither of those is true, then it means the touch can be allowed. |
| 3075 | */ |
| 3076 | InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3077 | const sp<WindowInfoHandle>& windowHandle, int32_t x, int32_t y) const { |
| 3078 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 3079 | int32_t displayId = windowInfo->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3080 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3081 | TouchOcclusionInfo info; |
| 3082 | info.hasBlockingOcclusion = false; |
| 3083 | info.obscuringOpacity = 0; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3084 | info.obscuringUid = gui::Uid::INVALID; |
| 3085 | std::map<gui::Uid, float> opacityByUid; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3086 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3087 | if (windowHandle == otherHandle) { |
| 3088 | break; // All future windows are below us. Exit early. |
| 3089 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3090 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 3091 | if (canBeObscuredBy(windowHandle, otherHandle) && otherInfo->frameContainsPoint(x, y) && |
| 3092 | !haveSameApplicationToken(windowInfo, otherInfo)) { |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 3093 | if (DEBUG_TOUCH_OCCLUSION) { |
| 3094 | info.debugInfo.push_back( |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 3095 | dumpWindowForTouchOcclusion(otherInfo, /*isTouchedWindow=*/false)); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 3096 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3097 | // canBeObscuredBy() has returned true above, which means this window is untrusted, so |
| 3098 | // we perform the checks below to see if the touch can be propagated or not based on the |
| 3099 | // window's touch occlusion mode |
| 3100 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::BLOCK_UNTRUSTED) { |
| 3101 | info.hasBlockingOcclusion = true; |
| 3102 | info.obscuringUid = otherInfo->ownerUid; |
| 3103 | info.obscuringPackage = otherInfo->packageName; |
| 3104 | break; |
| 3105 | } |
| 3106 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::USE_OPACITY) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3107 | const auto uid = otherInfo->ownerUid; |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3108 | float opacity = |
| 3109 | (opacityByUid.find(uid) == opacityByUid.end()) ? 0 : opacityByUid[uid]; |
| 3110 | // Given windows A and B: |
| 3111 | // opacity(A, B) = 1 - [1 - opacity(A)] * [1 - opacity(B)] |
| 3112 | opacity = 1 - (1 - opacity) * (1 - otherInfo->alpha); |
| 3113 | opacityByUid[uid] = opacity; |
| 3114 | if (opacity > info.obscuringOpacity) { |
| 3115 | info.obscuringOpacity = opacity; |
| 3116 | info.obscuringUid = uid; |
| 3117 | info.obscuringPackage = otherInfo->packageName; |
| 3118 | } |
| 3119 | } |
| 3120 | } |
| 3121 | } |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 3122 | if (DEBUG_TOUCH_OCCLUSION) { |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 3123 | info.debugInfo.push_back(dumpWindowForTouchOcclusion(windowInfo, /*isTouchedWindow=*/true)); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 3124 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3125 | return info; |
| 3126 | } |
| 3127 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3128 | std::string InputDispatcher::dumpWindowForTouchOcclusion(const WindowInfo* info, |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 3129 | bool isTouchedWindow) const { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3130 | return StringPrintf(INDENT2 "* %spackage=%s/%s, id=%" PRId32 ", mode=%s, alpha=%.2f, " |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 3131 | "frame=[%" PRId32 ",%" PRId32 "][%" PRId32 ",%" PRId32 |
| 3132 | "], touchableRegion=%s, window={%s}, inputConfig={%s}, " |
| 3133 | "hasToken=%s, applicationInfo.name=%s, applicationInfo.token=%s\n", |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 3134 | isTouchedWindow ? "[TOUCHED] " : "", info->packageName.c_str(), |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3135 | info->ownerUid.toString().c_str(), info->id, |
Chavi Weingarten | 7f01919 | 2023-08-08 20:39:01 +0000 | [diff] [blame] | 3136 | toString(info->touchOcclusionMode).c_str(), info->alpha, info->frame.left, |
| 3137 | info->frame.top, info->frame.right, info->frame.bottom, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3138 | dumpRegion(info->touchableRegion).c_str(), info->name.c_str(), |
| 3139 | info->inputConfig.string().c_str(), toString(info->token != nullptr), |
| 3140 | info->applicationInfo.name.c_str(), |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 3141 | binderToString(info->applicationInfo.token).c_str()); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 3142 | } |
| 3143 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3144 | bool InputDispatcher::isTouchTrustedLocked(const TouchOcclusionInfo& occlusionInfo) const { |
| 3145 | if (occlusionInfo.hasBlockingOcclusion) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3146 | ALOGW("Untrusted touch due to occlusion by %s/%s", occlusionInfo.obscuringPackage.c_str(), |
| 3147 | occlusionInfo.obscuringUid.toString().c_str()); |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3148 | return false; |
| 3149 | } |
| 3150 | if (occlusionInfo.obscuringOpacity > mMaximumObscuringOpacityForTouch) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3151 | ALOGW("Untrusted touch due to occlusion by %s/%s (obscuring opacity = " |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3152 | "%.2f, maximum allowed = %.2f)", |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3153 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid.toString().c_str(), |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3154 | occlusionInfo.obscuringOpacity, mMaximumObscuringOpacityForTouch); |
| 3155 | return false; |
| 3156 | } |
| 3157 | return true; |
| 3158 | } |
| 3159 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3160 | bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3161 | int32_t x, int32_t y) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3162 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3163 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 3164 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3165 | if (windowHandle == otherHandle) { |
| 3166 | break; // All future windows are below us. Exit early. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3167 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3168 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3169 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 3170 | otherInfo->frameContainsPoint(x, y)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3171 | return true; |
| 3172 | } |
| 3173 | } |
| 3174 | return false; |
| 3175 | } |
| 3176 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3177 | bool InputDispatcher::isWindowObscuredLocked(const sp<WindowInfoHandle>& windowHandle) const { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 3178 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3179 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 3180 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 3181 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3182 | if (windowHandle == otherHandle) { |
| 3183 | break; // All future windows are below us. Exit early. |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 3184 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3185 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3186 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 3187 | otherInfo->overlaps(windowInfo)) { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 3188 | return true; |
| 3189 | } |
| 3190 | } |
| 3191 | return false; |
| 3192 | } |
| 3193 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 3194 | std::string InputDispatcher::getApplicationWindowLabel( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3195 | const InputApplicationHandle* applicationHandle, const sp<WindowInfoHandle>& windowHandle) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3196 | if (applicationHandle != nullptr) { |
| 3197 | if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 3198 | return applicationHandle->getName() + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3199 | } else { |
| 3200 | return applicationHandle->getName(); |
| 3201 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3202 | } else if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 3203 | return windowHandle->getInfo()->applicationInfo.name + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3204 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3205 | return "<unknown application or window>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3206 | } |
| 3207 | } |
| 3208 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3209 | void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3210 | if (!isUserActivityEvent(eventEntry)) { |
| 3211 | // 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] | 3212 | return; |
| 3213 | } |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 3214 | |
| 3215 | const int32_t eventType = getUserActivityEventType(eventEntry); |
| 3216 | if (input_flags::rate_limit_user_activity_poke_in_dispatcher()) { |
| 3217 | // Note that we're directly getting the time diff between the current event and the previous |
| 3218 | // event. This is assuming that the first user event always happens at a timestamp that is |
| 3219 | // greater than `mMinTimeBetweenUserActivityPokes` (otherwise, the first user event will |
| 3220 | // wrongly be dropped). In real life, `mMinTimeBetweenUserActivityPokes` is a much smaller |
| 3221 | // value than the potential first user activity event time, so this is ok. |
| 3222 | std::chrono::nanoseconds timeSinceLastEvent = |
| 3223 | std::chrono::nanoseconds(eventEntry.eventTime - mLastUserActivityTimes[eventType]); |
| 3224 | if (timeSinceLastEvent < mMinTimeBetweenUserActivityPokes) { |
| 3225 | return; |
| 3226 | } |
| 3227 | } |
| 3228 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3229 | int32_t displayId = getTargetDisplayId(eventEntry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3230 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 3231 | const WindowInfo* windowDisablingUserActivityInfo = nullptr; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3232 | if (focusedWindowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3233 | const WindowInfo* info = focusedWindowHandle->getInfo(); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 3234 | if (info->inputConfig.test(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY)) { |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 3235 | windowDisablingUserActivityInfo = info; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3236 | } |
| 3237 | } |
| 3238 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3239 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3240 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3241 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 3242 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3243 | return; |
| 3244 | } |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 3245 | if (windowDisablingUserActivityInfo != nullptr) { |
| 3246 | if (DEBUG_DISPATCH_CYCLE) { |
| 3247 | ALOGD("Not poking user activity: disabled by window '%s'.", |
| 3248 | windowDisablingUserActivityInfo->name.c_str()); |
| 3249 | } |
| 3250 | return; |
| 3251 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3252 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3253 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3254 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3255 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 3256 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3257 | return; |
| 3258 | } |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 3259 | // If the key code is unknown, we don't consider it user activity |
| 3260 | if (keyEntry.keyCode == AKEYCODE_UNKNOWN) { |
| 3261 | return; |
| 3262 | } |
| 3263 | // Don't inhibit events that were intercepted or are not passed to |
| 3264 | // the apps, like system shortcuts |
| 3265 | if (windowDisablingUserActivityInfo != nullptr && |
| 3266 | keyEntry.interceptKeyResult != KeyEntry::InterceptKeyResult::SKIP && |
| 3267 | keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER) { |
| 3268 | if (DEBUG_DISPATCH_CYCLE) { |
| 3269 | ALOGD("Not poking user activity: disabled by window '%s'.", |
| 3270 | windowDisablingUserActivityInfo->name.c_str()); |
| 3271 | } |
| 3272 | return; |
| 3273 | } |
| 3274 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3275 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3276 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3277 | default: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3278 | LOG_ALWAYS_FATAL("%s events are not user activity", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3279 | ftl::enum_string(eventEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3280 | break; |
| 3281 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3282 | } |
| 3283 | |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 3284 | mLastUserActivityTimes[eventType] = eventEntry.eventTime; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3285 | auto command = [this, eventTime = eventEntry.eventTime, eventType, displayId]() |
| 3286 | REQUIRES(mLock) { |
| 3287 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 3288 | mPolicy.pokeUserActivity(eventTime, eventType, displayId); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3289 | }; |
| 3290 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3291 | } |
| 3292 | |
| 3293 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3294 | const std::shared_ptr<Connection>& connection, |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 3295 | std::shared_ptr<const EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3296 | const InputTarget& inputTarget) { |
Prabir Pradhan | 2dac8b8 | 2023-09-06 01:11:51 +0000 | [diff] [blame] | 3297 | ATRACE_NAME_IF(ATRACE_ENABLED(), |
| 3298 | StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
| 3299 | connection->getInputChannelName().c_str(), eventEntry->id)); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3300 | if (DEBUG_DISPATCH_CYCLE) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3301 | ALOGD("channel '%s' ~ prepareDispatchCycle - flags=%s, " |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 3302 | "globalScaleFactor=%f, pointerIds=%s %s", |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3303 | connection->getInputChannelName().c_str(), inputTarget.flags.string().c_str(), |
Prabir Pradhan | c32a411 | 2024-01-23 23:20:37 +0000 | [diff] [blame] | 3304 | inputTarget.globalScaleFactor, bitsetToString(inputTarget.getPointerIds()).c_str(), |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3305 | inputTarget.getPointerInfoString().c_str()); |
| 3306 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3307 | |
| 3308 | // Skip this event if the connection status is not normal. |
| 3309 | // 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] | 3310 | if (connection->status != Connection::Status::NORMAL) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3311 | if (DEBUG_DISPATCH_CYCLE) { |
| 3312 | ALOGD("channel '%s' ~ Dropping event because the channel status is %s", |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3313 | connection->getInputChannelName().c_str(), |
| 3314 | ftl::enum_string(connection->status).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3315 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3316 | return; |
| 3317 | } |
| 3318 | |
| 3319 | // Split a motion event if needed. |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3320 | if (inputTarget.flags.test(InputTarget::Flags::SPLIT)) { |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3321 | LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3322 | "Entry type %s should not have Flags::SPLIT", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3323 | ftl::enum_string(eventEntry->type).c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3324 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3325 | const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry); |
Prabir Pradhan | c32a411 | 2024-01-23 23:20:37 +0000 | [diff] [blame] | 3326 | if (inputTarget.getPointerIds().count() != originalMotionEntry.getPointerCount()) { |
Siarhei Vishniakou | 16e4fa0 | 2023-02-16 17:48:56 -0800 | [diff] [blame] | 3327 | if (!inputTarget.firstDownTimeInTarget.has_value()) { |
| 3328 | logDispatchStateLocked(); |
| 3329 | LOG(FATAL) << "Splitting motion events requires a down time to be set for the " |
| 3330 | "target on connection " |
| 3331 | << connection->getInputChannelName() << " for " |
| 3332 | << originalMotionEntry.getDescription(); |
| 3333 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3334 | std::unique_ptr<MotionEntry> splitMotionEntry = |
Prabir Pradhan | c32a411 | 2024-01-23 23:20:37 +0000 | [diff] [blame] | 3335 | splitMotionEvent(originalMotionEntry, inputTarget.getPointerIds(), |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3336 | inputTarget.firstDownTimeInTarget.value()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3337 | if (!splitMotionEntry) { |
| 3338 | return; // split event was dropped |
| 3339 | } |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3340 | if (splitMotionEntry->action == AMOTION_EVENT_ACTION_CANCEL) { |
| 3341 | std::string reason = std::string("reason=pointer cancel on split window"); |
| 3342 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 3343 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 3344 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3345 | if (DEBUG_FOCUS) { |
| 3346 | ALOGD("channel '%s' ~ Split motion event.", |
| 3347 | connection->getInputChannelName().c_str()); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3348 | logOutboundMotionDetails(" ", *splitMotionEntry); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3349 | } |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3350 | enqueueDispatchEntryAndStartDispatchCycleLocked(currentTime, connection, |
| 3351 | std::move(splitMotionEntry), |
| 3352 | inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3353 | return; |
| 3354 | } |
| 3355 | } |
| 3356 | |
| 3357 | // Not splitting. Enqueue dispatch entries for the event as is. |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3358 | enqueueDispatchEntryAndStartDispatchCycleLocked(currentTime, connection, eventEntry, |
| 3359 | inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3360 | } |
| 3361 | |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3362 | void InputDispatcher::enqueueDispatchEntryAndStartDispatchCycleLocked( |
| 3363 | nsecs_t currentTime, const std::shared_ptr<Connection>& connection, |
| 3364 | std::shared_ptr<const EventEntry> eventEntry, const InputTarget& inputTarget) { |
Prabir Pradhan | 2dac8b8 | 2023-09-06 01:11:51 +0000 | [diff] [blame] | 3365 | ATRACE_NAME_IF(ATRACE_ENABLED(), |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3366 | StringPrintf("enqueueDispatchEntryAndStartDispatchCycleLocked(inputChannel=%s, " |
| 3367 | "id=0x%" PRIx32 ")", |
Prabir Pradhan | 2dac8b8 | 2023-09-06 01:11:51 +0000 | [diff] [blame] | 3368 | connection->getInputChannelName().c_str(), eventEntry->id)); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3369 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 3370 | const bool wasEmpty = connection->outboundQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3371 | |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3372 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3373 | |
| 3374 | // If the outbound queue was previously empty, start the dispatch cycle going. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3375 | if (wasEmpty && !connection->outboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3376 | startDispatchCycleLocked(currentTime, connection); |
| 3377 | } |
| 3378 | } |
| 3379 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3380 | void InputDispatcher::enqueueDispatchEntryLocked(const std::shared_ptr<Connection>& connection, |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 3381 | std::shared_ptr<const EventEntry> eventEntry, |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3382 | const InputTarget& inputTarget) { |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 3383 | const bool isKeyOrMotion = eventEntry->type == EventEntry::Type::KEY || |
| 3384 | eventEntry->type == EventEntry::Type::MOTION; |
| 3385 | if (isKeyOrMotion && !inputTarget.windowHandle && !connection->monitor) { |
| 3386 | LOG(FATAL) << "All InputTargets for non-monitors must be associated with a window; target: " |
| 3387 | << inputTarget << " connection: " << connection->getInputChannelName() |
| 3388 | << " entry: " << eventEntry->getDescription(); |
| 3389 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3390 | // This is a new event. |
| 3391 | // Enqueue a new dispatch entry onto the outbound queue for this connection. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3392 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Prabir Pradhan | c88b1fa | 2024-01-23 21:48:03 +0000 | [diff] [blame] | 3393 | createDispatchEntry(mIdGenerator, inputTarget, eventEntry, inputTarget.flags, |
| 3394 | mWindowInfosVsyncId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3395 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3396 | // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a |
| 3397 | // different EventEntry than what was passed in. |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3398 | eventEntry = dispatchEntry->eventEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3399 | // Apply target flags and update the connection's input state. |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3400 | switch (eventEntry->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3401 | case EventEntry::Type::KEY: { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3402 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(*eventEntry); |
| 3403 | if (!connection->inputState.trackKey(keyEntry, keyEntry.flags)) { |
Siarhei Vishniakou | c94dafe | 2023-05-26 10:24:19 -0700 | [diff] [blame] | 3404 | LOG(WARNING) << "channel " << connection->getInputChannelName() |
| 3405 | << "~ dropping inconsistent event: " << *dispatchEntry; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3406 | return; // skip the inconsistent event |
| 3407 | } |
| 3408 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3409 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3410 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3411 | case EventEntry::Type::MOTION: { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3412 | std::shared_ptr<const MotionEntry> resolvedMotion = |
| 3413 | std::static_pointer_cast<const MotionEntry>(eventEntry); |
| 3414 | { |
| 3415 | // Determine the resolved motion entry. |
| 3416 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); |
| 3417 | int32_t resolvedAction = motionEntry.action; |
| 3418 | int32_t resolvedFlags = motionEntry.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3419 | |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3420 | if (inputTarget.dispatchMode == InputTarget::DispatchMode::OUTSIDE) { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3421 | resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3422 | } else if (inputTarget.dispatchMode == InputTarget::DispatchMode::HOVER_EXIT) { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3423 | resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3424 | } else if (inputTarget.dispatchMode == InputTarget::DispatchMode::HOVER_ENTER) { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3425 | resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3426 | } else if (inputTarget.dispatchMode == InputTarget::DispatchMode::SLIPPERY_EXIT) { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3427 | resolvedAction = AMOTION_EVENT_ACTION_CANCEL; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3428 | } else if (inputTarget.dispatchMode == InputTarget::DispatchMode::SLIPPERY_ENTER) { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3429 | resolvedAction = AMOTION_EVENT_ACTION_DOWN; |
| 3430 | } |
| 3431 | if (resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE && |
| 3432 | !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source, |
| 3433 | motionEntry.displayId)) { |
| 3434 | if (DEBUG_DISPATCH_CYCLE) { |
| 3435 | LOG(DEBUG) << "channel '" << connection->getInputChannelName().c_str() |
| 3436 | << "' ~ enqueueDispatchEntryLocked: filling in missing hover " |
| 3437 | "enter event"; |
| 3438 | } |
| 3439 | resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 3440 | } |
| 3441 | |
| 3442 | if (resolvedAction == AMOTION_EVENT_ACTION_CANCEL) { |
| 3443 | resolvedFlags |= AMOTION_EVENT_FLAG_CANCELED; |
| 3444 | } |
| 3445 | if (dispatchEntry->targetFlags.test(InputTarget::Flags::WINDOW_IS_OBSCURED)) { |
| 3446 | resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; |
| 3447 | } |
| 3448 | if (dispatchEntry->targetFlags.test( |
| 3449 | InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED)) { |
| 3450 | resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 3451 | } |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 3452 | if (dispatchEntry->targetFlags.test(InputTarget::Flags::NO_FOCUS_CHANGE)) { |
| 3453 | resolvedFlags |= AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE; |
| 3454 | } |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3455 | |
| 3456 | dispatchEntry->resolvedFlags = resolvedFlags; |
| 3457 | if (resolvedAction != motionEntry.action) { |
Siarhei Vishniakou | e9ef6bc | 2023-12-21 19:47:20 -0800 | [diff] [blame] | 3458 | std::optional<std::vector<PointerProperties>> usingProperties; |
| 3459 | std::optional<std::vector<PointerCoords>> usingCoords; |
| 3460 | if (resolvedAction == AMOTION_EVENT_ACTION_HOVER_EXIT || |
| 3461 | resolvedAction == AMOTION_EVENT_ACTION_CANCEL) { |
| 3462 | // This is a HOVER_EXIT or an ACTION_CANCEL event that was synthesized by |
| 3463 | // the dispatcher, and therefore the coordinates of this event are currently |
| 3464 | // incorrect. These events should use the coordinates of the last dispatched |
| 3465 | // ACTION_MOVE or HOVER_MOVE. We need to query InputState to get this data. |
| 3466 | const bool hovering = resolvedAction == AMOTION_EVENT_ACTION_HOVER_EXIT; |
| 3467 | std::optional<std::pair<std::vector<PointerProperties>, |
| 3468 | std::vector<PointerCoords>>> |
| 3469 | pointerInfo = |
| 3470 | connection->inputState.getPointersOfLastEvent(motionEntry, |
| 3471 | hovering); |
| 3472 | if (pointerInfo) { |
| 3473 | usingProperties = pointerInfo->first; |
| 3474 | usingCoords = pointerInfo->second; |
| 3475 | } |
| 3476 | } |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3477 | // Generate a new MotionEntry with a new eventId using the resolved action and |
| 3478 | // flags. |
Siarhei Vishniakou | e9ef6bc | 2023-12-21 19:47:20 -0800 | [diff] [blame] | 3479 | resolvedMotion = std::make_shared< |
| 3480 | MotionEntry>(mIdGenerator.nextId(), motionEntry.injectionState, |
| 3481 | motionEntry.eventTime, motionEntry.deviceId, |
| 3482 | motionEntry.source, motionEntry.displayId, |
| 3483 | motionEntry.policyFlags, resolvedAction, |
| 3484 | motionEntry.actionButton, resolvedFlags, |
| 3485 | motionEntry.metaState, motionEntry.buttonState, |
| 3486 | motionEntry.classification, motionEntry.edgeFlags, |
| 3487 | motionEntry.xPrecision, motionEntry.yPrecision, |
| 3488 | motionEntry.xCursorPosition, motionEntry.yCursorPosition, |
| 3489 | motionEntry.downTime, |
| 3490 | usingProperties.value_or(motionEntry.pointerProperties), |
| 3491 | usingCoords.value_or(motionEntry.pointerCoords)); |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3492 | if (ATRACE_ENABLED()) { |
| 3493 | std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32 |
| 3494 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3495 | motionEntry.id, resolvedMotion->id); |
| 3496 | ATRACE_NAME(message.c_str()); |
| 3497 | } |
| 3498 | |
| 3499 | // Set the resolved motion entry in the DispatchEntry. |
| 3500 | dispatchEntry->eventEntry = resolvedMotion; |
| 3501 | eventEntry = resolvedMotion; |
| 3502 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3503 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3504 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3505 | // Check if we need to cancel any of the ongoing gestures. We don't support multiple |
| 3506 | // devices being active at the same time in the same window, so if a new device is |
| 3507 | // active, cancel the gesture from the old device. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3508 | std::unique_ptr<EventEntry> cancelEvent = |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3509 | connection->inputState.cancelConflictingInputStream(*resolvedMotion); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3510 | if (cancelEvent != nullptr) { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3511 | LOG(INFO) << "Canceling pointers for device " << resolvedMotion->deviceId << " in " |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3512 | << connection->getInputChannelName() << " with event " |
| 3513 | << cancelEvent->getDescription(); |
| 3514 | std::unique_ptr<DispatchEntry> cancelDispatchEntry = |
Prabir Pradhan | c88b1fa | 2024-01-23 21:48:03 +0000 | [diff] [blame] | 3515 | createDispatchEntry(mIdGenerator, inputTarget, std::move(cancelEvent), |
Prabir Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame] | 3516 | ftl::Flags<InputTarget::Flags>(), mWindowInfosVsyncId); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3517 | |
| 3518 | // Send these cancel events to the queue before sending the event from the new |
| 3519 | // device. |
| 3520 | connection->outboundQueue.emplace_back(std::move(cancelDispatchEntry)); |
| 3521 | } |
| 3522 | |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3523 | if (!connection->inputState.trackMotion(*resolvedMotion, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3524 | dispatchEntry->resolvedFlags)) { |
Siarhei Vishniakou | c94dafe | 2023-05-26 10:24:19 -0700 | [diff] [blame] | 3525 | LOG(WARNING) << "channel " << connection->getInputChannelName() |
| 3526 | << "~ dropping inconsistent event: " << *dispatchEntry; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3527 | return; // skip the inconsistent event |
| 3528 | } |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 3529 | if ((dispatchEntry->resolvedFlags & AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) && |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3530 | (resolvedMotion->policyFlags & POLICY_FLAG_TRUSTED)) { |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 3531 | // Skip reporting pointer down outside focus to the policy. |
| 3532 | break; |
| 3533 | } |
| 3534 | |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3535 | dispatchPointerDownOutsideFocus(resolvedMotion->source, resolvedMotion->action, |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 3536 | inputTarget.connection->getToken()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3537 | |
| 3538 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3539 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3540 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3541 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3542 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3543 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3544 | break; |
| 3545 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3546 | case EventEntry::Type::SENSOR: { |
| 3547 | LOG_ALWAYS_FATAL("SENSOR events should not go to apps via input channel"); |
| 3548 | break; |
| 3549 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3550 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 3551 | case EventEntry::Type::DEVICE_RESET: { |
| 3552 | LOG_ALWAYS_FATAL("%s events should not go to apps", |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3553 | ftl::enum_string(eventEntry->type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3554 | break; |
| 3555 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3556 | } |
| 3557 | |
| 3558 | // Remember that we are waiting for this dispatch to complete. |
| 3559 | if (dispatchEntry->hasForegroundTarget()) { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3560 | incrementPendingForegroundDispatches(*eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3561 | } |
| 3562 | |
| 3563 | // Enqueue the dispatch entry. |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 3564 | connection->outboundQueue.emplace_back(std::move(dispatchEntry)); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3565 | traceOutboundQueueLength(*connection); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3566 | } |
| 3567 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3568 | /** |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3569 | * This function is for debugging and metrics collection. It has two roles. |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3570 | * |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3571 | * The first role is to log input interaction with windows, which helps determine what the user was |
| 3572 | * interacting with. For example, if user is touching launcher, we will see an input_interaction log |
| 3573 | * that user started interacting with launcher window, as well as any other window that received |
| 3574 | * that gesture, such as the wallpaper or other spy windows. A new input_interaction is only logged |
| 3575 | * when the set of tokens that received the event changes. It is not logged again as long as the |
| 3576 | * user is interacting with the same windows. |
| 3577 | * |
| 3578 | * The second role is to track input device activity for metrics collection. For each input event, |
| 3579 | * we report the set of UIDs that the input device interacted with to the policy. Unlike for the |
| 3580 | * input_interaction logs, the device interaction is reported even when the set of interaction |
| 3581 | * tokens do not change. |
| 3582 | * |
| 3583 | * For these purposes, we do not count ACTION_OUTSIDE, ACTION_UP and ACTION_CANCEL actions as |
| 3584 | * interaction. This includes up and cancel events for both keys and motions. |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3585 | */ |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3586 | void InputDispatcher::processInteractionsLocked(const EventEntry& entry, |
| 3587 | const std::vector<InputTarget>& targets) { |
| 3588 | int32_t deviceId; |
| 3589 | nsecs_t eventTime; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3590 | // Skip ACTION_UP events, and all events other than keys and motions |
| 3591 | if (entry.type == EventEntry::Type::KEY) { |
| 3592 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 3593 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
| 3594 | return; |
| 3595 | } |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3596 | deviceId = keyEntry.deviceId; |
| 3597 | eventTime = keyEntry.eventTime; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3598 | } else if (entry.type == EventEntry::Type::MOTION) { |
| 3599 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 3600 | if (motionEntry.action == AMOTION_EVENT_ACTION_UP || |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3601 | motionEntry.action == AMOTION_EVENT_ACTION_CANCEL || |
| 3602 | MotionEvent::getActionMasked(motionEntry.action) == AMOTION_EVENT_ACTION_POINTER_UP) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3603 | return; |
| 3604 | } |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3605 | deviceId = motionEntry.deviceId; |
| 3606 | eventTime = motionEntry.eventTime; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3607 | } else { |
| 3608 | return; // Not a key or a motion |
| 3609 | } |
| 3610 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3611 | std::set<gui::Uid> interactionUids; |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 3612 | std::unordered_set<sp<IBinder>, StrongPointerHash<IBinder>> newConnectionTokens; |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3613 | std::vector<std::shared_ptr<Connection>> newConnections; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3614 | for (const InputTarget& target : targets) { |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3615 | if (target.dispatchMode == InputTarget::DispatchMode::OUTSIDE) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3616 | continue; // Skip windows that receive ACTION_OUTSIDE |
| 3617 | } |
| 3618 | |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 3619 | sp<IBinder> token = target.connection->getToken(); |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3620 | newConnectionTokens.insert(std::move(token)); |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 3621 | newConnections.emplace_back(target.connection); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3622 | if (target.windowHandle) { |
| 3623 | interactionUids.emplace(target.windowHandle->getInfo()->ownerUid); |
| 3624 | } |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3625 | } |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3626 | |
| 3627 | auto command = [this, deviceId, eventTime, uids = std::move(interactionUids)]() |
| 3628 | REQUIRES(mLock) { |
| 3629 | scoped_unlock unlock(mLock); |
| 3630 | mPolicy.notifyDeviceInteraction(deviceId, eventTime, uids); |
| 3631 | }; |
| 3632 | postCommandLocked(std::move(command)); |
| 3633 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3634 | if (newConnectionTokens == mInteractionConnectionTokens) { |
| 3635 | return; // no change |
| 3636 | } |
| 3637 | mInteractionConnectionTokens = newConnectionTokens; |
| 3638 | |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3639 | std::string targetList; |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3640 | for (const std::shared_ptr<Connection>& connection : newConnections) { |
Siarhei Vishniakou | 10bfdc9 | 2024-02-01 11:12:57 -0800 | [diff] [blame] | 3641 | targetList += connection->getInputChannelName() + ", "; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3642 | } |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3643 | std::string message = "Interaction with: " + targetList; |
| 3644 | if (targetList.empty()) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3645 | message += "<none>"; |
| 3646 | } |
| 3647 | android_log_event_list(LOGTAG_INPUT_INTERACTION) << message << LOG_ID_EVENTS; |
| 3648 | } |
| 3649 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3650 | void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3651 | const sp<IBinder>& token) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3652 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3653 | uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK; |
| 3654 | if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3655 | return; |
| 3656 | } |
| 3657 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 3658 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3659 | if (focusedToken == token) { |
| 3660 | // ignore since token is focused |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3661 | return; |
| 3662 | } |
| 3663 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3664 | auto command = [this, token]() REQUIRES(mLock) { |
| 3665 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 3666 | mPolicy.onPointerDownOutsideFocus(token); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3667 | }; |
| 3668 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3669 | } |
| 3670 | |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3671 | status_t InputDispatcher::publishMotionEvent(Connection& connection, |
| 3672 | DispatchEntry& dispatchEntry) const { |
| 3673 | const EventEntry& eventEntry = *(dispatchEntry.eventEntry); |
| 3674 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 3675 | |
| 3676 | PointerCoords scaledCoords[MAX_POINTERS]; |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 3677 | const PointerCoords* usingCoords = motionEntry.pointerCoords.data(); |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3678 | |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 3679 | // TODO(b/316355518): Do not modify coords before dispatch. |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3680 | // Set the X and Y offset and X and Y scale depending on the input source. |
| 3681 | if ((motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) && |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3682 | !(dispatchEntry.targetFlags.test(InputTarget::Flags::ZERO_COORDS))) { |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3683 | float globalScaleFactor = dispatchEntry.globalScaleFactor; |
| 3684 | if (globalScaleFactor != 1.0f) { |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 3685 | for (uint32_t i = 0; i < motionEntry.getPointerCount(); i++) { |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3686 | scaledCoords[i] = motionEntry.pointerCoords[i]; |
| 3687 | // Don't apply window scale here since we don't want scale to affect raw |
| 3688 | // coordinates. The scale will be sent back to the client and applied |
| 3689 | // later when requesting relative coordinates. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 3690 | scaledCoords[i].scale(globalScaleFactor, /*windowXScale=*/1, /*windowYScale=*/1); |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3691 | } |
| 3692 | usingCoords = scaledCoords; |
| 3693 | } |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3694 | } |
| 3695 | |
| 3696 | std::array<uint8_t, 32> hmac = getSignature(motionEntry, dispatchEntry); |
| 3697 | |
| 3698 | // Publish the motion event. |
| 3699 | return connection.inputPublisher |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3700 | .publishMotionEvent(dispatchEntry.seq, motionEntry.id, motionEntry.deviceId, |
| 3701 | motionEntry.source, motionEntry.displayId, std::move(hmac), |
| 3702 | motionEntry.action, motionEntry.actionButton, |
| 3703 | dispatchEntry.resolvedFlags, motionEntry.edgeFlags, |
| 3704 | motionEntry.metaState, motionEntry.buttonState, |
| 3705 | motionEntry.classification, dispatchEntry.transform, |
| 3706 | motionEntry.xPrecision, motionEntry.yPrecision, |
| 3707 | motionEntry.xCursorPosition, motionEntry.yCursorPosition, |
| 3708 | dispatchEntry.rawTransform, motionEntry.downTime, |
| 3709 | motionEntry.eventTime, motionEntry.getPointerCount(), |
| 3710 | motionEntry.pointerProperties.data(), usingCoords); |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3711 | } |
| 3712 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3713 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3714 | const std::shared_ptr<Connection>& connection) { |
Prabir Pradhan | 2dac8b8 | 2023-09-06 01:11:51 +0000 | [diff] [blame] | 3715 | ATRACE_NAME_IF(ATRACE_ENABLED(), |
| 3716 | StringPrintf("startDispatchCycleLocked(inputChannel=%s)", |
| 3717 | connection->getInputChannelName().c_str())); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3718 | if (DEBUG_DISPATCH_CYCLE) { |
| 3719 | ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str()); |
| 3720 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3721 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3722 | while (connection->status == Connection::Status::NORMAL && !connection->outboundQueue.empty()) { |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 3723 | std::unique_ptr<DispatchEntry>& dispatchEntry = connection->outboundQueue.front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3724 | dispatchEntry->deliveryTime = currentTime; |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3725 | const std::chrono::nanoseconds timeout = getDispatchingTimeoutLocked(connection); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3726 | dispatchEntry->timeoutTime = currentTime + timeout.count(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3727 | |
| 3728 | // Publish the event. |
| 3729 | status_t status; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3730 | const EventEntry& eventEntry = *(dispatchEntry->eventEntry); |
| 3731 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3732 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3733 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 3734 | std::array<uint8_t, 32> hmac = getSignature(keyEntry, *dispatchEntry); |
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 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3739 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3740 | // Publish the key event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3741 | status = connection->inputPublisher |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3742 | .publishKeyEvent(dispatchEntry->seq, keyEntry.id, |
| 3743 | keyEntry.deviceId, keyEntry.source, |
| 3744 | keyEntry.displayId, std::move(hmac), |
| 3745 | keyEntry.action, dispatchEntry->resolvedFlags, |
| 3746 | keyEntry.keyCode, keyEntry.scanCode, |
| 3747 | keyEntry.metaState, keyEntry.repeatCount, |
| 3748 | keyEntry.downTime, keyEntry.eventTime); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 3749 | if (mTracer) { |
| 3750 | mTracer->traceEventDispatch(*dispatchEntry, keyEntry.traceTracker.get()); |
| 3751 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3752 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3753 | } |
| 3754 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3755 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 3756 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
Siarhei Vishniakou | 827d1ac | 2023-07-21 16:37:51 -0700 | [diff] [blame] | 3757 | LOG(INFO) << "Publishing " << *dispatchEntry << " to " |
| 3758 | << connection->getInputChannelName(); |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 3759 | } |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 3760 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3761 | status = publishMotionEvent(*connection, *dispatchEntry); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 3762 | if (mTracer) { |
| 3763 | mTracer->traceEventDispatch(*dispatchEntry, motionEntry.traceTracker.get()); |
| 3764 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3765 | break; |
| 3766 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3767 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3768 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3769 | const FocusEntry& focusEntry = static_cast<const FocusEntry&>(eventEntry); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3770 | status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3771 | focusEntry.id, |
Antonio Kantek | 3cfec7b | 2021-11-05 18:26:17 -0700 | [diff] [blame] | 3772 | focusEntry.hasFocus); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3773 | break; |
| 3774 | } |
| 3775 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3776 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 3777 | const TouchModeEntry& touchModeEntry = |
| 3778 | static_cast<const TouchModeEntry&>(eventEntry); |
| 3779 | status = connection->inputPublisher |
| 3780 | .publishTouchModeEvent(dispatchEntry->seq, touchModeEntry.id, |
| 3781 | touchModeEntry.inTouchMode); |
| 3782 | |
| 3783 | break; |
| 3784 | } |
| 3785 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3786 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 3787 | const auto& captureEntry = |
| 3788 | static_cast<const PointerCaptureChangedEntry&>(eventEntry); |
| 3789 | status = connection->inputPublisher |
| 3790 | .publishCaptureEvent(dispatchEntry->seq, captureEntry.id, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 3791 | captureEntry.pointerCaptureRequest.enable); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3792 | break; |
| 3793 | } |
| 3794 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3795 | case EventEntry::Type::DRAG: { |
| 3796 | const DragEntry& dragEntry = static_cast<const DragEntry&>(eventEntry); |
| 3797 | status = connection->inputPublisher.publishDragEvent(dispatchEntry->seq, |
| 3798 | dragEntry.id, dragEntry.x, |
| 3799 | dragEntry.y, |
| 3800 | dragEntry.isExiting); |
| 3801 | break; |
| 3802 | } |
| 3803 | |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3804 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3805 | case EventEntry::Type::DEVICE_RESET: |
| 3806 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3807 | LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3808 | ftl::enum_string(eventEntry.type).c_str()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3809 | return; |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3810 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3811 | } |
| 3812 | |
| 3813 | // Check the result. |
| 3814 | if (status) { |
| 3815 | if (status == WOULD_BLOCK) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3816 | if (connection->waitQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3817 | ALOGE("channel '%s' ~ Could not publish event because the pipe is full. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3818 | "This is unexpected because the wait queue is empty, so the pipe " |
| 3819 | "should be empty and we shouldn't have any problems writing an " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3820 | "event to it, status=%s(%d)", |
| 3821 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3822 | status); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 3823 | abortBrokenDispatchCycleLocked(currentTime, connection, /*notify=*/true); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3824 | } else { |
| 3825 | // Pipe is full and we are waiting for the app to finish process some events |
| 3826 | // before sending more events to it. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3827 | if (DEBUG_DISPATCH_CYCLE) { |
| 3828 | ALOGD("channel '%s' ~ Could not publish event because the pipe is full, " |
| 3829 | "waiting for the application to catch up", |
| 3830 | connection->getInputChannelName().c_str()); |
| 3831 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3832 | } |
| 3833 | } else { |
| 3834 | ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3835 | "status=%s(%d)", |
| 3836 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3837 | status); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 3838 | abortBrokenDispatchCycleLocked(currentTime, connection, /*notify=*/true); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3839 | } |
| 3840 | return; |
| 3841 | } |
| 3842 | |
| 3843 | // Re-enqueue the event on the wait queue. |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 3844 | const nsecs_t timeoutTime = dispatchEntry->timeoutTime; |
| 3845 | connection->waitQueue.emplace_back(std::move(dispatchEntry)); |
| 3846 | connection->outboundQueue.erase(connection->outboundQueue.begin()); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3847 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3848 | if (connection->responsive) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 3849 | mAnrTracker.insert(timeoutTime, connection->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3850 | } |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3851 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3852 | } |
| 3853 | } |
| 3854 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3855 | std::array<uint8_t, 32> InputDispatcher::sign(const VerifiedInputEvent& event) const { |
| 3856 | size_t size; |
| 3857 | switch (event.type) { |
| 3858 | case VerifiedInputEvent::Type::KEY: { |
| 3859 | size = sizeof(VerifiedKeyEvent); |
| 3860 | break; |
| 3861 | } |
| 3862 | case VerifiedInputEvent::Type::MOTION: { |
| 3863 | size = sizeof(VerifiedMotionEvent); |
| 3864 | break; |
| 3865 | } |
| 3866 | } |
| 3867 | const uint8_t* start = reinterpret_cast<const uint8_t*>(&event); |
| 3868 | return mHmacKeyManager.sign(start, size); |
| 3869 | } |
| 3870 | |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3871 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3872 | const MotionEntry& motionEntry, const DispatchEntry& dispatchEntry) const { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3873 | const int32_t actionMasked = MotionEvent::getActionMasked(motionEntry.action); |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3874 | if (actionMasked != AMOTION_EVENT_ACTION_UP && actionMasked != AMOTION_EVENT_ACTION_DOWN) { |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3875 | // Only sign events up and down events as the purely move events |
| 3876 | // are tied to their up/down counterparts so signing would be redundant. |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3877 | return INVALID_HMAC; |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3878 | } |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3879 | |
| 3880 | VerifiedMotionEvent verifiedEvent = |
| 3881 | verifiedMotionEventFromMotionEntry(motionEntry, dispatchEntry.rawTransform); |
| 3882 | verifiedEvent.actionMasked = actionMasked; |
| 3883 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS; |
| 3884 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3885 | } |
| 3886 | |
| 3887 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3888 | const KeyEntry& keyEntry, const DispatchEntry& dispatchEntry) const { |
| 3889 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(keyEntry); |
| 3890 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_KEY_EVENT_FLAGS; |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3891 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3892 | } |
| 3893 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3894 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3895 | const std::shared_ptr<Connection>& connection, |
| 3896 | uint32_t seq, bool handled, nsecs_t consumeTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3897 | if (DEBUG_DISPATCH_CYCLE) { |
| 3898 | ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s", |
| 3899 | connection->getInputChannelName().c_str(), seq, toString(handled)); |
| 3900 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3901 | |
Prabir Pradhan | 98ca4a2 | 2024-01-09 23:51:50 +0000 | [diff] [blame] | 3902 | if (connection->status != Connection::Status::NORMAL) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3903 | return; |
| 3904 | } |
| 3905 | |
| 3906 | // Notify other system components and prepare to start the next dispatch cycle. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3907 | auto command = [this, currentTime, connection, seq, handled, consumeTime]() REQUIRES(mLock) { |
| 3908 | doDispatchCycleFinishedCommand(currentTime, connection, seq, handled, consumeTime); |
| 3909 | }; |
| 3910 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3911 | } |
| 3912 | |
| 3913 | void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3914 | const std::shared_ptr<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3915 | bool notify) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3916 | if (DEBUG_DISPATCH_CYCLE) { |
Siarhei Vishniakou | 827d1ac | 2023-07-21 16:37:51 -0700 | [diff] [blame] | 3917 | LOG(INFO) << "channel '" << connection->getInputChannelName() << "'~ " << __func__ |
| 3918 | << " - notify=" << toString(notify); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3919 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3920 | |
| 3921 | // Clear the dispatch queues. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3922 | drainDispatchQueue(connection->outboundQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3923 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3924 | drainDispatchQueue(connection->waitQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3925 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3926 | |
| 3927 | // The connection appears to be unrecoverably broken. |
| 3928 | // Ignore already broken or zombie connections. |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3929 | if (connection->status == Connection::Status::NORMAL) { |
| 3930 | connection->status = Connection::Status::BROKEN; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3931 | |
| 3932 | if (notify) { |
| 3933 | // Notify other system components. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3934 | ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", |
| 3935 | connection->getInputChannelName().c_str()); |
| 3936 | |
| 3937 | auto command = [this, connection]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3938 | scoped_unlock unlock(mLock); |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 3939 | mPolicy.notifyInputChannelBroken(connection->getToken()); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3940 | }; |
| 3941 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3942 | } |
| 3943 | } |
| 3944 | } |
| 3945 | |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 3946 | void InputDispatcher::drainDispatchQueue(std::deque<std::unique_ptr<DispatchEntry>>& queue) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3947 | while (!queue.empty()) { |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 3948 | releaseDispatchEntry(std::move(queue.front())); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3949 | queue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3950 | } |
| 3951 | } |
| 3952 | |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 3953 | void InputDispatcher::releaseDispatchEntry(std::unique_ptr<DispatchEntry> dispatchEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3954 | if (dispatchEntry->hasForegroundTarget()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3955 | decrementPendingForegroundDispatches(*(dispatchEntry->eventEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3956 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3957 | } |
| 3958 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3959 | int InputDispatcher::handleReceiveCallback(int events, sp<IBinder> connectionToken) { |
| 3960 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3961 | std::shared_ptr<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3962 | if (connection == nullptr) { |
| 3963 | ALOGW("Received looper callback for unknown input channel token %p. events=0x%x", |
| 3964 | connectionToken.get(), events); |
| 3965 | return 0; // remove the callback |
| 3966 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3967 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3968 | bool notify; |
| 3969 | if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { |
| 3970 | if (!(events & ALOOPER_EVENT_INPUT)) { |
| 3971 | ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. " |
| 3972 | "events=0x%x", |
| 3973 | connection->getInputChannelName().c_str(), events); |
| 3974 | return 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3975 | } |
| 3976 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3977 | nsecs_t currentTime = now(); |
| 3978 | bool gotOne = false; |
| 3979 | status_t status = OK; |
| 3980 | for (;;) { |
| 3981 | Result<InputPublisher::ConsumerResponse> result = |
| 3982 | connection->inputPublisher.receiveConsumerResponse(); |
| 3983 | if (!result.ok()) { |
| 3984 | status = result.error().code(); |
| 3985 | break; |
| 3986 | } |
| 3987 | |
| 3988 | if (std::holds_alternative<InputPublisher::Finished>(*result)) { |
| 3989 | const InputPublisher::Finished& finish = |
| 3990 | std::get<InputPublisher::Finished>(*result); |
| 3991 | finishDispatchCycleLocked(currentTime, connection, finish.seq, finish.handled, |
| 3992 | finish.consumeTime); |
| 3993 | } else if (std::holds_alternative<InputPublisher::Timeline>(*result)) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3994 | if (shouldReportMetricsForConnection(*connection)) { |
| 3995 | const InputPublisher::Timeline& timeline = |
| 3996 | std::get<InputPublisher::Timeline>(*result); |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 3997 | mLatencyTracker.trackGraphicsLatency(timeline.inputEventId, |
| 3998 | connection->getToken(), |
| 3999 | std::move(timeline.graphicsTimeline)); |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 4000 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4001 | } |
| 4002 | gotOne = true; |
| 4003 | } |
| 4004 | if (gotOne) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 4005 | runCommandsLockedInterruptable(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4006 | if (status == WOULD_BLOCK) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4007 | return 1; |
| 4008 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4009 | } |
| 4010 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4011 | notify = status != DEAD_OBJECT || !connection->monitor; |
| 4012 | if (notify) { |
| 4013 | ALOGE("channel '%s' ~ Failed to receive finished signal. status=%s(%d)", |
| 4014 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 4015 | status); |
| 4016 | } |
| 4017 | } else { |
| 4018 | // Monitor channels are never explicitly unregistered. |
| 4019 | // 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] | 4020 | const bool stillHaveWindowHandle = getWindowHandleLocked(connection->getToken()) != nullptr; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4021 | notify = !connection->monitor && stillHaveWindowHandle; |
| 4022 | if (notify) { |
| 4023 | ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred. events=0x%x", |
| 4024 | connection->getInputChannelName().c_str(), events); |
| 4025 | } |
| 4026 | } |
| 4027 | |
| 4028 | // Remove the channel. |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 4029 | removeInputChannelLocked(connection->getToken(), notify); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4030 | return 0; // remove the callback |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4031 | } |
| 4032 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4033 | void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4034 | const CancelationOptions& options) { |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 4035 | // Cancel windows (i.e. non-monitors). |
| 4036 | // A channel must have at least one window to receive any input. If a window was removed, the |
| 4037 | // event streams directed to the window will already have been canceled during window removal. |
| 4038 | // So there is no need to generate cancellations for connections without any windows. |
| 4039 | const auto [cancelPointers, cancelNonPointers] = expandCancellationMode(options.mode); |
| 4040 | // Generate cancellations for touched windows first. This is to avoid generating cancellations |
| 4041 | // through a non-touched window if there are more than one window for an input channel. |
| 4042 | if (cancelPointers) { |
| 4043 | for (const auto& [displayId, touchState] : mTouchStatesByDisplay) { |
| 4044 | if (options.displayId.has_value() && options.displayId != displayId) { |
| 4045 | continue; |
| 4046 | } |
| 4047 | for (const auto& touchedWindow : touchState.windows) { |
| 4048 | synthesizeCancelationEventsForWindowLocked(touchedWindow.windowHandle, options); |
| 4049 | } |
| 4050 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4051 | } |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 4052 | // Follow up by generating cancellations for all windows, because we don't explicitly track |
| 4053 | // the windows that have an ongoing focus event stream. |
| 4054 | if (cancelNonPointers) { |
| 4055 | for (const auto& [_, handles] : mWindowHandlesByDisplay) { |
| 4056 | for (const auto& windowHandle : handles) { |
| 4057 | synthesizeCancelationEventsForWindowLocked(windowHandle, options); |
| 4058 | } |
| 4059 | } |
| 4060 | } |
| 4061 | |
| 4062 | // Cancel monitors. |
| 4063 | synthesizeCancelationEventsForMonitorsLocked(options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4064 | } |
| 4065 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4066 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 4067 | const CancelationOptions& options) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 4068 | for (const auto& [_, monitors] : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4069 | for (const Monitor& monitor : monitors) { |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 4070 | synthesizeCancelationEventsForConnectionLocked(monitor.connection, options, |
| 4071 | /*window=*/nullptr); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4072 | } |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 4073 | } |
| 4074 | } |
| 4075 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 4076 | void InputDispatcher::synthesizeCancelationEventsForWindowLocked( |
| 4077 | const sp<WindowInfoHandle>& windowHandle, const CancelationOptions& options, |
| 4078 | const std::shared_ptr<Connection>& connection) { |
| 4079 | if (windowHandle == nullptr) { |
| 4080 | LOG(FATAL) << __func__ << ": Window handle must not be null"; |
| 4081 | } |
| 4082 | if (connection) { |
| 4083 | // The connection can be optionally provided to avoid multiple lookups. |
| 4084 | if (windowHandle->getToken() != connection->getToken()) { |
| 4085 | LOG(FATAL) << __func__ |
| 4086 | << ": Wrong connection provided for window: " << windowHandle->getName(); |
| 4087 | } |
| 4088 | } |
| 4089 | |
| 4090 | std::shared_ptr<Connection> resolvedConnection = |
| 4091 | connection ? connection : getConnectionLocked(windowHandle->getToken()); |
| 4092 | if (!resolvedConnection) { |
| 4093 | LOG(DEBUG) << __func__ << "No connection found for window: " << windowHandle->getName(); |
| 4094 | return; |
| 4095 | } |
| 4096 | synthesizeCancelationEventsForConnectionLocked(resolvedConnection, options, windowHandle); |
| 4097 | } |
| 4098 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4099 | void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 4100 | const std::shared_ptr<Connection>& connection, const CancelationOptions& options, |
| 4101 | const sp<WindowInfoHandle>& window) { |
| 4102 | if (!connection->monitor && window == nullptr) { |
| 4103 | LOG(FATAL) << __func__ |
| 4104 | << ": Cannot send event to non-monitor channel without a window - channel: " |
| 4105 | << connection->getInputChannelName(); |
| 4106 | } |
Prabir Pradhan | b13da8f | 2024-01-09 23:10:13 +0000 | [diff] [blame] | 4107 | if (connection->status != Connection::Status::NORMAL) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4108 | return; |
| 4109 | } |
| 4110 | |
| 4111 | nsecs_t currentTime = now(); |
| 4112 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4113 | std::vector<std::unique_ptr<EventEntry>> cancelationEvents = |
Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 4114 | connection->inputState.synthesizeCancelationEvents(currentTime, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4115 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4116 | if (cancelationEvents.empty()) { |
| 4117 | return; |
| 4118 | } |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 4119 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4120 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 4121 | 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] | 4122 | "with reality: %s, mode=%s.", |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4123 | connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason, |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 4124 | ftl::enum_string(options.mode).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4125 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4126 | |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 4127 | std::string reason = std::string("reason=").append(options.reason); |
| 4128 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 4129 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 4130 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 4131 | const bool wasEmpty = connection->outboundQueue.empty(); |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 4132 | // The target to use if we don't find a window associated with the channel. |
Prabir Pradhan | c32a411 | 2024-01-23 23:20:37 +0000 | [diff] [blame] | 4133 | const InputTarget fallbackTarget{connection}; |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 4134 | const auto& token = connection->getToken(); |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 4135 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4136 | for (size_t i = 0; i < cancelationEvents.size(); i++) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4137 | std::unique_ptr<EventEntry> cancelationEventEntry = std::move(cancelationEvents[i]); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4138 | std::vector<InputTarget> targets{}; |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4139 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4140 | switch (cancelationEventEntry->type) { |
| 4141 | case EventEntry::Type::KEY: { |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4142 | const auto& keyEntry = static_cast<const KeyEntry&>(*cancelationEventEntry); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 4143 | if (window) { |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 4144 | addWindowTargetLocked(window, InputTarget::DispatchMode::AS_IS, |
| 4145 | /*targetFlags=*/{}, keyEntry.downTime, targets); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4146 | } else { |
| 4147 | targets.emplace_back(fallbackTarget); |
| 4148 | } |
| 4149 | logOutboundKeyDetails("cancel - ", keyEntry); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4150 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4151 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4152 | case EventEntry::Type::MOTION: { |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4153 | const auto& motionEntry = static_cast<const MotionEntry&>(*cancelationEventEntry); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 4154 | if (window) { |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4155 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4156 | for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.getPointerCount(); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4157 | pointerIndex++) { |
| 4158 | pointerIds.set(motionEntry.pointerProperties[pointerIndex].id); |
| 4159 | } |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 4160 | if (mDragState && mDragState->dragWindow->getToken() == token && |
| 4161 | pointerIds.test(mDragState->pointerId)) { |
| 4162 | LOG(INFO) << __func__ |
| 4163 | << ": Canceling drag and drop because the pointers for the drag " |
| 4164 | "window are being canceled."; |
| 4165 | sendDropWindowCommandLocked(nullptr, /*x=*/0, /*y=*/0); |
| 4166 | mDragState.reset(); |
| 4167 | } |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 4168 | addPointerWindowTargetLocked(window, InputTarget::DispatchMode::AS_IS, |
| 4169 | ftl::Flags<InputTarget::Flags>(), pointerIds, |
| 4170 | motionEntry.downTime, targets); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4171 | } else { |
| 4172 | targets.emplace_back(fallbackTarget); |
| 4173 | const auto it = mDisplayInfos.find(motionEntry.displayId); |
| 4174 | if (it != mDisplayInfos.end()) { |
| 4175 | targets.back().displayTransform = it->second.transform; |
| 4176 | targets.back().setDefaultPointerTransform(it->second.transform); |
| 4177 | } |
| 4178 | } |
| 4179 | logOutboundMotionDetails("cancel - ", motionEntry); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4180 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4181 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4182 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 4183 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4184 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 4185 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4186 | LOG_ALWAYS_FATAL("Canceling %s events is not supported", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 4187 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4188 | break; |
| 4189 | } |
| 4190 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4191 | case EventEntry::Type::DEVICE_RESET: |
| 4192 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4193 | 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] | 4194 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4195 | break; |
| 4196 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4197 | } |
| 4198 | |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4199 | if (targets.size() != 1) LOG(FATAL) << __func__ << ": InputTarget not created"; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 4200 | enqueueDispatchEntryLocked(connection, std::move(cancelationEventEntry), targets[0]); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4201 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4202 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 4203 | // If the outbound queue was previously empty, start the dispatch cycle going. |
| 4204 | if (wasEmpty && !connection->outboundQueue.empty()) { |
| 4205 | startDispatchCycleLocked(currentTime, connection); |
| 4206 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4207 | } |
| 4208 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4209 | void InputDispatcher::synthesizePointerDownEventsForConnectionLocked( |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 4210 | const nsecs_t downTime, const std::shared_ptr<Connection>& connection, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4211 | ftl::Flags<InputTarget::Flags> targetFlags) { |
Prabir Pradhan | 98ca4a2 | 2024-01-09 23:51:50 +0000 | [diff] [blame] | 4212 | if (connection->status != Connection::Status::NORMAL) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4213 | return; |
| 4214 | } |
| 4215 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4216 | std::vector<std::unique_ptr<EventEntry>> downEvents = |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4217 | connection->inputState.synthesizePointerDownEvents(downTime); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4218 | |
| 4219 | if (downEvents.empty()) { |
| 4220 | return; |
| 4221 | } |
| 4222 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4223 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4224 | ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.", |
| 4225 | connection->getInputChannelName().c_str(), downEvents.size()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4226 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4227 | |
Prabir Pradhan | feca057 | 2024-02-06 00:43:11 +0000 | [diff] [blame] | 4228 | const auto [_, touchedWindowState, displayId] = |
| 4229 | findTouchStateWindowAndDisplayLocked(connection->getToken()); |
| 4230 | if (touchedWindowState == nullptr) { |
| 4231 | LOG(FATAL) << __func__ << ": Touch state is out of sync: No touched window for token"; |
| 4232 | } |
| 4233 | const auto& windowHandle = touchedWindowState->windowHandle; |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4234 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 4235 | const bool wasEmpty = connection->outboundQueue.empty(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4236 | for (std::unique_ptr<EventEntry>& downEventEntry : downEvents) { |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 4237 | std::vector<InputTarget> targets{}; |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4238 | switch (downEventEntry->type) { |
| 4239 | case EventEntry::Type::MOTION: { |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 4240 | const auto& motionEntry = static_cast<const MotionEntry&>(*downEventEntry); |
| 4241 | if (windowHandle != nullptr) { |
| 4242 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4243 | for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.getPointerCount(); |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 4244 | pointerIndex++) { |
| 4245 | pointerIds.set(motionEntry.pointerProperties[pointerIndex].id); |
| 4246 | } |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 4247 | addPointerWindowTargetLocked(windowHandle, InputTarget::DispatchMode::AS_IS, |
| 4248 | targetFlags, pointerIds, motionEntry.downTime, |
| 4249 | targets); |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 4250 | } else { |
Prabir Pradhan | c32a411 | 2024-01-23 23:20:37 +0000 | [diff] [blame] | 4251 | targets.emplace_back(connection, targetFlags); |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 4252 | const auto it = mDisplayInfos.find(motionEntry.displayId); |
| 4253 | if (it != mDisplayInfos.end()) { |
| 4254 | targets.back().displayTransform = it->second.transform; |
| 4255 | targets.back().setDefaultPointerTransform(it->second.transform); |
| 4256 | } |
| 4257 | } |
| 4258 | logOutboundMotionDetails("down - ", motionEntry); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4259 | break; |
| 4260 | } |
| 4261 | |
| 4262 | case EventEntry::Type::KEY: |
| 4263 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 4264 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4265 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4266 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4267 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4268 | case EventEntry::Type::SENSOR: |
| 4269 | case EventEntry::Type::DRAG: { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4270 | 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] | 4271 | ftl::enum_string(downEventEntry->type).c_str()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4272 | break; |
| 4273 | } |
| 4274 | } |
| 4275 | |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 4276 | if (targets.size() != 1) LOG(FATAL) << __func__ << ": InputTarget not created"; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 4277 | enqueueDispatchEntryLocked(connection, std::move(downEventEntry), targets[0]); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4278 | } |
| 4279 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 4280 | // If the outbound queue was previously empty, start the dispatch cycle going. |
| 4281 | if (wasEmpty && !connection->outboundQueue.empty()) { |
| 4282 | startDispatchCycleLocked(downTime, connection); |
| 4283 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4284 | } |
| 4285 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4286 | std::unique_ptr<MotionEntry> InputDispatcher::splitMotionEvent( |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 4287 | const MotionEntry& originalMotionEntry, std::bitset<MAX_POINTER_ID + 1> pointerIds, |
| 4288 | nsecs_t splitDownTime) { |
| 4289 | ALOG_ASSERT(pointerIds.any()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4290 | |
| 4291 | uint32_t splitPointerIndexMap[MAX_POINTERS]; |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4292 | std::vector<PointerProperties> splitPointerProperties; |
| 4293 | std::vector<PointerCoords> splitPointerCoords; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4294 | |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4295 | uint32_t originalPointerCount = originalMotionEntry.getPointerCount(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4296 | uint32_t splitPointerCount = 0; |
| 4297 | |
| 4298 | for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4299 | originalPointerIndex++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4300 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 4301 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4302 | uint32_t pointerId = uint32_t(pointerProperties.id); |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 4303 | if (pointerIds.test(pointerId)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4304 | splitPointerIndexMap[splitPointerCount] = originalPointerIndex; |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4305 | splitPointerProperties.push_back(pointerProperties); |
| 4306 | splitPointerCoords.push_back(originalMotionEntry.pointerCoords[originalPointerIndex]); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4307 | splitPointerCount += 1; |
| 4308 | } |
| 4309 | } |
| 4310 | |
| 4311 | if (splitPointerCount != pointerIds.count()) { |
| 4312 | // This is bad. We are missing some of the pointers that we expected to deliver. |
| 4313 | // Most likely this indicates that we received an ACTION_MOVE events that has |
| 4314 | // different pointer ids than we expected based on the previous ACTION_DOWN |
| 4315 | // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers |
| 4316 | // in this way. |
| 4317 | ALOGW("Dropping split motion event because the pointer count is %d but " |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 4318 | "we expected there to be %zu pointers. This probably means we received " |
Siarhei Vishniakou | 16e4fa0 | 2023-02-16 17:48:56 -0800 | [diff] [blame] | 4319 | "a broken sequence of pointer ids from the input device: %s", |
| 4320 | splitPointerCount, pointerIds.count(), originalMotionEntry.getDescription().c_str()); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4321 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4322 | } |
| 4323 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 4324 | int32_t action = originalMotionEntry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4325 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4326 | if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN || |
| 4327 | maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
Siarhei Vishniakou | 5b9766d | 2023-07-18 14:06:29 -0700 | [diff] [blame] | 4328 | int32_t originalPointerIndex = MotionEvent::getActionIndex(action); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4329 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 4330 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4331 | uint32_t pointerId = uint32_t(pointerProperties.id); |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 4332 | if (pointerIds.test(pointerId)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4333 | if (pointerIds.count() == 1) { |
| 4334 | // The first/last pointer went down/up. |
| 4335 | action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4336 | ? AMOTION_EVENT_ACTION_DOWN |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4337 | : (originalMotionEntry.flags & AMOTION_EVENT_FLAG_CANCELED) != 0 |
| 4338 | ? AMOTION_EVENT_ACTION_CANCEL |
| 4339 | : AMOTION_EVENT_ACTION_UP; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4340 | } else { |
| 4341 | // A secondary pointer went down/up. |
| 4342 | uint32_t splitPointerIndex = 0; |
| 4343 | while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { |
| 4344 | splitPointerIndex += 1; |
| 4345 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4346 | action = maskedAction | |
| 4347 | (splitPointerIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4348 | } |
| 4349 | } else { |
| 4350 | // An unrelated pointer changed. |
| 4351 | action = AMOTION_EVENT_ACTION_MOVE; |
| 4352 | } |
| 4353 | } |
| 4354 | |
Siarhei Vishniakou | 59e302b | 2023-06-05 08:04:53 -0700 | [diff] [blame] | 4355 | if (action == AMOTION_EVENT_ACTION_DOWN && splitDownTime != originalMotionEntry.eventTime) { |
| 4356 | logDispatchStateLocked(); |
| 4357 | LOG_ALWAYS_FATAL("Split motion event has mismatching downTime and eventTime for " |
| 4358 | "ACTION_DOWN, motionEntry=%s, splitDownTime=%" PRId64, |
| 4359 | originalMotionEntry.getDescription().c_str(), splitDownTime); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4360 | } |
| 4361 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4362 | int32_t newId = mIdGenerator.nextId(); |
Prabir Pradhan | 2dac8b8 | 2023-09-06 01:11:51 +0000 | [diff] [blame] | 4363 | ATRACE_NAME_IF(ATRACE_ENABLED(), |
| 4364 | StringPrintf("Split MotionEvent(id=0x%" PRIx32 ") to MotionEvent(id=0x%" PRIx32 |
| 4365 | ").", |
| 4366 | originalMotionEntry.id, newId)); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4367 | std::unique_ptr<MotionEntry> splitMotionEntry = |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4368 | std::make_unique<MotionEntry>(newId, originalMotionEntry.injectionState, |
| 4369 | originalMotionEntry.eventTime, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4370 | originalMotionEntry.deviceId, originalMotionEntry.source, |
| 4371 | originalMotionEntry.displayId, |
| 4372 | originalMotionEntry.policyFlags, action, |
| 4373 | originalMotionEntry.actionButton, |
| 4374 | originalMotionEntry.flags, originalMotionEntry.metaState, |
| 4375 | originalMotionEntry.buttonState, |
| 4376 | originalMotionEntry.classification, |
| 4377 | originalMotionEntry.edgeFlags, |
| 4378 | originalMotionEntry.xPrecision, |
| 4379 | originalMotionEntry.yPrecision, |
| 4380 | originalMotionEntry.xCursorPosition, |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4381 | originalMotionEntry.yCursorPosition, splitDownTime, |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4382 | splitPointerProperties, splitPointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4383 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4384 | return splitMotionEntry; |
| 4385 | } |
| 4386 | |
Asmita Poddar | dd9a6cd | 2023-09-26 15:35:12 +0000 | [diff] [blame] | 4387 | void InputDispatcher::notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs& args) { |
| 4388 | std::scoped_lock _l(mLock); |
| 4389 | mLatencyTracker.setInputDevices(args.inputDeviceInfos); |
| 4390 | } |
| 4391 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4392 | void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4393 | if (debugInboundEventDetails()) { |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4394 | ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args.eventTime); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4395 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4396 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4397 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4398 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4399 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4400 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4401 | std::unique_ptr<ConfigurationChangedEntry> newEntry = |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4402 | std::make_unique<ConfigurationChangedEntry>(args.id, args.eventTime); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4403 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4404 | } // release lock |
| 4405 | |
| 4406 | if (needWake) { |
| 4407 | mLooper->wake(); |
| 4408 | } |
| 4409 | } |
| 4410 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4411 | void InputDispatcher::notifyKey(const NotifyKeyArgs& args) { |
Prabir Pradhan | 96282b0 | 2023-02-24 22:36:17 +0000 | [diff] [blame] | 4412 | ALOGD_IF(debugInboundEventDetails(), |
| 4413 | "notifyKey - id=%" PRIx32 ", eventTime=%" PRId64 |
| 4414 | ", deviceId=%d, source=%s, displayId=%" PRId32 |
| 4415 | "policyFlags=0x%x, action=%s, flags=0x%x, keyCode=%s, scanCode=0x%x, metaState=0x%x, " |
| 4416 | "downTime=%" PRId64, |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4417 | args.id, args.eventTime, args.deviceId, inputEventSourceToString(args.source).c_str(), |
| 4418 | args.displayId, args.policyFlags, KeyEvent::actionToString(args.action), args.flags, |
| 4419 | KeyEvent::getLabel(args.keyCode), args.scanCode, args.metaState, args.downTime); |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4420 | Result<void> keyCheck = validateKeyEvent(args.action); |
| 4421 | if (!keyCheck.ok()) { |
| 4422 | LOG(ERROR) << "invalid key event: " << keyCheck.error(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4423 | return; |
| 4424 | } |
| 4425 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4426 | uint32_t policyFlags = args.policyFlags; |
| 4427 | int32_t flags = args.flags; |
| 4428 | int32_t metaState = args.metaState; |
Siarhei Vishniakou | 622bd32 | 2018-10-29 18:02:27 -0700 | [diff] [blame] | 4429 | // InputDispatcher tracks and generates key repeats on behalf of |
| 4430 | // whatever notifies it, so repeatCount should always be set to 0 |
| 4431 | constexpr int32_t repeatCount = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4432 | if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { |
| 4433 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 4434 | flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
| 4435 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4436 | if (policyFlags & POLICY_FLAG_FUNCTION) { |
| 4437 | metaState |= AMETA_FUNCTION_ON; |
| 4438 | } |
| 4439 | |
| 4440 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 4441 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4442 | int32_t keyCode = args.keyCode; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4443 | KeyEvent event; |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4444 | event.initialize(args.id, args.deviceId, args.source, args.displayId, INVALID_HMAC, args.action, |
| 4445 | flags, keyCode, args.scanCode, metaState, repeatCount, args.downTime, |
| 4446 | args.eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4447 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4448 | android::base::Timer t; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4449 | mPolicy.interceptKeyBeforeQueueing(event, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4450 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4451 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4452 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4453 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4454 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4455 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4456 | { // acquire lock |
| 4457 | mLock.lock(); |
| 4458 | |
| 4459 | if (shouldSendKeyToInputFilterLocked(args)) { |
| 4460 | mLock.unlock(); |
| 4461 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4462 | policyFlags |= POLICY_FLAG_FILTERED; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4463 | if (!mPolicy.filterInputEvent(event, policyFlags)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4464 | return; // event was consumed by the filter |
| 4465 | } |
| 4466 | |
| 4467 | mLock.lock(); |
| 4468 | } |
| 4469 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4470 | std::unique_ptr<KeyEntry> newEntry = |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4471 | std::make_unique<KeyEntry>(args.id, /*injectionState=*/nullptr, args.eventTime, |
| 4472 | args.deviceId, args.source, args.displayId, policyFlags, |
| 4473 | args.action, flags, keyCode, args.scanCode, metaState, |
| 4474 | repeatCount, args.downTime); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 4475 | if (mTracer) { |
| 4476 | newEntry->traceTracker = mTracer->traceInboundEvent(*newEntry); |
| 4477 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4478 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4479 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4480 | mLock.unlock(); |
| 4481 | } // release lock |
| 4482 | |
| 4483 | if (needWake) { |
| 4484 | mLooper->wake(); |
| 4485 | } |
| 4486 | } |
| 4487 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4488 | bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs& args) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4489 | return mInputFilterEnabled; |
| 4490 | } |
| 4491 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4492 | void InputDispatcher::notifyMotion(const NotifyMotionArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4493 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 96282b0 | 2023-02-24 22:36:17 +0000 | [diff] [blame] | 4494 | ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=%s, " |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4495 | "displayId=%" PRId32 ", policyFlags=0x%x, " |
Siarhei Vishniakou | 6ebd069 | 2022-10-20 15:05:45 -0700 | [diff] [blame] | 4496 | "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] | 4497 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, xCursorPosition=%f, " |
| 4498 | "yCursorPosition=%f, downTime=%" PRId64, |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4499 | args.id, args.eventTime, args.deviceId, inputEventSourceToString(args.source).c_str(), |
| 4500 | args.displayId, args.policyFlags, MotionEvent::actionToString(args.action).c_str(), |
| 4501 | args.actionButton, args.flags, args.metaState, args.buttonState, args.edgeFlags, |
| 4502 | args.xPrecision, args.yPrecision, args.xCursorPosition, args.yCursorPosition, |
| 4503 | args.downTime); |
Siarhei Vishniakou | 3218fc0 | 2023-06-15 20:41:02 -0700 | [diff] [blame] | 4504 | for (uint32_t i = 0; i < args.getPointerCount(); i++) { |
Prabir Pradhan | 96282b0 | 2023-02-24 22:36:17 +0000 | [diff] [blame] | 4505 | ALOGD(" Pointer %d: id=%d, toolType=%s, x=%f, y=%f, pressure=%f, size=%f, " |
| 4506 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, orientation=%f", |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4507 | i, args.pointerProperties[i].id, |
| 4508 | ftl::enum_string(args.pointerProperties[i].toolType).c_str(), |
| 4509 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 4510 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 4511 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 4512 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 4513 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 4514 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 4515 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 4516 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 4517 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4518 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4519 | } |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4520 | |
Siarhei Vishniakou | 3218fc0 | 2023-06-15 20:41:02 -0700 | [diff] [blame] | 4521 | Result<void> motionCheck = |
| 4522 | validateMotionEvent(args.action, args.actionButton, args.getPointerCount(), |
| 4523 | args.pointerProperties.data()); |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4524 | if (!motionCheck.ok()) { |
| 4525 | LOG(FATAL) << "Invalid event: " << args.dump() << "; reason: " << motionCheck.error(); |
| 4526 | return; |
| 4527 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4528 | |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 4529 | if (DEBUG_VERIFY_EVENTS) { |
| 4530 | auto [it, _] = |
| 4531 | mVerifiersByDisplay.try_emplace(args.displayId, |
| 4532 | StringPrintf("display %" PRId32, args.displayId)); |
| 4533 | Result<void> result = |
Siarhei Vishniakou | 2d151ac | 2023-09-19 13:30:24 -0700 | [diff] [blame] | 4534 | it->second.processMovement(args.deviceId, args.source, args.action, |
| 4535 | args.getPointerCount(), args.pointerProperties.data(), |
| 4536 | args.pointerCoords.data(), args.flags); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 4537 | if (!result.ok()) { |
| 4538 | LOG(FATAL) << "Bad stream: " << result.error() << " caused by " << args.dump(); |
| 4539 | } |
| 4540 | } |
| 4541 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4542 | uint32_t policyFlags = args.policyFlags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4543 | policyFlags |= POLICY_FLAG_TRUSTED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4544 | |
| 4545 | android::base::Timer t; |
Yeabkal Wubshit | 88a9041 | 2023-12-21 18:23:04 -0800 | [diff] [blame] | 4546 | mPolicy.interceptMotionBeforeQueueing(args.displayId, args.source, args.action, args.eventTime, |
| 4547 | policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4548 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4549 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4550 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4551 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4552 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4553 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4554 | { // acquire lock |
| 4555 | mLock.lock(); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 4556 | if (!(policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
| 4557 | // Set the flag anyway if we already have an ongoing gesture. That would allow us to |
| 4558 | // complete the processing of the current stroke. |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4559 | const auto touchStateIt = mTouchStatesByDisplay.find(args.displayId); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 4560 | if (touchStateIt != mTouchStatesByDisplay.end()) { |
| 4561 | const TouchState& touchState = touchStateIt->second; |
Linnan Li | 907ae73 | 2023-09-05 17:14:21 +0800 | [diff] [blame] | 4562 | if (touchState.hasTouchingPointers(args.deviceId) || |
| 4563 | touchState.hasHoveringPointers(args.deviceId)) { |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 4564 | policyFlags |= POLICY_FLAG_PASS_TO_USER; |
| 4565 | } |
| 4566 | } |
| 4567 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4568 | |
| 4569 | if (shouldSendMotionToInputFilterLocked(args)) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4570 | ui::Transform displayTransform; |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4571 | if (const auto it = mDisplayInfos.find(args.displayId); it != mDisplayInfos.end()) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4572 | displayTransform = it->second.transform; |
| 4573 | } |
| 4574 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4575 | mLock.unlock(); |
| 4576 | |
| 4577 | MotionEvent event; |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4578 | event.initialize(args.id, args.deviceId, args.source, args.displayId, INVALID_HMAC, |
| 4579 | args.action, args.actionButton, args.flags, args.edgeFlags, |
| 4580 | args.metaState, args.buttonState, args.classification, |
| 4581 | displayTransform, args.xPrecision, args.yPrecision, |
| 4582 | args.xCursorPosition, args.yCursorPosition, displayTransform, |
Siarhei Vishniakou | 3218fc0 | 2023-06-15 20:41:02 -0700 | [diff] [blame] | 4583 | args.downTime, args.eventTime, args.getPointerCount(), |
| 4584 | args.pointerProperties.data(), args.pointerCoords.data()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4585 | |
| 4586 | policyFlags |= POLICY_FLAG_FILTERED; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4587 | if (!mPolicy.filterInputEvent(event, policyFlags)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4588 | return; // event was consumed by the filter |
| 4589 | } |
| 4590 | |
| 4591 | mLock.lock(); |
| 4592 | } |
| 4593 | |
| 4594 | // Just enqueue a new motion event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4595 | std::unique_ptr<MotionEntry> newEntry = |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4596 | std::make_unique<MotionEntry>(args.id, /*injectionState=*/nullptr, args.eventTime, |
| 4597 | args.deviceId, args.source, args.displayId, |
| 4598 | policyFlags, args.action, args.actionButton, |
| 4599 | args.flags, args.metaState, args.buttonState, |
| 4600 | args.classification, args.edgeFlags, args.xPrecision, |
| 4601 | args.yPrecision, args.xCursorPosition, |
| 4602 | args.yCursorPosition, args.downTime, |
| 4603 | args.pointerProperties, args.pointerCoords); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 4604 | if (mTracer) { |
| 4605 | newEntry->traceTracker = mTracer->traceInboundEvent(*newEntry); |
| 4606 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4607 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4608 | if (args.id != android::os::IInputConstants::INVALID_INPUT_EVENT_ID && |
| 4609 | IdGenerator::getSource(args.id) == IdGenerator::Source::INPUT_READER && |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 4610 | !mInputFilterEnabled) { |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4611 | const bool isDown = args.action == AMOTION_EVENT_ACTION_DOWN; |
Asmita Poddar | dd9a6cd | 2023-09-26 15:35:12 +0000 | [diff] [blame] | 4612 | std::set<InputDeviceUsageSource> sources = getUsageSourcesForMotionArgs(args); |
| 4613 | mLatencyTracker.trackListener(args.id, isDown, args.eventTime, args.readTime, |
| 4614 | args.deviceId, sources); |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 4615 | } |
| 4616 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4617 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4618 | mLock.unlock(); |
| 4619 | } // release lock |
| 4620 | |
| 4621 | if (needWake) { |
| 4622 | mLooper->wake(); |
| 4623 | } |
| 4624 | } |
| 4625 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4626 | void InputDispatcher::notifySensor(const NotifySensorArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4627 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4628 | ALOGD("notifySensor - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 4629 | " sensorType=%s", |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4630 | args.id, args.eventTime, args.deviceId, args.source, |
| 4631 | ftl::enum_string(args.sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4632 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4633 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4634 | bool needWake = false; |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4635 | { // acquire lock |
| 4636 | mLock.lock(); |
| 4637 | |
| 4638 | // Just enqueue a new sensor event. |
| 4639 | std::unique_ptr<SensorEntry> newEntry = |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4640 | std::make_unique<SensorEntry>(args.id, args.eventTime, args.deviceId, args.source, |
| 4641 | /* policyFlags=*/0, args.hwTimestamp, args.sensorType, |
| 4642 | args.accuracy, args.accuracyChanged, args.values); |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4643 | |
| 4644 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
| 4645 | mLock.unlock(); |
| 4646 | } // release lock |
| 4647 | |
| 4648 | if (needWake) { |
| 4649 | mLooper->wake(); |
| 4650 | } |
| 4651 | } |
| 4652 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4653 | void InputDispatcher::notifyVibratorState(const NotifyVibratorStateArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4654 | if (debugInboundEventDetails()) { |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4655 | ALOGD("notifyVibratorState - eventTime=%" PRId64 ", device=%d, isOn=%d", args.eventTime, |
| 4656 | args.deviceId, args.isOn); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4657 | } |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4658 | mPolicy.notifyVibratorState(args.deviceId, args.isOn); |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 4659 | } |
| 4660 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4661 | bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs& args) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4662 | return mInputFilterEnabled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4663 | } |
| 4664 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4665 | void InputDispatcher::notifySwitch(const NotifySwitchArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4666 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4667 | ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, " |
| 4668 | "switchMask=0x%08x", |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4669 | args.eventTime, args.policyFlags, args.switchValues, args.switchMask); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4670 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4671 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4672 | uint32_t policyFlags = args.policyFlags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4673 | policyFlags |= POLICY_FLAG_TRUSTED; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4674 | mPolicy.notifySwitch(args.eventTime, args.switchValues, args.switchMask, policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4675 | } |
| 4676 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4677 | void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4678 | if (debugInboundEventDetails()) { |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4679 | ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args.eventTime, |
| 4680 | args.deviceId); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4681 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4682 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4683 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4684 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4685 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4686 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4687 | std::unique_ptr<DeviceResetEntry> newEntry = |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4688 | std::make_unique<DeviceResetEntry>(args.id, args.eventTime, args.deviceId); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4689 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Siarhei Vishniakou | 1160ecd | 2023-06-28 15:57:47 -0700 | [diff] [blame] | 4690 | |
| 4691 | for (auto& [_, verifier] : mVerifiersByDisplay) { |
| 4692 | verifier.resetDevice(args.deviceId); |
| 4693 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4694 | } // release lock |
| 4695 | |
| 4696 | if (needWake) { |
| 4697 | mLooper->wake(); |
| 4698 | } |
| 4699 | } |
| 4700 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4701 | void InputDispatcher::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4702 | if (debugInboundEventDetails()) { |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4703 | ALOGD("notifyPointerCaptureChanged - eventTime=%" PRId64 ", enabled=%s", args.eventTime, |
| 4704 | args.request.enable ? "true" : "false"); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4705 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4706 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4707 | bool needWake = false; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4708 | { // acquire lock |
| 4709 | std::scoped_lock _l(mLock); |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4710 | auto entry = |
| 4711 | std::make_unique<PointerCaptureChangedEntry>(args.id, args.eventTime, args.request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4712 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 4713 | } // release lock |
| 4714 | |
| 4715 | if (needWake) { |
| 4716 | mLooper->wake(); |
| 4717 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4718 | } |
| 4719 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4720 | InputEventInjectionResult InputDispatcher::injectInputEvent(const InputEvent* event, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 4721 | std::optional<gui::Uid> targetUid, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4722 | InputEventInjectionSync syncMode, |
| 4723 | std::chrono::milliseconds timeout, |
| 4724 | uint32_t policyFlags) { |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4725 | Result<void> eventValidation = validateInputEvent(*event); |
| 4726 | if (!eventValidation.ok()) { |
| 4727 | LOG(INFO) << "Injection failed: invalid event: " << eventValidation.error(); |
| 4728 | return InputEventInjectionResult::FAILED; |
| 4729 | } |
| 4730 | |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4731 | if (debugInboundEventDetails()) { |
Siarhei Vishniakou | 827d1ac | 2023-07-21 16:37:51 -0700 | [diff] [blame] | 4732 | LOG(INFO) << __func__ << ": targetUid=" << toString(targetUid, &uidString) |
| 4733 | << ", syncMode=" << ftl::enum_string(syncMode) << ", timeout=" << timeout.count() |
| 4734 | << "ms, policyFlags=0x" << std::hex << policyFlags << std::dec |
| 4735 | << ", event=" << *event; |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4736 | } |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 4737 | 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] | 4738 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4739 | policyFlags |= POLICY_FLAG_INJECTED | POLICY_FLAG_TRUSTED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4740 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4741 | // 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] | 4742 | // that have gone through the InputFilter. If the event passed through the InputFilter, assign |
| 4743 | // the provided device id. If the InputFilter is accessibility, and it modifies or synthesizes |
| 4744 | // the injected event, it is responsible for setting POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY. |
| 4745 | // For those events, we will set FLAG_IS_ACCESSIBILITY_EVENT to allow apps to distinguish them |
| 4746 | // from events that originate from actual hardware. |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 4747 | DeviceId resolvedDeviceId = VIRTUAL_KEYBOARD_ID; |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4748 | if (policyFlags & POLICY_FLAG_FILTERED) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4749 | resolvedDeviceId = event->getDeviceId(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4750 | } |
| 4751 | |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4752 | const bool isAsync = syncMode == InputEventInjectionSync::NONE; |
| 4753 | auto injectionState = std::make_shared<InjectionState>(targetUid, isAsync); |
| 4754 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4755 | std::queue<std::unique_ptr<EventEntry>> injectedEntries; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4756 | switch (event->getType()) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4757 | case InputEventType::KEY: { |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4758 | const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event); |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4759 | const int32_t action = incomingKey.getAction(); |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4760 | int32_t flags = incomingKey.getFlags(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4761 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4762 | flags |= AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4763 | } |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4764 | int32_t keyCode = incomingKey.getKeyCode(); |
| 4765 | int32_t metaState = incomingKey.getMetaState(); |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4766 | KeyEvent keyEvent; |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4767 | keyEvent.initialize(incomingKey.getId(), resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4768 | incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode, |
| 4769 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), |
| 4770 | incomingKey.getDownTime(), incomingKey.getEventTime()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4771 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4772 | if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) { |
| 4773 | policyFlags |= POLICY_FLAG_VIRTUAL; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4774 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4775 | |
| 4776 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 4777 | android::base::Timer t; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4778 | mPolicy.interceptKeyBeforeQueueing(keyEvent, /*byref*/ policyFlags); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4779 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4780 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
| 4781 | std::to_string(t.duration().count()).c_str()); |
| 4782 | } |
| 4783 | } |
| 4784 | |
| 4785 | mLock.lock(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4786 | std::unique_ptr<KeyEntry> injectedEntry = |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4787 | std::make_unique<KeyEntry>(incomingKey.getId(), injectionState, |
| 4788 | incomingKey.getEventTime(), resolvedDeviceId, |
| 4789 | incomingKey.getSource(), incomingKey.getDisplayId(), |
| 4790 | policyFlags, action, flags, keyCode, |
| 4791 | incomingKey.getScanCode(), metaState, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4792 | incomingKey.getRepeatCount(), |
| 4793 | incomingKey.getDownTime()); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 4794 | if (mTracer) { |
| 4795 | injectedEntry->traceTracker = mTracer->traceInboundEvent(*injectedEntry); |
| 4796 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4797 | injectedEntries.push(std::move(injectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4798 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4799 | } |
| 4800 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4801 | case InputEventType::MOTION: { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4802 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4803 | const bool isPointerEvent = |
| 4804 | isFromSource(event->getSource(), AINPUT_SOURCE_CLASS_POINTER); |
| 4805 | // If a pointer event has no displayId specified, inject it to the default display. |
| 4806 | const uint32_t displayId = isPointerEvent && (event->getDisplayId() == ADISPLAY_ID_NONE) |
| 4807 | ? ADISPLAY_ID_DEFAULT |
| 4808 | : event->getDisplayId(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4809 | int32_t flags = motionEvent.getFlags(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4810 | |
| 4811 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4812 | nsecs_t eventTime = motionEvent.getEventTime(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4813 | android::base::Timer t; |
Yeabkal Wubshit | 88a9041 | 2023-12-21 18:23:04 -0800 | [diff] [blame] | 4814 | mPolicy.interceptMotionBeforeQueueing(displayId, motionEvent.getSource(), |
| 4815 | motionEvent.getAction(), eventTime, |
| 4816 | /*byref*/ policyFlags); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4817 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4818 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
| 4819 | std::to_string(t.duration().count()).c_str()); |
| 4820 | } |
| 4821 | } |
| 4822 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4823 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4824 | flags |= AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4825 | } |
| 4826 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4827 | mLock.lock(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4828 | const nsecs_t* sampleEventTimes = motionEvent.getSampleEventTimes(); |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4829 | const size_t pointerCount = motionEvent.getPointerCount(); |
| 4830 | const std::vector<PointerProperties> |
| 4831 | pointerProperties(motionEvent.getPointerProperties(), |
| 4832 | motionEvent.getPointerProperties() + pointerCount); |
| 4833 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4834 | const PointerCoords* samplePointerCoords = motionEvent.getSamplePointerCoords(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4835 | std::unique_ptr<MotionEntry> injectedEntry = |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4836 | std::make_unique<MotionEntry>(motionEvent.getId(), injectionState, |
| 4837 | *sampleEventTimes, resolvedDeviceId, |
| 4838 | motionEvent.getSource(), displayId, policyFlags, |
| 4839 | motionEvent.getAction(), |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4840 | motionEvent.getActionButton(), flags, |
| 4841 | motionEvent.getMetaState(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4842 | motionEvent.getButtonState(), |
| 4843 | motionEvent.getClassification(), |
| 4844 | motionEvent.getEdgeFlags(), |
| 4845 | motionEvent.getXPrecision(), |
| 4846 | motionEvent.getYPrecision(), |
| 4847 | motionEvent.getRawXCursorPosition(), |
| 4848 | motionEvent.getRawYCursorPosition(), |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4849 | motionEvent.getDownTime(), pointerProperties, |
| 4850 | std::vector<PointerCoords>(samplePointerCoords, |
| 4851 | samplePointerCoords + |
| 4852 | pointerCount)); |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4853 | transformMotionEntryForInjectionLocked(*injectedEntry, motionEvent.getTransform()); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 4854 | if (mTracer) { |
| 4855 | injectedEntry->traceTracker = mTracer->traceInboundEvent(*injectedEntry); |
| 4856 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4857 | injectedEntries.push(std::move(injectedEntry)); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4858 | for (size_t i = motionEvent.getHistorySize(); i > 0; i--) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4859 | sampleEventTimes += 1; |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4860 | samplePointerCoords += motionEvent.getPointerCount(); |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4861 | std::unique_ptr<MotionEntry> nextInjectedEntry = std::make_unique< |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4862 | MotionEntry>(motionEvent.getId(), injectionState, *sampleEventTimes, |
| 4863 | resolvedDeviceId, motionEvent.getSource(), displayId, |
| 4864 | policyFlags, motionEvent.getAction(), |
| 4865 | motionEvent.getActionButton(), flags, |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4866 | motionEvent.getMetaState(), motionEvent.getButtonState(), |
| 4867 | motionEvent.getClassification(), motionEvent.getEdgeFlags(), |
| 4868 | motionEvent.getXPrecision(), motionEvent.getYPrecision(), |
| 4869 | motionEvent.getRawXCursorPosition(), |
| 4870 | motionEvent.getRawYCursorPosition(), motionEvent.getDownTime(), |
| 4871 | pointerProperties, |
| 4872 | std::vector<PointerCoords>(samplePointerCoords, |
| 4873 | samplePointerCoords + |
| 4874 | pointerCount)); |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4875 | transformMotionEntryForInjectionLocked(*nextInjectedEntry, |
| 4876 | motionEvent.getTransform()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4877 | injectedEntries.push(std::move(nextInjectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4878 | } |
| 4879 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4880 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4881 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4882 | default: |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4883 | LOG(WARNING) << "Cannot inject " << ftl::enum_string(event->getType()) << " events"; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4884 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4885 | } |
| 4886 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4887 | bool needWake = false; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4888 | while (!injectedEntries.empty()) { |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 4889 | if (DEBUG_INJECTION) { |
Siarhei Vishniakou | 827d1ac | 2023-07-21 16:37:51 -0700 | [diff] [blame] | 4890 | LOG(INFO) << "Injecting " << injectedEntries.front()->getDescription(); |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 4891 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4892 | needWake |= enqueueInboundEventLocked(std::move(injectedEntries.front())); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4893 | injectedEntries.pop(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4894 | } |
| 4895 | |
| 4896 | mLock.unlock(); |
| 4897 | |
| 4898 | if (needWake) { |
| 4899 | mLooper->wake(); |
| 4900 | } |
| 4901 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4902 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4903 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4904 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4905 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4906 | if (syncMode == InputEventInjectionSync::NONE) { |
| 4907 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4908 | } else { |
| 4909 | for (;;) { |
| 4910 | injectionResult = injectionState->injectionResult; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4911 | if (injectionResult != InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4912 | break; |
| 4913 | } |
| 4914 | |
| 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 injection result " |
| 4919 | "to become available."); |
| 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 | mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4926 | } |
| 4927 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4928 | if (injectionResult == InputEventInjectionResult::SUCCEEDED && |
| 4929 | syncMode == InputEventInjectionSync::WAIT_FOR_FINISHED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4930 | while (injectionState->pendingForegroundDispatches != 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4931 | if (DEBUG_INJECTION) { |
| 4932 | ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", |
| 4933 | injectionState->pendingForegroundDispatches); |
| 4934 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4935 | nsecs_t remainingTimeout = endTime - now(); |
| 4936 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4937 | if (DEBUG_INJECTION) { |
| 4938 | ALOGD("injectInputEvent - Timed out waiting for pending foreground " |
| 4939 | "dispatches to finish."); |
| 4940 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4941 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4942 | break; |
| 4943 | } |
| 4944 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4945 | mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4946 | } |
| 4947 | } |
| 4948 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4949 | } // release lock |
| 4950 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4951 | if (DEBUG_INJECTION) { |
Siarhei Vishniakou | 827d1ac | 2023-07-21 16:37:51 -0700 | [diff] [blame] | 4952 | LOG(INFO) << "injectInputEvent - Finished with result " |
| 4953 | << ftl::enum_string(injectionResult); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4954 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4955 | |
| 4956 | return injectionResult; |
| 4957 | } |
| 4958 | |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4959 | std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4960 | std::array<uint8_t, 32> calculatedHmac; |
| 4961 | std::unique_ptr<VerifiedInputEvent> result; |
| 4962 | switch (event.getType()) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4963 | case InputEventType::KEY: { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4964 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event); |
| 4965 | VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent); |
| 4966 | result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4967 | calculatedHmac = sign(verifiedKeyEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4968 | break; |
| 4969 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4970 | case InputEventType::MOTION: { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4971 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event); |
| 4972 | VerifiedMotionEvent verifiedMotionEvent = |
| 4973 | verifiedMotionEventFromMotionEvent(motionEvent); |
| 4974 | result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4975 | calculatedHmac = sign(verifiedMotionEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4976 | break; |
| 4977 | } |
| 4978 | default: { |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 4979 | LOG(ERROR) << "Cannot verify events of type " << ftl::enum_string(event.getType()); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4980 | return nullptr; |
| 4981 | } |
| 4982 | } |
| 4983 | if (calculatedHmac == INVALID_HMAC) { |
| 4984 | return nullptr; |
| 4985 | } |
tyiu | 1573a67 | 2023-02-21 22:38:32 +0000 | [diff] [blame] | 4986 | if (0 != CRYPTO_memcmp(calculatedHmac.data(), event.getHmac().data(), calculatedHmac.size())) { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4987 | return nullptr; |
| 4988 | } |
| 4989 | return result; |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4990 | } |
| 4991 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 4992 | void InputDispatcher::setInjectionResult(const EventEntry& entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4993 | InputEventInjectionResult injectionResult) { |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4994 | if (!entry.injectionState) { |
| 4995 | // Not an injected event. |
| 4996 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4997 | } |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4998 | |
| 4999 | InjectionState& injectionState = *entry.injectionState; |
| 5000 | if (DEBUG_INJECTION) { |
| 5001 | LOG(INFO) << "Setting input event injection result to " |
| 5002 | << ftl::enum_string(injectionResult); |
| 5003 | } |
| 5004 | |
| 5005 | if (injectionState.injectionIsAsync && !(entry.policyFlags & POLICY_FLAG_FILTERED)) { |
| 5006 | // Log the outcome since the injector did not wait for the injection result. |
| 5007 | switch (injectionResult) { |
| 5008 | case InputEventInjectionResult::SUCCEEDED: |
| 5009 | ALOGV("Asynchronous input event injection succeeded."); |
| 5010 | break; |
| 5011 | case InputEventInjectionResult::TARGET_MISMATCH: |
| 5012 | ALOGV("Asynchronous input event injection target mismatch."); |
| 5013 | break; |
| 5014 | case InputEventInjectionResult::FAILED: |
| 5015 | ALOGW("Asynchronous input event injection failed."); |
| 5016 | break; |
| 5017 | case InputEventInjectionResult::TIMED_OUT: |
| 5018 | ALOGW("Asynchronous input event injection timed out."); |
| 5019 | break; |
| 5020 | case InputEventInjectionResult::PENDING: |
| 5021 | ALOGE("Setting result to 'PENDING' for asynchronous injection"); |
| 5022 | break; |
| 5023 | } |
| 5024 | } |
| 5025 | |
| 5026 | injectionState.injectionResult = injectionResult; |
| 5027 | mInjectionResultAvailable.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5028 | } |
| 5029 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 5030 | void InputDispatcher::transformMotionEntryForInjectionLocked( |
| 5031 | MotionEntry& entry, const ui::Transform& injectedTransform) const { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 5032 | // Input injection works in the logical display coordinate space, but the input pipeline works |
| 5033 | // display space, so we need to transform the injected events accordingly. |
| 5034 | const auto it = mDisplayInfos.find(entry.displayId); |
| 5035 | if (it == mDisplayInfos.end()) return; |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 5036 | const auto& transformToDisplay = it->second.transform.inverse() * injectedTransform; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 5037 | |
Prabir Pradhan | d9a2ebe | 2022-07-20 19:25:13 +0000 | [diff] [blame] | 5038 | if (entry.xCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION && |
| 5039 | entry.yCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) { |
| 5040 | const vec2 cursor = |
| 5041 | MotionEvent::calculateTransformedXY(entry.source, transformToDisplay, |
| 5042 | {entry.xCursorPosition, entry.yCursorPosition}); |
| 5043 | entry.xCursorPosition = cursor.x; |
| 5044 | entry.yCursorPosition = cursor.y; |
| 5045 | } |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 5046 | for (uint32_t i = 0; i < entry.getPointerCount(); i++) { |
Prabir Pradhan | 8e6ce22 | 2022-02-24 09:08:54 -0800 | [diff] [blame] | 5047 | entry.pointerCoords[i] = |
| 5048 | MotionEvent::calculateTransformedCoords(entry.source, transformToDisplay, |
| 5049 | entry.pointerCoords[i]); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 5050 | } |
| 5051 | } |
| 5052 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 5053 | void InputDispatcher::incrementPendingForegroundDispatches(const EventEntry& entry) { |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 5054 | if (entry.injectionState) { |
| 5055 | entry.injectionState->pendingForegroundDispatches += 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5056 | } |
| 5057 | } |
| 5058 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 5059 | void InputDispatcher::decrementPendingForegroundDispatches(const EventEntry& entry) { |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 5060 | if (entry.injectionState) { |
| 5061 | entry.injectionState->pendingForegroundDispatches -= 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5062 | |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 5063 | if (entry.injectionState->pendingForegroundDispatches == 0) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5064 | mInjectionSyncFinished.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5065 | } |
| 5066 | } |
| 5067 | } |
| 5068 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5069 | const std::vector<sp<WindowInfoHandle>>& InputDispatcher::getWindowHandlesLocked( |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5070 | int32_t displayId) const { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5071 | static const std::vector<sp<WindowInfoHandle>> EMPTY_WINDOW_HANDLES; |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5072 | auto it = mWindowHandlesByDisplay.find(displayId); |
| 5073 | return it != mWindowHandlesByDisplay.end() ? it->second : EMPTY_WINDOW_HANDLES; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5074 | } |
| 5075 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5076 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 5077 | const sp<IBinder>& windowHandleToken, std::optional<int32_t> displayId) const { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 5078 | if (windowHandleToken == nullptr) { |
| 5079 | return nullptr; |
| 5080 | } |
| 5081 | |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 5082 | if (!displayId) { |
| 5083 | // Look through all displays. |
| 5084 | for (auto& it : mWindowHandlesByDisplay) { |
| 5085 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 5086 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 5087 | if (windowHandle->getToken() == windowHandleToken) { |
| 5088 | return windowHandle; |
| 5089 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5090 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5091 | } |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5092 | return nullptr; |
| 5093 | } |
| 5094 | |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 5095 | // Only look through the requested display. |
| 5096 | for (const sp<WindowInfoHandle>& windowHandle : getWindowHandlesLocked(*displayId)) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5097 | if (windowHandle->getToken() == windowHandleToken) { |
| 5098 | return windowHandle; |
| 5099 | } |
| 5100 | } |
| 5101 | return nullptr; |
| 5102 | } |
| 5103 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5104 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
| 5105 | const sp<WindowInfoHandle>& windowHandle) const { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 5106 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5107 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 5108 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 5109 | if (handle->getId() == windowHandle->getId() && |
| 5110 | handle->getToken() == windowHandle->getToken()) { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 5111 | if (windowHandle->getInfo()->displayId != it.first) { |
| 5112 | ALOGE("Found window %s in display %" PRId32 |
| 5113 | ", but it should belong to display %" PRId32, |
| 5114 | windowHandle->getName().c_str(), it.first, |
| 5115 | windowHandle->getInfo()->displayId); |
| 5116 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 5117 | return handle; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5118 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5119 | } |
| 5120 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 5121 | return nullptr; |
| 5122 | } |
| 5123 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5124 | sp<WindowInfoHandle> InputDispatcher::getFocusedWindowHandleLocked(int displayId) const { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 5125 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(displayId); |
| 5126 | return getWindowHandleLocked(focusedToken, displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5127 | } |
| 5128 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5129 | ui::Transform InputDispatcher::getTransformLocked(int32_t displayId) const { |
| 5130 | auto displayInfoIt = mDisplayInfos.find(displayId); |
| 5131 | return displayInfoIt != mDisplayInfos.end() ? displayInfoIt->second.transform |
| 5132 | : kIdentityTransform; |
| 5133 | } |
| 5134 | |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5135 | bool InputDispatcher::canWindowReceiveMotionLocked(const sp<WindowInfoHandle>& window, |
| 5136 | const MotionEntry& motionEntry) const { |
| 5137 | const WindowInfo& info = *window->getInfo(); |
| 5138 | |
| 5139 | // Skip spy window targets that are not valid for targeted injection. |
| 5140 | if (const auto err = verifyTargetedInjection(window, motionEntry); err) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5141 | return false; |
| 5142 | } |
| 5143 | |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5144 | if (info.inputConfig.test(WindowInfo::InputConfig::PAUSE_DISPATCHING)) { |
| 5145 | ALOGI("Not sending touch event to %s because it is paused", window->getName().c_str()); |
| 5146 | return false; |
| 5147 | } |
| 5148 | |
| 5149 | if (info.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) { |
| 5150 | ALOGW("Not sending touch gesture to %s because it has config NO_INPUT_CHANNEL", |
| 5151 | window->getName().c_str()); |
| 5152 | return false; |
| 5153 | } |
| 5154 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 5155 | std::shared_ptr<Connection> connection = getConnectionLocked(window->getToken()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5156 | if (connection == nullptr) { |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5157 | ALOGW("Not sending touch to %s because there's no corresponding connection", |
| 5158 | window->getName().c_str()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5159 | return false; |
| 5160 | } |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5161 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5162 | if (!connection->responsive) { |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5163 | 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] | 5164 | return false; |
| 5165 | } |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5166 | |
| 5167 | // Drop events that can't be trusted due to occlusion |
| 5168 | const auto [x, y] = resolveTouchedPosition(motionEntry); |
| 5169 | TouchOcclusionInfo occlusionInfo = computeTouchOcclusionInfoLocked(window, x, y); |
| 5170 | if (!isTouchTrustedLocked(occlusionInfo)) { |
| 5171 | if (DEBUG_TOUCH_OCCLUSION) { |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 5172 | ALOGD("Stack of obscuring windows during untrusted touch (%.1f, %.1f):", x, y); |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5173 | for (const auto& log : occlusionInfo.debugInfo) { |
| 5174 | ALOGD("%s", log.c_str()); |
| 5175 | } |
| 5176 | } |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 5177 | ALOGW("Dropping untrusted touch event due to %s/%s", occlusionInfo.obscuringPackage.c_str(), |
| 5178 | occlusionInfo.obscuringUid.toString().c_str()); |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5179 | return false; |
| 5180 | } |
| 5181 | |
| 5182 | // Drop touch events if requested by input feature |
| 5183 | if (shouldDropInput(motionEntry, window)) { |
| 5184 | return false; |
| 5185 | } |
| 5186 | |
Siarhei Vishniakou | f77f60a | 2023-10-23 17:26:05 -0700 | [diff] [blame] | 5187 | // Ignore touches if stylus is down anywhere on screen |
| 5188 | if (info.inputConfig.test(WindowInfo::InputConfig::GLOBAL_STYLUS_BLOCKS_TOUCH) && |
| 5189 | isStylusActiveInDisplay(info.displayId, mTouchStatesByDisplay)) { |
| 5190 | LOG(INFO) << "Dropping touch from " << window->getName() << " because stylus is active"; |
| 5191 | return false; |
| 5192 | } |
| 5193 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5194 | return true; |
| 5195 | } |
| 5196 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5197 | void InputDispatcher::updateWindowHandlesForDisplayLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5198 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
| 5199 | if (windowInfoHandles.empty()) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5200 | // Remove all handles on a display if there are no windows left. |
| 5201 | mWindowHandlesByDisplay.erase(displayId); |
| 5202 | return; |
| 5203 | } |
| 5204 | |
| 5205 | // Since we compare the pointer of input window handles across window updates, we need |
| 5206 | // 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] | 5207 | const std::vector<sp<WindowInfoHandle>>& oldHandles = getWindowHandlesLocked(displayId); |
| 5208 | std::unordered_map<int32_t /*id*/, sp<WindowInfoHandle>> oldHandlesById; |
| 5209 | for (const sp<WindowInfoHandle>& handle : oldHandles) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5210 | oldHandlesById[handle->getId()] = handle; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5211 | } |
| 5212 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5213 | std::vector<sp<WindowInfoHandle>> newHandles; |
| 5214 | for (const sp<WindowInfoHandle>& handle : windowInfoHandles) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5215 | const WindowInfo* info = handle->getInfo(); |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 5216 | if (getConnectionLocked(handle->getToken()) == nullptr) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5217 | const bool noInputChannel = |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 5218 | info->inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5219 | const bool canReceiveInput = |
| 5220 | !info->inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE) || |
| 5221 | !info->inputConfig.test(WindowInfo::InputConfig::NOT_FOCUSABLE); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5222 | if (canReceiveInput && !noInputChannel) { |
John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 5223 | ALOGV("Window handle %s has no registered input channel", |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5224 | handle->getName().c_str()); |
Robert Carr | 2984b7a | 2020-04-13 17:06:45 -0700 | [diff] [blame] | 5225 | continue; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5226 | } |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5227 | } |
| 5228 | |
| 5229 | if (info->displayId != displayId) { |
| 5230 | ALOGE("Window %s updated by wrong display %d, should belong to display %d", |
| 5231 | handle->getName().c_str(), displayId, info->displayId); |
| 5232 | continue; |
| 5233 | } |
| 5234 | |
Robert Carr | edd1360 | 2020-04-13 17:24:34 -0700 | [diff] [blame] | 5235 | if ((oldHandlesById.find(handle->getId()) != oldHandlesById.end()) && |
| 5236 | (oldHandlesById.at(handle->getId())->getToken() == handle->getToken())) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5237 | const sp<WindowInfoHandle>& oldHandle = oldHandlesById.at(handle->getId()); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5238 | oldHandle->updateFrom(handle); |
| 5239 | newHandles.push_back(oldHandle); |
| 5240 | } else { |
| 5241 | newHandles.push_back(handle); |
| 5242 | } |
| 5243 | } |
| 5244 | |
| 5245 | // Insert or replace |
| 5246 | mWindowHandlesByDisplay[displayId] = newHandles; |
| 5247 | } |
| 5248 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5249 | /** |
| 5250 | * Called from InputManagerService, update window handle list by displayId that can receive input. |
| 5251 | * A window handle contains information about InputChannel, Touch Region, Types, Focused,... |
| 5252 | * If set an empty list, remove all handles from the specific display. |
| 5253 | * For focused handle, check if need to change and send a cancel event to previous one. |
| 5254 | * For removed handle, check if need to send a cancel event if already in touch. |
| 5255 | */ |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5256 | void InputDispatcher::setInputWindowsLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5257 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5258 | if (DEBUG_FOCUS) { |
| 5259 | std::string windowList; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5260 | for (const sp<WindowInfoHandle>& iwh : windowInfoHandles) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5261 | windowList += iwh->getName() + " "; |
| 5262 | } |
Siarhei Vishniakou | 366fb5b | 2023-12-06 11:23:41 -0800 | [diff] [blame] | 5263 | LOG(INFO) << "setInputWindows displayId=" << displayId << " " << windowList; |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5264 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5265 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 5266 | // Check preconditions for new input windows |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5267 | for (const sp<WindowInfoHandle>& window : windowInfoHandles) { |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 5268 | const WindowInfo& info = *window->getInfo(); |
| 5269 | |
| 5270 | // 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] | 5271 | const bool noInputWindow = info.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5272 | if (noInputWindow && window->getToken() != nullptr) { |
| 5273 | ALOGE("%s has feature NO_INPUT_WINDOW, but a non-null token. Clearing", |
| 5274 | window->getName().c_str()); |
| 5275 | window->releaseChannel(); |
| 5276 | } |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 5277 | |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 5278 | // Ensure all spy windows are trusted overlays |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5279 | LOG_ALWAYS_FATAL_IF(info.isSpy() && |
| 5280 | !info.inputConfig.test( |
| 5281 | WindowInfo::InputConfig::TRUSTED_OVERLAY), |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 5282 | "%s has feature SPY, but is not a trusted overlay.", |
| 5283 | window->getName().c_str()); |
| 5284 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 5285 | // Ensure all stylus interceptors are trusted overlays |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5286 | LOG_ALWAYS_FATAL_IF(info.interceptsStylus() && |
| 5287 | !info.inputConfig.test( |
| 5288 | WindowInfo::InputConfig::TRUSTED_OVERLAY), |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 5289 | "%s has feature INTERCEPTS_STYLUS, but is not a trusted overlay.", |
| 5290 | window->getName().c_str()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5291 | } |
| 5292 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5293 | // Copy old handles for release if they are no longer present. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5294 | const std::vector<sp<WindowInfoHandle>> oldWindowHandles = getWindowHandlesLocked(displayId); |
Prabir Pradhan | 4b9b1a1 | 2024-02-02 00:16:17 +0000 | [diff] [blame] | 5295 | const sp<WindowInfoHandle> removedFocusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5296 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5297 | updateWindowHandlesForDisplayLocked(windowInfoHandles, displayId); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5298 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5299 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5300 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5301 | std::optional<FocusResolver::FocusChanges> changes = |
| 5302 | mFocusResolver.setInputWindows(displayId, windowHandles); |
| 5303 | if (changes) { |
Prabir Pradhan | 4b9b1a1 | 2024-02-02 00:16:17 +0000 | [diff] [blame] | 5304 | onFocusChangedLocked(*changes, removedFocusedWindowHandle); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5305 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5306 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5307 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 5308 | mTouchStatesByDisplay.find(displayId); |
| 5309 | if (stateIt != mTouchStatesByDisplay.end()) { |
| 5310 | TouchState& state = stateIt->second; |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5311 | for (size_t i = 0; i < state.windows.size();) { |
| 5312 | TouchedWindow& touchedWindow = state.windows[i]; |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 5313 | if (getWindowHandleLocked(touchedWindow.windowHandle) == nullptr) { |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 5314 | LOG(INFO) << "Touched window was removed: " << touchedWindow.windowHandle->getName() |
| 5315 | << " in display %" << displayId; |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 5316 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
| 5317 | "touched window was removed"); |
| 5318 | synthesizeCancelationEventsForWindowLocked(touchedWindow.windowHandle, options); |
| 5319 | // Since we are about to drop the touch, cancel the events for the wallpaper as |
| 5320 | // well. |
| 5321 | if (touchedWindow.targetFlags.test(InputTarget::Flags::FOREGROUND) && |
| 5322 | touchedWindow.windowHandle->getInfo()->inputConfig.test( |
| 5323 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER)) { |
| 5324 | if (const auto& ww = state.getWallpaperWindow(); ww) { |
| 5325 | synthesizeCancelationEventsForWindowLocked(ww, options); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5326 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5327 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5328 | state.windows.erase(state.windows.begin() + i); |
| 5329 | } else { |
| 5330 | ++i; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5331 | } |
| 5332 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5333 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5334 | // 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] | 5335 | // could just clear the state here. |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 5336 | if (mDragState && mDragState->dragWindow->getInfo()->displayId == displayId && |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5337 | std::find(windowHandles.begin(), windowHandles.end(), mDragState->dragWindow) == |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5338 | windowHandles.end()) { |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 5339 | ALOGI("Drag window went away: %s", mDragState->dragWindow->getName().c_str()); |
| 5340 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5341 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5342 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5343 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 5344 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5345 | // Release information for windows that are no longer present. |
| 5346 | // This ensures that unused input channels are released promptly. |
| 5347 | // Otherwise, they might stick around until the window handle is destroyed |
| 5348 | // which might not happen until the next GC. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5349 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 5350 | if (getWindowHandleLocked(oldWindowHandle) == nullptr) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5351 | if (DEBUG_FOCUS) { |
| 5352 | ALOGD("Window went away: %s", oldWindowHandle->getName().c_str()); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 5353 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5354 | oldWindowHandle->releaseChannel(); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 5355 | } |
chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 5356 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5357 | } |
| 5358 | |
| 5359 | void InputDispatcher::setFocusedApplication( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5360 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5361 | if (DEBUG_FOCUS) { |
| 5362 | ALOGD("setFocusedApplication displayId=%" PRId32 " %s", displayId, |
| 5363 | inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>"); |
| 5364 | } |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 5365 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5366 | std::scoped_lock _l(mLock); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 5367 | setFocusedApplicationLocked(displayId, inputApplicationHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5368 | } // release lock |
| 5369 | |
| 5370 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5371 | mLooper->wake(); |
| 5372 | } |
| 5373 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 5374 | void InputDispatcher::setFocusedApplicationLocked( |
| 5375 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
| 5376 | std::shared_ptr<InputApplicationHandle> oldFocusedApplicationHandle = |
| 5377 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 5378 | |
| 5379 | if (sharedPointersEqual(oldFocusedApplicationHandle, inputApplicationHandle)) { |
| 5380 | return; // This application is already focused. No need to wake up or change anything. |
| 5381 | } |
| 5382 | |
| 5383 | // Set the new application handle. |
| 5384 | if (inputApplicationHandle != nullptr) { |
| 5385 | mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle; |
| 5386 | } else { |
| 5387 | mFocusedApplicationHandlesByDisplay.erase(displayId); |
| 5388 | } |
| 5389 | |
| 5390 | // No matter what the old focused application was, stop waiting on it because it is |
| 5391 | // no longer focused. |
| 5392 | resetNoFocusedWindowTimeoutLocked(); |
| 5393 | } |
| 5394 | |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 5395 | void InputDispatcher::setMinTimeBetweenUserActivityPokes(std::chrono::milliseconds interval) { |
| 5396 | if (interval.count() < 0) { |
| 5397 | LOG_ALWAYS_FATAL("Minimum time between user activity pokes should be >= 0"); |
| 5398 | } |
| 5399 | std::scoped_lock _l(mLock); |
| 5400 | mMinTimeBetweenUserActivityPokes = interval; |
| 5401 | } |
| 5402 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5403 | /** |
| 5404 | * Sets the focused display, which is responsible for receiving focus-dispatched input events where |
| 5405 | * the display not specified. |
| 5406 | * |
| 5407 | * We track any unreleased events for each window. If a window loses the ability to receive the |
| 5408 | * released event, we will send a cancel event to it. So when the focused display is changed, we |
| 5409 | * cancel all the unreleased display-unspecified events for the focused window on the old focused |
| 5410 | * display. The display-specified events won't be affected. |
| 5411 | */ |
| 5412 | void InputDispatcher::setFocusedDisplay(int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5413 | if (DEBUG_FOCUS) { |
| 5414 | ALOGD("setFocusedDisplay displayId=%" PRId32, displayId); |
| 5415 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5416 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5417 | std::scoped_lock _l(mLock); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5418 | |
| 5419 | if (mFocusedDisplayId != displayId) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5420 | sp<IBinder> oldFocusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5421 | mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5422 | if (oldFocusedWindowToken != nullptr) { |
Prabir Pradhan | 4b9b1a1 | 2024-02-02 00:16:17 +0000 | [diff] [blame] | 5423 | const auto windowHandle = |
| 5424 | getWindowHandleLocked(oldFocusedWindowToken, mFocusedDisplayId); |
| 5425 | if (windowHandle == nullptr) { |
| 5426 | LOG(FATAL) << __func__ << ": Previously focused token did not have a window"; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5427 | } |
Prabir Pradhan | 4b9b1a1 | 2024-02-02 00:16:17 +0000 | [diff] [blame] | 5428 | CancelationOptions |
| 5429 | options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, |
| 5430 | "The display which contains this window no longer has focus."); |
| 5431 | options.displayId = ADISPLAY_ID_NONE; |
| 5432 | synthesizeCancelationEventsForWindowLocked(windowHandle, options); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5433 | } |
| 5434 | mFocusedDisplayId = displayId; |
| 5435 | |
Chris Ye | 3c2d6f5 | 2020-08-09 10:39:48 -0700 | [diff] [blame] | 5436 | // Find new focused window and validate |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5437 | sp<IBinder> newFocusedWindowToken = mFocusResolver.getFocusedWindowToken(displayId); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5438 | sendFocusChangedCommandLocked(oldFocusedWindowToken, newFocusedWindowToken); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5439 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5440 | if (newFocusedWindowToken == nullptr) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5441 | ALOGW("Focused display #%" PRId32 " does not have a focused window.", displayId); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5442 | if (mFocusResolver.hasFocusedWindowTokens()) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5443 | ALOGE("But another display has a focused window\n%s", |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5444 | mFocusResolver.dumpFocusedWindows().c_str()); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5445 | } |
| 5446 | } |
| 5447 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5448 | } // release lock |
| 5449 | |
| 5450 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5451 | mLooper->wake(); |
| 5452 | } |
| 5453 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5454 | void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5455 | if (DEBUG_FOCUS) { |
| 5456 | ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); |
| 5457 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5458 | |
| 5459 | bool changed; |
| 5460 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5461 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5462 | |
| 5463 | if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { |
| 5464 | if (mDispatchFrozen && !frozen) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5465 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5466 | } |
| 5467 | |
| 5468 | if (mDispatchEnabled && !enabled) { |
| 5469 | resetAndDropEverythingLocked("dispatcher is being disabled"); |
| 5470 | } |
| 5471 | |
| 5472 | mDispatchEnabled = enabled; |
| 5473 | mDispatchFrozen = frozen; |
| 5474 | changed = true; |
| 5475 | } else { |
| 5476 | changed = false; |
| 5477 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5478 | } // release lock |
| 5479 | |
| 5480 | if (changed) { |
| 5481 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5482 | mLooper->wake(); |
| 5483 | } |
| 5484 | } |
| 5485 | |
| 5486 | void InputDispatcher::setInputFilterEnabled(bool enabled) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5487 | if (DEBUG_FOCUS) { |
| 5488 | ALOGD("setInputFilterEnabled: enabled=%d", enabled); |
| 5489 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5490 | |
| 5491 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5492 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5493 | |
| 5494 | if (mInputFilterEnabled == enabled) { |
| 5495 | return; |
| 5496 | } |
| 5497 | |
| 5498 | mInputFilterEnabled = enabled; |
| 5499 | resetAndDropEverythingLocked("input filter is being enabled or disabled"); |
| 5500 | } // release lock |
| 5501 | |
| 5502 | // Wake up poll loop since there might be work to do to drop everything. |
| 5503 | mLooper->wake(); |
| 5504 | } |
| 5505 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5506 | bool InputDispatcher::setInTouchMode(bool inTouchMode, gui::Pid pid, gui::Uid uid, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 5507 | bool hasPermission, int32_t displayId) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5508 | bool needWake = false; |
| 5509 | { |
| 5510 | std::scoped_lock lock(mLock); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5511 | ALOGD_IF(DEBUG_TOUCH_MODE, |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5512 | "Request to change touch mode to %s (calling pid=%s, uid=%s, " |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5513 | "hasPermission=%s, target displayId=%d, mTouchModePerDisplay[displayId]=%s)", |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5514 | toString(inTouchMode), pid.toString().c_str(), uid.toString().c_str(), |
| 5515 | toString(hasPermission), displayId, |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5516 | mTouchModePerDisplay.count(displayId) == 0 |
| 5517 | ? "not set" |
| 5518 | : std::to_string(mTouchModePerDisplay[displayId]).c_str()); |
| 5519 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5520 | auto touchModeIt = mTouchModePerDisplay.find(displayId); |
| 5521 | if (touchModeIt != mTouchModePerDisplay.end() && touchModeIt->second == inTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5522 | return false; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5523 | } |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5524 | if (!hasPermission) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 5525 | if (!focusedWindowIsOwnedByLocked(pid, uid) && |
| 5526 | !recentWindowsAreOwnedByLocked(pid, uid)) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5527 | 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] | 5528 | "window nor none of the previously interacted window", |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5529 | pid.toString().c_str(), uid.toString().c_str()); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5530 | return false; |
| 5531 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5532 | } |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5533 | mTouchModePerDisplay[displayId] = inTouchMode; |
| 5534 | auto entry = std::make_unique<TouchModeEntry>(mIdGenerator.nextId(), now(), inTouchMode, |
| 5535 | displayId); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5536 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 5537 | } // release lock |
| 5538 | |
| 5539 | if (needWake) { |
| 5540 | mLooper->wake(); |
| 5541 | } |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5542 | return true; |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 5543 | } |
| 5544 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5545 | bool InputDispatcher::focusedWindowIsOwnedByLocked(gui::Pid pid, gui::Uid uid) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 5546 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
| 5547 | if (focusedToken == nullptr) { |
| 5548 | return false; |
| 5549 | } |
| 5550 | sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(focusedToken); |
| 5551 | return isWindowOwnedBy(windowHandle, pid, uid); |
| 5552 | } |
| 5553 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5554 | bool InputDispatcher::recentWindowsAreOwnedByLocked(gui::Pid pid, gui::Uid uid) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 5555 | return std::find_if(mInteractionConnectionTokens.begin(), mInteractionConnectionTokens.end(), |
| 5556 | [&](const sp<IBinder>& connectionToken) REQUIRES(mLock) { |
| 5557 | const sp<WindowInfoHandle> windowHandle = |
| 5558 | getWindowHandleLocked(connectionToken); |
| 5559 | return isWindowOwnedBy(windowHandle, pid, uid); |
| 5560 | }) != mInteractionConnectionTokens.end(); |
| 5561 | } |
| 5562 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 5563 | void InputDispatcher::setMaximumObscuringOpacityForTouch(float opacity) { |
| 5564 | if (opacity < 0 || opacity > 1) { |
| 5565 | LOG_ALWAYS_FATAL("Maximum obscuring opacity for touch should be >= 0 and <= 1"); |
| 5566 | return; |
| 5567 | } |
| 5568 | |
| 5569 | std::scoped_lock lock(mLock); |
| 5570 | mMaximumObscuringOpacityForTouch = opacity; |
| 5571 | } |
| 5572 | |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5573 | std::tuple<TouchState*, TouchedWindow*, int32_t /*displayId*/> |
| 5574 | InputDispatcher::findTouchStateWindowAndDisplayLocked(const sp<IBinder>& token) { |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5575 | for (auto& [displayId, state] : mTouchStatesByDisplay) { |
| 5576 | for (TouchedWindow& w : state.windows) { |
| 5577 | if (w.windowHandle->getToken() == token) { |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5578 | return std::make_tuple(&state, &w, displayId); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5579 | } |
| 5580 | } |
| 5581 | } |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5582 | return std::make_tuple(nullptr, nullptr, ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5583 | } |
| 5584 | |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 5585 | bool InputDispatcher::transferTouchGesture(const sp<IBinder>& fromToken, const sp<IBinder>& toToken, |
| 5586 | bool isDragDrop) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 5587 | if (fromToken == toToken) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5588 | if (DEBUG_FOCUS) { |
| 5589 | ALOGD("Trivial transfer to same window."); |
| 5590 | } |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 5591 | return true; |
| 5592 | } |
| 5593 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5594 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5595 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5596 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5597 | // Find the target touch state and touched window by fromToken. |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5598 | auto [state, touchedWindow, displayId] = findTouchStateWindowAndDisplayLocked(fromToken); |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5599 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5600 | if (state == nullptr || touchedWindow == nullptr) { |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5601 | ALOGD("Touch transfer failed because from window is not being touched."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5602 | return false; |
| 5603 | } |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5604 | std::set<int32_t> deviceIds = touchedWindow->getTouchingDeviceIds(); |
| 5605 | if (deviceIds.size() != 1) { |
Siarhei Vishniakou | 827d1ac | 2023-07-21 16:37:51 -0700 | [diff] [blame] | 5606 | LOG(INFO) << "Can't transfer touch. Currently touching devices: " << dumpSet(deviceIds) |
| 5607 | << " for window: " << touchedWindow->dump(); |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5608 | return false; |
| 5609 | } |
| 5610 | const int32_t deviceId = *deviceIds.begin(); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5611 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 5612 | const sp<WindowInfoHandle> fromWindowHandle = touchedWindow->windowHandle; |
| 5613 | const sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(toToken, displayId); |
| 5614 | if (!toWindowHandle) { |
| 5615 | ALOGW("Cannot transfer touch because the transfer target window was not found."); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5616 | return false; |
| 5617 | } |
| 5618 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5619 | if (DEBUG_FOCUS) { |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 5620 | ALOGD("%s: fromWindowHandle=%s, toWindowHandle=%s", __func__, |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5621 | touchedWindow->windowHandle->getName().c_str(), |
| 5622 | toWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5623 | } |
| 5624 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5625 | // Erase old window. |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5626 | ftl::Flags<InputTarget::Flags> oldTargetFlags = touchedWindow->targetFlags; |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 5627 | std::vector<PointerProperties> pointers = touchedWindow->getTouchingPointers(deviceId); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5628 | state->removeWindowByToken(fromToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5629 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5630 | // Add new window. |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 5631 | nsecs_t downTimeInTarget = now(); |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5632 | ftl::Flags<InputTarget::Flags> newTargetFlags = |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 5633 | oldTargetFlags & (InputTarget::Flags::SPLIT); |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 5634 | if (canReceiveForegroundTouches(*toWindowHandle->getInfo())) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5635 | newTargetFlags |= InputTarget::Flags::FOREGROUND; |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 5636 | } |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 5637 | // Transferring touch focus using this API should not effect the focused window. |
| 5638 | newTargetFlags |= InputTarget::Flags::NO_FOCUS_CHANGE; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 5639 | state->addOrUpdateWindow(toWindowHandle, InputTarget::DispatchMode::AS_IS, newTargetFlags, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 5640 | deviceId, pointers, downTimeInTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5641 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5642 | // Store the dragging window. |
| 5643 | if (isDragDrop) { |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 5644 | if (pointers.size() != 1) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 5645 | ALOGW("The drag and drop cannot be started when there is no pointer or more than 1" |
| 5646 | " pointer on the window."); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 5647 | return false; |
| 5648 | } |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 5649 | // Track the pointer id for drag window and generate the drag state. |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 5650 | const size_t id = pointers.begin()->id; |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 5651 | mDragState = std::make_unique<DragState>(toWindowHandle, id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5652 | } |
| 5653 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5654 | // Synthesize cancel for old window and down for new window. |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 5655 | std::shared_ptr<Connection> fromConnection = getConnectionLocked(fromToken); |
| 5656 | std::shared_ptr<Connection> toConnection = getConnectionLocked(toToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5657 | if (fromConnection != nullptr && toConnection != nullptr) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5658 | fromConnection->inputState.mergePointerStateTo(toConnection->inputState); |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5659 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
| 5660 | "transferring touch from this window to another window"); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 5661 | synthesizeCancelationEventsForWindowLocked(fromWindowHandle, options, fromConnection); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5662 | synthesizePointerDownEventsForConnectionLocked(downTimeInTarget, toConnection, |
| 5663 | newTargetFlags); |
| 5664 | |
| 5665 | // Check if the wallpaper window should deliver the corresponding event. |
| 5666 | transferWallpaperTouch(oldTargetFlags, newTargetFlags, fromWindowHandle, toWindowHandle, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 5667 | *state, deviceId, pointers); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5668 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5669 | } // release lock |
| 5670 | |
| 5671 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5672 | mLooper->wake(); |
| 5673 | return true; |
| 5674 | } |
| 5675 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5676 | /** |
| 5677 | * Get the touched foreground window on the given display. |
| 5678 | * Return null if there are no windows touched on that display, or if more than one foreground |
| 5679 | * window is being touched. |
| 5680 | */ |
| 5681 | sp<WindowInfoHandle> InputDispatcher::findTouchedForegroundWindowLocked(int32_t displayId) const { |
| 5682 | auto stateIt = mTouchStatesByDisplay.find(displayId); |
| 5683 | if (stateIt == mTouchStatesByDisplay.end()) { |
| 5684 | ALOGI("No touch state on display %" PRId32, displayId); |
| 5685 | return nullptr; |
| 5686 | } |
| 5687 | |
| 5688 | const TouchState& state = stateIt->second; |
| 5689 | sp<WindowInfoHandle> touchedForegroundWindow; |
| 5690 | // If multiple foreground windows are touched, return nullptr |
| 5691 | for (const TouchedWindow& window : state.windows) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5692 | if (window.targetFlags.test(InputTarget::Flags::FOREGROUND)) { |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5693 | if (touchedForegroundWindow != nullptr) { |
| 5694 | ALOGI("Two or more foreground windows: %s and %s", |
| 5695 | touchedForegroundWindow->getName().c_str(), |
| 5696 | window.windowHandle->getName().c_str()); |
| 5697 | return nullptr; |
| 5698 | } |
| 5699 | touchedForegroundWindow = window.windowHandle; |
| 5700 | } |
| 5701 | } |
| 5702 | return touchedForegroundWindow; |
| 5703 | } |
| 5704 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5705 | // Binder call |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 5706 | bool InputDispatcher::transferTouchOnDisplay(const sp<IBinder>& destChannelToken, |
| 5707 | int32_t displayId) { |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5708 | sp<IBinder> fromToken; |
| 5709 | { // acquire lock |
| 5710 | std::scoped_lock _l(mLock); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5711 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(destChannelToken, displayId); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5712 | if (toWindowHandle == nullptr) { |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5713 | ALOGW("Could not find window associated with token=%p on display %" PRId32, |
| 5714 | destChannelToken.get(), displayId); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5715 | return false; |
| 5716 | } |
| 5717 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5718 | sp<WindowInfoHandle> from = findTouchedForegroundWindowLocked(displayId); |
| 5719 | if (from == nullptr) { |
| 5720 | ALOGE("Could not find a source window in %s for %p", __func__, destChannelToken.get()); |
| 5721 | return false; |
| 5722 | } |
| 5723 | |
| 5724 | fromToken = from->getToken(); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5725 | } // release lock |
| 5726 | |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 5727 | return transferTouchGesture(fromToken, destChannelToken); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5728 | } |
| 5729 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5730 | void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5731 | if (DEBUG_FOCUS) { |
| 5732 | ALOGD("Resetting and dropping all events (%s).", reason); |
| 5733 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5734 | |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 5735 | CancelationOptions options(CancelationOptions::Mode::CANCEL_ALL_EVENTS, reason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5736 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 5737 | |
| 5738 | resetKeyRepeatLocked(); |
| 5739 | releasePendingEventLocked(); |
| 5740 | drainInboundQueueLocked(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5741 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5742 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5743 | mAnrTracker.clear(); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5744 | mTouchStatesByDisplay.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5745 | } |
| 5746 | |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 5747 | void InputDispatcher::logDispatchStateLocked() const { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5748 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5749 | dumpDispatchStateLocked(dump); |
| 5750 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5751 | std::istringstream stream(dump); |
| 5752 | std::string line; |
| 5753 | |
| 5754 | while (std::getline(stream, line, '\n')) { |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 5755 | ALOGI("%s", line.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5756 | } |
| 5757 | } |
| 5758 | |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 5759 | std::string InputDispatcher::dumpPointerCaptureStateLocked() const { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5760 | std::string dump; |
| 5761 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5762 | dump += StringPrintf(INDENT "Pointer Capture Requested: %s\n", |
| 5763 | toString(mCurrentPointerCaptureRequest.enable)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5764 | |
| 5765 | std::string windowName = "None"; |
| 5766 | if (mWindowTokenWithPointerCapture) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5767 | const sp<WindowInfoHandle> captureWindowHandle = |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5768 | getWindowHandleLocked(mWindowTokenWithPointerCapture); |
| 5769 | windowName = captureWindowHandle ? captureWindowHandle->getName().c_str() |
| 5770 | : "token has capture without window"; |
| 5771 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5772 | 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] | 5773 | |
| 5774 | return dump; |
| 5775 | } |
| 5776 | |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 5777 | void InputDispatcher::dumpDispatchStateLocked(std::string& dump) const { |
Siarhei Vishniakou | 043a3ec | 2019-05-01 11:30:46 -0700 | [diff] [blame] | 5778 | dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled)); |
| 5779 | dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen)); |
| 5780 | dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5781 | dump += StringPrintf(INDENT "FocusedDisplayId: %" PRId32 "\n", mFocusedDisplayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5782 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5783 | if (!mFocusedApplicationHandlesByDisplay.empty()) { |
| 5784 | dump += StringPrintf(INDENT "FocusedApplications:\n"); |
| 5785 | for (auto& it : mFocusedApplicationHandlesByDisplay) { |
| 5786 | const int32_t displayId = it.first; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5787 | const std::shared_ptr<InputApplicationHandle>& applicationHandle = it.second; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5788 | const std::chrono::duration timeout = |
| 5789 | applicationHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5790 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5791 | ", name='%s', dispatchingTimeout=%" PRId64 "ms\n", |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5792 | displayId, applicationHandle->getName().c_str(), millis(timeout)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5793 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5794 | } else { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5795 | dump += StringPrintf(INDENT "FocusedApplications: <none>\n"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5796 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5797 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5798 | dump += mFocusResolver.dump(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5799 | dump += dumpPointerCaptureStateLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5800 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5801 | if (!mTouchStatesByDisplay.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5802 | dump += StringPrintf(INDENT "TouchStatesByDisplay:\n"); |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5803 | for (const auto& [displayId, state] : mTouchStatesByDisplay) { |
Siarhei Vishniakou | 6e1e987 | 2022-11-08 17:51:35 -0800 | [diff] [blame] | 5804 | std::string touchStateDump = addLinePrefix(state.dump(), INDENT2); |
| 5805 | dump += INDENT2 + std::to_string(displayId) + " : " + touchStateDump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5806 | } |
| 5807 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5808 | dump += INDENT "TouchStates: <no displays touched>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5809 | } |
| 5810 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5811 | if (mDragState) { |
| 5812 | dump += StringPrintf(INDENT "DragState:\n"); |
| 5813 | mDragState->dump(dump, INDENT2); |
| 5814 | } |
| 5815 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5816 | if (!mWindowHandlesByDisplay.empty()) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5817 | for (const auto& [displayId, windowHandles] : mWindowHandlesByDisplay) { |
| 5818 | dump += StringPrintf(INDENT "Display: %" PRId32 "\n", displayId); |
| 5819 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
| 5820 | const auto& displayInfo = it->second; |
| 5821 | dump += StringPrintf(INDENT2 "logicalSize=%dx%d\n", displayInfo.logicalWidth, |
| 5822 | displayInfo.logicalHeight); |
| 5823 | displayInfo.transform.dump(dump, "transform", INDENT4); |
| 5824 | } else { |
| 5825 | dump += INDENT2 "No DisplayInfo found!\n"; |
| 5826 | } |
| 5827 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5828 | if (!windowHandles.empty()) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5829 | dump += INDENT2 "Windows:\n"; |
| 5830 | for (size_t i = 0; i < windowHandles.size(); i++) { |
Siarhei Vishniakou | 366fb5b | 2023-12-06 11:23:41 -0800 | [diff] [blame] | 5831 | dump += StringPrintf(INDENT3 "%zu: %s", i, |
| 5832 | streamableToString(*windowHandles[i]).c_str()); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5833 | } |
| 5834 | } else { |
| 5835 | dump += INDENT2 "Windows: <none>\n"; |
| 5836 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5837 | } |
| 5838 | } else { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5839 | dump += INDENT "Displays: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5840 | } |
| 5841 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5842 | if (!mGlobalMonitorsByDisplay.empty()) { |
| 5843 | for (const auto& [displayId, monitors] : mGlobalMonitorsByDisplay) { |
| 5844 | dump += StringPrintf(INDENT "Global monitors on display %d:\n", displayId); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5845 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5846 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5847 | } else { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5848 | dump += INDENT "Global Monitors: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5849 | } |
| 5850 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5851 | const nsecs_t currentTime = now(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5852 | |
| 5853 | // Dump recently dispatched or dropped events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5854 | if (!mRecentQueue.empty()) { |
| 5855 | dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size()); |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 5856 | for (const std::shared_ptr<const EventEntry>& entry : mRecentQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5857 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5858 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5859 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5860 | } |
| 5861 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5862 | dump += INDENT "RecentQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5863 | } |
| 5864 | |
| 5865 | // Dump event currently being dispatched. |
| 5866 | if (mPendingEvent) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5867 | dump += INDENT "PendingEvent:\n"; |
| 5868 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5869 | dump += mPendingEvent->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5870 | dump += StringPrintf(", age=%" PRId64 "ms\n", |
| 5871 | ns2ms(currentTime - mPendingEvent->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5872 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5873 | dump += INDENT "PendingEvent: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5874 | } |
| 5875 | |
| 5876 | // Dump inbound events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5877 | if (!mInboundQueue.empty()) { |
| 5878 | dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size()); |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 5879 | for (const std::shared_ptr<const EventEntry>& entry : mInboundQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5880 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5881 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5882 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5883 | } |
| 5884 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5885 | dump += INDENT "InboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5886 | } |
| 5887 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5888 | if (!mCommandQueue.empty()) { |
| 5889 | dump += StringPrintf(INDENT "CommandQueue: size=%zu\n", mCommandQueue.size()); |
| 5890 | } else { |
| 5891 | dump += INDENT "CommandQueue: <empty>\n"; |
| 5892 | } |
| 5893 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5894 | if (!mConnectionsByToken.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5895 | dump += INDENT "Connections:\n"; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5896 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 10bfdc9 | 2024-02-01 11:12:57 -0800 | [diff] [blame] | 5897 | dump += StringPrintf(INDENT2 "%i: channelName='%s', " |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5898 | "status=%s, monitor=%s, responsive=%s\n", |
Siarhei Vishniakou | 7b9f4f5 | 2024-02-02 13:07:16 -0800 | [diff] [blame] | 5899 | connection->inputPublisher.getChannel().getFd(), |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5900 | connection->getInputChannelName().c_str(), |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5901 | ftl::enum_string(connection->status).c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5902 | toString(connection->monitor), toString(connection->responsive)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5903 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5904 | if (!connection->outboundQueue.empty()) { |
| 5905 | dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n", |
| 5906 | connection->outboundQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5907 | dump += dumpQueue(connection->outboundQueue, currentTime); |
| 5908 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5909 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5910 | dump += INDENT3 "OutboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5911 | } |
| 5912 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5913 | if (!connection->waitQueue.empty()) { |
| 5914 | dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n", |
| 5915 | connection->waitQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5916 | dump += dumpQueue(connection->waitQueue, currentTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5917 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5918 | dump += INDENT3 "WaitQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5919 | } |
Siarhei Vishniakou | 366fb5b | 2023-12-06 11:23:41 -0800 | [diff] [blame] | 5920 | std::string inputStateDump = streamableToString(connection->inputState); |
| 5921 | if (!inputStateDump.empty()) { |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 5922 | dump += INDENT3 "InputState: "; |
Siarhei Vishniakou | 366fb5b | 2023-12-06 11:23:41 -0800 | [diff] [blame] | 5923 | dump += inputStateDump + "\n"; |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 5924 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5925 | } |
| 5926 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5927 | dump += INDENT "Connections: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5928 | } |
| 5929 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5930 | if (!mTouchModePerDisplay.empty()) { |
| 5931 | dump += INDENT "TouchModePerDisplay:\n"; |
| 5932 | for (const auto& [displayId, touchMode] : mTouchModePerDisplay) { |
| 5933 | dump += StringPrintf(INDENT2 "Display: %" PRId32 " TouchMode: %s\n", displayId, |
| 5934 | std::to_string(touchMode).c_str()); |
| 5935 | } |
| 5936 | } else { |
| 5937 | dump += INDENT "TouchModePerDisplay: <none>\n"; |
| 5938 | } |
| 5939 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5940 | dump += INDENT "Configuration:\n"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5941 | dump += StringPrintf(INDENT2 "KeyRepeatDelay: %" PRId64 "ms\n", ns2ms(mConfig.keyRepeatDelay)); |
| 5942 | dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n", |
| 5943 | ns2ms(mConfig.keyRepeatTimeout)); |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 5944 | dump += mLatencyTracker.dump(INDENT2); |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 5945 | dump += mLatencyAggregator.dump(INDENT2); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 5946 | dump += INDENT "InputTracer: "; |
| 5947 | dump += mTracer == nullptr ? "Disabled" : "Enabled"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5948 | } |
| 5949 | |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 5950 | void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) const { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5951 | const size_t numMonitors = monitors.size(); |
| 5952 | for (size_t i = 0; i < numMonitors; i++) { |
| 5953 | const Monitor& monitor = monitors[i]; |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 5954 | const std::shared_ptr<Connection>& connection = monitor.connection; |
| 5955 | dump += StringPrintf(INDENT2 "%zu: '%s', ", i, connection->getInputChannelName().c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5956 | dump += "\n"; |
| 5957 | } |
| 5958 | } |
| 5959 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5960 | class LooperEventCallback : public LooperCallback { |
| 5961 | public: |
| 5962 | LooperEventCallback(std::function<int(int events)> callback) : mCallback(callback) {} |
| 5963 | int handleEvent(int /*fd*/, int events, void* /*data*/) override { return mCallback(events); } |
| 5964 | |
| 5965 | private: |
| 5966 | std::function<int(int events)> mCallback; |
| 5967 | }; |
| 5968 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5969 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputChannel(const std::string& name) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5970 | if (DEBUG_CHANNEL_CREATION) { |
| 5971 | ALOGD("channel '%s' ~ createInputChannel", name.c_str()); |
| 5972 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5973 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5974 | std::unique_ptr<InputChannel> serverChannel; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5975 | std::unique_ptr<InputChannel> clientChannel; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5976 | status_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5977 | |
| 5978 | if (result) { |
| 5979 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5980 | } |
| 5981 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5982 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5983 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5984 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Siarhei Vishniakou | 8d66013 | 2024-01-11 16:48:44 -0800 | [diff] [blame] | 5985 | const int fd = serverChannel->getFd(); |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 5986 | std::shared_ptr<Connection> connection = |
| 5987 | std::make_shared<Connection>(std::move(serverChannel), /*monitor=*/false, |
| 5988 | mIdGenerator); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5989 | |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 5990 | auto [_, inserted] = mConnectionsByToken.try_emplace(token, connection); |
| 5991 | if (!inserted) { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5992 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5993 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5994 | |
| 5995 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5996 | this, std::placeholders::_1, token); |
| 5997 | |
Siarhei Vishniakou | 8d66013 | 2024-01-11 16:48:44 -0800 | [diff] [blame] | 5998 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, sp<LooperEventCallback>::make(callback), |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5999 | nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6000 | } // release lock |
| 6001 | |
| 6002 | // Wake the looper because some connections have changed. |
| 6003 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 6004 | return clientChannel; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6005 | } |
| 6006 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 6007 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputMonitor(int32_t displayId, |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 6008 | const std::string& name, |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6009 | gui::Pid pid) { |
Siarhei Vishniakou | 7b9f4f5 | 2024-02-02 13:07:16 -0800 | [diff] [blame] | 6010 | std::unique_ptr<InputChannel> serverChannel; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 6011 | std::unique_ptr<InputChannel> clientChannel; |
Siarhei Vishniakou | 7b9f4f5 | 2024-02-02 13:07:16 -0800 | [diff] [blame] | 6012 | status_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 6013 | if (result) { |
| 6014 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 6015 | } |
| 6016 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6017 | { // acquire lock |
| 6018 | std::scoped_lock _l(mLock); |
| 6019 | |
| 6020 | if (displayId < 0) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 6021 | return base::Error(BAD_VALUE) << "Attempted to create input monitor with name " << name |
| 6022 | << " without a specified display."; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6023 | } |
| 6024 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 6025 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Siarhei Vishniakou | 8d66013 | 2024-01-11 16:48:44 -0800 | [diff] [blame] | 6026 | const int fd = serverChannel->getFd(); |
Siarhei Vishniakou | 7b9f4f5 | 2024-02-02 13:07:16 -0800 | [diff] [blame] | 6027 | std::shared_ptr<Connection> connection = |
| 6028 | std::make_shared<Connection>(std::move(serverChannel), /*monitor=*/true, |
| 6029 | mIdGenerator); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 6030 | |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6031 | auto [_, inserted] = mConnectionsByToken.emplace(token, connection); |
| 6032 | if (!inserted) { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 6033 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 6034 | } |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6035 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 6036 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 6037 | this, std::placeholders::_1, token); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6038 | |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6039 | mGlobalMonitorsByDisplay[displayId].emplace_back(connection, pid); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6040 | |
Siarhei Vishniakou | 8d66013 | 2024-01-11 16:48:44 -0800 | [diff] [blame] | 6041 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, sp<LooperEventCallback>::make(callback), |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6042 | nullptr); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6043 | } |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 6044 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6045 | // Wake the looper because some connections have changed. |
| 6046 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 6047 | return clientChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6048 | } |
| 6049 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 6050 | status_t InputDispatcher::removeInputChannel(const sp<IBinder>& connectionToken) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6051 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6052 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6053 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6054 | status_t status = removeInputChannelLocked(connectionToken, /*notify=*/false); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6055 | if (status) { |
| 6056 | return status; |
| 6057 | } |
| 6058 | } // release lock |
| 6059 | |
| 6060 | // Wake the poll loop because removing the connection may have changed the current |
| 6061 | // synchronization state. |
| 6062 | mLooper->wake(); |
| 6063 | return OK; |
| 6064 | } |
| 6065 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 6066 | status_t InputDispatcher::removeInputChannelLocked(const sp<IBinder>& connectionToken, |
| 6067 | bool notify) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6068 | std::shared_ptr<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 6069 | if (connection == nullptr) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 6070 | // 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] | 6071 | return BAD_VALUE; |
| 6072 | } |
| 6073 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6074 | removeConnectionLocked(connection); |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 6075 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6076 | if (connection->monitor) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 6077 | removeMonitorChannelLocked(connectionToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6078 | } |
| 6079 | |
Siarhei Vishniakou | 7b9f4f5 | 2024-02-02 13:07:16 -0800 | [diff] [blame] | 6080 | mLooper->removeFd(connection->inputPublisher.getChannel().getFd()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6081 | |
| 6082 | nsecs_t currentTime = now(); |
| 6083 | abortBrokenDispatchCycleLocked(currentTime, connection, notify); |
| 6084 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 6085 | connection->status = Connection::Status::ZOMBIE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6086 | return OK; |
| 6087 | } |
| 6088 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 6089 | void InputDispatcher::removeMonitorChannelLocked(const sp<IBinder>& connectionToken) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6090 | for (auto it = mGlobalMonitorsByDisplay.begin(); it != mGlobalMonitorsByDisplay.end();) { |
| 6091 | auto& [displayId, monitors] = *it; |
| 6092 | std::erase_if(monitors, [connectionToken](const Monitor& monitor) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6093 | return monitor.connection->getToken() == connectionToken; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6094 | }); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6095 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6096 | if (monitors.empty()) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6097 | it = mGlobalMonitorsByDisplay.erase(it); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6098 | } else { |
| 6099 | ++it; |
| 6100 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6101 | } |
| 6102 | } |
| 6103 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6104 | status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6105 | std::scoped_lock _l(mLock); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 6106 | return pilferPointersLocked(token); |
| 6107 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6108 | |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 6109 | status_t InputDispatcher::pilferPointersLocked(const sp<IBinder>& token) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6110 | const std::shared_ptr<Connection> requestingConnection = getConnectionLocked(token); |
| 6111 | if (!requestingConnection) { |
Siarhei Vishniakou | 8384e0d | 2023-09-18 18:48:27 -0700 | [diff] [blame] | 6112 | LOG(WARNING) |
| 6113 | << "Attempted to pilfer pointers from an un-registered channel or invalid token"; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6114 | return BAD_VALUE; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6115 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6116 | |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 6117 | auto [statePtr, windowPtr, displayId] = findTouchStateWindowAndDisplayLocked(token); |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 6118 | if (statePtr == nullptr || windowPtr == nullptr) { |
Siarhei Vishniakou | 8384e0d | 2023-09-18 18:48:27 -0700 | [diff] [blame] | 6119 | LOG(WARNING) |
| 6120 | << "Attempted to pilfer points from a channel without any on-going pointer streams." |
| 6121 | " Ignoring."; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6122 | return BAD_VALUE; |
| 6123 | } |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 6124 | std::set<int32_t> deviceIds = windowPtr->getTouchingDeviceIds(); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 6125 | if (deviceIds.empty()) { |
| 6126 | LOG(WARNING) << "Can't pilfer: no touching devices in window: " << windowPtr->dump(); |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 6127 | return BAD_VALUE; |
| 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 | for (const DeviceId deviceId : deviceIds) { |
| 6131 | TouchState& state = *statePtr; |
| 6132 | TouchedWindow& window = *windowPtr; |
| 6133 | // Send cancel events to all the input channels we're stealing from. |
| 6134 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
| 6135 | "input channel stole pointer stream"); |
| 6136 | options.deviceId = deviceId; |
| 6137 | options.displayId = displayId; |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 6138 | std::vector<PointerProperties> pointers = window.getTouchingPointers(deviceId); |
| 6139 | std::bitset<MAX_POINTER_ID + 1> pointerIds = getPointerIds(pointers); |
Siarhei Vishniakou | 8384e0d | 2023-09-18 18:48:27 -0700 | [diff] [blame] | 6140 | options.pointerIds = pointerIds; |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 6141 | |
Siarhei Vishniakou | 8384e0d | 2023-09-18 18:48:27 -0700 | [diff] [blame] | 6142 | std::string canceledWindows; |
| 6143 | for (const TouchedWindow& w : state.windows) { |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 6144 | if (w.windowHandle->getToken() != token) { |
| 6145 | synthesizeCancelationEventsForWindowLocked(w.windowHandle, options); |
Siarhei Vishniakou | 8384e0d | 2023-09-18 18:48:27 -0700 | [diff] [blame] | 6146 | canceledWindows += canceledWindows.empty() ? "[" : ", "; |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 6147 | canceledWindows += w.windowHandle->getName(); |
Siarhei Vishniakou | 8384e0d | 2023-09-18 18:48:27 -0700 | [diff] [blame] | 6148 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6149 | } |
Siarhei Vishniakou | 8384e0d | 2023-09-18 18:48:27 -0700 | [diff] [blame] | 6150 | canceledWindows += canceledWindows.empty() ? "[]" : "]"; |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6151 | LOG(INFO) << "Channel " << requestingConnection->getInputChannelName() |
Siarhei Vishniakou | 8384e0d | 2023-09-18 18:48:27 -0700 | [diff] [blame] | 6152 | << " is stealing input gesture for device " << deviceId << " from " |
| 6153 | << canceledWindows; |
| 6154 | |
| 6155 | // Prevent the gesture from being sent to any other windows. |
| 6156 | // This only blocks relevant pointers to be sent to other windows |
| 6157 | window.addPilferingPointers(deviceId, pointerIds); |
| 6158 | |
| 6159 | state.cancelPointersForWindowsExcept(deviceId, pointerIds, token); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6160 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6161 | return OK; |
| 6162 | } |
| 6163 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6164 | void InputDispatcher::requestPointerCapture(const sp<IBinder>& windowToken, bool enabled) { |
| 6165 | { // acquire lock |
| 6166 | std::scoped_lock _l(mLock); |
| 6167 | if (DEBUG_FOCUS) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 6168 | const sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(windowToken); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6169 | ALOGI("Request to %s Pointer Capture from: %s.", enabled ? "enable" : "disable", |
| 6170 | windowHandle != nullptr ? windowHandle->getName().c_str() |
| 6171 | : "token without window"); |
| 6172 | } |
| 6173 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6174 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6175 | if (focusedToken != windowToken) { |
| 6176 | ALOGW("Ignoring request to %s Pointer Capture: window does not have focus.", |
| 6177 | enabled ? "enable" : "disable"); |
| 6178 | return; |
| 6179 | } |
| 6180 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6181 | if (enabled == mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6182 | ALOGW("Ignoring request to %s Pointer Capture: " |
| 6183 | "window has %s requested pointer capture.", |
| 6184 | enabled ? "enable" : "disable", enabled ? "already" : "not"); |
| 6185 | return; |
| 6186 | } |
| 6187 | |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 6188 | if (enabled) { |
| 6189 | if (std::find(mIneligibleDisplaysForPointerCapture.begin(), |
| 6190 | mIneligibleDisplaysForPointerCapture.end(), |
| 6191 | mFocusedDisplayId) != mIneligibleDisplaysForPointerCapture.end()) { |
| 6192 | ALOGW("Ignoring request to enable Pointer Capture: display is not eligible"); |
| 6193 | return; |
| 6194 | } |
| 6195 | } |
| 6196 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6197 | setPointerCaptureLocked(enabled); |
| 6198 | } // release lock |
| 6199 | |
| 6200 | // Wake the thread to process command entries. |
| 6201 | mLooper->wake(); |
| 6202 | } |
| 6203 | |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 6204 | void InputDispatcher::setDisplayEligibilityForPointerCapture(int32_t displayId, bool isEligible) { |
| 6205 | { // acquire lock |
| 6206 | std::scoped_lock _l(mLock); |
| 6207 | std::erase(mIneligibleDisplaysForPointerCapture, displayId); |
| 6208 | if (!isEligible) { |
| 6209 | mIneligibleDisplaysForPointerCapture.push_back(displayId); |
| 6210 | } |
| 6211 | } // release lock |
| 6212 | } |
| 6213 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6214 | std::optional<gui::Pid> InputDispatcher::findMonitorPidByTokenLocked(const sp<IBinder>& token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6215 | for (const auto& [_, monitors] : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6216 | for (const Monitor& monitor : monitors) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6217 | if (monitor.connection->getToken() == token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6218 | return monitor.pid; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6219 | } |
| 6220 | } |
| 6221 | } |
| 6222 | return std::nullopt; |
| 6223 | } |
| 6224 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6225 | std::shared_ptr<Connection> InputDispatcher::getConnectionLocked( |
| 6226 | const sp<IBinder>& inputConnectionToken) const { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 6227 | if (inputConnectionToken == nullptr) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 6228 | return nullptr; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 6229 | } |
| 6230 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 6231 | for (const auto& [token, connection] : mConnectionsByToken) { |
| 6232 | if (token == inputConnectionToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 6233 | return connection; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6234 | } |
| 6235 | } |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 6236 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 6237 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6238 | } |
| 6239 | |
Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 6240 | std::string InputDispatcher::getConnectionNameLocked(const sp<IBinder>& connectionToken) const { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6241 | std::shared_ptr<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 6242 | if (connection == nullptr) { |
| 6243 | return "<nullptr>"; |
| 6244 | } |
| 6245 | return connection->getInputChannelName(); |
| 6246 | } |
| 6247 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6248 | void InputDispatcher::removeConnectionLocked(const std::shared_ptr<Connection>& connection) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6249 | mAnrTracker.eraseToken(connection->getToken()); |
| 6250 | mConnectionsByToken.erase(connection->getToken()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6251 | } |
| 6252 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6253 | void InputDispatcher::doDispatchCycleFinishedCommand(nsecs_t finishTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6254 | const std::shared_ptr<Connection>& connection, |
| 6255 | uint32_t seq, bool handled, |
| 6256 | nsecs_t consumeTime) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6257 | // Handle post-event policy actions. |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 6258 | std::unique_ptr<const KeyEntry> fallbackKeyEntry; |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6259 | |
| 6260 | { // Start critical section |
| 6261 | auto dispatchEntryIt = |
| 6262 | std::find_if(connection->waitQueue.begin(), connection->waitQueue.end(), |
| 6263 | [seq](auto& e) { return e->seq == seq; }); |
| 6264 | if (dispatchEntryIt == connection->waitQueue.end()) { |
| 6265 | return; |
| 6266 | } |
| 6267 | |
| 6268 | DispatchEntry& dispatchEntry = **dispatchEntryIt; |
| 6269 | |
| 6270 | const nsecs_t eventDuration = finishTime - dispatchEntry.deliveryTime; |
| 6271 | if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) { |
Siarhei Vishniakou | 10bfdc9 | 2024-02-01 11:12:57 -0800 | [diff] [blame] | 6272 | ALOGI("%s spent %" PRId64 "ms processing %s", connection->getInputChannelName().c_str(), |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6273 | ns2ms(eventDuration), dispatchEntry.eventEntry->getDescription().c_str()); |
| 6274 | } |
| 6275 | if (shouldReportFinishedEvent(dispatchEntry, *connection)) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6276 | mLatencyTracker.trackFinishedEvent(dispatchEntry.eventEntry->id, connection->getToken(), |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6277 | dispatchEntry.deliveryTime, consumeTime, finishTime); |
| 6278 | } |
| 6279 | |
| 6280 | if (dispatchEntry.eventEntry->type == EventEntry::Type::KEY) { |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6281 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(*(dispatchEntry.eventEntry)); |
| 6282 | fallbackKeyEntry = |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6283 | afterKeyEventLockedInterruptable(connection, dispatchEntry, keyEntry, handled); |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6284 | } |
| 6285 | } // End critical section: The -LockedInterruptable methods may have released the lock. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6286 | |
| 6287 | // Dequeue the event and start the next cycle. |
| 6288 | // Because the lock might have been released, it is possible that the |
| 6289 | // contents of the wait queue to have been drained, so we need to double-check |
| 6290 | // a few things. |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6291 | auto entryIt = std::find_if(connection->waitQueue.begin(), connection->waitQueue.end(), |
| 6292 | [seq](auto& e) { return e->seq == seq; }); |
| 6293 | if (entryIt != connection->waitQueue.end()) { |
| 6294 | std::unique_ptr<DispatchEntry> dispatchEntry = std::move(*entryIt); |
| 6295 | connection->waitQueue.erase(entryIt); |
| 6296 | |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6297 | const sp<IBinder>& connectionToken = connection->getToken(); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6298 | mAnrTracker.erase(dispatchEntry->timeoutTime, connectionToken); |
| 6299 | if (!connection->responsive) { |
| 6300 | connection->responsive = isConnectionResponsive(*connection); |
| 6301 | if (connection->responsive) { |
| 6302 | // The connection was unresponsive, and now it's responsive. |
| 6303 | processConnectionResponsiveLocked(*connection); |
| 6304 | } |
| 6305 | } |
| 6306 | traceWaitQueueLength(*connection); |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6307 | if (fallbackKeyEntry && connection->status == Connection::Status::NORMAL) { |
Prabir Pradhan | fa2c69f | 2024-02-01 20:31:34 +0000 | [diff] [blame] | 6308 | const auto windowHandle = getWindowHandleLocked(connection->getToken()); |
| 6309 | // Only dispatch fallbacks if there is a window for the connection. |
| 6310 | if (windowHandle != nullptr) { |
| 6311 | const auto inputTarget = |
| 6312 | createInputTargetLocked(windowHandle, InputTarget::DispatchMode::AS_IS, |
| 6313 | dispatchEntry->targetFlags, |
| 6314 | fallbackKeyEntry->downTime); |
| 6315 | if (inputTarget.has_value()) { |
| 6316 | enqueueDispatchEntryLocked(connection, std::move(fallbackKeyEntry), |
| 6317 | *inputTarget); |
| 6318 | } |
| 6319 | } |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6320 | } |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6321 | releaseDispatchEntry(std::move(dispatchEntry)); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6322 | } |
| 6323 | |
| 6324 | // Start the next dispatch cycle for this connection. |
| 6325 | startDispatchCycleLocked(now(), connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6326 | } |
| 6327 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6328 | void InputDispatcher::sendFocusChangedCommandLocked(const sp<IBinder>& oldToken, |
| 6329 | const sp<IBinder>& newToken) { |
| 6330 | auto command = [this, oldToken, newToken]() REQUIRES(mLock) { |
| 6331 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6332 | mPolicy.notifyFocusChanged(oldToken, newToken); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6333 | }; |
| 6334 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6335 | } |
| 6336 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6337 | void InputDispatcher::sendDropWindowCommandLocked(const sp<IBinder>& token, float x, float y) { |
| 6338 | auto command = [this, token, x, y]() REQUIRES(mLock) { |
| 6339 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6340 | mPolicy.notifyDropWindow(token, x, y); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6341 | }; |
| 6342 | postCommandLocked(std::move(command)); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 6343 | } |
| 6344 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6345 | void InputDispatcher::onAnrLocked(const std::shared_ptr<Connection>& connection) { |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6346 | if (connection == nullptr) { |
| 6347 | LOG_ALWAYS_FATAL("Caller must check for nullness"); |
| 6348 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6349 | // Since we are allowing the policy to extend the timeout, maybe the waitQueue |
| 6350 | // is already healthy again. Don't raise ANR in this situation |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6351 | if (connection->waitQueue.empty()) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6352 | ALOGI("Not raising ANR because the connection %s has recovered", |
Siarhei Vishniakou | 10bfdc9 | 2024-02-01 11:12:57 -0800 | [diff] [blame] | 6353 | connection->getInputChannelName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6354 | return; |
| 6355 | } |
| 6356 | /** |
| 6357 | * The "oldestEntry" is the entry that was first sent to the application. That entry, however, |
| 6358 | * may not be the one that caused the timeout to occur. One possibility is that window timeout |
| 6359 | * has changed. This could cause newer entries to time out before the already dispatched |
| 6360 | * entries. In that situation, the newest entries caused ANR. But in all likelihood, the app |
| 6361 | * processes the events linearly. So providing information about the oldest entry seems to be |
| 6362 | * most useful. |
| 6363 | */ |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6364 | DispatchEntry& oldestEntry = *connection->waitQueue.front(); |
| 6365 | const nsecs_t currentWait = now() - oldestEntry.deliveryTime; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6366 | std::string reason = |
| 6367 | android::base::StringPrintf("%s is not responding. Waited %" PRId64 "ms for %s", |
Siarhei Vishniakou | 10bfdc9 | 2024-02-01 11:12:57 -0800 | [diff] [blame] | 6368 | connection->getInputChannelName().c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6369 | ns2ms(currentWait), |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6370 | oldestEntry.eventEntry->getDescription().c_str()); |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6371 | sp<IBinder> connectionToken = connection->getToken(); |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 6372 | updateLastAnrStateLocked(getWindowHandleLocked(connectionToken), reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6373 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6374 | processConnectionUnresponsiveLocked(*connection, std::move(reason)); |
| 6375 | |
| 6376 | // Stop waking up for events on this connection, it is already unresponsive |
| 6377 | cancelEventsForAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6378 | } |
| 6379 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6380 | void InputDispatcher::onAnrLocked(std::shared_ptr<InputApplicationHandle> application) { |
| 6381 | std::string reason = |
| 6382 | StringPrintf("%s does not have a focused window", application->getName().c_str()); |
| 6383 | updateLastAnrStateLocked(*application, reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6384 | |
Yabin Cui | 8eb9c55 | 2023-06-08 18:05:07 +0000 | [diff] [blame] | 6385 | auto command = [this, app = std::move(application)]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6386 | scoped_unlock unlock(mLock); |
Yabin Cui | ced952f | 2023-06-09 21:12:51 +0000 | [diff] [blame] | 6387 | mPolicy.notifyNoFocusedWindowAnr(app); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6388 | }; |
| 6389 | postCommandLocked(std::move(command)); |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 6390 | } |
| 6391 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 6392 | void InputDispatcher::updateLastAnrStateLocked(const sp<WindowInfoHandle>& window, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6393 | const std::string& reason) { |
| 6394 | const std::string windowLabel = getApplicationWindowLabel(nullptr, window); |
| 6395 | updateLastAnrStateLocked(windowLabel, reason); |
| 6396 | } |
| 6397 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6398 | void InputDispatcher::updateLastAnrStateLocked(const InputApplicationHandle& application, |
| 6399 | const std::string& reason) { |
| 6400 | const std::string windowLabel = getApplicationWindowLabel(&application, nullptr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6401 | updateLastAnrStateLocked(windowLabel, reason); |
| 6402 | } |
| 6403 | |
| 6404 | void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel, |
| 6405 | const std::string& reason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6406 | // 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] | 6407 | time_t t = time(nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6408 | struct tm tm; |
| 6409 | localtime_r(&t, &tm); |
| 6410 | char timestr[64]; |
| 6411 | strftime(timestr, sizeof(timestr), "%F %T", &tm); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6412 | mLastAnrState.clear(); |
| 6413 | mLastAnrState += INDENT "ANR:\n"; |
| 6414 | mLastAnrState += StringPrintf(INDENT2 "Time: %s\n", timestr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6415 | mLastAnrState += StringPrintf(INDENT2 "Reason: %s\n", reason.c_str()); |
| 6416 | mLastAnrState += StringPrintf(INDENT2 "Window: %s\n", windowLabel.c_str()); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6417 | dumpDispatchStateLocked(mLastAnrState); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6418 | } |
| 6419 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6420 | void InputDispatcher::doInterceptKeyBeforeDispatchingCommand(const sp<IBinder>& focusedWindowToken, |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 6421 | const KeyEntry& entry) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6422 | const KeyEvent event = createKeyEvent(entry); |
| 6423 | nsecs_t delay = 0; |
| 6424 | { // release lock |
| 6425 | scoped_unlock unlock(mLock); |
| 6426 | android::base::Timer t; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6427 | delay = mPolicy.interceptKeyBeforeDispatching(focusedWindowToken, event, entry.policyFlags); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6428 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 6429 | ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms", |
| 6430 | std::to_string(t.duration().count()).c_str()); |
| 6431 | } |
| 6432 | } // acquire lock |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6433 | |
| 6434 | if (delay < 0) { |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 6435 | entry.interceptKeyResult = KeyEntry::InterceptKeyResult::SKIP; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6436 | } else if (delay == 0) { |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 6437 | entry.interceptKeyResult = KeyEntry::InterceptKeyResult::CONTINUE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6438 | } else { |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 6439 | entry.interceptKeyResult = KeyEntry::InterceptKeyResult::TRY_AGAIN_LATER; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6440 | entry.interceptKeyWakeupTime = now() + delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6441 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6442 | } |
| 6443 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6444 | void InputDispatcher::sendWindowUnresponsiveCommandLocked(const sp<IBinder>& token, |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6445 | std::optional<gui::Pid> pid, |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6446 | std::string reason) { |
Yabin Cui | 8eb9c55 | 2023-06-08 18:05:07 +0000 | [diff] [blame] | 6447 | auto command = [this, token, pid, r = std::move(reason)]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6448 | scoped_unlock unlock(mLock); |
Yabin Cui | ced952f | 2023-06-09 21:12:51 +0000 | [diff] [blame] | 6449 | mPolicy.notifyWindowUnresponsive(token, pid, r); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6450 | }; |
| 6451 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6452 | } |
| 6453 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6454 | void InputDispatcher::sendWindowResponsiveCommandLocked(const sp<IBinder>& token, |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6455 | std::optional<gui::Pid> pid) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6456 | auto command = [this, token, pid]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6457 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6458 | mPolicy.notifyWindowResponsive(token, pid); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6459 | }; |
| 6460 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6461 | } |
| 6462 | |
| 6463 | /** |
| 6464 | * Tell the policy that a connection has become unresponsive so that it can start ANR. |
| 6465 | * Check whether the connection of interest is a monitor or a window, and add the corresponding |
| 6466 | * command entry to the command queue. |
| 6467 | */ |
| 6468 | void InputDispatcher::processConnectionUnresponsiveLocked(const Connection& connection, |
| 6469 | std::string reason) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6470 | const sp<IBinder>& connectionToken = connection.getToken(); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6471 | std::optional<gui::Pid> pid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6472 | if (connection.monitor) { |
Siarhei Vishniakou | 10bfdc9 | 2024-02-01 11:12:57 -0800 | [diff] [blame] | 6473 | ALOGW("Monitor %s is unresponsive: %s", connection.getInputChannelName().c_str(), |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6474 | reason.c_str()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6475 | pid = findMonitorPidByTokenLocked(connectionToken); |
| 6476 | } else { |
| 6477 | // The connection is a window |
Siarhei Vishniakou | 10bfdc9 | 2024-02-01 11:12:57 -0800 | [diff] [blame] | 6478 | ALOGW("Window %s is unresponsive: %s", connection.getInputChannelName().c_str(), |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6479 | reason.c_str()); |
| 6480 | const sp<WindowInfoHandle> handle = getWindowHandleLocked(connectionToken); |
| 6481 | if (handle != nullptr) { |
| 6482 | pid = handle->getInfo()->ownerPid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6483 | } |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6484 | } |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6485 | sendWindowUnresponsiveCommandLocked(connectionToken, pid, std::move(reason)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6486 | } |
| 6487 | |
| 6488 | /** |
| 6489 | * Tell the policy that a connection has become responsive so that it can stop ANR. |
| 6490 | */ |
| 6491 | void InputDispatcher::processConnectionResponsiveLocked(const Connection& connection) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6492 | const sp<IBinder>& connectionToken = connection.getToken(); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6493 | std::optional<gui::Pid> pid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6494 | if (connection.monitor) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6495 | pid = findMonitorPidByTokenLocked(connectionToken); |
| 6496 | } else { |
| 6497 | // The connection is a window |
| 6498 | const sp<WindowInfoHandle> handle = getWindowHandleLocked(connectionToken); |
| 6499 | if (handle != nullptr) { |
| 6500 | pid = handle->getInfo()->ownerPid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6501 | } |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6502 | } |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6503 | sendWindowResponsiveCommandLocked(connectionToken, pid); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6504 | } |
| 6505 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 6506 | std::unique_ptr<const KeyEntry> InputDispatcher::afterKeyEventLockedInterruptable( |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6507 | const std::shared_ptr<Connection>& connection, DispatchEntry& dispatchEntry, |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6508 | const KeyEntry& keyEntry, bool handled) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6509 | if (keyEntry.flags & AKEY_EVENT_FLAG_FALLBACK) { |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6510 | if (!handled) { |
| 6511 | // Report the key as unhandled, since the fallback was not handled. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6512 | mReporter->reportUnhandledKey(keyEntry.id); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6513 | } |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6514 | return {}; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6515 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6516 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6517 | // Get the fallback key state. |
| 6518 | // Clear it out after dispatching the UP. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6519 | int32_t originalKeyCode = keyEntry.keyCode; |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6520 | std::optional<int32_t> fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6521 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6522 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6523 | } |
| 6524 | |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6525 | if (handled || !dispatchEntry.hasForegroundTarget()) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6526 | // If the application handles the original key for which we previously |
| 6527 | // generated a fallback or if the window is not a foreground window, |
| 6528 | // then cancel the associated fallback key, if any. |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6529 | if (fallbackKeyCode) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6530 | // Dispatch the unhandled key to the policy with the cancel flag. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6531 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6532 | ALOGD("Unhandled key event: Asking policy to cancel fallback action. " |
| 6533 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6534 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, |
| 6535 | keyEntry.policyFlags); |
| 6536 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6537 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6538 | event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6539 | |
| 6540 | mLock.unlock(); |
| 6541 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6542 | if (const auto unhandledKeyFallback = |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6543 | mPolicy.dispatchUnhandledKey(connection->getToken(), event, |
| 6544 | keyEntry.policyFlags); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6545 | unhandledKeyFallback) { |
| 6546 | event = *unhandledKeyFallback; |
| 6547 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6548 | |
| 6549 | mLock.lock(); |
| 6550 | |
Prabir Pradhan | fa2c69f | 2024-02-01 20:31:34 +0000 | [diff] [blame] | 6551 | // Cancel the fallback key, but only if we still have a window for the channel. |
| 6552 | // It could have been removed during the policy call. |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6553 | if (*fallbackKeyCode != AKEYCODE_UNKNOWN) { |
Prabir Pradhan | fa2c69f | 2024-02-01 20:31:34 +0000 | [diff] [blame] | 6554 | const auto windowHandle = getWindowHandleLocked(connection->getToken()); |
| 6555 | if (windowHandle != nullptr) { |
| 6556 | CancelationOptions options(CancelationOptions::Mode::CANCEL_FALLBACK_EVENTS, |
| 6557 | "application handled the original non-fallback key " |
| 6558 | "or is no longer a foreground target, " |
| 6559 | "canceling previously dispatched fallback key"); |
| 6560 | options.keyCode = *fallbackKeyCode; |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 6561 | synthesizeCancelationEventsForWindowLocked(windowHandle, options, connection); |
Prabir Pradhan | fa2c69f | 2024-02-01 20:31:34 +0000 | [diff] [blame] | 6562 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6563 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6564 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6565 | } |
| 6566 | } else { |
| 6567 | // If the application did not handle a non-fallback key, first check |
| 6568 | // that we are in a good state to perform unhandled key event processing |
| 6569 | // Then ask the policy what to do with it. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6570 | bool initialDown = keyEntry.action == AKEY_EVENT_ACTION_DOWN && keyEntry.repeatCount == 0; |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6571 | if (!fallbackKeyCode && !initialDown) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6572 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6573 | ALOGD("Unhandled key event: Skipping unhandled key event processing " |
| 6574 | "since this is not an initial down. " |
| 6575 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6576 | originalKeyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 6577 | } |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6578 | return {}; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6579 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6580 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6581 | // Dispatch the unhandled key to the policy. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6582 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6583 | ALOGD("Unhandled key event: Asking policy to perform fallback action. " |
| 6584 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6585 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 6586 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6587 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6588 | |
| 6589 | mLock.unlock(); |
| 6590 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6591 | bool fallback = false; |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6592 | if (auto fb = mPolicy.dispatchUnhandledKey(connection->getToken(), event, |
| 6593 | keyEntry.policyFlags); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6594 | fb) { |
| 6595 | fallback = true; |
| 6596 | event = *fb; |
| 6597 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6598 | |
| 6599 | mLock.lock(); |
| 6600 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 6601 | if (connection->status != Connection::Status::NORMAL) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6602 | connection->inputState.removeFallbackKey(originalKeyCode); |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6603 | return {}; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6604 | } |
| 6605 | |
| 6606 | // Latch the fallback keycode for this key on an initial down. |
| 6607 | // The fallback keycode cannot change at any other point in the lifecycle. |
| 6608 | if (initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6609 | if (fallback) { |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6610 | *fallbackKeyCode = event.getKeyCode(); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6611 | } else { |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6612 | *fallbackKeyCode = AKEYCODE_UNKNOWN; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6613 | } |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6614 | connection->inputState.setFallbackKey(originalKeyCode, *fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6615 | } |
| 6616 | |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6617 | ALOG_ASSERT(fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6618 | |
| 6619 | // Cancel the fallback key if the policy decides not to send it anymore. |
| 6620 | // We will continue to dispatch the key to the policy but we will no |
| 6621 | // longer dispatch a fallback key to the application. |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6622 | if (*fallbackKeyCode != AKEYCODE_UNKNOWN && |
| 6623 | (!fallback || *fallbackKeyCode != event.getKeyCode())) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6624 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6625 | if (fallback) { |
| 6626 | ALOGD("Unhandled key event: Policy requested to send key %d" |
| 6627 | "as a fallback for %d, but on the DOWN it had requested " |
| 6628 | "to send %d instead. Fallback canceled.", |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6629 | event.getKeyCode(), originalKeyCode, *fallbackKeyCode); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6630 | } else { |
| 6631 | ALOGD("Unhandled key event: Policy did not request fallback for %d, " |
| 6632 | "but on the DOWN it had requested to send %d. " |
| 6633 | "Fallback canceled.", |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6634 | originalKeyCode, *fallbackKeyCode); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6635 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6636 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6637 | |
Prabir Pradhan | fa2c69f | 2024-02-01 20:31:34 +0000 | [diff] [blame] | 6638 | const auto windowHandle = getWindowHandleLocked(connection->getToken()); |
| 6639 | if (windowHandle != nullptr) { |
| 6640 | CancelationOptions options(CancelationOptions::Mode::CANCEL_FALLBACK_EVENTS, |
| 6641 | "canceling fallback, policy no longer desires it"); |
| 6642 | options.keyCode = *fallbackKeyCode; |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 6643 | synthesizeCancelationEventsForWindowLocked(windowHandle, options, connection); |
Prabir Pradhan | fa2c69f | 2024-02-01 20:31:34 +0000 | [diff] [blame] | 6644 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6645 | |
| 6646 | fallback = false; |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6647 | *fallbackKeyCode = AKEYCODE_UNKNOWN; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6648 | if (keyEntry.action != AKEY_EVENT_ACTION_UP) { |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6649 | connection->inputState.setFallbackKey(originalKeyCode, *fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6650 | } |
| 6651 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6652 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6653 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6654 | { |
| 6655 | std::string msg; |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6656 | const std::map<int32_t, int32_t>& fallbackKeys = |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6657 | connection->inputState.getFallbackKeys(); |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6658 | for (const auto& [key, value] : fallbackKeys) { |
| 6659 | msg += StringPrintf(", %d->%d", key, value); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6660 | } |
| 6661 | ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.", |
| 6662 | fallbackKeys.size(), msg.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6663 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6664 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6665 | |
| 6666 | if (fallback) { |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6667 | // Return the fallback key that we want dispatched to the channel. |
| 6668 | std::unique_ptr<KeyEntry> newEntry = |
| 6669 | std::make_unique<KeyEntry>(mIdGenerator.nextId(), keyEntry.injectionState, |
| 6670 | event.getEventTime(), event.getDeviceId(), |
| 6671 | event.getSource(), event.getDisplayId(), |
| 6672 | keyEntry.policyFlags, keyEntry.action, |
| 6673 | event.getFlags() | AKEY_EVENT_FLAG_FALLBACK, |
| 6674 | *fallbackKeyCode, event.getScanCode(), |
| 6675 | event.getMetaState(), event.getRepeatCount(), |
| 6676 | event.getDownTime()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6677 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6678 | ALOGD("Unhandled key event: Dispatching fallback key. " |
| 6679 | "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x", |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6680 | originalKeyCode, *fallbackKeyCode, keyEntry.metaState); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6681 | } |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6682 | return newEntry; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6683 | } else { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6684 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6685 | ALOGD("Unhandled key event: No fallback key."); |
| 6686 | } |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6687 | |
| 6688 | // Report the key as unhandled, since there is no fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6689 | mReporter->reportUnhandledKey(keyEntry.id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6690 | } |
| 6691 | } |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6692 | return {}; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6693 | } |
| 6694 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6695 | void InputDispatcher::traceInboundQueueLengthLocked() { |
| 6696 | if (ATRACE_ENABLED()) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 6697 | ATRACE_INT("iq", mInboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6698 | } |
| 6699 | } |
| 6700 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6701 | void InputDispatcher::traceOutboundQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6702 | if (ATRACE_ENABLED()) { |
| 6703 | char counterName[40]; |
Siarhei Vishniakou | 10bfdc9 | 2024-02-01 11:12:57 -0800 | [diff] [blame] | 6704 | snprintf(counterName, sizeof(counterName), "oq:%s", |
| 6705 | connection.getInputChannelName().c_str()); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6706 | ATRACE_INT(counterName, connection.outboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6707 | } |
| 6708 | } |
| 6709 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6710 | void InputDispatcher::traceWaitQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6711 | if (ATRACE_ENABLED()) { |
| 6712 | char counterName[40]; |
Siarhei Vishniakou | 10bfdc9 | 2024-02-01 11:12:57 -0800 | [diff] [blame] | 6713 | snprintf(counterName, sizeof(counterName), "wq:%s", |
| 6714 | connection.getInputChannelName().c_str()); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6715 | ATRACE_INT(counterName, connection.waitQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6716 | } |
| 6717 | } |
| 6718 | |
Siarhei Vishniakou | 5e20f27 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 6719 | void InputDispatcher::dump(std::string& dump) const { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6720 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6721 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6722 | dump += "Input Dispatcher State:\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6723 | dumpDispatchStateLocked(dump); |
| 6724 | |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6725 | if (!mLastAnrState.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6726 | dump += "\nInput Dispatcher State at time of last ANR:\n"; |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6727 | dump += mLastAnrState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6728 | } |
| 6729 | } |
| 6730 | |
| 6731 | void InputDispatcher::monitor() { |
| 6732 | // 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] | 6733 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6734 | mLooper->wake(); |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6735 | mDispatcherIsAlive.wait(_l); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6736 | } |
| 6737 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6738 | /** |
| 6739 | * Wake up the dispatcher and wait until it processes all events and commands. |
| 6740 | * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so |
| 6741 | * this method can be safely called from any thread, as long as you've ensured that |
| 6742 | * the work you are interested in completing has already been queued. |
| 6743 | */ |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 6744 | bool InputDispatcher::waitForIdle() const { |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6745 | /** |
| 6746 | * Timeout should represent the longest possible time that a device might spend processing |
| 6747 | * events and commands. |
| 6748 | */ |
| 6749 | constexpr std::chrono::duration TIMEOUT = 100ms; |
| 6750 | std::unique_lock lock(mLock); |
| 6751 | mLooper->wake(); |
| 6752 | std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT); |
| 6753 | return result == std::cv_status::no_timeout; |
| 6754 | } |
| 6755 | |
Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 6756 | /** |
| 6757 | * Sets focus to the window identified by the token. This must be called |
| 6758 | * after updating any input window handles. |
| 6759 | * |
| 6760 | * Params: |
| 6761 | * request.token - input channel token used to identify the window that should gain focus. |
| 6762 | * request.focusedToken - the token that the caller expects currently to be focused. If the |
| 6763 | * specified token does not match the currently focused window, this request will be dropped. |
| 6764 | * If the specified focused token matches the currently focused window, the call will succeed. |
| 6765 | * Set this to "null" if this call should succeed no matter what the currently focused token is. |
| 6766 | * request.timestamp - SYSTEM_TIME_MONOTONIC timestamp in nanos set by the client (wm) |
| 6767 | * when requesting the focus change. This determines which request gets |
| 6768 | * precedence if there is a focus change request from another source such as pointer down. |
| 6769 | */ |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6770 | void InputDispatcher::setFocusedWindow(const FocusRequest& request) { |
| 6771 | { // acquire lock |
| 6772 | std::scoped_lock _l(mLock); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6773 | std::optional<FocusResolver::FocusChanges> changes = |
| 6774 | mFocusResolver.setFocusedWindow(request, getWindowHandlesLocked(request.displayId)); |
| 6775 | if (changes) { |
| 6776 | onFocusChangedLocked(*changes); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6777 | } |
| 6778 | } // release lock |
| 6779 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6780 | mLooper->wake(); |
| 6781 | } |
| 6782 | |
Prabir Pradhan | 4b9b1a1 | 2024-02-02 00:16:17 +0000 | [diff] [blame] | 6783 | void InputDispatcher::onFocusChangedLocked(const FocusResolver::FocusChanges& changes, |
| 6784 | const sp<WindowInfoHandle> removedFocusedWindowHandle) { |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6785 | if (changes.oldFocus) { |
Prabir Pradhan | 4b9b1a1 | 2024-02-02 00:16:17 +0000 | [diff] [blame] | 6786 | const auto resolvedWindow = removedFocusedWindowHandle != nullptr |
| 6787 | ? removedFocusedWindowHandle |
| 6788 | : getWindowHandleLocked(changes.oldFocus, changes.displayId); |
| 6789 | if (resolvedWindow == nullptr) { |
| 6790 | LOG(FATAL) << __func__ << ": Previously focused token did not have a window"; |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6791 | } |
Prabir Pradhan | 4b9b1a1 | 2024-02-02 00:16:17 +0000 | [diff] [blame] | 6792 | CancelationOptions options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, |
| 6793 | "focus left window"); |
| 6794 | synthesizeCancelationEventsForWindowLocked(resolvedWindow, options); |
| 6795 | enqueueFocusEventLocked(changes.oldFocus, /*hasFocus=*/false, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6796 | } |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6797 | if (changes.newFocus) { |
Siarhei Vishniakou | c033dfb | 2023-10-03 10:45:16 -0700 | [diff] [blame] | 6798 | resetNoFocusedWindowTimeoutLocked(); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6799 | enqueueFocusEventLocked(changes.newFocus, /*hasFocus=*/true, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6800 | } |
| 6801 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6802 | // If a window has pointer capture, then it must have focus. We need to ensure that this |
| 6803 | // contract is upheld when pointer capture is being disabled due to a loss of window focus. |
| 6804 | // If the window loses focus before it loses pointer capture, then the window can be in a state |
| 6805 | // where it has pointer capture but not focus, violating the contract. Therefore we must |
| 6806 | // dispatch the pointer capture event before the focus event. Since focus events are added to |
| 6807 | // the front of the queue (above), we add the pointer capture event to the front of the queue |
| 6808 | // after the focus events are added. This ensures the pointer capture event ends up at the |
| 6809 | // front. |
| 6810 | disablePointerCaptureForcedLocked(); |
| 6811 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6812 | if (mFocusedDisplayId == changes.displayId) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6813 | sendFocusChangedCommandLocked(changes.oldFocus, changes.newFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6814 | } |
| 6815 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6816 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6817 | void InputDispatcher::disablePointerCaptureForcedLocked() { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6818 | if (!mCurrentPointerCaptureRequest.enable && !mWindowTokenWithPointerCapture) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6819 | return; |
| 6820 | } |
| 6821 | |
| 6822 | ALOGD_IF(DEBUG_FOCUS, "Disabling Pointer Capture because the window lost focus."); |
| 6823 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6824 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6825 | setPointerCaptureLocked(false); |
| 6826 | } |
| 6827 | |
| 6828 | if (!mWindowTokenWithPointerCapture) { |
| 6829 | // No need to send capture changes because no window has capture. |
| 6830 | return; |
| 6831 | } |
| 6832 | |
| 6833 | if (mPendingEvent != nullptr) { |
| 6834 | // Move the pending event to the front of the queue. This will give the chance |
| 6835 | // for the pending event to be dropped if it is a captured event. |
| 6836 | mInboundQueue.push_front(mPendingEvent); |
| 6837 | mPendingEvent = nullptr; |
| 6838 | } |
| 6839 | |
| 6840 | auto entry = std::make_unique<PointerCaptureChangedEntry>(mIdGenerator.nextId(), now(), |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6841 | mCurrentPointerCaptureRequest); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6842 | mInboundQueue.push_front(std::move(entry)); |
| 6843 | } |
| 6844 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6845 | void InputDispatcher::setPointerCaptureLocked(bool enable) { |
| 6846 | mCurrentPointerCaptureRequest.enable = enable; |
| 6847 | mCurrentPointerCaptureRequest.seq++; |
| 6848 | auto command = [this, request = mCurrentPointerCaptureRequest]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6849 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6850 | mPolicy.setPointerCapture(request); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6851 | }; |
| 6852 | postCommandLocked(std::move(command)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6853 | } |
| 6854 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6855 | void InputDispatcher::displayRemoved(int32_t displayId) { |
| 6856 | { // acquire lock |
| 6857 | std::scoped_lock _l(mLock); |
| 6858 | // Set an empty list to remove all handles from the specific display. |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 6859 | setInputWindowsLocked(/*windowInfoHandles=*/{}, displayId); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6860 | setFocusedApplicationLocked(displayId, nullptr); |
| 6861 | // Call focus resolver to clean up stale requests. This must be called after input windows |
| 6862 | // have been removed for the removed display. |
| 6863 | mFocusResolver.displayRemoved(displayId); |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 6864 | // Reset pointer capture eligibility, regardless of previous state. |
| 6865 | std::erase(mIneligibleDisplaysForPointerCapture, displayId); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 6866 | // Remove the associated touch mode state. |
| 6867 | mTouchModePerDisplay.erase(displayId); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 6868 | mVerifiersByDisplay.erase(displayId); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6869 | } // release lock |
| 6870 | |
| 6871 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6872 | mLooper->wake(); |
| 6873 | } |
| 6874 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 6875 | void InputDispatcher::onWindowInfosChanged(const gui::WindowInfosUpdate& update) { |
Siarhei Vishniakou | aeed0da | 2024-01-09 08:57:13 -0800 | [diff] [blame] | 6876 | if (auto result = validateWindowInfosUpdate(update); !result.ok()) { |
| 6877 | { |
| 6878 | // acquire lock |
| 6879 | std::scoped_lock _l(mLock); |
| 6880 | logDispatchStateLocked(); |
| 6881 | } |
| 6882 | LOG_ALWAYS_FATAL("Incorrect WindowInfosUpdate provided: %s", |
| 6883 | result.error().message().c_str()); |
| 6884 | }; |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6885 | // The listener sends the windows as a flattened array. Separate the windows by display for |
| 6886 | // more convenient parsing. |
| 6887 | std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>> handlesPerDisplay; |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 6888 | for (const auto& info : update.windowInfos) { |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6889 | handlesPerDisplay.emplace(info.displayId, std::vector<sp<WindowInfoHandle>>()); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6890 | handlesPerDisplay[info.displayId].push_back(sp<WindowInfoHandle>::make(info)); |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6891 | } |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6892 | |
| 6893 | { // acquire lock |
| 6894 | std::scoped_lock _l(mLock); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 6895 | |
| 6896 | // Ensure that we have an entry created for all existing displays so that if a displayId has |
| 6897 | // no windows, we can tell that the windows were removed from the display. |
| 6898 | for (const auto& [displayId, _] : mWindowHandlesByDisplay) { |
| 6899 | handlesPerDisplay[displayId]; |
| 6900 | } |
| 6901 | |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6902 | mDisplayInfos.clear(); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 6903 | for (const auto& displayInfo : update.displayInfos) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6904 | mDisplayInfos.emplace(displayInfo.displayId, displayInfo); |
| 6905 | } |
| 6906 | |
| 6907 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 6908 | setInputWindowsLocked(handles, displayId); |
| 6909 | } |
Patrick Williams | 9464b2c | 2023-05-23 11:22:04 -0500 | [diff] [blame] | 6910 | |
| 6911 | if (update.vsyncId < mWindowInfosVsyncId) { |
| 6912 | ALOGE("Received out of order window infos update. Last update vsync id: %" PRId64 |
| 6913 | ", current update vsync id: %" PRId64, |
| 6914 | mWindowInfosVsyncId, update.vsyncId); |
| 6915 | } |
| 6916 | mWindowInfosVsyncId = update.vsyncId; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6917 | } |
| 6918 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6919 | mLooper->wake(); |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6920 | } |
| 6921 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6922 | bool InputDispatcher::shouldDropInput( |
| 6923 | const EventEntry& entry, const sp<android::gui::WindowInfoHandle>& windowHandle) const { |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6924 | if (windowHandle->getInfo()->inputConfig.test(WindowInfo::InputConfig::DROP_INPUT) || |
| 6925 | (windowHandle->getInfo()->inputConfig.test( |
| 6926 | WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED) && |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6927 | isWindowObscuredLocked(windowHandle))) { |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6928 | ALOGW("Dropping %s event targeting %s as requested by the input configuration {%s} on " |
| 6929 | "display %" PRId32 ".", |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6930 | ftl::enum_string(entry.type).c_str(), windowHandle->getName().c_str(), |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6931 | windowHandle->getInfo()->inputConfig.string().c_str(), |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6932 | windowHandle->getInfo()->displayId); |
| 6933 | return true; |
| 6934 | } |
| 6935 | return false; |
| 6936 | } |
| 6937 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 6938 | void InputDispatcher::DispatcherWindowListener::onWindowInfosChanged( |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 6939 | const gui::WindowInfosUpdate& update) { |
| 6940 | mDispatcher.onWindowInfosChanged(update); |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 6941 | } |
| 6942 | |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6943 | void InputDispatcher::cancelCurrentTouch() { |
| 6944 | { |
| 6945 | std::scoped_lock _l(mLock); |
| 6946 | ALOGD("Canceling all ongoing pointer gestures on all displays."); |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 6947 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6948 | "cancel current touch"); |
| 6949 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 6950 | |
| 6951 | mTouchStatesByDisplay.clear(); |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6952 | } |
| 6953 | // Wake up poll loop since there might be work to do. |
| 6954 | mLooper->wake(); |
| 6955 | } |
| 6956 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6957 | void InputDispatcher::setMonitorDispatchingTimeoutForTest(std::chrono::nanoseconds timeout) { |
| 6958 | std::scoped_lock _l(mLock); |
| 6959 | mMonitorDispatchingTimeout = timeout; |
| 6960 | } |
| 6961 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6962 | void InputDispatcher::slipWallpaperTouch(ftl::Flags<InputTarget::Flags> targetFlags, |
| 6963 | const sp<WindowInfoHandle>& oldWindowHandle, |
| 6964 | const sp<WindowInfoHandle>& newWindowHandle, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 6965 | TouchState& state, int32_t deviceId, |
| 6966 | const PointerProperties& pointerProperties, |
Siarhei Vishniakou | bf88052 | 2023-05-01 11:03:22 -0700 | [diff] [blame] | 6967 | std::vector<InputTarget>& targets) const { |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 6968 | std::vector<PointerProperties> pointers{pointerProperties}; |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6969 | const bool oldHasWallpaper = oldWindowHandle->getInfo()->inputConfig.test( |
| 6970 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER); |
| 6971 | const bool newHasWallpaper = targetFlags.test(InputTarget::Flags::FOREGROUND) && |
| 6972 | newWindowHandle->getInfo()->inputConfig.test( |
| 6973 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER); |
| 6974 | const sp<WindowInfoHandle> oldWallpaper = |
| 6975 | oldHasWallpaper ? state.getWallpaperWindow() : nullptr; |
| 6976 | const sp<WindowInfoHandle> newWallpaper = |
| 6977 | newHasWallpaper ? findWallpaperWindowBelow(newWindowHandle) : nullptr; |
| 6978 | if (oldWallpaper == newWallpaper) { |
| 6979 | return; |
| 6980 | } |
| 6981 | |
| 6982 | if (oldWallpaper != nullptr) { |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 6983 | const TouchedWindow& oldTouchedWindow = state.getTouchedWindow(oldWallpaper); |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 6984 | addPointerWindowTargetLocked(oldWallpaper, InputTarget::DispatchMode::SLIPPERY_EXIT, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 6985 | oldTouchedWindow.targetFlags, getPointerIds(pointers), |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 6986 | oldTouchedWindow.getDownTimeInTarget(deviceId), targets); |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 6987 | state.removeTouchingPointerFromWindow(deviceId, pointerProperties.id, oldWallpaper); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6988 | } |
| 6989 | |
| 6990 | if (newWallpaper != nullptr) { |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 6991 | state.addOrUpdateWindow(newWallpaper, InputTarget::DispatchMode::SLIPPERY_ENTER, |
| 6992 | InputTarget::Flags::WINDOW_IS_OBSCURED | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6993 | InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 6994 | deviceId, pointers); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6995 | } |
| 6996 | } |
| 6997 | |
| 6998 | void InputDispatcher::transferWallpaperTouch(ftl::Flags<InputTarget::Flags> oldTargetFlags, |
| 6999 | ftl::Flags<InputTarget::Flags> newTargetFlags, |
| 7000 | const sp<WindowInfoHandle> fromWindowHandle, |
| 7001 | const sp<WindowInfoHandle> toWindowHandle, |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 7002 | TouchState& state, int32_t deviceId, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 7003 | const std::vector<PointerProperties>& pointers) { |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 7004 | const bool oldHasWallpaper = oldTargetFlags.test(InputTarget::Flags::FOREGROUND) && |
| 7005 | fromWindowHandle->getInfo()->inputConfig.test( |
| 7006 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER); |
| 7007 | const bool newHasWallpaper = newTargetFlags.test(InputTarget::Flags::FOREGROUND) && |
| 7008 | toWindowHandle->getInfo()->inputConfig.test( |
| 7009 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER); |
| 7010 | |
| 7011 | const sp<WindowInfoHandle> oldWallpaper = |
| 7012 | oldHasWallpaper ? state.getWallpaperWindow() : nullptr; |
| 7013 | const sp<WindowInfoHandle> newWallpaper = |
| 7014 | newHasWallpaper ? findWallpaperWindowBelow(toWindowHandle) : nullptr; |
| 7015 | if (oldWallpaper == newWallpaper) { |
| 7016 | return; |
| 7017 | } |
| 7018 | |
| 7019 | if (oldWallpaper != nullptr) { |
| 7020 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
| 7021 | "transferring touch focus to another window"); |
| 7022 | state.removeWindowByToken(oldWallpaper->getToken()); |
| 7023 | synthesizeCancelationEventsForWindowLocked(oldWallpaper, options); |
| 7024 | } |
| 7025 | |
| 7026 | if (newWallpaper != nullptr) { |
| 7027 | nsecs_t downTimeInTarget = now(); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 7028 | ftl::Flags<InputTarget::Flags> wallpaperFlags = newTargetFlags; |
| 7029 | wallpaperFlags |= oldTargetFlags & InputTarget::Flags::SPLIT; |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 7030 | wallpaperFlags |= InputTarget::Flags::WINDOW_IS_OBSCURED | |
| 7031 | InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 7032 | state.addOrUpdateWindow(newWallpaper, InputTarget::DispatchMode::AS_IS, wallpaperFlags, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 7033 | deviceId, pointers, downTimeInTarget); |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 7034 | std::shared_ptr<Connection> wallpaperConnection = |
| 7035 | getConnectionLocked(newWallpaper->getToken()); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 7036 | if (wallpaperConnection != nullptr) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 7037 | std::shared_ptr<Connection> toConnection = |
| 7038 | getConnectionLocked(toWindowHandle->getToken()); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 7039 | toConnection->inputState.mergePointerStateTo(wallpaperConnection->inputState); |
| 7040 | synthesizePointerDownEventsForConnectionLocked(downTimeInTarget, wallpaperConnection, |
| 7041 | wallpaperFlags); |
| 7042 | } |
| 7043 | } |
| 7044 | } |
| 7045 | |
| 7046 | sp<WindowInfoHandle> InputDispatcher::findWallpaperWindowBelow( |
| 7047 | const sp<WindowInfoHandle>& windowHandle) const { |
| 7048 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
| 7049 | getWindowHandlesLocked(windowHandle->getInfo()->displayId); |
| 7050 | bool foundWindow = false; |
| 7051 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
| 7052 | if (!foundWindow && otherHandle != windowHandle) { |
| 7053 | continue; |
| 7054 | } |
| 7055 | if (windowHandle == otherHandle) { |
| 7056 | foundWindow = true; |
| 7057 | continue; |
| 7058 | } |
| 7059 | |
| 7060 | if (otherHandle->getInfo()->inputConfig.test(WindowInfo::InputConfig::IS_WALLPAPER)) { |
| 7061 | return otherHandle; |
| 7062 | } |
| 7063 | } |
| 7064 | return nullptr; |
| 7065 | } |
| 7066 | |
Siarhei Vishniakou | fa2a049 | 2023-11-14 13:13:18 -0800 | [diff] [blame] | 7067 | void InputDispatcher::setKeyRepeatConfiguration(std::chrono::nanoseconds timeout, |
| 7068 | std::chrono::nanoseconds delay) { |
Nergi Rahardi | 730cf3c | 2023-04-13 12:41:17 +0900 | [diff] [blame] | 7069 | std::scoped_lock _l(mLock); |
| 7070 | |
Siarhei Vishniakou | fa2a049 | 2023-11-14 13:13:18 -0800 | [diff] [blame] | 7071 | mConfig.keyRepeatTimeout = timeout.count(); |
| 7072 | mConfig.keyRepeatDelay = delay.count(); |
Nergi Rahardi | 730cf3c | 2023-04-13 12:41:17 +0900 | [diff] [blame] | 7073 | } |
| 7074 | |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 7075 | bool InputDispatcher::isPointerInWindow(const sp<android::IBinder>& token, int32_t displayId, |
| 7076 | DeviceId deviceId, int32_t pointerId) { |
| 7077 | std::scoped_lock _l(mLock); |
| 7078 | auto touchStateIt = mTouchStatesByDisplay.find(displayId); |
| 7079 | if (touchStateIt == mTouchStatesByDisplay.end()) { |
| 7080 | return false; |
| 7081 | } |
| 7082 | for (const TouchedWindow& window : touchStateIt->second.windows) { |
| 7083 | if (window.windowHandle->getToken() == token && |
| 7084 | (window.hasTouchingPointer(deviceId, pointerId) || |
| 7085 | window.hasHoveringPointer(deviceId, pointerId))) { |
| 7086 | return true; |
| 7087 | } |
| 7088 | } |
| 7089 | return false; |
| 7090 | } |
| 7091 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 7092 | } // namespace android::inputdispatcher |