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 | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 106 | // Temporarily releases a held mutex for the lifetime of the instance. |
| 107 | // Named to match std::scoped_lock |
| 108 | class scoped_unlock { |
| 109 | public: |
| 110 | explicit scoped_unlock(std::mutex& mutex) : mMutex(mutex) { mMutex.unlock(); } |
| 111 | ~scoped_unlock() { mMutex.lock(); } |
| 112 | |
| 113 | private: |
| 114 | std::mutex& mMutex; |
| 115 | }; |
| 116 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 117 | // Default input dispatching timeout if there is no focused application or paused window |
| 118 | // from which to determine an appropriate dispatching timeout. |
Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 119 | const std::chrono::duration DEFAULT_INPUT_DISPATCHING_TIMEOUT = std::chrono::milliseconds( |
| 120 | android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS * |
| 121 | HwTimeoutMultiplier()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 122 | |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 123 | // The default minimum time gap between two user activity poke events. |
| 124 | const std::chrono::milliseconds DEFAULT_USER_ACTIVITY_POKE_INTERVAL = 100ms; |
| 125 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 126 | const std::chrono::duration STALE_EVENT_TIMEOUT = std::chrono::seconds(10) * HwTimeoutMultiplier(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 127 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 128 | // 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] | 129 | constexpr nsecs_t SLOW_EVENT_PROCESSING_WARNING_TIMEOUT = 2000 * 1000000LL; // 2sec |
| 130 | |
| 131 | // Log a warning when an interception call takes longer than this to process. |
| 132 | constexpr std::chrono::milliseconds SLOW_INTERCEPTION_THRESHOLD = 50ms; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 133 | |
| 134 | // Number of recent events to keep for debugging purposes. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 135 | constexpr size_t RECENT_QUEUE_MAX_SIZE = 10; |
| 136 | |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 137 | // Event log tags. See EventLogTags.logtags for reference. |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 138 | constexpr int LOGTAG_INPUT_INTERACTION = 62000; |
| 139 | constexpr int LOGTAG_INPUT_FOCUS = 62001; |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 140 | constexpr int LOGTAG_INPUT_CANCEL = 62003; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 141 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 142 | const ui::Transform kIdentityTransform; |
| 143 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 144 | inline nsecs_t now() { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 145 | return systemTime(SYSTEM_TIME_MONOTONIC); |
| 146 | } |
| 147 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 148 | inline const std::string binderToString(const sp<IBinder>& binder) { |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 149 | if (binder == nullptr) { |
| 150 | return "<null>"; |
| 151 | } |
| 152 | return StringPrintf("%p", binder.get()); |
| 153 | } |
| 154 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 155 | static std::string uidString(const gui::Uid& uid) { |
| 156 | return uid.toString(); |
| 157 | } |
| 158 | |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 159 | Result<void> checkKeyAction(int32_t action) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 160 | switch (action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 161 | case AKEY_EVENT_ACTION_DOWN: |
| 162 | case AKEY_EVENT_ACTION_UP: |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 163 | return {}; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 164 | default: |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 165 | return Error() << "Key event has invalid action code " << action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 169 | Result<void> validateKeyEvent(int32_t action) { |
| 170 | return checkKeyAction(action); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 171 | } |
| 172 | |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 173 | Result<void> checkMotionAction(int32_t action, int32_t actionButton, int32_t pointerCount) { |
Siarhei Vishniakou | 2f61bdc | 2022-12-02 08:55:51 -0800 | [diff] [blame] | 174 | switch (MotionEvent::getActionMasked(action)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 175 | case AMOTION_EVENT_ACTION_DOWN: |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 176 | case AMOTION_EVENT_ACTION_UP: { |
| 177 | if (pointerCount != 1) { |
| 178 | return Error() << "invalid pointer count " << pointerCount; |
| 179 | } |
| 180 | return {}; |
| 181 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 182 | case AMOTION_EVENT_ACTION_MOVE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 183 | case AMOTION_EVENT_ACTION_HOVER_ENTER: |
| 184 | case AMOTION_EVENT_ACTION_HOVER_MOVE: |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 185 | case AMOTION_EVENT_ACTION_HOVER_EXIT: { |
| 186 | if (pointerCount < 1) { |
| 187 | return Error() << "invalid pointer count " << pointerCount; |
| 188 | } |
| 189 | return {}; |
| 190 | } |
Siarhei Vishniakou | 2f61bdc | 2022-12-02 08:55:51 -0800 | [diff] [blame] | 191 | case AMOTION_EVENT_ACTION_CANCEL: |
| 192 | case AMOTION_EVENT_ACTION_OUTSIDE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 193 | case AMOTION_EVENT_ACTION_SCROLL: |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 194 | return {}; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 195 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 196 | case AMOTION_EVENT_ACTION_POINTER_UP: { |
Siarhei Vishniakou | 2f61bdc | 2022-12-02 08:55:51 -0800 | [diff] [blame] | 197 | const int32_t index = MotionEvent::getActionIndex(action); |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 198 | if (index < 0) { |
| 199 | return Error() << "invalid index " << index << " for " |
| 200 | << MotionEvent::actionToString(action); |
| 201 | } |
| 202 | if (index >= pointerCount) { |
| 203 | return Error() << "invalid index " << index << " for pointerCount " << pointerCount; |
| 204 | } |
| 205 | if (pointerCount <= 1) { |
| 206 | return Error() << "invalid pointer count " << pointerCount << " for " |
| 207 | << MotionEvent::actionToString(action); |
| 208 | } |
| 209 | return {}; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 210 | } |
| 211 | case AMOTION_EVENT_ACTION_BUTTON_PRESS: |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 212 | case AMOTION_EVENT_ACTION_BUTTON_RELEASE: { |
| 213 | if (actionButton == 0) { |
| 214 | return Error() << "action button should be nonzero for " |
| 215 | << MotionEvent::actionToString(action); |
| 216 | } |
| 217 | return {}; |
| 218 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 219 | default: |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 220 | return Error() << "invalid action " << action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 224 | int64_t millis(std::chrono::nanoseconds t) { |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 225 | return std::chrono::duration_cast<std::chrono::milliseconds>(t).count(); |
| 226 | } |
| 227 | |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 228 | Result<void> validateMotionEvent(int32_t action, int32_t actionButton, size_t pointerCount, |
| 229 | const PointerProperties* pointerProperties) { |
| 230 | Result<void> actionCheck = checkMotionAction(action, actionButton, pointerCount); |
| 231 | if (!actionCheck.ok()) { |
| 232 | return actionCheck; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 233 | } |
| 234 | if (pointerCount < 1 || pointerCount > MAX_POINTERS) { |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 235 | return Error() << "Motion event has invalid pointer count " << pointerCount |
| 236 | << "; value must be between 1 and " << MAX_POINTERS << "."; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 237 | } |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 238 | std::bitset<MAX_POINTER_ID + 1> pointerIdBits; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 239 | for (size_t i = 0; i < pointerCount; i++) { |
| 240 | int32_t id = pointerProperties[i].id; |
| 241 | if (id < 0 || id > MAX_POINTER_ID) { |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 242 | return Error() << "Motion event has invalid pointer id " << id |
| 243 | << "; value must be between 0 and " << MAX_POINTER_ID; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 244 | } |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 245 | if (pointerIdBits.test(id)) { |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 246 | return Error() << "Motion event has duplicate pointer id " << id; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 247 | } |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 248 | pointerIdBits.set(id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 249 | } |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 250 | return {}; |
| 251 | } |
| 252 | |
| 253 | Result<void> validateInputEvent(const InputEvent& event) { |
| 254 | switch (event.getType()) { |
| 255 | case InputEventType::KEY: { |
| 256 | const KeyEvent& key = static_cast<const KeyEvent&>(event); |
| 257 | const int32_t action = key.getAction(); |
| 258 | return validateKeyEvent(action); |
| 259 | } |
| 260 | case InputEventType::MOTION: { |
| 261 | const MotionEvent& motion = static_cast<const MotionEvent&>(event); |
| 262 | const int32_t action = motion.getAction(); |
| 263 | const size_t pointerCount = motion.getPointerCount(); |
| 264 | const PointerProperties* pointerProperties = motion.getPointerProperties(); |
| 265 | const int32_t actionButton = motion.getActionButton(); |
| 266 | return validateMotionEvent(action, actionButton, pointerCount, pointerProperties); |
| 267 | } |
| 268 | default: { |
| 269 | return {}; |
| 270 | } |
| 271 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 272 | } |
| 273 | |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 274 | std::bitset<MAX_POINTER_ID + 1> getPointerIds(const std::vector<PointerProperties>& pointers) { |
| 275 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
| 276 | for (const PointerProperties& pointer : pointers) { |
| 277 | pointerIds.set(pointer.id); |
| 278 | } |
| 279 | return pointerIds; |
| 280 | } |
| 281 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 282 | std::string dumpRegion(const Region& region) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 283 | if (region.isEmpty()) { |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 284 | return "<empty>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 285 | } |
| 286 | |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 287 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 288 | bool first = true; |
| 289 | Region::const_iterator cur = region.begin(); |
| 290 | Region::const_iterator const tail = region.end(); |
| 291 | while (cur != tail) { |
| 292 | if (first) { |
| 293 | first = false; |
| 294 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 295 | dump += "|"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 296 | } |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 297 | 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] | 298 | cur++; |
| 299 | } |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 300 | return dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 301 | } |
| 302 | |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 303 | std::string dumpQueue(const std::deque<std::unique_ptr<DispatchEntry>>& queue, |
| 304 | nsecs_t currentTime) { |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 305 | constexpr size_t maxEntries = 50; // max events to print |
| 306 | constexpr size_t skipBegin = maxEntries / 2; |
| 307 | const size_t skipEnd = queue.size() - maxEntries / 2; |
| 308 | // skip from maxEntries / 2 ... size() - maxEntries/2 |
| 309 | // only print from 0 .. skipBegin and then from skipEnd .. size() |
| 310 | |
| 311 | std::string dump; |
| 312 | for (size_t i = 0; i < queue.size(); i++) { |
| 313 | const DispatchEntry& entry = *queue[i]; |
| 314 | if (i >= skipBegin && i < skipEnd) { |
| 315 | dump += StringPrintf(INDENT4 "<skipped %zu entries>\n", skipEnd - skipBegin); |
| 316 | i = skipEnd - 1; // it will be incremented to "skipEnd" by 'continue' |
| 317 | continue; |
| 318 | } |
| 319 | dump.append(INDENT4); |
| 320 | dump += entry.eventEntry->getDescription(); |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 321 | dump += StringPrintf(", seq=%" PRIu32 ", targetFlags=%s, age=%" PRId64 "ms", entry.seq, |
| 322 | entry.targetFlags.string().c_str(), |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 323 | ns2ms(currentTime - entry.eventEntry->eventTime)); |
| 324 | if (entry.deliveryTime != 0) { |
| 325 | // This entry was delivered, so add information on how long we've been waiting |
| 326 | dump += StringPrintf(", wait=%" PRId64 "ms", ns2ms(currentTime - entry.deliveryTime)); |
| 327 | } |
| 328 | dump.append("\n"); |
| 329 | } |
| 330 | return dump; |
| 331 | } |
| 332 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 333 | /** |
| 334 | * Find the entry in std::unordered_map by key, and return it. |
| 335 | * If the entry is not found, return a default constructed entry. |
| 336 | * |
| 337 | * Useful when the entries are vectors, since an empty vector will be returned |
| 338 | * if the entry is not found. |
| 339 | * Also useful when the entries are sp<>. If an entry is not found, nullptr is returned. |
| 340 | */ |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 341 | template <typename K, typename V> |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 342 | V getValueByKey(const std::unordered_map<K, V>& map, K key) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 343 | auto it = map.find(key); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 344 | return it != map.end() ? it->second : V{}; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 345 | } |
| 346 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 347 | bool haveSameToken(const sp<WindowInfoHandle>& first, const sp<WindowInfoHandle>& second) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 348 | if (first == second) { |
| 349 | return true; |
| 350 | } |
| 351 | |
| 352 | if (first == nullptr || second == nullptr) { |
| 353 | return false; |
| 354 | } |
| 355 | |
| 356 | return first->getToken() == second->getToken(); |
| 357 | } |
| 358 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 359 | bool haveSameApplicationToken(const WindowInfo* first, const WindowInfo* second) { |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 360 | if (first == nullptr || second == nullptr) { |
| 361 | return false; |
| 362 | } |
| 363 | return first->applicationInfo.token != nullptr && |
| 364 | first->applicationInfo.token == second->applicationInfo.token; |
| 365 | } |
| 366 | |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 367 | template <typename T> |
| 368 | size_t firstMarkedBit(T set) { |
| 369 | // TODO: replace with std::countr_zero from <bit> when that's available |
| 370 | LOG_ALWAYS_FATAL_IF(set.none()); |
| 371 | size_t i = 0; |
| 372 | while (!set.test(i)) { |
| 373 | i++; |
| 374 | } |
| 375 | return i; |
| 376 | } |
| 377 | |
Prabir Pradhan | c88b1fa | 2024-01-23 21:48:03 +0000 | [diff] [blame] | 378 | std::unique_ptr<DispatchEntry> createDispatchEntry(const IdGenerator& idGenerator, |
| 379 | const InputTarget& inputTarget, |
Prabir Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame] | 380 | std::shared_ptr<const EventEntry> eventEntry, |
| 381 | ftl::Flags<InputTarget::Flags> inputTargetFlags, |
| 382 | int64_t vsyncId) { |
Prabir Pradhan | c88b1fa | 2024-01-23 21:48:03 +0000 | [diff] [blame] | 383 | const bool zeroCoords = inputTargetFlags.test(InputTarget::Flags::ZERO_COORDS); |
Prabir Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame] | 384 | const sp<WindowInfoHandle> win = inputTarget.windowHandle; |
| 385 | const std::optional<int32_t> windowId = |
| 386 | win ? std::make_optional(win->getInfo()->id) : std::nullopt; |
| 387 | // Assume the only targets that are not associated with a window are global monitors, and use |
| 388 | // the system UID for global monitors for tracing purposes. |
| 389 | const gui::Uid uid = win ? win->getInfo()->ownerUid : gui::Uid(AID_SYSTEM); |
Prabir Pradhan | c88b1fa | 2024-01-23 21:48:03 +0000 | [diff] [blame] | 390 | |
| 391 | if (inputTarget.useDefaultPointerTransform() && !zeroCoords) { |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 392 | const ui::Transform& transform = inputTarget.getDefaultPointerTransform(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 393 | return std::make_unique<DispatchEntry>(eventEntry, inputTargetFlags, transform, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 394 | inputTarget.displayTransform, |
Prabir Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame] | 395 | inputTarget.globalScaleFactor, uid, vsyncId, |
| 396 | windowId); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | ALOG_ASSERT(eventEntry->type == EventEntry::Type::MOTION); |
| 400 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); |
| 401 | |
Prabir Pradhan | c88b1fa | 2024-01-23 21:48:03 +0000 | [diff] [blame] | 402 | std::vector<PointerCoords> pointerCoords{motionEntry.getPointerCount()}; |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 403 | |
Prabir Pradhan | c88b1fa | 2024-01-23 21:48:03 +0000 | [diff] [blame] | 404 | const ui::Transform* transform = &kIdentityTransform; |
| 405 | const ui::Transform* displayTransform = &kIdentityTransform; |
| 406 | if (zeroCoords) { |
| 407 | std::for_each(pointerCoords.begin(), pointerCoords.end(), [](auto& pc) { pc.clear(); }); |
| 408 | } else { |
| 409 | // Use the first pointer information to normalize all other pointers. This could be any |
| 410 | // pointer as long as all other pointers are normalized to the same value and the final |
| 411 | // DispatchEntry uses the transform for the normalized pointer. |
| 412 | transform = |
| 413 | &inputTarget.getTransformForPointer(firstMarkedBit(inputTarget.getPointerIds())); |
| 414 | const ui::Transform inverseTransform = transform->inverse(); |
| 415 | displayTransform = &inputTarget.displayTransform; |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 416 | |
Prabir Pradhan | c88b1fa | 2024-01-23 21:48:03 +0000 | [diff] [blame] | 417 | // Iterate through all pointers in the event to normalize against the first. |
| 418 | for (size_t i = 0; i < motionEntry.getPointerCount(); i++) { |
| 419 | PointerCoords& newCoords = pointerCoords[i]; |
| 420 | const auto pointerId = motionEntry.pointerProperties[i].id; |
| 421 | const ui::Transform& currTransform = inputTarget.getTransformForPointer(pointerId); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 422 | |
Prabir Pradhan | c88b1fa | 2024-01-23 21:48:03 +0000 | [diff] [blame] | 423 | newCoords.copyFrom(motionEntry.pointerCoords[i]); |
| 424 | // First, apply the current pointer's transform to update the coordinates into |
| 425 | // window space. |
| 426 | newCoords.transform(currTransform); |
| 427 | // Next, apply the inverse transform of the normalized coordinates so the |
| 428 | // current coordinates are transformed into the normalized coordinate space. |
| 429 | newCoords.transform(inverseTransform); |
| 430 | } |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 431 | } |
| 432 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 433 | std::unique_ptr<MotionEntry> combinedMotionEntry = |
Prabir Pradhan | c88b1fa | 2024-01-23 21:48:03 +0000 | [diff] [blame] | 434 | std::make_unique<MotionEntry>(idGenerator.nextId(), motionEntry.injectionState, |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 435 | motionEntry.eventTime, motionEntry.deviceId, |
| 436 | motionEntry.source, motionEntry.displayId, |
| 437 | motionEntry.policyFlags, motionEntry.action, |
| 438 | motionEntry.actionButton, motionEntry.flags, |
| 439 | motionEntry.metaState, motionEntry.buttonState, |
| 440 | motionEntry.classification, motionEntry.edgeFlags, |
| 441 | motionEntry.xPrecision, motionEntry.yPrecision, |
| 442 | motionEntry.xCursorPosition, motionEntry.yCursorPosition, |
| 443 | motionEntry.downTime, motionEntry.pointerProperties, |
| 444 | pointerCoords); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 445 | |
| 446 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 447 | std::make_unique<DispatchEntry>(std::move(combinedMotionEntry), inputTargetFlags, |
Prabir Pradhan | c88b1fa | 2024-01-23 21:48:03 +0000 | [diff] [blame] | 448 | *transform, *displayTransform, |
Prabir Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame] | 449 | inputTarget.globalScaleFactor, uid, vsyncId, windowId); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 450 | return dispatchEntry; |
| 451 | } |
| 452 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 453 | template <typename T> |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 454 | 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] | 455 | if (lhs == nullptr && rhs == nullptr) { |
| 456 | return true; |
| 457 | } |
| 458 | if (lhs == nullptr || rhs == nullptr) { |
| 459 | return false; |
| 460 | } |
| 461 | return *lhs == *rhs; |
| 462 | } |
| 463 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 464 | KeyEvent createKeyEvent(const KeyEntry& entry) { |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 465 | KeyEvent event; |
| 466 | event.initialize(entry.id, entry.deviceId, entry.source, entry.displayId, INVALID_HMAC, |
| 467 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, |
| 468 | entry.repeatCount, entry.downTime, entry.eventTime); |
| 469 | return event; |
| 470 | } |
| 471 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 472 | bool shouldReportMetricsForConnection(const Connection& connection) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 473 | // Do not keep track of gesture monitors. They receive every event and would disproportionately |
| 474 | // affect the statistics. |
| 475 | if (connection.monitor) { |
| 476 | return false; |
| 477 | } |
| 478 | // If the connection is experiencing ANR, let's skip it. We have separate ANR metrics |
| 479 | if (!connection.responsive) { |
| 480 | return false; |
| 481 | } |
| 482 | return true; |
| 483 | } |
| 484 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 485 | bool shouldReportFinishedEvent(const DispatchEntry& dispatchEntry, const Connection& connection) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 486 | const EventEntry& eventEntry = *dispatchEntry.eventEntry; |
| 487 | const int32_t& inputEventId = eventEntry.id; |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 488 | if (inputEventId == android::os::IInputConstants::INVALID_INPUT_EVENT_ID) { |
| 489 | return false; |
| 490 | } |
| 491 | // Only track latency for events that originated from hardware |
| 492 | if (eventEntry.isSynthesized()) { |
| 493 | return false; |
| 494 | } |
| 495 | const EventEntry::Type& inputEventEntryType = eventEntry.type; |
| 496 | if (inputEventEntryType == EventEntry::Type::KEY) { |
| 497 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 498 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
| 499 | return false; |
| 500 | } |
| 501 | } else if (inputEventEntryType == EventEntry::Type::MOTION) { |
| 502 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 503 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL || |
| 504 | motionEntry.action == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 505 | return false; |
| 506 | } |
| 507 | } else { |
| 508 | // Not a key or a motion |
| 509 | return false; |
| 510 | } |
| 511 | if (!shouldReportMetricsForConnection(connection)) { |
| 512 | return false; |
| 513 | } |
| 514 | return true; |
| 515 | } |
| 516 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 517 | /** |
| 518 | * Connection is responsive if it has no events in the waitQueue that are older than the |
| 519 | * current time. |
| 520 | */ |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 521 | bool isConnectionResponsive(const Connection& connection) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 522 | const nsecs_t currentTime = now(); |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 523 | for (const auto& dispatchEntry : connection.waitQueue) { |
| 524 | if (dispatchEntry->timeoutTime < currentTime) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 525 | return false; |
| 526 | } |
| 527 | } |
| 528 | return true; |
| 529 | } |
| 530 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 531 | // Returns true if the event type passed as argument represents a user activity. |
| 532 | bool isUserActivityEvent(const EventEntry& eventEntry) { |
| 533 | switch (eventEntry.type) { |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 534 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 535 | case EventEntry::Type::DEVICE_RESET: |
| 536 | case EventEntry::Type::DRAG: |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 537 | case EventEntry::Type::FOCUS: |
| 538 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 539 | case EventEntry::Type::SENSOR: |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 540 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 541 | return false; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 542 | case EventEntry::Type::KEY: |
| 543 | case EventEntry::Type::MOTION: |
| 544 | return true; |
| 545 | } |
| 546 | } |
| 547 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 548 | // 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] | 549 | bool windowAcceptsTouchAt(const WindowInfo& windowInfo, int32_t displayId, float x, float y, |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 550 | bool isStylus, const ui::Transform& displayTransform) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 551 | const auto inputConfig = windowInfo.inputConfig; |
| 552 | if (windowInfo.displayId != displayId || |
| 553 | inputConfig.test(WindowInfo::InputConfig::NOT_VISIBLE)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 554 | return false; |
| 555 | } |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 556 | const bool windowCanInterceptTouch = isStylus && windowInfo.interceptsStylus(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 557 | if (inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE) && !windowCanInterceptTouch) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 558 | return false; |
| 559 | } |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 560 | |
| 561 | // Window Manager works in the logical display coordinate space. When it specifies bounds for a |
| 562 | // window as (l, t, r, b), the range of x in [l, r) and y in [t, b) are considered to be inside |
| 563 | // the window. Points on the right and bottom edges should not be inside the window, so we need |
| 564 | // to be careful about performing a hit test when the display is rotated, since the "right" and |
| 565 | // "bottom" of the window will be different in the display (un-rotated) space compared to in the |
| 566 | // logical display in which WM determined the bounds. Perform the hit test in the logical |
| 567 | // display space to ensure these edges are considered correctly in all orientations. |
| 568 | const auto touchableRegion = displayTransform.transform(windowInfo.touchableRegion); |
| 569 | const auto p = displayTransform.transform(x, y); |
| 570 | if (!touchableRegion.contains(std::floor(p.x), std::floor(p.y))) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 571 | return false; |
| 572 | } |
| 573 | return true; |
| 574 | } |
| 575 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 576 | bool isPointerFromStylus(const MotionEntry& entry, int32_t pointerIndex) { |
| 577 | return isFromSource(entry.source, AINPUT_SOURCE_STYLUS) && |
Prabir Pradhan | e562696 | 2022-10-27 20:30:53 +0000 | [diff] [blame] | 578 | isStylusToolType(entry.pointerProperties[pointerIndex].toolType); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 579 | } |
| 580 | |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 581 | // Determines if the given window can be targeted as InputTarget::Flags::FOREGROUND. |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 582 | // Foreground events are only sent to "foreground targetable" windows, but not all gestures sent to |
| 583 | // such window are necessarily targeted with the flag. For example, an event with ACTION_OUTSIDE can |
| 584 | // 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] | 585 | // InputTarget::Flags::FOREGROUND. |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 586 | bool canReceiveForegroundTouches(const WindowInfo& info) { |
| 587 | // A non-touchable window can still receive touch events (e.g. in the case of |
| 588 | // STYLUS_INTERCEPTOR), so prevent such windows from receiving foreground events for touches. |
| 589 | return !info.inputConfig.test(gui::WindowInfo::InputConfig::NOT_TOUCHABLE) && !info.isSpy(); |
| 590 | } |
| 591 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 592 | bool isWindowOwnedBy(const sp<WindowInfoHandle>& windowHandle, gui::Pid pid, gui::Uid uid) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 593 | if (windowHandle == nullptr) { |
| 594 | return false; |
| 595 | } |
| 596 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 597 | if (pid == windowInfo->ownerPid && uid == windowInfo->ownerUid) { |
| 598 | return true; |
| 599 | } |
| 600 | return false; |
| 601 | } |
| 602 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 603 | // Checks targeted injection using the window's owner's uid. |
| 604 | // Returns an empty string if an entry can be sent to the given window, or an error message if the |
| 605 | // entry is a targeted injection whose uid target doesn't match the window owner. |
| 606 | std::optional<std::string> verifyTargetedInjection(const sp<WindowInfoHandle>& window, |
| 607 | const EventEntry& entry) { |
| 608 | if (entry.injectionState == nullptr || !entry.injectionState->targetUid) { |
| 609 | // The event was not injected, or the injected event does not target a window. |
| 610 | return {}; |
| 611 | } |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 612 | const auto uid = *entry.injectionState->targetUid; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 613 | if (window == nullptr) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 614 | return StringPrintf("No valid window target for injection into uid %s.", |
| 615 | uid.toString().c_str()); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 616 | } |
| 617 | if (entry.injectionState->targetUid != window->getInfo()->ownerUid) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 618 | return StringPrintf("Injected event targeted at uid %s would be dispatched to window '%s' " |
| 619 | "owned by uid %s.", |
| 620 | uid.toString().c_str(), window->getName().c_str(), |
| 621 | window->getInfo()->ownerUid.toString().c_str()); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 622 | } |
| 623 | return {}; |
| 624 | } |
| 625 | |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 626 | std::pair<float, float> resolveTouchedPosition(const MotionEntry& entry) { |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 627 | const bool isFromMouse = isFromSource(entry.source, AINPUT_SOURCE_MOUSE); |
| 628 | // Always dispatch mouse events to cursor position. |
| 629 | if (isFromMouse) { |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 630 | return {entry.xCursorPosition, entry.yCursorPosition}; |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 631 | } |
| 632 | |
Siarhei Vishniakou | 5b9766d | 2023-07-18 14:06:29 -0700 | [diff] [blame] | 633 | const int32_t pointerIndex = MotionEvent::getActionIndex(entry.action); |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 634 | return {entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 635 | entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_Y)}; |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 636 | } |
| 637 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 638 | std::optional<nsecs_t> getDownTime(const EventEntry& eventEntry) { |
| 639 | if (eventEntry.type == EventEntry::Type::KEY) { |
| 640 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 641 | return keyEntry.downTime; |
| 642 | } else if (eventEntry.type == EventEntry::Type::MOTION) { |
| 643 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 644 | return motionEntry.downTime; |
| 645 | } |
| 646 | return std::nullopt; |
| 647 | } |
| 648 | |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 649 | /** |
| 650 | * Compare the old touch state to the new touch state, and generate the corresponding touched |
| 651 | * windows (== input targets). |
| 652 | * If a window had the hovering pointer, but now it doesn't, produce HOVER_EXIT for that window. |
| 653 | * If the pointer just entered the new window, produce HOVER_ENTER. |
| 654 | * For pointers remaining in the window, produce HOVER_MOVE. |
| 655 | */ |
| 656 | std::vector<TouchedWindow> getHoveringWindowsLocked(const TouchState* oldState, |
| 657 | const TouchState& newTouchState, |
| 658 | const MotionEntry& entry) { |
| 659 | std::vector<TouchedWindow> out; |
| 660 | const int32_t maskedAction = MotionEvent::getActionMasked(entry.action); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 661 | |
| 662 | if (maskedAction == AMOTION_EVENT_ACTION_SCROLL) { |
| 663 | // ACTION_SCROLL events should not affect the hovering pointer dispatch |
| 664 | return {}; |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | // 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] | 668 | const PointerProperties& pointer = entry.pointerProperties[0]; |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 669 | |
| 670 | std::set<sp<WindowInfoHandle>> oldWindows; |
| 671 | if (oldState != nullptr) { |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 672 | oldWindows = oldState->getWindowsWithHoveringPointer(entry.deviceId, pointer.id); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | std::set<sp<WindowInfoHandle>> newWindows = |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 676 | newTouchState.getWindowsWithHoveringPointer(entry.deviceId, pointer.id); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 677 | |
| 678 | // If the pointer is no longer in the new window set, send HOVER_EXIT. |
| 679 | for (const sp<WindowInfoHandle>& oldWindow : oldWindows) { |
| 680 | if (newWindows.find(oldWindow) == newWindows.end()) { |
| 681 | TouchedWindow touchedWindow; |
| 682 | touchedWindow.windowHandle = oldWindow; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 683 | touchedWindow.dispatchMode = InputTarget::DispatchMode::HOVER_EXIT; |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 684 | out.push_back(touchedWindow); |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | for (const sp<WindowInfoHandle>& newWindow : newWindows) { |
| 689 | TouchedWindow touchedWindow; |
| 690 | touchedWindow.windowHandle = newWindow; |
| 691 | if (oldWindows.find(newWindow) == oldWindows.end()) { |
| 692 | // Any windows that have this pointer now, and didn't have it before, should get |
| 693 | // HOVER_ENTER |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 694 | touchedWindow.dispatchMode = InputTarget::DispatchMode::HOVER_ENTER; |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 695 | } else { |
| 696 | // This pointer was already sent to the window. Use ACTION_HOVER_MOVE. |
Siarhei Vishniakou | c2eb850 | 2023-04-11 18:33:36 -0700 | [diff] [blame] | 697 | if (CC_UNLIKELY(maskedAction != AMOTION_EVENT_ACTION_HOVER_MOVE)) { |
Daniel Norman | 7487dfa | 2023-08-02 16:39:45 -0700 | [diff] [blame] | 698 | android::base::LogSeverity severity = android::base::LogSeverity::FATAL; |
Ameer Armaly | cff4fa5 | 2023-10-04 23:45:11 +0000 | [diff] [blame] | 699 | if (!input_flags::a11y_crash_on_inconsistent_event_stream() && |
| 700 | entry.flags & AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT) { |
Daniel Norman | 7487dfa | 2023-08-02 16:39:45 -0700 | [diff] [blame] | 701 | // The Accessibility injected touch exploration event stream |
| 702 | // has known inconsistencies, so log ERROR instead of |
| 703 | // crashing the device with FATAL. |
Daniel Norman | 7487dfa | 2023-08-02 16:39:45 -0700 | [diff] [blame] | 704 | severity = android::base::LogSeverity::ERROR; |
| 705 | } |
| 706 | LOG(severity) << "Expected ACTION_HOVER_MOVE instead of " << entry.getDescription(); |
Siarhei Vishniakou | c2eb850 | 2023-04-11 18:33:36 -0700 | [diff] [blame] | 707 | } |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 708 | touchedWindow.dispatchMode = InputTarget::DispatchMode::AS_IS; |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 709 | } |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 710 | touchedWindow.addHoveringPointer(entry.deviceId, pointer); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 711 | if (canReceiveForegroundTouches(*newWindow->getInfo())) { |
| 712 | touchedWindow.targetFlags |= InputTarget::Flags::FOREGROUND; |
| 713 | } |
| 714 | out.push_back(touchedWindow); |
| 715 | } |
| 716 | return out; |
| 717 | } |
| 718 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 719 | template <typename T> |
| 720 | std::vector<T>& operator+=(std::vector<T>& left, const std::vector<T>& right) { |
| 721 | left.insert(left.end(), right.begin(), right.end()); |
| 722 | return left; |
| 723 | } |
| 724 | |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 725 | // Filter windows in a TouchState and targets in a vector to remove untrusted windows/targets from |
| 726 | // both. |
| 727 | void filterUntrustedTargets(TouchState& touchState, std::vector<InputTarget>& targets) { |
| 728 | std::erase_if(touchState.windows, [&](const TouchedWindow& window) { |
| 729 | if (!window.windowHandle->getInfo()->inputConfig.test( |
| 730 | WindowInfo::InputConfig::TRUSTED_OVERLAY)) { |
| 731 | // In addition to TouchState, erase this window from the input targets! We don't have a |
| 732 | // good way to do this today except by adding a nested loop. |
| 733 | // TODO(b/282025641): simplify this code once InputTargets are being identified |
| 734 | // separately from TouchedWindows. |
| 735 | std::erase_if(targets, [&](const InputTarget& target) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 736 | return target.connection->getToken() == window.windowHandle->getToken(); |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 737 | }); |
| 738 | return true; |
| 739 | } |
| 740 | return false; |
| 741 | }); |
| 742 | } |
| 743 | |
Siarhei Vishniakou | ce1fd47 | 2023-09-18 18:38:07 -0700 | [diff] [blame] | 744 | /** |
| 745 | * In general, touch should be always split between windows. Some exceptions: |
| 746 | * 1. Don't split touch if all of the below is true: |
| 747 | * (a) we have an active pointer down *and* |
| 748 | * (b) a new pointer is going down that's from the same device *and* |
| 749 | * (c) the window that's receiving the current pointer does not support split touch. |
| 750 | * 2. Don't split mouse events |
| 751 | */ |
| 752 | bool shouldSplitTouch(const TouchState& touchState, const MotionEntry& entry) { |
| 753 | if (isFromSource(entry.source, AINPUT_SOURCE_MOUSE)) { |
| 754 | // We should never split mouse events |
| 755 | return false; |
| 756 | } |
| 757 | for (const TouchedWindow& touchedWindow : touchState.windows) { |
| 758 | if (touchedWindow.windowHandle->getInfo()->isSpy()) { |
| 759 | // Spy windows should not affect whether or not touch is split. |
| 760 | continue; |
| 761 | } |
| 762 | if (touchedWindow.windowHandle->getInfo()->supportsSplitTouch()) { |
| 763 | continue; |
| 764 | } |
| 765 | if (touchedWindow.windowHandle->getInfo()->inputConfig.test( |
| 766 | gui::WindowInfo::InputConfig::IS_WALLPAPER)) { |
| 767 | // Wallpaper window should not affect whether or not touch is split |
| 768 | continue; |
| 769 | } |
| 770 | |
| 771 | if (touchedWindow.hasTouchingPointers(entry.deviceId)) { |
| 772 | return false; |
| 773 | } |
| 774 | } |
| 775 | return true; |
| 776 | } |
| 777 | |
Siarhei Vishniakou | f77f60a | 2023-10-23 17:26:05 -0700 | [diff] [blame] | 778 | /** |
| 779 | * Return true if stylus is currently down anywhere on the specified display, and false otherwise. |
| 780 | */ |
| 781 | bool isStylusActiveInDisplay( |
| 782 | int32_t displayId, |
| 783 | const std::unordered_map<int32_t /*displayId*/, TouchState>& touchStatesByDisplay) { |
| 784 | const auto it = touchStatesByDisplay.find(displayId); |
| 785 | if (it == touchStatesByDisplay.end()) { |
| 786 | return false; |
| 787 | } |
| 788 | const TouchState& state = it->second; |
| 789 | return state.hasActiveStylus(); |
| 790 | } |
| 791 | |
Siarhei Vishniakou | aeed0da | 2024-01-09 08:57:13 -0800 | [diff] [blame] | 792 | Result<void> validateWindowInfosUpdate(const gui::WindowInfosUpdate& update) { |
| 793 | struct HashFunction { |
| 794 | size_t operator()(const WindowInfo& info) const { return info.id; } |
| 795 | }; |
| 796 | |
| 797 | std::unordered_set<WindowInfo, HashFunction> windowSet; |
| 798 | for (const WindowInfo& info : update.windowInfos) { |
| 799 | const auto [_, inserted] = windowSet.insert(info); |
| 800 | if (!inserted) { |
| 801 | return Error() << "Duplicate entry for " << info; |
| 802 | } |
| 803 | } |
| 804 | return {}; |
| 805 | } |
| 806 | |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 807 | int32_t getUserActivityEventType(const EventEntry& eventEntry) { |
| 808 | switch (eventEntry.type) { |
| 809 | case EventEntry::Type::KEY: { |
| 810 | return USER_ACTIVITY_EVENT_BUTTON; |
| 811 | } |
| 812 | case EventEntry::Type::MOTION: { |
| 813 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 814 | if (MotionEvent::isTouchEvent(motionEntry.source, motionEntry.action)) { |
| 815 | return USER_ACTIVITY_EVENT_TOUCH; |
| 816 | } |
| 817 | return USER_ACTIVITY_EVENT_OTHER; |
| 818 | } |
| 819 | default: { |
| 820 | LOG_ALWAYS_FATAL("%s events are not user activity", |
| 821 | ftl::enum_string(eventEntry.type).c_str()); |
| 822 | } |
| 823 | } |
| 824 | } |
| 825 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 826 | std::pair<bool /*cancelPointers*/, bool /*cancelNonPointers*/> expandCancellationMode( |
| 827 | CancelationOptions::Mode mode) { |
| 828 | switch (mode) { |
| 829 | case CancelationOptions::Mode::CANCEL_ALL_EVENTS: |
| 830 | return {true, true}; |
| 831 | case CancelationOptions::Mode::CANCEL_POINTER_EVENTS: |
| 832 | return {true, false}; |
| 833 | case CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS: |
| 834 | return {false, true}; |
| 835 | case CancelationOptions::Mode::CANCEL_FALLBACK_EVENTS: |
| 836 | return {false, true}; |
| 837 | } |
| 838 | } |
| 839 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 840 | } // namespace |
| 841 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 842 | // --- InputDispatcher --- |
| 843 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 844 | InputDispatcher::InputDispatcher(InputDispatcherPolicyInterface& policy) |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame^] | 845 | : InputDispatcher(policy, createInputTracingBackendIfEnabled()) {} |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 846 | |
| 847 | InputDispatcher::InputDispatcher(InputDispatcherPolicyInterface& policy, |
| 848 | std::unique_ptr<trace::InputTracingBackendInterface> traceBackend) |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 849 | : mPolicy(policy), |
| 850 | mPendingEvent(nullptr), |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 851 | mLastDropReason(DropReason::NOT_DROPPED), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 852 | mIdGenerator(IdGenerator::Source::INPUT_DISPATCHER), |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 853 | mMinTimeBetweenUserActivityPokes(DEFAULT_USER_ACTIVITY_POKE_INTERVAL), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 854 | mNextUnblockedEvent(nullptr), |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 855 | mMonitorDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 856 | mDispatchEnabled(false), |
| 857 | mDispatchFrozen(false), |
| 858 | mInputFilterEnabled(false), |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 859 | mMaximumObscuringOpacityForTouch(1.0f), |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 860 | mFocusedDisplayId(ADISPLAY_ID_DEFAULT), |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 861 | mWindowTokenWithPointerCapture(nullptr), |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 862 | mLatencyAggregator(), |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 863 | mLatencyTracker(&mLatencyAggregator) { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 864 | mLooper = sp<Looper>::make(false); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 865 | mReporter = createInputReporter(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 866 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 867 | mWindowInfoListener = sp<DispatcherWindowListener>::make(*this); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 868 | #if defined(__ANDROID__) |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 869 | SurfaceComposerClient::getDefault()->addWindowInfosListener(mWindowInfoListener); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 870 | #endif |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 871 | mKeyRepeatState.lastKeyEntry = nullptr; |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 872 | |
| 873 | if (traceBackend) { |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 874 | mTracer = std::make_unique<trace::impl::InputTracer>(std::move(traceBackend)); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 875 | } |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 876 | |
| 877 | mLastUserActivityTimes.fill(0); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | InputDispatcher::~InputDispatcher() { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 881 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 882 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 883 | resetKeyRepeatLocked(); |
| 884 | releasePendingEventLocked(); |
| 885 | drainInboundQueueLocked(); |
| 886 | mCommandQueue.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 887 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 888 | while (!mConnectionsByToken.empty()) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 889 | std::shared_ptr<Connection> connection = mConnectionsByToken.begin()->second; |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 890 | removeInputChannelLocked(connection->getToken(), /*notify=*/false); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 891 | } |
| 892 | } |
| 893 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 894 | status_t InputDispatcher::start() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 895 | if (mThread) { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 896 | return ALREADY_EXISTS; |
| 897 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 898 | mThread = std::make_unique<InputThread>( |
| 899 | "InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); }); |
| 900 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | status_t InputDispatcher::stop() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 904 | if (mThread && mThread->isCallingThread()) { |
| 905 | ALOGE("InputDispatcher cannot be stopped from its own thread!"); |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 906 | return INVALID_OPERATION; |
| 907 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 908 | mThread.reset(); |
| 909 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 910 | } |
| 911 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 912 | void InputDispatcher::dispatchOnce() { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 913 | nsecs_t nextWakeupTime = LLONG_MAX; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 914 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 915 | std::scoped_lock _l(mLock); |
| 916 | mDispatcherIsAlive.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 917 | |
| 918 | // Run a dispatch loop if there are no pending commands. |
| 919 | // The dispatch loop might enqueue commands to run afterwards. |
| 920 | if (!haveCommandsLocked()) { |
Siarhei Vishniakou | 6950596 | 2023-12-28 12:07:04 -0800 | [diff] [blame] | 921 | dispatchOnceInnerLocked(/*byref*/ nextWakeupTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | // Run all pending commands if there are any. |
| 925 | // 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] | 926 | if (runCommandsLockedInterruptable()) { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 927 | nextWakeupTime = LLONG_MIN; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 928 | } |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 929 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 930 | // If we are still waiting for ack on some events, |
| 931 | // we might have to wake up earlier to check if an app is anr'ing. |
| 932 | const nsecs_t nextAnrCheck = processAnrsLocked(); |
| 933 | nextWakeupTime = std::min(nextWakeupTime, nextAnrCheck); |
| 934 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 935 | // We are about to enter an infinitely long sleep, because we have no commands or |
| 936 | // pending or queued events |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 937 | if (nextWakeupTime == LLONG_MAX) { |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 938 | mDispatcherEnteredIdle.notify_all(); |
| 939 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 940 | } // release lock |
| 941 | |
| 942 | // Wait for callback or timeout or wake. (make sure we round up, not down) |
| 943 | nsecs_t currentTime = now(); |
| 944 | int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime); |
| 945 | mLooper->pollOnce(timeoutMillis); |
| 946 | } |
| 947 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 948 | /** |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 949 | * Raise ANR if there is no focused window. |
| 950 | * Before the ANR is raised, do a final state check: |
| 951 | * 1. The currently focused application must be the same one we are waiting for. |
| 952 | * 2. Ensure we still don't have a focused window. |
| 953 | */ |
| 954 | void InputDispatcher::processNoFocusedWindowAnrLocked() { |
| 955 | // Check if the application that we are waiting for is still focused. |
| 956 | std::shared_ptr<InputApplicationHandle> focusedApplication = |
| 957 | getValueByKey(mFocusedApplicationHandlesByDisplay, mAwaitedApplicationDisplayId); |
| 958 | if (focusedApplication == nullptr || |
| 959 | focusedApplication->getApplicationToken() != |
| 960 | mAwaitedFocusedApplication->getApplicationToken()) { |
| 961 | // Unexpected because we should have reset the ANR timer when focused application changed |
| 962 | ALOGE("Waited for a focused window, but focused application has already changed to %s", |
| 963 | focusedApplication->getName().c_str()); |
| 964 | return; // The focused application has changed. |
| 965 | } |
| 966 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 967 | const sp<WindowInfoHandle>& focusedWindowHandle = |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 968 | getFocusedWindowHandleLocked(mAwaitedApplicationDisplayId); |
| 969 | if (focusedWindowHandle != nullptr) { |
| 970 | return; // We now have a focused window. No need for ANR. |
| 971 | } |
| 972 | onAnrLocked(mAwaitedFocusedApplication); |
| 973 | } |
| 974 | |
| 975 | /** |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 976 | * Check if any of the connections' wait queues have events that are too old. |
| 977 | * If we waited for events to be ack'ed for more than the window timeout, raise an ANR. |
| 978 | * Return the time at which we should wake up next. |
| 979 | */ |
| 980 | nsecs_t InputDispatcher::processAnrsLocked() { |
| 981 | const nsecs_t currentTime = now(); |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 982 | nsecs_t nextAnrCheck = LLONG_MAX; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 983 | // Check if we are waiting for a focused window to appear. Raise ANR if waited too long |
| 984 | if (mNoFocusedWindowTimeoutTime.has_value() && mAwaitedFocusedApplication != nullptr) { |
| 985 | if (currentTime >= *mNoFocusedWindowTimeoutTime) { |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 986 | processNoFocusedWindowAnrLocked(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 987 | mAwaitedFocusedApplication.reset(); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 988 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 989 | return LLONG_MIN; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 990 | } else { |
Siarhei Vishniakou | 38a6d27 | 2020-10-20 20:29:33 -0500 | [diff] [blame] | 991 | // Keep waiting. We will drop the event when mNoFocusedWindowTimeoutTime comes. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 992 | nextAnrCheck = *mNoFocusedWindowTimeoutTime; |
| 993 | } |
| 994 | } |
| 995 | |
| 996 | // Check if any connection ANRs are due |
| 997 | nextAnrCheck = std::min(nextAnrCheck, mAnrTracker.firstTimeout()); |
| 998 | if (currentTime < nextAnrCheck) { // most likely scenario |
| 999 | return nextAnrCheck; // everything is normal. Let's check again at nextAnrCheck |
| 1000 | } |
| 1001 | |
| 1002 | // If we reached here, we have an unresponsive connection. |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 1003 | std::shared_ptr<Connection> connection = getConnectionLocked(mAnrTracker.firstToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1004 | if (connection == nullptr) { |
| 1005 | ALOGE("Could not find connection for entry %" PRId64, mAnrTracker.firstTimeout()); |
| 1006 | return nextAnrCheck; |
| 1007 | } |
| 1008 | connection->responsive = false; |
| 1009 | // Stop waking up for this unresponsive connection |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 1010 | mAnrTracker.eraseToken(connection->getToken()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 1011 | onAnrLocked(connection); |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 1012 | return LLONG_MIN; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1013 | } |
| 1014 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 1015 | std::chrono::nanoseconds InputDispatcher::getDispatchingTimeoutLocked( |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 1016 | const std::shared_ptr<Connection>& connection) { |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 1017 | if (connection->monitor) { |
| 1018 | return mMonitorDispatchingTimeout; |
| 1019 | } |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 1020 | const sp<WindowInfoHandle> window = getWindowHandleLocked(connection->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1021 | if (window != nullptr) { |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 1022 | return window->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1023 | } |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 1024 | return DEFAULT_INPUT_DISPATCHING_TIMEOUT; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1025 | } |
| 1026 | |
Siarhei Vishniakou | 6950596 | 2023-12-28 12:07:04 -0800 | [diff] [blame] | 1027 | void InputDispatcher::dispatchOnceInnerLocked(nsecs_t& nextWakeupTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1028 | nsecs_t currentTime = now(); |
| 1029 | |
Jeff Brown | dc5992e | 2014-04-11 01:27:26 -0700 | [diff] [blame] | 1030 | // Reset the key repeat timer whenever normal dispatch is suspended while the |
| 1031 | // device is in a non-interactive state. This is to ensure that we abort a key |
| 1032 | // repeat if the device is just coming out of sleep. |
| 1033 | if (!mDispatchEnabled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1034 | resetKeyRepeatLocked(); |
| 1035 | } |
| 1036 | |
| 1037 | // If dispatching is frozen, do not process timeouts or try to deliver any new events. |
| 1038 | if (mDispatchFrozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1039 | if (DEBUG_FOCUS) { |
| 1040 | ALOGD("Dispatch frozen. Waiting some more."); |
| 1041 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1042 | return; |
| 1043 | } |
| 1044 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1045 | // Ready to start a new event. |
| 1046 | // If we don't already have a pending event, go grab one. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1047 | if (!mPendingEvent) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1048 | if (mInboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1049 | // Synthesize a key repeat if appropriate. |
| 1050 | if (mKeyRepeatState.lastKeyEntry) { |
| 1051 | if (currentTime >= mKeyRepeatState.nextRepeatTime) { |
| 1052 | mPendingEvent = synthesizeKeyRepeatLocked(currentTime); |
| 1053 | } else { |
Siarhei Vishniakou | 6950596 | 2023-12-28 12:07:04 -0800 | [diff] [blame] | 1054 | nextWakeupTime = std::min(nextWakeupTime, mKeyRepeatState.nextRepeatTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1055 | } |
| 1056 | } |
| 1057 | |
| 1058 | // Nothing to do if there is no pending event. |
| 1059 | if (!mPendingEvent) { |
| 1060 | return; |
| 1061 | } |
| 1062 | } else { |
| 1063 | // Inbound queue has at least one entry. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1064 | mPendingEvent = mInboundQueue.front(); |
| 1065 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1066 | traceInboundQueueLengthLocked(); |
| 1067 | } |
| 1068 | |
| 1069 | // Poke user activity for this event. |
| 1070 | if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1071 | pokeUserActivityLocked(*mPendingEvent); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1072 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | // Now we have an event to dispatch. |
| 1076 | // 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] | 1077 | ALOG_ASSERT(mPendingEvent != nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1078 | bool done = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1079 | DropReason dropReason = DropReason::NOT_DROPPED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1080 | if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1081 | dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1082 | } else if (!mDispatchEnabled) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1083 | dropReason = DropReason::DISABLED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | if (mNextUnblockedEvent == mPendingEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1087 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1088 | } |
| 1089 | |
| 1090 | switch (mPendingEvent->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1091 | case EventEntry::Type::CONFIGURATION_CHANGED: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1092 | const ConfigurationChangedEntry& typedEntry = |
| 1093 | static_cast<const ConfigurationChangedEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1094 | done = dispatchConfigurationChangedLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1095 | dropReason = DropReason::NOT_DROPPED; // configuration changes are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1096 | break; |
| 1097 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1098 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1099 | case EventEntry::Type::DEVICE_RESET: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1100 | const DeviceResetEntry& typedEntry = |
| 1101 | static_cast<const DeviceResetEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1102 | done = dispatchDeviceResetLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1103 | dropReason = DropReason::NOT_DROPPED; // device resets are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1104 | break; |
| 1105 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1106 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1107 | case EventEntry::Type::FOCUS: { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1108 | std::shared_ptr<const FocusEntry> typedEntry = |
| 1109 | std::static_pointer_cast<const FocusEntry>(mPendingEvent); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1110 | dispatchFocusLocked(currentTime, typedEntry); |
| 1111 | done = true; |
| 1112 | dropReason = DropReason::NOT_DROPPED; // focus events are never dropped |
| 1113 | break; |
| 1114 | } |
| 1115 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1116 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1117 | const auto typedEntry = std::static_pointer_cast<const TouchModeEntry>(mPendingEvent); |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1118 | dispatchTouchModeChangeLocked(currentTime, typedEntry); |
| 1119 | done = true; |
| 1120 | dropReason = DropReason::NOT_DROPPED; // touch mode events are never dropped |
| 1121 | break; |
| 1122 | } |
| 1123 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1124 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 1125 | const auto typedEntry = |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1126 | std::static_pointer_cast<const PointerCaptureChangedEntry>(mPendingEvent); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1127 | dispatchPointerCaptureChangedLocked(currentTime, typedEntry, dropReason); |
| 1128 | done = true; |
| 1129 | break; |
| 1130 | } |
| 1131 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1132 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1133 | std::shared_ptr<const DragEntry> typedEntry = |
| 1134 | std::static_pointer_cast<const DragEntry>(mPendingEvent); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1135 | dispatchDragLocked(currentTime, typedEntry); |
| 1136 | done = true; |
| 1137 | break; |
| 1138 | } |
| 1139 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1140 | case EventEntry::Type::KEY: { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1141 | std::shared_ptr<const KeyEntry> keyEntry = |
| 1142 | std::static_pointer_cast<const KeyEntry>(mPendingEvent); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1143 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *keyEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1144 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1145 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1146 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 1147 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1148 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1149 | done = dispatchKeyLocked(currentTime, keyEntry, &dropReason, nextWakeupTime); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 1150 | if (done && mTracer) { |
| 1151 | ensureEventTraced(*keyEntry); |
| 1152 | mTracer->eventProcessingComplete(*keyEntry->traceTracker); |
| 1153 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1154 | break; |
| 1155 | } |
| 1156 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1157 | case EventEntry::Type::MOTION: { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1158 | std::shared_ptr<const MotionEntry> motionEntry = |
| 1159 | std::static_pointer_cast<const MotionEntry>(mPendingEvent); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1160 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *motionEntry)) { |
Siarhei Vishniakou | 6b71b63 | 2023-10-27 21:34:46 -0700 | [diff] [blame] | 1161 | // The event is stale. However, only drop stale events if there isn't an ongoing |
| 1162 | // gesture. That would allow us to complete the processing of the current stroke. |
| 1163 | const auto touchStateIt = mTouchStatesByDisplay.find(motionEntry->displayId); |
| 1164 | if (touchStateIt != mTouchStatesByDisplay.end()) { |
| 1165 | const TouchState& touchState = touchStateIt->second; |
| 1166 | if (!touchState.hasTouchingPointers(motionEntry->deviceId) && |
| 1167 | !touchState.hasHoveringPointers(motionEntry->deviceId)) { |
| 1168 | dropReason = DropReason::STALE; |
| 1169 | } |
| 1170 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1171 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1172 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
Siarhei Vishniakou | 99e407b | 2023-12-26 18:09:32 -0800 | [diff] [blame] | 1173 | if (!isFromSource(motionEntry->source, AINPUT_SOURCE_CLASS_POINTER)) { |
| 1174 | // Only drop events that are focus-dispatched. |
| 1175 | dropReason = DropReason::BLOCKED; |
| 1176 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1177 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1178 | done = dispatchMotionLocked(currentTime, motionEntry, &dropReason, nextWakeupTime); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 1179 | if (done && mTracer) { |
| 1180 | ensureEventTraced(*motionEntry); |
| 1181 | mTracer->eventProcessingComplete(*motionEntry->traceTracker); |
| 1182 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1183 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1184 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1185 | |
| 1186 | case EventEntry::Type::SENSOR: { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1187 | std::shared_ptr<const SensorEntry> sensorEntry = |
| 1188 | std::static_pointer_cast<const SensorEntry>(mPendingEvent); |
Siarhei Vishniakou | e2404a1 | 2024-01-16 18:38:39 -0800 | [diff] [blame] | 1189 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1190 | // Sensor timestamps use SYSTEM_TIME_BOOTTIME time base, so we can't use |
| 1191 | // 'currentTime' here, get SYSTEM_TIME_BOOTTIME instead. |
| 1192 | nsecs_t bootTime = systemTime(SYSTEM_TIME_BOOTTIME); |
| 1193 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(bootTime, *sensorEntry)) { |
| 1194 | dropReason = DropReason::STALE; |
| 1195 | } |
| 1196 | dispatchSensorLocked(currentTime, sensorEntry, &dropReason, nextWakeupTime); |
| 1197 | done = true; |
| 1198 | break; |
| 1199 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | if (done) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1203 | if (dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1204 | dropInboundEventLocked(*mPendingEvent, dropReason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1205 | } |
Michael Wright | 3a98172 | 2015-06-10 15:26:13 +0100 | [diff] [blame] | 1206 | mLastDropReason = dropReason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1207 | |
| 1208 | releasePendingEventLocked(); |
Siarhei Vishniakou | 6950596 | 2023-12-28 12:07:04 -0800 | [diff] [blame] | 1209 | nextWakeupTime = LLONG_MIN; // force next poll to wake up immediately |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1210 | } |
| 1211 | } |
| 1212 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 1213 | bool InputDispatcher::isStaleEvent(nsecs_t currentTime, const EventEntry& entry) { |
Siarhei Vishniakou | a733311 | 2023-10-27 13:33:29 -0700 | [diff] [blame] | 1214 | return mPolicy.isStaleEvent(currentTime, entry.eventTime); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 1215 | } |
| 1216 | |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1217 | /** |
| 1218 | * Return true if the events preceding this incoming motion event should be dropped |
| 1219 | * Return false otherwise (the default behaviour) |
| 1220 | */ |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 1221 | bool InputDispatcher::shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) const { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1222 | const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN && |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 1223 | isFromSource(motionEntry.source, AINPUT_SOURCE_CLASS_POINTER); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1224 | |
| 1225 | // Optimize case where the current application is unresponsive and the user |
| 1226 | // decides to touch a window in a different application. |
| 1227 | // If the application takes too long to catch up then we drop all events preceding |
| 1228 | // the touch into the other window. |
| 1229 | if (isPointerDownEvent && mAwaitedFocusedApplication != nullptr) { |
Siarhei Vishniakou | 9306c38 | 2022-09-30 15:30:31 -0700 | [diff] [blame] | 1230 | const int32_t displayId = motionEntry.displayId; |
| 1231 | const auto [x, y] = resolveTouchedPosition(motionEntry); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1232 | const bool isStylus = isPointerFromStylus(motionEntry, /*pointerIndex=*/0); |
Siarhei Vishniakou | 9306c38 | 2022-09-30 15:30:31 -0700 | [diff] [blame] | 1233 | |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 1234 | sp<WindowInfoHandle> touchedWindowHandle = |
| 1235 | findTouchedWindowAtLocked(displayId, x, y, isStylus); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1236 | if (touchedWindowHandle != nullptr && |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1237 | touchedWindowHandle->getApplicationToken() != |
| 1238 | mAwaitedFocusedApplication->getApplicationToken()) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1239 | // User touched a different application than the one we are waiting on. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1240 | ALOGI("Pruning input queue because user touched a different application while waiting " |
| 1241 | "for %s", |
| 1242 | mAwaitedFocusedApplication->getName().c_str()); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1243 | return true; |
| 1244 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1245 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1246 | // Alternatively, maybe there's a spy window that could handle this event. |
| 1247 | const std::vector<sp<WindowInfoHandle>> touchedSpies = |
| 1248 | findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus); |
| 1249 | for (const auto& windowHandle : touchedSpies) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 1250 | const std::shared_ptr<Connection> connection = |
| 1251 | getConnectionLocked(windowHandle->getToken()); |
Siarhei Vishniakou | 34ed4d4 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1252 | if (connection != nullptr && connection->responsive) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1253 | // This spy window could take more input. Drop all events preceding this |
| 1254 | // 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] | 1255 | 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] | 1256 | "responsive spy window that may handle the event.", |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1257 | mAwaitedFocusedApplication->getName().c_str()); |
| 1258 | return true; |
| 1259 | } |
| 1260 | } |
| 1261 | } |
| 1262 | |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1263 | return false; |
| 1264 | } |
| 1265 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1266 | bool InputDispatcher::enqueueInboundEventLocked(std::unique_ptr<EventEntry> newEntry) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1267 | bool needWake = mInboundQueue.empty(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1268 | mInboundQueue.push_back(std::move(newEntry)); |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1269 | const EventEntry& entry = *(mInboundQueue.back()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1270 | traceInboundQueueLengthLocked(); |
| 1271 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1272 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1273 | case EventEntry::Type::KEY: { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1274 | LOG_ALWAYS_FATAL_IF((entry.policyFlags & POLICY_FLAG_TRUSTED) == 0, |
| 1275 | "Unexpected untrusted event."); |
Siarhei Vishniakou | e2404a1 | 2024-01-16 18:38:39 -0800 | [diff] [blame] | 1276 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1277 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 1278 | if (mTracer) { |
| 1279 | ensureEventTraced(keyEntry); |
| 1280 | } |
Siarhei Vishniakou | 6520a58 | 2023-10-27 21:53:45 -0700 | [diff] [blame] | 1281 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 1282 | // If a new up event comes in, and the pending event with same key code has been asked |
| 1283 | // to try again later because of the policy. We have to reset the intercept key wake up |
| 1284 | // time for it may have been handled in the policy and could be dropped. |
| 1285 | if (keyEntry.action == AKEY_EVENT_ACTION_UP && mPendingEvent && |
| 1286 | mPendingEvent->type == EventEntry::Type::KEY) { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1287 | const KeyEntry& pendingKey = static_cast<const KeyEntry&>(*mPendingEvent); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 1288 | if (pendingKey.keyCode == keyEntry.keyCode && |
| 1289 | pendingKey.interceptKeyResult == |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1290 | KeyEntry::InterceptKeyResult::TRY_AGAIN_LATER) { |
| 1291 | pendingKey.interceptKeyResult = KeyEntry::InterceptKeyResult::UNKNOWN; |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 1292 | pendingKey.interceptKeyWakeupTime = 0; |
| 1293 | needWake = true; |
| 1294 | } |
| 1295 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1296 | break; |
| 1297 | } |
| 1298 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1299 | case EventEntry::Type::MOTION: { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1300 | LOG_ALWAYS_FATAL_IF((entry.policyFlags & POLICY_FLAG_TRUSTED) == 0, |
| 1301 | "Unexpected untrusted event."); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 1302 | const auto& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1303 | if (mTracer) { |
| 1304 | ensureEventTraced(motionEntry); |
| 1305 | } |
| 1306 | if (shouldPruneInboundQueueLocked(motionEntry)) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1307 | mNextUnblockedEvent = mInboundQueue.back(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1308 | needWake = true; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1309 | } |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 1310 | |
| 1311 | const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN && |
| 1312 | isFromSource(motionEntry.source, AINPUT_SOURCE_CLASS_POINTER); |
| 1313 | if (isPointerDownEvent && mKeyIsWaitingForEventsTimeout) { |
| 1314 | // Prevent waiting too long for unprocessed events: if we have a pending key event, |
| 1315 | // and some other events have not yet been processed, the dispatcher will wait for |
| 1316 | // these events to be processed before dispatching the key event. This is because |
| 1317 | // the unprocessed events may cause the focus to change (for example, by launching a |
| 1318 | // new window or tapping a different window). To prevent waiting too long, we force |
| 1319 | // the key to be sent to the currently focused window when a new tap comes in. |
| 1320 | ALOGD("Received a new pointer down event, stop waiting for events to process and " |
| 1321 | "just send the pending key event to the currently focused window."); |
| 1322 | mKeyIsWaitingForEventsTimeout = now(); |
| 1323 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1324 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1325 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1326 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1327 | LOG_ALWAYS_FATAL("Focus events should be inserted using enqueueFocusEventLocked"); |
| 1328 | break; |
| 1329 | } |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1330 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1331 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1332 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1333 | case EventEntry::Type::SENSOR: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1334 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1335 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1336 | // nothing to do |
| 1337 | break; |
| 1338 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1339 | } |
| 1340 | |
| 1341 | return needWake; |
| 1342 | } |
| 1343 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1344 | void InputDispatcher::addRecentEventLocked(std::shared_ptr<const EventEntry> entry) { |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1345 | // Do not store sensor event in recent queue to avoid flooding the queue. |
| 1346 | if (entry->type != EventEntry::Type::SENSOR) { |
| 1347 | mRecentQueue.push_back(entry); |
| 1348 | } |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1349 | if (mRecentQueue.size() > RECENT_QUEUE_MAX_SIZE) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1350 | mRecentQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1351 | } |
| 1352 | } |
| 1353 | |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 1354 | sp<WindowInfoHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, float x, float y, |
| 1355 | bool isStylus, |
| 1356 | bool ignoreDragWindow) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1357 | // Traverse windows from front to back to find touched window. |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1358 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1359 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 1360 | if (ignoreDragWindow && haveSameToken(windowHandle, mDragState->dragWindow)) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1361 | continue; |
| 1362 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1363 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1364 | const WindowInfo& info = *windowHandle->getInfo(); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 1365 | if (!info.isSpy() && |
| 1366 | windowAcceptsTouchAt(info, displayId, x, y, isStylus, getTransformLocked(displayId))) { |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 1367 | return windowHandle; |
| 1368 | } |
| 1369 | } |
| 1370 | return nullptr; |
| 1371 | } |
| 1372 | |
| 1373 | std::vector<InputTarget> InputDispatcher::findOutsideTargetsLocked( |
Siarhei Vishniakou | 70f3d8c | 2023-09-19 15:36:52 -0700 | [diff] [blame] | 1374 | int32_t displayId, const sp<WindowInfoHandle>& touchedWindow, int32_t pointerId) const { |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 1375 | if (touchedWindow == nullptr) { |
| 1376 | return {}; |
| 1377 | } |
| 1378 | // Traverse windows from front to back until we encounter the touched window. |
| 1379 | std::vector<InputTarget> outsideTargets; |
| 1380 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
| 1381 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 1382 | if (windowHandle == touchedWindow) { |
| 1383 | // Stop iterating once we found a touched window. Any WATCH_OUTSIDE_TOUCH window |
| 1384 | // below the touched window will not get ACTION_OUTSIDE event. |
| 1385 | return outsideTargets; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1386 | } |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1387 | |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 1388 | const WindowInfo& info = *windowHandle->getInfo(); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1389 | if (info.inputConfig.test(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH)) { |
Siarhei Vishniakou | 70f3d8c | 2023-09-19 15:36:52 -0700 | [diff] [blame] | 1390 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
| 1391 | pointerIds.set(pointerId); |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 1392 | addPointerWindowTargetLocked(windowHandle, InputTarget::DispatchMode::OUTSIDE, |
| 1393 | ftl::Flags<InputTarget::Flags>(), pointerIds, |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 1394 | /*firstDownTimeInTarget=*/std::nullopt, outsideTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1395 | } |
| 1396 | } |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 1397 | return outsideTargets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1398 | } |
| 1399 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1400 | std::vector<sp<WindowInfoHandle>> InputDispatcher::findTouchedSpyWindowsAtLocked( |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 1401 | int32_t displayId, float x, float y, bool isStylus) const { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1402 | // Traverse windows from front to back and gather the touched spy windows. |
| 1403 | std::vector<sp<WindowInfoHandle>> spyWindows; |
| 1404 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
| 1405 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 1406 | const WindowInfo& info = *windowHandle->getInfo(); |
| 1407 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 1408 | if (!windowAcceptsTouchAt(info, displayId, x, y, isStylus, getTransformLocked(displayId))) { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1409 | continue; |
| 1410 | } |
| 1411 | if (!info.isSpy()) { |
| 1412 | // The first touched non-spy window was found, so return the spy windows touched so far. |
| 1413 | return spyWindows; |
| 1414 | } |
| 1415 | spyWindows.push_back(windowHandle); |
| 1416 | } |
| 1417 | return spyWindows; |
| 1418 | } |
| 1419 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1420 | void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1421 | const char* reason; |
| 1422 | switch (dropReason) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1423 | case DropReason::POLICY: |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 1424 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1425 | ALOGD("Dropped event because policy consumed it."); |
| 1426 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1427 | reason = "inbound event was dropped because the policy consumed it"; |
| 1428 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1429 | case DropReason::DISABLED: |
| 1430 | if (mLastDropReason != DropReason::DISABLED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1431 | ALOGI("Dropped event because input dispatch is disabled."); |
| 1432 | } |
| 1433 | reason = "inbound event was dropped because input dispatch is disabled"; |
| 1434 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1435 | case DropReason::BLOCKED: |
Siarhei Vishniakou | 99e407b | 2023-12-26 18:09:32 -0800 | [diff] [blame] | 1436 | LOG(INFO) << "Dropping because the current application is not responding and the user " |
| 1437 | "has started interacting with a different application: " |
| 1438 | << entry.getDescription(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1439 | reason = "inbound event was dropped because the current application is not responding " |
| 1440 | "and the user has started interacting with a different application"; |
| 1441 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1442 | case DropReason::STALE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1443 | ALOGI("Dropped event because it is stale."); |
| 1444 | reason = "inbound event was dropped because it is stale"; |
| 1445 | break; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1446 | case DropReason::NO_POINTER_CAPTURE: |
| 1447 | ALOGI("Dropped event because there is no window with Pointer Capture."); |
| 1448 | reason = "inbound event was dropped because there is no window with Pointer Capture"; |
| 1449 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1450 | case DropReason::NOT_DROPPED: { |
| 1451 | LOG_ALWAYS_FATAL("Should not be dropping a NOT_DROPPED event"); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1452 | return; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1453 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1454 | } |
| 1455 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1456 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1457 | case EventEntry::Type::KEY: { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1458 | CancelationOptions options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, reason); |
Hu Guo | 3cfa738 | 2023-11-15 09:50:04 +0000 | [diff] [blame] | 1459 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 1460 | options.displayId = keyEntry.displayId; |
| 1461 | options.deviceId = keyEntry.deviceId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1462 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1463 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1464 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1465 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1466 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1467 | if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1468 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, reason); |
Hu Guo | 3cfa738 | 2023-11-15 09:50:04 +0000 | [diff] [blame] | 1469 | options.displayId = motionEntry.displayId; |
| 1470 | options.deviceId = motionEntry.deviceId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1471 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1472 | } else { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1473 | CancelationOptions options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, |
| 1474 | reason); |
Hu Guo | 3cfa738 | 2023-11-15 09:50:04 +0000 | [diff] [blame] | 1475 | options.displayId = motionEntry.displayId; |
| 1476 | options.deviceId = motionEntry.deviceId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1477 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1478 | } |
| 1479 | break; |
| 1480 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1481 | case EventEntry::Type::SENSOR: { |
| 1482 | break; |
| 1483 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1484 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1485 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1486 | break; |
| 1487 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1488 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1489 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1490 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 1491 | case EventEntry::Type::DEVICE_RESET: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1492 | 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] | 1493 | break; |
| 1494 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1495 | } |
| 1496 | } |
| 1497 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1498 | bool InputDispatcher::haveCommandsLocked() const { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1499 | return !mCommandQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1500 | } |
| 1501 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1502 | bool InputDispatcher::runCommandsLockedInterruptable() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1503 | if (mCommandQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1504 | return false; |
| 1505 | } |
| 1506 | |
| 1507 | do { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1508 | auto command = std::move(mCommandQueue.front()); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1509 | mCommandQueue.pop_front(); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1510 | // Commands are run with the lock held, but may release and re-acquire the lock from within. |
| 1511 | command(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1512 | } while (!mCommandQueue.empty()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1513 | return true; |
| 1514 | } |
| 1515 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1516 | void InputDispatcher::postCommandLocked(Command&& command) { |
| 1517 | mCommandQueue.push_back(command); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1518 | } |
| 1519 | |
| 1520 | void InputDispatcher::drainInboundQueueLocked() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1521 | while (!mInboundQueue.empty()) { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1522 | std::shared_ptr<const EventEntry> entry = mInboundQueue.front(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1523 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1524 | releaseInboundEventLocked(entry); |
| 1525 | } |
| 1526 | traceInboundQueueLengthLocked(); |
| 1527 | } |
| 1528 | |
| 1529 | void InputDispatcher::releasePendingEventLocked() { |
| 1530 | if (mPendingEvent) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1531 | releaseInboundEventLocked(mPendingEvent); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1532 | mPendingEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1533 | } |
| 1534 | } |
| 1535 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1536 | void InputDispatcher::releaseInboundEventLocked(std::shared_ptr<const EventEntry> entry) { |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 1537 | const std::shared_ptr<InjectionState>& injectionState = entry->injectionState; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1538 | if (injectionState && injectionState->injectionResult == InputEventInjectionResult::PENDING) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1539 | if (DEBUG_DISPATCH_CYCLE) { |
| 1540 | ALOGD("Injected inbound event was dropped."); |
| 1541 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1542 | setInjectionResult(*entry, InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1543 | } |
| 1544 | if (entry == mNextUnblockedEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1545 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1546 | } |
| 1547 | addRecentEventLocked(entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1548 | } |
| 1549 | |
| 1550 | void InputDispatcher::resetKeyRepeatLocked() { |
| 1551 | if (mKeyRepeatState.lastKeyEntry) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1552 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1553 | } |
| 1554 | } |
| 1555 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1556 | std::shared_ptr<KeyEntry> InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1557 | std::shared_ptr<const KeyEntry> entry = mKeyRepeatState.lastKeyEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1558 | |
Michael Wright | 2e73295 | 2014-09-24 13:26:59 -0700 | [diff] [blame] | 1559 | uint32_t policyFlags = entry->policyFlags & |
| 1560 | (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1561 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1562 | std::shared_ptr<KeyEntry> newEntry = |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 1563 | std::make_unique<KeyEntry>(mIdGenerator.nextId(), /*injectionState=*/nullptr, |
| 1564 | currentTime, entry->deviceId, entry->source, |
| 1565 | entry->displayId, policyFlags, entry->action, entry->flags, |
| 1566 | entry->keyCode, entry->scanCode, entry->metaState, |
| 1567 | entry->repeatCount + 1, entry->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1568 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1569 | newEntry->syntheticRepeat = true; |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 1570 | if (mTracer) { |
| 1571 | newEntry->traceTracker = mTracer->traceInboundEvent(*newEntry); |
| 1572 | } |
| 1573 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1574 | mKeyRepeatState.lastKeyEntry = newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1575 | mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1576 | return newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1577 | } |
| 1578 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1579 | bool InputDispatcher::dispatchConfigurationChangedLocked(nsecs_t currentTime, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1580 | const ConfigurationChangedEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1581 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1582 | ALOGD("dispatchConfigurationChanged - eventTime=%" PRId64, entry.eventTime); |
| 1583 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1584 | |
| 1585 | // Reset key repeating in case a keyboard device was added or removed or something. |
| 1586 | resetKeyRepeatLocked(); |
| 1587 | |
| 1588 | // 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] | 1589 | auto command = [this, eventTime = entry.eventTime]() REQUIRES(mLock) { |
| 1590 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 1591 | mPolicy.notifyConfigurationChanged(eventTime); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1592 | }; |
| 1593 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1594 | return true; |
| 1595 | } |
| 1596 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1597 | bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime, |
| 1598 | const DeviceResetEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1599 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1600 | ALOGD("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry.eventTime, |
| 1601 | entry.deviceId); |
| 1602 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1603 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 1604 | // Reset key repeating in case a keyboard device was disabled or enabled. |
| 1605 | if (mKeyRepeatState.lastKeyEntry && mKeyRepeatState.lastKeyEntry->deviceId == entry.deviceId) { |
| 1606 | resetKeyRepeatLocked(); |
| 1607 | } |
| 1608 | |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1609 | CancelationOptions options(CancelationOptions::Mode::CANCEL_ALL_EVENTS, "device was reset"); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1610 | options.deviceId = entry.deviceId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1611 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 1612 | |
| 1613 | // Remove all active pointers from this device |
| 1614 | for (auto& [_, touchState] : mTouchStatesByDisplay) { |
| 1615 | touchState.removeAllPointersForDevice(entry.deviceId); |
| 1616 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1617 | return true; |
| 1618 | } |
| 1619 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1620 | void InputDispatcher::enqueueFocusEventLocked(const sp<IBinder>& windowToken, bool hasFocus, |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1621 | const std::string& reason) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1622 | if (mPendingEvent != nullptr) { |
| 1623 | // Move the pending event to the front of the queue. This will give the chance |
| 1624 | // for the pending event to get dispatched to the newly focused window |
| 1625 | mInboundQueue.push_front(mPendingEvent); |
| 1626 | mPendingEvent = nullptr; |
| 1627 | } |
| 1628 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1629 | std::unique_ptr<FocusEntry> focusEntry = |
| 1630 | std::make_unique<FocusEntry>(mIdGenerator.nextId(), now(), windowToken, hasFocus, |
| 1631 | reason); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1632 | |
| 1633 | // This event should go to the front of the queue, but behind all other focus events |
| 1634 | // Find the last focus event, and insert right after it |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1635 | auto it = std::find_if(mInboundQueue.rbegin(), mInboundQueue.rend(), |
| 1636 | [](const std::shared_ptr<const EventEntry>& event) { |
| 1637 | return event->type == EventEntry::Type::FOCUS; |
| 1638 | }); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1639 | |
| 1640 | // 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] | 1641 | mInboundQueue.insert(it.base(), std::move(focusEntry)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1642 | } |
| 1643 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1644 | void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime, |
| 1645 | std::shared_ptr<const FocusEntry> entry) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 1646 | std::shared_ptr<Connection> connection = getConnectionLocked(entry->connectionToken); |
| 1647 | if (connection == nullptr) { |
| 1648 | return; // Connection has gone away |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1649 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1650 | entry->dispatchInProgress = true; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1651 | std::string message = std::string("Focus ") + (entry->hasFocus ? "entering " : "leaving ") + |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 1652 | connection->getInputChannelName(); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 1653 | std::string reason = std::string("reason=").append(entry->reason); |
| 1654 | android_log_event_list(LOGTAG_INPUT_FOCUS) << message << reason << LOG_ID_EVENTS; |
Prabir Pradhan | c32a411 | 2024-01-23 23:20:37 +0000 | [diff] [blame] | 1655 | dispatchEventLocked(currentTime, entry, {{connection}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1656 | } |
| 1657 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1658 | void InputDispatcher::dispatchPointerCaptureChangedLocked( |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1659 | nsecs_t currentTime, const std::shared_ptr<const PointerCaptureChangedEntry>& entry, |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1660 | DropReason& dropReason) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1661 | dropReason = DropReason::NOT_DROPPED; |
| 1662 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1663 | const bool haveWindowWithPointerCapture = mWindowTokenWithPointerCapture != nullptr; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1664 | sp<IBinder> token; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1665 | |
| 1666 | if (entry->pointerCaptureRequest.enable) { |
| 1667 | // Enable Pointer Capture. |
| 1668 | if (haveWindowWithPointerCapture && |
| 1669 | (entry->pointerCaptureRequest == mCurrentPointerCaptureRequest)) { |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 1670 | // This can happen if pointer capture is disabled and re-enabled before we notify the |
| 1671 | // app of the state change, so there is no need to notify the app. |
| 1672 | ALOGI("Skipping dispatch of Pointer Capture being enabled: no state change."); |
| 1673 | return; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1674 | } |
| 1675 | if (!mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1676 | // This can happen if a window requests capture and immediately releases capture. |
| 1677 | ALOGW("No window requested Pointer Capture."); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1678 | dropReason = DropReason::NO_POINTER_CAPTURE; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1679 | return; |
| 1680 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1681 | if (entry->pointerCaptureRequest.seq != mCurrentPointerCaptureRequest.seq) { |
| 1682 | ALOGI("Skipping dispatch of Pointer Capture being enabled: sequence number mismatch."); |
| 1683 | return; |
| 1684 | } |
| 1685 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1686 | token = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1687 | LOG_ALWAYS_FATAL_IF(!token, "Cannot find focused window for Pointer Capture."); |
| 1688 | mWindowTokenWithPointerCapture = token; |
| 1689 | } else { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1690 | // Disable Pointer Capture. |
| 1691 | // We do not check if the sequence number matches for requests to disable Pointer Capture |
| 1692 | // for two reasons: |
| 1693 | // 1. Pointer Capture can be disabled by a focus change, which means we can get two entries |
| 1694 | // to disable capture with the same sequence number: one generated by |
| 1695 | // disablePointerCaptureForcedLocked() and another as an acknowledgement of Pointer |
| 1696 | // Capture being disabled in InputReader. |
| 1697 | // 2. We respect any request to disable Pointer Capture generated by InputReader, since the |
| 1698 | // actual Pointer Capture state that affects events being generated by input devices is |
| 1699 | // in InputReader. |
| 1700 | if (!haveWindowWithPointerCapture) { |
| 1701 | // Pointer capture was already forcefully disabled because of focus change. |
| 1702 | dropReason = DropReason::NOT_DROPPED; |
| 1703 | return; |
| 1704 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1705 | token = mWindowTokenWithPointerCapture; |
| 1706 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1707 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1708 | setPointerCaptureLocked(false); |
| 1709 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1710 | } |
| 1711 | |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 1712 | auto connection = getConnectionLocked(token); |
| 1713 | if (connection == nullptr) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1714 | // Window has gone away, clean up Pointer Capture state. |
| 1715 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1716 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1717 | setPointerCaptureLocked(false); |
| 1718 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1719 | return; |
| 1720 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1721 | entry->dispatchInProgress = true; |
Prabir Pradhan | c32a411 | 2024-01-23 23:20:37 +0000 | [diff] [blame] | 1722 | dispatchEventLocked(currentTime, entry, {{connection}}); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1723 | |
| 1724 | dropReason = DropReason::NOT_DROPPED; |
| 1725 | } |
| 1726 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1727 | void InputDispatcher::dispatchTouchModeChangeLocked( |
| 1728 | nsecs_t currentTime, const std::shared_ptr<const TouchModeEntry>& entry) { |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1729 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 1730 | getWindowHandlesLocked(entry->displayId); |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1731 | if (windowHandles.empty()) { |
| 1732 | return; |
| 1733 | } |
| 1734 | const std::vector<InputTarget> inputTargets = |
| 1735 | getInputTargetsFromWindowHandlesLocked(windowHandles); |
| 1736 | if (inputTargets.empty()) { |
| 1737 | return; |
| 1738 | } |
| 1739 | entry->dispatchInProgress = true; |
| 1740 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1741 | } |
| 1742 | |
| 1743 | std::vector<InputTarget> InputDispatcher::getInputTargetsFromWindowHandlesLocked( |
| 1744 | const std::vector<sp<WindowInfoHandle>>& windowHandles) const { |
| 1745 | std::vector<InputTarget> inputTargets; |
| 1746 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1747 | const sp<IBinder>& token = handle->getToken(); |
| 1748 | if (token == nullptr) { |
| 1749 | continue; |
| 1750 | } |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 1751 | std::shared_ptr<Connection> connection = getConnectionLocked(token); |
| 1752 | if (connection == nullptr) { |
| 1753 | continue; // Connection has gone away |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1754 | } |
Prabir Pradhan | c32a411 | 2024-01-23 23:20:37 +0000 | [diff] [blame] | 1755 | inputTargets.emplace_back(connection); |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1756 | } |
| 1757 | return inputTargets; |
| 1758 | } |
| 1759 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1760 | bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, std::shared_ptr<const KeyEntry> entry, |
Siarhei Vishniakou | 6950596 | 2023-12-28 12:07:04 -0800 | [diff] [blame] | 1761 | DropReason* dropReason, nsecs_t& nextWakeupTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1762 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1763 | if (!entry->dispatchInProgress) { |
| 1764 | if (entry->repeatCount == 0 && entry->action == AKEY_EVENT_ACTION_DOWN && |
| 1765 | (entry->policyFlags & POLICY_FLAG_TRUSTED) && |
| 1766 | (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { |
| 1767 | if (mKeyRepeatState.lastKeyEntry && |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1768 | mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode && |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1769 | // We have seen two identical key downs in a row which indicates that the device |
| 1770 | // driver is automatically generating key repeats itself. We take note of the |
| 1771 | // repeat here, but we disable our own next key repeat timer since it is clear that |
| 1772 | // we will not need to synthesize key repeats ourselves. |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1773 | mKeyRepeatState.lastKeyEntry->deviceId == entry->deviceId) { |
| 1774 | // Make sure we don't get key down from a different device. If a different |
| 1775 | // device Id has same key pressed down, the new device Id will replace the |
| 1776 | // current one to hold the key repeat with repeat count reset. |
| 1777 | // In the future when got a KEY_UP on the device id, drop it and do not |
| 1778 | // stop the key repeat on current device. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1779 | entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; |
| 1780 | resetKeyRepeatLocked(); |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 1781 | mKeyRepeatState.nextRepeatTime = LLONG_MAX; // don't generate repeats ourselves |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1782 | } else { |
| 1783 | // Not a repeat. Save key down state in case we do see a repeat later. |
| 1784 | resetKeyRepeatLocked(); |
| 1785 | mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; |
| 1786 | } |
| 1787 | mKeyRepeatState.lastKeyEntry = entry; |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1788 | } else if (entry->action == AKEY_EVENT_ACTION_UP && mKeyRepeatState.lastKeyEntry && |
| 1789 | mKeyRepeatState.lastKeyEntry->deviceId != entry->deviceId) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1790 | // 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] | 1791 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1792 | ALOGD("deviceId=%d got KEY_UP as stale", entry->deviceId); |
| 1793 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1794 | } else if (!entry->syntheticRepeat) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1795 | resetKeyRepeatLocked(); |
| 1796 | } |
| 1797 | |
| 1798 | if (entry->repeatCount == 1) { |
| 1799 | entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; |
| 1800 | } else { |
| 1801 | entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; |
| 1802 | } |
| 1803 | |
| 1804 | entry->dispatchInProgress = true; |
| 1805 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1806 | logOutboundKeyDetails("dispatchKey - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1807 | } |
| 1808 | |
| 1809 | // 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] | 1810 | if (entry->interceptKeyResult == KeyEntry::InterceptKeyResult::TRY_AGAIN_LATER) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1811 | if (currentTime < entry->interceptKeyWakeupTime) { |
Siarhei Vishniakou | 6950596 | 2023-12-28 12:07:04 -0800 | [diff] [blame] | 1812 | nextWakeupTime = std::min(nextWakeupTime, entry->interceptKeyWakeupTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1813 | return false; // wait until next wakeup |
| 1814 | } |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1815 | entry->interceptKeyResult = KeyEntry::InterceptKeyResult::UNKNOWN; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1816 | entry->interceptKeyWakeupTime = 0; |
| 1817 | } |
| 1818 | |
| 1819 | // Give the policy a chance to intercept the key. |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1820 | if (entry->interceptKeyResult == KeyEntry::InterceptKeyResult::UNKNOWN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1821 | if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1822 | sp<IBinder> focusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1823 | mFocusResolver.getFocusedWindowToken(getTargetDisplayId(*entry)); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1824 | |
| 1825 | auto command = [this, focusedWindowToken, entry]() REQUIRES(mLock) { |
| 1826 | doInterceptKeyBeforeDispatchingCommand(focusedWindowToken, *entry); |
| 1827 | }; |
| 1828 | postCommandLocked(std::move(command)); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1829 | // Poke user activity for keys not passed to user |
| 1830 | pokeUserActivityLocked(*entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1831 | return false; // wait for the command to run |
| 1832 | } else { |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1833 | entry->interceptKeyResult = KeyEntry::InterceptKeyResult::CONTINUE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1834 | } |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1835 | } else if (entry->interceptKeyResult == KeyEntry::InterceptKeyResult::SKIP) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1836 | if (*dropReason == DropReason::NOT_DROPPED) { |
| 1837 | *dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1838 | } |
| 1839 | } |
| 1840 | |
| 1841 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1842 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1843 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1844 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1845 | : InputEventInjectionResult::FAILED); |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1846 | mReporter->reportDroppedKey(entry->id); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1847 | // Poke user activity for undispatched keys |
| 1848 | pokeUserActivityLocked(*entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1849 | return true; |
| 1850 | } |
| 1851 | |
| 1852 | // Identify targets. |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1853 | InputEventInjectionResult injectionResult; |
| 1854 | sp<WindowInfoHandle> focusedWindow = |
| 1855 | findFocusedWindowTargetLocked(currentTime, *entry, nextWakeupTime, |
| 1856 | /*byref*/ injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1857 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1858 | return false; |
| 1859 | } |
| 1860 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1861 | setInjectionResult(*entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1862 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1863 | return true; |
| 1864 | } |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1865 | LOG_ALWAYS_FATAL_IF(focusedWindow == nullptr); |
| 1866 | |
| 1867 | std::vector<InputTarget> inputTargets; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 1868 | addWindowTargetLocked(focusedWindow, InputTarget::DispatchMode::AS_IS, |
| 1869 | InputTarget::Flags::FOREGROUND, getDownTime(*entry), inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1870 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1871 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1872 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1873 | |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 1874 | if (mTracer) { |
| 1875 | ensureEventTraced(*entry); |
| 1876 | for (const auto& target : inputTargets) { |
| 1877 | mTracer->dispatchToTargetHint(*entry->traceTracker, target); |
| 1878 | } |
| 1879 | } |
| 1880 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1881 | // Dispatch the key. |
| 1882 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1883 | return true; |
| 1884 | } |
| 1885 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1886 | void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1887 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1888 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 ", " |
| 1889 | "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, " |
| 1890 | "metaState=0x%x, repeatCount=%d, downTime=%" PRId64, |
| 1891 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, |
| 1892 | entry.policyFlags, entry.action, entry.flags, entry.keyCode, entry.scanCode, |
| 1893 | entry.metaState, entry.repeatCount, entry.downTime); |
| 1894 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1895 | } |
| 1896 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1897 | void InputDispatcher::dispatchSensorLocked(nsecs_t currentTime, |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1898 | const std::shared_ptr<const SensorEntry>& entry, |
Siarhei Vishniakou | 6950596 | 2023-12-28 12:07:04 -0800 | [diff] [blame] | 1899 | DropReason* dropReason, nsecs_t& nextWakeupTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1900 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1901 | ALOGD("notifySensorEvent eventTime=%" PRId64 ", hwTimestamp=%" PRId64 ", deviceId=%d, " |
| 1902 | "source=0x%x, sensorType=%s", |
| 1903 | entry->eventTime, entry->hwTimestamp, entry->deviceId, entry->source, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1904 | ftl::enum_string(entry->sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1905 | } |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1906 | auto command = [this, entry]() REQUIRES(mLock) { |
| 1907 | scoped_unlock unlock(mLock); |
| 1908 | |
| 1909 | if (entry->accuracyChanged) { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 1910 | mPolicy.notifySensorAccuracy(entry->deviceId, entry->sensorType, entry->accuracy); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1911 | } |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 1912 | mPolicy.notifySensorEvent(entry->deviceId, entry->sensorType, entry->accuracy, |
| 1913 | entry->hwTimestamp, entry->values); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1914 | }; |
| 1915 | postCommandLocked(std::move(command)); |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1916 | } |
| 1917 | |
| 1918 | bool InputDispatcher::flushSensor(int deviceId, InputDeviceSensorType sensorType) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1919 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1920 | ALOGD("flushSensor deviceId=%d, sensorType=%s", deviceId, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1921 | ftl::enum_string(sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1922 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1923 | { // acquire lock |
| 1924 | std::scoped_lock _l(mLock); |
| 1925 | |
| 1926 | for (auto it = mInboundQueue.begin(); it != mInboundQueue.end(); it++) { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1927 | std::shared_ptr<const EventEntry> entry = *it; |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1928 | if (entry->type == EventEntry::Type::SENSOR) { |
| 1929 | it = mInboundQueue.erase(it); |
| 1930 | releaseInboundEventLocked(entry); |
| 1931 | } |
| 1932 | } |
| 1933 | } |
| 1934 | return true; |
| 1935 | } |
| 1936 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1937 | bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, |
| 1938 | std::shared_ptr<const MotionEntry> entry, |
Siarhei Vishniakou | 6950596 | 2023-12-28 12:07:04 -0800 | [diff] [blame] | 1939 | DropReason* dropReason, nsecs_t& nextWakeupTime) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1940 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1941 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1942 | if (!entry->dispatchInProgress) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1943 | entry->dispatchInProgress = true; |
| 1944 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1945 | logOutboundMotionDetails("dispatchMotion - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1946 | } |
| 1947 | |
| 1948 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1949 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1950 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1951 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1952 | : InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1953 | return true; |
| 1954 | } |
| 1955 | |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 1956 | const bool isPointerEvent = isFromSource(entry->source, AINPUT_SOURCE_CLASS_POINTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1957 | |
| 1958 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1959 | std::vector<InputTarget> inputTargets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1960 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1961 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1962 | if (isPointerEvent) { |
| 1963 | // Pointer event. (eg. touchscreen) |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 1964 | |
| 1965 | if (mDragState && |
| 1966 | (entry->action & AMOTION_EVENT_ACTION_MASK) == AMOTION_EVENT_ACTION_POINTER_DOWN) { |
| 1967 | // If drag and drop ongoing and pointer down occur: pilfer drag window pointers |
| 1968 | pilferPointersLocked(mDragState->dragWindow->getToken()); |
| 1969 | } |
| 1970 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1971 | inputTargets = |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 1972 | findTouchedWindowTargetsLocked(currentTime, *entry, /*byref*/ injectionResult); |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 1973 | LOG_ALWAYS_FATAL_IF(injectionResult != InputEventInjectionResult::SUCCEEDED && |
| 1974 | !inputTargets.empty()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1975 | } else { |
| 1976 | // Non touch event. (eg. trackball) |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1977 | sp<WindowInfoHandle> focusedWindow = |
| 1978 | findFocusedWindowTargetLocked(currentTime, *entry, nextWakeupTime, injectionResult); |
| 1979 | if (injectionResult == InputEventInjectionResult::SUCCEEDED) { |
| 1980 | LOG_ALWAYS_FATAL_IF(focusedWindow == nullptr); |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 1981 | addWindowTargetLocked(focusedWindow, InputTarget::DispatchMode::AS_IS, |
| 1982 | InputTarget::Flags::FOREGROUND, getDownTime(*entry), |
| 1983 | inputTargets); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1984 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1985 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1986 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1987 | return false; |
| 1988 | } |
| 1989 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1990 | setInjectionResult(*entry, injectionResult); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1991 | if (injectionResult == InputEventInjectionResult::TARGET_MISMATCH) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1992 | return true; |
| 1993 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1994 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1995 | CancelationOptions::Mode mode( |
| 1996 | isPointerEvent ? CancelationOptions::Mode::CANCEL_POINTER_EVENTS |
| 1997 | : CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS); |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1998 | CancelationOptions options(mode, "input event injection failed"); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 1999 | options.displayId = entry->displayId; |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2000 | synthesizeCancelationEventsForMonitorsLocked(options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2001 | return true; |
| 2002 | } |
| 2003 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2004 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2005 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2006 | |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 2007 | if (mTracer) { |
| 2008 | ensureEventTraced(*entry); |
| 2009 | for (const auto& target : inputTargets) { |
| 2010 | mTracer->dispatchToTargetHint(*entry->traceTracker, target); |
| 2011 | } |
| 2012 | } |
| 2013 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2014 | // Dispatch the motion. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2015 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 2016 | return true; |
| 2017 | } |
| 2018 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2019 | void InputDispatcher::enqueueDragEventLocked(const sp<WindowInfoHandle>& windowHandle, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2020 | bool isExiting, const int32_t rawX, |
| 2021 | const int32_t rawY) { |
| 2022 | const vec2 xy = windowHandle->getInfo()->transform.transform(vec2(rawX, rawY)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2023 | std::unique_ptr<DragEntry> dragEntry = |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2024 | std::make_unique<DragEntry>(mIdGenerator.nextId(), now(), windowHandle->getToken(), |
| 2025 | isExiting, xy.x, xy.y); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2026 | |
| 2027 | enqueueInboundEventLocked(std::move(dragEntry)); |
| 2028 | } |
| 2029 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 2030 | void InputDispatcher::dispatchDragLocked(nsecs_t currentTime, |
| 2031 | std::shared_ptr<const DragEntry> entry) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 2032 | std::shared_ptr<Connection> connection = getConnectionLocked(entry->connectionToken); |
| 2033 | if (connection == nullptr) { |
| 2034 | return; // Connection has gone away |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2035 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2036 | entry->dispatchInProgress = true; |
Prabir Pradhan | c32a411 | 2024-01-23 23:20:37 +0000 | [diff] [blame] | 2037 | dispatchEventLocked(currentTime, entry, {{connection}}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2038 | } |
| 2039 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2040 | void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2041 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 2042 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=%s, displayId=%" PRId32 |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2043 | ", policyFlags=0x%x, " |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2044 | "action=%s, actionButton=0x%x, flags=0x%x, " |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2045 | "metaState=0x%x, buttonState=0x%x," |
| 2046 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64, |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 2047 | prefix, entry.eventTime, entry.deviceId, |
| 2048 | inputEventSourceToString(entry.source).c_str(), entry.displayId, entry.policyFlags, |
| 2049 | MotionEvent::actionToString(entry.action).c_str(), entry.actionButton, entry.flags, |
| 2050 | entry.metaState, entry.buttonState, entry.edgeFlags, entry.xPrecision, |
| 2051 | entry.yPrecision, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2052 | |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 2053 | for (uint32_t i = 0; i < entry.getPointerCount(); i++) { |
Siarhei Vishniakou | 09a8fe4 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2054 | ALOGD(" Pointer %d: id=%d, toolType=%s, " |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2055 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 2056 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 2057 | "orientation=%f", |
Siarhei Vishniakou | 09a8fe4 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2058 | i, entry.pointerProperties[i].id, |
| 2059 | ftl::enum_string(entry.pointerProperties[i].toolType).c_str(), |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2060 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 2061 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 2062 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 2063 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 2064 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 2065 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 2066 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 2067 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 2068 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| 2069 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2070 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2071 | } |
| 2072 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2073 | void InputDispatcher::dispatchEventLocked(nsecs_t currentTime, |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 2074 | std::shared_ptr<const EventEntry> eventEntry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2075 | const std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2076 | ATRACE_CALL(); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2077 | if (DEBUG_DISPATCH_CYCLE) { |
| 2078 | ALOGD("dispatchEventToCurrentInputTargets"); |
| 2079 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2080 | |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 2081 | processInteractionsLocked(*eventEntry, inputTargets); |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 2082 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2083 | ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true |
| 2084 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2085 | pokeUserActivityLocked(*eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2086 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2087 | for (const InputTarget& inputTarget : inputTargets) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 2088 | std::shared_ptr<Connection> connection = inputTarget.connection; |
| 2089 | prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2090 | } |
| 2091 | } |
| 2092 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 2093 | void InputDispatcher::cancelEventsForAnrLocked(const std::shared_ptr<Connection>& connection) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2094 | // We will not be breaking any connections here, even if the policy wants us to abort dispatch. |
| 2095 | // If the policy decides to close the app, we will get a channel removal event via |
| 2096 | // unregisterInputChannel, and will clean up the connection that way. We are already not |
| 2097 | // sending new pointers to the connection when it blocked, but focused events will continue to |
| 2098 | // pile up. |
| 2099 | ALOGW("Canceling events for %s because it is unresponsive", |
Siarhei Vishniakou | 10bfdc9 | 2024-02-01 11:12:57 -0800 | [diff] [blame] | 2100 | connection->getInputChannelName().c_str()); |
Prabir Pradhan | fc36472 | 2024-02-08 17:51:20 +0000 | [diff] [blame] | 2101 | if (connection->status != Connection::Status::NORMAL) { |
| 2102 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2103 | } |
Prabir Pradhan | fc36472 | 2024-02-08 17:51:20 +0000 | [diff] [blame] | 2104 | CancelationOptions options(CancelationOptions::Mode::CANCEL_ALL_EVENTS, |
| 2105 | "application not responding"); |
| 2106 | |
| 2107 | sp<WindowInfoHandle> windowHandle; |
| 2108 | if (!connection->monitor) { |
| 2109 | windowHandle = getWindowHandleLocked(connection->getToken()); |
| 2110 | if (windowHandle == nullptr) { |
| 2111 | // The window that is receiving this ANR was removed, so there is no need to generate |
| 2112 | // cancellations, because the cancellations would have already been generated when |
| 2113 | // the window was removed. |
| 2114 | return; |
| 2115 | } |
| 2116 | } |
| 2117 | synthesizeCancelationEventsForConnectionLocked(connection, options, windowHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2118 | } |
| 2119 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2120 | void InputDispatcher::resetNoFocusedWindowTimeoutLocked() { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2121 | if (DEBUG_FOCUS) { |
| 2122 | ALOGD("Resetting ANR timeouts."); |
| 2123 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2124 | |
| 2125 | // Reset input target wait timeout. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2126 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2127 | mAwaitedFocusedApplication.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2128 | } |
| 2129 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2130 | /** |
| 2131 | * Get the display id that the given event should go to. If this event specifies a valid display id, |
| 2132 | * then it should be dispatched to that display. Otherwise, the event goes to the focused display. |
| 2133 | * Focused display is the display that the user most recently interacted with. |
| 2134 | */ |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2135 | int32_t InputDispatcher::getTargetDisplayId(const EventEntry& entry) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2136 | int32_t displayId; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2137 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2138 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2139 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 2140 | displayId = keyEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2141 | break; |
| 2142 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2143 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2144 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 2145 | displayId = motionEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2146 | break; |
| 2147 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 2148 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 2149 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2150 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2151 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2152 | case EventEntry::Type::DEVICE_RESET: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2153 | case EventEntry::Type::SENSOR: |
| 2154 | case EventEntry::Type::DRAG: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2155 | 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] | 2156 | return ADISPLAY_ID_NONE; |
| 2157 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2158 | } |
| 2159 | return displayId == ADISPLAY_ID_NONE ? mFocusedDisplayId : displayId; |
| 2160 | } |
| 2161 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2162 | bool InputDispatcher::shouldWaitToSendKeyLocked(nsecs_t currentTime, |
| 2163 | const char* focusedWindowName) { |
| 2164 | if (mAnrTracker.empty()) { |
| 2165 | // already processed all events that we waited for |
| 2166 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 2167 | return false; |
| 2168 | } |
| 2169 | |
| 2170 | if (!mKeyIsWaitingForEventsTimeout.has_value()) { |
| 2171 | // Start the timer |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 2172 | // 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] | 2173 | mKeyIsWaitingForEventsTimeout = currentTime + |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 2174 | std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 2175 | mPolicy.getKeyWaitingForEventsTimeout()) |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 2176 | .count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2177 | return true; |
| 2178 | } |
| 2179 | |
| 2180 | // We still have pending events, and already started the timer |
| 2181 | if (currentTime < *mKeyIsWaitingForEventsTimeout) { |
| 2182 | return true; // Still waiting |
| 2183 | } |
| 2184 | |
| 2185 | // Waited too long, and some connection still hasn't processed all motions |
| 2186 | // Just send the key to the focused window |
| 2187 | ALOGW("Dispatching key to %s even though there are other unprocessed events", |
| 2188 | focusedWindowName); |
| 2189 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 2190 | return false; |
| 2191 | } |
| 2192 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2193 | sp<WindowInfoHandle> InputDispatcher::findFocusedWindowTargetLocked( |
Siarhei Vishniakou | 6950596 | 2023-12-28 12:07:04 -0800 | [diff] [blame] | 2194 | nsecs_t currentTime, const EventEntry& entry, nsecs_t& nextWakeupTime, |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2195 | InputEventInjectionResult& outInjectionResult) { |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2196 | outInjectionResult = InputEventInjectionResult::FAILED; // Default result |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2197 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2198 | int32_t displayId = getTargetDisplayId(entry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2199 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2200 | std::shared_ptr<InputApplicationHandle> focusedApplicationHandle = |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2201 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 2202 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2203 | // If there is no currently focused window and no focused application |
| 2204 | // then drop the event. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2205 | if (focusedWindowHandle == nullptr && focusedApplicationHandle == nullptr) { |
| 2206 | ALOGI("Dropping %s event because there is no focused window or focused application in " |
| 2207 | "display %" PRId32 ".", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2208 | ftl::enum_string(entry.type).c_str(), displayId); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2209 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2210 | } |
| 2211 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2212 | // Drop key events if requested by input feature |
| 2213 | if (focusedWindowHandle != nullptr && shouldDropInput(entry, focusedWindowHandle)) { |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2214 | return nullptr; |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2215 | } |
| 2216 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2217 | // Compatibility behavior: raise ANR if there is a focused application, but no focused window. |
| 2218 | // Only start counting when we have a focused event to dispatch. The ANR is canceled if we |
| 2219 | // start interacting with another application via touch (app switch). This code can be removed |
| 2220 | // if the "no focused window ANR" is moved to the policy. Input doesn't know whether |
| 2221 | // an app is expected to have a focused window. |
| 2222 | if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) { |
| 2223 | if (!mNoFocusedWindowTimeoutTime.has_value()) { |
| 2224 | // 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] | 2225 | std::chrono::nanoseconds timeout = focusedApplicationHandle->getDispatchingTimeout( |
| 2226 | DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
| 2227 | mNoFocusedWindowTimeoutTime = currentTime + timeout.count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2228 | mAwaitedFocusedApplication = focusedApplicationHandle; |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 2229 | mAwaitedApplicationDisplayId = displayId; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2230 | ALOGW("Waiting because no window has focus but %s may eventually add a " |
| 2231 | "window when it finishes starting up. Will wait for %" PRId64 "ms", |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 2232 | mAwaitedFocusedApplication->getName().c_str(), millis(timeout)); |
Siarhei Vishniakou | 6950596 | 2023-12-28 12:07:04 -0800 | [diff] [blame] | 2233 | nextWakeupTime = std::min(nextWakeupTime, *mNoFocusedWindowTimeoutTime); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2234 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 2235 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2236 | } else if (currentTime > *mNoFocusedWindowTimeoutTime) { |
| 2237 | // Already raised ANR. Drop the event |
| 2238 | ALOGE("Dropping %s event because there is no focused window", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2239 | ftl::enum_string(entry.type).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2240 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2241 | } else { |
| 2242 | // Still waiting for the focused window |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2243 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 2244 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2245 | } |
| 2246 | } |
| 2247 | |
| 2248 | // we have a valid, non-null focused window |
| 2249 | resetNoFocusedWindowTimeoutLocked(); |
| 2250 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2251 | // Verify targeted injection. |
| 2252 | if (const auto err = verifyTargetedInjection(focusedWindowHandle, entry); err) { |
| 2253 | ALOGW("Dropping injected event: %s", (*err).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2254 | outInjectionResult = InputEventInjectionResult::TARGET_MISMATCH; |
| 2255 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2256 | } |
| 2257 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2258 | if (focusedWindowHandle->getInfo()->inputConfig.test( |
| 2259 | WindowInfo::InputConfig::PAUSE_DISPATCHING)) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2260 | ALOGI("Waiting because %s is paused", focusedWindowHandle->getName().c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2261 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 2262 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2263 | } |
| 2264 | |
| 2265 | // If the event is a key event, then we must wait for all previous events to |
| 2266 | // complete before delivering it because previous events may have the |
| 2267 | // side-effect of transferring focus to a different window and we want to |
| 2268 | // ensure that the following keys are sent to the new window. |
| 2269 | // |
| 2270 | // Suppose the user touches a button in a window then immediately presses "A". |
| 2271 | // If the button causes a pop-up window to appear then we want to ensure that |
| 2272 | // the "A" key is delivered to the new pop-up window. This is because users |
| 2273 | // often anticipate pending UI changes when typing on a keyboard. |
| 2274 | // To obtain this behavior, we must serialize key events with respect to all |
| 2275 | // prior input events. |
| 2276 | if (entry.type == EventEntry::Type::KEY) { |
| 2277 | if (shouldWaitToSendKeyLocked(currentTime, focusedWindowHandle->getName().c_str())) { |
Siarhei Vishniakou | 6950596 | 2023-12-28 12:07:04 -0800 | [diff] [blame] | 2278 | nextWakeupTime = std::min(nextWakeupTime, *mKeyIsWaitingForEventsTimeout); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2279 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 2280 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2281 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2282 | } |
| 2283 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2284 | outInjectionResult = InputEventInjectionResult::SUCCEEDED; |
| 2285 | return focusedWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2286 | } |
| 2287 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2288 | /** |
| 2289 | * Given a list of monitors, remove the ones we cannot find a connection for, and the ones |
| 2290 | * that are currently unresponsive. |
| 2291 | */ |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2292 | std::vector<Monitor> InputDispatcher::selectResponsiveMonitorsLocked( |
| 2293 | const std::vector<Monitor>& monitors) const { |
| 2294 | std::vector<Monitor> responsiveMonitors; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2295 | std::copy_if(monitors.begin(), monitors.end(), std::back_inserter(responsiveMonitors), |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 2296 | [](const Monitor& monitor) REQUIRES(mLock) { |
| 2297 | std::shared_ptr<Connection> connection = monitor.connection; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2298 | if (!connection->responsive) { |
| 2299 | ALOGW("Unresponsive monitor %s will not get the new gesture", |
Siarhei Vishniakou | 10bfdc9 | 2024-02-01 11:12:57 -0800 | [diff] [blame] | 2300 | connection->getInputChannelName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2301 | return false; |
| 2302 | } |
| 2303 | return true; |
| 2304 | }); |
| 2305 | return responsiveMonitors; |
| 2306 | } |
| 2307 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2308 | std::vector<InputTarget> InputDispatcher::findTouchedWindowTargetsLocked( |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2309 | nsecs_t currentTime, const MotionEntry& entry, |
Siarhei Vishniakou | 4fe5739 | 2022-10-25 13:44:30 -0700 | [diff] [blame] | 2310 | InputEventInjectionResult& outInjectionResult) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2311 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2312 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2313 | std::vector<InputTarget> targets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2314 | // For security reasons, we defer updating the touch state until we are sure that |
| 2315 | // event injection will be allowed. |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2316 | const int32_t displayId = entry.displayId; |
| 2317 | const int32_t action = entry.action; |
Siarhei Vishniakou | bf88052 | 2023-05-01 11:03:22 -0700 | [diff] [blame] | 2318 | const int32_t maskedAction = MotionEvent::getActionMasked(action); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2319 | |
| 2320 | // 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] | 2321 | outInjectionResult = InputEventInjectionResult::PENDING; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2322 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2323 | // Copy current touch state into tempTouchState. |
| 2324 | // This state will be used to update mTouchStatesByDisplay at the end of this function. |
| 2325 | // 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] | 2326 | const TouchState* oldState = nullptr; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2327 | TouchState tempTouchState; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2328 | if (const auto it = mTouchStatesByDisplay.find(displayId); it != mTouchStatesByDisplay.end()) { |
| 2329 | oldState = &(it->second); |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 2330 | tempTouchState = *oldState; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2331 | } |
| 2332 | |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 2333 | bool isSplit = shouldSplitTouch(tempTouchState, entry); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2334 | |
| 2335 | const bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE || |
| 2336 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2337 | maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2338 | // A DOWN could be generated from POINTER_DOWN if the initial pointers did not land into any |
| 2339 | // touchable windows. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2340 | const bool wasDown = oldState != nullptr && oldState->isDown(entry.deviceId); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2341 | const bool isDown = (maskedAction == AMOTION_EVENT_ACTION_DOWN) || |
| 2342 | (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN && !wasDown); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2343 | const bool newGesture = isDown || maskedAction == AMOTION_EVENT_ACTION_SCROLL || |
| 2344 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2345 | maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE; |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 2346 | const bool isFromMouse = isFromSource(entry.source, AINPUT_SOURCE_MOUSE); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2347 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2348 | if (newGesture) { |
| 2349 | isSplit = false; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2350 | } |
| 2351 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2352 | if (isDown && tempTouchState.hasHoveringPointers(entry.deviceId)) { |
| 2353 | // Compatibility behaviour: ACTION_DOWN causes HOVER_EXIT to get generated. |
| 2354 | tempTouchState.clearHoveringPointers(entry.deviceId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2355 | } |
| 2356 | |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2357 | if (isHoverAction) { |
Siarhei Vishniakou | 7be50c9 | 2023-11-17 17:09:08 -0800 | [diff] [blame] | 2358 | if (wasDown) { |
| 2359 | // Started hovering, but the device is already down: reject the hover event |
| 2360 | LOG(ERROR) << "Got hover event " << entry.getDescription() |
| 2361 | << " but the device is already down " << oldState->dump(); |
| 2362 | outInjectionResult = InputEventInjectionResult::FAILED; |
| 2363 | return {}; |
| 2364 | } |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2365 | // For hover actions, we will treat 'tempTouchState' as a new state, so let's erase |
| 2366 | // all of the existing hovering pointers and recompute. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2367 | tempTouchState.clearHoveringPointers(entry.deviceId); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2368 | } |
| 2369 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2370 | if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { |
| 2371 | /* 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] | 2372 | const auto [x, y] = resolveTouchedPosition(entry); |
Siarhei Vishniakou | 5b9766d | 2023-07-18 14:06:29 -0700 | [diff] [blame] | 2373 | const int32_t pointerIndex = MotionEvent::getActionIndex(action); |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2374 | const PointerProperties& pointer = entry.pointerProperties[pointerIndex]; |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2375 | // Outside targets should be added upon first dispatched DOWN event. That means, this should |
| 2376 | // 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] | 2377 | const bool isStylus = isPointerFromStylus(entry, pointerIndex); |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 2378 | sp<WindowInfoHandle> newTouchedWindowHandle = |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2379 | findTouchedWindowAtLocked(displayId, x, y, isStylus); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2380 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2381 | if (isDown) { |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2382 | targets += findOutsideTargetsLocked(displayId, newTouchedWindowHandle, pointer.id); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2383 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2384 | // Handle the case where we did not find a window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2385 | if (newTouchedWindowHandle == nullptr) { |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 2386 | 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] | 2387 | // 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] | 2388 | newTouchedWindowHandle = tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2389 | } |
| 2390 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2391 | // Verify targeted injection. |
| 2392 | if (const auto err = verifyTargetedInjection(newTouchedWindowHandle, entry); err) { |
| 2393 | ALOGW("Dropping injected touch event: %s", (*err).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2394 | outInjectionResult = os::InputEventInjectionResult::TARGET_MISMATCH; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2395 | newTouchedWindowHandle = nullptr; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2396 | return {}; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2397 | } |
| 2398 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2399 | // Figure out whether splitting will be allowed for this window. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2400 | if (newTouchedWindowHandle != nullptr) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2401 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
| 2402 | // New window supports splitting, but we should never split mouse events. |
| 2403 | isSplit = !isFromMouse; |
| 2404 | } else if (isSplit) { |
| 2405 | // New window does not support splitting but we have already split events. |
| 2406 | // Ignore the new window. |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame] | 2407 | LOG(INFO) << "Skipping " << newTouchedWindowHandle->getName() |
| 2408 | << " because it doesn't support split touch"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2409 | newTouchedWindowHandle = nullptr; |
| 2410 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2411 | } else { |
| 2412 | // 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] | 2413 | // be delivered to a new window which supports split touch. Pointers from a mouse device |
| 2414 | // should never be split. |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 2415 | isSplit = !isFromMouse; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2416 | } |
| 2417 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2418 | std::vector<sp<WindowInfoHandle>> newTouchedWindows = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2419 | findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2420 | if (newTouchedWindowHandle != nullptr) { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2421 | // Process the foreground window first so that it is the first to receive the event. |
| 2422 | newTouchedWindows.insert(newTouchedWindows.begin(), newTouchedWindowHandle); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2423 | } |
| 2424 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2425 | if (newTouchedWindows.empty()) { |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 2426 | ALOGI("Dropping event because there is no touchable window at (%.1f, %.1f) on display " |
| 2427 | "%d.", |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2428 | x, y, displayId); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2429 | outInjectionResult = InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2430 | return {}; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2431 | } |
| 2432 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2433 | for (const sp<WindowInfoHandle>& windowHandle : newTouchedWindows) { |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 2434 | if (!canWindowReceiveMotionLocked(windowHandle, entry)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2435 | continue; |
| 2436 | } |
| 2437 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2438 | if (isHoverAction) { |
Siarhei Vishniakou | b681c20 | 2023-05-01 11:22:33 -0700 | [diff] [blame] | 2439 | // The "windowHandle" is the target of this hovering pointer. |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2440 | tempTouchState.addHoveringPointerToWindow(windowHandle, entry.deviceId, pointer); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2441 | } |
| 2442 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2443 | // Set target flags. |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2444 | ftl::Flags<InputTarget::Flags> targetFlags; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2445 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2446 | if (canReceiveForegroundTouches(*windowHandle->getInfo())) { |
| 2447 | // 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] | 2448 | targetFlags |= InputTarget::Flags::FOREGROUND; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2449 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2450 | |
| 2451 | if (isSplit) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2452 | targetFlags |= InputTarget::Flags::SPLIT; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2453 | } |
| 2454 | if (isWindowObscuredAtPointLocked(windowHandle, x, y)) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2455 | targetFlags |= InputTarget::Flags::WINDOW_IS_OBSCURED; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2456 | } else if (isWindowObscuredLocked(windowHandle)) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2457 | targetFlags |= InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2458 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2459 | |
| 2460 | // Update the temporary touch state. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2461 | |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2462 | if (!isHoverAction) { |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2463 | const bool isDownOrPointerDown = maskedAction == AMOTION_EVENT_ACTION_DOWN || |
| 2464 | maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2465 | tempTouchState.addOrUpdateWindow(windowHandle, InputTarget::DispatchMode::AS_IS, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2466 | targetFlags, entry.deviceId, {pointer}, |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2467 | isDownOrPointerDown |
| 2468 | ? std::make_optional(entry.eventTime) |
| 2469 | : std::nullopt); |
| 2470 | // If this is the pointer going down and the touched window has a wallpaper |
| 2471 | // then also add the touched wallpaper windows so they are locked in for the |
| 2472 | // duration of the touch gesture. We do not collect wallpapers during HOVER_MOVE or |
| 2473 | // SCROLL because the wallpaper engine only supports touch events. We would need to |
| 2474 | // add a mechanism similar to View.onGenericMotionEvent to enable wallpapers to |
| 2475 | // handle these events. |
| 2476 | if (isDownOrPointerDown && targetFlags.test(InputTarget::Flags::FOREGROUND) && |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2477 | windowHandle->getInfo()->inputConfig.test( |
| 2478 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER)) { |
| 2479 | sp<WindowInfoHandle> wallpaper = findWallpaperWindowBelow(windowHandle); |
| 2480 | if (wallpaper != nullptr) { |
| 2481 | ftl::Flags<InputTarget::Flags> wallpaperFlags = |
| 2482 | InputTarget::Flags::WINDOW_IS_OBSCURED | |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2483 | InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED; |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2484 | if (isSplit) { |
| 2485 | wallpaperFlags |= InputTarget::Flags::SPLIT; |
| 2486 | } |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2487 | tempTouchState.addOrUpdateWindow(wallpaper, |
| 2488 | InputTarget::DispatchMode::AS_IS, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2489 | wallpaperFlags, entry.deviceId, {pointer}, |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2490 | entry.eventTime); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2491 | } |
| 2492 | } |
| 2493 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2494 | } |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 2495 | |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 2496 | // If a window is already pilfering some pointers, give it this new pointer as well and |
| 2497 | // make it pilfering. This will prevent other non-spy windows from getting this pointer, |
| 2498 | // which is a specific behaviour that we want. |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 2499 | for (TouchedWindow& touchedWindow : tempTouchState.windows) { |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2500 | if (touchedWindow.hasTouchingPointer(entry.deviceId, pointer.id) && |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 2501 | touchedWindow.hasPilferingPointers(entry.deviceId)) { |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 2502 | // This window is already pilfering some pointers, and this new pointer is also |
| 2503 | // going to it. Therefore, take over this pointer and don't give it to anyone |
| 2504 | // else. |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2505 | touchedWindow.addPilferingPointer(entry.deviceId, pointer.id); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 2506 | } |
| 2507 | } |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 2508 | |
| 2509 | // Restrict all pilfered pointers to the pilfering windows. |
| 2510 | tempTouchState.cancelPointersForNonPilferingWindows(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2511 | } else { |
| 2512 | /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ |
| 2513 | |
| 2514 | // If the pointer is not currently down, then ignore the event. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2515 | if (!tempTouchState.isDown(entry.deviceId) && |
| 2516 | maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT) { |
Siarhei Vishniakou | 31dd155 | 2023-10-30 18:46:10 -0700 | [diff] [blame] | 2517 | if (DEBUG_DROPPED_EVENTS_VERBOSE) { |
| 2518 | LOG(INFO) << "Dropping event because the pointer for device " << entry.deviceId |
| 2519 | << " is not down or we previously dropped the pointer down event in " |
| 2520 | << "display " << displayId << ": " << entry.getDescription(); |
| 2521 | } |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2522 | outInjectionResult = InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2523 | return {}; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2524 | } |
| 2525 | |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2526 | // If the pointer is not currently hovering, then ignore the event. |
| 2527 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 2528 | const int32_t pointerId = entry.pointerProperties[0].id; |
| 2529 | if (oldState == nullptr || |
| 2530 | oldState->getWindowsWithHoveringPointer(entry.deviceId, pointerId).empty()) { |
| 2531 | LOG(INFO) << "Dropping event because the hovering pointer is not in any windows in " |
| 2532 | "display " |
| 2533 | << displayId << ": " << entry.getDescription(); |
| 2534 | outInjectionResult = InputEventInjectionResult::FAILED; |
| 2535 | return {}; |
| 2536 | } |
| 2537 | tempTouchState.removeHoveringPointer(entry.deviceId, pointerId); |
| 2538 | } |
| 2539 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2540 | addDragEventLocked(entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2541 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2542 | // Check whether touches should slip outside of the current foreground window. |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 2543 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.getPointerCount() == 1 && |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2544 | tempTouchState.isSlippery()) { |
Siarhei Vishniakou | 9306c38 | 2022-09-30 15:30:31 -0700 | [diff] [blame] | 2545 | const auto [x, y] = resolveTouchedPosition(entry); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2546 | const bool isStylus = isPointerFromStylus(entry, /*pointerIndex=*/0); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2547 | sp<WindowInfoHandle> oldTouchedWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2548 | tempTouchState.getFirstForegroundWindowHandle(); |
Siarhei Vishniakou | 0f6558d | 2023-04-21 12:05:13 -0700 | [diff] [blame] | 2549 | LOG_ALWAYS_FATAL_IF(oldTouchedWindowHandle == nullptr); |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 2550 | sp<WindowInfoHandle> newTouchedWindowHandle = |
| 2551 | findTouchedWindowAtLocked(displayId, x, y, isStylus); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2552 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2553 | // Verify targeted injection. |
| 2554 | if (const auto err = verifyTargetedInjection(newTouchedWindowHandle, entry); err) { |
| 2555 | ALOGW("Dropping injected event: %s", (*err).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2556 | outInjectionResult = os::InputEventInjectionResult::TARGET_MISMATCH; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2557 | return {}; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2558 | } |
| 2559 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2560 | // Drop touch events if requested by input feature |
| 2561 | if (newTouchedWindowHandle != nullptr && |
| 2562 | shouldDropInput(entry, newTouchedWindowHandle)) { |
| 2563 | newTouchedWindowHandle = nullptr; |
| 2564 | } |
| 2565 | |
Siarhei Vishniakou | afa08cc | 2023-05-08 22:35:50 -0700 | [diff] [blame] | 2566 | if (newTouchedWindowHandle != nullptr && |
| 2567 | !haveSameToken(oldTouchedWindowHandle, newTouchedWindowHandle)) { |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2568 | 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] | 2569 | oldTouchedWindowHandle->getName().c_str(), |
| 2570 | newTouchedWindowHandle->getName().c_str(), displayId); |
| 2571 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2572 | // Make a slippery exit from the old window. |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 2573 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2574 | const PointerProperties& pointer = entry.pointerProperties[0]; |
| 2575 | pointerIds.set(pointer.id); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2576 | |
| 2577 | const TouchedWindow& touchedWindow = |
| 2578 | tempTouchState.getTouchedWindow(oldTouchedWindowHandle); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2579 | addPointerWindowTargetLocked(oldTouchedWindowHandle, |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2580 | InputTarget::DispatchMode::SLIPPERY_EXIT, |
| 2581 | ftl::Flags<InputTarget::Flags>(), pointerIds, |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2582 | touchedWindow.getDownTimeInTarget(entry.deviceId), |
| 2583 | targets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2584 | |
| 2585 | // Make a slippery entrance into the new window. |
| 2586 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
Prabir Pradhan | 713bb3e | 2021-12-20 02:07:40 -0800 | [diff] [blame] | 2587 | isSplit = !isFromMouse; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2588 | } |
| 2589 | |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2590 | ftl::Flags<InputTarget::Flags> targetFlags; |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2591 | if (canReceiveForegroundTouches(*newTouchedWindowHandle->getInfo())) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2592 | targetFlags |= InputTarget::Flags::FOREGROUND; |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2593 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2594 | if (isSplit) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2595 | targetFlags |= InputTarget::Flags::SPLIT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2596 | } |
| 2597 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2598 | targetFlags |= InputTarget::Flags::WINDOW_IS_OBSCURED; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2599 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2600 | targetFlags |= InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2601 | } |
| 2602 | |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2603 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, |
| 2604 | InputTarget::DispatchMode::SLIPPERY_ENTER, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2605 | targetFlags, entry.deviceId, {pointer}, |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2606 | entry.eventTime); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2607 | |
| 2608 | // Check if the wallpaper window should deliver the corresponding event. |
| 2609 | slipWallpaperTouch(targetFlags, oldTouchedWindowHandle, newTouchedWindowHandle, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2610 | tempTouchState, entry.deviceId, pointer, targets); |
| 2611 | tempTouchState.removeTouchingPointerFromWindow(entry.deviceId, pointer.id, |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 2612 | oldTouchedWindowHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2613 | } |
| 2614 | } |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 2615 | |
| 2616 | // Update the pointerIds for non-splittable when it received pointer down. |
| 2617 | if (!isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN) { |
| 2618 | // If no split, we suppose all touched windows should receive pointer down. |
Siarhei Vishniakou | 5b9766d | 2023-07-18 14:06:29 -0700 | [diff] [blame] | 2619 | const int32_t pointerIndex = MotionEvent::getActionIndex(action); |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2620 | std::vector<PointerProperties> touchingPointers{entry.pointerProperties[pointerIndex]}; |
| 2621 | for (TouchedWindow& touchedWindow : tempTouchState.windows) { |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 2622 | // Ignore drag window for it should just track one pointer. |
| 2623 | if (mDragState && mDragState->dragWindow == touchedWindow.windowHandle) { |
| 2624 | continue; |
| 2625 | } |
Siarhei Vishniakou | 5b9766d | 2023-07-18 14:06:29 -0700 | [diff] [blame] | 2626 | touchedWindow.addTouchingPointers(entry.deviceId, touchingPointers); |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 2627 | } |
| 2628 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2629 | } |
| 2630 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2631 | // Update dispatching for hover enter and exit. |
Sam Dubey | f886dec | 2023-01-27 13:28:19 +0000 | [diff] [blame] | 2632 | { |
| 2633 | std::vector<TouchedWindow> hoveringWindows = |
| 2634 | getHoveringWindowsLocked(oldState, tempTouchState, entry); |
| 2635 | for (const TouchedWindow& touchedWindow : hoveringWindows) { |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 2636 | std::optional<InputTarget> target = |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2637 | createInputTargetLocked(touchedWindow.windowHandle, touchedWindow.dispatchMode, |
| 2638 | touchedWindow.targetFlags, |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 2639 | touchedWindow.getDownTimeInTarget(entry.deviceId)); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 2640 | if (!target) { |
| 2641 | continue; |
| 2642 | } |
| 2643 | // Hardcode to single hovering pointer for now. |
| 2644 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
| 2645 | pointerIds.set(entry.pointerProperties[0].id); |
| 2646 | target->addPointers(pointerIds, touchedWindow.windowHandle->getInfo()->transform); |
| 2647 | targets.push_back(*target); |
Sam Dubey | f886dec | 2023-01-27 13:28:19 +0000 | [diff] [blame] | 2648 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2649 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2650 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2651 | // Ensure that all touched windows are valid for injection. |
| 2652 | if (entry.injectionState != nullptr) { |
| 2653 | std::string errs; |
| 2654 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2655 | const auto err = verifyTargetedInjection(touchedWindow.windowHandle, entry); |
| 2656 | if (err) errs += "\n - " + *err; |
| 2657 | } |
| 2658 | if (!errs.empty()) { |
| 2659 | 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] | 2660 | "%s:%s", |
| 2661 | entry.injectionState->targetUid->toString().c_str(), errs.c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2662 | outInjectionResult = InputEventInjectionResult::TARGET_MISMATCH; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2663 | return {}; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2664 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2665 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2666 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2667 | // Check whether windows listening for outside touches are owned by the same UID. If the owner |
| 2668 | // 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] | 2669 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2670 | sp<WindowInfoHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2671 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2672 | if (foregroundWindowHandle) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 2673 | const auto foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2674 | for (InputTarget& target : targets) { |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2675 | if (target.dispatchMode == InputTarget::DispatchMode::OUTSIDE) { |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2676 | sp<WindowInfoHandle> targetWindow = |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 2677 | getWindowHandleLocked(target.connection->getToken()); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2678 | if (targetWindow->getInfo()->ownerUid != foregroundWindowUid) { |
| 2679 | target.flags |= InputTarget::Flags::ZERO_COORDS; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2680 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2681 | } |
| 2682 | } |
| 2683 | } |
| 2684 | } |
| 2685 | |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 2686 | // If this is a touchpad navigation gesture, it needs to only be sent to trusted targets, as we |
| 2687 | // only want the system UI to handle these gestures. |
| 2688 | const bool isTouchpadNavGesture = isFromSource(entry.source, AINPUT_SOURCE_MOUSE) && |
| 2689 | entry.classification == MotionClassification::MULTI_FINGER_SWIPE; |
| 2690 | if (isTouchpadNavGesture) { |
| 2691 | filterUntrustedTargets(/* byref */ tempTouchState, /* byref */ targets); |
| 2692 | } |
| 2693 | |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2694 | // Output targets from the touch state. |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2695 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2696 | std::vector<PointerProperties> touchingPointers = |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2697 | touchedWindow.getTouchingPointers(entry.deviceId); |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2698 | if (touchingPointers.empty()) { |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2699 | continue; |
| 2700 | } |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2701 | addPointerWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.dispatchMode, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2702 | touchedWindow.targetFlags, getPointerIds(touchingPointers), |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2703 | touchedWindow.getDownTimeInTarget(entry.deviceId), targets); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2704 | } |
Sam Dubey | 39d37cf | 2022-12-07 18:05:35 +0000 | [diff] [blame] | 2705 | |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 2706 | // During targeted injection, only allow owned targets to receive events |
| 2707 | std::erase_if(targets, [&](const InputTarget& target) { |
| 2708 | LOG_ALWAYS_FATAL_IF(target.windowHandle == nullptr); |
| 2709 | const auto err = verifyTargetedInjection(target.windowHandle, entry); |
| 2710 | if (err) { |
| 2711 | LOG(WARNING) << "Dropping injected event from " << target.windowHandle->getName() |
| 2712 | << ": " << (*err); |
| 2713 | return true; |
| 2714 | } |
| 2715 | return false; |
| 2716 | }); |
| 2717 | |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2718 | if (targets.empty()) { |
| 2719 | LOG(INFO) << "Dropping event because no targets were found: " << entry.getDescription(); |
| 2720 | outInjectionResult = InputEventInjectionResult::FAILED; |
| 2721 | return {}; |
| 2722 | } |
| 2723 | |
| 2724 | // If we only have windows getting ACTION_OUTSIDE, then drop the event, because there is no |
| 2725 | // window that is actually receiving the entire gesture. |
| 2726 | if (std::all_of(targets.begin(), targets.end(), [](const InputTarget& target) { |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2727 | return target.dispatchMode == InputTarget::DispatchMode::OUTSIDE; |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2728 | })) { |
| 2729 | LOG(INFO) << "Dropping event because all windows would just receive ACTION_OUTSIDE: " |
| 2730 | << entry.getDescription(); |
| 2731 | outInjectionResult = InputEventInjectionResult::FAILED; |
| 2732 | return {}; |
| 2733 | } |
| 2734 | |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2735 | outInjectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2736 | |
Prabir Pradhan | 502a725 | 2023-12-01 16:11:24 +0000 | [diff] [blame] | 2737 | // Now that we have generated all of the input targets for this event, reset the dispatch |
| 2738 | // mode for all touched window to AS_IS. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2739 | for (TouchedWindow& touchedWindow : tempTouchState.windows) { |
Prabir Pradhan | 502a725 | 2023-12-01 16:11:24 +0000 | [diff] [blame] | 2740 | touchedWindow.dispatchMode = InputTarget::DispatchMode::AS_IS; |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2741 | } |
| 2742 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2743 | // Update final pieces of touch state if the injector had permission. |
Siarhei Vishniakou | 7be50c9 | 2023-11-17 17:09:08 -0800 | [diff] [blame] | 2744 | if (maskedAction == AMOTION_EVENT_ACTION_UP) { |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2745 | // Pointer went up. |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 2746 | tempTouchState.removeTouchingPointer(entry.deviceId, entry.pointerProperties[0].id); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2747 | } else if (maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2748 | // All pointers up or canceled. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2749 | tempTouchState.removeAllPointersForDevice(entry.deviceId); |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2750 | } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 2751 | // One pointer went up. |
Siarhei Vishniakou | 5b9766d | 2023-07-18 14:06:29 -0700 | [diff] [blame] | 2752 | const int32_t pointerIndex = MotionEvent::getActionIndex(action); |
| 2753 | const uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
| 2754 | tempTouchState.removeTouchingPointer(entry.deviceId, pointerId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2755 | } |
| 2756 | |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2757 | // Save changes unless the action was scroll in which case the temporary touch |
| 2758 | // state was only valid for this one action. |
| 2759 | if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) { |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 2760 | if (displayId >= 0) { |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2761 | tempTouchState.clearWindowsWithoutPointers(); |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2762 | mTouchStatesByDisplay[displayId] = tempTouchState; |
| 2763 | } else { |
| 2764 | mTouchStatesByDisplay.erase(displayId); |
| 2765 | } |
| 2766 | } |
| 2767 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 2768 | if (tempTouchState.windows.empty()) { |
| 2769 | mTouchStatesByDisplay.erase(displayId); |
| 2770 | } |
| 2771 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2772 | return targets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2773 | } |
| 2774 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2775 | void InputDispatcher::finishDragAndDrop(int32_t displayId, float x, float y) { |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2776 | // Prevent stylus interceptor windows from affecting drag and drop behavior for now, until we |
| 2777 | // have an explicit reason to support it. |
| 2778 | constexpr bool isStylus = false; |
| 2779 | |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 2780 | sp<WindowInfoHandle> dropWindow = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2781 | findTouchedWindowAtLocked(displayId, x, y, isStylus, /*ignoreDragWindow=*/true); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2782 | if (dropWindow) { |
| 2783 | vec2 local = dropWindow->getInfo()->transform.transform(x, y); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2784 | sendDropWindowCommandLocked(dropWindow->getToken(), local.x, local.y); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 2785 | } else { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2786 | ALOGW("No window found when drop."); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2787 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2788 | } |
| 2789 | mDragState.reset(); |
| 2790 | } |
| 2791 | |
| 2792 | void InputDispatcher::addDragEventLocked(const MotionEntry& entry) { |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 2793 | if (!mDragState || mDragState->dragWindow->getInfo()->displayId != entry.displayId) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2794 | return; |
| 2795 | } |
| 2796 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2797 | if (!mDragState->isStartDrag) { |
| 2798 | mDragState->isStartDrag = true; |
| 2799 | mDragState->isStylusButtonDownAtStart = |
| 2800 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2801 | } |
| 2802 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2803 | // Find the pointer index by id. |
| 2804 | int32_t pointerIndex = 0; |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 2805 | for (; static_cast<uint32_t>(pointerIndex) < entry.getPointerCount(); pointerIndex++) { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2806 | const PointerProperties& pointerProperties = entry.pointerProperties[pointerIndex]; |
| 2807 | if (pointerProperties.id == mDragState->pointerId) { |
| 2808 | break; |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2809 | } |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2810 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2811 | |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 2812 | if (uint32_t(pointerIndex) == entry.getPointerCount()) { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2813 | 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] | 2814 | } |
| 2815 | |
| 2816 | const int32_t maskedAction = entry.action & AMOTION_EVENT_ACTION_MASK; |
| 2817 | const int32_t x = entry.pointerCoords[pointerIndex].getX(); |
| 2818 | const int32_t y = entry.pointerCoords[pointerIndex].getY(); |
| 2819 | |
| 2820 | switch (maskedAction) { |
| 2821 | case AMOTION_EVENT_ACTION_MOVE: { |
| 2822 | // Handle the special case : stylus button no longer pressed. |
| 2823 | bool isStylusButtonDown = |
| 2824 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2825 | if (mDragState->isStylusButtonDownAtStart && !isStylusButtonDown) { |
| 2826 | finishDragAndDrop(entry.displayId, x, y); |
| 2827 | return; |
| 2828 | } |
| 2829 | |
| 2830 | // Prevent stylus interceptor windows from affecting drag and drop behavior for now, |
| 2831 | // until we have an explicit reason to support it. |
| 2832 | constexpr bool isStylus = false; |
| 2833 | |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 2834 | sp<WindowInfoHandle> hoverWindowHandle = |
| 2835 | findTouchedWindowAtLocked(entry.displayId, x, y, isStylus, |
| 2836 | /*ignoreDragWindow=*/true); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2837 | // enqueue drag exit if needed. |
| 2838 | if (hoverWindowHandle != mDragState->dragHoverWindowHandle && |
| 2839 | !haveSameToken(hoverWindowHandle, mDragState->dragHoverWindowHandle)) { |
| 2840 | if (mDragState->dragHoverWindowHandle != nullptr) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2841 | enqueueDragEventLocked(mDragState->dragHoverWindowHandle, /*isExiting=*/true, x, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2842 | y); |
| 2843 | } |
| 2844 | mDragState->dragHoverWindowHandle = hoverWindowHandle; |
| 2845 | } |
| 2846 | // enqueue drag location if needed. |
| 2847 | if (hoverWindowHandle != nullptr) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2848 | enqueueDragEventLocked(hoverWindowHandle, /*isExiting=*/false, x, y); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2849 | } |
| 2850 | break; |
| 2851 | } |
| 2852 | |
| 2853 | case AMOTION_EVENT_ACTION_POINTER_UP: |
Siarhei Vishniakou | 5b9766d | 2023-07-18 14:06:29 -0700 | [diff] [blame] | 2854 | if (MotionEvent::getActionIndex(entry.action) != pointerIndex) { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2855 | break; |
| 2856 | } |
| 2857 | // The drag pointer is up. |
| 2858 | [[fallthrough]]; |
| 2859 | case AMOTION_EVENT_ACTION_UP: |
| 2860 | finishDragAndDrop(entry.displayId, x, y); |
| 2861 | break; |
| 2862 | case AMOTION_EVENT_ACTION_CANCEL: { |
| 2863 | ALOGD("Receiving cancel when drag and drop."); |
| 2864 | sendDropWindowCommandLocked(nullptr, 0, 0); |
| 2865 | mDragState.reset(); |
| 2866 | break; |
| 2867 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2868 | } |
| 2869 | } |
| 2870 | |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2871 | std::optional<InputTarget> InputDispatcher::createInputTargetLocked( |
| 2872 | const sp<android::gui::WindowInfoHandle>& windowHandle, |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2873 | InputTarget::DispatchMode dispatchMode, ftl::Flags<InputTarget::Flags> targetFlags, |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2874 | std::optional<nsecs_t> firstDownTimeInTarget) const { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 2875 | std::shared_ptr<Connection> connection = getConnectionLocked(windowHandle->getToken()); |
| 2876 | if (connection == nullptr) { |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2877 | ALOGW("Not creating InputTarget for %s, no input channel", windowHandle->getName().c_str()); |
| 2878 | return {}; |
| 2879 | } |
Prabir Pradhan | c32a411 | 2024-01-23 23:20:37 +0000 | [diff] [blame] | 2880 | InputTarget inputTarget{connection}; |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 2881 | inputTarget.windowHandle = windowHandle; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2882 | inputTarget.dispatchMode = dispatchMode; |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2883 | inputTarget.flags = targetFlags; |
| 2884 | inputTarget.globalScaleFactor = windowHandle->getInfo()->globalScaleFactor; |
| 2885 | inputTarget.firstDownTimeInTarget = firstDownTimeInTarget; |
| 2886 | const auto& displayInfoIt = mDisplayInfos.find(windowHandle->getInfo()->displayId); |
| 2887 | if (displayInfoIt != mDisplayInfos.end()) { |
| 2888 | inputTarget.displayTransform = displayInfoIt->second.transform; |
| 2889 | } else { |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 2890 | // DisplayInfo not found for this window on display windowHandle->getInfo()->displayId. |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2891 | // TODO(b/198444055): Make this an error message after 'setInputWindows' API is removed. |
| 2892 | } |
| 2893 | return inputTarget; |
| 2894 | } |
| 2895 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2896 | void InputDispatcher::addWindowTargetLocked(const sp<WindowInfoHandle>& windowHandle, |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2897 | InputTarget::DispatchMode dispatchMode, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2898 | ftl::Flags<InputTarget::Flags> targetFlags, |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2899 | std::optional<nsecs_t> firstDownTimeInTarget, |
Siarhei Vishniakou | f75cddb | 2022-10-25 10:42:16 -0700 | [diff] [blame] | 2900 | std::vector<InputTarget>& inputTargets) const { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2901 | std::vector<InputTarget>::iterator it = |
| 2902 | std::find_if(inputTargets.begin(), inputTargets.end(), |
| 2903 | [&windowHandle](const InputTarget& inputTarget) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 2904 | return inputTarget.connection->getToken() == windowHandle->getToken(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2905 | }); |
Chavi Weingarten | 97b8eec | 2020-01-09 18:09:08 +0000 | [diff] [blame] | 2906 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2907 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2908 | |
| 2909 | if (it == inputTargets.end()) { |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2910 | std::optional<InputTarget> target = |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2911 | createInputTargetLocked(windowHandle, dispatchMode, targetFlags, |
| 2912 | firstDownTimeInTarget); |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2913 | if (!target) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2914 | return; |
| 2915 | } |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2916 | inputTargets.push_back(*target); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2917 | it = inputTargets.end() - 1; |
| 2918 | } |
| 2919 | |
Siarhei Vishniakou | 23d73fb | 2023-10-29 13:27:46 -0700 | [diff] [blame] | 2920 | if (it->flags != targetFlags) { |
| 2921 | LOG(ERROR) << "Flags don't match! targetFlags=" << targetFlags.string() << ", it=" << *it; |
| 2922 | } |
| 2923 | if (it->globalScaleFactor != windowInfo->globalScaleFactor) { |
| 2924 | LOG(ERROR) << "Mismatch! it->globalScaleFactor=" << it->globalScaleFactor |
| 2925 | << ", windowInfo->globalScaleFactor=" << windowInfo->globalScaleFactor; |
| 2926 | } |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2927 | } |
| 2928 | |
| 2929 | void InputDispatcher::addPointerWindowTargetLocked( |
| 2930 | const sp<android::gui::WindowInfoHandle>& windowHandle, |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2931 | InputTarget::DispatchMode dispatchMode, ftl::Flags<InputTarget::Flags> targetFlags, |
| 2932 | std::bitset<MAX_POINTER_ID + 1> pointerIds, std::optional<nsecs_t> firstDownTimeInTarget, |
| 2933 | std::vector<InputTarget>& inputTargets) const REQUIRES(mLock) { |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2934 | if (pointerIds.none()) { |
| 2935 | for (const auto& target : inputTargets) { |
| 2936 | LOG(INFO) << "Target: " << target; |
| 2937 | } |
| 2938 | LOG(FATAL) << "No pointers specified for " << windowHandle->getName(); |
| 2939 | return; |
| 2940 | } |
| 2941 | std::vector<InputTarget>::iterator it = |
| 2942 | std::find_if(inputTargets.begin(), inputTargets.end(), |
| 2943 | [&windowHandle](const InputTarget& inputTarget) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 2944 | return inputTarget.connection->getToken() == windowHandle->getToken(); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2945 | }); |
| 2946 | |
| 2947 | // This is a hack, because the actual entry could potentially be an ACTION_DOWN event that |
| 2948 | // causes a HOVER_EXIT to be generated. That means that the same entry of ACTION_DOWN would |
| 2949 | // have DISPATCH_AS_HOVER_EXIT and DISPATCH_AS_IS. And therefore, we have to create separate |
| 2950 | // input targets for hovering pointers and for touching pointers. |
| 2951 | // If we picked an existing input target above, but it's for HOVER_EXIT - let's use a new |
| 2952 | // target instead. |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2953 | if (it != inputTargets.end() && it->dispatchMode == InputTarget::DispatchMode::HOVER_EXIT) { |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2954 | // Force the code below to create a new input target |
| 2955 | it = inputTargets.end(); |
| 2956 | } |
| 2957 | |
| 2958 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 2959 | |
| 2960 | if (it == inputTargets.end()) { |
| 2961 | std::optional<InputTarget> target = |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2962 | createInputTargetLocked(windowHandle, dispatchMode, targetFlags, |
| 2963 | firstDownTimeInTarget); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2964 | if (!target) { |
| 2965 | return; |
| 2966 | } |
| 2967 | inputTargets.push_back(*target); |
| 2968 | it = inputTargets.end() - 1; |
| 2969 | } |
| 2970 | |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2971 | if (it->dispatchMode != dispatchMode) { |
| 2972 | LOG(ERROR) << __func__ << ": DispatchMode doesn't match! ignoring new mode=" |
| 2973 | << ftl::enum_string(dispatchMode) << ", it=" << *it; |
| 2974 | } |
Siarhei Vishniakou | 4bd0b7c | 2023-10-27 00:51:14 -0700 | [diff] [blame] | 2975 | if (it->flags != targetFlags) { |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2976 | LOG(ERROR) << __func__ << ": Flags don't match! new targetFlags=" << targetFlags.string() |
| 2977 | << ", it=" << *it; |
Siarhei Vishniakou | 4bd0b7c | 2023-10-27 00:51:14 -0700 | [diff] [blame] | 2978 | } |
Siarhei Vishniakou | 23d73fb | 2023-10-29 13:27:46 -0700 | [diff] [blame] | 2979 | if (it->globalScaleFactor != windowInfo->globalScaleFactor) { |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2980 | LOG(ERROR) << __func__ << ": Mismatch! it->globalScaleFactor=" << it->globalScaleFactor |
Siarhei Vishniakou | 23d73fb | 2023-10-29 13:27:46 -0700 | [diff] [blame] | 2981 | << ", windowInfo->globalScaleFactor=" << windowInfo->globalScaleFactor; |
| 2982 | } |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2983 | |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2984 | it->addPointers(pointerIds, windowInfo->transform); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2985 | } |
| 2986 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2987 | void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2988 | int32_t displayId) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2989 | auto monitorsIt = mGlobalMonitorsByDisplay.find(displayId); |
| 2990 | if (monitorsIt == mGlobalMonitorsByDisplay.end()) return; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2991 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2992 | for (const Monitor& monitor : selectResponsiveMonitorsLocked(monitorsIt->second)) { |
Prabir Pradhan | c32a411 | 2024-01-23 23:20:37 +0000 | [diff] [blame] | 2993 | InputTarget target{monitor.connection}; |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2994 | // target.firstDownTimeInTarget is not set for global monitors. It is only required in split |
| 2995 | // touch and global monitoring works as intended even without setting firstDownTimeInTarget |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2996 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
| 2997 | target.displayTransform = it->second.transform; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2998 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2999 | target.setDefaultPointerTransform(target.displayTransform); |
| 3000 | inputTargets.push_back(target); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3001 | } |
| 3002 | } |
| 3003 | |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3004 | /** |
| 3005 | * Indicate whether one window handle should be considered as obscuring |
| 3006 | * another window handle. We only check a few preconditions. Actually |
| 3007 | * checking the bounds is left to the caller. |
| 3008 | */ |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3009 | static bool canBeObscuredBy(const sp<WindowInfoHandle>& windowHandle, |
| 3010 | const sp<WindowInfoHandle>& otherHandle) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3011 | // Compare by token so cloned layers aren't counted |
| 3012 | if (haveSameToken(windowHandle, otherHandle)) { |
| 3013 | return false; |
| 3014 | } |
| 3015 | auto info = windowHandle->getInfo(); |
| 3016 | auto otherInfo = otherHandle->getInfo(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 3017 | if (otherInfo->inputConfig.test(WindowInfo::InputConfig::NOT_VISIBLE)) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3018 | return false; |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 3019 | } else if (otherInfo->alpha == 0 && |
| 3020 | otherInfo->inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE)) { |
Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame] | 3021 | // Those act as if they were invisible, so we don't need to flag them. |
| 3022 | // We do want to potentially flag touchable windows even if they have 0 |
| 3023 | // opacity, since they can consume touches and alter the effects of the |
| 3024 | // user interaction (eg. apps that rely on |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3025 | // Flags::WINDOW_IS_PARTIALLY_OBSCURED should still be told about those |
Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame] | 3026 | // windows), hence we also check for FLAG_NOT_TOUCHABLE. |
| 3027 | return false; |
Bernardo Rufino | 8007daf | 2020-09-22 09:40:01 +0000 | [diff] [blame] | 3028 | } else if (info->ownerUid == otherInfo->ownerUid) { |
| 3029 | // If ownerUid is the same we don't generate occlusion events as there |
| 3030 | // is no security boundary within an uid. |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3031 | return false; |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 3032 | } else if (otherInfo->inputConfig.test(gui::WindowInfo::InputConfig::TRUSTED_OVERLAY)) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3033 | return false; |
| 3034 | } else if (otherInfo->displayId != info->displayId) { |
| 3035 | return false; |
| 3036 | } |
| 3037 | return true; |
| 3038 | } |
| 3039 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3040 | /** |
| 3041 | * Returns touch occlusion information in the form of TouchOcclusionInfo. To check if the touch is |
| 3042 | * untrusted, one should check: |
| 3043 | * |
| 3044 | * 1. If result.hasBlockingOcclusion is true. |
| 3045 | * If it's, it means the touch should be blocked due to a window with occlusion mode of |
| 3046 | * BLOCK_UNTRUSTED. |
| 3047 | * |
| 3048 | * 2. If result.obscuringOpacity > mMaximumObscuringOpacityForTouch. |
| 3049 | * If it is (and 1 is false), then the touch should be blocked because a stack of windows |
| 3050 | * (possibly only one) with occlusion mode of USE_OPACITY from one UID resulted in a composed |
| 3051 | * obscuring opacity above the threshold. Note that if there was no window of occlusion mode |
| 3052 | * USE_OPACITY, result.obscuringOpacity would've been 0 and since |
| 3053 | * mMaximumObscuringOpacityForTouch >= 0, the condition above would never be true. |
| 3054 | * |
| 3055 | * If neither of those is true, then it means the touch can be allowed. |
| 3056 | */ |
| 3057 | InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3058 | const sp<WindowInfoHandle>& windowHandle, int32_t x, int32_t y) const { |
| 3059 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 3060 | int32_t displayId = windowInfo->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3061 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3062 | TouchOcclusionInfo info; |
| 3063 | info.hasBlockingOcclusion = false; |
| 3064 | info.obscuringOpacity = 0; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3065 | info.obscuringUid = gui::Uid::INVALID; |
| 3066 | std::map<gui::Uid, float> opacityByUid; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3067 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3068 | if (windowHandle == otherHandle) { |
| 3069 | break; // All future windows are below us. Exit early. |
| 3070 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3071 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 3072 | if (canBeObscuredBy(windowHandle, otherHandle) && otherInfo->frameContainsPoint(x, y) && |
| 3073 | !haveSameApplicationToken(windowInfo, otherInfo)) { |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 3074 | if (DEBUG_TOUCH_OCCLUSION) { |
| 3075 | info.debugInfo.push_back( |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 3076 | dumpWindowForTouchOcclusion(otherInfo, /*isTouchedWindow=*/false)); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 3077 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3078 | // canBeObscuredBy() has returned true above, which means this window is untrusted, so |
| 3079 | // we perform the checks below to see if the touch can be propagated or not based on the |
| 3080 | // window's touch occlusion mode |
| 3081 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::BLOCK_UNTRUSTED) { |
| 3082 | info.hasBlockingOcclusion = true; |
| 3083 | info.obscuringUid = otherInfo->ownerUid; |
| 3084 | info.obscuringPackage = otherInfo->packageName; |
| 3085 | break; |
| 3086 | } |
| 3087 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::USE_OPACITY) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3088 | const auto uid = otherInfo->ownerUid; |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3089 | float opacity = |
| 3090 | (opacityByUid.find(uid) == opacityByUid.end()) ? 0 : opacityByUid[uid]; |
| 3091 | // Given windows A and B: |
| 3092 | // opacity(A, B) = 1 - [1 - opacity(A)] * [1 - opacity(B)] |
| 3093 | opacity = 1 - (1 - opacity) * (1 - otherInfo->alpha); |
| 3094 | opacityByUid[uid] = opacity; |
| 3095 | if (opacity > info.obscuringOpacity) { |
| 3096 | info.obscuringOpacity = opacity; |
| 3097 | info.obscuringUid = uid; |
| 3098 | info.obscuringPackage = otherInfo->packageName; |
| 3099 | } |
| 3100 | } |
| 3101 | } |
| 3102 | } |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 3103 | if (DEBUG_TOUCH_OCCLUSION) { |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 3104 | info.debugInfo.push_back(dumpWindowForTouchOcclusion(windowInfo, /*isTouchedWindow=*/true)); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 3105 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3106 | return info; |
| 3107 | } |
| 3108 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3109 | std::string InputDispatcher::dumpWindowForTouchOcclusion(const WindowInfo* info, |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 3110 | bool isTouchedWindow) const { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3111 | return StringPrintf(INDENT2 "* %spackage=%s/%s, id=%" PRId32 ", mode=%s, alpha=%.2f, " |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 3112 | "frame=[%" PRId32 ",%" PRId32 "][%" PRId32 ",%" PRId32 |
| 3113 | "], touchableRegion=%s, window={%s}, inputConfig={%s}, " |
| 3114 | "hasToken=%s, applicationInfo.name=%s, applicationInfo.token=%s\n", |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 3115 | isTouchedWindow ? "[TOUCHED] " : "", info->packageName.c_str(), |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3116 | info->ownerUid.toString().c_str(), info->id, |
Chavi Weingarten | 7f01919 | 2023-08-08 20:39:01 +0000 | [diff] [blame] | 3117 | toString(info->touchOcclusionMode).c_str(), info->alpha, info->frame.left, |
| 3118 | info->frame.top, info->frame.right, info->frame.bottom, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3119 | dumpRegion(info->touchableRegion).c_str(), info->name.c_str(), |
| 3120 | info->inputConfig.string().c_str(), toString(info->token != nullptr), |
| 3121 | info->applicationInfo.name.c_str(), |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 3122 | binderToString(info->applicationInfo.token).c_str()); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 3123 | } |
| 3124 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3125 | bool InputDispatcher::isTouchTrustedLocked(const TouchOcclusionInfo& occlusionInfo) const { |
| 3126 | if (occlusionInfo.hasBlockingOcclusion) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3127 | ALOGW("Untrusted touch due to occlusion by %s/%s", occlusionInfo.obscuringPackage.c_str(), |
| 3128 | occlusionInfo.obscuringUid.toString().c_str()); |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3129 | return false; |
| 3130 | } |
| 3131 | if (occlusionInfo.obscuringOpacity > mMaximumObscuringOpacityForTouch) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3132 | ALOGW("Untrusted touch due to occlusion by %s/%s (obscuring opacity = " |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3133 | "%.2f, maximum allowed = %.2f)", |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3134 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid.toString().c_str(), |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3135 | occlusionInfo.obscuringOpacity, mMaximumObscuringOpacityForTouch); |
| 3136 | return false; |
| 3137 | } |
| 3138 | return true; |
| 3139 | } |
| 3140 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3141 | bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3142 | int32_t x, int32_t y) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3143 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3144 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 3145 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3146 | if (windowHandle == otherHandle) { |
| 3147 | break; // All future windows are below us. Exit early. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3148 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3149 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3150 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 3151 | otherInfo->frameContainsPoint(x, y)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3152 | return true; |
| 3153 | } |
| 3154 | } |
| 3155 | return false; |
| 3156 | } |
| 3157 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3158 | bool InputDispatcher::isWindowObscuredLocked(const sp<WindowInfoHandle>& windowHandle) const { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 3159 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3160 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 3161 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 3162 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3163 | if (windowHandle == otherHandle) { |
| 3164 | break; // All future windows are below us. Exit early. |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 3165 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3166 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3167 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 3168 | otherInfo->overlaps(windowInfo)) { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 3169 | return true; |
| 3170 | } |
| 3171 | } |
| 3172 | return false; |
| 3173 | } |
| 3174 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 3175 | std::string InputDispatcher::getApplicationWindowLabel( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3176 | const InputApplicationHandle* applicationHandle, const sp<WindowInfoHandle>& windowHandle) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3177 | if (applicationHandle != nullptr) { |
| 3178 | if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 3179 | return applicationHandle->getName() + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3180 | } else { |
| 3181 | return applicationHandle->getName(); |
| 3182 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3183 | } else if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 3184 | return windowHandle->getInfo()->applicationInfo.name + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3185 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3186 | return "<unknown application or window>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3187 | } |
| 3188 | } |
| 3189 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3190 | void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3191 | if (!isUserActivityEvent(eventEntry)) { |
| 3192 | // 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] | 3193 | return; |
| 3194 | } |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 3195 | |
| 3196 | const int32_t eventType = getUserActivityEventType(eventEntry); |
| 3197 | if (input_flags::rate_limit_user_activity_poke_in_dispatcher()) { |
| 3198 | // Note that we're directly getting the time diff between the current event and the previous |
| 3199 | // event. This is assuming that the first user event always happens at a timestamp that is |
| 3200 | // greater than `mMinTimeBetweenUserActivityPokes` (otherwise, the first user event will |
| 3201 | // wrongly be dropped). In real life, `mMinTimeBetweenUserActivityPokes` is a much smaller |
| 3202 | // value than the potential first user activity event time, so this is ok. |
| 3203 | std::chrono::nanoseconds timeSinceLastEvent = |
| 3204 | std::chrono::nanoseconds(eventEntry.eventTime - mLastUserActivityTimes[eventType]); |
| 3205 | if (timeSinceLastEvent < mMinTimeBetweenUserActivityPokes) { |
| 3206 | return; |
| 3207 | } |
| 3208 | } |
| 3209 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3210 | int32_t displayId = getTargetDisplayId(eventEntry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3211 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 3212 | const WindowInfo* windowDisablingUserActivityInfo = nullptr; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3213 | if (focusedWindowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3214 | const WindowInfo* info = focusedWindowHandle->getInfo(); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 3215 | if (info->inputConfig.test(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY)) { |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 3216 | windowDisablingUserActivityInfo = info; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3217 | } |
| 3218 | } |
| 3219 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3220 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3221 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3222 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 3223 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3224 | return; |
| 3225 | } |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 3226 | if (windowDisablingUserActivityInfo != nullptr) { |
| 3227 | if (DEBUG_DISPATCH_CYCLE) { |
| 3228 | ALOGD("Not poking user activity: disabled by window '%s'.", |
| 3229 | windowDisablingUserActivityInfo->name.c_str()); |
| 3230 | } |
| 3231 | return; |
| 3232 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3233 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3234 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3235 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3236 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 3237 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3238 | return; |
| 3239 | } |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 3240 | // If the key code is unknown, we don't consider it user activity |
| 3241 | if (keyEntry.keyCode == AKEYCODE_UNKNOWN) { |
| 3242 | return; |
| 3243 | } |
| 3244 | // Don't inhibit events that were intercepted or are not passed to |
| 3245 | // the apps, like system shortcuts |
| 3246 | if (windowDisablingUserActivityInfo != nullptr && |
| 3247 | keyEntry.interceptKeyResult != KeyEntry::InterceptKeyResult::SKIP && |
| 3248 | keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER) { |
| 3249 | if (DEBUG_DISPATCH_CYCLE) { |
| 3250 | ALOGD("Not poking user activity: disabled by window '%s'.", |
| 3251 | windowDisablingUserActivityInfo->name.c_str()); |
| 3252 | } |
| 3253 | return; |
| 3254 | } |
| 3255 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3256 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3257 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3258 | default: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3259 | LOG_ALWAYS_FATAL("%s events are not user activity", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3260 | ftl::enum_string(eventEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3261 | break; |
| 3262 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3263 | } |
| 3264 | |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 3265 | mLastUserActivityTimes[eventType] = eventEntry.eventTime; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3266 | auto command = [this, eventTime = eventEntry.eventTime, eventType, displayId]() |
| 3267 | REQUIRES(mLock) { |
| 3268 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 3269 | mPolicy.pokeUserActivity(eventTime, eventType, displayId); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3270 | }; |
| 3271 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3272 | } |
| 3273 | |
| 3274 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3275 | const std::shared_ptr<Connection>& connection, |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 3276 | std::shared_ptr<const EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3277 | const InputTarget& inputTarget) { |
Prabir Pradhan | 2dac8b8 | 2023-09-06 01:11:51 +0000 | [diff] [blame] | 3278 | ATRACE_NAME_IF(ATRACE_ENABLED(), |
| 3279 | StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
| 3280 | connection->getInputChannelName().c_str(), eventEntry->id)); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3281 | if (DEBUG_DISPATCH_CYCLE) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3282 | ALOGD("channel '%s' ~ prepareDispatchCycle - flags=%s, " |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 3283 | "globalScaleFactor=%f, pointerIds=%s %s", |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3284 | connection->getInputChannelName().c_str(), inputTarget.flags.string().c_str(), |
Prabir Pradhan | c32a411 | 2024-01-23 23:20:37 +0000 | [diff] [blame] | 3285 | inputTarget.globalScaleFactor, bitsetToString(inputTarget.getPointerIds()).c_str(), |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3286 | inputTarget.getPointerInfoString().c_str()); |
| 3287 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3288 | |
| 3289 | // Skip this event if the connection status is not normal. |
| 3290 | // 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] | 3291 | if (connection->status != Connection::Status::NORMAL) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3292 | if (DEBUG_DISPATCH_CYCLE) { |
| 3293 | ALOGD("channel '%s' ~ Dropping event because the channel status is %s", |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3294 | connection->getInputChannelName().c_str(), |
| 3295 | ftl::enum_string(connection->status).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3296 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3297 | return; |
| 3298 | } |
| 3299 | |
| 3300 | // Split a motion event if needed. |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3301 | if (inputTarget.flags.test(InputTarget::Flags::SPLIT)) { |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3302 | LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3303 | "Entry type %s should not have Flags::SPLIT", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3304 | ftl::enum_string(eventEntry->type).c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3305 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3306 | const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry); |
Prabir Pradhan | c32a411 | 2024-01-23 23:20:37 +0000 | [diff] [blame] | 3307 | if (inputTarget.getPointerIds().count() != originalMotionEntry.getPointerCount()) { |
Siarhei Vishniakou | 16e4fa0 | 2023-02-16 17:48:56 -0800 | [diff] [blame] | 3308 | if (!inputTarget.firstDownTimeInTarget.has_value()) { |
| 3309 | logDispatchStateLocked(); |
| 3310 | LOG(FATAL) << "Splitting motion events requires a down time to be set for the " |
| 3311 | "target on connection " |
| 3312 | << connection->getInputChannelName() << " for " |
| 3313 | << originalMotionEntry.getDescription(); |
| 3314 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3315 | std::unique_ptr<MotionEntry> splitMotionEntry = |
Prabir Pradhan | c32a411 | 2024-01-23 23:20:37 +0000 | [diff] [blame] | 3316 | splitMotionEvent(originalMotionEntry, inputTarget.getPointerIds(), |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3317 | inputTarget.firstDownTimeInTarget.value()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3318 | if (!splitMotionEntry) { |
| 3319 | return; // split event was dropped |
| 3320 | } |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3321 | if (splitMotionEntry->action == AMOTION_EVENT_ACTION_CANCEL) { |
| 3322 | std::string reason = std::string("reason=pointer cancel on split window"); |
| 3323 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 3324 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 3325 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3326 | if (DEBUG_FOCUS) { |
| 3327 | ALOGD("channel '%s' ~ Split motion event.", |
| 3328 | connection->getInputChannelName().c_str()); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3329 | logOutboundMotionDetails(" ", *splitMotionEntry); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3330 | } |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3331 | enqueueDispatchEntryAndStartDispatchCycleLocked(currentTime, connection, |
| 3332 | std::move(splitMotionEntry), |
| 3333 | inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3334 | return; |
| 3335 | } |
| 3336 | } |
| 3337 | |
| 3338 | // Not splitting. Enqueue dispatch entries for the event as is. |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3339 | enqueueDispatchEntryAndStartDispatchCycleLocked(currentTime, connection, eventEntry, |
| 3340 | inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3341 | } |
| 3342 | |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3343 | void InputDispatcher::enqueueDispatchEntryAndStartDispatchCycleLocked( |
| 3344 | nsecs_t currentTime, const std::shared_ptr<Connection>& connection, |
| 3345 | std::shared_ptr<const EventEntry> eventEntry, const InputTarget& inputTarget) { |
Prabir Pradhan | 2dac8b8 | 2023-09-06 01:11:51 +0000 | [diff] [blame] | 3346 | ATRACE_NAME_IF(ATRACE_ENABLED(), |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3347 | StringPrintf("enqueueDispatchEntryAndStartDispatchCycleLocked(inputChannel=%s, " |
| 3348 | "id=0x%" PRIx32 ")", |
Prabir Pradhan | 2dac8b8 | 2023-09-06 01:11:51 +0000 | [diff] [blame] | 3349 | connection->getInputChannelName().c_str(), eventEntry->id)); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3350 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 3351 | const bool wasEmpty = connection->outboundQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3352 | |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3353 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3354 | |
| 3355 | // If the outbound queue was previously empty, start the dispatch cycle going. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3356 | if (wasEmpty && !connection->outboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3357 | startDispatchCycleLocked(currentTime, connection); |
| 3358 | } |
| 3359 | } |
| 3360 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3361 | void InputDispatcher::enqueueDispatchEntryLocked(const std::shared_ptr<Connection>& connection, |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 3362 | std::shared_ptr<const EventEntry> eventEntry, |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3363 | const InputTarget& inputTarget) { |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 3364 | const bool isKeyOrMotion = eventEntry->type == EventEntry::Type::KEY || |
| 3365 | eventEntry->type == EventEntry::Type::MOTION; |
| 3366 | if (isKeyOrMotion && !inputTarget.windowHandle && !connection->monitor) { |
| 3367 | LOG(FATAL) << "All InputTargets for non-monitors must be associated with a window; target: " |
| 3368 | << inputTarget << " connection: " << connection->getInputChannelName() |
| 3369 | << " entry: " << eventEntry->getDescription(); |
| 3370 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3371 | // This is a new event. |
| 3372 | // Enqueue a new dispatch entry onto the outbound queue for this connection. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3373 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Prabir Pradhan | c88b1fa | 2024-01-23 21:48:03 +0000 | [diff] [blame] | 3374 | createDispatchEntry(mIdGenerator, inputTarget, eventEntry, inputTarget.flags, |
| 3375 | mWindowInfosVsyncId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3376 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3377 | // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a |
| 3378 | // different EventEntry than what was passed in. |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3379 | eventEntry = dispatchEntry->eventEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3380 | // Apply target flags and update the connection's input state. |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3381 | switch (eventEntry->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3382 | case EventEntry::Type::KEY: { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3383 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(*eventEntry); |
| 3384 | if (!connection->inputState.trackKey(keyEntry, keyEntry.flags)) { |
Siarhei Vishniakou | c94dafe | 2023-05-26 10:24:19 -0700 | [diff] [blame] | 3385 | LOG(WARNING) << "channel " << connection->getInputChannelName() |
| 3386 | << "~ dropping inconsistent event: " << *dispatchEntry; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3387 | return; // skip the inconsistent event |
| 3388 | } |
| 3389 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3390 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3391 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3392 | case EventEntry::Type::MOTION: { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3393 | std::shared_ptr<const MotionEntry> resolvedMotion = |
| 3394 | std::static_pointer_cast<const MotionEntry>(eventEntry); |
| 3395 | { |
| 3396 | // Determine the resolved motion entry. |
| 3397 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); |
| 3398 | int32_t resolvedAction = motionEntry.action; |
| 3399 | int32_t resolvedFlags = motionEntry.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3400 | |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3401 | if (inputTarget.dispatchMode == InputTarget::DispatchMode::OUTSIDE) { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3402 | resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3403 | } else if (inputTarget.dispatchMode == InputTarget::DispatchMode::HOVER_EXIT) { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3404 | resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3405 | } else if (inputTarget.dispatchMode == InputTarget::DispatchMode::HOVER_ENTER) { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3406 | resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3407 | } else if (inputTarget.dispatchMode == InputTarget::DispatchMode::SLIPPERY_EXIT) { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3408 | resolvedAction = AMOTION_EVENT_ACTION_CANCEL; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3409 | } else if (inputTarget.dispatchMode == InputTarget::DispatchMode::SLIPPERY_ENTER) { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3410 | resolvedAction = AMOTION_EVENT_ACTION_DOWN; |
| 3411 | } |
| 3412 | if (resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE && |
| 3413 | !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source, |
| 3414 | motionEntry.displayId)) { |
| 3415 | if (DEBUG_DISPATCH_CYCLE) { |
| 3416 | LOG(DEBUG) << "channel '" << connection->getInputChannelName().c_str() |
| 3417 | << "' ~ enqueueDispatchEntryLocked: filling in missing hover " |
| 3418 | "enter event"; |
| 3419 | } |
| 3420 | resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 3421 | } |
| 3422 | |
| 3423 | if (resolvedAction == AMOTION_EVENT_ACTION_CANCEL) { |
| 3424 | resolvedFlags |= AMOTION_EVENT_FLAG_CANCELED; |
| 3425 | } |
| 3426 | if (dispatchEntry->targetFlags.test(InputTarget::Flags::WINDOW_IS_OBSCURED)) { |
| 3427 | resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; |
| 3428 | } |
| 3429 | if (dispatchEntry->targetFlags.test( |
| 3430 | InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED)) { |
| 3431 | resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 3432 | } |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 3433 | if (dispatchEntry->targetFlags.test(InputTarget::Flags::NO_FOCUS_CHANGE)) { |
| 3434 | resolvedFlags |= AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE; |
| 3435 | } |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3436 | |
| 3437 | dispatchEntry->resolvedFlags = resolvedFlags; |
| 3438 | if (resolvedAction != motionEntry.action) { |
Siarhei Vishniakou | e9ef6bc | 2023-12-21 19:47:20 -0800 | [diff] [blame] | 3439 | std::optional<std::vector<PointerProperties>> usingProperties; |
| 3440 | std::optional<std::vector<PointerCoords>> usingCoords; |
| 3441 | if (resolvedAction == AMOTION_EVENT_ACTION_HOVER_EXIT || |
| 3442 | resolvedAction == AMOTION_EVENT_ACTION_CANCEL) { |
| 3443 | // This is a HOVER_EXIT or an ACTION_CANCEL event that was synthesized by |
| 3444 | // the dispatcher, and therefore the coordinates of this event are currently |
| 3445 | // incorrect. These events should use the coordinates of the last dispatched |
| 3446 | // ACTION_MOVE or HOVER_MOVE. We need to query InputState to get this data. |
| 3447 | const bool hovering = resolvedAction == AMOTION_EVENT_ACTION_HOVER_EXIT; |
| 3448 | std::optional<std::pair<std::vector<PointerProperties>, |
| 3449 | std::vector<PointerCoords>>> |
| 3450 | pointerInfo = |
| 3451 | connection->inputState.getPointersOfLastEvent(motionEntry, |
| 3452 | hovering); |
| 3453 | if (pointerInfo) { |
| 3454 | usingProperties = pointerInfo->first; |
| 3455 | usingCoords = pointerInfo->second; |
| 3456 | } |
| 3457 | } |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3458 | // Generate a new MotionEntry with a new eventId using the resolved action and |
| 3459 | // flags. |
Siarhei Vishniakou | e9ef6bc | 2023-12-21 19:47:20 -0800 | [diff] [blame] | 3460 | resolvedMotion = std::make_shared< |
| 3461 | MotionEntry>(mIdGenerator.nextId(), motionEntry.injectionState, |
| 3462 | motionEntry.eventTime, motionEntry.deviceId, |
| 3463 | motionEntry.source, motionEntry.displayId, |
| 3464 | motionEntry.policyFlags, resolvedAction, |
| 3465 | motionEntry.actionButton, resolvedFlags, |
| 3466 | motionEntry.metaState, motionEntry.buttonState, |
| 3467 | motionEntry.classification, motionEntry.edgeFlags, |
| 3468 | motionEntry.xPrecision, motionEntry.yPrecision, |
| 3469 | motionEntry.xCursorPosition, motionEntry.yCursorPosition, |
| 3470 | motionEntry.downTime, |
| 3471 | usingProperties.value_or(motionEntry.pointerProperties), |
| 3472 | usingCoords.value_or(motionEntry.pointerCoords)); |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3473 | if (ATRACE_ENABLED()) { |
| 3474 | std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32 |
| 3475 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3476 | motionEntry.id, resolvedMotion->id); |
| 3477 | ATRACE_NAME(message.c_str()); |
| 3478 | } |
| 3479 | |
| 3480 | // Set the resolved motion entry in the DispatchEntry. |
| 3481 | dispatchEntry->eventEntry = resolvedMotion; |
| 3482 | eventEntry = resolvedMotion; |
| 3483 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3484 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3485 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3486 | // Check if we need to cancel any of the ongoing gestures. We don't support multiple |
| 3487 | // devices being active at the same time in the same window, so if a new device is |
| 3488 | // active, cancel the gesture from the old device. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3489 | std::unique_ptr<EventEntry> cancelEvent = |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3490 | connection->inputState.cancelConflictingInputStream(*resolvedMotion); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3491 | if (cancelEvent != nullptr) { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3492 | LOG(INFO) << "Canceling pointers for device " << resolvedMotion->deviceId << " in " |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3493 | << connection->getInputChannelName() << " with event " |
| 3494 | << cancelEvent->getDescription(); |
| 3495 | std::unique_ptr<DispatchEntry> cancelDispatchEntry = |
Prabir Pradhan | c88b1fa | 2024-01-23 21:48:03 +0000 | [diff] [blame] | 3496 | createDispatchEntry(mIdGenerator, inputTarget, std::move(cancelEvent), |
Prabir Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame] | 3497 | ftl::Flags<InputTarget::Flags>(), mWindowInfosVsyncId); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3498 | |
| 3499 | // Send these cancel events to the queue before sending the event from the new |
| 3500 | // device. |
| 3501 | connection->outboundQueue.emplace_back(std::move(cancelDispatchEntry)); |
| 3502 | } |
| 3503 | |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3504 | if (!connection->inputState.trackMotion(*resolvedMotion, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3505 | dispatchEntry->resolvedFlags)) { |
Siarhei Vishniakou | c94dafe | 2023-05-26 10:24:19 -0700 | [diff] [blame] | 3506 | LOG(WARNING) << "channel " << connection->getInputChannelName() |
| 3507 | << "~ dropping inconsistent event: " << *dispatchEntry; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3508 | return; // skip the inconsistent event |
| 3509 | } |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 3510 | if ((dispatchEntry->resolvedFlags & AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) && |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3511 | (resolvedMotion->policyFlags & POLICY_FLAG_TRUSTED)) { |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 3512 | // Skip reporting pointer down outside focus to the policy. |
| 3513 | break; |
| 3514 | } |
| 3515 | |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3516 | dispatchPointerDownOutsideFocus(resolvedMotion->source, resolvedMotion->action, |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 3517 | inputTarget.connection->getToken()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3518 | |
| 3519 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3520 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3521 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3522 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3523 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3524 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3525 | break; |
| 3526 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3527 | case EventEntry::Type::SENSOR: { |
| 3528 | LOG_ALWAYS_FATAL("SENSOR events should not go to apps via input channel"); |
| 3529 | break; |
| 3530 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3531 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 3532 | case EventEntry::Type::DEVICE_RESET: { |
| 3533 | LOG_ALWAYS_FATAL("%s events should not go to apps", |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3534 | ftl::enum_string(eventEntry->type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3535 | break; |
| 3536 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3537 | } |
| 3538 | |
| 3539 | // Remember that we are waiting for this dispatch to complete. |
| 3540 | if (dispatchEntry->hasForegroundTarget()) { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3541 | incrementPendingForegroundDispatches(*eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3542 | } |
| 3543 | |
| 3544 | // Enqueue the dispatch entry. |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 3545 | connection->outboundQueue.emplace_back(std::move(dispatchEntry)); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3546 | traceOutboundQueueLength(*connection); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3547 | } |
| 3548 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3549 | /** |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3550 | * This function is for debugging and metrics collection. It has two roles. |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3551 | * |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3552 | * The first role is to log input interaction with windows, which helps determine what the user was |
| 3553 | * interacting with. For example, if user is touching launcher, we will see an input_interaction log |
| 3554 | * that user started interacting with launcher window, as well as any other window that received |
| 3555 | * that gesture, such as the wallpaper or other spy windows. A new input_interaction is only logged |
| 3556 | * when the set of tokens that received the event changes. It is not logged again as long as the |
| 3557 | * user is interacting with the same windows. |
| 3558 | * |
| 3559 | * The second role is to track input device activity for metrics collection. For each input event, |
| 3560 | * we report the set of UIDs that the input device interacted with to the policy. Unlike for the |
| 3561 | * input_interaction logs, the device interaction is reported even when the set of interaction |
| 3562 | * tokens do not change. |
| 3563 | * |
| 3564 | * For these purposes, we do not count ACTION_OUTSIDE, ACTION_UP and ACTION_CANCEL actions as |
| 3565 | * interaction. This includes up and cancel events for both keys and motions. |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3566 | */ |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3567 | void InputDispatcher::processInteractionsLocked(const EventEntry& entry, |
| 3568 | const std::vector<InputTarget>& targets) { |
| 3569 | int32_t deviceId; |
| 3570 | nsecs_t eventTime; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3571 | // Skip ACTION_UP events, and all events other than keys and motions |
| 3572 | if (entry.type == EventEntry::Type::KEY) { |
| 3573 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 3574 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
| 3575 | return; |
| 3576 | } |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3577 | deviceId = keyEntry.deviceId; |
| 3578 | eventTime = keyEntry.eventTime; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3579 | } else if (entry.type == EventEntry::Type::MOTION) { |
| 3580 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 3581 | if (motionEntry.action == AMOTION_EVENT_ACTION_UP || |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3582 | motionEntry.action == AMOTION_EVENT_ACTION_CANCEL || |
| 3583 | MotionEvent::getActionMasked(motionEntry.action) == AMOTION_EVENT_ACTION_POINTER_UP) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3584 | return; |
| 3585 | } |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3586 | deviceId = motionEntry.deviceId; |
| 3587 | eventTime = motionEntry.eventTime; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3588 | } else { |
| 3589 | return; // Not a key or a motion |
| 3590 | } |
| 3591 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3592 | std::set<gui::Uid> interactionUids; |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 3593 | std::unordered_set<sp<IBinder>, StrongPointerHash<IBinder>> newConnectionTokens; |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3594 | std::vector<std::shared_ptr<Connection>> newConnections; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3595 | for (const InputTarget& target : targets) { |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3596 | if (target.dispatchMode == InputTarget::DispatchMode::OUTSIDE) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3597 | continue; // Skip windows that receive ACTION_OUTSIDE |
| 3598 | } |
| 3599 | |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 3600 | sp<IBinder> token = target.connection->getToken(); |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3601 | newConnectionTokens.insert(std::move(token)); |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 3602 | newConnections.emplace_back(target.connection); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3603 | if (target.windowHandle) { |
| 3604 | interactionUids.emplace(target.windowHandle->getInfo()->ownerUid); |
| 3605 | } |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3606 | } |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3607 | |
| 3608 | auto command = [this, deviceId, eventTime, uids = std::move(interactionUids)]() |
| 3609 | REQUIRES(mLock) { |
| 3610 | scoped_unlock unlock(mLock); |
| 3611 | mPolicy.notifyDeviceInteraction(deviceId, eventTime, uids); |
| 3612 | }; |
| 3613 | postCommandLocked(std::move(command)); |
| 3614 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3615 | if (newConnectionTokens == mInteractionConnectionTokens) { |
| 3616 | return; // no change |
| 3617 | } |
| 3618 | mInteractionConnectionTokens = newConnectionTokens; |
| 3619 | |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3620 | std::string targetList; |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3621 | for (const std::shared_ptr<Connection>& connection : newConnections) { |
Siarhei Vishniakou | 10bfdc9 | 2024-02-01 11:12:57 -0800 | [diff] [blame] | 3622 | targetList += connection->getInputChannelName() + ", "; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3623 | } |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3624 | std::string message = "Interaction with: " + targetList; |
| 3625 | if (targetList.empty()) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3626 | message += "<none>"; |
| 3627 | } |
| 3628 | android_log_event_list(LOGTAG_INPUT_INTERACTION) << message << LOG_ID_EVENTS; |
| 3629 | } |
| 3630 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3631 | void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3632 | const sp<IBinder>& token) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3633 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3634 | uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK; |
| 3635 | if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3636 | return; |
| 3637 | } |
| 3638 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 3639 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3640 | if (focusedToken == token) { |
| 3641 | // ignore since token is focused |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3642 | return; |
| 3643 | } |
| 3644 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3645 | auto command = [this, token]() REQUIRES(mLock) { |
| 3646 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 3647 | mPolicy.onPointerDownOutsideFocus(token); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3648 | }; |
| 3649 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3650 | } |
| 3651 | |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3652 | status_t InputDispatcher::publishMotionEvent(Connection& connection, |
| 3653 | DispatchEntry& dispatchEntry) const { |
| 3654 | const EventEntry& eventEntry = *(dispatchEntry.eventEntry); |
| 3655 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 3656 | |
| 3657 | PointerCoords scaledCoords[MAX_POINTERS]; |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 3658 | const PointerCoords* usingCoords = motionEntry.pointerCoords.data(); |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3659 | |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 3660 | // TODO(b/316355518): Do not modify coords before dispatch. |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3661 | // Set the X and Y offset and X and Y scale depending on the input source. |
| 3662 | if ((motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) && |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3663 | !(dispatchEntry.targetFlags.test(InputTarget::Flags::ZERO_COORDS))) { |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3664 | float globalScaleFactor = dispatchEntry.globalScaleFactor; |
| 3665 | if (globalScaleFactor != 1.0f) { |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 3666 | for (uint32_t i = 0; i < motionEntry.getPointerCount(); i++) { |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3667 | scaledCoords[i] = motionEntry.pointerCoords[i]; |
| 3668 | // Don't apply window scale here since we don't want scale to affect raw |
| 3669 | // coordinates. The scale will be sent back to the client and applied |
| 3670 | // later when requesting relative coordinates. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 3671 | scaledCoords[i].scale(globalScaleFactor, /*windowXScale=*/1, /*windowYScale=*/1); |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3672 | } |
| 3673 | usingCoords = scaledCoords; |
| 3674 | } |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3675 | } |
| 3676 | |
| 3677 | std::array<uint8_t, 32> hmac = getSignature(motionEntry, dispatchEntry); |
| 3678 | |
| 3679 | // Publish the motion event. |
| 3680 | return connection.inputPublisher |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3681 | .publishMotionEvent(dispatchEntry.seq, motionEntry.id, motionEntry.deviceId, |
| 3682 | motionEntry.source, motionEntry.displayId, std::move(hmac), |
| 3683 | motionEntry.action, motionEntry.actionButton, |
| 3684 | dispatchEntry.resolvedFlags, motionEntry.edgeFlags, |
| 3685 | motionEntry.metaState, motionEntry.buttonState, |
| 3686 | motionEntry.classification, dispatchEntry.transform, |
| 3687 | motionEntry.xPrecision, motionEntry.yPrecision, |
| 3688 | motionEntry.xCursorPosition, motionEntry.yCursorPosition, |
| 3689 | dispatchEntry.rawTransform, motionEntry.downTime, |
| 3690 | motionEntry.eventTime, motionEntry.getPointerCount(), |
| 3691 | motionEntry.pointerProperties.data(), usingCoords); |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3692 | } |
| 3693 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3694 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3695 | const std::shared_ptr<Connection>& connection) { |
Prabir Pradhan | 2dac8b8 | 2023-09-06 01:11:51 +0000 | [diff] [blame] | 3696 | ATRACE_NAME_IF(ATRACE_ENABLED(), |
| 3697 | StringPrintf("startDispatchCycleLocked(inputChannel=%s)", |
| 3698 | connection->getInputChannelName().c_str())); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3699 | if (DEBUG_DISPATCH_CYCLE) { |
| 3700 | ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str()); |
| 3701 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3702 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3703 | while (connection->status == Connection::Status::NORMAL && !connection->outboundQueue.empty()) { |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 3704 | std::unique_ptr<DispatchEntry>& dispatchEntry = connection->outboundQueue.front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3705 | dispatchEntry->deliveryTime = currentTime; |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3706 | const std::chrono::nanoseconds timeout = getDispatchingTimeoutLocked(connection); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3707 | dispatchEntry->timeoutTime = currentTime + timeout.count(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3708 | |
| 3709 | // Publish the event. |
| 3710 | status_t status; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3711 | const EventEntry& eventEntry = *(dispatchEntry->eventEntry); |
| 3712 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3713 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3714 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 3715 | std::array<uint8_t, 32> hmac = getSignature(keyEntry, *dispatchEntry); |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 3716 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
Siarhei Vishniakou | 827d1ac | 2023-07-21 16:37:51 -0700 | [diff] [blame] | 3717 | LOG(INFO) << "Publishing " << *dispatchEntry << " to " |
| 3718 | << connection->getInputChannelName(); |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 3719 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3720 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3721 | // Publish the key event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3722 | status = connection->inputPublisher |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3723 | .publishKeyEvent(dispatchEntry->seq, keyEntry.id, |
| 3724 | keyEntry.deviceId, keyEntry.source, |
| 3725 | keyEntry.displayId, std::move(hmac), |
| 3726 | keyEntry.action, dispatchEntry->resolvedFlags, |
| 3727 | keyEntry.keyCode, keyEntry.scanCode, |
| 3728 | keyEntry.metaState, keyEntry.repeatCount, |
| 3729 | keyEntry.downTime, keyEntry.eventTime); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 3730 | if (mTracer) { |
| 3731 | mTracer->traceEventDispatch(*dispatchEntry, keyEntry.traceTracker.get()); |
| 3732 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3733 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3734 | } |
| 3735 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3736 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 3737 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
Siarhei Vishniakou | 827d1ac | 2023-07-21 16:37:51 -0700 | [diff] [blame] | 3738 | LOG(INFO) << "Publishing " << *dispatchEntry << " to " |
| 3739 | << connection->getInputChannelName(); |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 3740 | } |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 3741 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3742 | status = publishMotionEvent(*connection, *dispatchEntry); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 3743 | if (mTracer) { |
| 3744 | mTracer->traceEventDispatch(*dispatchEntry, motionEntry.traceTracker.get()); |
| 3745 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3746 | break; |
| 3747 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3748 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3749 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3750 | const FocusEntry& focusEntry = static_cast<const FocusEntry&>(eventEntry); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3751 | status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3752 | focusEntry.id, |
Antonio Kantek | 3cfec7b | 2021-11-05 18:26:17 -0700 | [diff] [blame] | 3753 | focusEntry.hasFocus); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3754 | break; |
| 3755 | } |
| 3756 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3757 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 3758 | const TouchModeEntry& touchModeEntry = |
| 3759 | static_cast<const TouchModeEntry&>(eventEntry); |
| 3760 | status = connection->inputPublisher |
| 3761 | .publishTouchModeEvent(dispatchEntry->seq, touchModeEntry.id, |
| 3762 | touchModeEntry.inTouchMode); |
| 3763 | |
| 3764 | break; |
| 3765 | } |
| 3766 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3767 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 3768 | const auto& captureEntry = |
| 3769 | static_cast<const PointerCaptureChangedEntry&>(eventEntry); |
| 3770 | status = connection->inputPublisher |
| 3771 | .publishCaptureEvent(dispatchEntry->seq, captureEntry.id, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 3772 | captureEntry.pointerCaptureRequest.enable); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3773 | break; |
| 3774 | } |
| 3775 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3776 | case EventEntry::Type::DRAG: { |
| 3777 | const DragEntry& dragEntry = static_cast<const DragEntry&>(eventEntry); |
| 3778 | status = connection->inputPublisher.publishDragEvent(dispatchEntry->seq, |
| 3779 | dragEntry.id, dragEntry.x, |
| 3780 | dragEntry.y, |
| 3781 | dragEntry.isExiting); |
| 3782 | break; |
| 3783 | } |
| 3784 | |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3785 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3786 | case EventEntry::Type::DEVICE_RESET: |
| 3787 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3788 | LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3789 | ftl::enum_string(eventEntry.type).c_str()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3790 | return; |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3791 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3792 | } |
| 3793 | |
| 3794 | // Check the result. |
| 3795 | if (status) { |
| 3796 | if (status == WOULD_BLOCK) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3797 | if (connection->waitQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3798 | ALOGE("channel '%s' ~ Could not publish event because the pipe is full. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3799 | "This is unexpected because the wait queue is empty, so the pipe " |
| 3800 | "should be empty and we shouldn't have any problems writing an " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3801 | "event to it, status=%s(%d)", |
| 3802 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3803 | status); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 3804 | abortBrokenDispatchCycleLocked(currentTime, connection, /*notify=*/true); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3805 | } else { |
| 3806 | // Pipe is full and we are waiting for the app to finish process some events |
| 3807 | // before sending more events to it. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3808 | if (DEBUG_DISPATCH_CYCLE) { |
| 3809 | ALOGD("channel '%s' ~ Could not publish event because the pipe is full, " |
| 3810 | "waiting for the application to catch up", |
| 3811 | connection->getInputChannelName().c_str()); |
| 3812 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3813 | } |
| 3814 | } else { |
| 3815 | ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3816 | "status=%s(%d)", |
| 3817 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3818 | status); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 3819 | abortBrokenDispatchCycleLocked(currentTime, connection, /*notify=*/true); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3820 | } |
| 3821 | return; |
| 3822 | } |
| 3823 | |
| 3824 | // Re-enqueue the event on the wait queue. |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 3825 | const nsecs_t timeoutTime = dispatchEntry->timeoutTime; |
| 3826 | connection->waitQueue.emplace_back(std::move(dispatchEntry)); |
| 3827 | connection->outboundQueue.erase(connection->outboundQueue.begin()); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3828 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3829 | if (connection->responsive) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 3830 | mAnrTracker.insert(timeoutTime, connection->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3831 | } |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3832 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3833 | } |
| 3834 | } |
| 3835 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3836 | std::array<uint8_t, 32> InputDispatcher::sign(const VerifiedInputEvent& event) const { |
| 3837 | size_t size; |
| 3838 | switch (event.type) { |
| 3839 | case VerifiedInputEvent::Type::KEY: { |
| 3840 | size = sizeof(VerifiedKeyEvent); |
| 3841 | break; |
| 3842 | } |
| 3843 | case VerifiedInputEvent::Type::MOTION: { |
| 3844 | size = sizeof(VerifiedMotionEvent); |
| 3845 | break; |
| 3846 | } |
| 3847 | } |
| 3848 | const uint8_t* start = reinterpret_cast<const uint8_t*>(&event); |
| 3849 | return mHmacKeyManager.sign(start, size); |
| 3850 | } |
| 3851 | |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3852 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3853 | const MotionEntry& motionEntry, const DispatchEntry& dispatchEntry) const { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3854 | const int32_t actionMasked = MotionEvent::getActionMasked(motionEntry.action); |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3855 | if (actionMasked != AMOTION_EVENT_ACTION_UP && actionMasked != AMOTION_EVENT_ACTION_DOWN) { |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3856 | // Only sign events up and down events as the purely move events |
| 3857 | // are tied to their up/down counterparts so signing would be redundant. |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3858 | return INVALID_HMAC; |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3859 | } |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3860 | |
| 3861 | VerifiedMotionEvent verifiedEvent = |
| 3862 | verifiedMotionEventFromMotionEntry(motionEntry, dispatchEntry.rawTransform); |
| 3863 | verifiedEvent.actionMasked = actionMasked; |
| 3864 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS; |
| 3865 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3866 | } |
| 3867 | |
| 3868 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3869 | const KeyEntry& keyEntry, const DispatchEntry& dispatchEntry) const { |
| 3870 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(keyEntry); |
| 3871 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_KEY_EVENT_FLAGS; |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3872 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3873 | } |
| 3874 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3875 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3876 | const std::shared_ptr<Connection>& connection, |
| 3877 | uint32_t seq, bool handled, nsecs_t consumeTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3878 | if (DEBUG_DISPATCH_CYCLE) { |
| 3879 | ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s", |
| 3880 | connection->getInputChannelName().c_str(), seq, toString(handled)); |
| 3881 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3882 | |
Prabir Pradhan | 98ca4a2 | 2024-01-09 23:51:50 +0000 | [diff] [blame] | 3883 | if (connection->status != Connection::Status::NORMAL) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3884 | return; |
| 3885 | } |
| 3886 | |
| 3887 | // Notify other system components and prepare to start the next dispatch cycle. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3888 | auto command = [this, currentTime, connection, seq, handled, consumeTime]() REQUIRES(mLock) { |
| 3889 | doDispatchCycleFinishedCommand(currentTime, connection, seq, handled, consumeTime); |
| 3890 | }; |
| 3891 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3892 | } |
| 3893 | |
| 3894 | void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3895 | const std::shared_ptr<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3896 | bool notify) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3897 | if (DEBUG_DISPATCH_CYCLE) { |
Siarhei Vishniakou | 827d1ac | 2023-07-21 16:37:51 -0700 | [diff] [blame] | 3898 | LOG(INFO) << "channel '" << connection->getInputChannelName() << "'~ " << __func__ |
| 3899 | << " - notify=" << toString(notify); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3900 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3901 | |
| 3902 | // Clear the dispatch queues. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3903 | drainDispatchQueue(connection->outboundQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3904 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3905 | drainDispatchQueue(connection->waitQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3906 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3907 | |
| 3908 | // The connection appears to be unrecoverably broken. |
| 3909 | // Ignore already broken or zombie connections. |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3910 | if (connection->status == Connection::Status::NORMAL) { |
| 3911 | connection->status = Connection::Status::BROKEN; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3912 | |
| 3913 | if (notify) { |
| 3914 | // Notify other system components. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3915 | ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", |
| 3916 | connection->getInputChannelName().c_str()); |
| 3917 | |
| 3918 | auto command = [this, connection]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3919 | scoped_unlock unlock(mLock); |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 3920 | mPolicy.notifyInputChannelBroken(connection->getToken()); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3921 | }; |
| 3922 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3923 | } |
| 3924 | } |
| 3925 | } |
| 3926 | |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 3927 | void InputDispatcher::drainDispatchQueue(std::deque<std::unique_ptr<DispatchEntry>>& queue) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3928 | while (!queue.empty()) { |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 3929 | releaseDispatchEntry(std::move(queue.front())); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3930 | queue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3931 | } |
| 3932 | } |
| 3933 | |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 3934 | void InputDispatcher::releaseDispatchEntry(std::unique_ptr<DispatchEntry> dispatchEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3935 | if (dispatchEntry->hasForegroundTarget()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3936 | decrementPendingForegroundDispatches(*(dispatchEntry->eventEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3937 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3938 | } |
| 3939 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3940 | int InputDispatcher::handleReceiveCallback(int events, sp<IBinder> connectionToken) { |
| 3941 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3942 | std::shared_ptr<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3943 | if (connection == nullptr) { |
| 3944 | ALOGW("Received looper callback for unknown input channel token %p. events=0x%x", |
| 3945 | connectionToken.get(), events); |
| 3946 | return 0; // remove the callback |
| 3947 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3948 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3949 | bool notify; |
| 3950 | if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { |
| 3951 | if (!(events & ALOOPER_EVENT_INPUT)) { |
| 3952 | ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. " |
| 3953 | "events=0x%x", |
| 3954 | connection->getInputChannelName().c_str(), events); |
| 3955 | return 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3956 | } |
| 3957 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3958 | nsecs_t currentTime = now(); |
| 3959 | bool gotOne = false; |
| 3960 | status_t status = OK; |
| 3961 | for (;;) { |
| 3962 | Result<InputPublisher::ConsumerResponse> result = |
| 3963 | connection->inputPublisher.receiveConsumerResponse(); |
| 3964 | if (!result.ok()) { |
| 3965 | status = result.error().code(); |
| 3966 | break; |
| 3967 | } |
| 3968 | |
| 3969 | if (std::holds_alternative<InputPublisher::Finished>(*result)) { |
| 3970 | const InputPublisher::Finished& finish = |
| 3971 | std::get<InputPublisher::Finished>(*result); |
| 3972 | finishDispatchCycleLocked(currentTime, connection, finish.seq, finish.handled, |
| 3973 | finish.consumeTime); |
| 3974 | } else if (std::holds_alternative<InputPublisher::Timeline>(*result)) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3975 | if (shouldReportMetricsForConnection(*connection)) { |
| 3976 | const InputPublisher::Timeline& timeline = |
| 3977 | std::get<InputPublisher::Timeline>(*result); |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 3978 | mLatencyTracker.trackGraphicsLatency(timeline.inputEventId, |
| 3979 | connection->getToken(), |
| 3980 | std::move(timeline.graphicsTimeline)); |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3981 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3982 | } |
| 3983 | gotOne = true; |
| 3984 | } |
| 3985 | if (gotOne) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3986 | runCommandsLockedInterruptable(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3987 | if (status == WOULD_BLOCK) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3988 | return 1; |
| 3989 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3990 | } |
| 3991 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3992 | notify = status != DEAD_OBJECT || !connection->monitor; |
| 3993 | if (notify) { |
| 3994 | ALOGE("channel '%s' ~ Failed to receive finished signal. status=%s(%d)", |
| 3995 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3996 | status); |
| 3997 | } |
| 3998 | } else { |
| 3999 | // Monitor channels are never explicitly unregistered. |
| 4000 | // 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] | 4001 | const bool stillHaveWindowHandle = getWindowHandleLocked(connection->getToken()) != nullptr; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4002 | notify = !connection->monitor && stillHaveWindowHandle; |
| 4003 | if (notify) { |
| 4004 | ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred. events=0x%x", |
| 4005 | connection->getInputChannelName().c_str(), events); |
| 4006 | } |
| 4007 | } |
| 4008 | |
| 4009 | // Remove the channel. |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 4010 | removeInputChannelLocked(connection->getToken(), notify); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4011 | return 0; // remove the callback |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4012 | } |
| 4013 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4014 | void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4015 | const CancelationOptions& options) { |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 4016 | // Cancel windows (i.e. non-monitors). |
| 4017 | // A channel must have at least one window to receive any input. If a window was removed, the |
| 4018 | // event streams directed to the window will already have been canceled during window removal. |
| 4019 | // So there is no need to generate cancellations for connections without any windows. |
| 4020 | const auto [cancelPointers, cancelNonPointers] = expandCancellationMode(options.mode); |
| 4021 | // Generate cancellations for touched windows first. This is to avoid generating cancellations |
| 4022 | // through a non-touched window if there are more than one window for an input channel. |
| 4023 | if (cancelPointers) { |
| 4024 | for (const auto& [displayId, touchState] : mTouchStatesByDisplay) { |
| 4025 | if (options.displayId.has_value() && options.displayId != displayId) { |
| 4026 | continue; |
| 4027 | } |
| 4028 | for (const auto& touchedWindow : touchState.windows) { |
| 4029 | synthesizeCancelationEventsForWindowLocked(touchedWindow.windowHandle, options); |
| 4030 | } |
| 4031 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4032 | } |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 4033 | // Follow up by generating cancellations for all windows, because we don't explicitly track |
| 4034 | // the windows that have an ongoing focus event stream. |
| 4035 | if (cancelNonPointers) { |
| 4036 | for (const auto& [_, handles] : mWindowHandlesByDisplay) { |
| 4037 | for (const auto& windowHandle : handles) { |
| 4038 | synthesizeCancelationEventsForWindowLocked(windowHandle, options); |
| 4039 | } |
| 4040 | } |
| 4041 | } |
| 4042 | |
| 4043 | // Cancel monitors. |
| 4044 | synthesizeCancelationEventsForMonitorsLocked(options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4045 | } |
| 4046 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4047 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 4048 | const CancelationOptions& options) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 4049 | for (const auto& [_, monitors] : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4050 | for (const Monitor& monitor : monitors) { |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 4051 | synthesizeCancelationEventsForConnectionLocked(monitor.connection, options, |
| 4052 | /*window=*/nullptr); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4053 | } |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 4054 | } |
| 4055 | } |
| 4056 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 4057 | void InputDispatcher::synthesizeCancelationEventsForWindowLocked( |
| 4058 | const sp<WindowInfoHandle>& windowHandle, const CancelationOptions& options, |
| 4059 | const std::shared_ptr<Connection>& connection) { |
| 4060 | if (windowHandle == nullptr) { |
| 4061 | LOG(FATAL) << __func__ << ": Window handle must not be null"; |
| 4062 | } |
| 4063 | if (connection) { |
| 4064 | // The connection can be optionally provided to avoid multiple lookups. |
| 4065 | if (windowHandle->getToken() != connection->getToken()) { |
| 4066 | LOG(FATAL) << __func__ |
| 4067 | << ": Wrong connection provided for window: " << windowHandle->getName(); |
| 4068 | } |
| 4069 | } |
| 4070 | |
| 4071 | std::shared_ptr<Connection> resolvedConnection = |
| 4072 | connection ? connection : getConnectionLocked(windowHandle->getToken()); |
| 4073 | if (!resolvedConnection) { |
| 4074 | LOG(DEBUG) << __func__ << "No connection found for window: " << windowHandle->getName(); |
| 4075 | return; |
| 4076 | } |
| 4077 | synthesizeCancelationEventsForConnectionLocked(resolvedConnection, options, windowHandle); |
| 4078 | } |
| 4079 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4080 | void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 4081 | const std::shared_ptr<Connection>& connection, const CancelationOptions& options, |
| 4082 | const sp<WindowInfoHandle>& window) { |
| 4083 | if (!connection->monitor && window == nullptr) { |
| 4084 | LOG(FATAL) << __func__ |
| 4085 | << ": Cannot send event to non-monitor channel without a window - channel: " |
| 4086 | << connection->getInputChannelName(); |
| 4087 | } |
Prabir Pradhan | b13da8f | 2024-01-09 23:10:13 +0000 | [diff] [blame] | 4088 | if (connection->status != Connection::Status::NORMAL) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4089 | return; |
| 4090 | } |
| 4091 | |
| 4092 | nsecs_t currentTime = now(); |
| 4093 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4094 | std::vector<std::unique_ptr<EventEntry>> cancelationEvents = |
Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 4095 | connection->inputState.synthesizeCancelationEvents(currentTime, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4096 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4097 | if (cancelationEvents.empty()) { |
| 4098 | return; |
| 4099 | } |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 4100 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4101 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 4102 | 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] | 4103 | "with reality: %s, mode=%s.", |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4104 | connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason, |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 4105 | ftl::enum_string(options.mode).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4106 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4107 | |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 4108 | std::string reason = std::string("reason=").append(options.reason); |
| 4109 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 4110 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 4111 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 4112 | const bool wasEmpty = connection->outboundQueue.empty(); |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 4113 | // 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] | 4114 | const InputTarget fallbackTarget{connection}; |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 4115 | const auto& token = connection->getToken(); |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 4116 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4117 | for (size_t i = 0; i < cancelationEvents.size(); i++) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4118 | std::unique_ptr<EventEntry> cancelationEventEntry = std::move(cancelationEvents[i]); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4119 | std::vector<InputTarget> targets{}; |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4120 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4121 | switch (cancelationEventEntry->type) { |
| 4122 | case EventEntry::Type::KEY: { |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4123 | const auto& keyEntry = static_cast<const KeyEntry&>(*cancelationEventEntry); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 4124 | if (window) { |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 4125 | addWindowTargetLocked(window, InputTarget::DispatchMode::AS_IS, |
| 4126 | /*targetFlags=*/{}, keyEntry.downTime, targets); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4127 | } else { |
| 4128 | targets.emplace_back(fallbackTarget); |
| 4129 | } |
| 4130 | logOutboundKeyDetails("cancel - ", keyEntry); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4131 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4132 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4133 | case EventEntry::Type::MOTION: { |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4134 | const auto& motionEntry = static_cast<const MotionEntry&>(*cancelationEventEntry); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 4135 | if (window) { |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4136 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4137 | for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.getPointerCount(); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4138 | pointerIndex++) { |
| 4139 | pointerIds.set(motionEntry.pointerProperties[pointerIndex].id); |
| 4140 | } |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 4141 | if (mDragState && mDragState->dragWindow->getToken() == token && |
| 4142 | pointerIds.test(mDragState->pointerId)) { |
| 4143 | LOG(INFO) << __func__ |
| 4144 | << ": Canceling drag and drop because the pointers for the drag " |
| 4145 | "window are being canceled."; |
| 4146 | sendDropWindowCommandLocked(nullptr, /*x=*/0, /*y=*/0); |
| 4147 | mDragState.reset(); |
| 4148 | } |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 4149 | addPointerWindowTargetLocked(window, InputTarget::DispatchMode::AS_IS, |
| 4150 | ftl::Flags<InputTarget::Flags>(), pointerIds, |
| 4151 | motionEntry.downTime, targets); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4152 | } else { |
| 4153 | targets.emplace_back(fallbackTarget); |
| 4154 | const auto it = mDisplayInfos.find(motionEntry.displayId); |
| 4155 | if (it != mDisplayInfos.end()) { |
| 4156 | targets.back().displayTransform = it->second.transform; |
| 4157 | targets.back().setDefaultPointerTransform(it->second.transform); |
| 4158 | } |
| 4159 | } |
| 4160 | logOutboundMotionDetails("cancel - ", motionEntry); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4161 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4162 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4163 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 4164 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4165 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 4166 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4167 | LOG_ALWAYS_FATAL("Canceling %s events is not supported", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 4168 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4169 | break; |
| 4170 | } |
| 4171 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4172 | case EventEntry::Type::DEVICE_RESET: |
| 4173 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4174 | 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] | 4175 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4176 | break; |
| 4177 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4178 | } |
| 4179 | |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4180 | if (targets.size() != 1) LOG(FATAL) << __func__ << ": InputTarget not created"; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 4181 | enqueueDispatchEntryLocked(connection, std::move(cancelationEventEntry), targets[0]); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4182 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4183 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 4184 | // If the outbound queue was previously empty, start the dispatch cycle going. |
| 4185 | if (wasEmpty && !connection->outboundQueue.empty()) { |
| 4186 | startDispatchCycleLocked(currentTime, connection); |
| 4187 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4188 | } |
| 4189 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4190 | void InputDispatcher::synthesizePointerDownEventsForConnectionLocked( |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 4191 | const nsecs_t downTime, const std::shared_ptr<Connection>& connection, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4192 | ftl::Flags<InputTarget::Flags> targetFlags) { |
Prabir Pradhan | 98ca4a2 | 2024-01-09 23:51:50 +0000 | [diff] [blame] | 4193 | if (connection->status != Connection::Status::NORMAL) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4194 | return; |
| 4195 | } |
| 4196 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4197 | std::vector<std::unique_ptr<EventEntry>> downEvents = |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4198 | connection->inputState.synthesizePointerDownEvents(downTime); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4199 | |
| 4200 | if (downEvents.empty()) { |
| 4201 | return; |
| 4202 | } |
| 4203 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4204 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4205 | ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.", |
| 4206 | connection->getInputChannelName().c_str(), downEvents.size()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4207 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4208 | |
Prabir Pradhan | feca057 | 2024-02-06 00:43:11 +0000 | [diff] [blame] | 4209 | const auto [_, touchedWindowState, displayId] = |
| 4210 | findTouchStateWindowAndDisplayLocked(connection->getToken()); |
| 4211 | if (touchedWindowState == nullptr) { |
| 4212 | LOG(FATAL) << __func__ << ": Touch state is out of sync: No touched window for token"; |
| 4213 | } |
| 4214 | const auto& windowHandle = touchedWindowState->windowHandle; |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4215 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 4216 | const bool wasEmpty = connection->outboundQueue.empty(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4217 | for (std::unique_ptr<EventEntry>& downEventEntry : downEvents) { |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 4218 | std::vector<InputTarget> targets{}; |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4219 | switch (downEventEntry->type) { |
| 4220 | case EventEntry::Type::MOTION: { |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 4221 | const auto& motionEntry = static_cast<const MotionEntry&>(*downEventEntry); |
| 4222 | if (windowHandle != nullptr) { |
| 4223 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4224 | for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.getPointerCount(); |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 4225 | pointerIndex++) { |
| 4226 | pointerIds.set(motionEntry.pointerProperties[pointerIndex].id); |
| 4227 | } |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 4228 | addPointerWindowTargetLocked(windowHandle, InputTarget::DispatchMode::AS_IS, |
| 4229 | targetFlags, pointerIds, motionEntry.downTime, |
| 4230 | targets); |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 4231 | } else { |
Prabir Pradhan | c32a411 | 2024-01-23 23:20:37 +0000 | [diff] [blame] | 4232 | targets.emplace_back(connection, targetFlags); |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 4233 | const auto it = mDisplayInfos.find(motionEntry.displayId); |
| 4234 | if (it != mDisplayInfos.end()) { |
| 4235 | targets.back().displayTransform = it->second.transform; |
| 4236 | targets.back().setDefaultPointerTransform(it->second.transform); |
| 4237 | } |
| 4238 | } |
| 4239 | logOutboundMotionDetails("down - ", motionEntry); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4240 | break; |
| 4241 | } |
| 4242 | |
| 4243 | case EventEntry::Type::KEY: |
| 4244 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 4245 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4246 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4247 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4248 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4249 | case EventEntry::Type::SENSOR: |
| 4250 | case EventEntry::Type::DRAG: { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4251 | 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] | 4252 | ftl::enum_string(downEventEntry->type).c_str()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4253 | break; |
| 4254 | } |
| 4255 | } |
| 4256 | |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 4257 | if (targets.size() != 1) LOG(FATAL) << __func__ << ": InputTarget not created"; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 4258 | enqueueDispatchEntryLocked(connection, std::move(downEventEntry), targets[0]); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4259 | } |
| 4260 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 4261 | // If the outbound queue was previously empty, start the dispatch cycle going. |
| 4262 | if (wasEmpty && !connection->outboundQueue.empty()) { |
| 4263 | startDispatchCycleLocked(downTime, connection); |
| 4264 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4265 | } |
| 4266 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4267 | std::unique_ptr<MotionEntry> InputDispatcher::splitMotionEvent( |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 4268 | const MotionEntry& originalMotionEntry, std::bitset<MAX_POINTER_ID + 1> pointerIds, |
| 4269 | nsecs_t splitDownTime) { |
| 4270 | ALOG_ASSERT(pointerIds.any()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4271 | |
| 4272 | uint32_t splitPointerIndexMap[MAX_POINTERS]; |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4273 | std::vector<PointerProperties> splitPointerProperties; |
| 4274 | std::vector<PointerCoords> splitPointerCoords; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4275 | |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4276 | uint32_t originalPointerCount = originalMotionEntry.getPointerCount(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4277 | uint32_t splitPointerCount = 0; |
| 4278 | |
| 4279 | for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4280 | originalPointerIndex++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4281 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 4282 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4283 | uint32_t pointerId = uint32_t(pointerProperties.id); |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 4284 | if (pointerIds.test(pointerId)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4285 | splitPointerIndexMap[splitPointerCount] = originalPointerIndex; |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4286 | splitPointerProperties.push_back(pointerProperties); |
| 4287 | splitPointerCoords.push_back(originalMotionEntry.pointerCoords[originalPointerIndex]); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4288 | splitPointerCount += 1; |
| 4289 | } |
| 4290 | } |
| 4291 | |
| 4292 | if (splitPointerCount != pointerIds.count()) { |
| 4293 | // This is bad. We are missing some of the pointers that we expected to deliver. |
| 4294 | // Most likely this indicates that we received an ACTION_MOVE events that has |
| 4295 | // different pointer ids than we expected based on the previous ACTION_DOWN |
| 4296 | // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers |
| 4297 | // in this way. |
| 4298 | ALOGW("Dropping split motion event because the pointer count is %d but " |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 4299 | "we expected there to be %zu pointers. This probably means we received " |
Siarhei Vishniakou | 16e4fa0 | 2023-02-16 17:48:56 -0800 | [diff] [blame] | 4300 | "a broken sequence of pointer ids from the input device: %s", |
| 4301 | splitPointerCount, pointerIds.count(), originalMotionEntry.getDescription().c_str()); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4302 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4303 | } |
| 4304 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 4305 | int32_t action = originalMotionEntry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4306 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4307 | if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN || |
| 4308 | maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
Siarhei Vishniakou | 5b9766d | 2023-07-18 14:06:29 -0700 | [diff] [blame] | 4309 | int32_t originalPointerIndex = MotionEvent::getActionIndex(action); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4310 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 4311 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4312 | uint32_t pointerId = uint32_t(pointerProperties.id); |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 4313 | if (pointerIds.test(pointerId)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4314 | if (pointerIds.count() == 1) { |
| 4315 | // The first/last pointer went down/up. |
| 4316 | action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4317 | ? AMOTION_EVENT_ACTION_DOWN |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4318 | : (originalMotionEntry.flags & AMOTION_EVENT_FLAG_CANCELED) != 0 |
| 4319 | ? AMOTION_EVENT_ACTION_CANCEL |
| 4320 | : AMOTION_EVENT_ACTION_UP; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4321 | } else { |
| 4322 | // A secondary pointer went down/up. |
| 4323 | uint32_t splitPointerIndex = 0; |
| 4324 | while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { |
| 4325 | splitPointerIndex += 1; |
| 4326 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4327 | action = maskedAction | |
| 4328 | (splitPointerIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4329 | } |
| 4330 | } else { |
| 4331 | // An unrelated pointer changed. |
| 4332 | action = AMOTION_EVENT_ACTION_MOVE; |
| 4333 | } |
| 4334 | } |
| 4335 | |
Siarhei Vishniakou | 59e302b | 2023-06-05 08:04:53 -0700 | [diff] [blame] | 4336 | if (action == AMOTION_EVENT_ACTION_DOWN && splitDownTime != originalMotionEntry.eventTime) { |
| 4337 | logDispatchStateLocked(); |
| 4338 | LOG_ALWAYS_FATAL("Split motion event has mismatching downTime and eventTime for " |
| 4339 | "ACTION_DOWN, motionEntry=%s, splitDownTime=%" PRId64, |
| 4340 | originalMotionEntry.getDescription().c_str(), splitDownTime); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4341 | } |
| 4342 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4343 | int32_t newId = mIdGenerator.nextId(); |
Prabir Pradhan | 2dac8b8 | 2023-09-06 01:11:51 +0000 | [diff] [blame] | 4344 | ATRACE_NAME_IF(ATRACE_ENABLED(), |
| 4345 | StringPrintf("Split MotionEvent(id=0x%" PRIx32 ") to MotionEvent(id=0x%" PRIx32 |
| 4346 | ").", |
| 4347 | originalMotionEntry.id, newId)); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4348 | std::unique_ptr<MotionEntry> splitMotionEntry = |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4349 | std::make_unique<MotionEntry>(newId, originalMotionEntry.injectionState, |
| 4350 | originalMotionEntry.eventTime, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4351 | originalMotionEntry.deviceId, originalMotionEntry.source, |
| 4352 | originalMotionEntry.displayId, |
| 4353 | originalMotionEntry.policyFlags, action, |
| 4354 | originalMotionEntry.actionButton, |
| 4355 | originalMotionEntry.flags, originalMotionEntry.metaState, |
| 4356 | originalMotionEntry.buttonState, |
| 4357 | originalMotionEntry.classification, |
| 4358 | originalMotionEntry.edgeFlags, |
| 4359 | originalMotionEntry.xPrecision, |
| 4360 | originalMotionEntry.yPrecision, |
| 4361 | originalMotionEntry.xCursorPosition, |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4362 | originalMotionEntry.yCursorPosition, splitDownTime, |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4363 | splitPointerProperties, splitPointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4364 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4365 | return splitMotionEntry; |
| 4366 | } |
| 4367 | |
Asmita Poddar | dd9a6cd | 2023-09-26 15:35:12 +0000 | [diff] [blame] | 4368 | void InputDispatcher::notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs& args) { |
| 4369 | std::scoped_lock _l(mLock); |
| 4370 | mLatencyTracker.setInputDevices(args.inputDeviceInfos); |
| 4371 | } |
| 4372 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4373 | void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4374 | if (debugInboundEventDetails()) { |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4375 | ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args.eventTime); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4376 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4377 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4378 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4379 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4380 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4381 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4382 | std::unique_ptr<ConfigurationChangedEntry> newEntry = |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4383 | std::make_unique<ConfigurationChangedEntry>(args.id, args.eventTime); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4384 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4385 | } // release lock |
| 4386 | |
| 4387 | if (needWake) { |
| 4388 | mLooper->wake(); |
| 4389 | } |
| 4390 | } |
| 4391 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4392 | void InputDispatcher::notifyKey(const NotifyKeyArgs& args) { |
Prabir Pradhan | 96282b0 | 2023-02-24 22:36:17 +0000 | [diff] [blame] | 4393 | ALOGD_IF(debugInboundEventDetails(), |
| 4394 | "notifyKey - id=%" PRIx32 ", eventTime=%" PRId64 |
| 4395 | ", deviceId=%d, source=%s, displayId=%" PRId32 |
| 4396 | "policyFlags=0x%x, action=%s, flags=0x%x, keyCode=%s, scanCode=0x%x, metaState=0x%x, " |
| 4397 | "downTime=%" PRId64, |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4398 | args.id, args.eventTime, args.deviceId, inputEventSourceToString(args.source).c_str(), |
| 4399 | args.displayId, args.policyFlags, KeyEvent::actionToString(args.action), args.flags, |
| 4400 | KeyEvent::getLabel(args.keyCode), args.scanCode, args.metaState, args.downTime); |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4401 | Result<void> keyCheck = validateKeyEvent(args.action); |
| 4402 | if (!keyCheck.ok()) { |
| 4403 | LOG(ERROR) << "invalid key event: " << keyCheck.error(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4404 | return; |
| 4405 | } |
| 4406 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4407 | uint32_t policyFlags = args.policyFlags; |
| 4408 | int32_t flags = args.flags; |
| 4409 | int32_t metaState = args.metaState; |
Siarhei Vishniakou | 622bd32 | 2018-10-29 18:02:27 -0700 | [diff] [blame] | 4410 | // InputDispatcher tracks and generates key repeats on behalf of |
| 4411 | // whatever notifies it, so repeatCount should always be set to 0 |
| 4412 | constexpr int32_t repeatCount = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4413 | if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { |
| 4414 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 4415 | flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
| 4416 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4417 | if (policyFlags & POLICY_FLAG_FUNCTION) { |
| 4418 | metaState |= AMETA_FUNCTION_ON; |
| 4419 | } |
| 4420 | |
| 4421 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 4422 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4423 | int32_t keyCode = args.keyCode; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4424 | KeyEvent event; |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4425 | event.initialize(args.id, args.deviceId, args.source, args.displayId, INVALID_HMAC, args.action, |
| 4426 | flags, keyCode, args.scanCode, metaState, repeatCount, args.downTime, |
| 4427 | args.eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4428 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4429 | android::base::Timer t; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4430 | mPolicy.interceptKeyBeforeQueueing(event, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4431 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4432 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4433 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4434 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4435 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4436 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4437 | { // acquire lock |
| 4438 | mLock.lock(); |
| 4439 | |
| 4440 | if (shouldSendKeyToInputFilterLocked(args)) { |
| 4441 | mLock.unlock(); |
| 4442 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4443 | policyFlags |= POLICY_FLAG_FILTERED; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4444 | if (!mPolicy.filterInputEvent(event, policyFlags)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4445 | return; // event was consumed by the filter |
| 4446 | } |
| 4447 | |
| 4448 | mLock.lock(); |
| 4449 | } |
| 4450 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4451 | std::unique_ptr<KeyEntry> newEntry = |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4452 | std::make_unique<KeyEntry>(args.id, /*injectionState=*/nullptr, args.eventTime, |
| 4453 | args.deviceId, args.source, args.displayId, policyFlags, |
| 4454 | args.action, flags, keyCode, args.scanCode, metaState, |
| 4455 | repeatCount, args.downTime); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 4456 | if (mTracer) { |
| 4457 | newEntry->traceTracker = mTracer->traceInboundEvent(*newEntry); |
| 4458 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4459 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4460 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4461 | mLock.unlock(); |
| 4462 | } // release lock |
| 4463 | |
| 4464 | if (needWake) { |
| 4465 | mLooper->wake(); |
| 4466 | } |
| 4467 | } |
| 4468 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4469 | bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs& args) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4470 | return mInputFilterEnabled; |
| 4471 | } |
| 4472 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4473 | void InputDispatcher::notifyMotion(const NotifyMotionArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4474 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 96282b0 | 2023-02-24 22:36:17 +0000 | [diff] [blame] | 4475 | ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=%s, " |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4476 | "displayId=%" PRId32 ", policyFlags=0x%x, " |
Siarhei Vishniakou | 6ebd069 | 2022-10-20 15:05:45 -0700 | [diff] [blame] | 4477 | "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] | 4478 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, xCursorPosition=%f, " |
| 4479 | "yCursorPosition=%f, downTime=%" PRId64, |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4480 | args.id, args.eventTime, args.deviceId, inputEventSourceToString(args.source).c_str(), |
| 4481 | args.displayId, args.policyFlags, MotionEvent::actionToString(args.action).c_str(), |
| 4482 | args.actionButton, args.flags, args.metaState, args.buttonState, args.edgeFlags, |
| 4483 | args.xPrecision, args.yPrecision, args.xCursorPosition, args.yCursorPosition, |
| 4484 | args.downTime); |
Siarhei Vishniakou | 3218fc0 | 2023-06-15 20:41:02 -0700 | [diff] [blame] | 4485 | for (uint32_t i = 0; i < args.getPointerCount(); i++) { |
Prabir Pradhan | 96282b0 | 2023-02-24 22:36:17 +0000 | [diff] [blame] | 4486 | ALOGD(" Pointer %d: id=%d, toolType=%s, x=%f, y=%f, pressure=%f, size=%f, " |
| 4487 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, orientation=%f", |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4488 | i, args.pointerProperties[i].id, |
| 4489 | ftl::enum_string(args.pointerProperties[i].toolType).c_str(), |
| 4490 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 4491 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 4492 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 4493 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 4494 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 4495 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 4496 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 4497 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 4498 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4499 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4500 | } |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4501 | |
Siarhei Vishniakou | 3218fc0 | 2023-06-15 20:41:02 -0700 | [diff] [blame] | 4502 | Result<void> motionCheck = |
| 4503 | validateMotionEvent(args.action, args.actionButton, args.getPointerCount(), |
| 4504 | args.pointerProperties.data()); |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4505 | if (!motionCheck.ok()) { |
| 4506 | LOG(FATAL) << "Invalid event: " << args.dump() << "; reason: " << motionCheck.error(); |
| 4507 | return; |
| 4508 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4509 | |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 4510 | if (DEBUG_VERIFY_EVENTS) { |
| 4511 | auto [it, _] = |
| 4512 | mVerifiersByDisplay.try_emplace(args.displayId, |
| 4513 | StringPrintf("display %" PRId32, args.displayId)); |
| 4514 | Result<void> result = |
Siarhei Vishniakou | 2d151ac | 2023-09-19 13:30:24 -0700 | [diff] [blame] | 4515 | it->second.processMovement(args.deviceId, args.source, args.action, |
| 4516 | args.getPointerCount(), args.pointerProperties.data(), |
| 4517 | args.pointerCoords.data(), args.flags); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 4518 | if (!result.ok()) { |
| 4519 | LOG(FATAL) << "Bad stream: " << result.error() << " caused by " << args.dump(); |
| 4520 | } |
| 4521 | } |
| 4522 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4523 | uint32_t policyFlags = args.policyFlags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4524 | policyFlags |= POLICY_FLAG_TRUSTED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4525 | |
| 4526 | android::base::Timer t; |
Yeabkal Wubshit | 88a9041 | 2023-12-21 18:23:04 -0800 | [diff] [blame] | 4527 | mPolicy.interceptMotionBeforeQueueing(args.displayId, args.source, args.action, args.eventTime, |
| 4528 | policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4529 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4530 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4531 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4532 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4533 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4534 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4535 | { // acquire lock |
| 4536 | mLock.lock(); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 4537 | if (!(policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
| 4538 | // Set the flag anyway if we already have an ongoing gesture. That would allow us to |
| 4539 | // complete the processing of the current stroke. |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4540 | const auto touchStateIt = mTouchStatesByDisplay.find(args.displayId); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 4541 | if (touchStateIt != mTouchStatesByDisplay.end()) { |
| 4542 | const TouchState& touchState = touchStateIt->second; |
Linnan Li | 907ae73 | 2023-09-05 17:14:21 +0800 | [diff] [blame] | 4543 | if (touchState.hasTouchingPointers(args.deviceId) || |
| 4544 | touchState.hasHoveringPointers(args.deviceId)) { |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 4545 | policyFlags |= POLICY_FLAG_PASS_TO_USER; |
| 4546 | } |
| 4547 | } |
| 4548 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4549 | |
| 4550 | if (shouldSendMotionToInputFilterLocked(args)) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4551 | ui::Transform displayTransform; |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4552 | if (const auto it = mDisplayInfos.find(args.displayId); it != mDisplayInfos.end()) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4553 | displayTransform = it->second.transform; |
| 4554 | } |
| 4555 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4556 | mLock.unlock(); |
| 4557 | |
| 4558 | MotionEvent event; |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4559 | event.initialize(args.id, args.deviceId, args.source, args.displayId, INVALID_HMAC, |
| 4560 | args.action, args.actionButton, args.flags, args.edgeFlags, |
| 4561 | args.metaState, args.buttonState, args.classification, |
| 4562 | displayTransform, args.xPrecision, args.yPrecision, |
| 4563 | args.xCursorPosition, args.yCursorPosition, displayTransform, |
Siarhei Vishniakou | 3218fc0 | 2023-06-15 20:41:02 -0700 | [diff] [blame] | 4564 | args.downTime, args.eventTime, args.getPointerCount(), |
| 4565 | args.pointerProperties.data(), args.pointerCoords.data()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4566 | |
| 4567 | policyFlags |= POLICY_FLAG_FILTERED; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4568 | if (!mPolicy.filterInputEvent(event, policyFlags)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4569 | return; // event was consumed by the filter |
| 4570 | } |
| 4571 | |
| 4572 | mLock.lock(); |
| 4573 | } |
| 4574 | |
| 4575 | // Just enqueue a new motion event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4576 | std::unique_ptr<MotionEntry> newEntry = |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4577 | std::make_unique<MotionEntry>(args.id, /*injectionState=*/nullptr, args.eventTime, |
| 4578 | args.deviceId, args.source, args.displayId, |
| 4579 | policyFlags, args.action, args.actionButton, |
| 4580 | args.flags, args.metaState, args.buttonState, |
| 4581 | args.classification, args.edgeFlags, args.xPrecision, |
| 4582 | args.yPrecision, args.xCursorPosition, |
| 4583 | args.yCursorPosition, args.downTime, |
| 4584 | args.pointerProperties, args.pointerCoords); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 4585 | if (mTracer) { |
| 4586 | newEntry->traceTracker = mTracer->traceInboundEvent(*newEntry); |
| 4587 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4588 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4589 | if (args.id != android::os::IInputConstants::INVALID_INPUT_EVENT_ID && |
| 4590 | IdGenerator::getSource(args.id) == IdGenerator::Source::INPUT_READER && |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 4591 | !mInputFilterEnabled) { |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4592 | const bool isDown = args.action == AMOTION_EVENT_ACTION_DOWN; |
Asmita Poddar | dd9a6cd | 2023-09-26 15:35:12 +0000 | [diff] [blame] | 4593 | std::set<InputDeviceUsageSource> sources = getUsageSourcesForMotionArgs(args); |
| 4594 | mLatencyTracker.trackListener(args.id, isDown, args.eventTime, args.readTime, |
| 4595 | args.deviceId, sources); |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 4596 | } |
| 4597 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4598 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4599 | mLock.unlock(); |
| 4600 | } // release lock |
| 4601 | |
| 4602 | if (needWake) { |
| 4603 | mLooper->wake(); |
| 4604 | } |
| 4605 | } |
| 4606 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4607 | void InputDispatcher::notifySensor(const NotifySensorArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4608 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4609 | ALOGD("notifySensor - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 4610 | " sensorType=%s", |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4611 | args.id, args.eventTime, args.deviceId, args.source, |
| 4612 | ftl::enum_string(args.sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4613 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4614 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4615 | bool needWake = false; |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4616 | { // acquire lock |
| 4617 | mLock.lock(); |
| 4618 | |
| 4619 | // Just enqueue a new sensor event. |
| 4620 | std::unique_ptr<SensorEntry> newEntry = |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4621 | std::make_unique<SensorEntry>(args.id, args.eventTime, args.deviceId, args.source, |
| 4622 | /* policyFlags=*/0, args.hwTimestamp, args.sensorType, |
| 4623 | args.accuracy, args.accuracyChanged, args.values); |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4624 | |
| 4625 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
| 4626 | mLock.unlock(); |
| 4627 | } // release lock |
| 4628 | |
| 4629 | if (needWake) { |
| 4630 | mLooper->wake(); |
| 4631 | } |
| 4632 | } |
| 4633 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4634 | void InputDispatcher::notifyVibratorState(const NotifyVibratorStateArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4635 | if (debugInboundEventDetails()) { |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4636 | ALOGD("notifyVibratorState - eventTime=%" PRId64 ", device=%d, isOn=%d", args.eventTime, |
| 4637 | args.deviceId, args.isOn); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4638 | } |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4639 | mPolicy.notifyVibratorState(args.deviceId, args.isOn); |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 4640 | } |
| 4641 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4642 | bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs& args) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4643 | return mInputFilterEnabled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4644 | } |
| 4645 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4646 | void InputDispatcher::notifySwitch(const NotifySwitchArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4647 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4648 | ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, " |
| 4649 | "switchMask=0x%08x", |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4650 | args.eventTime, args.policyFlags, args.switchValues, args.switchMask); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4651 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4652 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4653 | uint32_t policyFlags = args.policyFlags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4654 | policyFlags |= POLICY_FLAG_TRUSTED; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4655 | mPolicy.notifySwitch(args.eventTime, args.switchValues, args.switchMask, policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4656 | } |
| 4657 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4658 | void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4659 | if (debugInboundEventDetails()) { |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4660 | ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args.eventTime, |
| 4661 | args.deviceId); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4662 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4663 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4664 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4665 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4666 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4667 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4668 | std::unique_ptr<DeviceResetEntry> newEntry = |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4669 | std::make_unique<DeviceResetEntry>(args.id, args.eventTime, args.deviceId); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4670 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Siarhei Vishniakou | 1160ecd | 2023-06-28 15:57:47 -0700 | [diff] [blame] | 4671 | |
| 4672 | for (auto& [_, verifier] : mVerifiersByDisplay) { |
| 4673 | verifier.resetDevice(args.deviceId); |
| 4674 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4675 | } // release lock |
| 4676 | |
| 4677 | if (needWake) { |
| 4678 | mLooper->wake(); |
| 4679 | } |
| 4680 | } |
| 4681 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4682 | void InputDispatcher::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4683 | if (debugInboundEventDetails()) { |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4684 | ALOGD("notifyPointerCaptureChanged - eventTime=%" PRId64 ", enabled=%s", args.eventTime, |
| 4685 | args.request.enable ? "true" : "false"); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4686 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4687 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4688 | bool needWake = false; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4689 | { // acquire lock |
| 4690 | std::scoped_lock _l(mLock); |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4691 | auto entry = |
| 4692 | std::make_unique<PointerCaptureChangedEntry>(args.id, args.eventTime, args.request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4693 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 4694 | } // release lock |
| 4695 | |
| 4696 | if (needWake) { |
| 4697 | mLooper->wake(); |
| 4698 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4699 | } |
| 4700 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4701 | InputEventInjectionResult InputDispatcher::injectInputEvent(const InputEvent* event, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 4702 | std::optional<gui::Uid> targetUid, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4703 | InputEventInjectionSync syncMode, |
| 4704 | std::chrono::milliseconds timeout, |
| 4705 | uint32_t policyFlags) { |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4706 | Result<void> eventValidation = validateInputEvent(*event); |
| 4707 | if (!eventValidation.ok()) { |
| 4708 | LOG(INFO) << "Injection failed: invalid event: " << eventValidation.error(); |
| 4709 | return InputEventInjectionResult::FAILED; |
| 4710 | } |
| 4711 | |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4712 | if (debugInboundEventDetails()) { |
Siarhei Vishniakou | 827d1ac | 2023-07-21 16:37:51 -0700 | [diff] [blame] | 4713 | LOG(INFO) << __func__ << ": targetUid=" << toString(targetUid, &uidString) |
| 4714 | << ", syncMode=" << ftl::enum_string(syncMode) << ", timeout=" << timeout.count() |
| 4715 | << "ms, policyFlags=0x" << std::hex << policyFlags << std::dec |
| 4716 | << ", event=" << *event; |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4717 | } |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 4718 | 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] | 4719 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4720 | policyFlags |= POLICY_FLAG_INJECTED | POLICY_FLAG_TRUSTED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4721 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4722 | // 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] | 4723 | // that have gone through the InputFilter. If the event passed through the InputFilter, assign |
| 4724 | // the provided device id. If the InputFilter is accessibility, and it modifies or synthesizes |
| 4725 | // the injected event, it is responsible for setting POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY. |
| 4726 | // For those events, we will set FLAG_IS_ACCESSIBILITY_EVENT to allow apps to distinguish them |
| 4727 | // from events that originate from actual hardware. |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 4728 | DeviceId resolvedDeviceId = VIRTUAL_KEYBOARD_ID; |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4729 | if (policyFlags & POLICY_FLAG_FILTERED) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4730 | resolvedDeviceId = event->getDeviceId(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4731 | } |
| 4732 | |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4733 | const bool isAsync = syncMode == InputEventInjectionSync::NONE; |
| 4734 | auto injectionState = std::make_shared<InjectionState>(targetUid, isAsync); |
| 4735 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4736 | std::queue<std::unique_ptr<EventEntry>> injectedEntries; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4737 | switch (event->getType()) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4738 | case InputEventType::KEY: { |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4739 | const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event); |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4740 | const int32_t action = incomingKey.getAction(); |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4741 | int32_t flags = incomingKey.getFlags(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4742 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4743 | flags |= AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4744 | } |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4745 | int32_t keyCode = incomingKey.getKeyCode(); |
| 4746 | int32_t metaState = incomingKey.getMetaState(); |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4747 | KeyEvent keyEvent; |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4748 | keyEvent.initialize(incomingKey.getId(), resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4749 | incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode, |
| 4750 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), |
| 4751 | incomingKey.getDownTime(), incomingKey.getEventTime()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4752 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4753 | if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) { |
| 4754 | policyFlags |= POLICY_FLAG_VIRTUAL; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4755 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4756 | |
| 4757 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 4758 | android::base::Timer t; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4759 | mPolicy.interceptKeyBeforeQueueing(keyEvent, /*byref*/ policyFlags); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4760 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4761 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
| 4762 | std::to_string(t.duration().count()).c_str()); |
| 4763 | } |
| 4764 | } |
| 4765 | |
| 4766 | mLock.lock(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4767 | std::unique_ptr<KeyEntry> injectedEntry = |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4768 | std::make_unique<KeyEntry>(incomingKey.getId(), injectionState, |
| 4769 | incomingKey.getEventTime(), resolvedDeviceId, |
| 4770 | incomingKey.getSource(), incomingKey.getDisplayId(), |
| 4771 | policyFlags, action, flags, keyCode, |
| 4772 | incomingKey.getScanCode(), metaState, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4773 | incomingKey.getRepeatCount(), |
| 4774 | incomingKey.getDownTime()); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 4775 | if (mTracer) { |
| 4776 | injectedEntry->traceTracker = mTracer->traceInboundEvent(*injectedEntry); |
| 4777 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4778 | injectedEntries.push(std::move(injectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4779 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4780 | } |
| 4781 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4782 | case InputEventType::MOTION: { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4783 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4784 | const bool isPointerEvent = |
| 4785 | isFromSource(event->getSource(), AINPUT_SOURCE_CLASS_POINTER); |
| 4786 | // If a pointer event has no displayId specified, inject it to the default display. |
| 4787 | const uint32_t displayId = isPointerEvent && (event->getDisplayId() == ADISPLAY_ID_NONE) |
| 4788 | ? ADISPLAY_ID_DEFAULT |
| 4789 | : event->getDisplayId(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4790 | int32_t flags = motionEvent.getFlags(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4791 | |
| 4792 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4793 | nsecs_t eventTime = motionEvent.getEventTime(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4794 | android::base::Timer t; |
Yeabkal Wubshit | 88a9041 | 2023-12-21 18:23:04 -0800 | [diff] [blame] | 4795 | mPolicy.interceptMotionBeforeQueueing(displayId, motionEvent.getSource(), |
| 4796 | motionEvent.getAction(), eventTime, |
| 4797 | /*byref*/ policyFlags); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4798 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4799 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
| 4800 | std::to_string(t.duration().count()).c_str()); |
| 4801 | } |
| 4802 | } |
| 4803 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4804 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4805 | flags |= AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4806 | } |
| 4807 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4808 | mLock.lock(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4809 | const nsecs_t* sampleEventTimes = motionEvent.getSampleEventTimes(); |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4810 | const size_t pointerCount = motionEvent.getPointerCount(); |
| 4811 | const std::vector<PointerProperties> |
| 4812 | pointerProperties(motionEvent.getPointerProperties(), |
| 4813 | motionEvent.getPointerProperties() + pointerCount); |
| 4814 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4815 | const PointerCoords* samplePointerCoords = motionEvent.getSamplePointerCoords(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4816 | std::unique_ptr<MotionEntry> injectedEntry = |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4817 | std::make_unique<MotionEntry>(motionEvent.getId(), injectionState, |
| 4818 | *sampleEventTimes, resolvedDeviceId, |
| 4819 | motionEvent.getSource(), displayId, policyFlags, |
| 4820 | motionEvent.getAction(), |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4821 | motionEvent.getActionButton(), flags, |
| 4822 | motionEvent.getMetaState(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4823 | motionEvent.getButtonState(), |
| 4824 | motionEvent.getClassification(), |
| 4825 | motionEvent.getEdgeFlags(), |
| 4826 | motionEvent.getXPrecision(), |
| 4827 | motionEvent.getYPrecision(), |
| 4828 | motionEvent.getRawXCursorPosition(), |
| 4829 | motionEvent.getRawYCursorPosition(), |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4830 | motionEvent.getDownTime(), pointerProperties, |
| 4831 | std::vector<PointerCoords>(samplePointerCoords, |
| 4832 | samplePointerCoords + |
| 4833 | pointerCount)); |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4834 | transformMotionEntryForInjectionLocked(*injectedEntry, motionEvent.getTransform()); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 4835 | if (mTracer) { |
| 4836 | injectedEntry->traceTracker = mTracer->traceInboundEvent(*injectedEntry); |
| 4837 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4838 | injectedEntries.push(std::move(injectedEntry)); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4839 | for (size_t i = motionEvent.getHistorySize(); i > 0; i--) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4840 | sampleEventTimes += 1; |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4841 | samplePointerCoords += motionEvent.getPointerCount(); |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4842 | std::unique_ptr<MotionEntry> nextInjectedEntry = std::make_unique< |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4843 | MotionEntry>(motionEvent.getId(), injectionState, *sampleEventTimes, |
| 4844 | resolvedDeviceId, motionEvent.getSource(), displayId, |
| 4845 | policyFlags, motionEvent.getAction(), |
| 4846 | motionEvent.getActionButton(), flags, |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4847 | motionEvent.getMetaState(), motionEvent.getButtonState(), |
| 4848 | motionEvent.getClassification(), motionEvent.getEdgeFlags(), |
| 4849 | motionEvent.getXPrecision(), motionEvent.getYPrecision(), |
| 4850 | motionEvent.getRawXCursorPosition(), |
| 4851 | motionEvent.getRawYCursorPosition(), motionEvent.getDownTime(), |
| 4852 | pointerProperties, |
| 4853 | std::vector<PointerCoords>(samplePointerCoords, |
| 4854 | samplePointerCoords + |
| 4855 | pointerCount)); |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4856 | transformMotionEntryForInjectionLocked(*nextInjectedEntry, |
| 4857 | motionEvent.getTransform()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4858 | injectedEntries.push(std::move(nextInjectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4859 | } |
| 4860 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4861 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4862 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4863 | default: |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4864 | LOG(WARNING) << "Cannot inject " << ftl::enum_string(event->getType()) << " events"; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4865 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4866 | } |
| 4867 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4868 | bool needWake = false; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4869 | while (!injectedEntries.empty()) { |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 4870 | if (DEBUG_INJECTION) { |
Siarhei Vishniakou | 827d1ac | 2023-07-21 16:37:51 -0700 | [diff] [blame] | 4871 | LOG(INFO) << "Injecting " << injectedEntries.front()->getDescription(); |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 4872 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4873 | needWake |= enqueueInboundEventLocked(std::move(injectedEntries.front())); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4874 | injectedEntries.pop(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4875 | } |
| 4876 | |
| 4877 | mLock.unlock(); |
| 4878 | |
| 4879 | if (needWake) { |
| 4880 | mLooper->wake(); |
| 4881 | } |
| 4882 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4883 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4884 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4885 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4886 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4887 | if (syncMode == InputEventInjectionSync::NONE) { |
| 4888 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4889 | } else { |
| 4890 | for (;;) { |
| 4891 | injectionResult = injectionState->injectionResult; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4892 | if (injectionResult != InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4893 | break; |
| 4894 | } |
| 4895 | |
| 4896 | nsecs_t remainingTimeout = endTime - now(); |
| 4897 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4898 | if (DEBUG_INJECTION) { |
| 4899 | ALOGD("injectInputEvent - Timed out waiting for injection result " |
| 4900 | "to become available."); |
| 4901 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4902 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4903 | break; |
| 4904 | } |
| 4905 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4906 | mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4907 | } |
| 4908 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4909 | if (injectionResult == InputEventInjectionResult::SUCCEEDED && |
| 4910 | syncMode == InputEventInjectionSync::WAIT_FOR_FINISHED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4911 | while (injectionState->pendingForegroundDispatches != 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4912 | if (DEBUG_INJECTION) { |
| 4913 | ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", |
| 4914 | injectionState->pendingForegroundDispatches); |
| 4915 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4916 | nsecs_t remainingTimeout = endTime - now(); |
| 4917 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4918 | if (DEBUG_INJECTION) { |
| 4919 | ALOGD("injectInputEvent - Timed out waiting for pending foreground " |
| 4920 | "dispatches to finish."); |
| 4921 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4922 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4923 | break; |
| 4924 | } |
| 4925 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4926 | mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4927 | } |
| 4928 | } |
| 4929 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4930 | } // release lock |
| 4931 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4932 | if (DEBUG_INJECTION) { |
Siarhei Vishniakou | 827d1ac | 2023-07-21 16:37:51 -0700 | [diff] [blame] | 4933 | LOG(INFO) << "injectInputEvent - Finished with result " |
| 4934 | << ftl::enum_string(injectionResult); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4935 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4936 | |
| 4937 | return injectionResult; |
| 4938 | } |
| 4939 | |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4940 | std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4941 | std::array<uint8_t, 32> calculatedHmac; |
| 4942 | std::unique_ptr<VerifiedInputEvent> result; |
| 4943 | switch (event.getType()) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4944 | case InputEventType::KEY: { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4945 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event); |
| 4946 | VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent); |
| 4947 | result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4948 | calculatedHmac = sign(verifiedKeyEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4949 | break; |
| 4950 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4951 | case InputEventType::MOTION: { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4952 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event); |
| 4953 | VerifiedMotionEvent verifiedMotionEvent = |
| 4954 | verifiedMotionEventFromMotionEvent(motionEvent); |
| 4955 | result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4956 | calculatedHmac = sign(verifiedMotionEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4957 | break; |
| 4958 | } |
| 4959 | default: { |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 4960 | LOG(ERROR) << "Cannot verify events of type " << ftl::enum_string(event.getType()); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4961 | return nullptr; |
| 4962 | } |
| 4963 | } |
| 4964 | if (calculatedHmac == INVALID_HMAC) { |
| 4965 | return nullptr; |
| 4966 | } |
tyiu | 1573a67 | 2023-02-21 22:38:32 +0000 | [diff] [blame] | 4967 | if (0 != CRYPTO_memcmp(calculatedHmac.data(), event.getHmac().data(), calculatedHmac.size())) { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4968 | return nullptr; |
| 4969 | } |
| 4970 | return result; |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4971 | } |
| 4972 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 4973 | void InputDispatcher::setInjectionResult(const EventEntry& entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4974 | InputEventInjectionResult injectionResult) { |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4975 | if (!entry.injectionState) { |
| 4976 | // Not an injected event. |
| 4977 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4978 | } |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4979 | |
| 4980 | InjectionState& injectionState = *entry.injectionState; |
| 4981 | if (DEBUG_INJECTION) { |
| 4982 | LOG(INFO) << "Setting input event injection result to " |
| 4983 | << ftl::enum_string(injectionResult); |
| 4984 | } |
| 4985 | |
| 4986 | if (injectionState.injectionIsAsync && !(entry.policyFlags & POLICY_FLAG_FILTERED)) { |
| 4987 | // Log the outcome since the injector did not wait for the injection result. |
| 4988 | switch (injectionResult) { |
| 4989 | case InputEventInjectionResult::SUCCEEDED: |
| 4990 | ALOGV("Asynchronous input event injection succeeded."); |
| 4991 | break; |
| 4992 | case InputEventInjectionResult::TARGET_MISMATCH: |
| 4993 | ALOGV("Asynchronous input event injection target mismatch."); |
| 4994 | break; |
| 4995 | case InputEventInjectionResult::FAILED: |
| 4996 | ALOGW("Asynchronous input event injection failed."); |
| 4997 | break; |
| 4998 | case InputEventInjectionResult::TIMED_OUT: |
| 4999 | ALOGW("Asynchronous input event injection timed out."); |
| 5000 | break; |
| 5001 | case InputEventInjectionResult::PENDING: |
| 5002 | ALOGE("Setting result to 'PENDING' for asynchronous injection"); |
| 5003 | break; |
| 5004 | } |
| 5005 | } |
| 5006 | |
| 5007 | injectionState.injectionResult = injectionResult; |
| 5008 | mInjectionResultAvailable.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5009 | } |
| 5010 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 5011 | void InputDispatcher::transformMotionEntryForInjectionLocked( |
| 5012 | MotionEntry& entry, const ui::Transform& injectedTransform) const { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 5013 | // Input injection works in the logical display coordinate space, but the input pipeline works |
| 5014 | // display space, so we need to transform the injected events accordingly. |
| 5015 | const auto it = mDisplayInfos.find(entry.displayId); |
| 5016 | if (it == mDisplayInfos.end()) return; |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 5017 | const auto& transformToDisplay = it->second.transform.inverse() * injectedTransform; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 5018 | |
Prabir Pradhan | d9a2ebe | 2022-07-20 19:25:13 +0000 | [diff] [blame] | 5019 | if (entry.xCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION && |
| 5020 | entry.yCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) { |
| 5021 | const vec2 cursor = |
| 5022 | MotionEvent::calculateTransformedXY(entry.source, transformToDisplay, |
| 5023 | {entry.xCursorPosition, entry.yCursorPosition}); |
| 5024 | entry.xCursorPosition = cursor.x; |
| 5025 | entry.yCursorPosition = cursor.y; |
| 5026 | } |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 5027 | for (uint32_t i = 0; i < entry.getPointerCount(); i++) { |
Prabir Pradhan | 8e6ce22 | 2022-02-24 09:08:54 -0800 | [diff] [blame] | 5028 | entry.pointerCoords[i] = |
| 5029 | MotionEvent::calculateTransformedCoords(entry.source, transformToDisplay, |
| 5030 | entry.pointerCoords[i]); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 5031 | } |
| 5032 | } |
| 5033 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 5034 | void InputDispatcher::incrementPendingForegroundDispatches(const EventEntry& entry) { |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 5035 | if (entry.injectionState) { |
| 5036 | entry.injectionState->pendingForegroundDispatches += 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5037 | } |
| 5038 | } |
| 5039 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 5040 | void InputDispatcher::decrementPendingForegroundDispatches(const EventEntry& entry) { |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 5041 | if (entry.injectionState) { |
| 5042 | entry.injectionState->pendingForegroundDispatches -= 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5043 | |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 5044 | if (entry.injectionState->pendingForegroundDispatches == 0) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5045 | mInjectionSyncFinished.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5046 | } |
| 5047 | } |
| 5048 | } |
| 5049 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5050 | const std::vector<sp<WindowInfoHandle>>& InputDispatcher::getWindowHandlesLocked( |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5051 | int32_t displayId) const { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5052 | static const std::vector<sp<WindowInfoHandle>> EMPTY_WINDOW_HANDLES; |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5053 | auto it = mWindowHandlesByDisplay.find(displayId); |
| 5054 | return it != mWindowHandlesByDisplay.end() ? it->second : EMPTY_WINDOW_HANDLES; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5055 | } |
| 5056 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5057 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 5058 | const sp<IBinder>& windowHandleToken, std::optional<int32_t> displayId) const { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 5059 | if (windowHandleToken == nullptr) { |
| 5060 | return nullptr; |
| 5061 | } |
| 5062 | |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 5063 | if (!displayId) { |
| 5064 | // Look through all displays. |
| 5065 | for (auto& it : mWindowHandlesByDisplay) { |
| 5066 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 5067 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 5068 | if (windowHandle->getToken() == windowHandleToken) { |
| 5069 | return windowHandle; |
| 5070 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5071 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5072 | } |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5073 | return nullptr; |
| 5074 | } |
| 5075 | |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 5076 | // Only look through the requested display. |
| 5077 | for (const sp<WindowInfoHandle>& windowHandle : getWindowHandlesLocked(*displayId)) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5078 | if (windowHandle->getToken() == windowHandleToken) { |
| 5079 | return windowHandle; |
| 5080 | } |
| 5081 | } |
| 5082 | return nullptr; |
| 5083 | } |
| 5084 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5085 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
| 5086 | const sp<WindowInfoHandle>& windowHandle) const { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 5087 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5088 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 5089 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 5090 | if (handle->getId() == windowHandle->getId() && |
| 5091 | handle->getToken() == windowHandle->getToken()) { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 5092 | if (windowHandle->getInfo()->displayId != it.first) { |
| 5093 | ALOGE("Found window %s in display %" PRId32 |
| 5094 | ", but it should belong to display %" PRId32, |
| 5095 | windowHandle->getName().c_str(), it.first, |
| 5096 | windowHandle->getInfo()->displayId); |
| 5097 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 5098 | return handle; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5099 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5100 | } |
| 5101 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 5102 | return nullptr; |
| 5103 | } |
| 5104 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5105 | sp<WindowInfoHandle> InputDispatcher::getFocusedWindowHandleLocked(int displayId) const { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 5106 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(displayId); |
| 5107 | return getWindowHandleLocked(focusedToken, displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5108 | } |
| 5109 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5110 | ui::Transform InputDispatcher::getTransformLocked(int32_t displayId) const { |
| 5111 | auto displayInfoIt = mDisplayInfos.find(displayId); |
| 5112 | return displayInfoIt != mDisplayInfos.end() ? displayInfoIt->second.transform |
| 5113 | : kIdentityTransform; |
| 5114 | } |
| 5115 | |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5116 | bool InputDispatcher::canWindowReceiveMotionLocked(const sp<WindowInfoHandle>& window, |
| 5117 | const MotionEntry& motionEntry) const { |
| 5118 | const WindowInfo& info = *window->getInfo(); |
| 5119 | |
| 5120 | // Skip spy window targets that are not valid for targeted injection. |
| 5121 | if (const auto err = verifyTargetedInjection(window, motionEntry); err) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5122 | return false; |
| 5123 | } |
| 5124 | |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5125 | if (info.inputConfig.test(WindowInfo::InputConfig::PAUSE_DISPATCHING)) { |
| 5126 | ALOGI("Not sending touch event to %s because it is paused", window->getName().c_str()); |
| 5127 | return false; |
| 5128 | } |
| 5129 | |
| 5130 | if (info.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) { |
| 5131 | ALOGW("Not sending touch gesture to %s because it has config NO_INPUT_CHANNEL", |
| 5132 | window->getName().c_str()); |
| 5133 | return false; |
| 5134 | } |
| 5135 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 5136 | std::shared_ptr<Connection> connection = getConnectionLocked(window->getToken()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5137 | if (connection == nullptr) { |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5138 | ALOGW("Not sending touch to %s because there's no corresponding connection", |
| 5139 | window->getName().c_str()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5140 | return false; |
| 5141 | } |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5142 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5143 | if (!connection->responsive) { |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5144 | 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] | 5145 | return false; |
| 5146 | } |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5147 | |
| 5148 | // Drop events that can't be trusted due to occlusion |
| 5149 | const auto [x, y] = resolveTouchedPosition(motionEntry); |
| 5150 | TouchOcclusionInfo occlusionInfo = computeTouchOcclusionInfoLocked(window, x, y); |
| 5151 | if (!isTouchTrustedLocked(occlusionInfo)) { |
| 5152 | if (DEBUG_TOUCH_OCCLUSION) { |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 5153 | ALOGD("Stack of obscuring windows during untrusted touch (%.1f, %.1f):", x, y); |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5154 | for (const auto& log : occlusionInfo.debugInfo) { |
| 5155 | ALOGD("%s", log.c_str()); |
| 5156 | } |
| 5157 | } |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 5158 | ALOGW("Dropping untrusted touch event due to %s/%s", occlusionInfo.obscuringPackage.c_str(), |
| 5159 | occlusionInfo.obscuringUid.toString().c_str()); |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5160 | return false; |
| 5161 | } |
| 5162 | |
| 5163 | // Drop touch events if requested by input feature |
| 5164 | if (shouldDropInput(motionEntry, window)) { |
| 5165 | return false; |
| 5166 | } |
| 5167 | |
Siarhei Vishniakou | f77f60a | 2023-10-23 17:26:05 -0700 | [diff] [blame] | 5168 | // Ignore touches if stylus is down anywhere on screen |
| 5169 | if (info.inputConfig.test(WindowInfo::InputConfig::GLOBAL_STYLUS_BLOCKS_TOUCH) && |
| 5170 | isStylusActiveInDisplay(info.displayId, mTouchStatesByDisplay)) { |
| 5171 | LOG(INFO) << "Dropping touch from " << window->getName() << " because stylus is active"; |
| 5172 | return false; |
| 5173 | } |
| 5174 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5175 | return true; |
| 5176 | } |
| 5177 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5178 | void InputDispatcher::updateWindowHandlesForDisplayLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5179 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
| 5180 | if (windowInfoHandles.empty()) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5181 | // Remove all handles on a display if there are no windows left. |
| 5182 | mWindowHandlesByDisplay.erase(displayId); |
| 5183 | return; |
| 5184 | } |
| 5185 | |
| 5186 | // Since we compare the pointer of input window handles across window updates, we need |
| 5187 | // 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] | 5188 | const std::vector<sp<WindowInfoHandle>>& oldHandles = getWindowHandlesLocked(displayId); |
| 5189 | std::unordered_map<int32_t /*id*/, sp<WindowInfoHandle>> oldHandlesById; |
| 5190 | for (const sp<WindowInfoHandle>& handle : oldHandles) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5191 | oldHandlesById[handle->getId()] = handle; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5192 | } |
| 5193 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5194 | std::vector<sp<WindowInfoHandle>> newHandles; |
| 5195 | for (const sp<WindowInfoHandle>& handle : windowInfoHandles) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5196 | const WindowInfo* info = handle->getInfo(); |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 5197 | if (getConnectionLocked(handle->getToken()) == nullptr) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5198 | const bool noInputChannel = |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 5199 | info->inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5200 | const bool canReceiveInput = |
| 5201 | !info->inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE) || |
| 5202 | !info->inputConfig.test(WindowInfo::InputConfig::NOT_FOCUSABLE); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5203 | if (canReceiveInput && !noInputChannel) { |
John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 5204 | ALOGV("Window handle %s has no registered input channel", |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5205 | handle->getName().c_str()); |
Robert Carr | 2984b7a | 2020-04-13 17:06:45 -0700 | [diff] [blame] | 5206 | continue; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5207 | } |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5208 | } |
| 5209 | |
| 5210 | if (info->displayId != displayId) { |
| 5211 | ALOGE("Window %s updated by wrong display %d, should belong to display %d", |
| 5212 | handle->getName().c_str(), displayId, info->displayId); |
| 5213 | continue; |
| 5214 | } |
| 5215 | |
Robert Carr | edd1360 | 2020-04-13 17:24:34 -0700 | [diff] [blame] | 5216 | if ((oldHandlesById.find(handle->getId()) != oldHandlesById.end()) && |
| 5217 | (oldHandlesById.at(handle->getId())->getToken() == handle->getToken())) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5218 | const sp<WindowInfoHandle>& oldHandle = oldHandlesById.at(handle->getId()); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5219 | oldHandle->updateFrom(handle); |
| 5220 | newHandles.push_back(oldHandle); |
| 5221 | } else { |
| 5222 | newHandles.push_back(handle); |
| 5223 | } |
| 5224 | } |
| 5225 | |
| 5226 | // Insert or replace |
| 5227 | mWindowHandlesByDisplay[displayId] = newHandles; |
| 5228 | } |
| 5229 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5230 | /** |
| 5231 | * Called from InputManagerService, update window handle list by displayId that can receive input. |
| 5232 | * A window handle contains information about InputChannel, Touch Region, Types, Focused,... |
| 5233 | * If set an empty list, remove all handles from the specific display. |
| 5234 | * For focused handle, check if need to change and send a cancel event to previous one. |
| 5235 | * For removed handle, check if need to send a cancel event if already in touch. |
| 5236 | */ |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5237 | void InputDispatcher::setInputWindowsLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5238 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5239 | if (DEBUG_FOCUS) { |
| 5240 | std::string windowList; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5241 | for (const sp<WindowInfoHandle>& iwh : windowInfoHandles) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5242 | windowList += iwh->getName() + " "; |
| 5243 | } |
Siarhei Vishniakou | 366fb5b | 2023-12-06 11:23:41 -0800 | [diff] [blame] | 5244 | LOG(INFO) << "setInputWindows displayId=" << displayId << " " << windowList; |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5245 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5246 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 5247 | // Check preconditions for new input windows |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5248 | for (const sp<WindowInfoHandle>& window : windowInfoHandles) { |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 5249 | const WindowInfo& info = *window->getInfo(); |
| 5250 | |
| 5251 | // 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] | 5252 | const bool noInputWindow = info.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5253 | if (noInputWindow && window->getToken() != nullptr) { |
| 5254 | ALOGE("%s has feature NO_INPUT_WINDOW, but a non-null token. Clearing", |
| 5255 | window->getName().c_str()); |
| 5256 | window->releaseChannel(); |
| 5257 | } |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 5258 | |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 5259 | // Ensure all spy windows are trusted overlays |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5260 | LOG_ALWAYS_FATAL_IF(info.isSpy() && |
| 5261 | !info.inputConfig.test( |
| 5262 | WindowInfo::InputConfig::TRUSTED_OVERLAY), |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 5263 | "%s has feature SPY, but is not a trusted overlay.", |
| 5264 | window->getName().c_str()); |
| 5265 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 5266 | // Ensure all stylus interceptors are trusted overlays |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5267 | LOG_ALWAYS_FATAL_IF(info.interceptsStylus() && |
| 5268 | !info.inputConfig.test( |
| 5269 | WindowInfo::InputConfig::TRUSTED_OVERLAY), |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 5270 | "%s has feature INTERCEPTS_STYLUS, but is not a trusted overlay.", |
| 5271 | window->getName().c_str()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5272 | } |
| 5273 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5274 | // Copy old handles for release if they are no longer present. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5275 | const std::vector<sp<WindowInfoHandle>> oldWindowHandles = getWindowHandlesLocked(displayId); |
Prabir Pradhan | 4b9b1a1 | 2024-02-02 00:16:17 +0000 | [diff] [blame] | 5276 | const sp<WindowInfoHandle> removedFocusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5277 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5278 | updateWindowHandlesForDisplayLocked(windowInfoHandles, displayId); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5279 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5280 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5281 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5282 | std::optional<FocusResolver::FocusChanges> changes = |
| 5283 | mFocusResolver.setInputWindows(displayId, windowHandles); |
| 5284 | if (changes) { |
Prabir Pradhan | 4b9b1a1 | 2024-02-02 00:16:17 +0000 | [diff] [blame] | 5285 | onFocusChangedLocked(*changes, removedFocusedWindowHandle); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5286 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5287 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5288 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 5289 | mTouchStatesByDisplay.find(displayId); |
| 5290 | if (stateIt != mTouchStatesByDisplay.end()) { |
| 5291 | TouchState& state = stateIt->second; |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5292 | for (size_t i = 0; i < state.windows.size();) { |
| 5293 | TouchedWindow& touchedWindow = state.windows[i]; |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 5294 | if (getWindowHandleLocked(touchedWindow.windowHandle) == nullptr) { |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 5295 | LOG(INFO) << "Touched window was removed: " << touchedWindow.windowHandle->getName() |
| 5296 | << " in display %" << displayId; |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 5297 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
| 5298 | "touched window was removed"); |
| 5299 | synthesizeCancelationEventsForWindowLocked(touchedWindow.windowHandle, options); |
| 5300 | // Since we are about to drop the touch, cancel the events for the wallpaper as |
| 5301 | // well. |
| 5302 | if (touchedWindow.targetFlags.test(InputTarget::Flags::FOREGROUND) && |
| 5303 | touchedWindow.windowHandle->getInfo()->inputConfig.test( |
| 5304 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER)) { |
| 5305 | if (const auto& ww = state.getWallpaperWindow(); ww) { |
| 5306 | synthesizeCancelationEventsForWindowLocked(ww, options); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5307 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5308 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5309 | state.windows.erase(state.windows.begin() + i); |
| 5310 | } else { |
| 5311 | ++i; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5312 | } |
| 5313 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5314 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5315 | // 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] | 5316 | // could just clear the state here. |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 5317 | if (mDragState && mDragState->dragWindow->getInfo()->displayId == displayId && |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5318 | std::find(windowHandles.begin(), windowHandles.end(), mDragState->dragWindow) == |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5319 | windowHandles.end()) { |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 5320 | ALOGI("Drag window went away: %s", mDragState->dragWindow->getName().c_str()); |
| 5321 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5322 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5323 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5324 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 5325 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5326 | // Release information for windows that are no longer present. |
| 5327 | // This ensures that unused input channels are released promptly. |
| 5328 | // Otherwise, they might stick around until the window handle is destroyed |
| 5329 | // which might not happen until the next GC. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5330 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 5331 | if (getWindowHandleLocked(oldWindowHandle) == nullptr) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5332 | if (DEBUG_FOCUS) { |
| 5333 | ALOGD("Window went away: %s", oldWindowHandle->getName().c_str()); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 5334 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5335 | oldWindowHandle->releaseChannel(); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 5336 | } |
chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 5337 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5338 | } |
| 5339 | |
| 5340 | void InputDispatcher::setFocusedApplication( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5341 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5342 | if (DEBUG_FOCUS) { |
| 5343 | ALOGD("setFocusedApplication displayId=%" PRId32 " %s", displayId, |
| 5344 | inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>"); |
| 5345 | } |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 5346 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5347 | std::scoped_lock _l(mLock); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 5348 | setFocusedApplicationLocked(displayId, inputApplicationHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5349 | } // release lock |
| 5350 | |
| 5351 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5352 | mLooper->wake(); |
| 5353 | } |
| 5354 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 5355 | void InputDispatcher::setFocusedApplicationLocked( |
| 5356 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
| 5357 | std::shared_ptr<InputApplicationHandle> oldFocusedApplicationHandle = |
| 5358 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 5359 | |
| 5360 | if (sharedPointersEqual(oldFocusedApplicationHandle, inputApplicationHandle)) { |
| 5361 | return; // This application is already focused. No need to wake up or change anything. |
| 5362 | } |
| 5363 | |
| 5364 | // Set the new application handle. |
| 5365 | if (inputApplicationHandle != nullptr) { |
| 5366 | mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle; |
| 5367 | } else { |
| 5368 | mFocusedApplicationHandlesByDisplay.erase(displayId); |
| 5369 | } |
| 5370 | |
| 5371 | // No matter what the old focused application was, stop waiting on it because it is |
| 5372 | // no longer focused. |
| 5373 | resetNoFocusedWindowTimeoutLocked(); |
| 5374 | } |
| 5375 | |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 5376 | void InputDispatcher::setMinTimeBetweenUserActivityPokes(std::chrono::milliseconds interval) { |
| 5377 | if (interval.count() < 0) { |
| 5378 | LOG_ALWAYS_FATAL("Minimum time between user activity pokes should be >= 0"); |
| 5379 | } |
| 5380 | std::scoped_lock _l(mLock); |
| 5381 | mMinTimeBetweenUserActivityPokes = interval; |
| 5382 | } |
| 5383 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5384 | /** |
| 5385 | * Sets the focused display, which is responsible for receiving focus-dispatched input events where |
| 5386 | * the display not specified. |
| 5387 | * |
| 5388 | * We track any unreleased events for each window. If a window loses the ability to receive the |
| 5389 | * released event, we will send a cancel event to it. So when the focused display is changed, we |
| 5390 | * cancel all the unreleased display-unspecified events for the focused window on the old focused |
| 5391 | * display. The display-specified events won't be affected. |
| 5392 | */ |
| 5393 | void InputDispatcher::setFocusedDisplay(int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5394 | if (DEBUG_FOCUS) { |
| 5395 | ALOGD("setFocusedDisplay displayId=%" PRId32, displayId); |
| 5396 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5397 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5398 | std::scoped_lock _l(mLock); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5399 | |
| 5400 | if (mFocusedDisplayId != displayId) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5401 | sp<IBinder> oldFocusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5402 | mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5403 | if (oldFocusedWindowToken != nullptr) { |
Prabir Pradhan | 4b9b1a1 | 2024-02-02 00:16:17 +0000 | [diff] [blame] | 5404 | const auto windowHandle = |
| 5405 | getWindowHandleLocked(oldFocusedWindowToken, mFocusedDisplayId); |
| 5406 | if (windowHandle == nullptr) { |
| 5407 | LOG(FATAL) << __func__ << ": Previously focused token did not have a window"; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5408 | } |
Prabir Pradhan | 4b9b1a1 | 2024-02-02 00:16:17 +0000 | [diff] [blame] | 5409 | CancelationOptions |
| 5410 | options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, |
| 5411 | "The display which contains this window no longer has focus."); |
| 5412 | options.displayId = ADISPLAY_ID_NONE; |
| 5413 | synthesizeCancelationEventsForWindowLocked(windowHandle, options); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5414 | } |
| 5415 | mFocusedDisplayId = displayId; |
| 5416 | |
Chris Ye | 3c2d6f5 | 2020-08-09 10:39:48 -0700 | [diff] [blame] | 5417 | // Find new focused window and validate |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5418 | sp<IBinder> newFocusedWindowToken = mFocusResolver.getFocusedWindowToken(displayId); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5419 | sendFocusChangedCommandLocked(oldFocusedWindowToken, newFocusedWindowToken); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5420 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5421 | if (newFocusedWindowToken == nullptr) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5422 | ALOGW("Focused display #%" PRId32 " does not have a focused window.", displayId); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5423 | if (mFocusResolver.hasFocusedWindowTokens()) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5424 | ALOGE("But another display has a focused window\n%s", |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5425 | mFocusResolver.dumpFocusedWindows().c_str()); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5426 | } |
| 5427 | } |
| 5428 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5429 | } // release lock |
| 5430 | |
| 5431 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5432 | mLooper->wake(); |
| 5433 | } |
| 5434 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5435 | void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5436 | if (DEBUG_FOCUS) { |
| 5437 | ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); |
| 5438 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5439 | |
| 5440 | bool changed; |
| 5441 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5442 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5443 | |
| 5444 | if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { |
| 5445 | if (mDispatchFrozen && !frozen) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5446 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5447 | } |
| 5448 | |
| 5449 | if (mDispatchEnabled && !enabled) { |
| 5450 | resetAndDropEverythingLocked("dispatcher is being disabled"); |
| 5451 | } |
| 5452 | |
| 5453 | mDispatchEnabled = enabled; |
| 5454 | mDispatchFrozen = frozen; |
| 5455 | changed = true; |
| 5456 | } else { |
| 5457 | changed = false; |
| 5458 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5459 | } // release lock |
| 5460 | |
| 5461 | if (changed) { |
| 5462 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5463 | mLooper->wake(); |
| 5464 | } |
| 5465 | } |
| 5466 | |
| 5467 | void InputDispatcher::setInputFilterEnabled(bool enabled) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5468 | if (DEBUG_FOCUS) { |
| 5469 | ALOGD("setInputFilterEnabled: enabled=%d", enabled); |
| 5470 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5471 | |
| 5472 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5473 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5474 | |
| 5475 | if (mInputFilterEnabled == enabled) { |
| 5476 | return; |
| 5477 | } |
| 5478 | |
| 5479 | mInputFilterEnabled = enabled; |
| 5480 | resetAndDropEverythingLocked("input filter is being enabled or disabled"); |
| 5481 | } // release lock |
| 5482 | |
| 5483 | // Wake up poll loop since there might be work to do to drop everything. |
| 5484 | mLooper->wake(); |
| 5485 | } |
| 5486 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5487 | bool InputDispatcher::setInTouchMode(bool inTouchMode, gui::Pid pid, gui::Uid uid, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 5488 | bool hasPermission, int32_t displayId) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5489 | bool needWake = false; |
| 5490 | { |
| 5491 | std::scoped_lock lock(mLock); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5492 | ALOGD_IF(DEBUG_TOUCH_MODE, |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5493 | "Request to change touch mode to %s (calling pid=%s, uid=%s, " |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5494 | "hasPermission=%s, target displayId=%d, mTouchModePerDisplay[displayId]=%s)", |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5495 | toString(inTouchMode), pid.toString().c_str(), uid.toString().c_str(), |
| 5496 | toString(hasPermission), displayId, |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5497 | mTouchModePerDisplay.count(displayId) == 0 |
| 5498 | ? "not set" |
| 5499 | : std::to_string(mTouchModePerDisplay[displayId]).c_str()); |
| 5500 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5501 | auto touchModeIt = mTouchModePerDisplay.find(displayId); |
| 5502 | if (touchModeIt != mTouchModePerDisplay.end() && touchModeIt->second == inTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5503 | return false; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5504 | } |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5505 | if (!hasPermission) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 5506 | if (!focusedWindowIsOwnedByLocked(pid, uid) && |
| 5507 | !recentWindowsAreOwnedByLocked(pid, uid)) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5508 | 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] | 5509 | "window nor none of the previously interacted window", |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5510 | pid.toString().c_str(), uid.toString().c_str()); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5511 | return false; |
| 5512 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5513 | } |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5514 | mTouchModePerDisplay[displayId] = inTouchMode; |
| 5515 | auto entry = std::make_unique<TouchModeEntry>(mIdGenerator.nextId(), now(), inTouchMode, |
| 5516 | displayId); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5517 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 5518 | } // release lock |
| 5519 | |
| 5520 | if (needWake) { |
| 5521 | mLooper->wake(); |
| 5522 | } |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5523 | return true; |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 5524 | } |
| 5525 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5526 | bool InputDispatcher::focusedWindowIsOwnedByLocked(gui::Pid pid, gui::Uid uid) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 5527 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
| 5528 | if (focusedToken == nullptr) { |
| 5529 | return false; |
| 5530 | } |
| 5531 | sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(focusedToken); |
| 5532 | return isWindowOwnedBy(windowHandle, pid, uid); |
| 5533 | } |
| 5534 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5535 | bool InputDispatcher::recentWindowsAreOwnedByLocked(gui::Pid pid, gui::Uid uid) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 5536 | return std::find_if(mInteractionConnectionTokens.begin(), mInteractionConnectionTokens.end(), |
| 5537 | [&](const sp<IBinder>& connectionToken) REQUIRES(mLock) { |
| 5538 | const sp<WindowInfoHandle> windowHandle = |
| 5539 | getWindowHandleLocked(connectionToken); |
| 5540 | return isWindowOwnedBy(windowHandle, pid, uid); |
| 5541 | }) != mInteractionConnectionTokens.end(); |
| 5542 | } |
| 5543 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 5544 | void InputDispatcher::setMaximumObscuringOpacityForTouch(float opacity) { |
| 5545 | if (opacity < 0 || opacity > 1) { |
| 5546 | LOG_ALWAYS_FATAL("Maximum obscuring opacity for touch should be >= 0 and <= 1"); |
| 5547 | return; |
| 5548 | } |
| 5549 | |
| 5550 | std::scoped_lock lock(mLock); |
| 5551 | mMaximumObscuringOpacityForTouch = opacity; |
| 5552 | } |
| 5553 | |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5554 | std::tuple<TouchState*, TouchedWindow*, int32_t /*displayId*/> |
| 5555 | InputDispatcher::findTouchStateWindowAndDisplayLocked(const sp<IBinder>& token) { |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5556 | for (auto& [displayId, state] : mTouchStatesByDisplay) { |
| 5557 | for (TouchedWindow& w : state.windows) { |
| 5558 | if (w.windowHandle->getToken() == token) { |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5559 | return std::make_tuple(&state, &w, displayId); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5560 | } |
| 5561 | } |
| 5562 | } |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5563 | return std::make_tuple(nullptr, nullptr, ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5564 | } |
| 5565 | |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 5566 | bool InputDispatcher::transferTouchGesture(const sp<IBinder>& fromToken, const sp<IBinder>& toToken, |
| 5567 | bool isDragDrop) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 5568 | if (fromToken == toToken) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5569 | if (DEBUG_FOCUS) { |
| 5570 | ALOGD("Trivial transfer to same window."); |
| 5571 | } |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 5572 | return true; |
| 5573 | } |
| 5574 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5575 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5576 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5577 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5578 | // Find the target touch state and touched window by fromToken. |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5579 | auto [state, touchedWindow, displayId] = findTouchStateWindowAndDisplayLocked(fromToken); |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5580 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5581 | if (state == nullptr || touchedWindow == nullptr) { |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5582 | ALOGD("Touch transfer failed because from window is not being touched."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5583 | return false; |
| 5584 | } |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5585 | std::set<int32_t> deviceIds = touchedWindow->getTouchingDeviceIds(); |
| 5586 | if (deviceIds.size() != 1) { |
Siarhei Vishniakou | 827d1ac | 2023-07-21 16:37:51 -0700 | [diff] [blame] | 5587 | LOG(INFO) << "Can't transfer touch. Currently touching devices: " << dumpSet(deviceIds) |
| 5588 | << " for window: " << touchedWindow->dump(); |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5589 | return false; |
| 5590 | } |
| 5591 | const int32_t deviceId = *deviceIds.begin(); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5592 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 5593 | const sp<WindowInfoHandle> fromWindowHandle = touchedWindow->windowHandle; |
| 5594 | const sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(toToken, displayId); |
| 5595 | if (!toWindowHandle) { |
| 5596 | ALOGW("Cannot transfer touch because the transfer target window was not found."); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5597 | return false; |
| 5598 | } |
| 5599 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5600 | if (DEBUG_FOCUS) { |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 5601 | ALOGD("%s: fromWindowHandle=%s, toWindowHandle=%s", __func__, |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5602 | touchedWindow->windowHandle->getName().c_str(), |
| 5603 | toWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5604 | } |
| 5605 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5606 | // Erase old window. |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5607 | ftl::Flags<InputTarget::Flags> oldTargetFlags = touchedWindow->targetFlags; |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 5608 | std::vector<PointerProperties> pointers = touchedWindow->getTouchingPointers(deviceId); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5609 | state->removeWindowByToken(fromToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5610 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5611 | // Add new window. |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 5612 | nsecs_t downTimeInTarget = now(); |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5613 | ftl::Flags<InputTarget::Flags> newTargetFlags = |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 5614 | oldTargetFlags & (InputTarget::Flags::SPLIT); |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 5615 | if (canReceiveForegroundTouches(*toWindowHandle->getInfo())) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5616 | newTargetFlags |= InputTarget::Flags::FOREGROUND; |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 5617 | } |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 5618 | // Transferring touch focus using this API should not effect the focused window. |
| 5619 | newTargetFlags |= InputTarget::Flags::NO_FOCUS_CHANGE; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 5620 | state->addOrUpdateWindow(toWindowHandle, InputTarget::DispatchMode::AS_IS, newTargetFlags, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 5621 | deviceId, pointers, downTimeInTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5622 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5623 | // Store the dragging window. |
| 5624 | if (isDragDrop) { |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 5625 | if (pointers.size() != 1) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 5626 | ALOGW("The drag and drop cannot be started when there is no pointer or more than 1" |
| 5627 | " pointer on the window."); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 5628 | return false; |
| 5629 | } |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 5630 | // Track the pointer id for drag window and generate the drag state. |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 5631 | const size_t id = pointers.begin()->id; |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 5632 | mDragState = std::make_unique<DragState>(toWindowHandle, id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5633 | } |
| 5634 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5635 | // Synthesize cancel for old window and down for new window. |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 5636 | std::shared_ptr<Connection> fromConnection = getConnectionLocked(fromToken); |
| 5637 | std::shared_ptr<Connection> toConnection = getConnectionLocked(toToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5638 | if (fromConnection != nullptr && toConnection != nullptr) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5639 | fromConnection->inputState.mergePointerStateTo(toConnection->inputState); |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5640 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
| 5641 | "transferring touch from this window to another window"); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 5642 | synthesizeCancelationEventsForWindowLocked(fromWindowHandle, options, fromConnection); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5643 | synthesizePointerDownEventsForConnectionLocked(downTimeInTarget, toConnection, |
| 5644 | newTargetFlags); |
| 5645 | |
| 5646 | // Check if the wallpaper window should deliver the corresponding event. |
| 5647 | transferWallpaperTouch(oldTargetFlags, newTargetFlags, fromWindowHandle, toWindowHandle, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 5648 | *state, deviceId, pointers); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5649 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5650 | } // release lock |
| 5651 | |
| 5652 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5653 | mLooper->wake(); |
| 5654 | return true; |
| 5655 | } |
| 5656 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5657 | /** |
| 5658 | * Get the touched foreground window on the given display. |
| 5659 | * Return null if there are no windows touched on that display, or if more than one foreground |
| 5660 | * window is being touched. |
| 5661 | */ |
| 5662 | sp<WindowInfoHandle> InputDispatcher::findTouchedForegroundWindowLocked(int32_t displayId) const { |
| 5663 | auto stateIt = mTouchStatesByDisplay.find(displayId); |
| 5664 | if (stateIt == mTouchStatesByDisplay.end()) { |
| 5665 | ALOGI("No touch state on display %" PRId32, displayId); |
| 5666 | return nullptr; |
| 5667 | } |
| 5668 | |
| 5669 | const TouchState& state = stateIt->second; |
| 5670 | sp<WindowInfoHandle> touchedForegroundWindow; |
| 5671 | // If multiple foreground windows are touched, return nullptr |
| 5672 | for (const TouchedWindow& window : state.windows) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5673 | if (window.targetFlags.test(InputTarget::Flags::FOREGROUND)) { |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5674 | if (touchedForegroundWindow != nullptr) { |
| 5675 | ALOGI("Two or more foreground windows: %s and %s", |
| 5676 | touchedForegroundWindow->getName().c_str(), |
| 5677 | window.windowHandle->getName().c_str()); |
| 5678 | return nullptr; |
| 5679 | } |
| 5680 | touchedForegroundWindow = window.windowHandle; |
| 5681 | } |
| 5682 | } |
| 5683 | return touchedForegroundWindow; |
| 5684 | } |
| 5685 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5686 | // Binder call |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 5687 | bool InputDispatcher::transferTouchOnDisplay(const sp<IBinder>& destChannelToken, |
| 5688 | int32_t displayId) { |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5689 | sp<IBinder> fromToken; |
| 5690 | { // acquire lock |
| 5691 | std::scoped_lock _l(mLock); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5692 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(destChannelToken, displayId); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5693 | if (toWindowHandle == nullptr) { |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5694 | ALOGW("Could not find window associated with token=%p on display %" PRId32, |
| 5695 | destChannelToken.get(), displayId); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5696 | return false; |
| 5697 | } |
| 5698 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5699 | sp<WindowInfoHandle> from = findTouchedForegroundWindowLocked(displayId); |
| 5700 | if (from == nullptr) { |
| 5701 | ALOGE("Could not find a source window in %s for %p", __func__, destChannelToken.get()); |
| 5702 | return false; |
| 5703 | } |
| 5704 | |
| 5705 | fromToken = from->getToken(); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5706 | } // release lock |
| 5707 | |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 5708 | return transferTouchGesture(fromToken, destChannelToken); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5709 | } |
| 5710 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5711 | void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5712 | if (DEBUG_FOCUS) { |
| 5713 | ALOGD("Resetting and dropping all events (%s).", reason); |
| 5714 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5715 | |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 5716 | CancelationOptions options(CancelationOptions::Mode::CANCEL_ALL_EVENTS, reason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5717 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 5718 | |
| 5719 | resetKeyRepeatLocked(); |
| 5720 | releasePendingEventLocked(); |
| 5721 | drainInboundQueueLocked(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5722 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5723 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5724 | mAnrTracker.clear(); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5725 | mTouchStatesByDisplay.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5726 | } |
| 5727 | |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 5728 | void InputDispatcher::logDispatchStateLocked() const { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5729 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5730 | dumpDispatchStateLocked(dump); |
| 5731 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5732 | std::istringstream stream(dump); |
| 5733 | std::string line; |
| 5734 | |
| 5735 | while (std::getline(stream, line, '\n')) { |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 5736 | ALOGI("%s", line.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5737 | } |
| 5738 | } |
| 5739 | |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 5740 | std::string InputDispatcher::dumpPointerCaptureStateLocked() const { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5741 | std::string dump; |
| 5742 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5743 | dump += StringPrintf(INDENT "Pointer Capture Requested: %s\n", |
| 5744 | toString(mCurrentPointerCaptureRequest.enable)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5745 | |
| 5746 | std::string windowName = "None"; |
| 5747 | if (mWindowTokenWithPointerCapture) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5748 | const sp<WindowInfoHandle> captureWindowHandle = |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5749 | getWindowHandleLocked(mWindowTokenWithPointerCapture); |
| 5750 | windowName = captureWindowHandle ? captureWindowHandle->getName().c_str() |
| 5751 | : "token has capture without window"; |
| 5752 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5753 | 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] | 5754 | |
| 5755 | return dump; |
| 5756 | } |
| 5757 | |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 5758 | void InputDispatcher::dumpDispatchStateLocked(std::string& dump) const { |
Siarhei Vishniakou | 043a3ec | 2019-05-01 11:30:46 -0700 | [diff] [blame] | 5759 | dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled)); |
| 5760 | dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen)); |
| 5761 | dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5762 | dump += StringPrintf(INDENT "FocusedDisplayId: %" PRId32 "\n", mFocusedDisplayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5763 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5764 | if (!mFocusedApplicationHandlesByDisplay.empty()) { |
| 5765 | dump += StringPrintf(INDENT "FocusedApplications:\n"); |
| 5766 | for (auto& it : mFocusedApplicationHandlesByDisplay) { |
| 5767 | const int32_t displayId = it.first; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5768 | const std::shared_ptr<InputApplicationHandle>& applicationHandle = it.second; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5769 | const std::chrono::duration timeout = |
| 5770 | applicationHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5771 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5772 | ", name='%s', dispatchingTimeout=%" PRId64 "ms\n", |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5773 | displayId, applicationHandle->getName().c_str(), millis(timeout)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5774 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5775 | } else { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5776 | dump += StringPrintf(INDENT "FocusedApplications: <none>\n"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5777 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5778 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5779 | dump += mFocusResolver.dump(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5780 | dump += dumpPointerCaptureStateLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5781 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5782 | if (!mTouchStatesByDisplay.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5783 | dump += StringPrintf(INDENT "TouchStatesByDisplay:\n"); |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5784 | for (const auto& [displayId, state] : mTouchStatesByDisplay) { |
Siarhei Vishniakou | 6e1e987 | 2022-11-08 17:51:35 -0800 | [diff] [blame] | 5785 | std::string touchStateDump = addLinePrefix(state.dump(), INDENT2); |
| 5786 | dump += INDENT2 + std::to_string(displayId) + " : " + touchStateDump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5787 | } |
| 5788 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5789 | dump += INDENT "TouchStates: <no displays touched>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5790 | } |
| 5791 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5792 | if (mDragState) { |
| 5793 | dump += StringPrintf(INDENT "DragState:\n"); |
| 5794 | mDragState->dump(dump, INDENT2); |
| 5795 | } |
| 5796 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5797 | if (!mWindowHandlesByDisplay.empty()) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5798 | for (const auto& [displayId, windowHandles] : mWindowHandlesByDisplay) { |
| 5799 | dump += StringPrintf(INDENT "Display: %" PRId32 "\n", displayId); |
| 5800 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
| 5801 | const auto& displayInfo = it->second; |
| 5802 | dump += StringPrintf(INDENT2 "logicalSize=%dx%d\n", displayInfo.logicalWidth, |
| 5803 | displayInfo.logicalHeight); |
| 5804 | displayInfo.transform.dump(dump, "transform", INDENT4); |
| 5805 | } else { |
| 5806 | dump += INDENT2 "No DisplayInfo found!\n"; |
| 5807 | } |
| 5808 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5809 | if (!windowHandles.empty()) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5810 | dump += INDENT2 "Windows:\n"; |
| 5811 | for (size_t i = 0; i < windowHandles.size(); i++) { |
Siarhei Vishniakou | 366fb5b | 2023-12-06 11:23:41 -0800 | [diff] [blame] | 5812 | dump += StringPrintf(INDENT3 "%zu: %s", i, |
| 5813 | streamableToString(*windowHandles[i]).c_str()); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5814 | } |
| 5815 | } else { |
| 5816 | dump += INDENT2 "Windows: <none>\n"; |
| 5817 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5818 | } |
| 5819 | } else { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5820 | dump += INDENT "Displays: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5821 | } |
| 5822 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5823 | if (!mGlobalMonitorsByDisplay.empty()) { |
| 5824 | for (const auto& [displayId, monitors] : mGlobalMonitorsByDisplay) { |
| 5825 | dump += StringPrintf(INDENT "Global monitors on display %d:\n", displayId); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5826 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5827 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5828 | } else { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5829 | dump += INDENT "Global Monitors: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5830 | } |
| 5831 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5832 | const nsecs_t currentTime = now(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5833 | |
| 5834 | // Dump recently dispatched or dropped events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5835 | if (!mRecentQueue.empty()) { |
| 5836 | dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size()); |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 5837 | for (const std::shared_ptr<const EventEntry>& entry : mRecentQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5838 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5839 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5840 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5841 | } |
| 5842 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5843 | dump += INDENT "RecentQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5844 | } |
| 5845 | |
| 5846 | // Dump event currently being dispatched. |
| 5847 | if (mPendingEvent) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5848 | dump += INDENT "PendingEvent:\n"; |
| 5849 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5850 | dump += mPendingEvent->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5851 | dump += StringPrintf(", age=%" PRId64 "ms\n", |
| 5852 | ns2ms(currentTime - mPendingEvent->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5853 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5854 | dump += INDENT "PendingEvent: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5855 | } |
| 5856 | |
| 5857 | // Dump inbound events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5858 | if (!mInboundQueue.empty()) { |
| 5859 | dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size()); |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 5860 | for (const std::shared_ptr<const EventEntry>& entry : mInboundQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5861 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5862 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5863 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5864 | } |
| 5865 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5866 | dump += INDENT "InboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5867 | } |
| 5868 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5869 | if (!mCommandQueue.empty()) { |
| 5870 | dump += StringPrintf(INDENT "CommandQueue: size=%zu\n", mCommandQueue.size()); |
| 5871 | } else { |
| 5872 | dump += INDENT "CommandQueue: <empty>\n"; |
| 5873 | } |
| 5874 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5875 | if (!mConnectionsByToken.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5876 | dump += INDENT "Connections:\n"; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5877 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 10bfdc9 | 2024-02-01 11:12:57 -0800 | [diff] [blame] | 5878 | dump += StringPrintf(INDENT2 "%i: channelName='%s', " |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5879 | "status=%s, monitor=%s, responsive=%s\n", |
Siarhei Vishniakou | 7b9f4f5 | 2024-02-02 13:07:16 -0800 | [diff] [blame] | 5880 | connection->inputPublisher.getChannel().getFd(), |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5881 | connection->getInputChannelName().c_str(), |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5882 | ftl::enum_string(connection->status).c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5883 | toString(connection->monitor), toString(connection->responsive)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5884 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5885 | if (!connection->outboundQueue.empty()) { |
| 5886 | dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n", |
| 5887 | connection->outboundQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5888 | dump += dumpQueue(connection->outboundQueue, currentTime); |
| 5889 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5890 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5891 | dump += INDENT3 "OutboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5892 | } |
| 5893 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5894 | if (!connection->waitQueue.empty()) { |
| 5895 | dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n", |
| 5896 | connection->waitQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5897 | dump += dumpQueue(connection->waitQueue, currentTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5898 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5899 | dump += INDENT3 "WaitQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5900 | } |
Siarhei Vishniakou | 366fb5b | 2023-12-06 11:23:41 -0800 | [diff] [blame] | 5901 | std::string inputStateDump = streamableToString(connection->inputState); |
| 5902 | if (!inputStateDump.empty()) { |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 5903 | dump += INDENT3 "InputState: "; |
Siarhei Vishniakou | 366fb5b | 2023-12-06 11:23:41 -0800 | [diff] [blame] | 5904 | dump += inputStateDump + "\n"; |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 5905 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5906 | } |
| 5907 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5908 | dump += INDENT "Connections: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5909 | } |
| 5910 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5911 | if (!mTouchModePerDisplay.empty()) { |
| 5912 | dump += INDENT "TouchModePerDisplay:\n"; |
| 5913 | for (const auto& [displayId, touchMode] : mTouchModePerDisplay) { |
| 5914 | dump += StringPrintf(INDENT2 "Display: %" PRId32 " TouchMode: %s\n", displayId, |
| 5915 | std::to_string(touchMode).c_str()); |
| 5916 | } |
| 5917 | } else { |
| 5918 | dump += INDENT "TouchModePerDisplay: <none>\n"; |
| 5919 | } |
| 5920 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5921 | dump += INDENT "Configuration:\n"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5922 | dump += StringPrintf(INDENT2 "KeyRepeatDelay: %" PRId64 "ms\n", ns2ms(mConfig.keyRepeatDelay)); |
| 5923 | dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n", |
| 5924 | ns2ms(mConfig.keyRepeatTimeout)); |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 5925 | dump += mLatencyTracker.dump(INDENT2); |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 5926 | dump += mLatencyAggregator.dump(INDENT2); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 5927 | dump += INDENT "InputTracer: "; |
| 5928 | dump += mTracer == nullptr ? "Disabled" : "Enabled"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5929 | } |
| 5930 | |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 5931 | void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) const { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5932 | const size_t numMonitors = monitors.size(); |
| 5933 | for (size_t i = 0; i < numMonitors; i++) { |
| 5934 | const Monitor& monitor = monitors[i]; |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 5935 | const std::shared_ptr<Connection>& connection = monitor.connection; |
| 5936 | dump += StringPrintf(INDENT2 "%zu: '%s', ", i, connection->getInputChannelName().c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5937 | dump += "\n"; |
| 5938 | } |
| 5939 | } |
| 5940 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5941 | class LooperEventCallback : public LooperCallback { |
| 5942 | public: |
| 5943 | LooperEventCallback(std::function<int(int events)> callback) : mCallback(callback) {} |
| 5944 | int handleEvent(int /*fd*/, int events, void* /*data*/) override { return mCallback(events); } |
| 5945 | |
| 5946 | private: |
| 5947 | std::function<int(int events)> mCallback; |
| 5948 | }; |
| 5949 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5950 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputChannel(const std::string& name) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5951 | if (DEBUG_CHANNEL_CREATION) { |
| 5952 | ALOGD("channel '%s' ~ createInputChannel", name.c_str()); |
| 5953 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5954 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5955 | std::unique_ptr<InputChannel> serverChannel; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5956 | std::unique_ptr<InputChannel> clientChannel; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5957 | status_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5958 | |
| 5959 | if (result) { |
| 5960 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5961 | } |
| 5962 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5963 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5964 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5965 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Siarhei Vishniakou | 8d66013 | 2024-01-11 16:48:44 -0800 | [diff] [blame] | 5966 | const int fd = serverChannel->getFd(); |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 5967 | std::shared_ptr<Connection> connection = |
| 5968 | std::make_shared<Connection>(std::move(serverChannel), /*monitor=*/false, |
| 5969 | mIdGenerator); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5970 | |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 5971 | auto [_, inserted] = mConnectionsByToken.try_emplace(token, connection); |
| 5972 | if (!inserted) { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5973 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5974 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5975 | |
| 5976 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5977 | this, std::placeholders::_1, token); |
| 5978 | |
Siarhei Vishniakou | 8d66013 | 2024-01-11 16:48:44 -0800 | [diff] [blame] | 5979 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, sp<LooperEventCallback>::make(callback), |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5980 | nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5981 | } // release lock |
| 5982 | |
| 5983 | // Wake the looper because some connections have changed. |
| 5984 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5985 | return clientChannel; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5986 | } |
| 5987 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5988 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputMonitor(int32_t displayId, |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5989 | const std::string& name, |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5990 | gui::Pid pid) { |
Siarhei Vishniakou | 7b9f4f5 | 2024-02-02 13:07:16 -0800 | [diff] [blame] | 5991 | std::unique_ptr<InputChannel> serverChannel; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5992 | std::unique_ptr<InputChannel> clientChannel; |
Siarhei Vishniakou | 7b9f4f5 | 2024-02-02 13:07:16 -0800 | [diff] [blame] | 5993 | status_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5994 | if (result) { |
| 5995 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5996 | } |
| 5997 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5998 | { // acquire lock |
| 5999 | std::scoped_lock _l(mLock); |
| 6000 | |
| 6001 | if (displayId < 0) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 6002 | return base::Error(BAD_VALUE) << "Attempted to create input monitor with name " << name |
| 6003 | << " without a specified display."; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6004 | } |
| 6005 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 6006 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Siarhei Vishniakou | 8d66013 | 2024-01-11 16:48:44 -0800 | [diff] [blame] | 6007 | const int fd = serverChannel->getFd(); |
Siarhei Vishniakou | 7b9f4f5 | 2024-02-02 13:07:16 -0800 | [diff] [blame] | 6008 | std::shared_ptr<Connection> connection = |
| 6009 | std::make_shared<Connection>(std::move(serverChannel), /*monitor=*/true, |
| 6010 | mIdGenerator); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 6011 | |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6012 | auto [_, inserted] = mConnectionsByToken.emplace(token, connection); |
| 6013 | if (!inserted) { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 6014 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 6015 | } |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6016 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 6017 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 6018 | this, std::placeholders::_1, token); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6019 | |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6020 | mGlobalMonitorsByDisplay[displayId].emplace_back(connection, pid); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6021 | |
Siarhei Vishniakou | 8d66013 | 2024-01-11 16:48:44 -0800 | [diff] [blame] | 6022 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, sp<LooperEventCallback>::make(callback), |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6023 | nullptr); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6024 | } |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 6025 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6026 | // Wake the looper because some connections have changed. |
| 6027 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 6028 | return clientChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6029 | } |
| 6030 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 6031 | status_t InputDispatcher::removeInputChannel(const sp<IBinder>& connectionToken) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6032 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6033 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6034 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6035 | status_t status = removeInputChannelLocked(connectionToken, /*notify=*/false); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6036 | if (status) { |
| 6037 | return status; |
| 6038 | } |
| 6039 | } // release lock |
| 6040 | |
| 6041 | // Wake the poll loop because removing the connection may have changed the current |
| 6042 | // synchronization state. |
| 6043 | mLooper->wake(); |
| 6044 | return OK; |
| 6045 | } |
| 6046 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 6047 | status_t InputDispatcher::removeInputChannelLocked(const sp<IBinder>& connectionToken, |
| 6048 | bool notify) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6049 | std::shared_ptr<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 6050 | if (connection == nullptr) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 6051 | // 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] | 6052 | return BAD_VALUE; |
| 6053 | } |
| 6054 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6055 | removeConnectionLocked(connection); |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 6056 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6057 | if (connection->monitor) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 6058 | removeMonitorChannelLocked(connectionToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6059 | } |
| 6060 | |
Siarhei Vishniakou | 7b9f4f5 | 2024-02-02 13:07:16 -0800 | [diff] [blame] | 6061 | mLooper->removeFd(connection->inputPublisher.getChannel().getFd()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6062 | |
| 6063 | nsecs_t currentTime = now(); |
| 6064 | abortBrokenDispatchCycleLocked(currentTime, connection, notify); |
| 6065 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 6066 | connection->status = Connection::Status::ZOMBIE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6067 | return OK; |
| 6068 | } |
| 6069 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 6070 | void InputDispatcher::removeMonitorChannelLocked(const sp<IBinder>& connectionToken) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6071 | for (auto it = mGlobalMonitorsByDisplay.begin(); it != mGlobalMonitorsByDisplay.end();) { |
| 6072 | auto& [displayId, monitors] = *it; |
| 6073 | std::erase_if(monitors, [connectionToken](const Monitor& monitor) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6074 | return monitor.connection->getToken() == connectionToken; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6075 | }); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6076 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6077 | if (monitors.empty()) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6078 | it = mGlobalMonitorsByDisplay.erase(it); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6079 | } else { |
| 6080 | ++it; |
| 6081 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6082 | } |
| 6083 | } |
| 6084 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6085 | status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6086 | std::scoped_lock _l(mLock); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 6087 | return pilferPointersLocked(token); |
| 6088 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6089 | |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 6090 | status_t InputDispatcher::pilferPointersLocked(const sp<IBinder>& token) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6091 | const std::shared_ptr<Connection> requestingConnection = getConnectionLocked(token); |
| 6092 | if (!requestingConnection) { |
Siarhei Vishniakou | 8384e0d | 2023-09-18 18:48:27 -0700 | [diff] [blame] | 6093 | LOG(WARNING) |
| 6094 | << "Attempted to pilfer pointers from an un-registered channel or invalid token"; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6095 | return BAD_VALUE; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6096 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6097 | |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 6098 | auto [statePtr, windowPtr, displayId] = findTouchStateWindowAndDisplayLocked(token); |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 6099 | if (statePtr == nullptr || windowPtr == nullptr) { |
Siarhei Vishniakou | 8384e0d | 2023-09-18 18:48:27 -0700 | [diff] [blame] | 6100 | LOG(WARNING) |
| 6101 | << "Attempted to pilfer points from a channel without any on-going pointer streams." |
| 6102 | " Ignoring."; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6103 | return BAD_VALUE; |
| 6104 | } |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 6105 | std::set<int32_t> deviceIds = windowPtr->getTouchingDeviceIds(); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 6106 | if (deviceIds.empty()) { |
| 6107 | LOG(WARNING) << "Can't pilfer: no touching devices in window: " << windowPtr->dump(); |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 6108 | return BAD_VALUE; |
| 6109 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6110 | |
Siarhei Vishniakou | 8384e0d | 2023-09-18 18:48:27 -0700 | [diff] [blame] | 6111 | for (const DeviceId deviceId : deviceIds) { |
| 6112 | TouchState& state = *statePtr; |
| 6113 | TouchedWindow& window = *windowPtr; |
| 6114 | // Send cancel events to all the input channels we're stealing from. |
| 6115 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
| 6116 | "input channel stole pointer stream"); |
| 6117 | options.deviceId = deviceId; |
| 6118 | options.displayId = displayId; |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 6119 | std::vector<PointerProperties> pointers = window.getTouchingPointers(deviceId); |
| 6120 | std::bitset<MAX_POINTER_ID + 1> pointerIds = getPointerIds(pointers); |
Siarhei Vishniakou | 8384e0d | 2023-09-18 18:48:27 -0700 | [diff] [blame] | 6121 | options.pointerIds = pointerIds; |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 6122 | |
Siarhei Vishniakou | 8384e0d | 2023-09-18 18:48:27 -0700 | [diff] [blame] | 6123 | std::string canceledWindows; |
| 6124 | for (const TouchedWindow& w : state.windows) { |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 6125 | if (w.windowHandle->getToken() != token) { |
| 6126 | synthesizeCancelationEventsForWindowLocked(w.windowHandle, options); |
Siarhei Vishniakou | 8384e0d | 2023-09-18 18:48:27 -0700 | [diff] [blame] | 6127 | canceledWindows += canceledWindows.empty() ? "[" : ", "; |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 6128 | canceledWindows += w.windowHandle->getName(); |
Siarhei Vishniakou | 8384e0d | 2023-09-18 18:48:27 -0700 | [diff] [blame] | 6129 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6130 | } |
Siarhei Vishniakou | 8384e0d | 2023-09-18 18:48:27 -0700 | [diff] [blame] | 6131 | canceledWindows += canceledWindows.empty() ? "[]" : "]"; |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6132 | LOG(INFO) << "Channel " << requestingConnection->getInputChannelName() |
Siarhei Vishniakou | 8384e0d | 2023-09-18 18:48:27 -0700 | [diff] [blame] | 6133 | << " is stealing input gesture for device " << deviceId << " from " |
| 6134 | << canceledWindows; |
| 6135 | |
| 6136 | // Prevent the gesture from being sent to any other windows. |
| 6137 | // This only blocks relevant pointers to be sent to other windows |
| 6138 | window.addPilferingPointers(deviceId, pointerIds); |
| 6139 | |
| 6140 | state.cancelPointersForWindowsExcept(deviceId, pointerIds, token); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6141 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6142 | return OK; |
| 6143 | } |
| 6144 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6145 | void InputDispatcher::requestPointerCapture(const sp<IBinder>& windowToken, bool enabled) { |
| 6146 | { // acquire lock |
| 6147 | std::scoped_lock _l(mLock); |
| 6148 | if (DEBUG_FOCUS) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 6149 | const sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(windowToken); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6150 | ALOGI("Request to %s Pointer Capture from: %s.", enabled ? "enable" : "disable", |
| 6151 | windowHandle != nullptr ? windowHandle->getName().c_str() |
| 6152 | : "token without window"); |
| 6153 | } |
| 6154 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6155 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6156 | if (focusedToken != windowToken) { |
| 6157 | ALOGW("Ignoring request to %s Pointer Capture: window does not have focus.", |
| 6158 | enabled ? "enable" : "disable"); |
| 6159 | return; |
| 6160 | } |
| 6161 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6162 | if (enabled == mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6163 | ALOGW("Ignoring request to %s Pointer Capture: " |
| 6164 | "window has %s requested pointer capture.", |
| 6165 | enabled ? "enable" : "disable", enabled ? "already" : "not"); |
| 6166 | return; |
| 6167 | } |
| 6168 | |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 6169 | if (enabled) { |
| 6170 | if (std::find(mIneligibleDisplaysForPointerCapture.begin(), |
| 6171 | mIneligibleDisplaysForPointerCapture.end(), |
| 6172 | mFocusedDisplayId) != mIneligibleDisplaysForPointerCapture.end()) { |
| 6173 | ALOGW("Ignoring request to enable Pointer Capture: display is not eligible"); |
| 6174 | return; |
| 6175 | } |
| 6176 | } |
| 6177 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6178 | setPointerCaptureLocked(enabled); |
| 6179 | } // release lock |
| 6180 | |
| 6181 | // Wake the thread to process command entries. |
| 6182 | mLooper->wake(); |
| 6183 | } |
| 6184 | |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 6185 | void InputDispatcher::setDisplayEligibilityForPointerCapture(int32_t displayId, bool isEligible) { |
| 6186 | { // acquire lock |
| 6187 | std::scoped_lock _l(mLock); |
| 6188 | std::erase(mIneligibleDisplaysForPointerCapture, displayId); |
| 6189 | if (!isEligible) { |
| 6190 | mIneligibleDisplaysForPointerCapture.push_back(displayId); |
| 6191 | } |
| 6192 | } // release lock |
| 6193 | } |
| 6194 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6195 | std::optional<gui::Pid> InputDispatcher::findMonitorPidByTokenLocked(const sp<IBinder>& token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6196 | for (const auto& [_, monitors] : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6197 | for (const Monitor& monitor : monitors) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6198 | if (monitor.connection->getToken() == token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6199 | return monitor.pid; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6200 | } |
| 6201 | } |
| 6202 | } |
| 6203 | return std::nullopt; |
| 6204 | } |
| 6205 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6206 | std::shared_ptr<Connection> InputDispatcher::getConnectionLocked( |
| 6207 | const sp<IBinder>& inputConnectionToken) const { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 6208 | if (inputConnectionToken == nullptr) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 6209 | return nullptr; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 6210 | } |
| 6211 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 6212 | for (const auto& [token, connection] : mConnectionsByToken) { |
| 6213 | if (token == inputConnectionToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 6214 | return connection; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6215 | } |
| 6216 | } |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 6217 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 6218 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6219 | } |
| 6220 | |
Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 6221 | std::string InputDispatcher::getConnectionNameLocked(const sp<IBinder>& connectionToken) const { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6222 | std::shared_ptr<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 6223 | if (connection == nullptr) { |
| 6224 | return "<nullptr>"; |
| 6225 | } |
| 6226 | return connection->getInputChannelName(); |
| 6227 | } |
| 6228 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6229 | void InputDispatcher::removeConnectionLocked(const std::shared_ptr<Connection>& connection) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6230 | mAnrTracker.eraseToken(connection->getToken()); |
| 6231 | mConnectionsByToken.erase(connection->getToken()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6232 | } |
| 6233 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6234 | void InputDispatcher::doDispatchCycleFinishedCommand(nsecs_t finishTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6235 | const std::shared_ptr<Connection>& connection, |
| 6236 | uint32_t seq, bool handled, |
| 6237 | nsecs_t consumeTime) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6238 | // Handle post-event policy actions. |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 6239 | std::unique_ptr<const KeyEntry> fallbackKeyEntry; |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6240 | |
| 6241 | { // Start critical section |
| 6242 | auto dispatchEntryIt = |
| 6243 | std::find_if(connection->waitQueue.begin(), connection->waitQueue.end(), |
| 6244 | [seq](auto& e) { return e->seq == seq; }); |
| 6245 | if (dispatchEntryIt == connection->waitQueue.end()) { |
| 6246 | return; |
| 6247 | } |
| 6248 | |
| 6249 | DispatchEntry& dispatchEntry = **dispatchEntryIt; |
| 6250 | |
| 6251 | const nsecs_t eventDuration = finishTime - dispatchEntry.deliveryTime; |
| 6252 | if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) { |
Siarhei Vishniakou | 10bfdc9 | 2024-02-01 11:12:57 -0800 | [diff] [blame] | 6253 | ALOGI("%s spent %" PRId64 "ms processing %s", connection->getInputChannelName().c_str(), |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6254 | ns2ms(eventDuration), dispatchEntry.eventEntry->getDescription().c_str()); |
| 6255 | } |
| 6256 | if (shouldReportFinishedEvent(dispatchEntry, *connection)) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6257 | mLatencyTracker.trackFinishedEvent(dispatchEntry.eventEntry->id, connection->getToken(), |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6258 | dispatchEntry.deliveryTime, consumeTime, finishTime); |
| 6259 | } |
| 6260 | |
| 6261 | if (dispatchEntry.eventEntry->type == EventEntry::Type::KEY) { |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6262 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(*(dispatchEntry.eventEntry)); |
| 6263 | fallbackKeyEntry = |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6264 | afterKeyEventLockedInterruptable(connection, dispatchEntry, keyEntry, handled); |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6265 | } |
| 6266 | } // End critical section: The -LockedInterruptable methods may have released the lock. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6267 | |
| 6268 | // Dequeue the event and start the next cycle. |
| 6269 | // Because the lock might have been released, it is possible that the |
| 6270 | // contents of the wait queue to have been drained, so we need to double-check |
| 6271 | // a few things. |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6272 | auto entryIt = std::find_if(connection->waitQueue.begin(), connection->waitQueue.end(), |
| 6273 | [seq](auto& e) { return e->seq == seq; }); |
| 6274 | if (entryIt != connection->waitQueue.end()) { |
| 6275 | std::unique_ptr<DispatchEntry> dispatchEntry = std::move(*entryIt); |
| 6276 | connection->waitQueue.erase(entryIt); |
| 6277 | |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6278 | const sp<IBinder>& connectionToken = connection->getToken(); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6279 | mAnrTracker.erase(dispatchEntry->timeoutTime, connectionToken); |
| 6280 | if (!connection->responsive) { |
| 6281 | connection->responsive = isConnectionResponsive(*connection); |
| 6282 | if (connection->responsive) { |
| 6283 | // The connection was unresponsive, and now it's responsive. |
| 6284 | processConnectionResponsiveLocked(*connection); |
| 6285 | } |
| 6286 | } |
| 6287 | traceWaitQueueLength(*connection); |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6288 | if (fallbackKeyEntry && connection->status == Connection::Status::NORMAL) { |
Prabir Pradhan | fa2c69f | 2024-02-01 20:31:34 +0000 | [diff] [blame] | 6289 | const auto windowHandle = getWindowHandleLocked(connection->getToken()); |
| 6290 | // Only dispatch fallbacks if there is a window for the connection. |
| 6291 | if (windowHandle != nullptr) { |
| 6292 | const auto inputTarget = |
| 6293 | createInputTargetLocked(windowHandle, InputTarget::DispatchMode::AS_IS, |
| 6294 | dispatchEntry->targetFlags, |
| 6295 | fallbackKeyEntry->downTime); |
| 6296 | if (inputTarget.has_value()) { |
| 6297 | enqueueDispatchEntryLocked(connection, std::move(fallbackKeyEntry), |
| 6298 | *inputTarget); |
| 6299 | } |
| 6300 | } |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6301 | } |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6302 | releaseDispatchEntry(std::move(dispatchEntry)); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6303 | } |
| 6304 | |
| 6305 | // Start the next dispatch cycle for this connection. |
| 6306 | startDispatchCycleLocked(now(), connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6307 | } |
| 6308 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6309 | void InputDispatcher::sendFocusChangedCommandLocked(const sp<IBinder>& oldToken, |
| 6310 | const sp<IBinder>& newToken) { |
| 6311 | auto command = [this, oldToken, newToken]() REQUIRES(mLock) { |
| 6312 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6313 | mPolicy.notifyFocusChanged(oldToken, newToken); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6314 | }; |
| 6315 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6316 | } |
| 6317 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6318 | void InputDispatcher::sendDropWindowCommandLocked(const sp<IBinder>& token, float x, float y) { |
| 6319 | auto command = [this, token, x, y]() REQUIRES(mLock) { |
| 6320 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6321 | mPolicy.notifyDropWindow(token, x, y); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6322 | }; |
| 6323 | postCommandLocked(std::move(command)); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 6324 | } |
| 6325 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6326 | void InputDispatcher::onAnrLocked(const std::shared_ptr<Connection>& connection) { |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6327 | if (connection == nullptr) { |
| 6328 | LOG_ALWAYS_FATAL("Caller must check for nullness"); |
| 6329 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6330 | // Since we are allowing the policy to extend the timeout, maybe the waitQueue |
| 6331 | // is already healthy again. Don't raise ANR in this situation |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6332 | if (connection->waitQueue.empty()) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6333 | ALOGI("Not raising ANR because the connection %s has recovered", |
Siarhei Vishniakou | 10bfdc9 | 2024-02-01 11:12:57 -0800 | [diff] [blame] | 6334 | connection->getInputChannelName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6335 | return; |
| 6336 | } |
| 6337 | /** |
| 6338 | * The "oldestEntry" is the entry that was first sent to the application. That entry, however, |
| 6339 | * may not be the one that caused the timeout to occur. One possibility is that window timeout |
| 6340 | * has changed. This could cause newer entries to time out before the already dispatched |
| 6341 | * entries. In that situation, the newest entries caused ANR. But in all likelihood, the app |
| 6342 | * processes the events linearly. So providing information about the oldest entry seems to be |
| 6343 | * most useful. |
| 6344 | */ |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6345 | DispatchEntry& oldestEntry = *connection->waitQueue.front(); |
| 6346 | const nsecs_t currentWait = now() - oldestEntry.deliveryTime; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6347 | std::string reason = |
| 6348 | android::base::StringPrintf("%s is not responding. Waited %" PRId64 "ms for %s", |
Siarhei Vishniakou | 10bfdc9 | 2024-02-01 11:12:57 -0800 | [diff] [blame] | 6349 | connection->getInputChannelName().c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6350 | ns2ms(currentWait), |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6351 | oldestEntry.eventEntry->getDescription().c_str()); |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6352 | sp<IBinder> connectionToken = connection->getToken(); |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 6353 | updateLastAnrStateLocked(getWindowHandleLocked(connectionToken), reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6354 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6355 | processConnectionUnresponsiveLocked(*connection, std::move(reason)); |
| 6356 | |
| 6357 | // Stop waking up for events on this connection, it is already unresponsive |
| 6358 | cancelEventsForAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6359 | } |
| 6360 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6361 | void InputDispatcher::onAnrLocked(std::shared_ptr<InputApplicationHandle> application) { |
| 6362 | std::string reason = |
| 6363 | StringPrintf("%s does not have a focused window", application->getName().c_str()); |
| 6364 | updateLastAnrStateLocked(*application, reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6365 | |
Yabin Cui | 8eb9c55 | 2023-06-08 18:05:07 +0000 | [diff] [blame] | 6366 | auto command = [this, app = std::move(application)]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6367 | scoped_unlock unlock(mLock); |
Yabin Cui | ced952f | 2023-06-09 21:12:51 +0000 | [diff] [blame] | 6368 | mPolicy.notifyNoFocusedWindowAnr(app); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6369 | }; |
| 6370 | postCommandLocked(std::move(command)); |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 6371 | } |
| 6372 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 6373 | void InputDispatcher::updateLastAnrStateLocked(const sp<WindowInfoHandle>& window, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6374 | const std::string& reason) { |
| 6375 | const std::string windowLabel = getApplicationWindowLabel(nullptr, window); |
| 6376 | updateLastAnrStateLocked(windowLabel, reason); |
| 6377 | } |
| 6378 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6379 | void InputDispatcher::updateLastAnrStateLocked(const InputApplicationHandle& application, |
| 6380 | const std::string& reason) { |
| 6381 | const std::string windowLabel = getApplicationWindowLabel(&application, nullptr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6382 | updateLastAnrStateLocked(windowLabel, reason); |
| 6383 | } |
| 6384 | |
| 6385 | void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel, |
| 6386 | const std::string& reason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6387 | // 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] | 6388 | time_t t = time(nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6389 | struct tm tm; |
| 6390 | localtime_r(&t, &tm); |
| 6391 | char timestr[64]; |
| 6392 | strftime(timestr, sizeof(timestr), "%F %T", &tm); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6393 | mLastAnrState.clear(); |
| 6394 | mLastAnrState += INDENT "ANR:\n"; |
| 6395 | mLastAnrState += StringPrintf(INDENT2 "Time: %s\n", timestr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6396 | mLastAnrState += StringPrintf(INDENT2 "Reason: %s\n", reason.c_str()); |
| 6397 | mLastAnrState += StringPrintf(INDENT2 "Window: %s\n", windowLabel.c_str()); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6398 | dumpDispatchStateLocked(mLastAnrState); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6399 | } |
| 6400 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6401 | void InputDispatcher::doInterceptKeyBeforeDispatchingCommand(const sp<IBinder>& focusedWindowToken, |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 6402 | const KeyEntry& entry) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6403 | const KeyEvent event = createKeyEvent(entry); |
| 6404 | nsecs_t delay = 0; |
| 6405 | { // release lock |
| 6406 | scoped_unlock unlock(mLock); |
| 6407 | android::base::Timer t; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6408 | delay = mPolicy.interceptKeyBeforeDispatching(focusedWindowToken, event, entry.policyFlags); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6409 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 6410 | ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms", |
| 6411 | std::to_string(t.duration().count()).c_str()); |
| 6412 | } |
| 6413 | } // acquire lock |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6414 | |
| 6415 | if (delay < 0) { |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 6416 | entry.interceptKeyResult = KeyEntry::InterceptKeyResult::SKIP; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6417 | } else if (delay == 0) { |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 6418 | entry.interceptKeyResult = KeyEntry::InterceptKeyResult::CONTINUE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6419 | } else { |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 6420 | entry.interceptKeyResult = KeyEntry::InterceptKeyResult::TRY_AGAIN_LATER; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6421 | entry.interceptKeyWakeupTime = now() + delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6422 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6423 | } |
| 6424 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6425 | void InputDispatcher::sendWindowUnresponsiveCommandLocked(const sp<IBinder>& token, |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6426 | std::optional<gui::Pid> pid, |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6427 | std::string reason) { |
Yabin Cui | 8eb9c55 | 2023-06-08 18:05:07 +0000 | [diff] [blame] | 6428 | auto command = [this, token, pid, r = std::move(reason)]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6429 | scoped_unlock unlock(mLock); |
Yabin Cui | ced952f | 2023-06-09 21:12:51 +0000 | [diff] [blame] | 6430 | mPolicy.notifyWindowUnresponsive(token, pid, r); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6431 | }; |
| 6432 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6433 | } |
| 6434 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6435 | void InputDispatcher::sendWindowResponsiveCommandLocked(const sp<IBinder>& token, |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6436 | std::optional<gui::Pid> pid) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6437 | auto command = [this, token, pid]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6438 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6439 | mPolicy.notifyWindowResponsive(token, pid); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6440 | }; |
| 6441 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6442 | } |
| 6443 | |
| 6444 | /** |
| 6445 | * Tell the policy that a connection has become unresponsive so that it can start ANR. |
| 6446 | * Check whether the connection of interest is a monitor or a window, and add the corresponding |
| 6447 | * command entry to the command queue. |
| 6448 | */ |
| 6449 | void InputDispatcher::processConnectionUnresponsiveLocked(const Connection& connection, |
| 6450 | std::string reason) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6451 | const sp<IBinder>& connectionToken = connection.getToken(); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6452 | std::optional<gui::Pid> pid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6453 | if (connection.monitor) { |
Siarhei Vishniakou | 10bfdc9 | 2024-02-01 11:12:57 -0800 | [diff] [blame] | 6454 | ALOGW("Monitor %s is unresponsive: %s", connection.getInputChannelName().c_str(), |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6455 | reason.c_str()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6456 | pid = findMonitorPidByTokenLocked(connectionToken); |
| 6457 | } else { |
| 6458 | // The connection is a window |
Siarhei Vishniakou | 10bfdc9 | 2024-02-01 11:12:57 -0800 | [diff] [blame] | 6459 | ALOGW("Window %s is unresponsive: %s", connection.getInputChannelName().c_str(), |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6460 | reason.c_str()); |
| 6461 | const sp<WindowInfoHandle> handle = getWindowHandleLocked(connectionToken); |
| 6462 | if (handle != nullptr) { |
| 6463 | pid = handle->getInfo()->ownerPid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6464 | } |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6465 | } |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6466 | sendWindowUnresponsiveCommandLocked(connectionToken, pid, std::move(reason)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6467 | } |
| 6468 | |
| 6469 | /** |
| 6470 | * Tell the policy that a connection has become responsive so that it can stop ANR. |
| 6471 | */ |
| 6472 | void InputDispatcher::processConnectionResponsiveLocked(const Connection& connection) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6473 | const sp<IBinder>& connectionToken = connection.getToken(); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6474 | std::optional<gui::Pid> pid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6475 | if (connection.monitor) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6476 | pid = findMonitorPidByTokenLocked(connectionToken); |
| 6477 | } else { |
| 6478 | // The connection is a window |
| 6479 | const sp<WindowInfoHandle> handle = getWindowHandleLocked(connectionToken); |
| 6480 | if (handle != nullptr) { |
| 6481 | pid = handle->getInfo()->ownerPid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6482 | } |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6483 | } |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6484 | sendWindowResponsiveCommandLocked(connectionToken, pid); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6485 | } |
| 6486 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 6487 | std::unique_ptr<const KeyEntry> InputDispatcher::afterKeyEventLockedInterruptable( |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6488 | const std::shared_ptr<Connection>& connection, DispatchEntry& dispatchEntry, |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6489 | const KeyEntry& keyEntry, bool handled) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6490 | if (keyEntry.flags & AKEY_EVENT_FLAG_FALLBACK) { |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6491 | if (!handled) { |
| 6492 | // Report the key as unhandled, since the fallback was not handled. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6493 | mReporter->reportUnhandledKey(keyEntry.id); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6494 | } |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6495 | return {}; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6496 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6497 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6498 | // Get the fallback key state. |
| 6499 | // Clear it out after dispatching the UP. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6500 | int32_t originalKeyCode = keyEntry.keyCode; |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6501 | std::optional<int32_t> fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6502 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6503 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6504 | } |
| 6505 | |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6506 | if (handled || !dispatchEntry.hasForegroundTarget()) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6507 | // If the application handles the original key for which we previously |
| 6508 | // generated a fallback or if the window is not a foreground window, |
| 6509 | // then cancel the associated fallback key, if any. |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6510 | if (fallbackKeyCode) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6511 | // Dispatch the unhandled key to the policy with the cancel flag. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6512 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6513 | ALOGD("Unhandled key event: Asking policy to cancel fallback action. " |
| 6514 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6515 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, |
| 6516 | keyEntry.policyFlags); |
| 6517 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6518 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6519 | event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6520 | |
| 6521 | mLock.unlock(); |
| 6522 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6523 | if (const auto unhandledKeyFallback = |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6524 | mPolicy.dispatchUnhandledKey(connection->getToken(), event, |
| 6525 | keyEntry.policyFlags); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6526 | unhandledKeyFallback) { |
| 6527 | event = *unhandledKeyFallback; |
| 6528 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6529 | |
| 6530 | mLock.lock(); |
| 6531 | |
Prabir Pradhan | fa2c69f | 2024-02-01 20:31:34 +0000 | [diff] [blame] | 6532 | // Cancel the fallback key, but only if we still have a window for the channel. |
| 6533 | // It could have been removed during the policy call. |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6534 | if (*fallbackKeyCode != AKEYCODE_UNKNOWN) { |
Prabir Pradhan | fa2c69f | 2024-02-01 20:31:34 +0000 | [diff] [blame] | 6535 | const auto windowHandle = getWindowHandleLocked(connection->getToken()); |
| 6536 | if (windowHandle != nullptr) { |
| 6537 | CancelationOptions options(CancelationOptions::Mode::CANCEL_FALLBACK_EVENTS, |
| 6538 | "application handled the original non-fallback key " |
| 6539 | "or is no longer a foreground target, " |
| 6540 | "canceling previously dispatched fallback key"); |
| 6541 | options.keyCode = *fallbackKeyCode; |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 6542 | synthesizeCancelationEventsForWindowLocked(windowHandle, options, connection); |
Prabir Pradhan | fa2c69f | 2024-02-01 20:31:34 +0000 | [diff] [blame] | 6543 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6544 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6545 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6546 | } |
| 6547 | } else { |
| 6548 | // If the application did not handle a non-fallback key, first check |
| 6549 | // that we are in a good state to perform unhandled key event processing |
| 6550 | // Then ask the policy what to do with it. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6551 | bool initialDown = keyEntry.action == AKEY_EVENT_ACTION_DOWN && keyEntry.repeatCount == 0; |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6552 | if (!fallbackKeyCode && !initialDown) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6553 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6554 | ALOGD("Unhandled key event: Skipping unhandled key event processing " |
| 6555 | "since this is not an initial down. " |
| 6556 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6557 | originalKeyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 6558 | } |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6559 | return {}; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6560 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6561 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6562 | // Dispatch the unhandled key to the policy. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6563 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6564 | ALOGD("Unhandled key event: Asking policy to perform fallback action. " |
| 6565 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6566 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 6567 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6568 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6569 | |
| 6570 | mLock.unlock(); |
| 6571 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6572 | bool fallback = false; |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6573 | if (auto fb = mPolicy.dispatchUnhandledKey(connection->getToken(), event, |
| 6574 | keyEntry.policyFlags); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6575 | fb) { |
| 6576 | fallback = true; |
| 6577 | event = *fb; |
| 6578 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6579 | |
| 6580 | mLock.lock(); |
| 6581 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 6582 | if (connection->status != Connection::Status::NORMAL) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6583 | connection->inputState.removeFallbackKey(originalKeyCode); |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6584 | return {}; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6585 | } |
| 6586 | |
| 6587 | // Latch the fallback keycode for this key on an initial down. |
| 6588 | // The fallback keycode cannot change at any other point in the lifecycle. |
| 6589 | if (initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6590 | if (fallback) { |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6591 | *fallbackKeyCode = event.getKeyCode(); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6592 | } else { |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6593 | *fallbackKeyCode = AKEYCODE_UNKNOWN; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6594 | } |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6595 | connection->inputState.setFallbackKey(originalKeyCode, *fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6596 | } |
| 6597 | |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6598 | ALOG_ASSERT(fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6599 | |
| 6600 | // Cancel the fallback key if the policy decides not to send it anymore. |
| 6601 | // We will continue to dispatch the key to the policy but we will no |
| 6602 | // longer dispatch a fallback key to the application. |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6603 | if (*fallbackKeyCode != AKEYCODE_UNKNOWN && |
| 6604 | (!fallback || *fallbackKeyCode != event.getKeyCode())) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6605 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6606 | if (fallback) { |
| 6607 | ALOGD("Unhandled key event: Policy requested to send key %d" |
| 6608 | "as a fallback for %d, but on the DOWN it had requested " |
| 6609 | "to send %d instead. Fallback canceled.", |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6610 | event.getKeyCode(), originalKeyCode, *fallbackKeyCode); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6611 | } else { |
| 6612 | ALOGD("Unhandled key event: Policy did not request fallback for %d, " |
| 6613 | "but on the DOWN it had requested to send %d. " |
| 6614 | "Fallback canceled.", |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6615 | originalKeyCode, *fallbackKeyCode); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6616 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6617 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6618 | |
Prabir Pradhan | fa2c69f | 2024-02-01 20:31:34 +0000 | [diff] [blame] | 6619 | const auto windowHandle = getWindowHandleLocked(connection->getToken()); |
| 6620 | if (windowHandle != nullptr) { |
| 6621 | CancelationOptions options(CancelationOptions::Mode::CANCEL_FALLBACK_EVENTS, |
| 6622 | "canceling fallback, policy no longer desires it"); |
| 6623 | options.keyCode = *fallbackKeyCode; |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 6624 | synthesizeCancelationEventsForWindowLocked(windowHandle, options, connection); |
Prabir Pradhan | fa2c69f | 2024-02-01 20:31:34 +0000 | [diff] [blame] | 6625 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6626 | |
| 6627 | fallback = false; |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6628 | *fallbackKeyCode = AKEYCODE_UNKNOWN; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6629 | if (keyEntry.action != AKEY_EVENT_ACTION_UP) { |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6630 | connection->inputState.setFallbackKey(originalKeyCode, *fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6631 | } |
| 6632 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6633 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6634 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6635 | { |
| 6636 | std::string msg; |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6637 | const std::map<int32_t, int32_t>& fallbackKeys = |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6638 | connection->inputState.getFallbackKeys(); |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6639 | for (const auto& [key, value] : fallbackKeys) { |
| 6640 | msg += StringPrintf(", %d->%d", key, value); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6641 | } |
| 6642 | ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.", |
| 6643 | fallbackKeys.size(), msg.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6644 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6645 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6646 | |
| 6647 | if (fallback) { |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6648 | // Return the fallback key that we want dispatched to the channel. |
| 6649 | std::unique_ptr<KeyEntry> newEntry = |
| 6650 | std::make_unique<KeyEntry>(mIdGenerator.nextId(), keyEntry.injectionState, |
| 6651 | event.getEventTime(), event.getDeviceId(), |
| 6652 | event.getSource(), event.getDisplayId(), |
| 6653 | keyEntry.policyFlags, keyEntry.action, |
| 6654 | event.getFlags() | AKEY_EVENT_FLAG_FALLBACK, |
| 6655 | *fallbackKeyCode, event.getScanCode(), |
| 6656 | event.getMetaState(), event.getRepeatCount(), |
| 6657 | event.getDownTime()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6658 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6659 | ALOGD("Unhandled key event: Dispatching fallback key. " |
| 6660 | "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x", |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6661 | originalKeyCode, *fallbackKeyCode, keyEntry.metaState); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6662 | } |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6663 | return newEntry; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6664 | } else { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6665 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6666 | ALOGD("Unhandled key event: No fallback key."); |
| 6667 | } |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6668 | |
| 6669 | // Report the key as unhandled, since there is no fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6670 | mReporter->reportUnhandledKey(keyEntry.id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6671 | } |
| 6672 | } |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6673 | return {}; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6674 | } |
| 6675 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6676 | void InputDispatcher::traceInboundQueueLengthLocked() { |
| 6677 | if (ATRACE_ENABLED()) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 6678 | ATRACE_INT("iq", mInboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6679 | } |
| 6680 | } |
| 6681 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6682 | void InputDispatcher::traceOutboundQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6683 | if (ATRACE_ENABLED()) { |
| 6684 | char counterName[40]; |
Siarhei Vishniakou | 10bfdc9 | 2024-02-01 11:12:57 -0800 | [diff] [blame] | 6685 | snprintf(counterName, sizeof(counterName), "oq:%s", |
| 6686 | connection.getInputChannelName().c_str()); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6687 | ATRACE_INT(counterName, connection.outboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6688 | } |
| 6689 | } |
| 6690 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6691 | void InputDispatcher::traceWaitQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6692 | if (ATRACE_ENABLED()) { |
| 6693 | char counterName[40]; |
Siarhei Vishniakou | 10bfdc9 | 2024-02-01 11:12:57 -0800 | [diff] [blame] | 6694 | snprintf(counterName, sizeof(counterName), "wq:%s", |
| 6695 | connection.getInputChannelName().c_str()); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6696 | ATRACE_INT(counterName, connection.waitQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6697 | } |
| 6698 | } |
| 6699 | |
Siarhei Vishniakou | 5e20f27 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 6700 | void InputDispatcher::dump(std::string& dump) const { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6701 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6702 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6703 | dump += "Input Dispatcher State:\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6704 | dumpDispatchStateLocked(dump); |
| 6705 | |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6706 | if (!mLastAnrState.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6707 | dump += "\nInput Dispatcher State at time of last ANR:\n"; |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6708 | dump += mLastAnrState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6709 | } |
| 6710 | } |
| 6711 | |
| 6712 | void InputDispatcher::monitor() { |
| 6713 | // 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] | 6714 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6715 | mLooper->wake(); |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6716 | mDispatcherIsAlive.wait(_l); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6717 | } |
| 6718 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6719 | /** |
| 6720 | * Wake up the dispatcher and wait until it processes all events and commands. |
| 6721 | * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so |
| 6722 | * this method can be safely called from any thread, as long as you've ensured that |
| 6723 | * the work you are interested in completing has already been queued. |
| 6724 | */ |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 6725 | bool InputDispatcher::waitForIdle() const { |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6726 | /** |
| 6727 | * Timeout should represent the longest possible time that a device might spend processing |
| 6728 | * events and commands. |
| 6729 | */ |
| 6730 | constexpr std::chrono::duration TIMEOUT = 100ms; |
| 6731 | std::unique_lock lock(mLock); |
| 6732 | mLooper->wake(); |
| 6733 | std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT); |
| 6734 | return result == std::cv_status::no_timeout; |
| 6735 | } |
| 6736 | |
Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 6737 | /** |
| 6738 | * Sets focus to the window identified by the token. This must be called |
| 6739 | * after updating any input window handles. |
| 6740 | * |
| 6741 | * Params: |
| 6742 | * request.token - input channel token used to identify the window that should gain focus. |
| 6743 | * request.focusedToken - the token that the caller expects currently to be focused. If the |
| 6744 | * specified token does not match the currently focused window, this request will be dropped. |
| 6745 | * If the specified focused token matches the currently focused window, the call will succeed. |
| 6746 | * Set this to "null" if this call should succeed no matter what the currently focused token is. |
| 6747 | * request.timestamp - SYSTEM_TIME_MONOTONIC timestamp in nanos set by the client (wm) |
| 6748 | * when requesting the focus change. This determines which request gets |
| 6749 | * precedence if there is a focus change request from another source such as pointer down. |
| 6750 | */ |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6751 | void InputDispatcher::setFocusedWindow(const FocusRequest& request) { |
| 6752 | { // acquire lock |
| 6753 | std::scoped_lock _l(mLock); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6754 | std::optional<FocusResolver::FocusChanges> changes = |
| 6755 | mFocusResolver.setFocusedWindow(request, getWindowHandlesLocked(request.displayId)); |
| 6756 | if (changes) { |
| 6757 | onFocusChangedLocked(*changes); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6758 | } |
| 6759 | } // release lock |
| 6760 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6761 | mLooper->wake(); |
| 6762 | } |
| 6763 | |
Prabir Pradhan | 4b9b1a1 | 2024-02-02 00:16:17 +0000 | [diff] [blame] | 6764 | void InputDispatcher::onFocusChangedLocked(const FocusResolver::FocusChanges& changes, |
| 6765 | const sp<WindowInfoHandle> removedFocusedWindowHandle) { |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6766 | if (changes.oldFocus) { |
Prabir Pradhan | 4b9b1a1 | 2024-02-02 00:16:17 +0000 | [diff] [blame] | 6767 | const auto resolvedWindow = removedFocusedWindowHandle != nullptr |
| 6768 | ? removedFocusedWindowHandle |
| 6769 | : getWindowHandleLocked(changes.oldFocus, changes.displayId); |
| 6770 | if (resolvedWindow == nullptr) { |
| 6771 | LOG(FATAL) << __func__ << ": Previously focused token did not have a window"; |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6772 | } |
Prabir Pradhan | 4b9b1a1 | 2024-02-02 00:16:17 +0000 | [diff] [blame] | 6773 | CancelationOptions options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, |
| 6774 | "focus left window"); |
| 6775 | synthesizeCancelationEventsForWindowLocked(resolvedWindow, options); |
| 6776 | enqueueFocusEventLocked(changes.oldFocus, /*hasFocus=*/false, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6777 | } |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6778 | if (changes.newFocus) { |
Siarhei Vishniakou | c033dfb | 2023-10-03 10:45:16 -0700 | [diff] [blame] | 6779 | resetNoFocusedWindowTimeoutLocked(); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6780 | enqueueFocusEventLocked(changes.newFocus, /*hasFocus=*/true, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6781 | } |
| 6782 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6783 | // If a window has pointer capture, then it must have focus. We need to ensure that this |
| 6784 | // contract is upheld when pointer capture is being disabled due to a loss of window focus. |
| 6785 | // If the window loses focus before it loses pointer capture, then the window can be in a state |
| 6786 | // where it has pointer capture but not focus, violating the contract. Therefore we must |
| 6787 | // dispatch the pointer capture event before the focus event. Since focus events are added to |
| 6788 | // the front of the queue (above), we add the pointer capture event to the front of the queue |
| 6789 | // after the focus events are added. This ensures the pointer capture event ends up at the |
| 6790 | // front. |
| 6791 | disablePointerCaptureForcedLocked(); |
| 6792 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6793 | if (mFocusedDisplayId == changes.displayId) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6794 | sendFocusChangedCommandLocked(changes.oldFocus, changes.newFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6795 | } |
| 6796 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6797 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6798 | void InputDispatcher::disablePointerCaptureForcedLocked() { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6799 | if (!mCurrentPointerCaptureRequest.enable && !mWindowTokenWithPointerCapture) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6800 | return; |
| 6801 | } |
| 6802 | |
| 6803 | ALOGD_IF(DEBUG_FOCUS, "Disabling Pointer Capture because the window lost focus."); |
| 6804 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6805 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6806 | setPointerCaptureLocked(false); |
| 6807 | } |
| 6808 | |
| 6809 | if (!mWindowTokenWithPointerCapture) { |
| 6810 | // No need to send capture changes because no window has capture. |
| 6811 | return; |
| 6812 | } |
| 6813 | |
| 6814 | if (mPendingEvent != nullptr) { |
| 6815 | // Move the pending event to the front of the queue. This will give the chance |
| 6816 | // for the pending event to be dropped if it is a captured event. |
| 6817 | mInboundQueue.push_front(mPendingEvent); |
| 6818 | mPendingEvent = nullptr; |
| 6819 | } |
| 6820 | |
| 6821 | auto entry = std::make_unique<PointerCaptureChangedEntry>(mIdGenerator.nextId(), now(), |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6822 | mCurrentPointerCaptureRequest); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6823 | mInboundQueue.push_front(std::move(entry)); |
| 6824 | } |
| 6825 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6826 | void InputDispatcher::setPointerCaptureLocked(bool enable) { |
| 6827 | mCurrentPointerCaptureRequest.enable = enable; |
| 6828 | mCurrentPointerCaptureRequest.seq++; |
| 6829 | auto command = [this, request = mCurrentPointerCaptureRequest]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6830 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6831 | mPolicy.setPointerCapture(request); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6832 | }; |
| 6833 | postCommandLocked(std::move(command)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6834 | } |
| 6835 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6836 | void InputDispatcher::displayRemoved(int32_t displayId) { |
| 6837 | { // acquire lock |
| 6838 | std::scoped_lock _l(mLock); |
| 6839 | // Set an empty list to remove all handles from the specific display. |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 6840 | setInputWindowsLocked(/*windowInfoHandles=*/{}, displayId); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6841 | setFocusedApplicationLocked(displayId, nullptr); |
| 6842 | // Call focus resolver to clean up stale requests. This must be called after input windows |
| 6843 | // have been removed for the removed display. |
| 6844 | mFocusResolver.displayRemoved(displayId); |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 6845 | // Reset pointer capture eligibility, regardless of previous state. |
| 6846 | std::erase(mIneligibleDisplaysForPointerCapture, displayId); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 6847 | // Remove the associated touch mode state. |
| 6848 | mTouchModePerDisplay.erase(displayId); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 6849 | mVerifiersByDisplay.erase(displayId); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6850 | } // release lock |
| 6851 | |
| 6852 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6853 | mLooper->wake(); |
| 6854 | } |
| 6855 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 6856 | void InputDispatcher::onWindowInfosChanged(const gui::WindowInfosUpdate& update) { |
Siarhei Vishniakou | aeed0da | 2024-01-09 08:57:13 -0800 | [diff] [blame] | 6857 | if (auto result = validateWindowInfosUpdate(update); !result.ok()) { |
| 6858 | { |
| 6859 | // acquire lock |
| 6860 | std::scoped_lock _l(mLock); |
| 6861 | logDispatchStateLocked(); |
| 6862 | } |
| 6863 | LOG_ALWAYS_FATAL("Incorrect WindowInfosUpdate provided: %s", |
| 6864 | result.error().message().c_str()); |
| 6865 | }; |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6866 | // The listener sends the windows as a flattened array. Separate the windows by display for |
| 6867 | // more convenient parsing. |
| 6868 | std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>> handlesPerDisplay; |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 6869 | for (const auto& info : update.windowInfos) { |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6870 | handlesPerDisplay.emplace(info.displayId, std::vector<sp<WindowInfoHandle>>()); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6871 | handlesPerDisplay[info.displayId].push_back(sp<WindowInfoHandle>::make(info)); |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6872 | } |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6873 | |
| 6874 | { // acquire lock |
| 6875 | std::scoped_lock _l(mLock); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 6876 | |
| 6877 | // Ensure that we have an entry created for all existing displays so that if a displayId has |
| 6878 | // no windows, we can tell that the windows were removed from the display. |
| 6879 | for (const auto& [displayId, _] : mWindowHandlesByDisplay) { |
| 6880 | handlesPerDisplay[displayId]; |
| 6881 | } |
| 6882 | |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6883 | mDisplayInfos.clear(); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 6884 | for (const auto& displayInfo : update.displayInfos) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6885 | mDisplayInfos.emplace(displayInfo.displayId, displayInfo); |
| 6886 | } |
| 6887 | |
| 6888 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 6889 | setInputWindowsLocked(handles, displayId); |
| 6890 | } |
Patrick Williams | 9464b2c | 2023-05-23 11:22:04 -0500 | [diff] [blame] | 6891 | |
| 6892 | if (update.vsyncId < mWindowInfosVsyncId) { |
| 6893 | ALOGE("Received out of order window infos update. Last update vsync id: %" PRId64 |
| 6894 | ", current update vsync id: %" PRId64, |
| 6895 | mWindowInfosVsyncId, update.vsyncId); |
| 6896 | } |
| 6897 | mWindowInfosVsyncId = update.vsyncId; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6898 | } |
| 6899 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6900 | mLooper->wake(); |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6901 | } |
| 6902 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6903 | bool InputDispatcher::shouldDropInput( |
| 6904 | const EventEntry& entry, const sp<android::gui::WindowInfoHandle>& windowHandle) const { |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6905 | if (windowHandle->getInfo()->inputConfig.test(WindowInfo::InputConfig::DROP_INPUT) || |
| 6906 | (windowHandle->getInfo()->inputConfig.test( |
| 6907 | WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED) && |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6908 | isWindowObscuredLocked(windowHandle))) { |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6909 | ALOGW("Dropping %s event targeting %s as requested by the input configuration {%s} on " |
| 6910 | "display %" PRId32 ".", |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6911 | ftl::enum_string(entry.type).c_str(), windowHandle->getName().c_str(), |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6912 | windowHandle->getInfo()->inputConfig.string().c_str(), |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6913 | windowHandle->getInfo()->displayId); |
| 6914 | return true; |
| 6915 | } |
| 6916 | return false; |
| 6917 | } |
| 6918 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 6919 | void InputDispatcher::DispatcherWindowListener::onWindowInfosChanged( |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 6920 | const gui::WindowInfosUpdate& update) { |
| 6921 | mDispatcher.onWindowInfosChanged(update); |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 6922 | } |
| 6923 | |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6924 | void InputDispatcher::cancelCurrentTouch() { |
| 6925 | { |
| 6926 | std::scoped_lock _l(mLock); |
| 6927 | ALOGD("Canceling all ongoing pointer gestures on all displays."); |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 6928 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6929 | "cancel current touch"); |
| 6930 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 6931 | |
| 6932 | mTouchStatesByDisplay.clear(); |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6933 | } |
| 6934 | // Wake up poll loop since there might be work to do. |
| 6935 | mLooper->wake(); |
| 6936 | } |
| 6937 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6938 | void InputDispatcher::setMonitorDispatchingTimeoutForTest(std::chrono::nanoseconds timeout) { |
| 6939 | std::scoped_lock _l(mLock); |
| 6940 | mMonitorDispatchingTimeout = timeout; |
| 6941 | } |
| 6942 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6943 | void InputDispatcher::slipWallpaperTouch(ftl::Flags<InputTarget::Flags> targetFlags, |
| 6944 | const sp<WindowInfoHandle>& oldWindowHandle, |
| 6945 | const sp<WindowInfoHandle>& newWindowHandle, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 6946 | TouchState& state, int32_t deviceId, |
| 6947 | const PointerProperties& pointerProperties, |
Siarhei Vishniakou | bf88052 | 2023-05-01 11:03:22 -0700 | [diff] [blame] | 6948 | std::vector<InputTarget>& targets) const { |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 6949 | std::vector<PointerProperties> pointers{pointerProperties}; |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6950 | const bool oldHasWallpaper = oldWindowHandle->getInfo()->inputConfig.test( |
| 6951 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER); |
| 6952 | const bool newHasWallpaper = targetFlags.test(InputTarget::Flags::FOREGROUND) && |
| 6953 | newWindowHandle->getInfo()->inputConfig.test( |
| 6954 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER); |
| 6955 | const sp<WindowInfoHandle> oldWallpaper = |
| 6956 | oldHasWallpaper ? state.getWallpaperWindow() : nullptr; |
| 6957 | const sp<WindowInfoHandle> newWallpaper = |
| 6958 | newHasWallpaper ? findWallpaperWindowBelow(newWindowHandle) : nullptr; |
| 6959 | if (oldWallpaper == newWallpaper) { |
| 6960 | return; |
| 6961 | } |
| 6962 | |
| 6963 | if (oldWallpaper != nullptr) { |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 6964 | const TouchedWindow& oldTouchedWindow = state.getTouchedWindow(oldWallpaper); |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 6965 | addPointerWindowTargetLocked(oldWallpaper, InputTarget::DispatchMode::SLIPPERY_EXIT, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 6966 | oldTouchedWindow.targetFlags, getPointerIds(pointers), |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 6967 | oldTouchedWindow.getDownTimeInTarget(deviceId), targets); |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 6968 | state.removeTouchingPointerFromWindow(deviceId, pointerProperties.id, oldWallpaper); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6969 | } |
| 6970 | |
| 6971 | if (newWallpaper != nullptr) { |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 6972 | state.addOrUpdateWindow(newWallpaper, InputTarget::DispatchMode::SLIPPERY_ENTER, |
| 6973 | InputTarget::Flags::WINDOW_IS_OBSCURED | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6974 | InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 6975 | deviceId, pointers); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6976 | } |
| 6977 | } |
| 6978 | |
| 6979 | void InputDispatcher::transferWallpaperTouch(ftl::Flags<InputTarget::Flags> oldTargetFlags, |
| 6980 | ftl::Flags<InputTarget::Flags> newTargetFlags, |
| 6981 | const sp<WindowInfoHandle> fromWindowHandle, |
| 6982 | const sp<WindowInfoHandle> toWindowHandle, |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 6983 | TouchState& state, int32_t deviceId, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 6984 | const std::vector<PointerProperties>& pointers) { |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6985 | const bool oldHasWallpaper = oldTargetFlags.test(InputTarget::Flags::FOREGROUND) && |
| 6986 | fromWindowHandle->getInfo()->inputConfig.test( |
| 6987 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER); |
| 6988 | const bool newHasWallpaper = newTargetFlags.test(InputTarget::Flags::FOREGROUND) && |
| 6989 | toWindowHandle->getInfo()->inputConfig.test( |
| 6990 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER); |
| 6991 | |
| 6992 | const sp<WindowInfoHandle> oldWallpaper = |
| 6993 | oldHasWallpaper ? state.getWallpaperWindow() : nullptr; |
| 6994 | const sp<WindowInfoHandle> newWallpaper = |
| 6995 | newHasWallpaper ? findWallpaperWindowBelow(toWindowHandle) : nullptr; |
| 6996 | if (oldWallpaper == newWallpaper) { |
| 6997 | return; |
| 6998 | } |
| 6999 | |
| 7000 | if (oldWallpaper != nullptr) { |
| 7001 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
| 7002 | "transferring touch focus to another window"); |
| 7003 | state.removeWindowByToken(oldWallpaper->getToken()); |
| 7004 | synthesizeCancelationEventsForWindowLocked(oldWallpaper, options); |
| 7005 | } |
| 7006 | |
| 7007 | if (newWallpaper != nullptr) { |
| 7008 | nsecs_t downTimeInTarget = now(); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 7009 | ftl::Flags<InputTarget::Flags> wallpaperFlags = newTargetFlags; |
| 7010 | wallpaperFlags |= oldTargetFlags & InputTarget::Flags::SPLIT; |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 7011 | wallpaperFlags |= InputTarget::Flags::WINDOW_IS_OBSCURED | |
| 7012 | InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 7013 | state.addOrUpdateWindow(newWallpaper, InputTarget::DispatchMode::AS_IS, wallpaperFlags, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 7014 | deviceId, pointers, downTimeInTarget); |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 7015 | std::shared_ptr<Connection> wallpaperConnection = |
| 7016 | getConnectionLocked(newWallpaper->getToken()); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 7017 | if (wallpaperConnection != nullptr) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 7018 | std::shared_ptr<Connection> toConnection = |
| 7019 | getConnectionLocked(toWindowHandle->getToken()); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 7020 | toConnection->inputState.mergePointerStateTo(wallpaperConnection->inputState); |
| 7021 | synthesizePointerDownEventsForConnectionLocked(downTimeInTarget, wallpaperConnection, |
| 7022 | wallpaperFlags); |
| 7023 | } |
| 7024 | } |
| 7025 | } |
| 7026 | |
| 7027 | sp<WindowInfoHandle> InputDispatcher::findWallpaperWindowBelow( |
| 7028 | const sp<WindowInfoHandle>& windowHandle) const { |
| 7029 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
| 7030 | getWindowHandlesLocked(windowHandle->getInfo()->displayId); |
| 7031 | bool foundWindow = false; |
| 7032 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
| 7033 | if (!foundWindow && otherHandle != windowHandle) { |
| 7034 | continue; |
| 7035 | } |
| 7036 | if (windowHandle == otherHandle) { |
| 7037 | foundWindow = true; |
| 7038 | continue; |
| 7039 | } |
| 7040 | |
| 7041 | if (otherHandle->getInfo()->inputConfig.test(WindowInfo::InputConfig::IS_WALLPAPER)) { |
| 7042 | return otherHandle; |
| 7043 | } |
| 7044 | } |
| 7045 | return nullptr; |
| 7046 | } |
| 7047 | |
Siarhei Vishniakou | fa2a049 | 2023-11-14 13:13:18 -0800 | [diff] [blame] | 7048 | void InputDispatcher::setKeyRepeatConfiguration(std::chrono::nanoseconds timeout, |
| 7049 | std::chrono::nanoseconds delay) { |
Nergi Rahardi | 730cf3c | 2023-04-13 12:41:17 +0900 | [diff] [blame] | 7050 | std::scoped_lock _l(mLock); |
| 7051 | |
Siarhei Vishniakou | fa2a049 | 2023-11-14 13:13:18 -0800 | [diff] [blame] | 7052 | mConfig.keyRepeatTimeout = timeout.count(); |
| 7053 | mConfig.keyRepeatDelay = delay.count(); |
Nergi Rahardi | 730cf3c | 2023-04-13 12:41:17 +0900 | [diff] [blame] | 7054 | } |
| 7055 | |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 7056 | bool InputDispatcher::isPointerInWindow(const sp<android::IBinder>& token, int32_t displayId, |
| 7057 | DeviceId deviceId, int32_t pointerId) { |
| 7058 | std::scoped_lock _l(mLock); |
| 7059 | auto touchStateIt = mTouchStatesByDisplay.find(displayId); |
| 7060 | if (touchStateIt == mTouchStatesByDisplay.end()) { |
| 7061 | return false; |
| 7062 | } |
| 7063 | for (const TouchedWindow& window : touchStateIt->second.windows) { |
| 7064 | if (window.windowHandle->getToken() == token && |
| 7065 | (window.hasTouchingPointer(deviceId, pointerId) || |
| 7066 | window.hasHoveringPointer(deviceId, pointerId))) { |
| 7067 | return true; |
| 7068 | } |
| 7069 | } |
| 7070 | return false; |
| 7071 | } |
| 7072 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 7073 | } // namespace android::inputdispatcher |