Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "InputDispatcher" |
| 18 | #define ATRACE_TAG ATRACE_TAG_INPUT |
| 19 | |
John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 20 | #define LOG_NDEBUG 1 |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 21 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 22 | #include <android-base/chrono_utils.h> |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 23 | #include <android-base/logging.h> |
Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 24 | #include <android-base/properties.h> |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 25 | #include <android-base/stringprintf.h> |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 26 | #include <android/os/IInputConstants.h> |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 27 | #include <binder/Binder.h> |
Ameer Armaly | cff4fa5 | 2023-10-04 23:45:11 +0000 | [diff] [blame] | 28 | #include <com_android_input_flags.h> |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 29 | #include <ftl/enum.h> |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 30 | #include <log/log_event_list.h> |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 31 | #if defined(__ANDROID__) |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 32 | #include <gui/SurfaceComposerClient.h> |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 33 | #endif |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 34 | #include <input/InputDevice.h> |
Siarhei Vishniakou | 6e1e987 | 2022-11-08 17:51:35 -0800 | [diff] [blame] | 35 | #include <input/PrintTools.h> |
Prabir Pradhan | a37bad1 | 2023-08-18 15:55:32 +0000 | [diff] [blame] | 36 | #include <input/TraceTools.h> |
tyiu | 1573a67 | 2023-02-21 22:38:32 +0000 | [diff] [blame] | 37 | #include <openssl/mem.h> |
Prabir Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame] | 38 | #include <private/android_filesystem_config.h> |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 39 | #include <unistd.h> |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 40 | #include <utils/Trace.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 41 | |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 42 | #include <cerrno> |
| 43 | #include <cinttypes> |
| 44 | #include <climits> |
| 45 | #include <cstddef> |
| 46 | #include <ctime> |
| 47 | #include <queue> |
| 48 | #include <sstream> |
| 49 | |
Asmita Poddar | dd9a6cd | 2023-09-26 15:35:12 +0000 | [diff] [blame] | 50 | #include "../InputDeviceMetricsSource.h" |
| 51 | |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 52 | #include "Connection.h" |
Arthur Hung | 1a1007b | 2022-05-11 07:15:01 +0000 | [diff] [blame] | 53 | #include "DebugConfig.h" |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 54 | #include "InputDispatcher.h" |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 55 | #include "trace/InputTracer.h" |
| 56 | #include "trace/InputTracingPerfettoBackend.h" |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 57 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 58 | #define INDENT " " |
| 59 | #define INDENT2 " " |
| 60 | #define INDENT3 " " |
| 61 | #define INDENT4 " " |
| 62 | |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 63 | using namespace android::ftl::flag_operators; |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 64 | using android::base::Error; |
Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 65 | using android::base::HwTimeoutMultiplier; |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 66 | using android::base::Result; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 67 | using android::base::StringPrintf; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 68 | using android::gui::DisplayInfo; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 69 | using android::gui::FocusRequest; |
| 70 | using android::gui::TouchOcclusionMode; |
| 71 | using android::gui::WindowInfo; |
| 72 | using android::gui::WindowInfoHandle; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 73 | using android::os::InputEventInjectionResult; |
| 74 | using android::os::InputEventInjectionSync; |
Ameer Armaly | cff4fa5 | 2023-10-04 23:45:11 +0000 | [diff] [blame] | 75 | namespace input_flags = com::android::input::flags; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 76 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 77 | namespace android::inputdispatcher { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 78 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 79 | namespace { |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 80 | |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 81 | // Input tracing is only available on debuggable builds (userdebug and eng) when the feature |
| 82 | // flag is enabled. When the flag is changed, tracing will only be available after reboot. |
| 83 | bool isInputTracingEnabled() { |
| 84 | static const std::string buildType = base::GetProperty("ro.build.type", "user"); |
| 85 | static const bool isUserdebugOrEng = buildType == "userdebug" || buildType == "eng"; |
| 86 | return input_flags::enable_input_event_tracing() && isUserdebugOrEng; |
| 87 | } |
| 88 | |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 89 | template <class Entry> |
| 90 | void ensureEventTraced(const Entry& entry) { |
| 91 | if (!entry.traceTracker) { |
| 92 | LOG(FATAL) << "Expected event entry to be traced, but it wasn't: " << entry; |
| 93 | } |
| 94 | } |
| 95 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 96 | // Temporarily releases a held mutex for the lifetime of the instance. |
| 97 | // Named to match std::scoped_lock |
| 98 | class scoped_unlock { |
| 99 | public: |
| 100 | explicit scoped_unlock(std::mutex& mutex) : mMutex(mutex) { mMutex.unlock(); } |
| 101 | ~scoped_unlock() { mMutex.lock(); } |
| 102 | |
| 103 | private: |
| 104 | std::mutex& mMutex; |
| 105 | }; |
| 106 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 107 | // Default input dispatching timeout if there is no focused application or paused window |
| 108 | // from which to determine an appropriate dispatching timeout. |
Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 109 | const std::chrono::duration DEFAULT_INPUT_DISPATCHING_TIMEOUT = std::chrono::milliseconds( |
| 110 | android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS * |
| 111 | HwTimeoutMultiplier()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 112 | |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 113 | // The default minimum time gap between two user activity poke events. |
| 114 | const std::chrono::milliseconds DEFAULT_USER_ACTIVITY_POKE_INTERVAL = 100ms; |
| 115 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 116 | const std::chrono::duration STALE_EVENT_TIMEOUT = std::chrono::seconds(10) * HwTimeoutMultiplier(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 117 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 118 | // Log a warning when an event takes longer than this to process, even if an ANR does not occur. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 119 | constexpr nsecs_t SLOW_EVENT_PROCESSING_WARNING_TIMEOUT = 2000 * 1000000LL; // 2sec |
| 120 | |
| 121 | // Log a warning when an interception call takes longer than this to process. |
| 122 | constexpr std::chrono::milliseconds SLOW_INTERCEPTION_THRESHOLD = 50ms; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 123 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 124 | // Additional key latency in case a connection is still processing some motion events. |
| 125 | // This will help with the case when a user touched a button that opens a new window, |
| 126 | // and gives us the chance to dispatch the key to this new window. |
| 127 | constexpr std::chrono::nanoseconds KEY_WAITING_FOR_EVENTS_TIMEOUT = 500ms; |
| 128 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 129 | // Number of recent events to keep for debugging purposes. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 130 | constexpr size_t RECENT_QUEUE_MAX_SIZE = 10; |
| 131 | |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 132 | // Event log tags. See EventLogTags.logtags for reference. |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 133 | constexpr int LOGTAG_INPUT_INTERACTION = 62000; |
| 134 | constexpr int LOGTAG_INPUT_FOCUS = 62001; |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 135 | constexpr int LOGTAG_INPUT_CANCEL = 62003; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 136 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 137 | const ui::Transform kIdentityTransform; |
| 138 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 139 | inline nsecs_t now() { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 140 | return systemTime(SYSTEM_TIME_MONOTONIC); |
| 141 | } |
| 142 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 143 | inline const std::string binderToString(const sp<IBinder>& binder) { |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 144 | if (binder == nullptr) { |
| 145 | return "<null>"; |
| 146 | } |
| 147 | return StringPrintf("%p", binder.get()); |
| 148 | } |
| 149 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 150 | static std::string uidString(const gui::Uid& uid) { |
| 151 | return uid.toString(); |
| 152 | } |
| 153 | |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 154 | Result<void> checkKeyAction(int32_t action) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 155 | switch (action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 156 | case AKEY_EVENT_ACTION_DOWN: |
| 157 | case AKEY_EVENT_ACTION_UP: |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 158 | return {}; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 159 | default: |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 160 | return Error() << "Key event has invalid action code " << action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 164 | Result<void> validateKeyEvent(int32_t action) { |
| 165 | return checkKeyAction(action); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 166 | } |
| 167 | |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 168 | Result<void> checkMotionAction(int32_t action, int32_t actionButton, int32_t pointerCount) { |
Siarhei Vishniakou | 2f61bdc | 2022-12-02 08:55:51 -0800 | [diff] [blame] | 169 | switch (MotionEvent::getActionMasked(action)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 170 | case AMOTION_EVENT_ACTION_DOWN: |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 171 | case AMOTION_EVENT_ACTION_UP: { |
| 172 | if (pointerCount != 1) { |
| 173 | return Error() << "invalid pointer count " << pointerCount; |
| 174 | } |
| 175 | return {}; |
| 176 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 177 | case AMOTION_EVENT_ACTION_MOVE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 178 | case AMOTION_EVENT_ACTION_HOVER_ENTER: |
| 179 | case AMOTION_EVENT_ACTION_HOVER_MOVE: |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 180 | case AMOTION_EVENT_ACTION_HOVER_EXIT: { |
| 181 | if (pointerCount < 1) { |
| 182 | return Error() << "invalid pointer count " << pointerCount; |
| 183 | } |
| 184 | return {}; |
| 185 | } |
Siarhei Vishniakou | 2f61bdc | 2022-12-02 08:55:51 -0800 | [diff] [blame] | 186 | case AMOTION_EVENT_ACTION_CANCEL: |
| 187 | case AMOTION_EVENT_ACTION_OUTSIDE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 188 | case AMOTION_EVENT_ACTION_SCROLL: |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 189 | return {}; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 190 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 191 | case AMOTION_EVENT_ACTION_POINTER_UP: { |
Siarhei Vishniakou | 2f61bdc | 2022-12-02 08:55:51 -0800 | [diff] [blame] | 192 | const int32_t index = MotionEvent::getActionIndex(action); |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 193 | if (index < 0) { |
| 194 | return Error() << "invalid index " << index << " for " |
| 195 | << MotionEvent::actionToString(action); |
| 196 | } |
| 197 | if (index >= pointerCount) { |
| 198 | return Error() << "invalid index " << index << " for pointerCount " << pointerCount; |
| 199 | } |
| 200 | if (pointerCount <= 1) { |
| 201 | return Error() << "invalid pointer count " << pointerCount << " for " |
| 202 | << MotionEvent::actionToString(action); |
| 203 | } |
| 204 | return {}; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 205 | } |
| 206 | case AMOTION_EVENT_ACTION_BUTTON_PRESS: |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 207 | case AMOTION_EVENT_ACTION_BUTTON_RELEASE: { |
| 208 | if (actionButton == 0) { |
| 209 | return Error() << "action button should be nonzero for " |
| 210 | << MotionEvent::actionToString(action); |
| 211 | } |
| 212 | return {}; |
| 213 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 214 | default: |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 215 | return Error() << "invalid action " << action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 219 | int64_t millis(std::chrono::nanoseconds t) { |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 220 | return std::chrono::duration_cast<std::chrono::milliseconds>(t).count(); |
| 221 | } |
| 222 | |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 223 | Result<void> validateMotionEvent(int32_t action, int32_t actionButton, size_t pointerCount, |
| 224 | const PointerProperties* pointerProperties) { |
| 225 | Result<void> actionCheck = checkMotionAction(action, actionButton, pointerCount); |
| 226 | if (!actionCheck.ok()) { |
| 227 | return actionCheck; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 228 | } |
| 229 | if (pointerCount < 1 || pointerCount > MAX_POINTERS) { |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 230 | return Error() << "Motion event has invalid pointer count " << pointerCount |
| 231 | << "; value must be between 1 and " << MAX_POINTERS << "."; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 232 | } |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 233 | std::bitset<MAX_POINTER_ID + 1> pointerIdBits; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 234 | for (size_t i = 0; i < pointerCount; i++) { |
| 235 | int32_t id = pointerProperties[i].id; |
| 236 | if (id < 0 || id > MAX_POINTER_ID) { |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 237 | return Error() << "Motion event has invalid pointer id " << id |
| 238 | << "; value must be between 0 and " << MAX_POINTER_ID; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 239 | } |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 240 | if (pointerIdBits.test(id)) { |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 241 | return Error() << "Motion event has duplicate pointer id " << id; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 242 | } |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 243 | pointerIdBits.set(id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 244 | } |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 245 | return {}; |
| 246 | } |
| 247 | |
| 248 | Result<void> validateInputEvent(const InputEvent& event) { |
| 249 | switch (event.getType()) { |
| 250 | case InputEventType::KEY: { |
| 251 | const KeyEvent& key = static_cast<const KeyEvent&>(event); |
| 252 | const int32_t action = key.getAction(); |
| 253 | return validateKeyEvent(action); |
| 254 | } |
| 255 | case InputEventType::MOTION: { |
| 256 | const MotionEvent& motion = static_cast<const MotionEvent&>(event); |
| 257 | const int32_t action = motion.getAction(); |
| 258 | const size_t pointerCount = motion.getPointerCount(); |
| 259 | const PointerProperties* pointerProperties = motion.getPointerProperties(); |
| 260 | const int32_t actionButton = motion.getActionButton(); |
| 261 | return validateMotionEvent(action, actionButton, pointerCount, pointerProperties); |
| 262 | } |
| 263 | default: { |
| 264 | return {}; |
| 265 | } |
| 266 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 267 | } |
| 268 | |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 269 | std::bitset<MAX_POINTER_ID + 1> getPointerIds(const std::vector<PointerProperties>& pointers) { |
| 270 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
| 271 | for (const PointerProperties& pointer : pointers) { |
| 272 | pointerIds.set(pointer.id); |
| 273 | } |
| 274 | return pointerIds; |
| 275 | } |
| 276 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 277 | std::string dumpRegion(const Region& region) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 278 | if (region.isEmpty()) { |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 279 | return "<empty>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 280 | } |
| 281 | |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 282 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 283 | bool first = true; |
| 284 | Region::const_iterator cur = region.begin(); |
| 285 | Region::const_iterator const tail = region.end(); |
| 286 | while (cur != tail) { |
| 287 | if (first) { |
| 288 | first = false; |
| 289 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 290 | dump += "|"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 291 | } |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 292 | 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] | 293 | cur++; |
| 294 | } |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 295 | return dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 296 | } |
| 297 | |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 298 | std::string dumpQueue(const std::deque<std::unique_ptr<DispatchEntry>>& queue, |
| 299 | nsecs_t currentTime) { |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 300 | constexpr size_t maxEntries = 50; // max events to print |
| 301 | constexpr size_t skipBegin = maxEntries / 2; |
| 302 | const size_t skipEnd = queue.size() - maxEntries / 2; |
| 303 | // skip from maxEntries / 2 ... size() - maxEntries/2 |
| 304 | // only print from 0 .. skipBegin and then from skipEnd .. size() |
| 305 | |
| 306 | std::string dump; |
| 307 | for (size_t i = 0; i < queue.size(); i++) { |
| 308 | const DispatchEntry& entry = *queue[i]; |
| 309 | if (i >= skipBegin && i < skipEnd) { |
| 310 | dump += StringPrintf(INDENT4 "<skipped %zu entries>\n", skipEnd - skipBegin); |
| 311 | i = skipEnd - 1; // it will be incremented to "skipEnd" by 'continue' |
| 312 | continue; |
| 313 | } |
| 314 | dump.append(INDENT4); |
| 315 | dump += entry.eventEntry->getDescription(); |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 316 | dump += StringPrintf(", seq=%" PRIu32 ", targetFlags=%s, age=%" PRId64 "ms", entry.seq, |
| 317 | entry.targetFlags.string().c_str(), |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 318 | ns2ms(currentTime - entry.eventEntry->eventTime)); |
| 319 | if (entry.deliveryTime != 0) { |
| 320 | // This entry was delivered, so add information on how long we've been waiting |
| 321 | dump += StringPrintf(", wait=%" PRId64 "ms", ns2ms(currentTime - entry.deliveryTime)); |
| 322 | } |
| 323 | dump.append("\n"); |
| 324 | } |
| 325 | return dump; |
| 326 | } |
| 327 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 328 | /** |
| 329 | * Find the entry in std::unordered_map by key, and return it. |
| 330 | * If the entry is not found, return a default constructed entry. |
| 331 | * |
| 332 | * Useful when the entries are vectors, since an empty vector will be returned |
| 333 | * if the entry is not found. |
| 334 | * Also useful when the entries are sp<>. If an entry is not found, nullptr is returned. |
| 335 | */ |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 336 | template <typename K, typename V> |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 337 | V getValueByKey(const std::unordered_map<K, V>& map, K key) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 338 | auto it = map.find(key); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 339 | return it != map.end() ? it->second : V{}; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 340 | } |
| 341 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 342 | bool haveSameToken(const sp<WindowInfoHandle>& first, const sp<WindowInfoHandle>& second) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 343 | if (first == second) { |
| 344 | return true; |
| 345 | } |
| 346 | |
| 347 | if (first == nullptr || second == nullptr) { |
| 348 | return false; |
| 349 | } |
| 350 | |
| 351 | return first->getToken() == second->getToken(); |
| 352 | } |
| 353 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 354 | bool haveSameApplicationToken(const WindowInfo* first, const WindowInfo* second) { |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 355 | if (first == nullptr || second == nullptr) { |
| 356 | return false; |
| 357 | } |
| 358 | return first->applicationInfo.token != nullptr && |
| 359 | first->applicationInfo.token == second->applicationInfo.token; |
| 360 | } |
| 361 | |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 362 | template <typename T> |
| 363 | size_t firstMarkedBit(T set) { |
| 364 | // TODO: replace with std::countr_zero from <bit> when that's available |
| 365 | LOG_ALWAYS_FATAL_IF(set.none()); |
| 366 | size_t i = 0; |
| 367 | while (!set.test(i)) { |
| 368 | i++; |
| 369 | } |
| 370 | return i; |
| 371 | } |
| 372 | |
Prabir Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame] | 373 | std::unique_ptr<DispatchEntry> createDispatchEntry(const InputTarget& inputTarget, |
| 374 | std::shared_ptr<const EventEntry> eventEntry, |
| 375 | ftl::Flags<InputTarget::Flags> inputTargetFlags, |
| 376 | int64_t vsyncId) { |
| 377 | const sp<WindowInfoHandle> win = inputTarget.windowHandle; |
| 378 | const std::optional<int32_t> windowId = |
| 379 | win ? std::make_optional(win->getInfo()->id) : std::nullopt; |
| 380 | // Assume the only targets that are not associated with a window are global monitors, and use |
| 381 | // the system UID for global monitors for tracing purposes. |
| 382 | const gui::Uid uid = win ? win->getInfo()->ownerUid : gui::Uid(AID_SYSTEM); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 383 | if (inputTarget.useDefaultPointerTransform()) { |
| 384 | const ui::Transform& transform = inputTarget.getDefaultPointerTransform(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 385 | return std::make_unique<DispatchEntry>(eventEntry, inputTargetFlags, transform, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 386 | inputTarget.displayTransform, |
Prabir Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame] | 387 | inputTarget.globalScaleFactor, uid, vsyncId, |
| 388 | windowId); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | ALOG_ASSERT(eventEntry->type == EventEntry::Type::MOTION); |
| 392 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); |
| 393 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 394 | std::vector<PointerCoords> pointerCoords; |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 395 | pointerCoords.resize(motionEntry.getPointerCount()); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 396 | |
| 397 | // Use the first pointer information to normalize all other pointers. This could be any pointer |
| 398 | // as long as all other pointers are normalized to the same value and the final DispatchEntry |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 399 | // uses the transform for the normalized pointer. |
| 400 | const ui::Transform& firstPointerTransform = |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 401 | inputTarget.pointerTransforms[firstMarkedBit(inputTarget.pointerIds)]; |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 402 | ui::Transform inverseFirstTransform = firstPointerTransform.inverse(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 403 | |
| 404 | // Iterate through all pointers in the event to normalize against the first. |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 405 | for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.getPointerCount(); pointerIndex++) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 406 | const PointerProperties& pointerProperties = motionEntry.pointerProperties[pointerIndex]; |
| 407 | uint32_t pointerId = uint32_t(pointerProperties.id); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 408 | const ui::Transform& currTransform = inputTarget.pointerTransforms[pointerId]; |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 409 | |
| 410 | pointerCoords[pointerIndex].copyFrom(motionEntry.pointerCoords[pointerIndex]); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 411 | // First, apply the current pointer's transform to update the coordinates into |
| 412 | // window space. |
| 413 | pointerCoords[pointerIndex].transform(currTransform); |
| 414 | // Next, apply the inverse transform of the normalized coordinates so the |
| 415 | // current coordinates are transformed into the normalized coordinate space. |
| 416 | pointerCoords[pointerIndex].transform(inverseFirstTransform); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 419 | std::unique_ptr<MotionEntry> combinedMotionEntry = |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 420 | std::make_unique<MotionEntry>(motionEntry.id, motionEntry.injectionState, |
| 421 | motionEntry.eventTime, motionEntry.deviceId, |
| 422 | motionEntry.source, motionEntry.displayId, |
| 423 | motionEntry.policyFlags, motionEntry.action, |
| 424 | motionEntry.actionButton, motionEntry.flags, |
| 425 | motionEntry.metaState, motionEntry.buttonState, |
| 426 | motionEntry.classification, motionEntry.edgeFlags, |
| 427 | motionEntry.xPrecision, motionEntry.yPrecision, |
| 428 | motionEntry.xCursorPosition, motionEntry.yCursorPosition, |
| 429 | motionEntry.downTime, motionEntry.pointerProperties, |
| 430 | pointerCoords); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 431 | |
| 432 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 433 | std::make_unique<DispatchEntry>(std::move(combinedMotionEntry), inputTargetFlags, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 434 | firstPointerTransform, inputTarget.displayTransform, |
Prabir Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame] | 435 | inputTarget.globalScaleFactor, uid, vsyncId, windowId); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 436 | return dispatchEntry; |
| 437 | } |
| 438 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 439 | status_t openInputChannelPair(const std::string& name, std::shared_ptr<InputChannel>& serverChannel, |
| 440 | std::unique_ptr<InputChannel>& clientChannel) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 441 | std::unique_ptr<InputChannel> uniqueServerChannel; |
| 442 | status_t result = InputChannel::openInputChannelPair(name, uniqueServerChannel, clientChannel); |
| 443 | |
| 444 | serverChannel = std::move(uniqueServerChannel); |
| 445 | return result; |
| 446 | } |
| 447 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 448 | template <typename T> |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 449 | 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] | 450 | if (lhs == nullptr && rhs == nullptr) { |
| 451 | return true; |
| 452 | } |
| 453 | if (lhs == nullptr || rhs == nullptr) { |
| 454 | return false; |
| 455 | } |
| 456 | return *lhs == *rhs; |
| 457 | } |
| 458 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 459 | KeyEvent createKeyEvent(const KeyEntry& entry) { |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 460 | KeyEvent event; |
| 461 | event.initialize(entry.id, entry.deviceId, entry.source, entry.displayId, INVALID_HMAC, |
| 462 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, |
| 463 | entry.repeatCount, entry.downTime, entry.eventTime); |
| 464 | return event; |
| 465 | } |
| 466 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 467 | bool shouldReportMetricsForConnection(const Connection& connection) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 468 | // Do not keep track of gesture monitors. They receive every event and would disproportionately |
| 469 | // affect the statistics. |
| 470 | if (connection.monitor) { |
| 471 | return false; |
| 472 | } |
| 473 | // If the connection is experiencing ANR, let's skip it. We have separate ANR metrics |
| 474 | if (!connection.responsive) { |
| 475 | return false; |
| 476 | } |
| 477 | return true; |
| 478 | } |
| 479 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 480 | bool shouldReportFinishedEvent(const DispatchEntry& dispatchEntry, const Connection& connection) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 481 | const EventEntry& eventEntry = *dispatchEntry.eventEntry; |
| 482 | const int32_t& inputEventId = eventEntry.id; |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 483 | if (inputEventId == android::os::IInputConstants::INVALID_INPUT_EVENT_ID) { |
| 484 | return false; |
| 485 | } |
| 486 | // Only track latency for events that originated from hardware |
| 487 | if (eventEntry.isSynthesized()) { |
| 488 | return false; |
| 489 | } |
| 490 | const EventEntry::Type& inputEventEntryType = eventEntry.type; |
| 491 | if (inputEventEntryType == EventEntry::Type::KEY) { |
| 492 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 493 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
| 494 | return false; |
| 495 | } |
| 496 | } else if (inputEventEntryType == EventEntry::Type::MOTION) { |
| 497 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 498 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL || |
| 499 | motionEntry.action == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 500 | return false; |
| 501 | } |
| 502 | } else { |
| 503 | // Not a key or a motion |
| 504 | return false; |
| 505 | } |
| 506 | if (!shouldReportMetricsForConnection(connection)) { |
| 507 | return false; |
| 508 | } |
| 509 | return true; |
| 510 | } |
| 511 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 512 | /** |
| 513 | * Connection is responsive if it has no events in the waitQueue that are older than the |
| 514 | * current time. |
| 515 | */ |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 516 | bool isConnectionResponsive(const Connection& connection) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 517 | const nsecs_t currentTime = now(); |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 518 | for (const auto& dispatchEntry : connection.waitQueue) { |
| 519 | if (dispatchEntry->timeoutTime < currentTime) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 520 | return false; |
| 521 | } |
| 522 | } |
| 523 | return true; |
| 524 | } |
| 525 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 526 | // Returns true if the event type passed as argument represents a user activity. |
| 527 | bool isUserActivityEvent(const EventEntry& eventEntry) { |
| 528 | switch (eventEntry.type) { |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 529 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 530 | case EventEntry::Type::DEVICE_RESET: |
| 531 | case EventEntry::Type::DRAG: |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 532 | case EventEntry::Type::FOCUS: |
| 533 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 534 | case EventEntry::Type::SENSOR: |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 535 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 536 | return false; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 537 | case EventEntry::Type::KEY: |
| 538 | case EventEntry::Type::MOTION: |
| 539 | return true; |
| 540 | } |
| 541 | } |
| 542 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 543 | // 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] | 544 | bool windowAcceptsTouchAt(const WindowInfo& windowInfo, int32_t displayId, float x, float y, |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 545 | bool isStylus, const ui::Transform& displayTransform) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 546 | const auto inputConfig = windowInfo.inputConfig; |
| 547 | if (windowInfo.displayId != displayId || |
| 548 | inputConfig.test(WindowInfo::InputConfig::NOT_VISIBLE)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 549 | return false; |
| 550 | } |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 551 | const bool windowCanInterceptTouch = isStylus && windowInfo.interceptsStylus(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 552 | if (inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE) && !windowCanInterceptTouch) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 553 | return false; |
| 554 | } |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 555 | |
| 556 | // Window Manager works in the logical display coordinate space. When it specifies bounds for a |
| 557 | // window as (l, t, r, b), the range of x in [l, r) and y in [t, b) are considered to be inside |
| 558 | // the window. Points on the right and bottom edges should not be inside the window, so we need |
| 559 | // to be careful about performing a hit test when the display is rotated, since the "right" and |
| 560 | // "bottom" of the window will be different in the display (un-rotated) space compared to in the |
| 561 | // logical display in which WM determined the bounds. Perform the hit test in the logical |
| 562 | // display space to ensure these edges are considered correctly in all orientations. |
| 563 | const auto touchableRegion = displayTransform.transform(windowInfo.touchableRegion); |
| 564 | const auto p = displayTransform.transform(x, y); |
| 565 | if (!touchableRegion.contains(std::floor(p.x), std::floor(p.y))) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 566 | return false; |
| 567 | } |
| 568 | return true; |
| 569 | } |
| 570 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 571 | bool isPointerFromStylus(const MotionEntry& entry, int32_t pointerIndex) { |
| 572 | return isFromSource(entry.source, AINPUT_SOURCE_STYLUS) && |
Prabir Pradhan | e562696 | 2022-10-27 20:30:53 +0000 | [diff] [blame] | 573 | isStylusToolType(entry.pointerProperties[pointerIndex].toolType); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 574 | } |
| 575 | |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 576 | // Determines if the given window can be targeted as InputTarget::Flags::FOREGROUND. |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 577 | // Foreground events are only sent to "foreground targetable" windows, but not all gestures sent to |
| 578 | // such window are necessarily targeted with the flag. For example, an event with ACTION_OUTSIDE can |
| 579 | // 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] | 580 | // InputTarget::Flags::FOREGROUND. |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 581 | bool canReceiveForegroundTouches(const WindowInfo& info) { |
| 582 | // A non-touchable window can still receive touch events (e.g. in the case of |
| 583 | // STYLUS_INTERCEPTOR), so prevent such windows from receiving foreground events for touches. |
| 584 | return !info.inputConfig.test(gui::WindowInfo::InputConfig::NOT_TOUCHABLE) && !info.isSpy(); |
| 585 | } |
| 586 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 587 | bool isWindowOwnedBy(const sp<WindowInfoHandle>& windowHandle, gui::Pid pid, gui::Uid uid) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 588 | if (windowHandle == nullptr) { |
| 589 | return false; |
| 590 | } |
| 591 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 592 | if (pid == windowInfo->ownerPid && uid == windowInfo->ownerUid) { |
| 593 | return true; |
| 594 | } |
| 595 | return false; |
| 596 | } |
| 597 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 598 | // Checks targeted injection using the window's owner's uid. |
| 599 | // Returns an empty string if an entry can be sent to the given window, or an error message if the |
| 600 | // entry is a targeted injection whose uid target doesn't match the window owner. |
| 601 | std::optional<std::string> verifyTargetedInjection(const sp<WindowInfoHandle>& window, |
| 602 | const EventEntry& entry) { |
| 603 | if (entry.injectionState == nullptr || !entry.injectionState->targetUid) { |
| 604 | // The event was not injected, or the injected event does not target a window. |
| 605 | return {}; |
| 606 | } |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 607 | const auto uid = *entry.injectionState->targetUid; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 608 | if (window == nullptr) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 609 | return StringPrintf("No valid window target for injection into uid %s.", |
| 610 | uid.toString().c_str()); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 611 | } |
| 612 | if (entry.injectionState->targetUid != window->getInfo()->ownerUid) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 613 | return StringPrintf("Injected event targeted at uid %s would be dispatched to window '%s' " |
| 614 | "owned by uid %s.", |
| 615 | uid.toString().c_str(), window->getName().c_str(), |
| 616 | window->getInfo()->ownerUid.toString().c_str()); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 617 | } |
| 618 | return {}; |
| 619 | } |
| 620 | |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 621 | std::pair<float, float> resolveTouchedPosition(const MotionEntry& entry) { |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 622 | const bool isFromMouse = isFromSource(entry.source, AINPUT_SOURCE_MOUSE); |
| 623 | // Always dispatch mouse events to cursor position. |
| 624 | if (isFromMouse) { |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 625 | return {entry.xCursorPosition, entry.yCursorPosition}; |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 626 | } |
| 627 | |
Siarhei Vishniakou | 5b9766d | 2023-07-18 14:06:29 -0700 | [diff] [blame] | 628 | const int32_t pointerIndex = MotionEvent::getActionIndex(entry.action); |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 629 | return {entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 630 | entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_Y)}; |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 631 | } |
| 632 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 633 | std::optional<nsecs_t> getDownTime(const EventEntry& eventEntry) { |
| 634 | if (eventEntry.type == EventEntry::Type::KEY) { |
| 635 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 636 | return keyEntry.downTime; |
| 637 | } else if (eventEntry.type == EventEntry::Type::MOTION) { |
| 638 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 639 | return motionEntry.downTime; |
| 640 | } |
| 641 | return std::nullopt; |
| 642 | } |
| 643 | |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 644 | /** |
| 645 | * Compare the old touch state to the new touch state, and generate the corresponding touched |
| 646 | * windows (== input targets). |
| 647 | * If a window had the hovering pointer, but now it doesn't, produce HOVER_EXIT for that window. |
| 648 | * If the pointer just entered the new window, produce HOVER_ENTER. |
| 649 | * For pointers remaining in the window, produce HOVER_MOVE. |
| 650 | */ |
| 651 | std::vector<TouchedWindow> getHoveringWindowsLocked(const TouchState* oldState, |
| 652 | const TouchState& newTouchState, |
| 653 | const MotionEntry& entry) { |
| 654 | std::vector<TouchedWindow> out; |
| 655 | const int32_t maskedAction = MotionEvent::getActionMasked(entry.action); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 656 | |
| 657 | if (maskedAction == AMOTION_EVENT_ACTION_SCROLL) { |
| 658 | // ACTION_SCROLL events should not affect the hovering pointer dispatch |
| 659 | return {}; |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | // 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] | 663 | const PointerProperties& pointer = entry.pointerProperties[0]; |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 664 | |
| 665 | std::set<sp<WindowInfoHandle>> oldWindows; |
| 666 | if (oldState != nullptr) { |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 667 | oldWindows = oldState->getWindowsWithHoveringPointer(entry.deviceId, pointer.id); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | std::set<sp<WindowInfoHandle>> newWindows = |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 671 | newTouchState.getWindowsWithHoveringPointer(entry.deviceId, pointer.id); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 672 | |
| 673 | // If the pointer is no longer in the new window set, send HOVER_EXIT. |
| 674 | for (const sp<WindowInfoHandle>& oldWindow : oldWindows) { |
| 675 | if (newWindows.find(oldWindow) == newWindows.end()) { |
| 676 | TouchedWindow touchedWindow; |
| 677 | touchedWindow.windowHandle = oldWindow; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 678 | touchedWindow.dispatchMode = InputTarget::DispatchMode::HOVER_EXIT; |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 679 | out.push_back(touchedWindow); |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | for (const sp<WindowInfoHandle>& newWindow : newWindows) { |
| 684 | TouchedWindow touchedWindow; |
| 685 | touchedWindow.windowHandle = newWindow; |
| 686 | if (oldWindows.find(newWindow) == oldWindows.end()) { |
| 687 | // Any windows that have this pointer now, and didn't have it before, should get |
| 688 | // HOVER_ENTER |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 689 | touchedWindow.dispatchMode = InputTarget::DispatchMode::HOVER_ENTER; |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 690 | } else { |
| 691 | // This pointer was already sent to the window. Use ACTION_HOVER_MOVE. |
Siarhei Vishniakou | c2eb850 | 2023-04-11 18:33:36 -0700 | [diff] [blame] | 692 | if (CC_UNLIKELY(maskedAction != AMOTION_EVENT_ACTION_HOVER_MOVE)) { |
Daniel Norman | 7487dfa | 2023-08-02 16:39:45 -0700 | [diff] [blame] | 693 | android::base::LogSeverity severity = android::base::LogSeverity::FATAL; |
Ameer Armaly | cff4fa5 | 2023-10-04 23:45:11 +0000 | [diff] [blame] | 694 | if (!input_flags::a11y_crash_on_inconsistent_event_stream() && |
| 695 | entry.flags & AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT) { |
Daniel Norman | 7487dfa | 2023-08-02 16:39:45 -0700 | [diff] [blame] | 696 | // The Accessibility injected touch exploration event stream |
| 697 | // has known inconsistencies, so log ERROR instead of |
| 698 | // crashing the device with FATAL. |
Daniel Norman | 7487dfa | 2023-08-02 16:39:45 -0700 | [diff] [blame] | 699 | severity = android::base::LogSeverity::ERROR; |
| 700 | } |
| 701 | LOG(severity) << "Expected ACTION_HOVER_MOVE instead of " << entry.getDescription(); |
Siarhei Vishniakou | c2eb850 | 2023-04-11 18:33:36 -0700 | [diff] [blame] | 702 | } |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 703 | touchedWindow.dispatchMode = InputTarget::DispatchMode::AS_IS; |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 704 | } |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 705 | touchedWindow.addHoveringPointer(entry.deviceId, pointer); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 706 | if (canReceiveForegroundTouches(*newWindow->getInfo())) { |
| 707 | touchedWindow.targetFlags |= InputTarget::Flags::FOREGROUND; |
| 708 | } |
| 709 | out.push_back(touchedWindow); |
| 710 | } |
| 711 | return out; |
| 712 | } |
| 713 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 714 | template <typename T> |
| 715 | std::vector<T>& operator+=(std::vector<T>& left, const std::vector<T>& right) { |
| 716 | left.insert(left.end(), right.begin(), right.end()); |
| 717 | return left; |
| 718 | } |
| 719 | |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 720 | // Filter windows in a TouchState and targets in a vector to remove untrusted windows/targets from |
| 721 | // both. |
| 722 | void filterUntrustedTargets(TouchState& touchState, std::vector<InputTarget>& targets) { |
| 723 | std::erase_if(touchState.windows, [&](const TouchedWindow& window) { |
| 724 | if (!window.windowHandle->getInfo()->inputConfig.test( |
| 725 | WindowInfo::InputConfig::TRUSTED_OVERLAY)) { |
| 726 | // In addition to TouchState, erase this window from the input targets! We don't have a |
| 727 | // good way to do this today except by adding a nested loop. |
| 728 | // TODO(b/282025641): simplify this code once InputTargets are being identified |
| 729 | // separately from TouchedWindows. |
| 730 | std::erase_if(targets, [&](const InputTarget& target) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 731 | return target.connection->getToken() == window.windowHandle->getToken(); |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 732 | }); |
| 733 | return true; |
| 734 | } |
| 735 | return false; |
| 736 | }); |
| 737 | } |
| 738 | |
Siarhei Vishniakou | ce1fd47 | 2023-09-18 18:38:07 -0700 | [diff] [blame] | 739 | /** |
| 740 | * In general, touch should be always split between windows. Some exceptions: |
| 741 | * 1. Don't split touch if all of the below is true: |
| 742 | * (a) we have an active pointer down *and* |
| 743 | * (b) a new pointer is going down that's from the same device *and* |
| 744 | * (c) the window that's receiving the current pointer does not support split touch. |
| 745 | * 2. Don't split mouse events |
| 746 | */ |
| 747 | bool shouldSplitTouch(const TouchState& touchState, const MotionEntry& entry) { |
| 748 | if (isFromSource(entry.source, AINPUT_SOURCE_MOUSE)) { |
| 749 | // We should never split mouse events |
| 750 | return false; |
| 751 | } |
| 752 | for (const TouchedWindow& touchedWindow : touchState.windows) { |
| 753 | if (touchedWindow.windowHandle->getInfo()->isSpy()) { |
| 754 | // Spy windows should not affect whether or not touch is split. |
| 755 | continue; |
| 756 | } |
| 757 | if (touchedWindow.windowHandle->getInfo()->supportsSplitTouch()) { |
| 758 | continue; |
| 759 | } |
| 760 | if (touchedWindow.windowHandle->getInfo()->inputConfig.test( |
| 761 | gui::WindowInfo::InputConfig::IS_WALLPAPER)) { |
| 762 | // Wallpaper window should not affect whether or not touch is split |
| 763 | continue; |
| 764 | } |
| 765 | |
| 766 | if (touchedWindow.hasTouchingPointers(entry.deviceId)) { |
| 767 | return false; |
| 768 | } |
| 769 | } |
| 770 | return true; |
| 771 | } |
| 772 | |
Siarhei Vishniakou | f77f60a | 2023-10-23 17:26:05 -0700 | [diff] [blame] | 773 | /** |
| 774 | * Return true if stylus is currently down anywhere on the specified display, and false otherwise. |
| 775 | */ |
| 776 | bool isStylusActiveInDisplay( |
| 777 | int32_t displayId, |
| 778 | const std::unordered_map<int32_t /*displayId*/, TouchState>& touchStatesByDisplay) { |
| 779 | const auto it = touchStatesByDisplay.find(displayId); |
| 780 | if (it == touchStatesByDisplay.end()) { |
| 781 | return false; |
| 782 | } |
| 783 | const TouchState& state = it->second; |
| 784 | return state.hasActiveStylus(); |
| 785 | } |
| 786 | |
Siarhei Vishniakou | aeed0da | 2024-01-09 08:57:13 -0800 | [diff] [blame] | 787 | Result<void> validateWindowInfosUpdate(const gui::WindowInfosUpdate& update) { |
| 788 | struct HashFunction { |
| 789 | size_t operator()(const WindowInfo& info) const { return info.id; } |
| 790 | }; |
| 791 | |
| 792 | std::unordered_set<WindowInfo, HashFunction> windowSet; |
| 793 | for (const WindowInfo& info : update.windowInfos) { |
| 794 | const auto [_, inserted] = windowSet.insert(info); |
| 795 | if (!inserted) { |
| 796 | return Error() << "Duplicate entry for " << info; |
| 797 | } |
| 798 | } |
| 799 | return {}; |
| 800 | } |
| 801 | |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 802 | int32_t getUserActivityEventType(const EventEntry& eventEntry) { |
| 803 | switch (eventEntry.type) { |
| 804 | case EventEntry::Type::KEY: { |
| 805 | return USER_ACTIVITY_EVENT_BUTTON; |
| 806 | } |
| 807 | case EventEntry::Type::MOTION: { |
| 808 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 809 | if (MotionEvent::isTouchEvent(motionEntry.source, motionEntry.action)) { |
| 810 | return USER_ACTIVITY_EVENT_TOUCH; |
| 811 | } |
| 812 | return USER_ACTIVITY_EVENT_OTHER; |
| 813 | } |
| 814 | default: { |
| 815 | LOG_ALWAYS_FATAL("%s events are not user activity", |
| 816 | ftl::enum_string(eventEntry.type).c_str()); |
| 817 | } |
| 818 | } |
| 819 | } |
| 820 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 821 | } // namespace |
| 822 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 823 | // --- InputDispatcher --- |
| 824 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 825 | InputDispatcher::InputDispatcher(InputDispatcherPolicyInterface& policy) |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 826 | : InputDispatcher(policy, |
| 827 | isInputTracingEnabled() ? std::make_unique<trace::impl::PerfettoBackend>() |
| 828 | : nullptr) {} |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 829 | |
| 830 | InputDispatcher::InputDispatcher(InputDispatcherPolicyInterface& policy, |
| 831 | std::unique_ptr<trace::InputTracingBackendInterface> traceBackend) |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 832 | : mPolicy(policy), |
| 833 | mPendingEvent(nullptr), |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 834 | mLastDropReason(DropReason::NOT_DROPPED), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 835 | mIdGenerator(IdGenerator::Source::INPUT_DISPATCHER), |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 836 | mMinTimeBetweenUserActivityPokes(DEFAULT_USER_ACTIVITY_POKE_INTERVAL), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 837 | mNextUnblockedEvent(nullptr), |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 838 | mMonitorDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 839 | mDispatchEnabled(false), |
| 840 | mDispatchFrozen(false), |
| 841 | mInputFilterEnabled(false), |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 842 | mMaximumObscuringOpacityForTouch(1.0f), |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 843 | mFocusedDisplayId(ADISPLAY_ID_DEFAULT), |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 844 | mWindowTokenWithPointerCapture(nullptr), |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 845 | mLatencyAggregator(), |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 846 | mLatencyTracker(&mLatencyAggregator) { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 847 | mLooper = sp<Looper>::make(false); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 848 | mReporter = createInputReporter(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 849 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 850 | mWindowInfoListener = sp<DispatcherWindowListener>::make(*this); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 851 | #if defined(__ANDROID__) |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 852 | SurfaceComposerClient::getDefault()->addWindowInfosListener(mWindowInfoListener); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 853 | #endif |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 854 | mKeyRepeatState.lastKeyEntry = nullptr; |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 855 | |
| 856 | if (traceBackend) { |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 857 | mTracer = std::make_unique<trace::impl::InputTracer>(std::move(traceBackend)); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 858 | } |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 859 | |
| 860 | mLastUserActivityTimes.fill(0); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | InputDispatcher::~InputDispatcher() { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 864 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 865 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 866 | resetKeyRepeatLocked(); |
| 867 | releasePendingEventLocked(); |
| 868 | drainInboundQueueLocked(); |
| 869 | mCommandQueue.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 870 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 871 | while (!mConnectionsByToken.empty()) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 872 | std::shared_ptr<Connection> connection = mConnectionsByToken.begin()->second; |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 873 | removeInputChannelLocked(connection->getToken(), /*notify=*/false); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 874 | } |
| 875 | } |
| 876 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 877 | status_t InputDispatcher::start() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 878 | if (mThread) { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 879 | return ALREADY_EXISTS; |
| 880 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 881 | mThread = std::make_unique<InputThread>( |
| 882 | "InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); }); |
| 883 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | status_t InputDispatcher::stop() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 887 | if (mThread && mThread->isCallingThread()) { |
| 888 | ALOGE("InputDispatcher cannot be stopped from its own thread!"); |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 889 | return INVALID_OPERATION; |
| 890 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 891 | mThread.reset(); |
| 892 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 893 | } |
| 894 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 895 | void InputDispatcher::dispatchOnce() { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 896 | nsecs_t nextWakeupTime = LLONG_MAX; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 897 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 898 | std::scoped_lock _l(mLock); |
| 899 | mDispatcherIsAlive.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 900 | |
| 901 | // Run a dispatch loop if there are no pending commands. |
| 902 | // The dispatch loop might enqueue commands to run afterwards. |
| 903 | if (!haveCommandsLocked()) { |
Siarhei Vishniakou | 6950596 | 2023-12-28 12:07:04 -0800 | [diff] [blame] | 904 | dispatchOnceInnerLocked(/*byref*/ nextWakeupTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | // Run all pending commands if there are any. |
| 908 | // If any commands were run then force the next poll to wake up immediately. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 909 | if (runCommandsLockedInterruptable()) { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 910 | nextWakeupTime = LLONG_MIN; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 911 | } |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 912 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 913 | // If we are still waiting for ack on some events, |
| 914 | // we might have to wake up earlier to check if an app is anr'ing. |
| 915 | const nsecs_t nextAnrCheck = processAnrsLocked(); |
| 916 | nextWakeupTime = std::min(nextWakeupTime, nextAnrCheck); |
| 917 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 918 | // We are about to enter an infinitely long sleep, because we have no commands or |
| 919 | // pending or queued events |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 920 | if (nextWakeupTime == LLONG_MAX) { |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 921 | mDispatcherEnteredIdle.notify_all(); |
| 922 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 923 | } // release lock |
| 924 | |
| 925 | // Wait for callback or timeout or wake. (make sure we round up, not down) |
| 926 | nsecs_t currentTime = now(); |
| 927 | int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime); |
| 928 | mLooper->pollOnce(timeoutMillis); |
| 929 | } |
| 930 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 931 | /** |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 932 | * Raise ANR if there is no focused window. |
| 933 | * Before the ANR is raised, do a final state check: |
| 934 | * 1. The currently focused application must be the same one we are waiting for. |
| 935 | * 2. Ensure we still don't have a focused window. |
| 936 | */ |
| 937 | void InputDispatcher::processNoFocusedWindowAnrLocked() { |
| 938 | // Check if the application that we are waiting for is still focused. |
| 939 | std::shared_ptr<InputApplicationHandle> focusedApplication = |
| 940 | getValueByKey(mFocusedApplicationHandlesByDisplay, mAwaitedApplicationDisplayId); |
| 941 | if (focusedApplication == nullptr || |
| 942 | focusedApplication->getApplicationToken() != |
| 943 | mAwaitedFocusedApplication->getApplicationToken()) { |
| 944 | // Unexpected because we should have reset the ANR timer when focused application changed |
| 945 | ALOGE("Waited for a focused window, but focused application has already changed to %s", |
| 946 | focusedApplication->getName().c_str()); |
| 947 | return; // The focused application has changed. |
| 948 | } |
| 949 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 950 | const sp<WindowInfoHandle>& focusedWindowHandle = |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 951 | getFocusedWindowHandleLocked(mAwaitedApplicationDisplayId); |
| 952 | if (focusedWindowHandle != nullptr) { |
| 953 | return; // We now have a focused window. No need for ANR. |
| 954 | } |
| 955 | onAnrLocked(mAwaitedFocusedApplication); |
| 956 | } |
| 957 | |
| 958 | /** |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 959 | * Check if any of the connections' wait queues have events that are too old. |
| 960 | * If we waited for events to be ack'ed for more than the window timeout, raise an ANR. |
| 961 | * Return the time at which we should wake up next. |
| 962 | */ |
| 963 | nsecs_t InputDispatcher::processAnrsLocked() { |
| 964 | const nsecs_t currentTime = now(); |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 965 | nsecs_t nextAnrCheck = LLONG_MAX; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 966 | // Check if we are waiting for a focused window to appear. Raise ANR if waited too long |
| 967 | if (mNoFocusedWindowTimeoutTime.has_value() && mAwaitedFocusedApplication != nullptr) { |
| 968 | if (currentTime >= *mNoFocusedWindowTimeoutTime) { |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 969 | processNoFocusedWindowAnrLocked(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 970 | mAwaitedFocusedApplication.reset(); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 971 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 972 | return LLONG_MIN; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 973 | } else { |
Siarhei Vishniakou | 38a6d27 | 2020-10-20 20:29:33 -0500 | [diff] [blame] | 974 | // Keep waiting. We will drop the event when mNoFocusedWindowTimeoutTime comes. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 975 | nextAnrCheck = *mNoFocusedWindowTimeoutTime; |
| 976 | } |
| 977 | } |
| 978 | |
| 979 | // Check if any connection ANRs are due |
| 980 | nextAnrCheck = std::min(nextAnrCheck, mAnrTracker.firstTimeout()); |
| 981 | if (currentTime < nextAnrCheck) { // most likely scenario |
| 982 | return nextAnrCheck; // everything is normal. Let's check again at nextAnrCheck |
| 983 | } |
| 984 | |
| 985 | // If we reached here, we have an unresponsive connection. |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 986 | std::shared_ptr<Connection> connection = getConnectionLocked(mAnrTracker.firstToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 987 | if (connection == nullptr) { |
| 988 | ALOGE("Could not find connection for entry %" PRId64, mAnrTracker.firstTimeout()); |
| 989 | return nextAnrCheck; |
| 990 | } |
| 991 | connection->responsive = false; |
| 992 | // Stop waking up for this unresponsive connection |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 993 | mAnrTracker.eraseToken(connection->getToken()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 994 | onAnrLocked(connection); |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 995 | return LLONG_MIN; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 996 | } |
| 997 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 998 | std::chrono::nanoseconds InputDispatcher::getDispatchingTimeoutLocked( |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 999 | const std::shared_ptr<Connection>& connection) { |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 1000 | if (connection->monitor) { |
| 1001 | return mMonitorDispatchingTimeout; |
| 1002 | } |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 1003 | const sp<WindowInfoHandle> window = getWindowHandleLocked(connection->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1004 | if (window != nullptr) { |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 1005 | return window->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1006 | } |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 1007 | return DEFAULT_INPUT_DISPATCHING_TIMEOUT; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1008 | } |
| 1009 | |
Siarhei Vishniakou | 6950596 | 2023-12-28 12:07:04 -0800 | [diff] [blame] | 1010 | void InputDispatcher::dispatchOnceInnerLocked(nsecs_t& nextWakeupTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1011 | nsecs_t currentTime = now(); |
| 1012 | |
Jeff Brown | dc5992e | 2014-04-11 01:27:26 -0700 | [diff] [blame] | 1013 | // Reset the key repeat timer whenever normal dispatch is suspended while the |
| 1014 | // device is in a non-interactive state. This is to ensure that we abort a key |
| 1015 | // repeat if the device is just coming out of sleep. |
| 1016 | if (!mDispatchEnabled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1017 | resetKeyRepeatLocked(); |
| 1018 | } |
| 1019 | |
| 1020 | // If dispatching is frozen, do not process timeouts or try to deliver any new events. |
| 1021 | if (mDispatchFrozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1022 | if (DEBUG_FOCUS) { |
| 1023 | ALOGD("Dispatch frozen. Waiting some more."); |
| 1024 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1025 | return; |
| 1026 | } |
| 1027 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1028 | // Ready to start a new event. |
| 1029 | // If we don't already have a pending event, go grab one. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1030 | if (!mPendingEvent) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1031 | if (mInboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1032 | // Synthesize a key repeat if appropriate. |
| 1033 | if (mKeyRepeatState.lastKeyEntry) { |
| 1034 | if (currentTime >= mKeyRepeatState.nextRepeatTime) { |
| 1035 | mPendingEvent = synthesizeKeyRepeatLocked(currentTime); |
| 1036 | } else { |
Siarhei Vishniakou | 6950596 | 2023-12-28 12:07:04 -0800 | [diff] [blame] | 1037 | nextWakeupTime = std::min(nextWakeupTime, mKeyRepeatState.nextRepeatTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1038 | } |
| 1039 | } |
| 1040 | |
| 1041 | // Nothing to do if there is no pending event. |
| 1042 | if (!mPendingEvent) { |
| 1043 | return; |
| 1044 | } |
| 1045 | } else { |
| 1046 | // Inbound queue has at least one entry. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1047 | mPendingEvent = mInboundQueue.front(); |
| 1048 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1049 | traceInboundQueueLengthLocked(); |
| 1050 | } |
| 1051 | |
| 1052 | // Poke user activity for this event. |
| 1053 | if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1054 | pokeUserActivityLocked(*mPendingEvent); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1055 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1056 | } |
| 1057 | |
| 1058 | // Now we have an event to dispatch. |
| 1059 | // All events are eventually dequeued and processed this way, even if we intend to drop them. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1060 | ALOG_ASSERT(mPendingEvent != nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1061 | bool done = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1062 | DropReason dropReason = DropReason::NOT_DROPPED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1063 | if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1064 | dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1065 | } else if (!mDispatchEnabled) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1066 | dropReason = DropReason::DISABLED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | if (mNextUnblockedEvent == mPendingEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1070 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1071 | } |
| 1072 | |
| 1073 | switch (mPendingEvent->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1074 | case EventEntry::Type::CONFIGURATION_CHANGED: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1075 | const ConfigurationChangedEntry& typedEntry = |
| 1076 | static_cast<const ConfigurationChangedEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1077 | done = dispatchConfigurationChangedLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1078 | dropReason = DropReason::NOT_DROPPED; // configuration changes are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1079 | break; |
| 1080 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1081 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1082 | case EventEntry::Type::DEVICE_RESET: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1083 | const DeviceResetEntry& typedEntry = |
| 1084 | static_cast<const DeviceResetEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1085 | done = dispatchDeviceResetLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1086 | dropReason = DropReason::NOT_DROPPED; // device resets are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1087 | break; |
| 1088 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1089 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1090 | case EventEntry::Type::FOCUS: { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1091 | std::shared_ptr<const FocusEntry> typedEntry = |
| 1092 | std::static_pointer_cast<const FocusEntry>(mPendingEvent); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1093 | dispatchFocusLocked(currentTime, typedEntry); |
| 1094 | done = true; |
| 1095 | dropReason = DropReason::NOT_DROPPED; // focus events are never dropped |
| 1096 | break; |
| 1097 | } |
| 1098 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1099 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1100 | const auto typedEntry = std::static_pointer_cast<const TouchModeEntry>(mPendingEvent); |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1101 | dispatchTouchModeChangeLocked(currentTime, typedEntry); |
| 1102 | done = true; |
| 1103 | dropReason = DropReason::NOT_DROPPED; // touch mode events are never dropped |
| 1104 | break; |
| 1105 | } |
| 1106 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1107 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 1108 | const auto typedEntry = |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1109 | std::static_pointer_cast<const PointerCaptureChangedEntry>(mPendingEvent); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1110 | dispatchPointerCaptureChangedLocked(currentTime, typedEntry, dropReason); |
| 1111 | done = true; |
| 1112 | break; |
| 1113 | } |
| 1114 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1115 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1116 | std::shared_ptr<const DragEntry> typedEntry = |
| 1117 | std::static_pointer_cast<const DragEntry>(mPendingEvent); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1118 | dispatchDragLocked(currentTime, typedEntry); |
| 1119 | done = true; |
| 1120 | break; |
| 1121 | } |
| 1122 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1123 | case EventEntry::Type::KEY: { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1124 | std::shared_ptr<const KeyEntry> keyEntry = |
| 1125 | std::static_pointer_cast<const KeyEntry>(mPendingEvent); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1126 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *keyEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1127 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1128 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1129 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 1130 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1131 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1132 | done = dispatchKeyLocked(currentTime, keyEntry, &dropReason, nextWakeupTime); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 1133 | if (done && mTracer) { |
| 1134 | ensureEventTraced(*keyEntry); |
| 1135 | mTracer->eventProcessingComplete(*keyEntry->traceTracker); |
| 1136 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1137 | break; |
| 1138 | } |
| 1139 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1140 | case EventEntry::Type::MOTION: { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1141 | std::shared_ptr<const MotionEntry> motionEntry = |
| 1142 | std::static_pointer_cast<const MotionEntry>(mPendingEvent); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1143 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *motionEntry)) { |
Siarhei Vishniakou | 6b71b63 | 2023-10-27 21:34:46 -0700 | [diff] [blame] | 1144 | // The event is stale. However, only drop stale events if there isn't an ongoing |
| 1145 | // gesture. That would allow us to complete the processing of the current stroke. |
| 1146 | const auto touchStateIt = mTouchStatesByDisplay.find(motionEntry->displayId); |
| 1147 | if (touchStateIt != mTouchStatesByDisplay.end()) { |
| 1148 | const TouchState& touchState = touchStateIt->second; |
| 1149 | if (!touchState.hasTouchingPointers(motionEntry->deviceId) && |
| 1150 | !touchState.hasHoveringPointers(motionEntry->deviceId)) { |
| 1151 | dropReason = DropReason::STALE; |
| 1152 | } |
| 1153 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1154 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1155 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
Siarhei Vishniakou | 99e407b | 2023-12-26 18:09:32 -0800 | [diff] [blame] | 1156 | if (!isFromSource(motionEntry->source, AINPUT_SOURCE_CLASS_POINTER)) { |
| 1157 | // Only drop events that are focus-dispatched. |
| 1158 | dropReason = DropReason::BLOCKED; |
| 1159 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1160 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1161 | done = dispatchMotionLocked(currentTime, motionEntry, &dropReason, nextWakeupTime); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 1162 | if (done && mTracer) { |
| 1163 | ensureEventTraced(*motionEntry); |
| 1164 | mTracer->eventProcessingComplete(*motionEntry->traceTracker); |
| 1165 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1166 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1167 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1168 | |
| 1169 | case EventEntry::Type::SENSOR: { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1170 | std::shared_ptr<const SensorEntry> sensorEntry = |
| 1171 | std::static_pointer_cast<const SensorEntry>(mPendingEvent); |
Siarhei Vishniakou | e2404a1 | 2024-01-16 18:38:39 -0800 | [diff] [blame] | 1172 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1173 | // Sensor timestamps use SYSTEM_TIME_BOOTTIME time base, so we can't use |
| 1174 | // 'currentTime' here, get SYSTEM_TIME_BOOTTIME instead. |
| 1175 | nsecs_t bootTime = systemTime(SYSTEM_TIME_BOOTTIME); |
| 1176 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(bootTime, *sensorEntry)) { |
| 1177 | dropReason = DropReason::STALE; |
| 1178 | } |
| 1179 | dispatchSensorLocked(currentTime, sensorEntry, &dropReason, nextWakeupTime); |
| 1180 | done = true; |
| 1181 | break; |
| 1182 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1183 | } |
| 1184 | |
| 1185 | if (done) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1186 | if (dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1187 | dropInboundEventLocked(*mPendingEvent, dropReason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1188 | } |
Michael Wright | 3a98172 | 2015-06-10 15:26:13 +0100 | [diff] [blame] | 1189 | mLastDropReason = dropReason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1190 | |
| 1191 | releasePendingEventLocked(); |
Siarhei Vishniakou | 6950596 | 2023-12-28 12:07:04 -0800 | [diff] [blame] | 1192 | nextWakeupTime = LLONG_MIN; // force next poll to wake up immediately |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1193 | } |
| 1194 | } |
| 1195 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 1196 | bool InputDispatcher::isStaleEvent(nsecs_t currentTime, const EventEntry& entry) { |
Siarhei Vishniakou | a733311 | 2023-10-27 13:33:29 -0700 | [diff] [blame] | 1197 | return mPolicy.isStaleEvent(currentTime, entry.eventTime); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 1198 | } |
| 1199 | |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1200 | /** |
| 1201 | * Return true if the events preceding this incoming motion event should be dropped |
| 1202 | * Return false otherwise (the default behaviour) |
| 1203 | */ |
| 1204 | bool InputDispatcher::shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1205 | const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN && |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 1206 | isFromSource(motionEntry.source, AINPUT_SOURCE_CLASS_POINTER); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1207 | |
| 1208 | // Optimize case where the current application is unresponsive and the user |
| 1209 | // decides to touch a window in a different application. |
| 1210 | // If the application takes too long to catch up then we drop all events preceding |
| 1211 | // the touch into the other window. |
| 1212 | if (isPointerDownEvent && mAwaitedFocusedApplication != nullptr) { |
Siarhei Vishniakou | 9306c38 | 2022-09-30 15:30:31 -0700 | [diff] [blame] | 1213 | const int32_t displayId = motionEntry.displayId; |
| 1214 | const auto [x, y] = resolveTouchedPosition(motionEntry); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1215 | const bool isStylus = isPointerFromStylus(motionEntry, /*pointerIndex=*/0); |
Siarhei Vishniakou | 9306c38 | 2022-09-30 15:30:31 -0700 | [diff] [blame] | 1216 | |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 1217 | sp<WindowInfoHandle> touchedWindowHandle = |
| 1218 | findTouchedWindowAtLocked(displayId, x, y, isStylus); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1219 | if (touchedWindowHandle != nullptr && |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1220 | touchedWindowHandle->getApplicationToken() != |
| 1221 | mAwaitedFocusedApplication->getApplicationToken()) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1222 | // User touched a different application than the one we are waiting on. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1223 | ALOGI("Pruning input queue because user touched a different application while waiting " |
| 1224 | "for %s", |
| 1225 | mAwaitedFocusedApplication->getName().c_str()); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1226 | return true; |
| 1227 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1228 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1229 | // Alternatively, maybe there's a spy window that could handle this event. |
| 1230 | const std::vector<sp<WindowInfoHandle>> touchedSpies = |
| 1231 | findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus); |
| 1232 | for (const auto& windowHandle : touchedSpies) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 1233 | const std::shared_ptr<Connection> connection = |
| 1234 | getConnectionLocked(windowHandle->getToken()); |
Siarhei Vishniakou | 34ed4d4 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1235 | if (connection != nullptr && connection->responsive) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1236 | // This spy window could take more input. Drop all events preceding this |
| 1237 | // event, so that the spy window can get a chance to receive the stream. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1238 | ALOGW("Pruning the input queue because %s is unresponsive, but we have a " |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1239 | "responsive spy window that may handle the event.", |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1240 | mAwaitedFocusedApplication->getName().c_str()); |
| 1241 | return true; |
| 1242 | } |
| 1243 | } |
| 1244 | } |
| 1245 | |
| 1246 | // Prevent getting stuck: if we have a pending key event, and some motion events that have not |
| 1247 | // yet been processed by some connections, the dispatcher will wait for these motion |
| 1248 | // events to be processed before dispatching the key event. This is because these motion events |
| 1249 | // may cause a new window to be launched, which the user might expect to receive focus. |
| 1250 | // To prevent waiting forever for such events, just send the key to the currently focused window |
| 1251 | if (isPointerDownEvent && mKeyIsWaitingForEventsTimeout) { |
| 1252 | ALOGD("Received a new pointer down event, stop waiting for events to process and " |
| 1253 | "just send the pending key event to the focused window."); |
| 1254 | mKeyIsWaitingForEventsTimeout = now(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1255 | } |
| 1256 | return false; |
| 1257 | } |
| 1258 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1259 | bool InputDispatcher::enqueueInboundEventLocked(std::unique_ptr<EventEntry> newEntry) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1260 | bool needWake = mInboundQueue.empty(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1261 | mInboundQueue.push_back(std::move(newEntry)); |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1262 | const EventEntry& entry = *(mInboundQueue.back()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1263 | traceInboundQueueLengthLocked(); |
| 1264 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1265 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1266 | case EventEntry::Type::KEY: { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1267 | LOG_ALWAYS_FATAL_IF((entry.policyFlags & POLICY_FLAG_TRUSTED) == 0, |
| 1268 | "Unexpected untrusted event."); |
Siarhei Vishniakou | e2404a1 | 2024-01-16 18:38:39 -0800 | [diff] [blame] | 1269 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1270 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 1271 | if (mTracer) { |
| 1272 | ensureEventTraced(keyEntry); |
| 1273 | } |
Siarhei Vishniakou | 6520a58 | 2023-10-27 21:53:45 -0700 | [diff] [blame] | 1274 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 1275 | // If a new up event comes in, and the pending event with same key code has been asked |
| 1276 | // to try again later because of the policy. We have to reset the intercept key wake up |
| 1277 | // time for it may have been handled in the policy and could be dropped. |
| 1278 | if (keyEntry.action == AKEY_EVENT_ACTION_UP && mPendingEvent && |
| 1279 | mPendingEvent->type == EventEntry::Type::KEY) { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1280 | const KeyEntry& pendingKey = static_cast<const KeyEntry&>(*mPendingEvent); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 1281 | if (pendingKey.keyCode == keyEntry.keyCode && |
| 1282 | pendingKey.interceptKeyResult == |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1283 | KeyEntry::InterceptKeyResult::TRY_AGAIN_LATER) { |
| 1284 | pendingKey.interceptKeyResult = KeyEntry::InterceptKeyResult::UNKNOWN; |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 1285 | pendingKey.interceptKeyWakeupTime = 0; |
| 1286 | needWake = true; |
| 1287 | } |
| 1288 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1289 | break; |
| 1290 | } |
| 1291 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1292 | case EventEntry::Type::MOTION: { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1293 | LOG_ALWAYS_FATAL_IF((entry.policyFlags & POLICY_FLAG_TRUSTED) == 0, |
| 1294 | "Unexpected untrusted event."); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 1295 | const auto& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1296 | if (mTracer) { |
| 1297 | ensureEventTraced(motionEntry); |
| 1298 | } |
| 1299 | if (shouldPruneInboundQueueLocked(motionEntry)) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1300 | mNextUnblockedEvent = mInboundQueue.back(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1301 | needWake = true; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1302 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1303 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1304 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1305 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1306 | LOG_ALWAYS_FATAL("Focus events should be inserted using enqueueFocusEventLocked"); |
| 1307 | break; |
| 1308 | } |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1309 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1310 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1311 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1312 | case EventEntry::Type::SENSOR: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1313 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1314 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1315 | // nothing to do |
| 1316 | break; |
| 1317 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1318 | } |
| 1319 | |
| 1320 | return needWake; |
| 1321 | } |
| 1322 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1323 | void InputDispatcher::addRecentEventLocked(std::shared_ptr<const EventEntry> entry) { |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1324 | // Do not store sensor event in recent queue to avoid flooding the queue. |
| 1325 | if (entry->type != EventEntry::Type::SENSOR) { |
| 1326 | mRecentQueue.push_back(entry); |
| 1327 | } |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1328 | if (mRecentQueue.size() > RECENT_QUEUE_MAX_SIZE) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1329 | mRecentQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1330 | } |
| 1331 | } |
| 1332 | |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 1333 | sp<WindowInfoHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, float x, float y, |
| 1334 | bool isStylus, |
| 1335 | bool ignoreDragWindow) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1336 | // Traverse windows from front to back to find touched window. |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1337 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1338 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 1339 | if (ignoreDragWindow && haveSameToken(windowHandle, mDragState->dragWindow)) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1340 | continue; |
| 1341 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1342 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1343 | const WindowInfo& info = *windowHandle->getInfo(); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 1344 | if (!info.isSpy() && |
| 1345 | windowAcceptsTouchAt(info, displayId, x, y, isStylus, getTransformLocked(displayId))) { |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 1346 | return windowHandle; |
| 1347 | } |
| 1348 | } |
| 1349 | return nullptr; |
| 1350 | } |
| 1351 | |
| 1352 | std::vector<InputTarget> InputDispatcher::findOutsideTargetsLocked( |
Siarhei Vishniakou | 70f3d8c | 2023-09-19 15:36:52 -0700 | [diff] [blame] | 1353 | int32_t displayId, const sp<WindowInfoHandle>& touchedWindow, int32_t pointerId) const { |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 1354 | if (touchedWindow == nullptr) { |
| 1355 | return {}; |
| 1356 | } |
| 1357 | // Traverse windows from front to back until we encounter the touched window. |
| 1358 | std::vector<InputTarget> outsideTargets; |
| 1359 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
| 1360 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 1361 | if (windowHandle == touchedWindow) { |
| 1362 | // Stop iterating once we found a touched window. Any WATCH_OUTSIDE_TOUCH window |
| 1363 | // below the touched window will not get ACTION_OUTSIDE event. |
| 1364 | return outsideTargets; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1365 | } |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1366 | |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 1367 | const WindowInfo& info = *windowHandle->getInfo(); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1368 | if (info.inputConfig.test(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH)) { |
Siarhei Vishniakou | 70f3d8c | 2023-09-19 15:36:52 -0700 | [diff] [blame] | 1369 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
| 1370 | pointerIds.set(pointerId); |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 1371 | addPointerWindowTargetLocked(windowHandle, InputTarget::DispatchMode::OUTSIDE, |
| 1372 | ftl::Flags<InputTarget::Flags>(), pointerIds, |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 1373 | /*firstDownTimeInTarget=*/std::nullopt, outsideTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1374 | } |
| 1375 | } |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 1376 | return outsideTargets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1377 | } |
| 1378 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1379 | std::vector<sp<WindowInfoHandle>> InputDispatcher::findTouchedSpyWindowsAtLocked( |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 1380 | int32_t displayId, float x, float y, bool isStylus) const { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1381 | // Traverse windows from front to back and gather the touched spy windows. |
| 1382 | std::vector<sp<WindowInfoHandle>> spyWindows; |
| 1383 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
| 1384 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 1385 | const WindowInfo& info = *windowHandle->getInfo(); |
| 1386 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 1387 | if (!windowAcceptsTouchAt(info, displayId, x, y, isStylus, getTransformLocked(displayId))) { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1388 | continue; |
| 1389 | } |
| 1390 | if (!info.isSpy()) { |
| 1391 | // The first touched non-spy window was found, so return the spy windows touched so far. |
| 1392 | return spyWindows; |
| 1393 | } |
| 1394 | spyWindows.push_back(windowHandle); |
| 1395 | } |
| 1396 | return spyWindows; |
| 1397 | } |
| 1398 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1399 | void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1400 | const char* reason; |
| 1401 | switch (dropReason) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1402 | case DropReason::POLICY: |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 1403 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1404 | ALOGD("Dropped event because policy consumed it."); |
| 1405 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1406 | reason = "inbound event was dropped because the policy consumed it"; |
| 1407 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1408 | case DropReason::DISABLED: |
| 1409 | if (mLastDropReason != DropReason::DISABLED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1410 | ALOGI("Dropped event because input dispatch is disabled."); |
| 1411 | } |
| 1412 | reason = "inbound event was dropped because input dispatch is disabled"; |
| 1413 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1414 | case DropReason::BLOCKED: |
Siarhei Vishniakou | 99e407b | 2023-12-26 18:09:32 -0800 | [diff] [blame] | 1415 | LOG(INFO) << "Dropping because the current application is not responding and the user " |
| 1416 | "has started interacting with a different application: " |
| 1417 | << entry.getDescription(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1418 | reason = "inbound event was dropped because the current application is not responding " |
| 1419 | "and the user has started interacting with a different application"; |
| 1420 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1421 | case DropReason::STALE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1422 | ALOGI("Dropped event because it is stale."); |
| 1423 | reason = "inbound event was dropped because it is stale"; |
| 1424 | break; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1425 | case DropReason::NO_POINTER_CAPTURE: |
| 1426 | ALOGI("Dropped event because there is no window with Pointer Capture."); |
| 1427 | reason = "inbound event was dropped because there is no window with Pointer Capture"; |
| 1428 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1429 | case DropReason::NOT_DROPPED: { |
| 1430 | LOG_ALWAYS_FATAL("Should not be dropping a NOT_DROPPED event"); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1431 | return; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1432 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1433 | } |
| 1434 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1435 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1436 | case EventEntry::Type::KEY: { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1437 | CancelationOptions options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, reason); |
Hu Guo | 3cfa738 | 2023-11-15 09:50:04 +0000 | [diff] [blame] | 1438 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 1439 | options.displayId = keyEntry.displayId; |
| 1440 | options.deviceId = keyEntry.deviceId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1441 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1442 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1443 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1444 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1445 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1446 | if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1447 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, reason); |
Hu Guo | 3cfa738 | 2023-11-15 09:50:04 +0000 | [diff] [blame] | 1448 | options.displayId = motionEntry.displayId; |
| 1449 | options.deviceId = motionEntry.deviceId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1450 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1451 | } else { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1452 | CancelationOptions options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, |
| 1453 | reason); |
Hu Guo | 3cfa738 | 2023-11-15 09:50:04 +0000 | [diff] [blame] | 1454 | options.displayId = motionEntry.displayId; |
| 1455 | options.deviceId = motionEntry.deviceId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1456 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1457 | } |
| 1458 | break; |
| 1459 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1460 | case EventEntry::Type::SENSOR: { |
| 1461 | break; |
| 1462 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1463 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1464 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1465 | break; |
| 1466 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1467 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1468 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1469 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 1470 | case EventEntry::Type::DEVICE_RESET: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1471 | 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] | 1472 | break; |
| 1473 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1474 | } |
| 1475 | } |
| 1476 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1477 | bool InputDispatcher::haveCommandsLocked() const { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1478 | return !mCommandQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1479 | } |
| 1480 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1481 | bool InputDispatcher::runCommandsLockedInterruptable() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1482 | if (mCommandQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1483 | return false; |
| 1484 | } |
| 1485 | |
| 1486 | do { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1487 | auto command = std::move(mCommandQueue.front()); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1488 | mCommandQueue.pop_front(); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1489 | // Commands are run with the lock held, but may release and re-acquire the lock from within. |
| 1490 | command(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1491 | } while (!mCommandQueue.empty()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1492 | return true; |
| 1493 | } |
| 1494 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1495 | void InputDispatcher::postCommandLocked(Command&& command) { |
| 1496 | mCommandQueue.push_back(command); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1497 | } |
| 1498 | |
| 1499 | void InputDispatcher::drainInboundQueueLocked() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1500 | while (!mInboundQueue.empty()) { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1501 | std::shared_ptr<const EventEntry> entry = mInboundQueue.front(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1502 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1503 | releaseInboundEventLocked(entry); |
| 1504 | } |
| 1505 | traceInboundQueueLengthLocked(); |
| 1506 | } |
| 1507 | |
| 1508 | void InputDispatcher::releasePendingEventLocked() { |
| 1509 | if (mPendingEvent) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1510 | releaseInboundEventLocked(mPendingEvent); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1511 | mPendingEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1512 | } |
| 1513 | } |
| 1514 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1515 | void InputDispatcher::releaseInboundEventLocked(std::shared_ptr<const EventEntry> entry) { |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 1516 | const std::shared_ptr<InjectionState>& injectionState = entry->injectionState; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1517 | if (injectionState && injectionState->injectionResult == InputEventInjectionResult::PENDING) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1518 | if (DEBUG_DISPATCH_CYCLE) { |
| 1519 | ALOGD("Injected inbound event was dropped."); |
| 1520 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1521 | setInjectionResult(*entry, InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1522 | } |
| 1523 | if (entry == mNextUnblockedEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1524 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1525 | } |
| 1526 | addRecentEventLocked(entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1527 | } |
| 1528 | |
| 1529 | void InputDispatcher::resetKeyRepeatLocked() { |
| 1530 | if (mKeyRepeatState.lastKeyEntry) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1531 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1532 | } |
| 1533 | } |
| 1534 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1535 | std::shared_ptr<KeyEntry> InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1536 | std::shared_ptr<const KeyEntry> entry = mKeyRepeatState.lastKeyEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1537 | |
Michael Wright | 2e73295 | 2014-09-24 13:26:59 -0700 | [diff] [blame] | 1538 | uint32_t policyFlags = entry->policyFlags & |
| 1539 | (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1540 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1541 | std::shared_ptr<KeyEntry> newEntry = |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 1542 | std::make_unique<KeyEntry>(mIdGenerator.nextId(), /*injectionState=*/nullptr, |
| 1543 | currentTime, entry->deviceId, entry->source, |
| 1544 | entry->displayId, policyFlags, entry->action, entry->flags, |
| 1545 | entry->keyCode, entry->scanCode, entry->metaState, |
| 1546 | entry->repeatCount + 1, entry->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1547 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1548 | newEntry->syntheticRepeat = true; |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 1549 | if (mTracer) { |
| 1550 | newEntry->traceTracker = mTracer->traceInboundEvent(*newEntry); |
| 1551 | } |
| 1552 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1553 | mKeyRepeatState.lastKeyEntry = newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1554 | mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1555 | return newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1556 | } |
| 1557 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1558 | bool InputDispatcher::dispatchConfigurationChangedLocked(nsecs_t currentTime, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1559 | const ConfigurationChangedEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1560 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1561 | ALOGD("dispatchConfigurationChanged - eventTime=%" PRId64, entry.eventTime); |
| 1562 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1563 | |
| 1564 | // Reset key repeating in case a keyboard device was added or removed or something. |
| 1565 | resetKeyRepeatLocked(); |
| 1566 | |
| 1567 | // 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] | 1568 | auto command = [this, eventTime = entry.eventTime]() REQUIRES(mLock) { |
| 1569 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 1570 | mPolicy.notifyConfigurationChanged(eventTime); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1571 | }; |
| 1572 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1573 | return true; |
| 1574 | } |
| 1575 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1576 | bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime, |
| 1577 | const DeviceResetEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1578 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1579 | ALOGD("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry.eventTime, |
| 1580 | entry.deviceId); |
| 1581 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1582 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 1583 | // Reset key repeating in case a keyboard device was disabled or enabled. |
| 1584 | if (mKeyRepeatState.lastKeyEntry && mKeyRepeatState.lastKeyEntry->deviceId == entry.deviceId) { |
| 1585 | resetKeyRepeatLocked(); |
| 1586 | } |
| 1587 | |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1588 | CancelationOptions options(CancelationOptions::Mode::CANCEL_ALL_EVENTS, "device was reset"); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1589 | options.deviceId = entry.deviceId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1590 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 1591 | |
| 1592 | // Remove all active pointers from this device |
| 1593 | for (auto& [_, touchState] : mTouchStatesByDisplay) { |
| 1594 | touchState.removeAllPointersForDevice(entry.deviceId); |
| 1595 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1596 | return true; |
| 1597 | } |
| 1598 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1599 | void InputDispatcher::enqueueFocusEventLocked(const sp<IBinder>& windowToken, bool hasFocus, |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1600 | const std::string& reason) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1601 | if (mPendingEvent != nullptr) { |
| 1602 | // Move the pending event to the front of the queue. This will give the chance |
| 1603 | // for the pending event to get dispatched to the newly focused window |
| 1604 | mInboundQueue.push_front(mPendingEvent); |
| 1605 | mPendingEvent = nullptr; |
| 1606 | } |
| 1607 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1608 | std::unique_ptr<FocusEntry> focusEntry = |
| 1609 | std::make_unique<FocusEntry>(mIdGenerator.nextId(), now(), windowToken, hasFocus, |
| 1610 | reason); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1611 | |
| 1612 | // This event should go to the front of the queue, but behind all other focus events |
| 1613 | // Find the last focus event, and insert right after it |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1614 | auto it = std::find_if(mInboundQueue.rbegin(), mInboundQueue.rend(), |
| 1615 | [](const std::shared_ptr<const EventEntry>& event) { |
| 1616 | return event->type == EventEntry::Type::FOCUS; |
| 1617 | }); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1618 | |
| 1619 | // 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] | 1620 | mInboundQueue.insert(it.base(), std::move(focusEntry)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1621 | } |
| 1622 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1623 | void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime, |
| 1624 | std::shared_ptr<const FocusEntry> entry) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 1625 | std::shared_ptr<Connection> connection = getConnectionLocked(entry->connectionToken); |
| 1626 | if (connection == nullptr) { |
| 1627 | return; // Connection has gone away |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1628 | } |
| 1629 | InputTarget target; |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 1630 | target.connection = connection; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1631 | entry->dispatchInProgress = true; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1632 | std::string message = std::string("Focus ") + (entry->hasFocus ? "entering " : "leaving ") + |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 1633 | connection->getInputChannelName(); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 1634 | std::string reason = std::string("reason=").append(entry->reason); |
| 1635 | android_log_event_list(LOGTAG_INPUT_FOCUS) << message << reason << LOG_ID_EVENTS; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1636 | dispatchEventLocked(currentTime, entry, {target}); |
| 1637 | } |
| 1638 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1639 | void InputDispatcher::dispatchPointerCaptureChangedLocked( |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1640 | nsecs_t currentTime, const std::shared_ptr<const PointerCaptureChangedEntry>& entry, |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1641 | DropReason& dropReason) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1642 | dropReason = DropReason::NOT_DROPPED; |
| 1643 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1644 | const bool haveWindowWithPointerCapture = mWindowTokenWithPointerCapture != nullptr; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1645 | sp<IBinder> token; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1646 | |
| 1647 | if (entry->pointerCaptureRequest.enable) { |
| 1648 | // Enable Pointer Capture. |
| 1649 | if (haveWindowWithPointerCapture && |
| 1650 | (entry->pointerCaptureRequest == mCurrentPointerCaptureRequest)) { |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 1651 | // This can happen if pointer capture is disabled and re-enabled before we notify the |
| 1652 | // app of the state change, so there is no need to notify the app. |
| 1653 | ALOGI("Skipping dispatch of Pointer Capture being enabled: no state change."); |
| 1654 | return; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1655 | } |
| 1656 | if (!mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1657 | // This can happen if a window requests capture and immediately releases capture. |
| 1658 | ALOGW("No window requested Pointer Capture."); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1659 | dropReason = DropReason::NO_POINTER_CAPTURE; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1660 | return; |
| 1661 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1662 | if (entry->pointerCaptureRequest.seq != mCurrentPointerCaptureRequest.seq) { |
| 1663 | ALOGI("Skipping dispatch of Pointer Capture being enabled: sequence number mismatch."); |
| 1664 | return; |
| 1665 | } |
| 1666 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1667 | token = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1668 | LOG_ALWAYS_FATAL_IF(!token, "Cannot find focused window for Pointer Capture."); |
| 1669 | mWindowTokenWithPointerCapture = token; |
| 1670 | } else { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1671 | // Disable Pointer Capture. |
| 1672 | // We do not check if the sequence number matches for requests to disable Pointer Capture |
| 1673 | // for two reasons: |
| 1674 | // 1. Pointer Capture can be disabled by a focus change, which means we can get two entries |
| 1675 | // to disable capture with the same sequence number: one generated by |
| 1676 | // disablePointerCaptureForcedLocked() and another as an acknowledgement of Pointer |
| 1677 | // Capture being disabled in InputReader. |
| 1678 | // 2. We respect any request to disable Pointer Capture generated by InputReader, since the |
| 1679 | // actual Pointer Capture state that affects events being generated by input devices is |
| 1680 | // in InputReader. |
| 1681 | if (!haveWindowWithPointerCapture) { |
| 1682 | // Pointer capture was already forcefully disabled because of focus change. |
| 1683 | dropReason = DropReason::NOT_DROPPED; |
| 1684 | return; |
| 1685 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1686 | token = mWindowTokenWithPointerCapture; |
| 1687 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1688 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1689 | setPointerCaptureLocked(false); |
| 1690 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1691 | } |
| 1692 | |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 1693 | auto connection = getConnectionLocked(token); |
| 1694 | if (connection == nullptr) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1695 | // Window has gone away, clean up Pointer Capture state. |
| 1696 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1697 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1698 | setPointerCaptureLocked(false); |
| 1699 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1700 | return; |
| 1701 | } |
| 1702 | InputTarget target; |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 1703 | target.connection = connection; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1704 | entry->dispatchInProgress = true; |
| 1705 | dispatchEventLocked(currentTime, entry, {target}); |
| 1706 | |
| 1707 | dropReason = DropReason::NOT_DROPPED; |
| 1708 | } |
| 1709 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1710 | void InputDispatcher::dispatchTouchModeChangeLocked( |
| 1711 | nsecs_t currentTime, const std::shared_ptr<const TouchModeEntry>& entry) { |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1712 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 1713 | getWindowHandlesLocked(entry->displayId); |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1714 | if (windowHandles.empty()) { |
| 1715 | return; |
| 1716 | } |
| 1717 | const std::vector<InputTarget> inputTargets = |
| 1718 | getInputTargetsFromWindowHandlesLocked(windowHandles); |
| 1719 | if (inputTargets.empty()) { |
| 1720 | return; |
| 1721 | } |
| 1722 | entry->dispatchInProgress = true; |
| 1723 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1724 | } |
| 1725 | |
| 1726 | std::vector<InputTarget> InputDispatcher::getInputTargetsFromWindowHandlesLocked( |
| 1727 | const std::vector<sp<WindowInfoHandle>>& windowHandles) const { |
| 1728 | std::vector<InputTarget> inputTargets; |
| 1729 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1730 | const sp<IBinder>& token = handle->getToken(); |
| 1731 | if (token == nullptr) { |
| 1732 | continue; |
| 1733 | } |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 1734 | std::shared_ptr<Connection> connection = getConnectionLocked(token); |
| 1735 | if (connection == nullptr) { |
| 1736 | continue; // Connection has gone away |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1737 | } |
| 1738 | InputTarget target; |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 1739 | target.connection = connection; |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1740 | inputTargets.push_back(target); |
| 1741 | } |
| 1742 | return inputTargets; |
| 1743 | } |
| 1744 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1745 | bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, std::shared_ptr<const KeyEntry> entry, |
Siarhei Vishniakou | 6950596 | 2023-12-28 12:07:04 -0800 | [diff] [blame] | 1746 | DropReason* dropReason, nsecs_t& nextWakeupTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1747 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1748 | if (!entry->dispatchInProgress) { |
| 1749 | if (entry->repeatCount == 0 && entry->action == AKEY_EVENT_ACTION_DOWN && |
| 1750 | (entry->policyFlags & POLICY_FLAG_TRUSTED) && |
| 1751 | (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { |
| 1752 | if (mKeyRepeatState.lastKeyEntry && |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1753 | mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode && |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1754 | // We have seen two identical key downs in a row which indicates that the device |
| 1755 | // driver is automatically generating key repeats itself. We take note of the |
| 1756 | // repeat here, but we disable our own next key repeat timer since it is clear that |
| 1757 | // we will not need to synthesize key repeats ourselves. |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1758 | mKeyRepeatState.lastKeyEntry->deviceId == entry->deviceId) { |
| 1759 | // Make sure we don't get key down from a different device. If a different |
| 1760 | // device Id has same key pressed down, the new device Id will replace the |
| 1761 | // current one to hold the key repeat with repeat count reset. |
| 1762 | // In the future when got a KEY_UP on the device id, drop it and do not |
| 1763 | // stop the key repeat on current device. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1764 | entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; |
| 1765 | resetKeyRepeatLocked(); |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 1766 | mKeyRepeatState.nextRepeatTime = LLONG_MAX; // don't generate repeats ourselves |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1767 | } else { |
| 1768 | // Not a repeat. Save key down state in case we do see a repeat later. |
| 1769 | resetKeyRepeatLocked(); |
| 1770 | mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; |
| 1771 | } |
| 1772 | mKeyRepeatState.lastKeyEntry = entry; |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1773 | } else if (entry->action == AKEY_EVENT_ACTION_UP && mKeyRepeatState.lastKeyEntry && |
| 1774 | mKeyRepeatState.lastKeyEntry->deviceId != entry->deviceId) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1775 | // 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] | 1776 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1777 | ALOGD("deviceId=%d got KEY_UP as stale", entry->deviceId); |
| 1778 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1779 | } else if (!entry->syntheticRepeat) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1780 | resetKeyRepeatLocked(); |
| 1781 | } |
| 1782 | |
| 1783 | if (entry->repeatCount == 1) { |
| 1784 | entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; |
| 1785 | } else { |
| 1786 | entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; |
| 1787 | } |
| 1788 | |
| 1789 | entry->dispatchInProgress = true; |
| 1790 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1791 | logOutboundKeyDetails("dispatchKey - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1792 | } |
| 1793 | |
| 1794 | // 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] | 1795 | if (entry->interceptKeyResult == KeyEntry::InterceptKeyResult::TRY_AGAIN_LATER) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1796 | if (currentTime < entry->interceptKeyWakeupTime) { |
Siarhei Vishniakou | 6950596 | 2023-12-28 12:07:04 -0800 | [diff] [blame] | 1797 | nextWakeupTime = std::min(nextWakeupTime, entry->interceptKeyWakeupTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1798 | return false; // wait until next wakeup |
| 1799 | } |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1800 | entry->interceptKeyResult = KeyEntry::InterceptKeyResult::UNKNOWN; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1801 | entry->interceptKeyWakeupTime = 0; |
| 1802 | } |
| 1803 | |
| 1804 | // Give the policy a chance to intercept the key. |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1805 | if (entry->interceptKeyResult == KeyEntry::InterceptKeyResult::UNKNOWN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1806 | if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1807 | sp<IBinder> focusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1808 | mFocusResolver.getFocusedWindowToken(getTargetDisplayId(*entry)); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1809 | |
| 1810 | auto command = [this, focusedWindowToken, entry]() REQUIRES(mLock) { |
| 1811 | doInterceptKeyBeforeDispatchingCommand(focusedWindowToken, *entry); |
| 1812 | }; |
| 1813 | postCommandLocked(std::move(command)); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1814 | // Poke user activity for keys not passed to user |
| 1815 | pokeUserActivityLocked(*entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1816 | return false; // wait for the command to run |
| 1817 | } else { |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1818 | entry->interceptKeyResult = KeyEntry::InterceptKeyResult::CONTINUE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1819 | } |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1820 | } else if (entry->interceptKeyResult == KeyEntry::InterceptKeyResult::SKIP) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1821 | if (*dropReason == DropReason::NOT_DROPPED) { |
| 1822 | *dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1823 | } |
| 1824 | } |
| 1825 | |
| 1826 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1827 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1828 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1829 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1830 | : InputEventInjectionResult::FAILED); |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1831 | mReporter->reportDroppedKey(entry->id); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1832 | // Poke user activity for undispatched keys |
| 1833 | pokeUserActivityLocked(*entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1834 | return true; |
| 1835 | } |
| 1836 | |
| 1837 | // Identify targets. |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1838 | InputEventInjectionResult injectionResult; |
| 1839 | sp<WindowInfoHandle> focusedWindow = |
| 1840 | findFocusedWindowTargetLocked(currentTime, *entry, nextWakeupTime, |
| 1841 | /*byref*/ injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1842 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1843 | return false; |
| 1844 | } |
| 1845 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1846 | setInjectionResult(*entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1847 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1848 | return true; |
| 1849 | } |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1850 | LOG_ALWAYS_FATAL_IF(focusedWindow == nullptr); |
| 1851 | |
| 1852 | std::vector<InputTarget> inputTargets; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 1853 | addWindowTargetLocked(focusedWindow, InputTarget::DispatchMode::AS_IS, |
| 1854 | InputTarget::Flags::FOREGROUND, getDownTime(*entry), inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1855 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1856 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1857 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1858 | |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 1859 | if (mTracer) { |
| 1860 | ensureEventTraced(*entry); |
| 1861 | for (const auto& target : inputTargets) { |
| 1862 | mTracer->dispatchToTargetHint(*entry->traceTracker, target); |
| 1863 | } |
| 1864 | } |
| 1865 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1866 | // Dispatch the key. |
| 1867 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1868 | return true; |
| 1869 | } |
| 1870 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1871 | void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1872 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1873 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 ", " |
| 1874 | "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, " |
| 1875 | "metaState=0x%x, repeatCount=%d, downTime=%" PRId64, |
| 1876 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, |
| 1877 | entry.policyFlags, entry.action, entry.flags, entry.keyCode, entry.scanCode, |
| 1878 | entry.metaState, entry.repeatCount, entry.downTime); |
| 1879 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1880 | } |
| 1881 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1882 | void InputDispatcher::dispatchSensorLocked(nsecs_t currentTime, |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1883 | const std::shared_ptr<const SensorEntry>& entry, |
Siarhei Vishniakou | 6950596 | 2023-12-28 12:07:04 -0800 | [diff] [blame] | 1884 | DropReason* dropReason, nsecs_t& nextWakeupTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1885 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1886 | ALOGD("notifySensorEvent eventTime=%" PRId64 ", hwTimestamp=%" PRId64 ", deviceId=%d, " |
| 1887 | "source=0x%x, sensorType=%s", |
| 1888 | entry->eventTime, entry->hwTimestamp, entry->deviceId, entry->source, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1889 | ftl::enum_string(entry->sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1890 | } |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1891 | auto command = [this, entry]() REQUIRES(mLock) { |
| 1892 | scoped_unlock unlock(mLock); |
| 1893 | |
| 1894 | if (entry->accuracyChanged) { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 1895 | mPolicy.notifySensorAccuracy(entry->deviceId, entry->sensorType, entry->accuracy); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1896 | } |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 1897 | mPolicy.notifySensorEvent(entry->deviceId, entry->sensorType, entry->accuracy, |
| 1898 | entry->hwTimestamp, entry->values); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1899 | }; |
| 1900 | postCommandLocked(std::move(command)); |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1901 | } |
| 1902 | |
| 1903 | bool InputDispatcher::flushSensor(int deviceId, InputDeviceSensorType sensorType) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1904 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1905 | ALOGD("flushSensor deviceId=%d, sensorType=%s", deviceId, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1906 | ftl::enum_string(sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1907 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1908 | { // acquire lock |
| 1909 | std::scoped_lock _l(mLock); |
| 1910 | |
| 1911 | for (auto it = mInboundQueue.begin(); it != mInboundQueue.end(); it++) { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1912 | std::shared_ptr<const EventEntry> entry = *it; |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1913 | if (entry->type == EventEntry::Type::SENSOR) { |
| 1914 | it = mInboundQueue.erase(it); |
| 1915 | releaseInboundEventLocked(entry); |
| 1916 | } |
| 1917 | } |
| 1918 | } |
| 1919 | return true; |
| 1920 | } |
| 1921 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1922 | bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, |
| 1923 | std::shared_ptr<const MotionEntry> entry, |
Siarhei Vishniakou | 6950596 | 2023-12-28 12:07:04 -0800 | [diff] [blame] | 1924 | DropReason* dropReason, nsecs_t& nextWakeupTime) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1925 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1926 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1927 | if (!entry->dispatchInProgress) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1928 | entry->dispatchInProgress = true; |
| 1929 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1930 | logOutboundMotionDetails("dispatchMotion - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1931 | } |
| 1932 | |
| 1933 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1934 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1935 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1936 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1937 | : InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1938 | return true; |
| 1939 | } |
| 1940 | |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 1941 | const bool isPointerEvent = isFromSource(entry->source, AINPUT_SOURCE_CLASS_POINTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1942 | |
| 1943 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1944 | std::vector<InputTarget> inputTargets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1945 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1946 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1947 | if (isPointerEvent) { |
| 1948 | // Pointer event. (eg. touchscreen) |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 1949 | |
| 1950 | if (mDragState && |
| 1951 | (entry->action & AMOTION_EVENT_ACTION_MASK) == AMOTION_EVENT_ACTION_POINTER_DOWN) { |
| 1952 | // If drag and drop ongoing and pointer down occur: pilfer drag window pointers |
| 1953 | pilferPointersLocked(mDragState->dragWindow->getToken()); |
| 1954 | } |
| 1955 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1956 | inputTargets = |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 1957 | findTouchedWindowTargetsLocked(currentTime, *entry, /*byref*/ injectionResult); |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 1958 | LOG_ALWAYS_FATAL_IF(injectionResult != InputEventInjectionResult::SUCCEEDED && |
| 1959 | !inputTargets.empty()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1960 | } else { |
| 1961 | // Non touch event. (eg. trackball) |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1962 | sp<WindowInfoHandle> focusedWindow = |
| 1963 | findFocusedWindowTargetLocked(currentTime, *entry, nextWakeupTime, injectionResult); |
| 1964 | if (injectionResult == InputEventInjectionResult::SUCCEEDED) { |
| 1965 | LOG_ALWAYS_FATAL_IF(focusedWindow == nullptr); |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 1966 | addWindowTargetLocked(focusedWindow, InputTarget::DispatchMode::AS_IS, |
| 1967 | InputTarget::Flags::FOREGROUND, getDownTime(*entry), |
| 1968 | inputTargets); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1969 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1970 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1971 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1972 | return false; |
| 1973 | } |
| 1974 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1975 | setInjectionResult(*entry, injectionResult); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1976 | if (injectionResult == InputEventInjectionResult::TARGET_MISMATCH) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1977 | return true; |
| 1978 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1979 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1980 | CancelationOptions::Mode mode( |
| 1981 | isPointerEvent ? CancelationOptions::Mode::CANCEL_POINTER_EVENTS |
| 1982 | : CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS); |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1983 | CancelationOptions options(mode, "input event injection failed"); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 1984 | options.displayId = entry->displayId; |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1985 | synthesizeCancelationEventsForMonitorsLocked(options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1986 | return true; |
| 1987 | } |
| 1988 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1989 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1990 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1991 | |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 1992 | if (mTracer) { |
| 1993 | ensureEventTraced(*entry); |
| 1994 | for (const auto& target : inputTargets) { |
| 1995 | mTracer->dispatchToTargetHint(*entry->traceTracker, target); |
| 1996 | } |
| 1997 | } |
| 1998 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1999 | // Dispatch the motion. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2000 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 2001 | return true; |
| 2002 | } |
| 2003 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2004 | void InputDispatcher::enqueueDragEventLocked(const sp<WindowInfoHandle>& windowHandle, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2005 | bool isExiting, const int32_t rawX, |
| 2006 | const int32_t rawY) { |
| 2007 | const vec2 xy = windowHandle->getInfo()->transform.transform(vec2(rawX, rawY)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2008 | std::unique_ptr<DragEntry> dragEntry = |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2009 | std::make_unique<DragEntry>(mIdGenerator.nextId(), now(), windowHandle->getToken(), |
| 2010 | isExiting, xy.x, xy.y); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2011 | |
| 2012 | enqueueInboundEventLocked(std::move(dragEntry)); |
| 2013 | } |
| 2014 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 2015 | void InputDispatcher::dispatchDragLocked(nsecs_t currentTime, |
| 2016 | std::shared_ptr<const DragEntry> entry) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 2017 | std::shared_ptr<Connection> connection = getConnectionLocked(entry->connectionToken); |
| 2018 | if (connection == nullptr) { |
| 2019 | return; // Connection has gone away |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2020 | } |
| 2021 | InputTarget target; |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 2022 | target.connection = connection; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2023 | entry->dispatchInProgress = true; |
| 2024 | dispatchEventLocked(currentTime, entry, {target}); |
| 2025 | } |
| 2026 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2027 | void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2028 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 2029 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=%s, displayId=%" PRId32 |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2030 | ", policyFlags=0x%x, " |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2031 | "action=%s, actionButton=0x%x, flags=0x%x, " |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2032 | "metaState=0x%x, buttonState=0x%x," |
| 2033 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64, |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 2034 | prefix, entry.eventTime, entry.deviceId, |
| 2035 | inputEventSourceToString(entry.source).c_str(), entry.displayId, entry.policyFlags, |
| 2036 | MotionEvent::actionToString(entry.action).c_str(), entry.actionButton, entry.flags, |
| 2037 | entry.metaState, entry.buttonState, entry.edgeFlags, entry.xPrecision, |
| 2038 | entry.yPrecision, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2039 | |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 2040 | for (uint32_t i = 0; i < entry.getPointerCount(); i++) { |
Siarhei Vishniakou | 09a8fe4 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2041 | ALOGD(" Pointer %d: id=%d, toolType=%s, " |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2042 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 2043 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 2044 | "orientation=%f", |
Siarhei Vishniakou | 09a8fe4 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2045 | i, entry.pointerProperties[i].id, |
| 2046 | ftl::enum_string(entry.pointerProperties[i].toolType).c_str(), |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2047 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 2048 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 2049 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 2050 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 2051 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 2052 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 2053 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 2054 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 2055 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| 2056 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2057 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2058 | } |
| 2059 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2060 | void InputDispatcher::dispatchEventLocked(nsecs_t currentTime, |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 2061 | std::shared_ptr<const EventEntry> eventEntry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2062 | const std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2063 | ATRACE_CALL(); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2064 | if (DEBUG_DISPATCH_CYCLE) { |
| 2065 | ALOGD("dispatchEventToCurrentInputTargets"); |
| 2066 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2067 | |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 2068 | processInteractionsLocked(*eventEntry, inputTargets); |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 2069 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2070 | ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true |
| 2071 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2072 | pokeUserActivityLocked(*eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2073 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2074 | for (const InputTarget& inputTarget : inputTargets) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 2075 | std::shared_ptr<Connection> connection = inputTarget.connection; |
| 2076 | prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2077 | } |
| 2078 | } |
| 2079 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 2080 | void InputDispatcher::cancelEventsForAnrLocked(const std::shared_ptr<Connection>& connection) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2081 | // We will not be breaking any connections here, even if the policy wants us to abort dispatch. |
| 2082 | // If the policy decides to close the app, we will get a channel removal event via |
| 2083 | // unregisterInputChannel, and will clean up the connection that way. We are already not |
| 2084 | // sending new pointers to the connection when it blocked, but focused events will continue to |
| 2085 | // pile up. |
| 2086 | ALOGW("Canceling events for %s because it is unresponsive", |
Siarhei Vishniakou | 10bfdc9 | 2024-02-01 11:12:57 -0800 | [diff] [blame^] | 2087 | connection->getInputChannelName().c_str()); |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 2088 | if (connection->status == Connection::Status::NORMAL) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 2089 | CancelationOptions options(CancelationOptions::Mode::CANCEL_ALL_EVENTS, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2090 | "application not responding"); |
| 2091 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2092 | } |
| 2093 | } |
| 2094 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2095 | void InputDispatcher::resetNoFocusedWindowTimeoutLocked() { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2096 | if (DEBUG_FOCUS) { |
| 2097 | ALOGD("Resetting ANR timeouts."); |
| 2098 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2099 | |
| 2100 | // Reset input target wait timeout. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2101 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2102 | mAwaitedFocusedApplication.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2103 | } |
| 2104 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2105 | /** |
| 2106 | * Get the display id that the given event should go to. If this event specifies a valid display id, |
| 2107 | * then it should be dispatched to that display. Otherwise, the event goes to the focused display. |
| 2108 | * Focused display is the display that the user most recently interacted with. |
| 2109 | */ |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2110 | int32_t InputDispatcher::getTargetDisplayId(const EventEntry& entry) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2111 | int32_t displayId; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2112 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2113 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2114 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 2115 | displayId = keyEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2116 | break; |
| 2117 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2118 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2119 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 2120 | displayId = motionEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2121 | break; |
| 2122 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 2123 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 2124 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2125 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2126 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2127 | case EventEntry::Type::DEVICE_RESET: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2128 | case EventEntry::Type::SENSOR: |
| 2129 | case EventEntry::Type::DRAG: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2130 | 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] | 2131 | return ADISPLAY_ID_NONE; |
| 2132 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2133 | } |
| 2134 | return displayId == ADISPLAY_ID_NONE ? mFocusedDisplayId : displayId; |
| 2135 | } |
| 2136 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2137 | bool InputDispatcher::shouldWaitToSendKeyLocked(nsecs_t currentTime, |
| 2138 | const char* focusedWindowName) { |
| 2139 | if (mAnrTracker.empty()) { |
| 2140 | // already processed all events that we waited for |
| 2141 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 2142 | return false; |
| 2143 | } |
| 2144 | |
| 2145 | if (!mKeyIsWaitingForEventsTimeout.has_value()) { |
| 2146 | // Start the timer |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 2147 | // 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] | 2148 | mKeyIsWaitingForEventsTimeout = currentTime + |
| 2149 | std::chrono::duration_cast<std::chrono::nanoseconds>(KEY_WAITING_FOR_EVENTS_TIMEOUT) |
| 2150 | .count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2151 | return true; |
| 2152 | } |
| 2153 | |
| 2154 | // We still have pending events, and already started the timer |
| 2155 | if (currentTime < *mKeyIsWaitingForEventsTimeout) { |
| 2156 | return true; // Still waiting |
| 2157 | } |
| 2158 | |
| 2159 | // Waited too long, and some connection still hasn't processed all motions |
| 2160 | // Just send the key to the focused window |
| 2161 | ALOGW("Dispatching key to %s even though there are other unprocessed events", |
| 2162 | focusedWindowName); |
| 2163 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 2164 | return false; |
| 2165 | } |
| 2166 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2167 | sp<WindowInfoHandle> InputDispatcher::findFocusedWindowTargetLocked( |
Siarhei Vishniakou | 6950596 | 2023-12-28 12:07:04 -0800 | [diff] [blame] | 2168 | nsecs_t currentTime, const EventEntry& entry, nsecs_t& nextWakeupTime, |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2169 | InputEventInjectionResult& outInjectionResult) { |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2170 | outInjectionResult = InputEventInjectionResult::FAILED; // Default result |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2171 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2172 | int32_t displayId = getTargetDisplayId(entry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2173 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2174 | std::shared_ptr<InputApplicationHandle> focusedApplicationHandle = |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2175 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 2176 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2177 | // If there is no currently focused window and no focused application |
| 2178 | // then drop the event. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2179 | if (focusedWindowHandle == nullptr && focusedApplicationHandle == nullptr) { |
| 2180 | ALOGI("Dropping %s event because there is no focused window or focused application in " |
| 2181 | "display %" PRId32 ".", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2182 | ftl::enum_string(entry.type).c_str(), displayId); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2183 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2184 | } |
| 2185 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2186 | // Drop key events if requested by input feature |
| 2187 | if (focusedWindowHandle != nullptr && shouldDropInput(entry, focusedWindowHandle)) { |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2188 | return nullptr; |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2189 | } |
| 2190 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2191 | // Compatibility behavior: raise ANR if there is a focused application, but no focused window. |
| 2192 | // Only start counting when we have a focused event to dispatch. The ANR is canceled if we |
| 2193 | // start interacting with another application via touch (app switch). This code can be removed |
| 2194 | // if the "no focused window ANR" is moved to the policy. Input doesn't know whether |
| 2195 | // an app is expected to have a focused window. |
| 2196 | if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) { |
| 2197 | if (!mNoFocusedWindowTimeoutTime.has_value()) { |
| 2198 | // 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] | 2199 | std::chrono::nanoseconds timeout = focusedApplicationHandle->getDispatchingTimeout( |
| 2200 | DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
| 2201 | mNoFocusedWindowTimeoutTime = currentTime + timeout.count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2202 | mAwaitedFocusedApplication = focusedApplicationHandle; |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 2203 | mAwaitedApplicationDisplayId = displayId; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2204 | ALOGW("Waiting because no window has focus but %s may eventually add a " |
| 2205 | "window when it finishes starting up. Will wait for %" PRId64 "ms", |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 2206 | mAwaitedFocusedApplication->getName().c_str(), millis(timeout)); |
Siarhei Vishniakou | 6950596 | 2023-12-28 12:07:04 -0800 | [diff] [blame] | 2207 | nextWakeupTime = std::min(nextWakeupTime, *mNoFocusedWindowTimeoutTime); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2208 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 2209 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2210 | } else if (currentTime > *mNoFocusedWindowTimeoutTime) { |
| 2211 | // Already raised ANR. Drop the event |
| 2212 | ALOGE("Dropping %s event because there is no focused window", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2213 | ftl::enum_string(entry.type).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2214 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2215 | } else { |
| 2216 | // Still waiting for the focused window |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2217 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 2218 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2219 | } |
| 2220 | } |
| 2221 | |
| 2222 | // we have a valid, non-null focused window |
| 2223 | resetNoFocusedWindowTimeoutLocked(); |
| 2224 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2225 | // Verify targeted injection. |
| 2226 | if (const auto err = verifyTargetedInjection(focusedWindowHandle, entry); err) { |
| 2227 | ALOGW("Dropping injected event: %s", (*err).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2228 | outInjectionResult = InputEventInjectionResult::TARGET_MISMATCH; |
| 2229 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2230 | } |
| 2231 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2232 | if (focusedWindowHandle->getInfo()->inputConfig.test( |
| 2233 | WindowInfo::InputConfig::PAUSE_DISPATCHING)) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2234 | ALOGI("Waiting because %s is paused", focusedWindowHandle->getName().c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2235 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 2236 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2237 | } |
| 2238 | |
| 2239 | // If the event is a key event, then we must wait for all previous events to |
| 2240 | // complete before delivering it because previous events may have the |
| 2241 | // side-effect of transferring focus to a different window and we want to |
| 2242 | // ensure that the following keys are sent to the new window. |
| 2243 | // |
| 2244 | // Suppose the user touches a button in a window then immediately presses "A". |
| 2245 | // If the button causes a pop-up window to appear then we want to ensure that |
| 2246 | // the "A" key is delivered to the new pop-up window. This is because users |
| 2247 | // often anticipate pending UI changes when typing on a keyboard. |
| 2248 | // To obtain this behavior, we must serialize key events with respect to all |
| 2249 | // prior input events. |
| 2250 | if (entry.type == EventEntry::Type::KEY) { |
| 2251 | if (shouldWaitToSendKeyLocked(currentTime, focusedWindowHandle->getName().c_str())) { |
Siarhei Vishniakou | 6950596 | 2023-12-28 12:07:04 -0800 | [diff] [blame] | 2252 | nextWakeupTime = std::min(nextWakeupTime, *mKeyIsWaitingForEventsTimeout); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2253 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 2254 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2255 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2256 | } |
| 2257 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2258 | outInjectionResult = InputEventInjectionResult::SUCCEEDED; |
| 2259 | return focusedWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2260 | } |
| 2261 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2262 | /** |
| 2263 | * Given a list of monitors, remove the ones we cannot find a connection for, and the ones |
| 2264 | * that are currently unresponsive. |
| 2265 | */ |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2266 | std::vector<Monitor> InputDispatcher::selectResponsiveMonitorsLocked( |
| 2267 | const std::vector<Monitor>& monitors) const { |
| 2268 | std::vector<Monitor> responsiveMonitors; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2269 | std::copy_if(monitors.begin(), monitors.end(), std::back_inserter(responsiveMonitors), |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 2270 | [](const Monitor& monitor) REQUIRES(mLock) { |
| 2271 | std::shared_ptr<Connection> connection = monitor.connection; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2272 | if (!connection->responsive) { |
| 2273 | ALOGW("Unresponsive monitor %s will not get the new gesture", |
Siarhei Vishniakou | 10bfdc9 | 2024-02-01 11:12:57 -0800 | [diff] [blame^] | 2274 | connection->getInputChannelName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2275 | return false; |
| 2276 | } |
| 2277 | return true; |
| 2278 | }); |
| 2279 | return responsiveMonitors; |
| 2280 | } |
| 2281 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2282 | std::vector<InputTarget> InputDispatcher::findTouchedWindowTargetsLocked( |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2283 | nsecs_t currentTime, const MotionEntry& entry, |
Siarhei Vishniakou | 4fe5739 | 2022-10-25 13:44:30 -0700 | [diff] [blame] | 2284 | InputEventInjectionResult& outInjectionResult) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2285 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2286 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2287 | std::vector<InputTarget> targets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2288 | // For security reasons, we defer updating the touch state until we are sure that |
| 2289 | // event injection will be allowed. |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2290 | const int32_t displayId = entry.displayId; |
| 2291 | const int32_t action = entry.action; |
Siarhei Vishniakou | bf88052 | 2023-05-01 11:03:22 -0700 | [diff] [blame] | 2292 | const int32_t maskedAction = MotionEvent::getActionMasked(action); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2293 | |
| 2294 | // 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] | 2295 | outInjectionResult = InputEventInjectionResult::PENDING; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2296 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2297 | // Copy current touch state into tempTouchState. |
| 2298 | // This state will be used to update mTouchStatesByDisplay at the end of this function. |
| 2299 | // 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] | 2300 | const TouchState* oldState = nullptr; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2301 | TouchState tempTouchState; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2302 | if (const auto it = mTouchStatesByDisplay.find(displayId); it != mTouchStatesByDisplay.end()) { |
| 2303 | oldState = &(it->second); |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 2304 | tempTouchState = *oldState; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2305 | } |
| 2306 | |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 2307 | bool isSplit = shouldSplitTouch(tempTouchState, entry); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2308 | |
| 2309 | const bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE || |
| 2310 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2311 | maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2312 | // A DOWN could be generated from POINTER_DOWN if the initial pointers did not land into any |
| 2313 | // touchable windows. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2314 | const bool wasDown = oldState != nullptr && oldState->isDown(entry.deviceId); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2315 | const bool isDown = (maskedAction == AMOTION_EVENT_ACTION_DOWN) || |
| 2316 | (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN && !wasDown); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2317 | const bool newGesture = isDown || maskedAction == AMOTION_EVENT_ACTION_SCROLL || |
| 2318 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2319 | maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE; |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 2320 | const bool isFromMouse = isFromSource(entry.source, AINPUT_SOURCE_MOUSE); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2321 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2322 | if (newGesture) { |
| 2323 | isSplit = false; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2324 | } |
| 2325 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2326 | if (isDown && tempTouchState.hasHoveringPointers(entry.deviceId)) { |
| 2327 | // Compatibility behaviour: ACTION_DOWN causes HOVER_EXIT to get generated. |
| 2328 | tempTouchState.clearHoveringPointers(entry.deviceId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2329 | } |
| 2330 | |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2331 | if (isHoverAction) { |
Siarhei Vishniakou | 7be50c9 | 2023-11-17 17:09:08 -0800 | [diff] [blame] | 2332 | if (wasDown) { |
| 2333 | // Started hovering, but the device is already down: reject the hover event |
| 2334 | LOG(ERROR) << "Got hover event " << entry.getDescription() |
| 2335 | << " but the device is already down " << oldState->dump(); |
| 2336 | outInjectionResult = InputEventInjectionResult::FAILED; |
| 2337 | return {}; |
| 2338 | } |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2339 | // For hover actions, we will treat 'tempTouchState' as a new state, so let's erase |
| 2340 | // all of the existing hovering pointers and recompute. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2341 | tempTouchState.clearHoveringPointers(entry.deviceId); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2342 | } |
| 2343 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2344 | if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { |
| 2345 | /* 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] | 2346 | const auto [x, y] = resolveTouchedPosition(entry); |
Siarhei Vishniakou | 5b9766d | 2023-07-18 14:06:29 -0700 | [diff] [blame] | 2347 | const int32_t pointerIndex = MotionEvent::getActionIndex(action); |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2348 | const PointerProperties& pointer = entry.pointerProperties[pointerIndex]; |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2349 | // Outside targets should be added upon first dispatched DOWN event. That means, this should |
| 2350 | // 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] | 2351 | const bool isStylus = isPointerFromStylus(entry, pointerIndex); |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 2352 | sp<WindowInfoHandle> newTouchedWindowHandle = |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2353 | findTouchedWindowAtLocked(displayId, x, y, isStylus); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2354 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2355 | if (isDown) { |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2356 | targets += findOutsideTargetsLocked(displayId, newTouchedWindowHandle, pointer.id); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2357 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2358 | // Handle the case where we did not find a window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2359 | if (newTouchedWindowHandle == nullptr) { |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 2360 | 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] | 2361 | // 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] | 2362 | newTouchedWindowHandle = tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2363 | } |
| 2364 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2365 | // Verify targeted injection. |
| 2366 | if (const auto err = verifyTargetedInjection(newTouchedWindowHandle, entry); err) { |
| 2367 | ALOGW("Dropping injected touch event: %s", (*err).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2368 | outInjectionResult = os::InputEventInjectionResult::TARGET_MISMATCH; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2369 | newTouchedWindowHandle = nullptr; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2370 | return {}; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2371 | } |
| 2372 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2373 | // Figure out whether splitting will be allowed for this window. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2374 | if (newTouchedWindowHandle != nullptr) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2375 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
| 2376 | // New window supports splitting, but we should never split mouse events. |
| 2377 | isSplit = !isFromMouse; |
| 2378 | } else if (isSplit) { |
| 2379 | // New window does not support splitting but we have already split events. |
| 2380 | // Ignore the new window. |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame] | 2381 | LOG(INFO) << "Skipping " << newTouchedWindowHandle->getName() |
| 2382 | << " because it doesn't support split touch"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2383 | newTouchedWindowHandle = nullptr; |
| 2384 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2385 | } else { |
| 2386 | // 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] | 2387 | // be delivered to a new window which supports split touch. Pointers from a mouse device |
| 2388 | // should never be split. |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 2389 | isSplit = !isFromMouse; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2390 | } |
| 2391 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2392 | std::vector<sp<WindowInfoHandle>> newTouchedWindows = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2393 | findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2394 | if (newTouchedWindowHandle != nullptr) { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2395 | // Process the foreground window first so that it is the first to receive the event. |
| 2396 | newTouchedWindows.insert(newTouchedWindows.begin(), newTouchedWindowHandle); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2397 | } |
| 2398 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2399 | if (newTouchedWindows.empty()) { |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 2400 | ALOGI("Dropping event because there is no touchable window at (%.1f, %.1f) on display " |
| 2401 | "%d.", |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2402 | x, y, displayId); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2403 | outInjectionResult = InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2404 | return {}; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2405 | } |
| 2406 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2407 | for (const sp<WindowInfoHandle>& windowHandle : newTouchedWindows) { |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 2408 | if (!canWindowReceiveMotionLocked(windowHandle, entry)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2409 | continue; |
| 2410 | } |
| 2411 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2412 | if (isHoverAction) { |
Siarhei Vishniakou | b681c20 | 2023-05-01 11:22:33 -0700 | [diff] [blame] | 2413 | // The "windowHandle" is the target of this hovering pointer. |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2414 | tempTouchState.addHoveringPointerToWindow(windowHandle, entry.deviceId, pointer); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2415 | } |
| 2416 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2417 | // Set target flags. |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2418 | ftl::Flags<InputTarget::Flags> targetFlags; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2419 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2420 | if (canReceiveForegroundTouches(*windowHandle->getInfo())) { |
| 2421 | // 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] | 2422 | targetFlags |= InputTarget::Flags::FOREGROUND; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2423 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2424 | |
| 2425 | if (isSplit) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2426 | targetFlags |= InputTarget::Flags::SPLIT; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2427 | } |
| 2428 | if (isWindowObscuredAtPointLocked(windowHandle, x, y)) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2429 | targetFlags |= InputTarget::Flags::WINDOW_IS_OBSCURED; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2430 | } else if (isWindowObscuredLocked(windowHandle)) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2431 | targetFlags |= InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2432 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2433 | |
| 2434 | // Update the temporary touch state. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2435 | |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2436 | if (!isHoverAction) { |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2437 | const bool isDownOrPointerDown = maskedAction == AMOTION_EVENT_ACTION_DOWN || |
| 2438 | maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2439 | tempTouchState.addOrUpdateWindow(windowHandle, InputTarget::DispatchMode::AS_IS, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2440 | targetFlags, entry.deviceId, {pointer}, |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2441 | isDownOrPointerDown |
| 2442 | ? std::make_optional(entry.eventTime) |
| 2443 | : std::nullopt); |
| 2444 | // If this is the pointer going down and the touched window has a wallpaper |
| 2445 | // then also add the touched wallpaper windows so they are locked in for the |
| 2446 | // duration of the touch gesture. We do not collect wallpapers during HOVER_MOVE or |
| 2447 | // SCROLL because the wallpaper engine only supports touch events. We would need to |
| 2448 | // add a mechanism similar to View.onGenericMotionEvent to enable wallpapers to |
| 2449 | // handle these events. |
| 2450 | if (isDownOrPointerDown && targetFlags.test(InputTarget::Flags::FOREGROUND) && |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2451 | windowHandle->getInfo()->inputConfig.test( |
| 2452 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER)) { |
| 2453 | sp<WindowInfoHandle> wallpaper = findWallpaperWindowBelow(windowHandle); |
| 2454 | if (wallpaper != nullptr) { |
| 2455 | ftl::Flags<InputTarget::Flags> wallpaperFlags = |
| 2456 | InputTarget::Flags::WINDOW_IS_OBSCURED | |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2457 | InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED; |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2458 | if (isSplit) { |
| 2459 | wallpaperFlags |= InputTarget::Flags::SPLIT; |
| 2460 | } |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2461 | tempTouchState.addOrUpdateWindow(wallpaper, |
| 2462 | InputTarget::DispatchMode::AS_IS, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2463 | wallpaperFlags, entry.deviceId, {pointer}, |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2464 | entry.eventTime); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2465 | } |
| 2466 | } |
| 2467 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2468 | } |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 2469 | |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 2470 | // If a window is already pilfering some pointers, give it this new pointer as well and |
| 2471 | // make it pilfering. This will prevent other non-spy windows from getting this pointer, |
| 2472 | // which is a specific behaviour that we want. |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 2473 | for (TouchedWindow& touchedWindow : tempTouchState.windows) { |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2474 | if (touchedWindow.hasTouchingPointer(entry.deviceId, pointer.id) && |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 2475 | touchedWindow.hasPilferingPointers(entry.deviceId)) { |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 2476 | // This window is already pilfering some pointers, and this new pointer is also |
| 2477 | // going to it. Therefore, take over this pointer and don't give it to anyone |
| 2478 | // else. |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2479 | touchedWindow.addPilferingPointer(entry.deviceId, pointer.id); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 2480 | } |
| 2481 | } |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 2482 | |
| 2483 | // Restrict all pilfered pointers to the pilfering windows. |
| 2484 | tempTouchState.cancelPointersForNonPilferingWindows(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2485 | } else { |
| 2486 | /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ |
| 2487 | |
| 2488 | // If the pointer is not currently down, then ignore the event. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2489 | if (!tempTouchState.isDown(entry.deviceId) && |
| 2490 | maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT) { |
Siarhei Vishniakou | 31dd155 | 2023-10-30 18:46:10 -0700 | [diff] [blame] | 2491 | if (DEBUG_DROPPED_EVENTS_VERBOSE) { |
| 2492 | LOG(INFO) << "Dropping event because the pointer for device " << entry.deviceId |
| 2493 | << " is not down or we previously dropped the pointer down event in " |
| 2494 | << "display " << displayId << ": " << entry.getDescription(); |
| 2495 | } |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2496 | outInjectionResult = InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2497 | return {}; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2498 | } |
| 2499 | |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2500 | // If the pointer is not currently hovering, then ignore the event. |
| 2501 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 2502 | const int32_t pointerId = entry.pointerProperties[0].id; |
| 2503 | if (oldState == nullptr || |
| 2504 | oldState->getWindowsWithHoveringPointer(entry.deviceId, pointerId).empty()) { |
| 2505 | LOG(INFO) << "Dropping event because the hovering pointer is not in any windows in " |
| 2506 | "display " |
| 2507 | << displayId << ": " << entry.getDescription(); |
| 2508 | outInjectionResult = InputEventInjectionResult::FAILED; |
| 2509 | return {}; |
| 2510 | } |
| 2511 | tempTouchState.removeHoveringPointer(entry.deviceId, pointerId); |
| 2512 | } |
| 2513 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2514 | addDragEventLocked(entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2515 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2516 | // Check whether touches should slip outside of the current foreground window. |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 2517 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.getPointerCount() == 1 && |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2518 | tempTouchState.isSlippery()) { |
Siarhei Vishniakou | 9306c38 | 2022-09-30 15:30:31 -0700 | [diff] [blame] | 2519 | const auto [x, y] = resolveTouchedPosition(entry); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2520 | const bool isStylus = isPointerFromStylus(entry, /*pointerIndex=*/0); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2521 | sp<WindowInfoHandle> oldTouchedWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2522 | tempTouchState.getFirstForegroundWindowHandle(); |
Siarhei Vishniakou | 0f6558d | 2023-04-21 12:05:13 -0700 | [diff] [blame] | 2523 | LOG_ALWAYS_FATAL_IF(oldTouchedWindowHandle == nullptr); |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 2524 | sp<WindowInfoHandle> newTouchedWindowHandle = |
| 2525 | findTouchedWindowAtLocked(displayId, x, y, isStylus); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2526 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2527 | // Verify targeted injection. |
| 2528 | if (const auto err = verifyTargetedInjection(newTouchedWindowHandle, entry); err) { |
| 2529 | ALOGW("Dropping injected event: %s", (*err).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2530 | outInjectionResult = os::InputEventInjectionResult::TARGET_MISMATCH; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2531 | return {}; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2532 | } |
| 2533 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2534 | // Drop touch events if requested by input feature |
| 2535 | if (newTouchedWindowHandle != nullptr && |
| 2536 | shouldDropInput(entry, newTouchedWindowHandle)) { |
| 2537 | newTouchedWindowHandle = nullptr; |
| 2538 | } |
| 2539 | |
Siarhei Vishniakou | afa08cc | 2023-05-08 22:35:50 -0700 | [diff] [blame] | 2540 | if (newTouchedWindowHandle != nullptr && |
| 2541 | !haveSameToken(oldTouchedWindowHandle, newTouchedWindowHandle)) { |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2542 | 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] | 2543 | oldTouchedWindowHandle->getName().c_str(), |
| 2544 | newTouchedWindowHandle->getName().c_str(), displayId); |
| 2545 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2546 | // Make a slippery exit from the old window. |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 2547 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2548 | const PointerProperties& pointer = entry.pointerProperties[0]; |
| 2549 | pointerIds.set(pointer.id); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2550 | |
| 2551 | const TouchedWindow& touchedWindow = |
| 2552 | tempTouchState.getTouchedWindow(oldTouchedWindowHandle); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2553 | addPointerWindowTargetLocked(oldTouchedWindowHandle, |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2554 | InputTarget::DispatchMode::SLIPPERY_EXIT, |
| 2555 | ftl::Flags<InputTarget::Flags>(), pointerIds, |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2556 | touchedWindow.getDownTimeInTarget(entry.deviceId), |
| 2557 | targets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2558 | |
| 2559 | // Make a slippery entrance into the new window. |
| 2560 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
Prabir Pradhan | 713bb3e | 2021-12-20 02:07:40 -0800 | [diff] [blame] | 2561 | isSplit = !isFromMouse; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2562 | } |
| 2563 | |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2564 | ftl::Flags<InputTarget::Flags> targetFlags; |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2565 | if (canReceiveForegroundTouches(*newTouchedWindowHandle->getInfo())) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2566 | targetFlags |= InputTarget::Flags::FOREGROUND; |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2567 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2568 | if (isSplit) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2569 | targetFlags |= InputTarget::Flags::SPLIT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2570 | } |
| 2571 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2572 | targetFlags |= InputTarget::Flags::WINDOW_IS_OBSCURED; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2573 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2574 | targetFlags |= InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2575 | } |
| 2576 | |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2577 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, |
| 2578 | InputTarget::DispatchMode::SLIPPERY_ENTER, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2579 | targetFlags, entry.deviceId, {pointer}, |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2580 | entry.eventTime); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2581 | |
| 2582 | // Check if the wallpaper window should deliver the corresponding event. |
| 2583 | slipWallpaperTouch(targetFlags, oldTouchedWindowHandle, newTouchedWindowHandle, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2584 | tempTouchState, entry.deviceId, pointer, targets); |
| 2585 | tempTouchState.removeTouchingPointerFromWindow(entry.deviceId, pointer.id, |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 2586 | oldTouchedWindowHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2587 | } |
| 2588 | } |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 2589 | |
| 2590 | // Update the pointerIds for non-splittable when it received pointer down. |
| 2591 | if (!isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN) { |
| 2592 | // If no split, we suppose all touched windows should receive pointer down. |
Siarhei Vishniakou | 5b9766d | 2023-07-18 14:06:29 -0700 | [diff] [blame] | 2593 | const int32_t pointerIndex = MotionEvent::getActionIndex(action); |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2594 | std::vector<PointerProperties> touchingPointers{entry.pointerProperties[pointerIndex]}; |
| 2595 | for (TouchedWindow& touchedWindow : tempTouchState.windows) { |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 2596 | // Ignore drag window for it should just track one pointer. |
| 2597 | if (mDragState && mDragState->dragWindow == touchedWindow.windowHandle) { |
| 2598 | continue; |
| 2599 | } |
Siarhei Vishniakou | 5b9766d | 2023-07-18 14:06:29 -0700 | [diff] [blame] | 2600 | touchedWindow.addTouchingPointers(entry.deviceId, touchingPointers); |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 2601 | } |
| 2602 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2603 | } |
| 2604 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2605 | // Update dispatching for hover enter and exit. |
Sam Dubey | f886dec | 2023-01-27 13:28:19 +0000 | [diff] [blame] | 2606 | { |
| 2607 | std::vector<TouchedWindow> hoveringWindows = |
| 2608 | getHoveringWindowsLocked(oldState, tempTouchState, entry); |
| 2609 | for (const TouchedWindow& touchedWindow : hoveringWindows) { |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 2610 | std::optional<InputTarget> target = |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2611 | createInputTargetLocked(touchedWindow.windowHandle, touchedWindow.dispatchMode, |
| 2612 | touchedWindow.targetFlags, |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 2613 | touchedWindow.getDownTimeInTarget(entry.deviceId)); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 2614 | if (!target) { |
| 2615 | continue; |
| 2616 | } |
| 2617 | // Hardcode to single hovering pointer for now. |
| 2618 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
| 2619 | pointerIds.set(entry.pointerProperties[0].id); |
| 2620 | target->addPointers(pointerIds, touchedWindow.windowHandle->getInfo()->transform); |
| 2621 | targets.push_back(*target); |
Sam Dubey | f886dec | 2023-01-27 13:28:19 +0000 | [diff] [blame] | 2622 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2623 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2624 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2625 | // Ensure that all touched windows are valid for injection. |
| 2626 | if (entry.injectionState != nullptr) { |
| 2627 | std::string errs; |
| 2628 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2629 | const auto err = verifyTargetedInjection(touchedWindow.windowHandle, entry); |
| 2630 | if (err) errs += "\n - " + *err; |
| 2631 | } |
| 2632 | if (!errs.empty()) { |
| 2633 | 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] | 2634 | "%s:%s", |
| 2635 | entry.injectionState->targetUid->toString().c_str(), errs.c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2636 | outInjectionResult = InputEventInjectionResult::TARGET_MISMATCH; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2637 | return {}; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2638 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2639 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2640 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2641 | // Check whether windows listening for outside touches are owned by the same UID. If the owner |
| 2642 | // 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] | 2643 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2644 | sp<WindowInfoHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2645 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2646 | if (foregroundWindowHandle) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 2647 | const auto foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2648 | for (InputTarget& target : targets) { |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2649 | if (target.dispatchMode == InputTarget::DispatchMode::OUTSIDE) { |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2650 | sp<WindowInfoHandle> targetWindow = |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 2651 | getWindowHandleLocked(target.connection->getToken()); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2652 | if (targetWindow->getInfo()->ownerUid != foregroundWindowUid) { |
| 2653 | target.flags |= InputTarget::Flags::ZERO_COORDS; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2654 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2655 | } |
| 2656 | } |
| 2657 | } |
| 2658 | } |
| 2659 | |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 2660 | // If this is a touchpad navigation gesture, it needs to only be sent to trusted targets, as we |
| 2661 | // only want the system UI to handle these gestures. |
| 2662 | const bool isTouchpadNavGesture = isFromSource(entry.source, AINPUT_SOURCE_MOUSE) && |
| 2663 | entry.classification == MotionClassification::MULTI_FINGER_SWIPE; |
| 2664 | if (isTouchpadNavGesture) { |
| 2665 | filterUntrustedTargets(/* byref */ tempTouchState, /* byref */ targets); |
| 2666 | } |
| 2667 | |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2668 | // Output targets from the touch state. |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2669 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2670 | std::vector<PointerProperties> touchingPointers = |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2671 | touchedWindow.getTouchingPointers(entry.deviceId); |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2672 | if (touchingPointers.empty()) { |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2673 | continue; |
| 2674 | } |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2675 | addPointerWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.dispatchMode, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2676 | touchedWindow.targetFlags, getPointerIds(touchingPointers), |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2677 | touchedWindow.getDownTimeInTarget(entry.deviceId), targets); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2678 | } |
Sam Dubey | 39d37cf | 2022-12-07 18:05:35 +0000 | [diff] [blame] | 2679 | |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 2680 | // During targeted injection, only allow owned targets to receive events |
| 2681 | std::erase_if(targets, [&](const InputTarget& target) { |
| 2682 | LOG_ALWAYS_FATAL_IF(target.windowHandle == nullptr); |
| 2683 | const auto err = verifyTargetedInjection(target.windowHandle, entry); |
| 2684 | if (err) { |
| 2685 | LOG(WARNING) << "Dropping injected event from " << target.windowHandle->getName() |
| 2686 | << ": " << (*err); |
| 2687 | return true; |
| 2688 | } |
| 2689 | return false; |
| 2690 | }); |
| 2691 | |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2692 | if (targets.empty()) { |
| 2693 | LOG(INFO) << "Dropping event because no targets were found: " << entry.getDescription(); |
| 2694 | outInjectionResult = InputEventInjectionResult::FAILED; |
| 2695 | return {}; |
| 2696 | } |
| 2697 | |
| 2698 | // If we only have windows getting ACTION_OUTSIDE, then drop the event, because there is no |
| 2699 | // window that is actually receiving the entire gesture. |
| 2700 | if (std::all_of(targets.begin(), targets.end(), [](const InputTarget& target) { |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2701 | return target.dispatchMode == InputTarget::DispatchMode::OUTSIDE; |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2702 | })) { |
| 2703 | LOG(INFO) << "Dropping event because all windows would just receive ACTION_OUTSIDE: " |
| 2704 | << entry.getDescription(); |
| 2705 | outInjectionResult = InputEventInjectionResult::FAILED; |
| 2706 | return {}; |
| 2707 | } |
| 2708 | |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2709 | outInjectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2710 | |
Prabir Pradhan | 502a725 | 2023-12-01 16:11:24 +0000 | [diff] [blame] | 2711 | // Now that we have generated all of the input targets for this event, reset the dispatch |
| 2712 | // mode for all touched window to AS_IS. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2713 | for (TouchedWindow& touchedWindow : tempTouchState.windows) { |
Prabir Pradhan | 502a725 | 2023-12-01 16:11:24 +0000 | [diff] [blame] | 2714 | touchedWindow.dispatchMode = InputTarget::DispatchMode::AS_IS; |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2715 | } |
| 2716 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2717 | // Update final pieces of touch state if the injector had permission. |
Siarhei Vishniakou | 7be50c9 | 2023-11-17 17:09:08 -0800 | [diff] [blame] | 2718 | if (maskedAction == AMOTION_EVENT_ACTION_UP) { |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2719 | // Pointer went up. |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 2720 | tempTouchState.removeTouchingPointer(entry.deviceId, entry.pointerProperties[0].id); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2721 | } else if (maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2722 | // All pointers up or canceled. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2723 | tempTouchState.removeAllPointersForDevice(entry.deviceId); |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2724 | } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 2725 | // One pointer went up. |
Siarhei Vishniakou | 5b9766d | 2023-07-18 14:06:29 -0700 | [diff] [blame] | 2726 | const int32_t pointerIndex = MotionEvent::getActionIndex(action); |
| 2727 | const uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
| 2728 | tempTouchState.removeTouchingPointer(entry.deviceId, pointerId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2729 | } |
| 2730 | |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2731 | // Save changes unless the action was scroll in which case the temporary touch |
| 2732 | // state was only valid for this one action. |
| 2733 | if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) { |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 2734 | if (displayId >= 0) { |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2735 | tempTouchState.clearWindowsWithoutPointers(); |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2736 | mTouchStatesByDisplay[displayId] = tempTouchState; |
| 2737 | } else { |
| 2738 | mTouchStatesByDisplay.erase(displayId); |
| 2739 | } |
| 2740 | } |
| 2741 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 2742 | if (tempTouchState.windows.empty()) { |
| 2743 | mTouchStatesByDisplay.erase(displayId); |
| 2744 | } |
| 2745 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2746 | return targets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2747 | } |
| 2748 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2749 | void InputDispatcher::finishDragAndDrop(int32_t displayId, float x, float y) { |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2750 | // Prevent stylus interceptor windows from affecting drag and drop behavior for now, until we |
| 2751 | // have an explicit reason to support it. |
| 2752 | constexpr bool isStylus = false; |
| 2753 | |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 2754 | sp<WindowInfoHandle> dropWindow = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2755 | findTouchedWindowAtLocked(displayId, x, y, isStylus, /*ignoreDragWindow=*/true); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2756 | if (dropWindow) { |
| 2757 | vec2 local = dropWindow->getInfo()->transform.transform(x, y); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2758 | sendDropWindowCommandLocked(dropWindow->getToken(), local.x, local.y); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 2759 | } else { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2760 | ALOGW("No window found when drop."); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2761 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2762 | } |
| 2763 | mDragState.reset(); |
| 2764 | } |
| 2765 | |
| 2766 | void InputDispatcher::addDragEventLocked(const MotionEntry& entry) { |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 2767 | if (!mDragState || mDragState->dragWindow->getInfo()->displayId != entry.displayId) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2768 | return; |
| 2769 | } |
| 2770 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2771 | if (!mDragState->isStartDrag) { |
| 2772 | mDragState->isStartDrag = true; |
| 2773 | mDragState->isStylusButtonDownAtStart = |
| 2774 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2775 | } |
| 2776 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2777 | // Find the pointer index by id. |
| 2778 | int32_t pointerIndex = 0; |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 2779 | for (; static_cast<uint32_t>(pointerIndex) < entry.getPointerCount(); pointerIndex++) { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2780 | const PointerProperties& pointerProperties = entry.pointerProperties[pointerIndex]; |
| 2781 | if (pointerProperties.id == mDragState->pointerId) { |
| 2782 | break; |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2783 | } |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2784 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2785 | |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 2786 | if (uint32_t(pointerIndex) == entry.getPointerCount()) { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2787 | 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] | 2788 | } |
| 2789 | |
| 2790 | const int32_t maskedAction = entry.action & AMOTION_EVENT_ACTION_MASK; |
| 2791 | const int32_t x = entry.pointerCoords[pointerIndex].getX(); |
| 2792 | const int32_t y = entry.pointerCoords[pointerIndex].getY(); |
| 2793 | |
| 2794 | switch (maskedAction) { |
| 2795 | case AMOTION_EVENT_ACTION_MOVE: { |
| 2796 | // Handle the special case : stylus button no longer pressed. |
| 2797 | bool isStylusButtonDown = |
| 2798 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2799 | if (mDragState->isStylusButtonDownAtStart && !isStylusButtonDown) { |
| 2800 | finishDragAndDrop(entry.displayId, x, y); |
| 2801 | return; |
| 2802 | } |
| 2803 | |
| 2804 | // Prevent stylus interceptor windows from affecting drag and drop behavior for now, |
| 2805 | // until we have an explicit reason to support it. |
| 2806 | constexpr bool isStylus = false; |
| 2807 | |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 2808 | sp<WindowInfoHandle> hoverWindowHandle = |
| 2809 | findTouchedWindowAtLocked(entry.displayId, x, y, isStylus, |
| 2810 | /*ignoreDragWindow=*/true); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2811 | // enqueue drag exit if needed. |
| 2812 | if (hoverWindowHandle != mDragState->dragHoverWindowHandle && |
| 2813 | !haveSameToken(hoverWindowHandle, mDragState->dragHoverWindowHandle)) { |
| 2814 | if (mDragState->dragHoverWindowHandle != nullptr) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2815 | enqueueDragEventLocked(mDragState->dragHoverWindowHandle, /*isExiting=*/true, x, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2816 | y); |
| 2817 | } |
| 2818 | mDragState->dragHoverWindowHandle = hoverWindowHandle; |
| 2819 | } |
| 2820 | // enqueue drag location if needed. |
| 2821 | if (hoverWindowHandle != nullptr) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2822 | enqueueDragEventLocked(hoverWindowHandle, /*isExiting=*/false, x, y); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2823 | } |
| 2824 | break; |
| 2825 | } |
| 2826 | |
| 2827 | case AMOTION_EVENT_ACTION_POINTER_UP: |
Siarhei Vishniakou | 5b9766d | 2023-07-18 14:06:29 -0700 | [diff] [blame] | 2828 | if (MotionEvent::getActionIndex(entry.action) != pointerIndex) { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2829 | break; |
| 2830 | } |
| 2831 | // The drag pointer is up. |
| 2832 | [[fallthrough]]; |
| 2833 | case AMOTION_EVENT_ACTION_UP: |
| 2834 | finishDragAndDrop(entry.displayId, x, y); |
| 2835 | break; |
| 2836 | case AMOTION_EVENT_ACTION_CANCEL: { |
| 2837 | ALOGD("Receiving cancel when drag and drop."); |
| 2838 | sendDropWindowCommandLocked(nullptr, 0, 0); |
| 2839 | mDragState.reset(); |
| 2840 | break; |
| 2841 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2842 | } |
| 2843 | } |
| 2844 | |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2845 | std::optional<InputTarget> InputDispatcher::createInputTargetLocked( |
| 2846 | const sp<android::gui::WindowInfoHandle>& windowHandle, |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2847 | InputTarget::DispatchMode dispatchMode, ftl::Flags<InputTarget::Flags> targetFlags, |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2848 | std::optional<nsecs_t> firstDownTimeInTarget) const { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 2849 | std::shared_ptr<Connection> connection = getConnectionLocked(windowHandle->getToken()); |
| 2850 | if (connection == nullptr) { |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2851 | ALOGW("Not creating InputTarget for %s, no input channel", windowHandle->getName().c_str()); |
| 2852 | return {}; |
| 2853 | } |
| 2854 | InputTarget inputTarget; |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 2855 | inputTarget.connection = connection; |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 2856 | inputTarget.windowHandle = windowHandle; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2857 | inputTarget.dispatchMode = dispatchMode; |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2858 | inputTarget.flags = targetFlags; |
| 2859 | inputTarget.globalScaleFactor = windowHandle->getInfo()->globalScaleFactor; |
| 2860 | inputTarget.firstDownTimeInTarget = firstDownTimeInTarget; |
| 2861 | const auto& displayInfoIt = mDisplayInfos.find(windowHandle->getInfo()->displayId); |
| 2862 | if (displayInfoIt != mDisplayInfos.end()) { |
| 2863 | inputTarget.displayTransform = displayInfoIt->second.transform; |
| 2864 | } else { |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 2865 | // DisplayInfo not found for this window on display windowHandle->getInfo()->displayId. |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2866 | // TODO(b/198444055): Make this an error message after 'setInputWindows' API is removed. |
| 2867 | } |
| 2868 | return inputTarget; |
| 2869 | } |
| 2870 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2871 | void InputDispatcher::addWindowTargetLocked(const sp<WindowInfoHandle>& windowHandle, |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2872 | InputTarget::DispatchMode dispatchMode, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2873 | ftl::Flags<InputTarget::Flags> targetFlags, |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2874 | std::optional<nsecs_t> firstDownTimeInTarget, |
Siarhei Vishniakou | f75cddb | 2022-10-25 10:42:16 -0700 | [diff] [blame] | 2875 | std::vector<InputTarget>& inputTargets) const { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2876 | std::vector<InputTarget>::iterator it = |
| 2877 | std::find_if(inputTargets.begin(), inputTargets.end(), |
| 2878 | [&windowHandle](const InputTarget& inputTarget) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 2879 | return inputTarget.connection->getToken() == windowHandle->getToken(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2880 | }); |
Chavi Weingarten | 97b8eec | 2020-01-09 18:09:08 +0000 | [diff] [blame] | 2881 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2882 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2883 | |
| 2884 | if (it == inputTargets.end()) { |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2885 | std::optional<InputTarget> target = |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2886 | createInputTargetLocked(windowHandle, dispatchMode, targetFlags, |
| 2887 | firstDownTimeInTarget); |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2888 | if (!target) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2889 | return; |
| 2890 | } |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2891 | inputTargets.push_back(*target); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2892 | it = inputTargets.end() - 1; |
| 2893 | } |
| 2894 | |
Siarhei Vishniakou | 23d73fb | 2023-10-29 13:27:46 -0700 | [diff] [blame] | 2895 | if (it->flags != targetFlags) { |
| 2896 | LOG(ERROR) << "Flags don't match! targetFlags=" << targetFlags.string() << ", it=" << *it; |
| 2897 | } |
| 2898 | if (it->globalScaleFactor != windowInfo->globalScaleFactor) { |
| 2899 | LOG(ERROR) << "Mismatch! it->globalScaleFactor=" << it->globalScaleFactor |
| 2900 | << ", windowInfo->globalScaleFactor=" << windowInfo->globalScaleFactor; |
| 2901 | } |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2902 | } |
| 2903 | |
| 2904 | void InputDispatcher::addPointerWindowTargetLocked( |
| 2905 | const sp<android::gui::WindowInfoHandle>& windowHandle, |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2906 | InputTarget::DispatchMode dispatchMode, ftl::Flags<InputTarget::Flags> targetFlags, |
| 2907 | std::bitset<MAX_POINTER_ID + 1> pointerIds, std::optional<nsecs_t> firstDownTimeInTarget, |
| 2908 | std::vector<InputTarget>& inputTargets) const REQUIRES(mLock) { |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2909 | if (pointerIds.none()) { |
| 2910 | for (const auto& target : inputTargets) { |
| 2911 | LOG(INFO) << "Target: " << target; |
| 2912 | } |
| 2913 | LOG(FATAL) << "No pointers specified for " << windowHandle->getName(); |
| 2914 | return; |
| 2915 | } |
| 2916 | std::vector<InputTarget>::iterator it = |
| 2917 | std::find_if(inputTargets.begin(), inputTargets.end(), |
| 2918 | [&windowHandle](const InputTarget& inputTarget) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 2919 | return inputTarget.connection->getToken() == windowHandle->getToken(); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2920 | }); |
| 2921 | |
| 2922 | // This is a hack, because the actual entry could potentially be an ACTION_DOWN event that |
| 2923 | // causes a HOVER_EXIT to be generated. That means that the same entry of ACTION_DOWN would |
| 2924 | // have DISPATCH_AS_HOVER_EXIT and DISPATCH_AS_IS. And therefore, we have to create separate |
| 2925 | // input targets for hovering pointers and for touching pointers. |
| 2926 | // If we picked an existing input target above, but it's for HOVER_EXIT - let's use a new |
| 2927 | // target instead. |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2928 | if (it != inputTargets.end() && it->dispatchMode == InputTarget::DispatchMode::HOVER_EXIT) { |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2929 | // Force the code below to create a new input target |
| 2930 | it = inputTargets.end(); |
| 2931 | } |
| 2932 | |
| 2933 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 2934 | |
| 2935 | if (it == inputTargets.end()) { |
| 2936 | std::optional<InputTarget> target = |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2937 | createInputTargetLocked(windowHandle, dispatchMode, targetFlags, |
| 2938 | firstDownTimeInTarget); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2939 | if (!target) { |
| 2940 | return; |
| 2941 | } |
| 2942 | inputTargets.push_back(*target); |
| 2943 | it = inputTargets.end() - 1; |
| 2944 | } |
| 2945 | |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2946 | if (it->dispatchMode != dispatchMode) { |
| 2947 | LOG(ERROR) << __func__ << ": DispatchMode doesn't match! ignoring new mode=" |
| 2948 | << ftl::enum_string(dispatchMode) << ", it=" << *it; |
| 2949 | } |
Siarhei Vishniakou | 4bd0b7c | 2023-10-27 00:51:14 -0700 | [diff] [blame] | 2950 | if (it->flags != targetFlags) { |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2951 | LOG(ERROR) << __func__ << ": Flags don't match! new targetFlags=" << targetFlags.string() |
| 2952 | << ", it=" << *it; |
Siarhei Vishniakou | 4bd0b7c | 2023-10-27 00:51:14 -0700 | [diff] [blame] | 2953 | } |
Siarhei Vishniakou | 23d73fb | 2023-10-29 13:27:46 -0700 | [diff] [blame] | 2954 | if (it->globalScaleFactor != windowInfo->globalScaleFactor) { |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2955 | LOG(ERROR) << __func__ << ": Mismatch! it->globalScaleFactor=" << it->globalScaleFactor |
Siarhei Vishniakou | 23d73fb | 2023-10-29 13:27:46 -0700 | [diff] [blame] | 2956 | << ", windowInfo->globalScaleFactor=" << windowInfo->globalScaleFactor; |
| 2957 | } |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2958 | |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2959 | it->addPointers(pointerIds, windowInfo->transform); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2960 | } |
| 2961 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2962 | void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2963 | int32_t displayId) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2964 | auto monitorsIt = mGlobalMonitorsByDisplay.find(displayId); |
| 2965 | if (monitorsIt == mGlobalMonitorsByDisplay.end()) return; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2966 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2967 | for (const Monitor& monitor : selectResponsiveMonitorsLocked(monitorsIt->second)) { |
| 2968 | InputTarget target; |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 2969 | target.connection = monitor.connection; |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2970 | // target.firstDownTimeInTarget is not set for global monitors. It is only required in split |
| 2971 | // touch and global monitoring works as intended even without setting firstDownTimeInTarget |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2972 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
| 2973 | target.displayTransform = it->second.transform; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2974 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2975 | target.setDefaultPointerTransform(target.displayTransform); |
| 2976 | inputTargets.push_back(target); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2977 | } |
| 2978 | } |
| 2979 | |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2980 | /** |
| 2981 | * Indicate whether one window handle should be considered as obscuring |
| 2982 | * another window handle. We only check a few preconditions. Actually |
| 2983 | * checking the bounds is left to the caller. |
| 2984 | */ |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2985 | static bool canBeObscuredBy(const sp<WindowInfoHandle>& windowHandle, |
| 2986 | const sp<WindowInfoHandle>& otherHandle) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2987 | // Compare by token so cloned layers aren't counted |
| 2988 | if (haveSameToken(windowHandle, otherHandle)) { |
| 2989 | return false; |
| 2990 | } |
| 2991 | auto info = windowHandle->getInfo(); |
| 2992 | auto otherInfo = otherHandle->getInfo(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2993 | if (otherInfo->inputConfig.test(WindowInfo::InputConfig::NOT_VISIBLE)) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2994 | return false; |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2995 | } else if (otherInfo->alpha == 0 && |
| 2996 | otherInfo->inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE)) { |
Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame] | 2997 | // Those act as if they were invisible, so we don't need to flag them. |
| 2998 | // We do want to potentially flag touchable windows even if they have 0 |
| 2999 | // opacity, since they can consume touches and alter the effects of the |
| 3000 | // user interaction (eg. apps that rely on |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3001 | // Flags::WINDOW_IS_PARTIALLY_OBSCURED should still be told about those |
Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame] | 3002 | // windows), hence we also check for FLAG_NOT_TOUCHABLE. |
| 3003 | return false; |
Bernardo Rufino | 8007daf | 2020-09-22 09:40:01 +0000 | [diff] [blame] | 3004 | } else if (info->ownerUid == otherInfo->ownerUid) { |
| 3005 | // If ownerUid is the same we don't generate occlusion events as there |
| 3006 | // is no security boundary within an uid. |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3007 | return false; |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 3008 | } else if (otherInfo->inputConfig.test(gui::WindowInfo::InputConfig::TRUSTED_OVERLAY)) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3009 | return false; |
| 3010 | } else if (otherInfo->displayId != info->displayId) { |
| 3011 | return false; |
| 3012 | } |
| 3013 | return true; |
| 3014 | } |
| 3015 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3016 | /** |
| 3017 | * Returns touch occlusion information in the form of TouchOcclusionInfo. To check if the touch is |
| 3018 | * untrusted, one should check: |
| 3019 | * |
| 3020 | * 1. If result.hasBlockingOcclusion is true. |
| 3021 | * If it's, it means the touch should be blocked due to a window with occlusion mode of |
| 3022 | * BLOCK_UNTRUSTED. |
| 3023 | * |
| 3024 | * 2. If result.obscuringOpacity > mMaximumObscuringOpacityForTouch. |
| 3025 | * If it is (and 1 is false), then the touch should be blocked because a stack of windows |
| 3026 | * (possibly only one) with occlusion mode of USE_OPACITY from one UID resulted in a composed |
| 3027 | * obscuring opacity above the threshold. Note that if there was no window of occlusion mode |
| 3028 | * USE_OPACITY, result.obscuringOpacity would've been 0 and since |
| 3029 | * mMaximumObscuringOpacityForTouch >= 0, the condition above would never be true. |
| 3030 | * |
| 3031 | * If neither of those is true, then it means the touch can be allowed. |
| 3032 | */ |
| 3033 | InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3034 | const sp<WindowInfoHandle>& windowHandle, int32_t x, int32_t y) const { |
| 3035 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 3036 | int32_t displayId = windowInfo->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3037 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3038 | TouchOcclusionInfo info; |
| 3039 | info.hasBlockingOcclusion = false; |
| 3040 | info.obscuringOpacity = 0; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3041 | info.obscuringUid = gui::Uid::INVALID; |
| 3042 | std::map<gui::Uid, float> opacityByUid; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3043 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3044 | if (windowHandle == otherHandle) { |
| 3045 | break; // All future windows are below us. Exit early. |
| 3046 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3047 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 3048 | if (canBeObscuredBy(windowHandle, otherHandle) && otherInfo->frameContainsPoint(x, y) && |
| 3049 | !haveSameApplicationToken(windowInfo, otherInfo)) { |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 3050 | if (DEBUG_TOUCH_OCCLUSION) { |
| 3051 | info.debugInfo.push_back( |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 3052 | dumpWindowForTouchOcclusion(otherInfo, /*isTouchedWindow=*/false)); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 3053 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3054 | // canBeObscuredBy() has returned true above, which means this window is untrusted, so |
| 3055 | // we perform the checks below to see if the touch can be propagated or not based on the |
| 3056 | // window's touch occlusion mode |
| 3057 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::BLOCK_UNTRUSTED) { |
| 3058 | info.hasBlockingOcclusion = true; |
| 3059 | info.obscuringUid = otherInfo->ownerUid; |
| 3060 | info.obscuringPackage = otherInfo->packageName; |
| 3061 | break; |
| 3062 | } |
| 3063 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::USE_OPACITY) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3064 | const auto uid = otherInfo->ownerUid; |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3065 | float opacity = |
| 3066 | (opacityByUid.find(uid) == opacityByUid.end()) ? 0 : opacityByUid[uid]; |
| 3067 | // Given windows A and B: |
| 3068 | // opacity(A, B) = 1 - [1 - opacity(A)] * [1 - opacity(B)] |
| 3069 | opacity = 1 - (1 - opacity) * (1 - otherInfo->alpha); |
| 3070 | opacityByUid[uid] = opacity; |
| 3071 | if (opacity > info.obscuringOpacity) { |
| 3072 | info.obscuringOpacity = opacity; |
| 3073 | info.obscuringUid = uid; |
| 3074 | info.obscuringPackage = otherInfo->packageName; |
| 3075 | } |
| 3076 | } |
| 3077 | } |
| 3078 | } |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 3079 | if (DEBUG_TOUCH_OCCLUSION) { |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 3080 | info.debugInfo.push_back(dumpWindowForTouchOcclusion(windowInfo, /*isTouchedWindow=*/true)); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 3081 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3082 | return info; |
| 3083 | } |
| 3084 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3085 | std::string InputDispatcher::dumpWindowForTouchOcclusion(const WindowInfo* info, |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 3086 | bool isTouchedWindow) const { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3087 | return StringPrintf(INDENT2 "* %spackage=%s/%s, id=%" PRId32 ", mode=%s, alpha=%.2f, " |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 3088 | "frame=[%" PRId32 ",%" PRId32 "][%" PRId32 ",%" PRId32 |
| 3089 | "], touchableRegion=%s, window={%s}, inputConfig={%s}, " |
| 3090 | "hasToken=%s, applicationInfo.name=%s, applicationInfo.token=%s\n", |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 3091 | isTouchedWindow ? "[TOUCHED] " : "", info->packageName.c_str(), |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3092 | info->ownerUid.toString().c_str(), info->id, |
Chavi Weingarten | 7f01919 | 2023-08-08 20:39:01 +0000 | [diff] [blame] | 3093 | toString(info->touchOcclusionMode).c_str(), info->alpha, info->frame.left, |
| 3094 | info->frame.top, info->frame.right, info->frame.bottom, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3095 | dumpRegion(info->touchableRegion).c_str(), info->name.c_str(), |
| 3096 | info->inputConfig.string().c_str(), toString(info->token != nullptr), |
| 3097 | info->applicationInfo.name.c_str(), |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 3098 | binderToString(info->applicationInfo.token).c_str()); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 3099 | } |
| 3100 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3101 | bool InputDispatcher::isTouchTrustedLocked(const TouchOcclusionInfo& occlusionInfo) const { |
| 3102 | if (occlusionInfo.hasBlockingOcclusion) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3103 | ALOGW("Untrusted touch due to occlusion by %s/%s", occlusionInfo.obscuringPackage.c_str(), |
| 3104 | occlusionInfo.obscuringUid.toString().c_str()); |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3105 | return false; |
| 3106 | } |
| 3107 | if (occlusionInfo.obscuringOpacity > mMaximumObscuringOpacityForTouch) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3108 | ALOGW("Untrusted touch due to occlusion by %s/%s (obscuring opacity = " |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3109 | "%.2f, maximum allowed = %.2f)", |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3110 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid.toString().c_str(), |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3111 | occlusionInfo.obscuringOpacity, mMaximumObscuringOpacityForTouch); |
| 3112 | return false; |
| 3113 | } |
| 3114 | return true; |
| 3115 | } |
| 3116 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3117 | bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3118 | int32_t x, int32_t y) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3119 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3120 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 3121 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3122 | if (windowHandle == otherHandle) { |
| 3123 | break; // All future windows are below us. Exit early. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3124 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3125 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3126 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 3127 | otherInfo->frameContainsPoint(x, y)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3128 | return true; |
| 3129 | } |
| 3130 | } |
| 3131 | return false; |
| 3132 | } |
| 3133 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3134 | bool InputDispatcher::isWindowObscuredLocked(const sp<WindowInfoHandle>& windowHandle) const { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 3135 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3136 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 3137 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 3138 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3139 | if (windowHandle == otherHandle) { |
| 3140 | break; // All future windows are below us. Exit early. |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 3141 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3142 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3143 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 3144 | otherInfo->overlaps(windowInfo)) { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 3145 | return true; |
| 3146 | } |
| 3147 | } |
| 3148 | return false; |
| 3149 | } |
| 3150 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 3151 | std::string InputDispatcher::getApplicationWindowLabel( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3152 | const InputApplicationHandle* applicationHandle, const sp<WindowInfoHandle>& windowHandle) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3153 | if (applicationHandle != nullptr) { |
| 3154 | if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 3155 | return applicationHandle->getName() + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3156 | } else { |
| 3157 | return applicationHandle->getName(); |
| 3158 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3159 | } else if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 3160 | return windowHandle->getInfo()->applicationInfo.name + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3161 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3162 | return "<unknown application or window>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3163 | } |
| 3164 | } |
| 3165 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3166 | void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3167 | if (!isUserActivityEvent(eventEntry)) { |
| 3168 | // 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] | 3169 | return; |
| 3170 | } |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 3171 | |
| 3172 | const int32_t eventType = getUserActivityEventType(eventEntry); |
| 3173 | if (input_flags::rate_limit_user_activity_poke_in_dispatcher()) { |
| 3174 | // Note that we're directly getting the time diff between the current event and the previous |
| 3175 | // event. This is assuming that the first user event always happens at a timestamp that is |
| 3176 | // greater than `mMinTimeBetweenUserActivityPokes` (otherwise, the first user event will |
| 3177 | // wrongly be dropped). In real life, `mMinTimeBetweenUserActivityPokes` is a much smaller |
| 3178 | // value than the potential first user activity event time, so this is ok. |
| 3179 | std::chrono::nanoseconds timeSinceLastEvent = |
| 3180 | std::chrono::nanoseconds(eventEntry.eventTime - mLastUserActivityTimes[eventType]); |
| 3181 | if (timeSinceLastEvent < mMinTimeBetweenUserActivityPokes) { |
| 3182 | return; |
| 3183 | } |
| 3184 | } |
| 3185 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3186 | int32_t displayId = getTargetDisplayId(eventEntry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3187 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 3188 | const WindowInfo* windowDisablingUserActivityInfo = nullptr; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3189 | if (focusedWindowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3190 | const WindowInfo* info = focusedWindowHandle->getInfo(); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 3191 | if (info->inputConfig.test(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY)) { |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 3192 | windowDisablingUserActivityInfo = info; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3193 | } |
| 3194 | } |
| 3195 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3196 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3197 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3198 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 3199 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3200 | return; |
| 3201 | } |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 3202 | if (windowDisablingUserActivityInfo != nullptr) { |
| 3203 | if (DEBUG_DISPATCH_CYCLE) { |
| 3204 | ALOGD("Not poking user activity: disabled by window '%s'.", |
| 3205 | windowDisablingUserActivityInfo->name.c_str()); |
| 3206 | } |
| 3207 | return; |
| 3208 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3209 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3210 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3211 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3212 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 3213 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3214 | return; |
| 3215 | } |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 3216 | // If the key code is unknown, we don't consider it user activity |
| 3217 | if (keyEntry.keyCode == AKEYCODE_UNKNOWN) { |
| 3218 | return; |
| 3219 | } |
| 3220 | // Don't inhibit events that were intercepted or are not passed to |
| 3221 | // the apps, like system shortcuts |
| 3222 | if (windowDisablingUserActivityInfo != nullptr && |
| 3223 | keyEntry.interceptKeyResult != KeyEntry::InterceptKeyResult::SKIP && |
| 3224 | keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER) { |
| 3225 | if (DEBUG_DISPATCH_CYCLE) { |
| 3226 | ALOGD("Not poking user activity: disabled by window '%s'.", |
| 3227 | windowDisablingUserActivityInfo->name.c_str()); |
| 3228 | } |
| 3229 | return; |
| 3230 | } |
| 3231 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3232 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3233 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3234 | default: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3235 | LOG_ALWAYS_FATAL("%s events are not user activity", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3236 | ftl::enum_string(eventEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3237 | break; |
| 3238 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3239 | } |
| 3240 | |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 3241 | mLastUserActivityTimes[eventType] = eventEntry.eventTime; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3242 | auto command = [this, eventTime = eventEntry.eventTime, eventType, displayId]() |
| 3243 | REQUIRES(mLock) { |
| 3244 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 3245 | mPolicy.pokeUserActivity(eventTime, eventType, displayId); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3246 | }; |
| 3247 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3248 | } |
| 3249 | |
| 3250 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3251 | const std::shared_ptr<Connection>& connection, |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 3252 | std::shared_ptr<const EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3253 | const InputTarget& inputTarget) { |
Prabir Pradhan | 2dac8b8 | 2023-09-06 01:11:51 +0000 | [diff] [blame] | 3254 | ATRACE_NAME_IF(ATRACE_ENABLED(), |
| 3255 | StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
| 3256 | connection->getInputChannelName().c_str(), eventEntry->id)); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3257 | if (DEBUG_DISPATCH_CYCLE) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3258 | ALOGD("channel '%s' ~ prepareDispatchCycle - flags=%s, " |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 3259 | "globalScaleFactor=%f, pointerIds=%s %s", |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3260 | connection->getInputChannelName().c_str(), inputTarget.flags.string().c_str(), |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 3261 | inputTarget.globalScaleFactor, bitsetToString(inputTarget.pointerIds).c_str(), |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3262 | inputTarget.getPointerInfoString().c_str()); |
| 3263 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3264 | |
| 3265 | // Skip this event if the connection status is not normal. |
| 3266 | // 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] | 3267 | if (connection->status != Connection::Status::NORMAL) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3268 | if (DEBUG_DISPATCH_CYCLE) { |
| 3269 | ALOGD("channel '%s' ~ Dropping event because the channel status is %s", |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3270 | connection->getInputChannelName().c_str(), |
| 3271 | ftl::enum_string(connection->status).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3272 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3273 | return; |
| 3274 | } |
| 3275 | |
| 3276 | // Split a motion event if needed. |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3277 | if (inputTarget.flags.test(InputTarget::Flags::SPLIT)) { |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3278 | LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3279 | "Entry type %s should not have Flags::SPLIT", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3280 | ftl::enum_string(eventEntry->type).c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3281 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3282 | const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry); |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 3283 | if (inputTarget.pointerIds.count() != originalMotionEntry.getPointerCount()) { |
Siarhei Vishniakou | 16e4fa0 | 2023-02-16 17:48:56 -0800 | [diff] [blame] | 3284 | if (!inputTarget.firstDownTimeInTarget.has_value()) { |
| 3285 | logDispatchStateLocked(); |
| 3286 | LOG(FATAL) << "Splitting motion events requires a down time to be set for the " |
| 3287 | "target on connection " |
| 3288 | << connection->getInputChannelName() << " for " |
| 3289 | << originalMotionEntry.getDescription(); |
| 3290 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3291 | std::unique_ptr<MotionEntry> splitMotionEntry = |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3292 | splitMotionEvent(originalMotionEntry, inputTarget.pointerIds, |
| 3293 | inputTarget.firstDownTimeInTarget.value()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3294 | if (!splitMotionEntry) { |
| 3295 | return; // split event was dropped |
| 3296 | } |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3297 | if (splitMotionEntry->action == AMOTION_EVENT_ACTION_CANCEL) { |
| 3298 | std::string reason = std::string("reason=pointer cancel on split window"); |
| 3299 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 3300 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 3301 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3302 | if (DEBUG_FOCUS) { |
| 3303 | ALOGD("channel '%s' ~ Split motion event.", |
| 3304 | connection->getInputChannelName().c_str()); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3305 | logOutboundMotionDetails(" ", *splitMotionEntry); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3306 | } |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3307 | enqueueDispatchEntryAndStartDispatchCycleLocked(currentTime, connection, |
| 3308 | std::move(splitMotionEntry), |
| 3309 | inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3310 | return; |
| 3311 | } |
| 3312 | } |
| 3313 | |
| 3314 | // Not splitting. Enqueue dispatch entries for the event as is. |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3315 | enqueueDispatchEntryAndStartDispatchCycleLocked(currentTime, connection, eventEntry, |
| 3316 | inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3317 | } |
| 3318 | |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3319 | void InputDispatcher::enqueueDispatchEntryAndStartDispatchCycleLocked( |
| 3320 | nsecs_t currentTime, const std::shared_ptr<Connection>& connection, |
| 3321 | std::shared_ptr<const EventEntry> eventEntry, const InputTarget& inputTarget) { |
Prabir Pradhan | 2dac8b8 | 2023-09-06 01:11:51 +0000 | [diff] [blame] | 3322 | ATRACE_NAME_IF(ATRACE_ENABLED(), |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3323 | StringPrintf("enqueueDispatchEntryAndStartDispatchCycleLocked(inputChannel=%s, " |
| 3324 | "id=0x%" PRIx32 ")", |
Prabir Pradhan | 2dac8b8 | 2023-09-06 01:11:51 +0000 | [diff] [blame] | 3325 | connection->getInputChannelName().c_str(), eventEntry->id)); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3326 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 3327 | const bool wasEmpty = connection->outboundQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3328 | |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3329 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3330 | |
| 3331 | // If the outbound queue was previously empty, start the dispatch cycle going. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3332 | if (wasEmpty && !connection->outboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3333 | startDispatchCycleLocked(currentTime, connection); |
| 3334 | } |
| 3335 | } |
| 3336 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3337 | void InputDispatcher::enqueueDispatchEntryLocked(const std::shared_ptr<Connection>& connection, |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 3338 | std::shared_ptr<const EventEntry> eventEntry, |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3339 | const InputTarget& inputTarget) { |
Prabir Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame] | 3340 | // TODO(b/210460522): Verify all targets excluding global monitors are associated with a window. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3341 | // This is a new event. |
| 3342 | // Enqueue a new dispatch entry onto the outbound queue for this connection. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3343 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Prabir Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame] | 3344 | createDispatchEntry(inputTarget, eventEntry, inputTarget.flags, mWindowInfosVsyncId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3345 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3346 | // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a |
| 3347 | // different EventEntry than what was passed in. |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3348 | eventEntry = dispatchEntry->eventEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3349 | // Apply target flags and update the connection's input state. |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3350 | switch (eventEntry->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3351 | case EventEntry::Type::KEY: { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3352 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(*eventEntry); |
| 3353 | if (!connection->inputState.trackKey(keyEntry, keyEntry.flags)) { |
Siarhei Vishniakou | c94dafe | 2023-05-26 10:24:19 -0700 | [diff] [blame] | 3354 | LOG(WARNING) << "channel " << connection->getInputChannelName() |
| 3355 | << "~ dropping inconsistent event: " << *dispatchEntry; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3356 | return; // skip the inconsistent event |
| 3357 | } |
| 3358 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3359 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3360 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3361 | case EventEntry::Type::MOTION: { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3362 | std::shared_ptr<const MotionEntry> resolvedMotion = |
| 3363 | std::static_pointer_cast<const MotionEntry>(eventEntry); |
| 3364 | { |
| 3365 | // Determine the resolved motion entry. |
| 3366 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); |
| 3367 | int32_t resolvedAction = motionEntry.action; |
| 3368 | int32_t resolvedFlags = motionEntry.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3369 | |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3370 | if (inputTarget.dispatchMode == InputTarget::DispatchMode::OUTSIDE) { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3371 | resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3372 | } else if (inputTarget.dispatchMode == InputTarget::DispatchMode::HOVER_EXIT) { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3373 | resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3374 | } else if (inputTarget.dispatchMode == InputTarget::DispatchMode::HOVER_ENTER) { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3375 | resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3376 | } else if (inputTarget.dispatchMode == InputTarget::DispatchMode::SLIPPERY_EXIT) { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3377 | resolvedAction = AMOTION_EVENT_ACTION_CANCEL; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3378 | } else if (inputTarget.dispatchMode == InputTarget::DispatchMode::SLIPPERY_ENTER) { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3379 | resolvedAction = AMOTION_EVENT_ACTION_DOWN; |
| 3380 | } |
| 3381 | if (resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE && |
| 3382 | !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source, |
| 3383 | motionEntry.displayId)) { |
| 3384 | if (DEBUG_DISPATCH_CYCLE) { |
| 3385 | LOG(DEBUG) << "channel '" << connection->getInputChannelName().c_str() |
| 3386 | << "' ~ enqueueDispatchEntryLocked: filling in missing hover " |
| 3387 | "enter event"; |
| 3388 | } |
| 3389 | resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 3390 | } |
| 3391 | |
| 3392 | if (resolvedAction == AMOTION_EVENT_ACTION_CANCEL) { |
| 3393 | resolvedFlags |= AMOTION_EVENT_FLAG_CANCELED; |
| 3394 | } |
| 3395 | if (dispatchEntry->targetFlags.test(InputTarget::Flags::WINDOW_IS_OBSCURED)) { |
| 3396 | resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; |
| 3397 | } |
| 3398 | if (dispatchEntry->targetFlags.test( |
| 3399 | InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED)) { |
| 3400 | resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 3401 | } |
| 3402 | |
| 3403 | dispatchEntry->resolvedFlags = resolvedFlags; |
| 3404 | if (resolvedAction != motionEntry.action) { |
Siarhei Vishniakou | e9ef6bc | 2023-12-21 19:47:20 -0800 | [diff] [blame] | 3405 | std::optional<std::vector<PointerProperties>> usingProperties; |
| 3406 | std::optional<std::vector<PointerCoords>> usingCoords; |
| 3407 | if (resolvedAction == AMOTION_EVENT_ACTION_HOVER_EXIT || |
| 3408 | resolvedAction == AMOTION_EVENT_ACTION_CANCEL) { |
| 3409 | // This is a HOVER_EXIT or an ACTION_CANCEL event that was synthesized by |
| 3410 | // the dispatcher, and therefore the coordinates of this event are currently |
| 3411 | // incorrect. These events should use the coordinates of the last dispatched |
| 3412 | // ACTION_MOVE or HOVER_MOVE. We need to query InputState to get this data. |
| 3413 | const bool hovering = resolvedAction == AMOTION_EVENT_ACTION_HOVER_EXIT; |
| 3414 | std::optional<std::pair<std::vector<PointerProperties>, |
| 3415 | std::vector<PointerCoords>>> |
| 3416 | pointerInfo = |
| 3417 | connection->inputState.getPointersOfLastEvent(motionEntry, |
| 3418 | hovering); |
| 3419 | if (pointerInfo) { |
| 3420 | usingProperties = pointerInfo->first; |
| 3421 | usingCoords = pointerInfo->second; |
| 3422 | } |
| 3423 | } |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3424 | // Generate a new MotionEntry with a new eventId using the resolved action and |
| 3425 | // flags. |
Siarhei Vishniakou | e9ef6bc | 2023-12-21 19:47:20 -0800 | [diff] [blame] | 3426 | resolvedMotion = std::make_shared< |
| 3427 | MotionEntry>(mIdGenerator.nextId(), motionEntry.injectionState, |
| 3428 | motionEntry.eventTime, motionEntry.deviceId, |
| 3429 | motionEntry.source, motionEntry.displayId, |
| 3430 | motionEntry.policyFlags, resolvedAction, |
| 3431 | motionEntry.actionButton, resolvedFlags, |
| 3432 | motionEntry.metaState, motionEntry.buttonState, |
| 3433 | motionEntry.classification, motionEntry.edgeFlags, |
| 3434 | motionEntry.xPrecision, motionEntry.yPrecision, |
| 3435 | motionEntry.xCursorPosition, motionEntry.yCursorPosition, |
| 3436 | motionEntry.downTime, |
| 3437 | usingProperties.value_or(motionEntry.pointerProperties), |
| 3438 | usingCoords.value_or(motionEntry.pointerCoords)); |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3439 | if (ATRACE_ENABLED()) { |
| 3440 | std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32 |
| 3441 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3442 | motionEntry.id, resolvedMotion->id); |
| 3443 | ATRACE_NAME(message.c_str()); |
| 3444 | } |
| 3445 | |
| 3446 | // Set the resolved motion entry in the DispatchEntry. |
| 3447 | dispatchEntry->eventEntry = resolvedMotion; |
| 3448 | eventEntry = resolvedMotion; |
| 3449 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3450 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3451 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3452 | // Check if we need to cancel any of the ongoing gestures. We don't support multiple |
| 3453 | // devices being active at the same time in the same window, so if a new device is |
| 3454 | // active, cancel the gesture from the old device. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3455 | std::unique_ptr<EventEntry> cancelEvent = |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3456 | connection->inputState.cancelConflictingInputStream(*resolvedMotion); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3457 | if (cancelEvent != nullptr) { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3458 | LOG(INFO) << "Canceling pointers for device " << resolvedMotion->deviceId << " in " |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3459 | << connection->getInputChannelName() << " with event " |
| 3460 | << cancelEvent->getDescription(); |
| 3461 | std::unique_ptr<DispatchEntry> cancelDispatchEntry = |
| 3462 | createDispatchEntry(inputTarget, std::move(cancelEvent), |
Prabir Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame] | 3463 | ftl::Flags<InputTarget::Flags>(), mWindowInfosVsyncId); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3464 | |
| 3465 | // Send these cancel events to the queue before sending the event from the new |
| 3466 | // device. |
| 3467 | connection->outboundQueue.emplace_back(std::move(cancelDispatchEntry)); |
| 3468 | } |
| 3469 | |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3470 | if (!connection->inputState.trackMotion(*resolvedMotion, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3471 | dispatchEntry->resolvedFlags)) { |
Siarhei Vishniakou | c94dafe | 2023-05-26 10:24:19 -0700 | [diff] [blame] | 3472 | LOG(WARNING) << "channel " << connection->getInputChannelName() |
| 3473 | << "~ dropping inconsistent event: " << *dispatchEntry; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3474 | return; // skip the inconsistent event |
| 3475 | } |
| 3476 | |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3477 | if ((resolvedMotion->flags & AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) && |
| 3478 | (resolvedMotion->policyFlags & POLICY_FLAG_TRUSTED)) { |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 3479 | // Skip reporting pointer down outside focus to the policy. |
| 3480 | break; |
| 3481 | } |
| 3482 | |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3483 | dispatchPointerDownOutsideFocus(resolvedMotion->source, resolvedMotion->action, |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 3484 | inputTarget.connection->getToken()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3485 | |
| 3486 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3487 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3488 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3489 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3490 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3491 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3492 | break; |
| 3493 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3494 | case EventEntry::Type::SENSOR: { |
| 3495 | LOG_ALWAYS_FATAL("SENSOR events should not go to apps via input channel"); |
| 3496 | break; |
| 3497 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3498 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 3499 | case EventEntry::Type::DEVICE_RESET: { |
| 3500 | LOG_ALWAYS_FATAL("%s events should not go to apps", |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3501 | ftl::enum_string(eventEntry->type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3502 | break; |
| 3503 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3504 | } |
| 3505 | |
| 3506 | // Remember that we are waiting for this dispatch to complete. |
| 3507 | if (dispatchEntry->hasForegroundTarget()) { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3508 | incrementPendingForegroundDispatches(*eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3509 | } |
| 3510 | |
| 3511 | // Enqueue the dispatch entry. |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 3512 | connection->outboundQueue.emplace_back(std::move(dispatchEntry)); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3513 | traceOutboundQueueLength(*connection); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3514 | } |
| 3515 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3516 | /** |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3517 | * This function is for debugging and metrics collection. It has two roles. |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3518 | * |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3519 | * The first role is to log input interaction with windows, which helps determine what the user was |
| 3520 | * interacting with. For example, if user is touching launcher, we will see an input_interaction log |
| 3521 | * that user started interacting with launcher window, as well as any other window that received |
| 3522 | * that gesture, such as the wallpaper or other spy windows. A new input_interaction is only logged |
| 3523 | * when the set of tokens that received the event changes. It is not logged again as long as the |
| 3524 | * user is interacting with the same windows. |
| 3525 | * |
| 3526 | * The second role is to track input device activity for metrics collection. For each input event, |
| 3527 | * we report the set of UIDs that the input device interacted with to the policy. Unlike for the |
| 3528 | * input_interaction logs, the device interaction is reported even when the set of interaction |
| 3529 | * tokens do not change. |
| 3530 | * |
| 3531 | * For these purposes, we do not count ACTION_OUTSIDE, ACTION_UP and ACTION_CANCEL actions as |
| 3532 | * interaction. This includes up and cancel events for both keys and motions. |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3533 | */ |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3534 | void InputDispatcher::processInteractionsLocked(const EventEntry& entry, |
| 3535 | const std::vector<InputTarget>& targets) { |
| 3536 | int32_t deviceId; |
| 3537 | nsecs_t eventTime; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3538 | // Skip ACTION_UP events, and all events other than keys and motions |
| 3539 | if (entry.type == EventEntry::Type::KEY) { |
| 3540 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 3541 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
| 3542 | return; |
| 3543 | } |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3544 | deviceId = keyEntry.deviceId; |
| 3545 | eventTime = keyEntry.eventTime; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3546 | } else if (entry.type == EventEntry::Type::MOTION) { |
| 3547 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 3548 | if (motionEntry.action == AMOTION_EVENT_ACTION_UP || |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3549 | motionEntry.action == AMOTION_EVENT_ACTION_CANCEL || |
| 3550 | MotionEvent::getActionMasked(motionEntry.action) == AMOTION_EVENT_ACTION_POINTER_UP) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3551 | return; |
| 3552 | } |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3553 | deviceId = motionEntry.deviceId; |
| 3554 | eventTime = motionEntry.eventTime; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3555 | } else { |
| 3556 | return; // Not a key or a motion |
| 3557 | } |
| 3558 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3559 | std::set<gui::Uid> interactionUids; |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 3560 | std::unordered_set<sp<IBinder>, StrongPointerHash<IBinder>> newConnectionTokens; |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3561 | std::vector<std::shared_ptr<Connection>> newConnections; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3562 | for (const InputTarget& target : targets) { |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3563 | if (target.dispatchMode == InputTarget::DispatchMode::OUTSIDE) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3564 | continue; // Skip windows that receive ACTION_OUTSIDE |
| 3565 | } |
| 3566 | |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 3567 | sp<IBinder> token = target.connection->getToken(); |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3568 | newConnectionTokens.insert(std::move(token)); |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 3569 | newConnections.emplace_back(target.connection); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3570 | if (target.windowHandle) { |
| 3571 | interactionUids.emplace(target.windowHandle->getInfo()->ownerUid); |
| 3572 | } |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3573 | } |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3574 | |
| 3575 | auto command = [this, deviceId, eventTime, uids = std::move(interactionUids)]() |
| 3576 | REQUIRES(mLock) { |
| 3577 | scoped_unlock unlock(mLock); |
| 3578 | mPolicy.notifyDeviceInteraction(deviceId, eventTime, uids); |
| 3579 | }; |
| 3580 | postCommandLocked(std::move(command)); |
| 3581 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3582 | if (newConnectionTokens == mInteractionConnectionTokens) { |
| 3583 | return; // no change |
| 3584 | } |
| 3585 | mInteractionConnectionTokens = newConnectionTokens; |
| 3586 | |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3587 | std::string targetList; |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3588 | for (const std::shared_ptr<Connection>& connection : newConnections) { |
Siarhei Vishniakou | 10bfdc9 | 2024-02-01 11:12:57 -0800 | [diff] [blame^] | 3589 | targetList += connection->getInputChannelName() + ", "; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3590 | } |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3591 | std::string message = "Interaction with: " + targetList; |
| 3592 | if (targetList.empty()) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3593 | message += "<none>"; |
| 3594 | } |
| 3595 | android_log_event_list(LOGTAG_INPUT_INTERACTION) << message << LOG_ID_EVENTS; |
| 3596 | } |
| 3597 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3598 | void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3599 | const sp<IBinder>& token) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3600 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3601 | uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK; |
| 3602 | if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3603 | return; |
| 3604 | } |
| 3605 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 3606 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3607 | if (focusedToken == token) { |
| 3608 | // ignore since token is focused |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3609 | return; |
| 3610 | } |
| 3611 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3612 | auto command = [this, token]() REQUIRES(mLock) { |
| 3613 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 3614 | mPolicy.onPointerDownOutsideFocus(token); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3615 | }; |
| 3616 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3617 | } |
| 3618 | |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3619 | status_t InputDispatcher::publishMotionEvent(Connection& connection, |
| 3620 | DispatchEntry& dispatchEntry) const { |
| 3621 | const EventEntry& eventEntry = *(dispatchEntry.eventEntry); |
| 3622 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 3623 | |
| 3624 | PointerCoords scaledCoords[MAX_POINTERS]; |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 3625 | const PointerCoords* usingCoords = motionEntry.pointerCoords.data(); |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3626 | |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 3627 | // TODO(b/316355518): Do not modify coords before dispatch. |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3628 | // Set the X and Y offset and X and Y scale depending on the input source. |
| 3629 | if ((motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) && |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3630 | !(dispatchEntry.targetFlags.test(InputTarget::Flags::ZERO_COORDS))) { |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3631 | float globalScaleFactor = dispatchEntry.globalScaleFactor; |
| 3632 | if (globalScaleFactor != 1.0f) { |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 3633 | for (uint32_t i = 0; i < motionEntry.getPointerCount(); i++) { |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3634 | scaledCoords[i] = motionEntry.pointerCoords[i]; |
| 3635 | // Don't apply window scale here since we don't want scale to affect raw |
| 3636 | // coordinates. The scale will be sent back to the client and applied |
| 3637 | // later when requesting relative coordinates. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 3638 | scaledCoords[i].scale(globalScaleFactor, /*windowXScale=*/1, /*windowYScale=*/1); |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3639 | } |
| 3640 | usingCoords = scaledCoords; |
| 3641 | } |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3642 | } else if (dispatchEntry.targetFlags.test(InputTarget::Flags::ZERO_COORDS)) { |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3643 | // We don't want the dispatch target to know the coordinates |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 3644 | for (uint32_t i = 0; i < motionEntry.getPointerCount(); i++) { |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3645 | scaledCoords[i].clear(); |
| 3646 | } |
| 3647 | usingCoords = scaledCoords; |
| 3648 | } |
| 3649 | |
| 3650 | std::array<uint8_t, 32> hmac = getSignature(motionEntry, dispatchEntry); |
| 3651 | |
| 3652 | // Publish the motion event. |
| 3653 | return connection.inputPublisher |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3654 | .publishMotionEvent(dispatchEntry.seq, motionEntry.id, motionEntry.deviceId, |
| 3655 | motionEntry.source, motionEntry.displayId, std::move(hmac), |
| 3656 | motionEntry.action, motionEntry.actionButton, |
| 3657 | dispatchEntry.resolvedFlags, motionEntry.edgeFlags, |
| 3658 | motionEntry.metaState, motionEntry.buttonState, |
| 3659 | motionEntry.classification, dispatchEntry.transform, |
| 3660 | motionEntry.xPrecision, motionEntry.yPrecision, |
| 3661 | motionEntry.xCursorPosition, motionEntry.yCursorPosition, |
| 3662 | dispatchEntry.rawTransform, motionEntry.downTime, |
| 3663 | motionEntry.eventTime, motionEntry.getPointerCount(), |
| 3664 | motionEntry.pointerProperties.data(), usingCoords); |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3665 | } |
| 3666 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3667 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3668 | const std::shared_ptr<Connection>& connection) { |
Prabir Pradhan | 2dac8b8 | 2023-09-06 01:11:51 +0000 | [diff] [blame] | 3669 | ATRACE_NAME_IF(ATRACE_ENABLED(), |
| 3670 | StringPrintf("startDispatchCycleLocked(inputChannel=%s)", |
| 3671 | connection->getInputChannelName().c_str())); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3672 | if (DEBUG_DISPATCH_CYCLE) { |
| 3673 | ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str()); |
| 3674 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3675 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3676 | while (connection->status == Connection::Status::NORMAL && !connection->outboundQueue.empty()) { |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 3677 | std::unique_ptr<DispatchEntry>& dispatchEntry = connection->outboundQueue.front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3678 | dispatchEntry->deliveryTime = currentTime; |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3679 | const std::chrono::nanoseconds timeout = getDispatchingTimeoutLocked(connection); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3680 | dispatchEntry->timeoutTime = currentTime + timeout.count(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3681 | |
| 3682 | // Publish the event. |
| 3683 | status_t status; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3684 | const EventEntry& eventEntry = *(dispatchEntry->eventEntry); |
| 3685 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3686 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3687 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 3688 | std::array<uint8_t, 32> hmac = getSignature(keyEntry, *dispatchEntry); |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 3689 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
Siarhei Vishniakou | 827d1ac | 2023-07-21 16:37:51 -0700 | [diff] [blame] | 3690 | LOG(INFO) << "Publishing " << *dispatchEntry << " to " |
| 3691 | << connection->getInputChannelName(); |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 3692 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3693 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3694 | // Publish the key event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3695 | status = connection->inputPublisher |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3696 | .publishKeyEvent(dispatchEntry->seq, keyEntry.id, |
| 3697 | keyEntry.deviceId, keyEntry.source, |
| 3698 | keyEntry.displayId, std::move(hmac), |
| 3699 | keyEntry.action, dispatchEntry->resolvedFlags, |
| 3700 | keyEntry.keyCode, keyEntry.scanCode, |
| 3701 | keyEntry.metaState, keyEntry.repeatCount, |
| 3702 | keyEntry.downTime, keyEntry.eventTime); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 3703 | if (mTracer) { |
| 3704 | mTracer->traceEventDispatch(*dispatchEntry, keyEntry.traceTracker.get()); |
| 3705 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3706 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3707 | } |
| 3708 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3709 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 3710 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
Siarhei Vishniakou | 827d1ac | 2023-07-21 16:37:51 -0700 | [diff] [blame] | 3711 | LOG(INFO) << "Publishing " << *dispatchEntry << " to " |
| 3712 | << connection->getInputChannelName(); |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 3713 | } |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 3714 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3715 | status = publishMotionEvent(*connection, *dispatchEntry); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 3716 | if (mTracer) { |
| 3717 | mTracer->traceEventDispatch(*dispatchEntry, motionEntry.traceTracker.get()); |
| 3718 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3719 | break; |
| 3720 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3721 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3722 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3723 | const FocusEntry& focusEntry = static_cast<const FocusEntry&>(eventEntry); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3724 | status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3725 | focusEntry.id, |
Antonio Kantek | 3cfec7b | 2021-11-05 18:26:17 -0700 | [diff] [blame] | 3726 | focusEntry.hasFocus); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3727 | break; |
| 3728 | } |
| 3729 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3730 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 3731 | const TouchModeEntry& touchModeEntry = |
| 3732 | static_cast<const TouchModeEntry&>(eventEntry); |
| 3733 | status = connection->inputPublisher |
| 3734 | .publishTouchModeEvent(dispatchEntry->seq, touchModeEntry.id, |
| 3735 | touchModeEntry.inTouchMode); |
| 3736 | |
| 3737 | break; |
| 3738 | } |
| 3739 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3740 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 3741 | const auto& captureEntry = |
| 3742 | static_cast<const PointerCaptureChangedEntry&>(eventEntry); |
| 3743 | status = connection->inputPublisher |
| 3744 | .publishCaptureEvent(dispatchEntry->seq, captureEntry.id, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 3745 | captureEntry.pointerCaptureRequest.enable); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3746 | break; |
| 3747 | } |
| 3748 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3749 | case EventEntry::Type::DRAG: { |
| 3750 | const DragEntry& dragEntry = static_cast<const DragEntry&>(eventEntry); |
| 3751 | status = connection->inputPublisher.publishDragEvent(dispatchEntry->seq, |
| 3752 | dragEntry.id, dragEntry.x, |
| 3753 | dragEntry.y, |
| 3754 | dragEntry.isExiting); |
| 3755 | break; |
| 3756 | } |
| 3757 | |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3758 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3759 | case EventEntry::Type::DEVICE_RESET: |
| 3760 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3761 | LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3762 | ftl::enum_string(eventEntry.type).c_str()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3763 | return; |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3764 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3765 | } |
| 3766 | |
| 3767 | // Check the result. |
| 3768 | if (status) { |
| 3769 | if (status == WOULD_BLOCK) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3770 | if (connection->waitQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3771 | ALOGE("channel '%s' ~ Could not publish event because the pipe is full. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3772 | "This is unexpected because the wait queue is empty, so the pipe " |
| 3773 | "should be empty and we shouldn't have any problems writing an " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3774 | "event to it, status=%s(%d)", |
| 3775 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3776 | status); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 3777 | abortBrokenDispatchCycleLocked(currentTime, connection, /*notify=*/true); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3778 | } else { |
| 3779 | // Pipe is full and we are waiting for the app to finish process some events |
| 3780 | // before sending more events to it. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3781 | if (DEBUG_DISPATCH_CYCLE) { |
| 3782 | ALOGD("channel '%s' ~ Could not publish event because the pipe is full, " |
| 3783 | "waiting for the application to catch up", |
| 3784 | connection->getInputChannelName().c_str()); |
| 3785 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3786 | } |
| 3787 | } else { |
| 3788 | ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3789 | "status=%s(%d)", |
| 3790 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3791 | status); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 3792 | abortBrokenDispatchCycleLocked(currentTime, connection, /*notify=*/true); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3793 | } |
| 3794 | return; |
| 3795 | } |
| 3796 | |
| 3797 | // Re-enqueue the event on the wait queue. |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 3798 | const nsecs_t timeoutTime = dispatchEntry->timeoutTime; |
| 3799 | connection->waitQueue.emplace_back(std::move(dispatchEntry)); |
| 3800 | connection->outboundQueue.erase(connection->outboundQueue.begin()); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3801 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3802 | if (connection->responsive) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 3803 | mAnrTracker.insert(timeoutTime, connection->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3804 | } |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3805 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3806 | } |
| 3807 | } |
| 3808 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3809 | std::array<uint8_t, 32> InputDispatcher::sign(const VerifiedInputEvent& event) const { |
| 3810 | size_t size; |
| 3811 | switch (event.type) { |
| 3812 | case VerifiedInputEvent::Type::KEY: { |
| 3813 | size = sizeof(VerifiedKeyEvent); |
| 3814 | break; |
| 3815 | } |
| 3816 | case VerifiedInputEvent::Type::MOTION: { |
| 3817 | size = sizeof(VerifiedMotionEvent); |
| 3818 | break; |
| 3819 | } |
| 3820 | } |
| 3821 | const uint8_t* start = reinterpret_cast<const uint8_t*>(&event); |
| 3822 | return mHmacKeyManager.sign(start, size); |
| 3823 | } |
| 3824 | |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3825 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3826 | const MotionEntry& motionEntry, const DispatchEntry& dispatchEntry) const { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3827 | const int32_t actionMasked = MotionEvent::getActionMasked(motionEntry.action); |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3828 | if (actionMasked != AMOTION_EVENT_ACTION_UP && actionMasked != AMOTION_EVENT_ACTION_DOWN) { |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3829 | // Only sign events up and down events as the purely move events |
| 3830 | // are tied to their up/down counterparts so signing would be redundant. |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3831 | return INVALID_HMAC; |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3832 | } |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3833 | |
| 3834 | VerifiedMotionEvent verifiedEvent = |
| 3835 | verifiedMotionEventFromMotionEntry(motionEntry, dispatchEntry.rawTransform); |
| 3836 | verifiedEvent.actionMasked = actionMasked; |
| 3837 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS; |
| 3838 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3839 | } |
| 3840 | |
| 3841 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3842 | const KeyEntry& keyEntry, const DispatchEntry& dispatchEntry) const { |
| 3843 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(keyEntry); |
| 3844 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_KEY_EVENT_FLAGS; |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3845 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3846 | } |
| 3847 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3848 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3849 | const std::shared_ptr<Connection>& connection, |
| 3850 | uint32_t seq, bool handled, nsecs_t consumeTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3851 | if (DEBUG_DISPATCH_CYCLE) { |
| 3852 | ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s", |
| 3853 | connection->getInputChannelName().c_str(), seq, toString(handled)); |
| 3854 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3855 | |
Prabir Pradhan | 98ca4a2 | 2024-01-09 23:51:50 +0000 | [diff] [blame] | 3856 | if (connection->status != Connection::Status::NORMAL) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3857 | return; |
| 3858 | } |
| 3859 | |
| 3860 | // Notify other system components and prepare to start the next dispatch cycle. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3861 | auto command = [this, currentTime, connection, seq, handled, consumeTime]() REQUIRES(mLock) { |
| 3862 | doDispatchCycleFinishedCommand(currentTime, connection, seq, handled, consumeTime); |
| 3863 | }; |
| 3864 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3865 | } |
| 3866 | |
| 3867 | void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3868 | const std::shared_ptr<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3869 | bool notify) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3870 | if (DEBUG_DISPATCH_CYCLE) { |
Siarhei Vishniakou | 827d1ac | 2023-07-21 16:37:51 -0700 | [diff] [blame] | 3871 | LOG(INFO) << "channel '" << connection->getInputChannelName() << "'~ " << __func__ |
| 3872 | << " - notify=" << toString(notify); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3873 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3874 | |
| 3875 | // Clear the dispatch queues. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3876 | drainDispatchQueue(connection->outboundQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3877 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3878 | drainDispatchQueue(connection->waitQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3879 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3880 | |
| 3881 | // The connection appears to be unrecoverably broken. |
| 3882 | // Ignore already broken or zombie connections. |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3883 | if (connection->status == Connection::Status::NORMAL) { |
| 3884 | connection->status = Connection::Status::BROKEN; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3885 | |
| 3886 | if (notify) { |
| 3887 | // Notify other system components. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3888 | ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", |
| 3889 | connection->getInputChannelName().c_str()); |
| 3890 | |
| 3891 | auto command = [this, connection]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3892 | scoped_unlock unlock(mLock); |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 3893 | mPolicy.notifyInputChannelBroken(connection->getToken()); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3894 | }; |
| 3895 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3896 | } |
| 3897 | } |
| 3898 | } |
| 3899 | |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 3900 | void InputDispatcher::drainDispatchQueue(std::deque<std::unique_ptr<DispatchEntry>>& queue) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3901 | while (!queue.empty()) { |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 3902 | releaseDispatchEntry(std::move(queue.front())); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3903 | queue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3904 | } |
| 3905 | } |
| 3906 | |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 3907 | void InputDispatcher::releaseDispatchEntry(std::unique_ptr<DispatchEntry> dispatchEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3908 | if (dispatchEntry->hasForegroundTarget()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3909 | decrementPendingForegroundDispatches(*(dispatchEntry->eventEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3910 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3911 | } |
| 3912 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3913 | int InputDispatcher::handleReceiveCallback(int events, sp<IBinder> connectionToken) { |
| 3914 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3915 | std::shared_ptr<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3916 | if (connection == nullptr) { |
| 3917 | ALOGW("Received looper callback for unknown input channel token %p. events=0x%x", |
| 3918 | connectionToken.get(), events); |
| 3919 | return 0; // remove the callback |
| 3920 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3921 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3922 | bool notify; |
| 3923 | if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { |
| 3924 | if (!(events & ALOOPER_EVENT_INPUT)) { |
| 3925 | ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. " |
| 3926 | "events=0x%x", |
| 3927 | connection->getInputChannelName().c_str(), events); |
| 3928 | return 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3929 | } |
| 3930 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3931 | nsecs_t currentTime = now(); |
| 3932 | bool gotOne = false; |
| 3933 | status_t status = OK; |
| 3934 | for (;;) { |
| 3935 | Result<InputPublisher::ConsumerResponse> result = |
| 3936 | connection->inputPublisher.receiveConsumerResponse(); |
| 3937 | if (!result.ok()) { |
| 3938 | status = result.error().code(); |
| 3939 | break; |
| 3940 | } |
| 3941 | |
| 3942 | if (std::holds_alternative<InputPublisher::Finished>(*result)) { |
| 3943 | const InputPublisher::Finished& finish = |
| 3944 | std::get<InputPublisher::Finished>(*result); |
| 3945 | finishDispatchCycleLocked(currentTime, connection, finish.seq, finish.handled, |
| 3946 | finish.consumeTime); |
| 3947 | } else if (std::holds_alternative<InputPublisher::Timeline>(*result)) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3948 | if (shouldReportMetricsForConnection(*connection)) { |
| 3949 | const InputPublisher::Timeline& timeline = |
| 3950 | std::get<InputPublisher::Timeline>(*result); |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 3951 | mLatencyTracker.trackGraphicsLatency(timeline.inputEventId, |
| 3952 | connection->getToken(), |
| 3953 | std::move(timeline.graphicsTimeline)); |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3954 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3955 | } |
| 3956 | gotOne = true; |
| 3957 | } |
| 3958 | if (gotOne) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3959 | runCommandsLockedInterruptable(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3960 | if (status == WOULD_BLOCK) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3961 | return 1; |
| 3962 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3963 | } |
| 3964 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3965 | notify = status != DEAD_OBJECT || !connection->monitor; |
| 3966 | if (notify) { |
| 3967 | ALOGE("channel '%s' ~ Failed to receive finished signal. status=%s(%d)", |
| 3968 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3969 | status); |
| 3970 | } |
| 3971 | } else { |
| 3972 | // Monitor channels are never explicitly unregistered. |
| 3973 | // 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] | 3974 | const bool stillHaveWindowHandle = getWindowHandleLocked(connection->getToken()) != nullptr; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3975 | notify = !connection->monitor && stillHaveWindowHandle; |
| 3976 | if (notify) { |
| 3977 | ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred. events=0x%x", |
| 3978 | connection->getInputChannelName().c_str(), events); |
| 3979 | } |
| 3980 | } |
| 3981 | |
| 3982 | // Remove the channel. |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 3983 | removeInputChannelLocked(connection->getToken(), notify); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3984 | return 0; // remove the callback |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3985 | } |
| 3986 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3987 | void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3988 | const CancelationOptions& options) { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3989 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 3990 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3991 | } |
| 3992 | } |
| 3993 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3994 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3995 | const CancelationOptions& options) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 3996 | for (const auto& [_, monitors] : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3997 | for (const Monitor& monitor : monitors) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 3998 | synthesizeCancelationEventsForConnectionLocked(monitor.connection, options); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3999 | } |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 4000 | } |
| 4001 | } |
| 4002 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4003 | void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 4004 | const std::shared_ptr<Connection>& connection, const CancelationOptions& options) { |
Prabir Pradhan | b13da8f | 2024-01-09 23:10:13 +0000 | [diff] [blame] | 4005 | if (connection->status != Connection::Status::NORMAL) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4006 | return; |
| 4007 | } |
| 4008 | |
| 4009 | nsecs_t currentTime = now(); |
| 4010 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4011 | std::vector<std::unique_ptr<EventEntry>> cancelationEvents = |
Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 4012 | connection->inputState.synthesizeCancelationEvents(currentTime, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4013 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4014 | if (cancelationEvents.empty()) { |
| 4015 | return; |
| 4016 | } |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 4017 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4018 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 4019 | 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] | 4020 | "with reality: %s, mode=%s.", |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4021 | connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason, |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 4022 | ftl::enum_string(options.mode).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4023 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4024 | |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 4025 | std::string reason = std::string("reason=").append(options.reason); |
| 4026 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 4027 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 4028 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 4029 | const bool wasEmpty = connection->outboundQueue.empty(); |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 4030 | // The target to use if we don't find a window associated with the channel. |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 4031 | const InputTarget fallbackTarget{.connection = connection}; |
| 4032 | const auto& token = connection->getToken(); |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 4033 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4034 | for (size_t i = 0; i < cancelationEvents.size(); i++) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4035 | std::unique_ptr<EventEntry> cancelationEventEntry = std::move(cancelationEvents[i]); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4036 | std::vector<InputTarget> targets{}; |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4037 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4038 | switch (cancelationEventEntry->type) { |
| 4039 | case EventEntry::Type::KEY: { |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4040 | const auto& keyEntry = static_cast<const KeyEntry&>(*cancelationEventEntry); |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 4041 | const std::optional<int32_t> targetDisplay = keyEntry.displayId != ADISPLAY_ID_NONE |
| 4042 | ? std::make_optional(keyEntry.displayId) |
| 4043 | : std::nullopt; |
| 4044 | if (const auto& window = getWindowHandleLocked(token, targetDisplay); window) { |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 4045 | addWindowTargetLocked(window, InputTarget::DispatchMode::AS_IS, |
| 4046 | /*targetFlags=*/{}, keyEntry.downTime, targets); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4047 | } else { |
| 4048 | targets.emplace_back(fallbackTarget); |
| 4049 | } |
| 4050 | logOutboundKeyDetails("cancel - ", keyEntry); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4051 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4052 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4053 | case EventEntry::Type::MOTION: { |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4054 | const auto& motionEntry = static_cast<const MotionEntry&>(*cancelationEventEntry); |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 4055 | const std::optional<int32_t> targetDisplay = |
| 4056 | motionEntry.displayId != ADISPLAY_ID_NONE |
| 4057 | ? std::make_optional(motionEntry.displayId) |
| 4058 | : std::nullopt; |
| 4059 | if (const auto& window = getWindowHandleLocked(token, targetDisplay); window) { |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4060 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4061 | for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.getPointerCount(); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4062 | pointerIndex++) { |
| 4063 | pointerIds.set(motionEntry.pointerProperties[pointerIndex].id); |
| 4064 | } |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 4065 | if (mDragState && mDragState->dragWindow->getToken() == token && |
| 4066 | pointerIds.test(mDragState->pointerId)) { |
| 4067 | LOG(INFO) << __func__ |
| 4068 | << ": Canceling drag and drop because the pointers for the drag " |
| 4069 | "window are being canceled."; |
| 4070 | sendDropWindowCommandLocked(nullptr, /*x=*/0, /*y=*/0); |
| 4071 | mDragState.reset(); |
| 4072 | } |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 4073 | addPointerWindowTargetLocked(window, InputTarget::DispatchMode::AS_IS, |
| 4074 | ftl::Flags<InputTarget::Flags>(), pointerIds, |
| 4075 | motionEntry.downTime, targets); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4076 | } else { |
| 4077 | targets.emplace_back(fallbackTarget); |
| 4078 | const auto it = mDisplayInfos.find(motionEntry.displayId); |
| 4079 | if (it != mDisplayInfos.end()) { |
| 4080 | targets.back().displayTransform = it->second.transform; |
| 4081 | targets.back().setDefaultPointerTransform(it->second.transform); |
| 4082 | } |
| 4083 | } |
| 4084 | logOutboundMotionDetails("cancel - ", motionEntry); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4085 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4086 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4087 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 4088 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4089 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 4090 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4091 | LOG_ALWAYS_FATAL("Canceling %s events is not supported", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 4092 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4093 | break; |
| 4094 | } |
| 4095 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4096 | case EventEntry::Type::DEVICE_RESET: |
| 4097 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4098 | 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] | 4099 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4100 | break; |
| 4101 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4102 | } |
| 4103 | |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4104 | if (targets.size() != 1) LOG(FATAL) << __func__ << ": InputTarget not created"; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 4105 | enqueueDispatchEntryLocked(connection, std::move(cancelationEventEntry), targets[0]); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4106 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4107 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 4108 | // If the outbound queue was previously empty, start the dispatch cycle going. |
| 4109 | if (wasEmpty && !connection->outboundQueue.empty()) { |
| 4110 | startDispatchCycleLocked(currentTime, connection); |
| 4111 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4112 | } |
| 4113 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4114 | void InputDispatcher::synthesizePointerDownEventsForConnectionLocked( |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 4115 | const nsecs_t downTime, const std::shared_ptr<Connection>& connection, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4116 | ftl::Flags<InputTarget::Flags> targetFlags) { |
Prabir Pradhan | 98ca4a2 | 2024-01-09 23:51:50 +0000 | [diff] [blame] | 4117 | if (connection->status != Connection::Status::NORMAL) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4118 | return; |
| 4119 | } |
| 4120 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4121 | std::vector<std::unique_ptr<EventEntry>> downEvents = |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4122 | connection->inputState.synthesizePointerDownEvents(downTime); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4123 | |
| 4124 | if (downEvents.empty()) { |
| 4125 | return; |
| 4126 | } |
| 4127 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4128 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4129 | ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.", |
| 4130 | connection->getInputChannelName().c_str(), downEvents.size()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4131 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4132 | |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 4133 | sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(connection->getToken()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4134 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 4135 | const bool wasEmpty = connection->outboundQueue.empty(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4136 | for (std::unique_ptr<EventEntry>& downEventEntry : downEvents) { |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 4137 | std::vector<InputTarget> targets{}; |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4138 | switch (downEventEntry->type) { |
| 4139 | case EventEntry::Type::MOTION: { |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 4140 | const auto& motionEntry = static_cast<const MotionEntry&>(*downEventEntry); |
| 4141 | if (windowHandle != nullptr) { |
| 4142 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4143 | for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.getPointerCount(); |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 4144 | pointerIndex++) { |
| 4145 | pointerIds.set(motionEntry.pointerProperties[pointerIndex].id); |
| 4146 | } |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 4147 | addPointerWindowTargetLocked(windowHandle, InputTarget::DispatchMode::AS_IS, |
| 4148 | targetFlags, pointerIds, motionEntry.downTime, |
| 4149 | targets); |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 4150 | } else { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 4151 | targets.emplace_back( |
| 4152 | InputTarget{.connection = connection, .flags = targetFlags}); |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 4153 | const auto it = mDisplayInfos.find(motionEntry.displayId); |
| 4154 | if (it != mDisplayInfos.end()) { |
| 4155 | targets.back().displayTransform = it->second.transform; |
| 4156 | targets.back().setDefaultPointerTransform(it->second.transform); |
| 4157 | } |
| 4158 | } |
| 4159 | logOutboundMotionDetails("down - ", motionEntry); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4160 | break; |
| 4161 | } |
| 4162 | |
| 4163 | case EventEntry::Type::KEY: |
| 4164 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 4165 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4166 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4167 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4168 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4169 | case EventEntry::Type::SENSOR: |
| 4170 | case EventEntry::Type::DRAG: { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4171 | 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] | 4172 | ftl::enum_string(downEventEntry->type).c_str()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4173 | break; |
| 4174 | } |
| 4175 | } |
| 4176 | |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 4177 | if (targets.size() != 1) LOG(FATAL) << __func__ << ": InputTarget not created"; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 4178 | enqueueDispatchEntryLocked(connection, std::move(downEventEntry), targets[0]); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4179 | } |
| 4180 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 4181 | // If the outbound queue was previously empty, start the dispatch cycle going. |
| 4182 | if (wasEmpty && !connection->outboundQueue.empty()) { |
| 4183 | startDispatchCycleLocked(downTime, connection); |
| 4184 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4185 | } |
| 4186 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4187 | void InputDispatcher::synthesizeCancelationEventsForWindowLocked( |
| 4188 | const sp<WindowInfoHandle>& windowHandle, const CancelationOptions& options) { |
| 4189 | if (windowHandle != nullptr) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 4190 | std::shared_ptr<Connection> wallpaperConnection = |
| 4191 | getConnectionLocked(windowHandle->getToken()); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4192 | if (wallpaperConnection != nullptr) { |
| 4193 | synthesizeCancelationEventsForConnectionLocked(wallpaperConnection, options); |
| 4194 | } |
| 4195 | } |
| 4196 | } |
| 4197 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4198 | std::unique_ptr<MotionEntry> InputDispatcher::splitMotionEvent( |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 4199 | const MotionEntry& originalMotionEntry, std::bitset<MAX_POINTER_ID + 1> pointerIds, |
| 4200 | nsecs_t splitDownTime) { |
| 4201 | ALOG_ASSERT(pointerIds.any()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4202 | |
| 4203 | uint32_t splitPointerIndexMap[MAX_POINTERS]; |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4204 | std::vector<PointerProperties> splitPointerProperties; |
| 4205 | std::vector<PointerCoords> splitPointerCoords; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4206 | |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4207 | uint32_t originalPointerCount = originalMotionEntry.getPointerCount(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4208 | uint32_t splitPointerCount = 0; |
| 4209 | |
| 4210 | for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4211 | originalPointerIndex++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4212 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 4213 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4214 | uint32_t pointerId = uint32_t(pointerProperties.id); |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 4215 | if (pointerIds.test(pointerId)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4216 | splitPointerIndexMap[splitPointerCount] = originalPointerIndex; |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4217 | splitPointerProperties.push_back(pointerProperties); |
| 4218 | splitPointerCoords.push_back(originalMotionEntry.pointerCoords[originalPointerIndex]); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4219 | splitPointerCount += 1; |
| 4220 | } |
| 4221 | } |
| 4222 | |
| 4223 | if (splitPointerCount != pointerIds.count()) { |
| 4224 | // This is bad. We are missing some of the pointers that we expected to deliver. |
| 4225 | // Most likely this indicates that we received an ACTION_MOVE events that has |
| 4226 | // different pointer ids than we expected based on the previous ACTION_DOWN |
| 4227 | // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers |
| 4228 | // in this way. |
| 4229 | ALOGW("Dropping split motion event because the pointer count is %d but " |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 4230 | "we expected there to be %zu pointers. This probably means we received " |
Siarhei Vishniakou | 16e4fa0 | 2023-02-16 17:48:56 -0800 | [diff] [blame] | 4231 | "a broken sequence of pointer ids from the input device: %s", |
| 4232 | splitPointerCount, pointerIds.count(), originalMotionEntry.getDescription().c_str()); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4233 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4234 | } |
| 4235 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 4236 | int32_t action = originalMotionEntry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4237 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4238 | if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN || |
| 4239 | maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
Siarhei Vishniakou | 5b9766d | 2023-07-18 14:06:29 -0700 | [diff] [blame] | 4240 | int32_t originalPointerIndex = MotionEvent::getActionIndex(action); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4241 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 4242 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4243 | uint32_t pointerId = uint32_t(pointerProperties.id); |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 4244 | if (pointerIds.test(pointerId)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4245 | if (pointerIds.count() == 1) { |
| 4246 | // The first/last pointer went down/up. |
| 4247 | action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4248 | ? AMOTION_EVENT_ACTION_DOWN |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4249 | : (originalMotionEntry.flags & AMOTION_EVENT_FLAG_CANCELED) != 0 |
| 4250 | ? AMOTION_EVENT_ACTION_CANCEL |
| 4251 | : AMOTION_EVENT_ACTION_UP; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4252 | } else { |
| 4253 | // A secondary pointer went down/up. |
| 4254 | uint32_t splitPointerIndex = 0; |
| 4255 | while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { |
| 4256 | splitPointerIndex += 1; |
| 4257 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4258 | action = maskedAction | |
| 4259 | (splitPointerIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4260 | } |
| 4261 | } else { |
| 4262 | // An unrelated pointer changed. |
| 4263 | action = AMOTION_EVENT_ACTION_MOVE; |
| 4264 | } |
| 4265 | } |
| 4266 | |
Siarhei Vishniakou | 59e302b | 2023-06-05 08:04:53 -0700 | [diff] [blame] | 4267 | if (action == AMOTION_EVENT_ACTION_DOWN && splitDownTime != originalMotionEntry.eventTime) { |
| 4268 | logDispatchStateLocked(); |
| 4269 | LOG_ALWAYS_FATAL("Split motion event has mismatching downTime and eventTime for " |
| 4270 | "ACTION_DOWN, motionEntry=%s, splitDownTime=%" PRId64, |
| 4271 | originalMotionEntry.getDescription().c_str(), splitDownTime); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4272 | } |
| 4273 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4274 | int32_t newId = mIdGenerator.nextId(); |
Prabir Pradhan | 2dac8b8 | 2023-09-06 01:11:51 +0000 | [diff] [blame] | 4275 | ATRACE_NAME_IF(ATRACE_ENABLED(), |
| 4276 | StringPrintf("Split MotionEvent(id=0x%" PRIx32 ") to MotionEvent(id=0x%" PRIx32 |
| 4277 | ").", |
| 4278 | originalMotionEntry.id, newId)); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4279 | std::unique_ptr<MotionEntry> splitMotionEntry = |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4280 | std::make_unique<MotionEntry>(newId, originalMotionEntry.injectionState, |
| 4281 | originalMotionEntry.eventTime, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4282 | originalMotionEntry.deviceId, originalMotionEntry.source, |
| 4283 | originalMotionEntry.displayId, |
| 4284 | originalMotionEntry.policyFlags, action, |
| 4285 | originalMotionEntry.actionButton, |
| 4286 | originalMotionEntry.flags, originalMotionEntry.metaState, |
| 4287 | originalMotionEntry.buttonState, |
| 4288 | originalMotionEntry.classification, |
| 4289 | originalMotionEntry.edgeFlags, |
| 4290 | originalMotionEntry.xPrecision, |
| 4291 | originalMotionEntry.yPrecision, |
| 4292 | originalMotionEntry.xCursorPosition, |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4293 | originalMotionEntry.yCursorPosition, splitDownTime, |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4294 | splitPointerProperties, splitPointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4295 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4296 | return splitMotionEntry; |
| 4297 | } |
| 4298 | |
Asmita Poddar | dd9a6cd | 2023-09-26 15:35:12 +0000 | [diff] [blame] | 4299 | void InputDispatcher::notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs& args) { |
| 4300 | std::scoped_lock _l(mLock); |
| 4301 | mLatencyTracker.setInputDevices(args.inputDeviceInfos); |
| 4302 | } |
| 4303 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4304 | void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4305 | if (debugInboundEventDetails()) { |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4306 | ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args.eventTime); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4307 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4308 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4309 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4310 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4311 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4312 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4313 | std::unique_ptr<ConfigurationChangedEntry> newEntry = |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4314 | std::make_unique<ConfigurationChangedEntry>(args.id, args.eventTime); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4315 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4316 | } // release lock |
| 4317 | |
| 4318 | if (needWake) { |
| 4319 | mLooper->wake(); |
| 4320 | } |
| 4321 | } |
| 4322 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4323 | void InputDispatcher::notifyKey(const NotifyKeyArgs& args) { |
Prabir Pradhan | 96282b0 | 2023-02-24 22:36:17 +0000 | [diff] [blame] | 4324 | ALOGD_IF(debugInboundEventDetails(), |
| 4325 | "notifyKey - id=%" PRIx32 ", eventTime=%" PRId64 |
| 4326 | ", deviceId=%d, source=%s, displayId=%" PRId32 |
| 4327 | "policyFlags=0x%x, action=%s, flags=0x%x, keyCode=%s, scanCode=0x%x, metaState=0x%x, " |
| 4328 | "downTime=%" PRId64, |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4329 | args.id, args.eventTime, args.deviceId, inputEventSourceToString(args.source).c_str(), |
| 4330 | args.displayId, args.policyFlags, KeyEvent::actionToString(args.action), args.flags, |
| 4331 | KeyEvent::getLabel(args.keyCode), args.scanCode, args.metaState, args.downTime); |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4332 | Result<void> keyCheck = validateKeyEvent(args.action); |
| 4333 | if (!keyCheck.ok()) { |
| 4334 | LOG(ERROR) << "invalid key event: " << keyCheck.error(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4335 | return; |
| 4336 | } |
| 4337 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4338 | uint32_t policyFlags = args.policyFlags; |
| 4339 | int32_t flags = args.flags; |
| 4340 | int32_t metaState = args.metaState; |
Siarhei Vishniakou | 622bd32 | 2018-10-29 18:02:27 -0700 | [diff] [blame] | 4341 | // InputDispatcher tracks and generates key repeats on behalf of |
| 4342 | // whatever notifies it, so repeatCount should always be set to 0 |
| 4343 | constexpr int32_t repeatCount = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4344 | if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { |
| 4345 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 4346 | flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
| 4347 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4348 | if (policyFlags & POLICY_FLAG_FUNCTION) { |
| 4349 | metaState |= AMETA_FUNCTION_ON; |
| 4350 | } |
| 4351 | |
| 4352 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 4353 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4354 | int32_t keyCode = args.keyCode; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4355 | KeyEvent event; |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4356 | event.initialize(args.id, args.deviceId, args.source, args.displayId, INVALID_HMAC, args.action, |
| 4357 | flags, keyCode, args.scanCode, metaState, repeatCount, args.downTime, |
| 4358 | args.eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4359 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4360 | android::base::Timer t; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4361 | mPolicy.interceptKeyBeforeQueueing(event, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4362 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4363 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4364 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4365 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4366 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4367 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4368 | { // acquire lock |
| 4369 | mLock.lock(); |
| 4370 | |
| 4371 | if (shouldSendKeyToInputFilterLocked(args)) { |
| 4372 | mLock.unlock(); |
| 4373 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4374 | policyFlags |= POLICY_FLAG_FILTERED; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4375 | if (!mPolicy.filterInputEvent(event, policyFlags)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4376 | return; // event was consumed by the filter |
| 4377 | } |
| 4378 | |
| 4379 | mLock.lock(); |
| 4380 | } |
| 4381 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4382 | std::unique_ptr<KeyEntry> newEntry = |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4383 | std::make_unique<KeyEntry>(args.id, /*injectionState=*/nullptr, args.eventTime, |
| 4384 | args.deviceId, args.source, args.displayId, policyFlags, |
| 4385 | args.action, flags, keyCode, args.scanCode, metaState, |
| 4386 | repeatCount, args.downTime); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 4387 | if (mTracer) { |
| 4388 | newEntry->traceTracker = mTracer->traceInboundEvent(*newEntry); |
| 4389 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4390 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4391 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4392 | mLock.unlock(); |
| 4393 | } // release lock |
| 4394 | |
| 4395 | if (needWake) { |
| 4396 | mLooper->wake(); |
| 4397 | } |
| 4398 | } |
| 4399 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4400 | bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs& args) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4401 | return mInputFilterEnabled; |
| 4402 | } |
| 4403 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4404 | void InputDispatcher::notifyMotion(const NotifyMotionArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4405 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 96282b0 | 2023-02-24 22:36:17 +0000 | [diff] [blame] | 4406 | ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=%s, " |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4407 | "displayId=%" PRId32 ", policyFlags=0x%x, " |
Siarhei Vishniakou | 6ebd069 | 2022-10-20 15:05:45 -0700 | [diff] [blame] | 4408 | "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] | 4409 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, xCursorPosition=%f, " |
| 4410 | "yCursorPosition=%f, downTime=%" PRId64, |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4411 | args.id, args.eventTime, args.deviceId, inputEventSourceToString(args.source).c_str(), |
| 4412 | args.displayId, args.policyFlags, MotionEvent::actionToString(args.action).c_str(), |
| 4413 | args.actionButton, args.flags, args.metaState, args.buttonState, args.edgeFlags, |
| 4414 | args.xPrecision, args.yPrecision, args.xCursorPosition, args.yCursorPosition, |
| 4415 | args.downTime); |
Siarhei Vishniakou | 3218fc0 | 2023-06-15 20:41:02 -0700 | [diff] [blame] | 4416 | for (uint32_t i = 0; i < args.getPointerCount(); i++) { |
Prabir Pradhan | 96282b0 | 2023-02-24 22:36:17 +0000 | [diff] [blame] | 4417 | ALOGD(" Pointer %d: id=%d, toolType=%s, x=%f, y=%f, pressure=%f, size=%f, " |
| 4418 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, orientation=%f", |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4419 | i, args.pointerProperties[i].id, |
| 4420 | ftl::enum_string(args.pointerProperties[i].toolType).c_str(), |
| 4421 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 4422 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 4423 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 4424 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 4425 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 4426 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 4427 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 4428 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 4429 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4430 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4431 | } |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4432 | |
Siarhei Vishniakou | 3218fc0 | 2023-06-15 20:41:02 -0700 | [diff] [blame] | 4433 | Result<void> motionCheck = |
| 4434 | validateMotionEvent(args.action, args.actionButton, args.getPointerCount(), |
| 4435 | args.pointerProperties.data()); |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4436 | if (!motionCheck.ok()) { |
| 4437 | LOG(FATAL) << "Invalid event: " << args.dump() << "; reason: " << motionCheck.error(); |
| 4438 | return; |
| 4439 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4440 | |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 4441 | if (DEBUG_VERIFY_EVENTS) { |
| 4442 | auto [it, _] = |
| 4443 | mVerifiersByDisplay.try_emplace(args.displayId, |
| 4444 | StringPrintf("display %" PRId32, args.displayId)); |
| 4445 | Result<void> result = |
Siarhei Vishniakou | 2d151ac | 2023-09-19 13:30:24 -0700 | [diff] [blame] | 4446 | it->second.processMovement(args.deviceId, args.source, args.action, |
| 4447 | args.getPointerCount(), args.pointerProperties.data(), |
| 4448 | args.pointerCoords.data(), args.flags); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 4449 | if (!result.ok()) { |
| 4450 | LOG(FATAL) << "Bad stream: " << result.error() << " caused by " << args.dump(); |
| 4451 | } |
| 4452 | } |
| 4453 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4454 | uint32_t policyFlags = args.policyFlags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4455 | policyFlags |= POLICY_FLAG_TRUSTED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4456 | |
| 4457 | android::base::Timer t; |
Yeabkal Wubshit | 88a9041 | 2023-12-21 18:23:04 -0800 | [diff] [blame] | 4458 | mPolicy.interceptMotionBeforeQueueing(args.displayId, args.source, args.action, args.eventTime, |
| 4459 | policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4460 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4461 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4462 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4463 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4464 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4465 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4466 | { // acquire lock |
| 4467 | mLock.lock(); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 4468 | if (!(policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
| 4469 | // Set the flag anyway if we already have an ongoing gesture. That would allow us to |
| 4470 | // complete the processing of the current stroke. |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4471 | const auto touchStateIt = mTouchStatesByDisplay.find(args.displayId); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 4472 | if (touchStateIt != mTouchStatesByDisplay.end()) { |
| 4473 | const TouchState& touchState = touchStateIt->second; |
Linnan Li | 907ae73 | 2023-09-05 17:14:21 +0800 | [diff] [blame] | 4474 | if (touchState.hasTouchingPointers(args.deviceId) || |
| 4475 | touchState.hasHoveringPointers(args.deviceId)) { |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 4476 | policyFlags |= POLICY_FLAG_PASS_TO_USER; |
| 4477 | } |
| 4478 | } |
| 4479 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4480 | |
| 4481 | if (shouldSendMotionToInputFilterLocked(args)) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4482 | ui::Transform displayTransform; |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4483 | if (const auto it = mDisplayInfos.find(args.displayId); it != mDisplayInfos.end()) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4484 | displayTransform = it->second.transform; |
| 4485 | } |
| 4486 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4487 | mLock.unlock(); |
| 4488 | |
| 4489 | MotionEvent event; |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4490 | event.initialize(args.id, args.deviceId, args.source, args.displayId, INVALID_HMAC, |
| 4491 | args.action, args.actionButton, args.flags, args.edgeFlags, |
| 4492 | args.metaState, args.buttonState, args.classification, |
| 4493 | displayTransform, args.xPrecision, args.yPrecision, |
| 4494 | args.xCursorPosition, args.yCursorPosition, displayTransform, |
Siarhei Vishniakou | 3218fc0 | 2023-06-15 20:41:02 -0700 | [diff] [blame] | 4495 | args.downTime, args.eventTime, args.getPointerCount(), |
| 4496 | args.pointerProperties.data(), args.pointerCoords.data()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4497 | |
| 4498 | policyFlags |= POLICY_FLAG_FILTERED; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4499 | if (!mPolicy.filterInputEvent(event, policyFlags)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4500 | return; // event was consumed by the filter |
| 4501 | } |
| 4502 | |
| 4503 | mLock.lock(); |
| 4504 | } |
| 4505 | |
| 4506 | // Just enqueue a new motion event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4507 | std::unique_ptr<MotionEntry> newEntry = |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4508 | std::make_unique<MotionEntry>(args.id, /*injectionState=*/nullptr, args.eventTime, |
| 4509 | args.deviceId, args.source, args.displayId, |
| 4510 | policyFlags, args.action, args.actionButton, |
| 4511 | args.flags, args.metaState, args.buttonState, |
| 4512 | args.classification, args.edgeFlags, args.xPrecision, |
| 4513 | args.yPrecision, args.xCursorPosition, |
| 4514 | args.yCursorPosition, args.downTime, |
| 4515 | args.pointerProperties, args.pointerCoords); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 4516 | if (mTracer) { |
| 4517 | newEntry->traceTracker = mTracer->traceInboundEvent(*newEntry); |
| 4518 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4519 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4520 | if (args.id != android::os::IInputConstants::INVALID_INPUT_EVENT_ID && |
| 4521 | IdGenerator::getSource(args.id) == IdGenerator::Source::INPUT_READER && |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 4522 | !mInputFilterEnabled) { |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4523 | const bool isDown = args.action == AMOTION_EVENT_ACTION_DOWN; |
Asmita Poddar | dd9a6cd | 2023-09-26 15:35:12 +0000 | [diff] [blame] | 4524 | std::set<InputDeviceUsageSource> sources = getUsageSourcesForMotionArgs(args); |
| 4525 | mLatencyTracker.trackListener(args.id, isDown, args.eventTime, args.readTime, |
| 4526 | args.deviceId, sources); |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 4527 | } |
| 4528 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4529 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4530 | mLock.unlock(); |
| 4531 | } // release lock |
| 4532 | |
| 4533 | if (needWake) { |
| 4534 | mLooper->wake(); |
| 4535 | } |
| 4536 | } |
| 4537 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4538 | void InputDispatcher::notifySensor(const NotifySensorArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4539 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4540 | ALOGD("notifySensor - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 4541 | " sensorType=%s", |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4542 | args.id, args.eventTime, args.deviceId, args.source, |
| 4543 | ftl::enum_string(args.sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4544 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4545 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4546 | bool needWake = false; |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4547 | { // acquire lock |
| 4548 | mLock.lock(); |
| 4549 | |
| 4550 | // Just enqueue a new sensor event. |
| 4551 | std::unique_ptr<SensorEntry> newEntry = |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4552 | std::make_unique<SensorEntry>(args.id, args.eventTime, args.deviceId, args.source, |
| 4553 | /* policyFlags=*/0, args.hwTimestamp, args.sensorType, |
| 4554 | args.accuracy, args.accuracyChanged, args.values); |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4555 | |
| 4556 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
| 4557 | mLock.unlock(); |
| 4558 | } // release lock |
| 4559 | |
| 4560 | if (needWake) { |
| 4561 | mLooper->wake(); |
| 4562 | } |
| 4563 | } |
| 4564 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4565 | void InputDispatcher::notifyVibratorState(const NotifyVibratorStateArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4566 | if (debugInboundEventDetails()) { |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4567 | ALOGD("notifyVibratorState - eventTime=%" PRId64 ", device=%d, isOn=%d", args.eventTime, |
| 4568 | args.deviceId, args.isOn); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4569 | } |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4570 | mPolicy.notifyVibratorState(args.deviceId, args.isOn); |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 4571 | } |
| 4572 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4573 | bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs& args) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4574 | return mInputFilterEnabled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4575 | } |
| 4576 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4577 | void InputDispatcher::notifySwitch(const NotifySwitchArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4578 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4579 | ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, " |
| 4580 | "switchMask=0x%08x", |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4581 | args.eventTime, args.policyFlags, args.switchValues, args.switchMask); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4582 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4583 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4584 | uint32_t policyFlags = args.policyFlags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4585 | policyFlags |= POLICY_FLAG_TRUSTED; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4586 | mPolicy.notifySwitch(args.eventTime, args.switchValues, args.switchMask, policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4587 | } |
| 4588 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4589 | void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4590 | if (debugInboundEventDetails()) { |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4591 | ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args.eventTime, |
| 4592 | args.deviceId); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4593 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4594 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4595 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4596 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4597 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4598 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4599 | std::unique_ptr<DeviceResetEntry> newEntry = |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4600 | std::make_unique<DeviceResetEntry>(args.id, args.eventTime, args.deviceId); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4601 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Siarhei Vishniakou | 1160ecd | 2023-06-28 15:57:47 -0700 | [diff] [blame] | 4602 | |
| 4603 | for (auto& [_, verifier] : mVerifiersByDisplay) { |
| 4604 | verifier.resetDevice(args.deviceId); |
| 4605 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4606 | } // release lock |
| 4607 | |
| 4608 | if (needWake) { |
| 4609 | mLooper->wake(); |
| 4610 | } |
| 4611 | } |
| 4612 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4613 | void InputDispatcher::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4614 | if (debugInboundEventDetails()) { |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4615 | ALOGD("notifyPointerCaptureChanged - eventTime=%" PRId64 ", enabled=%s", args.eventTime, |
| 4616 | args.request.enable ? "true" : "false"); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4617 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4618 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4619 | bool needWake = false; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4620 | { // acquire lock |
| 4621 | std::scoped_lock _l(mLock); |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4622 | auto entry = |
| 4623 | std::make_unique<PointerCaptureChangedEntry>(args.id, args.eventTime, args.request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4624 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 4625 | } // release lock |
| 4626 | |
| 4627 | if (needWake) { |
| 4628 | mLooper->wake(); |
| 4629 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4630 | } |
| 4631 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4632 | InputEventInjectionResult InputDispatcher::injectInputEvent(const InputEvent* event, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 4633 | std::optional<gui::Uid> targetUid, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4634 | InputEventInjectionSync syncMode, |
| 4635 | std::chrono::milliseconds timeout, |
| 4636 | uint32_t policyFlags) { |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4637 | Result<void> eventValidation = validateInputEvent(*event); |
| 4638 | if (!eventValidation.ok()) { |
| 4639 | LOG(INFO) << "Injection failed: invalid event: " << eventValidation.error(); |
| 4640 | return InputEventInjectionResult::FAILED; |
| 4641 | } |
| 4642 | |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4643 | if (debugInboundEventDetails()) { |
Siarhei Vishniakou | 827d1ac | 2023-07-21 16:37:51 -0700 | [diff] [blame] | 4644 | LOG(INFO) << __func__ << ": targetUid=" << toString(targetUid, &uidString) |
| 4645 | << ", syncMode=" << ftl::enum_string(syncMode) << ", timeout=" << timeout.count() |
| 4646 | << "ms, policyFlags=0x" << std::hex << policyFlags << std::dec |
| 4647 | << ", event=" << *event; |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4648 | } |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 4649 | 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] | 4650 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4651 | policyFlags |= POLICY_FLAG_INJECTED | POLICY_FLAG_TRUSTED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4652 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4653 | // 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] | 4654 | // that have gone through the InputFilter. If the event passed through the InputFilter, assign |
| 4655 | // the provided device id. If the InputFilter is accessibility, and it modifies or synthesizes |
| 4656 | // the injected event, it is responsible for setting POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY. |
| 4657 | // For those events, we will set FLAG_IS_ACCESSIBILITY_EVENT to allow apps to distinguish them |
| 4658 | // from events that originate from actual hardware. |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 4659 | DeviceId resolvedDeviceId = VIRTUAL_KEYBOARD_ID; |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4660 | if (policyFlags & POLICY_FLAG_FILTERED) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4661 | resolvedDeviceId = event->getDeviceId(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4662 | } |
| 4663 | |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4664 | const bool isAsync = syncMode == InputEventInjectionSync::NONE; |
| 4665 | auto injectionState = std::make_shared<InjectionState>(targetUid, isAsync); |
| 4666 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4667 | std::queue<std::unique_ptr<EventEntry>> injectedEntries; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4668 | switch (event->getType()) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4669 | case InputEventType::KEY: { |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4670 | const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event); |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4671 | const int32_t action = incomingKey.getAction(); |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4672 | int32_t flags = incomingKey.getFlags(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4673 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4674 | flags |= AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4675 | } |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4676 | int32_t keyCode = incomingKey.getKeyCode(); |
| 4677 | int32_t metaState = incomingKey.getMetaState(); |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4678 | KeyEvent keyEvent; |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4679 | keyEvent.initialize(incomingKey.getId(), resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4680 | incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode, |
| 4681 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), |
| 4682 | incomingKey.getDownTime(), incomingKey.getEventTime()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4683 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4684 | if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) { |
| 4685 | policyFlags |= POLICY_FLAG_VIRTUAL; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4686 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4687 | |
| 4688 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 4689 | android::base::Timer t; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4690 | mPolicy.interceptKeyBeforeQueueing(keyEvent, /*byref*/ policyFlags); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4691 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4692 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
| 4693 | std::to_string(t.duration().count()).c_str()); |
| 4694 | } |
| 4695 | } |
| 4696 | |
| 4697 | mLock.lock(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4698 | std::unique_ptr<KeyEntry> injectedEntry = |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4699 | std::make_unique<KeyEntry>(incomingKey.getId(), injectionState, |
| 4700 | incomingKey.getEventTime(), resolvedDeviceId, |
| 4701 | incomingKey.getSource(), incomingKey.getDisplayId(), |
| 4702 | policyFlags, action, flags, keyCode, |
| 4703 | incomingKey.getScanCode(), metaState, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4704 | incomingKey.getRepeatCount(), |
| 4705 | incomingKey.getDownTime()); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 4706 | if (mTracer) { |
| 4707 | injectedEntry->traceTracker = mTracer->traceInboundEvent(*injectedEntry); |
| 4708 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4709 | injectedEntries.push(std::move(injectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4710 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4711 | } |
| 4712 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4713 | case InputEventType::MOTION: { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4714 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4715 | const bool isPointerEvent = |
| 4716 | isFromSource(event->getSource(), AINPUT_SOURCE_CLASS_POINTER); |
| 4717 | // If a pointer event has no displayId specified, inject it to the default display. |
| 4718 | const uint32_t displayId = isPointerEvent && (event->getDisplayId() == ADISPLAY_ID_NONE) |
| 4719 | ? ADISPLAY_ID_DEFAULT |
| 4720 | : event->getDisplayId(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4721 | int32_t flags = motionEvent.getFlags(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4722 | |
| 4723 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4724 | nsecs_t eventTime = motionEvent.getEventTime(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4725 | android::base::Timer t; |
Yeabkal Wubshit | 88a9041 | 2023-12-21 18:23:04 -0800 | [diff] [blame] | 4726 | mPolicy.interceptMotionBeforeQueueing(displayId, motionEvent.getSource(), |
| 4727 | motionEvent.getAction(), eventTime, |
| 4728 | /*byref*/ policyFlags); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4729 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4730 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
| 4731 | std::to_string(t.duration().count()).c_str()); |
| 4732 | } |
| 4733 | } |
| 4734 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4735 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4736 | flags |= AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4737 | } |
| 4738 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4739 | mLock.lock(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4740 | const nsecs_t* sampleEventTimes = motionEvent.getSampleEventTimes(); |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4741 | const size_t pointerCount = motionEvent.getPointerCount(); |
| 4742 | const std::vector<PointerProperties> |
| 4743 | pointerProperties(motionEvent.getPointerProperties(), |
| 4744 | motionEvent.getPointerProperties() + pointerCount); |
| 4745 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4746 | const PointerCoords* samplePointerCoords = motionEvent.getSamplePointerCoords(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4747 | std::unique_ptr<MotionEntry> injectedEntry = |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4748 | std::make_unique<MotionEntry>(motionEvent.getId(), injectionState, |
| 4749 | *sampleEventTimes, resolvedDeviceId, |
| 4750 | motionEvent.getSource(), displayId, policyFlags, |
| 4751 | motionEvent.getAction(), |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4752 | motionEvent.getActionButton(), flags, |
| 4753 | motionEvent.getMetaState(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4754 | motionEvent.getButtonState(), |
| 4755 | motionEvent.getClassification(), |
| 4756 | motionEvent.getEdgeFlags(), |
| 4757 | motionEvent.getXPrecision(), |
| 4758 | motionEvent.getYPrecision(), |
| 4759 | motionEvent.getRawXCursorPosition(), |
| 4760 | motionEvent.getRawYCursorPosition(), |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4761 | motionEvent.getDownTime(), pointerProperties, |
| 4762 | std::vector<PointerCoords>(samplePointerCoords, |
| 4763 | samplePointerCoords + |
| 4764 | pointerCount)); |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4765 | transformMotionEntryForInjectionLocked(*injectedEntry, motionEvent.getTransform()); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 4766 | if (mTracer) { |
| 4767 | injectedEntry->traceTracker = mTracer->traceInboundEvent(*injectedEntry); |
| 4768 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4769 | injectedEntries.push(std::move(injectedEntry)); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4770 | for (size_t i = motionEvent.getHistorySize(); i > 0; i--) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4771 | sampleEventTimes += 1; |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4772 | samplePointerCoords += motionEvent.getPointerCount(); |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4773 | std::unique_ptr<MotionEntry> nextInjectedEntry = std::make_unique< |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4774 | MotionEntry>(motionEvent.getId(), injectionState, *sampleEventTimes, |
| 4775 | resolvedDeviceId, motionEvent.getSource(), displayId, |
| 4776 | policyFlags, motionEvent.getAction(), |
| 4777 | motionEvent.getActionButton(), flags, |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4778 | motionEvent.getMetaState(), motionEvent.getButtonState(), |
| 4779 | motionEvent.getClassification(), motionEvent.getEdgeFlags(), |
| 4780 | motionEvent.getXPrecision(), motionEvent.getYPrecision(), |
| 4781 | motionEvent.getRawXCursorPosition(), |
| 4782 | motionEvent.getRawYCursorPosition(), motionEvent.getDownTime(), |
| 4783 | pointerProperties, |
| 4784 | std::vector<PointerCoords>(samplePointerCoords, |
| 4785 | samplePointerCoords + |
| 4786 | pointerCount)); |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4787 | transformMotionEntryForInjectionLocked(*nextInjectedEntry, |
| 4788 | motionEvent.getTransform()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4789 | injectedEntries.push(std::move(nextInjectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4790 | } |
| 4791 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4792 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4793 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4794 | default: |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4795 | LOG(WARNING) << "Cannot inject " << ftl::enum_string(event->getType()) << " events"; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4796 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4797 | } |
| 4798 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4799 | bool needWake = false; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4800 | while (!injectedEntries.empty()) { |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 4801 | if (DEBUG_INJECTION) { |
Siarhei Vishniakou | 827d1ac | 2023-07-21 16:37:51 -0700 | [diff] [blame] | 4802 | LOG(INFO) << "Injecting " << injectedEntries.front()->getDescription(); |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 4803 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4804 | needWake |= enqueueInboundEventLocked(std::move(injectedEntries.front())); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4805 | injectedEntries.pop(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4806 | } |
| 4807 | |
| 4808 | mLock.unlock(); |
| 4809 | |
| 4810 | if (needWake) { |
| 4811 | mLooper->wake(); |
| 4812 | } |
| 4813 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4814 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4815 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4816 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4817 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4818 | if (syncMode == InputEventInjectionSync::NONE) { |
| 4819 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4820 | } else { |
| 4821 | for (;;) { |
| 4822 | injectionResult = injectionState->injectionResult; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4823 | if (injectionResult != InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4824 | break; |
| 4825 | } |
| 4826 | |
| 4827 | nsecs_t remainingTimeout = endTime - now(); |
| 4828 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4829 | if (DEBUG_INJECTION) { |
| 4830 | ALOGD("injectInputEvent - Timed out waiting for injection result " |
| 4831 | "to become available."); |
| 4832 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4833 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4834 | break; |
| 4835 | } |
| 4836 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4837 | mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4838 | } |
| 4839 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4840 | if (injectionResult == InputEventInjectionResult::SUCCEEDED && |
| 4841 | syncMode == InputEventInjectionSync::WAIT_FOR_FINISHED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4842 | while (injectionState->pendingForegroundDispatches != 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4843 | if (DEBUG_INJECTION) { |
| 4844 | ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", |
| 4845 | injectionState->pendingForegroundDispatches); |
| 4846 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4847 | nsecs_t remainingTimeout = endTime - now(); |
| 4848 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4849 | if (DEBUG_INJECTION) { |
| 4850 | ALOGD("injectInputEvent - Timed out waiting for pending foreground " |
| 4851 | "dispatches to finish."); |
| 4852 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4853 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4854 | break; |
| 4855 | } |
| 4856 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4857 | mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4858 | } |
| 4859 | } |
| 4860 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4861 | } // release lock |
| 4862 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4863 | if (DEBUG_INJECTION) { |
Siarhei Vishniakou | 827d1ac | 2023-07-21 16:37:51 -0700 | [diff] [blame] | 4864 | LOG(INFO) << "injectInputEvent - Finished with result " |
| 4865 | << ftl::enum_string(injectionResult); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4866 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4867 | |
| 4868 | return injectionResult; |
| 4869 | } |
| 4870 | |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4871 | std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4872 | std::array<uint8_t, 32> calculatedHmac; |
| 4873 | std::unique_ptr<VerifiedInputEvent> result; |
| 4874 | switch (event.getType()) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4875 | case InputEventType::KEY: { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4876 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event); |
| 4877 | VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent); |
| 4878 | result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4879 | calculatedHmac = sign(verifiedKeyEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4880 | break; |
| 4881 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4882 | case InputEventType::MOTION: { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4883 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event); |
| 4884 | VerifiedMotionEvent verifiedMotionEvent = |
| 4885 | verifiedMotionEventFromMotionEvent(motionEvent); |
| 4886 | result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4887 | calculatedHmac = sign(verifiedMotionEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4888 | break; |
| 4889 | } |
| 4890 | default: { |
| 4891 | ALOGE("Cannot verify events of type %" PRId32, event.getType()); |
| 4892 | return nullptr; |
| 4893 | } |
| 4894 | } |
| 4895 | if (calculatedHmac == INVALID_HMAC) { |
| 4896 | return nullptr; |
| 4897 | } |
tyiu | 1573a67 | 2023-02-21 22:38:32 +0000 | [diff] [blame] | 4898 | if (0 != CRYPTO_memcmp(calculatedHmac.data(), event.getHmac().data(), calculatedHmac.size())) { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4899 | return nullptr; |
| 4900 | } |
| 4901 | return result; |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4902 | } |
| 4903 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 4904 | void InputDispatcher::setInjectionResult(const EventEntry& entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4905 | InputEventInjectionResult injectionResult) { |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4906 | if (!entry.injectionState) { |
| 4907 | // Not an injected event. |
| 4908 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4909 | } |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4910 | |
| 4911 | InjectionState& injectionState = *entry.injectionState; |
| 4912 | if (DEBUG_INJECTION) { |
| 4913 | LOG(INFO) << "Setting input event injection result to " |
| 4914 | << ftl::enum_string(injectionResult); |
| 4915 | } |
| 4916 | |
| 4917 | if (injectionState.injectionIsAsync && !(entry.policyFlags & POLICY_FLAG_FILTERED)) { |
| 4918 | // Log the outcome since the injector did not wait for the injection result. |
| 4919 | switch (injectionResult) { |
| 4920 | case InputEventInjectionResult::SUCCEEDED: |
| 4921 | ALOGV("Asynchronous input event injection succeeded."); |
| 4922 | break; |
| 4923 | case InputEventInjectionResult::TARGET_MISMATCH: |
| 4924 | ALOGV("Asynchronous input event injection target mismatch."); |
| 4925 | break; |
| 4926 | case InputEventInjectionResult::FAILED: |
| 4927 | ALOGW("Asynchronous input event injection failed."); |
| 4928 | break; |
| 4929 | case InputEventInjectionResult::TIMED_OUT: |
| 4930 | ALOGW("Asynchronous input event injection timed out."); |
| 4931 | break; |
| 4932 | case InputEventInjectionResult::PENDING: |
| 4933 | ALOGE("Setting result to 'PENDING' for asynchronous injection"); |
| 4934 | break; |
| 4935 | } |
| 4936 | } |
| 4937 | |
| 4938 | injectionState.injectionResult = injectionResult; |
| 4939 | mInjectionResultAvailable.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4940 | } |
| 4941 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4942 | void InputDispatcher::transformMotionEntryForInjectionLocked( |
| 4943 | MotionEntry& entry, const ui::Transform& injectedTransform) const { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4944 | // Input injection works in the logical display coordinate space, but the input pipeline works |
| 4945 | // display space, so we need to transform the injected events accordingly. |
| 4946 | const auto it = mDisplayInfos.find(entry.displayId); |
| 4947 | if (it == mDisplayInfos.end()) return; |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4948 | const auto& transformToDisplay = it->second.transform.inverse() * injectedTransform; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4949 | |
Prabir Pradhan | d9a2ebe | 2022-07-20 19:25:13 +0000 | [diff] [blame] | 4950 | if (entry.xCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION && |
| 4951 | entry.yCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) { |
| 4952 | const vec2 cursor = |
| 4953 | MotionEvent::calculateTransformedXY(entry.source, transformToDisplay, |
| 4954 | {entry.xCursorPosition, entry.yCursorPosition}); |
| 4955 | entry.xCursorPosition = cursor.x; |
| 4956 | entry.yCursorPosition = cursor.y; |
| 4957 | } |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4958 | for (uint32_t i = 0; i < entry.getPointerCount(); i++) { |
Prabir Pradhan | 8e6ce22 | 2022-02-24 09:08:54 -0800 | [diff] [blame] | 4959 | entry.pointerCoords[i] = |
| 4960 | MotionEvent::calculateTransformedCoords(entry.source, transformToDisplay, |
| 4961 | entry.pointerCoords[i]); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4962 | } |
| 4963 | } |
| 4964 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 4965 | void InputDispatcher::incrementPendingForegroundDispatches(const EventEntry& entry) { |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4966 | if (entry.injectionState) { |
| 4967 | entry.injectionState->pendingForegroundDispatches += 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4968 | } |
| 4969 | } |
| 4970 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 4971 | void InputDispatcher::decrementPendingForegroundDispatches(const EventEntry& entry) { |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4972 | if (entry.injectionState) { |
| 4973 | entry.injectionState->pendingForegroundDispatches -= 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4974 | |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4975 | if (entry.injectionState->pendingForegroundDispatches == 0) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4976 | mInjectionSyncFinished.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4977 | } |
| 4978 | } |
| 4979 | } |
| 4980 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4981 | const std::vector<sp<WindowInfoHandle>>& InputDispatcher::getWindowHandlesLocked( |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4982 | int32_t displayId) const { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4983 | static const std::vector<sp<WindowInfoHandle>> EMPTY_WINDOW_HANDLES; |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4984 | auto it = mWindowHandlesByDisplay.find(displayId); |
| 4985 | return it != mWindowHandlesByDisplay.end() ? it->second : EMPTY_WINDOW_HANDLES; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4986 | } |
| 4987 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4988 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 4989 | const sp<IBinder>& windowHandleToken, std::optional<int32_t> displayId) const { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4990 | if (windowHandleToken == nullptr) { |
| 4991 | return nullptr; |
| 4992 | } |
| 4993 | |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 4994 | if (!displayId) { |
| 4995 | // Look through all displays. |
| 4996 | for (auto& it : mWindowHandlesByDisplay) { |
| 4997 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 4998 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 4999 | if (windowHandle->getToken() == windowHandleToken) { |
| 5000 | return windowHandle; |
| 5001 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5002 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5003 | } |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5004 | return nullptr; |
| 5005 | } |
| 5006 | |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 5007 | // Only look through the requested display. |
| 5008 | for (const sp<WindowInfoHandle>& windowHandle : getWindowHandlesLocked(*displayId)) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5009 | if (windowHandle->getToken() == windowHandleToken) { |
| 5010 | return windowHandle; |
| 5011 | } |
| 5012 | } |
| 5013 | return nullptr; |
| 5014 | } |
| 5015 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5016 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
| 5017 | const sp<WindowInfoHandle>& windowHandle) const { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 5018 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5019 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 5020 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 5021 | if (handle->getId() == windowHandle->getId() && |
| 5022 | handle->getToken() == windowHandle->getToken()) { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 5023 | if (windowHandle->getInfo()->displayId != it.first) { |
| 5024 | ALOGE("Found window %s in display %" PRId32 |
| 5025 | ", but it should belong to display %" PRId32, |
| 5026 | windowHandle->getName().c_str(), it.first, |
| 5027 | windowHandle->getInfo()->displayId); |
| 5028 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 5029 | return handle; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5030 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5031 | } |
| 5032 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 5033 | return nullptr; |
| 5034 | } |
| 5035 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5036 | sp<WindowInfoHandle> InputDispatcher::getFocusedWindowHandleLocked(int displayId) const { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 5037 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(displayId); |
| 5038 | return getWindowHandleLocked(focusedToken, displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5039 | } |
| 5040 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5041 | ui::Transform InputDispatcher::getTransformLocked(int32_t displayId) const { |
| 5042 | auto displayInfoIt = mDisplayInfos.find(displayId); |
| 5043 | return displayInfoIt != mDisplayInfos.end() ? displayInfoIt->second.transform |
| 5044 | : kIdentityTransform; |
| 5045 | } |
| 5046 | |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5047 | bool InputDispatcher::canWindowReceiveMotionLocked(const sp<WindowInfoHandle>& window, |
| 5048 | const MotionEntry& motionEntry) const { |
| 5049 | const WindowInfo& info = *window->getInfo(); |
| 5050 | |
| 5051 | // Skip spy window targets that are not valid for targeted injection. |
| 5052 | if (const auto err = verifyTargetedInjection(window, motionEntry); err) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5053 | return false; |
| 5054 | } |
| 5055 | |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5056 | if (info.inputConfig.test(WindowInfo::InputConfig::PAUSE_DISPATCHING)) { |
| 5057 | ALOGI("Not sending touch event to %s because it is paused", window->getName().c_str()); |
| 5058 | return false; |
| 5059 | } |
| 5060 | |
| 5061 | if (info.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) { |
| 5062 | ALOGW("Not sending touch gesture to %s because it has config NO_INPUT_CHANNEL", |
| 5063 | window->getName().c_str()); |
| 5064 | return false; |
| 5065 | } |
| 5066 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 5067 | std::shared_ptr<Connection> connection = getConnectionLocked(window->getToken()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5068 | if (connection == nullptr) { |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5069 | ALOGW("Not sending touch to %s because there's no corresponding connection", |
| 5070 | window->getName().c_str()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5071 | return false; |
| 5072 | } |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5073 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5074 | if (!connection->responsive) { |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5075 | 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] | 5076 | return false; |
| 5077 | } |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5078 | |
| 5079 | // Drop events that can't be trusted due to occlusion |
| 5080 | const auto [x, y] = resolveTouchedPosition(motionEntry); |
| 5081 | TouchOcclusionInfo occlusionInfo = computeTouchOcclusionInfoLocked(window, x, y); |
| 5082 | if (!isTouchTrustedLocked(occlusionInfo)) { |
| 5083 | if (DEBUG_TOUCH_OCCLUSION) { |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 5084 | ALOGD("Stack of obscuring windows during untrusted touch (%.1f, %.1f):", x, y); |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5085 | for (const auto& log : occlusionInfo.debugInfo) { |
| 5086 | ALOGD("%s", log.c_str()); |
| 5087 | } |
| 5088 | } |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 5089 | ALOGW("Dropping untrusted touch event due to %s/%s", occlusionInfo.obscuringPackage.c_str(), |
| 5090 | occlusionInfo.obscuringUid.toString().c_str()); |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5091 | return false; |
| 5092 | } |
| 5093 | |
| 5094 | // Drop touch events if requested by input feature |
| 5095 | if (shouldDropInput(motionEntry, window)) { |
| 5096 | return false; |
| 5097 | } |
| 5098 | |
Siarhei Vishniakou | f77f60a | 2023-10-23 17:26:05 -0700 | [diff] [blame] | 5099 | // Ignore touches if stylus is down anywhere on screen |
| 5100 | if (info.inputConfig.test(WindowInfo::InputConfig::GLOBAL_STYLUS_BLOCKS_TOUCH) && |
| 5101 | isStylusActiveInDisplay(info.displayId, mTouchStatesByDisplay)) { |
| 5102 | LOG(INFO) << "Dropping touch from " << window->getName() << " because stylus is active"; |
| 5103 | return false; |
| 5104 | } |
| 5105 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5106 | return true; |
| 5107 | } |
| 5108 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5109 | void InputDispatcher::updateWindowHandlesForDisplayLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5110 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
| 5111 | if (windowInfoHandles.empty()) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5112 | // Remove all handles on a display if there are no windows left. |
| 5113 | mWindowHandlesByDisplay.erase(displayId); |
| 5114 | return; |
| 5115 | } |
| 5116 | |
| 5117 | // Since we compare the pointer of input window handles across window updates, we need |
| 5118 | // 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] | 5119 | const std::vector<sp<WindowInfoHandle>>& oldHandles = getWindowHandlesLocked(displayId); |
| 5120 | std::unordered_map<int32_t /*id*/, sp<WindowInfoHandle>> oldHandlesById; |
| 5121 | for (const sp<WindowInfoHandle>& handle : oldHandles) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5122 | oldHandlesById[handle->getId()] = handle; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5123 | } |
| 5124 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5125 | std::vector<sp<WindowInfoHandle>> newHandles; |
| 5126 | for (const sp<WindowInfoHandle>& handle : windowInfoHandles) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5127 | const WindowInfo* info = handle->getInfo(); |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 5128 | if (getConnectionLocked(handle->getToken()) == nullptr) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5129 | const bool noInputChannel = |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 5130 | info->inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5131 | const bool canReceiveInput = |
| 5132 | !info->inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE) || |
| 5133 | !info->inputConfig.test(WindowInfo::InputConfig::NOT_FOCUSABLE); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5134 | if (canReceiveInput && !noInputChannel) { |
John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 5135 | ALOGV("Window handle %s has no registered input channel", |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5136 | handle->getName().c_str()); |
Robert Carr | 2984b7a | 2020-04-13 17:06:45 -0700 | [diff] [blame] | 5137 | continue; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5138 | } |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5139 | } |
| 5140 | |
| 5141 | if (info->displayId != displayId) { |
| 5142 | ALOGE("Window %s updated by wrong display %d, should belong to display %d", |
| 5143 | handle->getName().c_str(), displayId, info->displayId); |
| 5144 | continue; |
| 5145 | } |
| 5146 | |
Robert Carr | edd1360 | 2020-04-13 17:24:34 -0700 | [diff] [blame] | 5147 | if ((oldHandlesById.find(handle->getId()) != oldHandlesById.end()) && |
| 5148 | (oldHandlesById.at(handle->getId())->getToken() == handle->getToken())) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5149 | const sp<WindowInfoHandle>& oldHandle = oldHandlesById.at(handle->getId()); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5150 | oldHandle->updateFrom(handle); |
| 5151 | newHandles.push_back(oldHandle); |
| 5152 | } else { |
| 5153 | newHandles.push_back(handle); |
| 5154 | } |
| 5155 | } |
| 5156 | |
| 5157 | // Insert or replace |
| 5158 | mWindowHandlesByDisplay[displayId] = newHandles; |
| 5159 | } |
| 5160 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5161 | /** |
| 5162 | * Called from InputManagerService, update window handle list by displayId that can receive input. |
| 5163 | * A window handle contains information about InputChannel, Touch Region, Types, Focused,... |
| 5164 | * If set an empty list, remove all handles from the specific display. |
| 5165 | * For focused handle, check if need to change and send a cancel event to previous one. |
| 5166 | * For removed handle, check if need to send a cancel event if already in touch. |
| 5167 | */ |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5168 | void InputDispatcher::setInputWindowsLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5169 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5170 | if (DEBUG_FOCUS) { |
| 5171 | std::string windowList; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5172 | for (const sp<WindowInfoHandle>& iwh : windowInfoHandles) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5173 | windowList += iwh->getName() + " "; |
| 5174 | } |
Siarhei Vishniakou | 366fb5b | 2023-12-06 11:23:41 -0800 | [diff] [blame] | 5175 | LOG(INFO) << "setInputWindows displayId=" << displayId << " " << windowList; |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5176 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5177 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 5178 | // Check preconditions for new input windows |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5179 | for (const sp<WindowInfoHandle>& window : windowInfoHandles) { |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 5180 | const WindowInfo& info = *window->getInfo(); |
| 5181 | |
| 5182 | // 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] | 5183 | const bool noInputWindow = info.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5184 | if (noInputWindow && window->getToken() != nullptr) { |
| 5185 | ALOGE("%s has feature NO_INPUT_WINDOW, but a non-null token. Clearing", |
| 5186 | window->getName().c_str()); |
| 5187 | window->releaseChannel(); |
| 5188 | } |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 5189 | |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 5190 | // Ensure all spy windows are trusted overlays |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5191 | LOG_ALWAYS_FATAL_IF(info.isSpy() && |
| 5192 | !info.inputConfig.test( |
| 5193 | WindowInfo::InputConfig::TRUSTED_OVERLAY), |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 5194 | "%s has feature SPY, but is not a trusted overlay.", |
| 5195 | window->getName().c_str()); |
| 5196 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 5197 | // Ensure all stylus interceptors are trusted overlays |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5198 | LOG_ALWAYS_FATAL_IF(info.interceptsStylus() && |
| 5199 | !info.inputConfig.test( |
| 5200 | WindowInfo::InputConfig::TRUSTED_OVERLAY), |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 5201 | "%s has feature INTERCEPTS_STYLUS, but is not a trusted overlay.", |
| 5202 | window->getName().c_str()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5203 | } |
| 5204 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5205 | // Copy old handles for release if they are no longer present. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5206 | const std::vector<sp<WindowInfoHandle>> oldWindowHandles = getWindowHandlesLocked(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5207 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5208 | updateWindowHandlesForDisplayLocked(windowInfoHandles, displayId); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5209 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5210 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5211 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5212 | std::optional<FocusResolver::FocusChanges> changes = |
| 5213 | mFocusResolver.setInputWindows(displayId, windowHandles); |
| 5214 | if (changes) { |
| 5215 | onFocusChangedLocked(*changes); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5216 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5217 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5218 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 5219 | mTouchStatesByDisplay.find(displayId); |
| 5220 | if (stateIt != mTouchStatesByDisplay.end()) { |
| 5221 | TouchState& state = stateIt->second; |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5222 | for (size_t i = 0; i < state.windows.size();) { |
| 5223 | TouchedWindow& touchedWindow = state.windows[i]; |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 5224 | if (getWindowHandleLocked(touchedWindow.windowHandle) == nullptr) { |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 5225 | LOG(INFO) << "Touched window was removed: " << touchedWindow.windowHandle->getName() |
| 5226 | << " in display %" << displayId; |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 5227 | std::shared_ptr<Connection> touchedConnection = |
| 5228 | getConnectionLocked(touchedWindow.windowHandle->getToken()); |
| 5229 | if (touchedConnection != nullptr) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 5230 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5231 | "touched window was removed"); |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 5232 | synthesizeCancelationEventsForConnectionLocked(touchedConnection, options); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5233 | // Since we are about to drop the touch, cancel the events for the wallpaper as |
| 5234 | // well. |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5235 | if (touchedWindow.targetFlags.test(InputTarget::Flags::FOREGROUND) && |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5236 | touchedWindow.windowHandle->getInfo()->inputConfig.test( |
| 5237 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER)) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5238 | sp<WindowInfoHandle> wallpaper = state.getWallpaperWindow(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5239 | synthesizeCancelationEventsForWindowLocked(wallpaper, options); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5240 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5241 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5242 | state.windows.erase(state.windows.begin() + i); |
| 5243 | } else { |
| 5244 | ++i; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5245 | } |
| 5246 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5247 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5248 | // 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] | 5249 | // could just clear the state here. |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 5250 | if (mDragState && mDragState->dragWindow->getInfo()->displayId == displayId && |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5251 | std::find(windowHandles.begin(), windowHandles.end(), mDragState->dragWindow) == |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5252 | windowHandles.end()) { |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 5253 | ALOGI("Drag window went away: %s", mDragState->dragWindow->getName().c_str()); |
| 5254 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5255 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5256 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5257 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 5258 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5259 | // Release information for windows that are no longer present. |
| 5260 | // This ensures that unused input channels are released promptly. |
| 5261 | // Otherwise, they might stick around until the window handle is destroyed |
| 5262 | // which might not happen until the next GC. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5263 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 5264 | if (getWindowHandleLocked(oldWindowHandle) == nullptr) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5265 | if (DEBUG_FOCUS) { |
| 5266 | ALOGD("Window went away: %s", oldWindowHandle->getName().c_str()); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 5267 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5268 | oldWindowHandle->releaseChannel(); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 5269 | } |
chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 5270 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5271 | } |
| 5272 | |
| 5273 | void InputDispatcher::setFocusedApplication( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5274 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5275 | if (DEBUG_FOCUS) { |
| 5276 | ALOGD("setFocusedApplication displayId=%" PRId32 " %s", displayId, |
| 5277 | inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>"); |
| 5278 | } |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 5279 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5280 | std::scoped_lock _l(mLock); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 5281 | setFocusedApplicationLocked(displayId, inputApplicationHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5282 | } // release lock |
| 5283 | |
| 5284 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5285 | mLooper->wake(); |
| 5286 | } |
| 5287 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 5288 | void InputDispatcher::setFocusedApplicationLocked( |
| 5289 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
| 5290 | std::shared_ptr<InputApplicationHandle> oldFocusedApplicationHandle = |
| 5291 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 5292 | |
| 5293 | if (sharedPointersEqual(oldFocusedApplicationHandle, inputApplicationHandle)) { |
| 5294 | return; // This application is already focused. No need to wake up or change anything. |
| 5295 | } |
| 5296 | |
| 5297 | // Set the new application handle. |
| 5298 | if (inputApplicationHandle != nullptr) { |
| 5299 | mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle; |
| 5300 | } else { |
| 5301 | mFocusedApplicationHandlesByDisplay.erase(displayId); |
| 5302 | } |
| 5303 | |
| 5304 | // No matter what the old focused application was, stop waiting on it because it is |
| 5305 | // no longer focused. |
| 5306 | resetNoFocusedWindowTimeoutLocked(); |
| 5307 | } |
| 5308 | |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 5309 | void InputDispatcher::setMinTimeBetweenUserActivityPokes(std::chrono::milliseconds interval) { |
| 5310 | if (interval.count() < 0) { |
| 5311 | LOG_ALWAYS_FATAL("Minimum time between user activity pokes should be >= 0"); |
| 5312 | } |
| 5313 | std::scoped_lock _l(mLock); |
| 5314 | mMinTimeBetweenUserActivityPokes = interval; |
| 5315 | } |
| 5316 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5317 | /** |
| 5318 | * Sets the focused display, which is responsible for receiving focus-dispatched input events where |
| 5319 | * the display not specified. |
| 5320 | * |
| 5321 | * We track any unreleased events for each window. If a window loses the ability to receive the |
| 5322 | * released event, we will send a cancel event to it. So when the focused display is changed, we |
| 5323 | * cancel all the unreleased display-unspecified events for the focused window on the old focused |
| 5324 | * display. The display-specified events won't be affected. |
| 5325 | */ |
| 5326 | void InputDispatcher::setFocusedDisplay(int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5327 | if (DEBUG_FOCUS) { |
| 5328 | ALOGD("setFocusedDisplay displayId=%" PRId32, displayId); |
| 5329 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5330 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5331 | std::scoped_lock _l(mLock); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5332 | |
| 5333 | if (mFocusedDisplayId != displayId) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5334 | sp<IBinder> oldFocusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5335 | mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5336 | if (oldFocusedWindowToken != nullptr) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 5337 | std::shared_ptr<Connection> connection = getConnectionLocked(oldFocusedWindowToken); |
| 5338 | if (connection != nullptr) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5339 | CancelationOptions |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 5340 | options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5341 | "The display which contains this window no longer has focus."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5342 | options.displayId = ADISPLAY_ID_NONE; |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 5343 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5344 | } |
| 5345 | } |
| 5346 | mFocusedDisplayId = displayId; |
| 5347 | |
Chris Ye | 3c2d6f5 | 2020-08-09 10:39:48 -0700 | [diff] [blame] | 5348 | // Find new focused window and validate |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5349 | sp<IBinder> newFocusedWindowToken = mFocusResolver.getFocusedWindowToken(displayId); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5350 | sendFocusChangedCommandLocked(oldFocusedWindowToken, newFocusedWindowToken); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5351 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5352 | if (newFocusedWindowToken == nullptr) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5353 | ALOGW("Focused display #%" PRId32 " does not have a focused window.", displayId); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5354 | if (mFocusResolver.hasFocusedWindowTokens()) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5355 | ALOGE("But another display has a focused window\n%s", |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5356 | mFocusResolver.dumpFocusedWindows().c_str()); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5357 | } |
| 5358 | } |
| 5359 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5360 | } // release lock |
| 5361 | |
| 5362 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5363 | mLooper->wake(); |
| 5364 | } |
| 5365 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5366 | void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5367 | if (DEBUG_FOCUS) { |
| 5368 | ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); |
| 5369 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5370 | |
| 5371 | bool changed; |
| 5372 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5373 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5374 | |
| 5375 | if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { |
| 5376 | if (mDispatchFrozen && !frozen) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5377 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5378 | } |
| 5379 | |
| 5380 | if (mDispatchEnabled && !enabled) { |
| 5381 | resetAndDropEverythingLocked("dispatcher is being disabled"); |
| 5382 | } |
| 5383 | |
| 5384 | mDispatchEnabled = enabled; |
| 5385 | mDispatchFrozen = frozen; |
| 5386 | changed = true; |
| 5387 | } else { |
| 5388 | changed = false; |
| 5389 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5390 | } // release lock |
| 5391 | |
| 5392 | if (changed) { |
| 5393 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5394 | mLooper->wake(); |
| 5395 | } |
| 5396 | } |
| 5397 | |
| 5398 | void InputDispatcher::setInputFilterEnabled(bool enabled) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5399 | if (DEBUG_FOCUS) { |
| 5400 | ALOGD("setInputFilterEnabled: enabled=%d", enabled); |
| 5401 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5402 | |
| 5403 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5404 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5405 | |
| 5406 | if (mInputFilterEnabled == enabled) { |
| 5407 | return; |
| 5408 | } |
| 5409 | |
| 5410 | mInputFilterEnabled = enabled; |
| 5411 | resetAndDropEverythingLocked("input filter is being enabled or disabled"); |
| 5412 | } // release lock |
| 5413 | |
| 5414 | // Wake up poll loop since there might be work to do to drop everything. |
| 5415 | mLooper->wake(); |
| 5416 | } |
| 5417 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5418 | bool InputDispatcher::setInTouchMode(bool inTouchMode, gui::Pid pid, gui::Uid uid, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 5419 | bool hasPermission, int32_t displayId) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5420 | bool needWake = false; |
| 5421 | { |
| 5422 | std::scoped_lock lock(mLock); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5423 | ALOGD_IF(DEBUG_TOUCH_MODE, |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5424 | "Request to change touch mode to %s (calling pid=%s, uid=%s, " |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5425 | "hasPermission=%s, target displayId=%d, mTouchModePerDisplay[displayId]=%s)", |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5426 | toString(inTouchMode), pid.toString().c_str(), uid.toString().c_str(), |
| 5427 | toString(hasPermission), displayId, |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5428 | mTouchModePerDisplay.count(displayId) == 0 |
| 5429 | ? "not set" |
| 5430 | : std::to_string(mTouchModePerDisplay[displayId]).c_str()); |
| 5431 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5432 | auto touchModeIt = mTouchModePerDisplay.find(displayId); |
| 5433 | if (touchModeIt != mTouchModePerDisplay.end() && touchModeIt->second == inTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5434 | return false; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5435 | } |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5436 | if (!hasPermission) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 5437 | if (!focusedWindowIsOwnedByLocked(pid, uid) && |
| 5438 | !recentWindowsAreOwnedByLocked(pid, uid)) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5439 | 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] | 5440 | "window nor none of the previously interacted window", |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5441 | pid.toString().c_str(), uid.toString().c_str()); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5442 | return false; |
| 5443 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5444 | } |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5445 | mTouchModePerDisplay[displayId] = inTouchMode; |
| 5446 | auto entry = std::make_unique<TouchModeEntry>(mIdGenerator.nextId(), now(), inTouchMode, |
| 5447 | displayId); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5448 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 5449 | } // release lock |
| 5450 | |
| 5451 | if (needWake) { |
| 5452 | mLooper->wake(); |
| 5453 | } |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5454 | return true; |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 5455 | } |
| 5456 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5457 | bool InputDispatcher::focusedWindowIsOwnedByLocked(gui::Pid pid, gui::Uid uid) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 5458 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
| 5459 | if (focusedToken == nullptr) { |
| 5460 | return false; |
| 5461 | } |
| 5462 | sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(focusedToken); |
| 5463 | return isWindowOwnedBy(windowHandle, pid, uid); |
| 5464 | } |
| 5465 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5466 | bool InputDispatcher::recentWindowsAreOwnedByLocked(gui::Pid pid, gui::Uid uid) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 5467 | return std::find_if(mInteractionConnectionTokens.begin(), mInteractionConnectionTokens.end(), |
| 5468 | [&](const sp<IBinder>& connectionToken) REQUIRES(mLock) { |
| 5469 | const sp<WindowInfoHandle> windowHandle = |
| 5470 | getWindowHandleLocked(connectionToken); |
| 5471 | return isWindowOwnedBy(windowHandle, pid, uid); |
| 5472 | }) != mInteractionConnectionTokens.end(); |
| 5473 | } |
| 5474 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 5475 | void InputDispatcher::setMaximumObscuringOpacityForTouch(float opacity) { |
| 5476 | if (opacity < 0 || opacity > 1) { |
| 5477 | LOG_ALWAYS_FATAL("Maximum obscuring opacity for touch should be >= 0 and <= 1"); |
| 5478 | return; |
| 5479 | } |
| 5480 | |
| 5481 | std::scoped_lock lock(mLock); |
| 5482 | mMaximumObscuringOpacityForTouch = opacity; |
| 5483 | } |
| 5484 | |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5485 | std::tuple<TouchState*, TouchedWindow*, int32_t /*displayId*/> |
| 5486 | InputDispatcher::findTouchStateWindowAndDisplayLocked(const sp<IBinder>& token) { |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5487 | for (auto& [displayId, state] : mTouchStatesByDisplay) { |
| 5488 | for (TouchedWindow& w : state.windows) { |
| 5489 | if (w.windowHandle->getToken() == token) { |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5490 | return std::make_tuple(&state, &w, displayId); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5491 | } |
| 5492 | } |
| 5493 | } |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5494 | return std::make_tuple(nullptr, nullptr, ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5495 | } |
| 5496 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5497 | bool InputDispatcher::transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken, |
| 5498 | bool isDragDrop) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 5499 | if (fromToken == toToken) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5500 | if (DEBUG_FOCUS) { |
| 5501 | ALOGD("Trivial transfer to same window."); |
| 5502 | } |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 5503 | return true; |
| 5504 | } |
| 5505 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5506 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5507 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5508 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5509 | // Find the target touch state and touched window by fromToken. |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5510 | auto [state, touchedWindow, displayId] = findTouchStateWindowAndDisplayLocked(fromToken); |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5511 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5512 | if (state == nullptr || touchedWindow == nullptr) { |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5513 | ALOGD("Touch transfer failed because from window is not being touched."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5514 | return false; |
| 5515 | } |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5516 | std::set<int32_t> deviceIds = touchedWindow->getTouchingDeviceIds(); |
| 5517 | if (deviceIds.size() != 1) { |
Siarhei Vishniakou | 827d1ac | 2023-07-21 16:37:51 -0700 | [diff] [blame] | 5518 | LOG(INFO) << "Can't transfer touch. Currently touching devices: " << dumpSet(deviceIds) |
| 5519 | << " for window: " << touchedWindow->dump(); |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5520 | return false; |
| 5521 | } |
| 5522 | const int32_t deviceId = *deviceIds.begin(); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5523 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5524 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(toToken, displayId); |
| 5525 | if (toWindowHandle == nullptr) { |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5526 | ALOGW("Cannot transfer touch because to window not found."); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5527 | return false; |
| 5528 | } |
| 5529 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5530 | if (DEBUG_FOCUS) { |
| 5531 | ALOGD("transferTouchFocus: fromWindowHandle=%s, toWindowHandle=%s", |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5532 | touchedWindow->windowHandle->getName().c_str(), |
| 5533 | toWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5534 | } |
| 5535 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5536 | // Erase old window. |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5537 | ftl::Flags<InputTarget::Flags> oldTargetFlags = touchedWindow->targetFlags; |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 5538 | std::vector<PointerProperties> pointers = touchedWindow->getTouchingPointers(deviceId); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5539 | sp<WindowInfoHandle> fromWindowHandle = touchedWindow->windowHandle; |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5540 | state->removeWindowByToken(fromToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5541 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5542 | // Add new window. |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 5543 | nsecs_t downTimeInTarget = now(); |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5544 | ftl::Flags<InputTarget::Flags> newTargetFlags = |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 5545 | oldTargetFlags & (InputTarget::Flags::SPLIT); |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 5546 | if (canReceiveForegroundTouches(*toWindowHandle->getInfo())) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5547 | newTargetFlags |= InputTarget::Flags::FOREGROUND; |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 5548 | } |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 5549 | state->addOrUpdateWindow(toWindowHandle, InputTarget::DispatchMode::AS_IS, newTargetFlags, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 5550 | deviceId, pointers, downTimeInTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5551 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5552 | // Store the dragging window. |
| 5553 | if (isDragDrop) { |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 5554 | if (pointers.size() != 1) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 5555 | ALOGW("The drag and drop cannot be started when there is no pointer or more than 1" |
| 5556 | " pointer on the window."); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 5557 | return false; |
| 5558 | } |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 5559 | // Track the pointer id for drag window and generate the drag state. |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 5560 | const size_t id = pointers.begin()->id; |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 5561 | mDragState = std::make_unique<DragState>(toWindowHandle, id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5562 | } |
| 5563 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5564 | // Synthesize cancel for old window and down for new window. |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 5565 | std::shared_ptr<Connection> fromConnection = getConnectionLocked(fromToken); |
| 5566 | std::shared_ptr<Connection> toConnection = getConnectionLocked(toToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5567 | if (fromConnection != nullptr && toConnection != nullptr) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5568 | fromConnection->inputState.mergePointerStateTo(toConnection->inputState); |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5569 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
| 5570 | "transferring touch from this window to another window"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5571 | synthesizeCancelationEventsForConnectionLocked(fromConnection, options); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5572 | synthesizePointerDownEventsForConnectionLocked(downTimeInTarget, toConnection, |
| 5573 | newTargetFlags); |
| 5574 | |
| 5575 | // Check if the wallpaper window should deliver the corresponding event. |
| 5576 | transferWallpaperTouch(oldTargetFlags, newTargetFlags, fromWindowHandle, toWindowHandle, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 5577 | *state, deviceId, pointers); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5578 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5579 | } // release lock |
| 5580 | |
| 5581 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5582 | mLooper->wake(); |
| 5583 | return true; |
| 5584 | } |
| 5585 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5586 | /** |
| 5587 | * Get the touched foreground window on the given display. |
| 5588 | * Return null if there are no windows touched on that display, or if more than one foreground |
| 5589 | * window is being touched. |
| 5590 | */ |
| 5591 | sp<WindowInfoHandle> InputDispatcher::findTouchedForegroundWindowLocked(int32_t displayId) const { |
| 5592 | auto stateIt = mTouchStatesByDisplay.find(displayId); |
| 5593 | if (stateIt == mTouchStatesByDisplay.end()) { |
| 5594 | ALOGI("No touch state on display %" PRId32, displayId); |
| 5595 | return nullptr; |
| 5596 | } |
| 5597 | |
| 5598 | const TouchState& state = stateIt->second; |
| 5599 | sp<WindowInfoHandle> touchedForegroundWindow; |
| 5600 | // If multiple foreground windows are touched, return nullptr |
| 5601 | for (const TouchedWindow& window : state.windows) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5602 | if (window.targetFlags.test(InputTarget::Flags::FOREGROUND)) { |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5603 | if (touchedForegroundWindow != nullptr) { |
| 5604 | ALOGI("Two or more foreground windows: %s and %s", |
| 5605 | touchedForegroundWindow->getName().c_str(), |
| 5606 | window.windowHandle->getName().c_str()); |
| 5607 | return nullptr; |
| 5608 | } |
| 5609 | touchedForegroundWindow = window.windowHandle; |
| 5610 | } |
| 5611 | } |
| 5612 | return touchedForegroundWindow; |
| 5613 | } |
| 5614 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5615 | // Binder call |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5616 | bool InputDispatcher::transferTouch(const sp<IBinder>& destChannelToken, int32_t displayId) { |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5617 | sp<IBinder> fromToken; |
| 5618 | { // acquire lock |
| 5619 | std::scoped_lock _l(mLock); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5620 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(destChannelToken, displayId); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5621 | if (toWindowHandle == nullptr) { |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5622 | ALOGW("Could not find window associated with token=%p on display %" PRId32, |
| 5623 | destChannelToken.get(), displayId); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5624 | return false; |
| 5625 | } |
| 5626 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5627 | sp<WindowInfoHandle> from = findTouchedForegroundWindowLocked(displayId); |
| 5628 | if (from == nullptr) { |
| 5629 | ALOGE("Could not find a source window in %s for %p", __func__, destChannelToken.get()); |
| 5630 | return false; |
| 5631 | } |
| 5632 | |
| 5633 | fromToken = from->getToken(); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5634 | } // release lock |
| 5635 | |
| 5636 | return transferTouchFocus(fromToken, destChannelToken); |
| 5637 | } |
| 5638 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5639 | void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5640 | if (DEBUG_FOCUS) { |
| 5641 | ALOGD("Resetting and dropping all events (%s).", reason); |
| 5642 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5643 | |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 5644 | CancelationOptions options(CancelationOptions::Mode::CANCEL_ALL_EVENTS, reason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5645 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 5646 | |
| 5647 | resetKeyRepeatLocked(); |
| 5648 | releasePendingEventLocked(); |
| 5649 | drainInboundQueueLocked(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5650 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5651 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5652 | mAnrTracker.clear(); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5653 | mTouchStatesByDisplay.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5654 | } |
| 5655 | |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 5656 | void InputDispatcher::logDispatchStateLocked() const { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5657 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5658 | dumpDispatchStateLocked(dump); |
| 5659 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5660 | std::istringstream stream(dump); |
| 5661 | std::string line; |
| 5662 | |
| 5663 | while (std::getline(stream, line, '\n')) { |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 5664 | ALOGI("%s", line.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5665 | } |
| 5666 | } |
| 5667 | |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 5668 | std::string InputDispatcher::dumpPointerCaptureStateLocked() const { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5669 | std::string dump; |
| 5670 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5671 | dump += StringPrintf(INDENT "Pointer Capture Requested: %s\n", |
| 5672 | toString(mCurrentPointerCaptureRequest.enable)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5673 | |
| 5674 | std::string windowName = "None"; |
| 5675 | if (mWindowTokenWithPointerCapture) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5676 | const sp<WindowInfoHandle> captureWindowHandle = |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5677 | getWindowHandleLocked(mWindowTokenWithPointerCapture); |
| 5678 | windowName = captureWindowHandle ? captureWindowHandle->getName().c_str() |
| 5679 | : "token has capture without window"; |
| 5680 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5681 | 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] | 5682 | |
| 5683 | return dump; |
| 5684 | } |
| 5685 | |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 5686 | void InputDispatcher::dumpDispatchStateLocked(std::string& dump) const { |
Siarhei Vishniakou | 043a3ec | 2019-05-01 11:30:46 -0700 | [diff] [blame] | 5687 | dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled)); |
| 5688 | dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen)); |
| 5689 | dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5690 | dump += StringPrintf(INDENT "FocusedDisplayId: %" PRId32 "\n", mFocusedDisplayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5691 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5692 | if (!mFocusedApplicationHandlesByDisplay.empty()) { |
| 5693 | dump += StringPrintf(INDENT "FocusedApplications:\n"); |
| 5694 | for (auto& it : mFocusedApplicationHandlesByDisplay) { |
| 5695 | const int32_t displayId = it.first; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5696 | const std::shared_ptr<InputApplicationHandle>& applicationHandle = it.second; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5697 | const std::chrono::duration timeout = |
| 5698 | applicationHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5699 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5700 | ", name='%s', dispatchingTimeout=%" PRId64 "ms\n", |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5701 | displayId, applicationHandle->getName().c_str(), millis(timeout)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5702 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5703 | } else { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5704 | dump += StringPrintf(INDENT "FocusedApplications: <none>\n"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5705 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5706 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5707 | dump += mFocusResolver.dump(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5708 | dump += dumpPointerCaptureStateLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5709 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5710 | if (!mTouchStatesByDisplay.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5711 | dump += StringPrintf(INDENT "TouchStatesByDisplay:\n"); |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5712 | for (const auto& [displayId, state] : mTouchStatesByDisplay) { |
Siarhei Vishniakou | 6e1e987 | 2022-11-08 17:51:35 -0800 | [diff] [blame] | 5713 | std::string touchStateDump = addLinePrefix(state.dump(), INDENT2); |
| 5714 | dump += INDENT2 + std::to_string(displayId) + " : " + touchStateDump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5715 | } |
| 5716 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5717 | dump += INDENT "TouchStates: <no displays touched>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5718 | } |
| 5719 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5720 | if (mDragState) { |
| 5721 | dump += StringPrintf(INDENT "DragState:\n"); |
| 5722 | mDragState->dump(dump, INDENT2); |
| 5723 | } |
| 5724 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5725 | if (!mWindowHandlesByDisplay.empty()) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5726 | for (const auto& [displayId, windowHandles] : mWindowHandlesByDisplay) { |
| 5727 | dump += StringPrintf(INDENT "Display: %" PRId32 "\n", displayId); |
| 5728 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
| 5729 | const auto& displayInfo = it->second; |
| 5730 | dump += StringPrintf(INDENT2 "logicalSize=%dx%d\n", displayInfo.logicalWidth, |
| 5731 | displayInfo.logicalHeight); |
| 5732 | displayInfo.transform.dump(dump, "transform", INDENT4); |
| 5733 | } else { |
| 5734 | dump += INDENT2 "No DisplayInfo found!\n"; |
| 5735 | } |
| 5736 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5737 | if (!windowHandles.empty()) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5738 | dump += INDENT2 "Windows:\n"; |
| 5739 | for (size_t i = 0; i < windowHandles.size(); i++) { |
Siarhei Vishniakou | 366fb5b | 2023-12-06 11:23:41 -0800 | [diff] [blame] | 5740 | dump += StringPrintf(INDENT3 "%zu: %s", i, |
| 5741 | streamableToString(*windowHandles[i]).c_str()); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5742 | } |
| 5743 | } else { |
| 5744 | dump += INDENT2 "Windows: <none>\n"; |
| 5745 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5746 | } |
| 5747 | } else { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5748 | dump += INDENT "Displays: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5749 | } |
| 5750 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5751 | if (!mGlobalMonitorsByDisplay.empty()) { |
| 5752 | for (const auto& [displayId, monitors] : mGlobalMonitorsByDisplay) { |
| 5753 | dump += StringPrintf(INDENT "Global monitors on display %d:\n", displayId); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5754 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5755 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5756 | } else { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5757 | dump += INDENT "Global Monitors: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5758 | } |
| 5759 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5760 | const nsecs_t currentTime = now(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5761 | |
| 5762 | // Dump recently dispatched or dropped events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5763 | if (!mRecentQueue.empty()) { |
| 5764 | dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size()); |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 5765 | for (const std::shared_ptr<const EventEntry>& entry : mRecentQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5766 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5767 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5768 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5769 | } |
| 5770 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5771 | dump += INDENT "RecentQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5772 | } |
| 5773 | |
| 5774 | // Dump event currently being dispatched. |
| 5775 | if (mPendingEvent) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5776 | dump += INDENT "PendingEvent:\n"; |
| 5777 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5778 | dump += mPendingEvent->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5779 | dump += StringPrintf(", age=%" PRId64 "ms\n", |
| 5780 | ns2ms(currentTime - mPendingEvent->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5781 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5782 | dump += INDENT "PendingEvent: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5783 | } |
| 5784 | |
| 5785 | // Dump inbound events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5786 | if (!mInboundQueue.empty()) { |
| 5787 | dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size()); |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 5788 | for (const std::shared_ptr<const EventEntry>& entry : mInboundQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5789 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5790 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5791 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5792 | } |
| 5793 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5794 | dump += INDENT "InboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5795 | } |
| 5796 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5797 | if (!mCommandQueue.empty()) { |
| 5798 | dump += StringPrintf(INDENT "CommandQueue: size=%zu\n", mCommandQueue.size()); |
| 5799 | } else { |
| 5800 | dump += INDENT "CommandQueue: <empty>\n"; |
| 5801 | } |
| 5802 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5803 | if (!mConnectionsByToken.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5804 | dump += INDENT "Connections:\n"; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5805 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 10bfdc9 | 2024-02-01 11:12:57 -0800 | [diff] [blame^] | 5806 | dump += StringPrintf(INDENT2 "%i: channelName='%s', " |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5807 | "status=%s, monitor=%s, responsive=%s\n", |
Siarhei Vishniakou | 10bfdc9 | 2024-02-01 11:12:57 -0800 | [diff] [blame^] | 5808 | connection->inputPublisher.getChannel()->getFd(), |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5809 | connection->getInputChannelName().c_str(), |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5810 | ftl::enum_string(connection->status).c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5811 | toString(connection->monitor), toString(connection->responsive)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5812 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5813 | if (!connection->outboundQueue.empty()) { |
| 5814 | dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n", |
| 5815 | connection->outboundQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5816 | dump += dumpQueue(connection->outboundQueue, currentTime); |
| 5817 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5818 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5819 | dump += INDENT3 "OutboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5820 | } |
| 5821 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5822 | if (!connection->waitQueue.empty()) { |
| 5823 | dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n", |
| 5824 | connection->waitQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5825 | dump += dumpQueue(connection->waitQueue, currentTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5826 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5827 | dump += INDENT3 "WaitQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5828 | } |
Siarhei Vishniakou | 366fb5b | 2023-12-06 11:23:41 -0800 | [diff] [blame] | 5829 | std::string inputStateDump = streamableToString(connection->inputState); |
| 5830 | if (!inputStateDump.empty()) { |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 5831 | dump += INDENT3 "InputState: "; |
Siarhei Vishniakou | 366fb5b | 2023-12-06 11:23:41 -0800 | [diff] [blame] | 5832 | dump += inputStateDump + "\n"; |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 5833 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5834 | } |
| 5835 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5836 | dump += INDENT "Connections: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5837 | } |
| 5838 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5839 | if (!mTouchModePerDisplay.empty()) { |
| 5840 | dump += INDENT "TouchModePerDisplay:\n"; |
| 5841 | for (const auto& [displayId, touchMode] : mTouchModePerDisplay) { |
| 5842 | dump += StringPrintf(INDENT2 "Display: %" PRId32 " TouchMode: %s\n", displayId, |
| 5843 | std::to_string(touchMode).c_str()); |
| 5844 | } |
| 5845 | } else { |
| 5846 | dump += INDENT "TouchModePerDisplay: <none>\n"; |
| 5847 | } |
| 5848 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5849 | dump += INDENT "Configuration:\n"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5850 | dump += StringPrintf(INDENT2 "KeyRepeatDelay: %" PRId64 "ms\n", ns2ms(mConfig.keyRepeatDelay)); |
| 5851 | dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n", |
| 5852 | ns2ms(mConfig.keyRepeatTimeout)); |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 5853 | dump += mLatencyTracker.dump(INDENT2); |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 5854 | dump += mLatencyAggregator.dump(INDENT2); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 5855 | dump += INDENT "InputTracer: "; |
| 5856 | dump += mTracer == nullptr ? "Disabled" : "Enabled"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5857 | } |
| 5858 | |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 5859 | void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) const { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5860 | const size_t numMonitors = monitors.size(); |
| 5861 | for (size_t i = 0; i < numMonitors; i++) { |
| 5862 | const Monitor& monitor = monitors[i]; |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 5863 | const std::shared_ptr<Connection>& connection = monitor.connection; |
| 5864 | dump += StringPrintf(INDENT2 "%zu: '%s', ", i, connection->getInputChannelName().c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5865 | dump += "\n"; |
| 5866 | } |
| 5867 | } |
| 5868 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5869 | class LooperEventCallback : public LooperCallback { |
| 5870 | public: |
| 5871 | LooperEventCallback(std::function<int(int events)> callback) : mCallback(callback) {} |
| 5872 | int handleEvent(int /*fd*/, int events, void* /*data*/) override { return mCallback(events); } |
| 5873 | |
| 5874 | private: |
| 5875 | std::function<int(int events)> mCallback; |
| 5876 | }; |
| 5877 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5878 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputChannel(const std::string& name) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5879 | if (DEBUG_CHANNEL_CREATION) { |
| 5880 | ALOGD("channel '%s' ~ createInputChannel", name.c_str()); |
| 5881 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5882 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5883 | std::unique_ptr<InputChannel> serverChannel; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5884 | std::unique_ptr<InputChannel> clientChannel; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5885 | status_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5886 | |
| 5887 | if (result) { |
| 5888 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5889 | } |
| 5890 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5891 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5892 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5893 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Siarhei Vishniakou | 8d66013 | 2024-01-11 16:48:44 -0800 | [diff] [blame] | 5894 | const int fd = serverChannel->getFd(); |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 5895 | std::shared_ptr<Connection> connection = |
| 5896 | std::make_shared<Connection>(std::move(serverChannel), /*monitor=*/false, |
| 5897 | mIdGenerator); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5898 | |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 5899 | auto [_, inserted] = mConnectionsByToken.try_emplace(token, connection); |
| 5900 | if (!inserted) { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5901 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5902 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5903 | |
| 5904 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5905 | this, std::placeholders::_1, token); |
| 5906 | |
Siarhei Vishniakou | 8d66013 | 2024-01-11 16:48:44 -0800 | [diff] [blame] | 5907 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, sp<LooperEventCallback>::make(callback), |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5908 | nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5909 | } // release lock |
| 5910 | |
| 5911 | // Wake the looper because some connections have changed. |
| 5912 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5913 | return clientChannel; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5914 | } |
| 5915 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5916 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputMonitor(int32_t displayId, |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5917 | const std::string& name, |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5918 | gui::Pid pid) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5919 | std::shared_ptr<InputChannel> serverChannel; |
| 5920 | std::unique_ptr<InputChannel> clientChannel; |
| 5921 | status_t result = openInputChannelPair(name, serverChannel, clientChannel); |
| 5922 | if (result) { |
| 5923 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5924 | } |
| 5925 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5926 | { // acquire lock |
| 5927 | std::scoped_lock _l(mLock); |
| 5928 | |
| 5929 | if (displayId < 0) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5930 | return base::Error(BAD_VALUE) << "Attempted to create input monitor with name " << name |
| 5931 | << " without a specified display."; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5932 | } |
| 5933 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 5934 | std::shared_ptr<Connection> connection = |
| 5935 | std::make_shared<Connection>(serverChannel, /*monitor=*/true, mIdGenerator); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5936 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Siarhei Vishniakou | 8d66013 | 2024-01-11 16:48:44 -0800 | [diff] [blame] | 5937 | const int fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5938 | |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 5939 | auto [_, inserted] = mConnectionsByToken.emplace(token, connection); |
| 5940 | if (!inserted) { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5941 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5942 | } |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 5943 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5944 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5945 | this, std::placeholders::_1, token); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5946 | |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 5947 | mGlobalMonitorsByDisplay[displayId].emplace_back(connection, pid); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5948 | |
Siarhei Vishniakou | 8d66013 | 2024-01-11 16:48:44 -0800 | [diff] [blame] | 5949 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, sp<LooperEventCallback>::make(callback), |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5950 | nullptr); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5951 | } |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5952 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5953 | // Wake the looper because some connections have changed. |
| 5954 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5955 | return clientChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5956 | } |
| 5957 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5958 | status_t InputDispatcher::removeInputChannel(const sp<IBinder>& connectionToken) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5959 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5960 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5961 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5962 | status_t status = removeInputChannelLocked(connectionToken, /*notify=*/false); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5963 | if (status) { |
| 5964 | return status; |
| 5965 | } |
| 5966 | } // release lock |
| 5967 | |
| 5968 | // Wake the poll loop because removing the connection may have changed the current |
| 5969 | // synchronization state. |
| 5970 | mLooper->wake(); |
| 5971 | return OK; |
| 5972 | } |
| 5973 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5974 | status_t InputDispatcher::removeInputChannelLocked(const sp<IBinder>& connectionToken, |
| 5975 | bool notify) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 5976 | std::shared_ptr<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5977 | if (connection == nullptr) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5978 | // 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] | 5979 | return BAD_VALUE; |
| 5980 | } |
| 5981 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5982 | removeConnectionLocked(connection); |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 5983 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5984 | if (connection->monitor) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5985 | removeMonitorChannelLocked(connectionToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5986 | } |
| 5987 | |
Siarhei Vishniakou | 10bfdc9 | 2024-02-01 11:12:57 -0800 | [diff] [blame^] | 5988 | mLooper->removeFd(connection->inputPublisher.getChannel()->getFd()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5989 | |
| 5990 | nsecs_t currentTime = now(); |
| 5991 | abortBrokenDispatchCycleLocked(currentTime, connection, notify); |
| 5992 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5993 | connection->status = Connection::Status::ZOMBIE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5994 | return OK; |
| 5995 | } |
| 5996 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5997 | void InputDispatcher::removeMonitorChannelLocked(const sp<IBinder>& connectionToken) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5998 | for (auto it = mGlobalMonitorsByDisplay.begin(); it != mGlobalMonitorsByDisplay.end();) { |
| 5999 | auto& [displayId, monitors] = *it; |
| 6000 | std::erase_if(monitors, [connectionToken](const Monitor& monitor) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6001 | return monitor.connection->getToken() == connectionToken; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6002 | }); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6003 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6004 | if (monitors.empty()) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6005 | it = mGlobalMonitorsByDisplay.erase(it); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6006 | } else { |
| 6007 | ++it; |
| 6008 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6009 | } |
| 6010 | } |
| 6011 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6012 | status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6013 | std::scoped_lock _l(mLock); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 6014 | return pilferPointersLocked(token); |
| 6015 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6016 | |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 6017 | status_t InputDispatcher::pilferPointersLocked(const sp<IBinder>& token) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6018 | const std::shared_ptr<Connection> requestingConnection = getConnectionLocked(token); |
| 6019 | if (!requestingConnection) { |
Siarhei Vishniakou | 8384e0d | 2023-09-18 18:48:27 -0700 | [diff] [blame] | 6020 | LOG(WARNING) |
| 6021 | << "Attempted to pilfer pointers from an un-registered channel or invalid token"; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6022 | return BAD_VALUE; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6023 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6024 | |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 6025 | auto [statePtr, windowPtr, displayId] = findTouchStateWindowAndDisplayLocked(token); |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 6026 | if (statePtr == nullptr || windowPtr == nullptr) { |
Siarhei Vishniakou | 8384e0d | 2023-09-18 18:48:27 -0700 | [diff] [blame] | 6027 | LOG(WARNING) |
| 6028 | << "Attempted to pilfer points from a channel without any on-going pointer streams." |
| 6029 | " Ignoring."; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6030 | return BAD_VALUE; |
| 6031 | } |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 6032 | std::set<int32_t> deviceIds = windowPtr->getTouchingDeviceIds(); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 6033 | if (deviceIds.empty()) { |
| 6034 | LOG(WARNING) << "Can't pilfer: no touching devices in window: " << windowPtr->dump(); |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 6035 | return BAD_VALUE; |
| 6036 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6037 | |
Siarhei Vishniakou | 8384e0d | 2023-09-18 18:48:27 -0700 | [diff] [blame] | 6038 | for (const DeviceId deviceId : deviceIds) { |
| 6039 | TouchState& state = *statePtr; |
| 6040 | TouchedWindow& window = *windowPtr; |
| 6041 | // Send cancel events to all the input channels we're stealing from. |
| 6042 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
| 6043 | "input channel stole pointer stream"); |
| 6044 | options.deviceId = deviceId; |
| 6045 | options.displayId = displayId; |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 6046 | std::vector<PointerProperties> pointers = window.getTouchingPointers(deviceId); |
| 6047 | std::bitset<MAX_POINTER_ID + 1> pointerIds = getPointerIds(pointers); |
Siarhei Vishniakou | 8384e0d | 2023-09-18 18:48:27 -0700 | [diff] [blame] | 6048 | options.pointerIds = pointerIds; |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 6049 | |
Siarhei Vishniakou | 8384e0d | 2023-09-18 18:48:27 -0700 | [diff] [blame] | 6050 | std::string canceledWindows; |
| 6051 | for (const TouchedWindow& w : state.windows) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6052 | const std::shared_ptr<Connection> connection = |
| 6053 | getConnectionLocked(w.windowHandle->getToken()); |
| 6054 | if (connection != nullptr && connection->getToken() != token) { |
| 6055 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Siarhei Vishniakou | 8384e0d | 2023-09-18 18:48:27 -0700 | [diff] [blame] | 6056 | canceledWindows += canceledWindows.empty() ? "[" : ", "; |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6057 | canceledWindows += connection->getInputChannelName(); |
Siarhei Vishniakou | 8384e0d | 2023-09-18 18:48:27 -0700 | [diff] [blame] | 6058 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6059 | } |
Siarhei Vishniakou | 8384e0d | 2023-09-18 18:48:27 -0700 | [diff] [blame] | 6060 | canceledWindows += canceledWindows.empty() ? "[]" : "]"; |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6061 | LOG(INFO) << "Channel " << requestingConnection->getInputChannelName() |
Siarhei Vishniakou | 8384e0d | 2023-09-18 18:48:27 -0700 | [diff] [blame] | 6062 | << " is stealing input gesture for device " << deviceId << " from " |
| 6063 | << canceledWindows; |
| 6064 | |
| 6065 | // Prevent the gesture from being sent to any other windows. |
| 6066 | // This only blocks relevant pointers to be sent to other windows |
| 6067 | window.addPilferingPointers(deviceId, pointerIds); |
| 6068 | |
| 6069 | state.cancelPointersForWindowsExcept(deviceId, pointerIds, token); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6070 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6071 | return OK; |
| 6072 | } |
| 6073 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6074 | void InputDispatcher::requestPointerCapture(const sp<IBinder>& windowToken, bool enabled) { |
| 6075 | { // acquire lock |
| 6076 | std::scoped_lock _l(mLock); |
| 6077 | if (DEBUG_FOCUS) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 6078 | const sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(windowToken); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6079 | ALOGI("Request to %s Pointer Capture from: %s.", enabled ? "enable" : "disable", |
| 6080 | windowHandle != nullptr ? windowHandle->getName().c_str() |
| 6081 | : "token without window"); |
| 6082 | } |
| 6083 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6084 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6085 | if (focusedToken != windowToken) { |
| 6086 | ALOGW("Ignoring request to %s Pointer Capture: window does not have focus.", |
| 6087 | enabled ? "enable" : "disable"); |
| 6088 | return; |
| 6089 | } |
| 6090 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6091 | if (enabled == mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6092 | ALOGW("Ignoring request to %s Pointer Capture: " |
| 6093 | "window has %s requested pointer capture.", |
| 6094 | enabled ? "enable" : "disable", enabled ? "already" : "not"); |
| 6095 | return; |
| 6096 | } |
| 6097 | |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 6098 | if (enabled) { |
| 6099 | if (std::find(mIneligibleDisplaysForPointerCapture.begin(), |
| 6100 | mIneligibleDisplaysForPointerCapture.end(), |
| 6101 | mFocusedDisplayId) != mIneligibleDisplaysForPointerCapture.end()) { |
| 6102 | ALOGW("Ignoring request to enable Pointer Capture: display is not eligible"); |
| 6103 | return; |
| 6104 | } |
| 6105 | } |
| 6106 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6107 | setPointerCaptureLocked(enabled); |
| 6108 | } // release lock |
| 6109 | |
| 6110 | // Wake the thread to process command entries. |
| 6111 | mLooper->wake(); |
| 6112 | } |
| 6113 | |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 6114 | void InputDispatcher::setDisplayEligibilityForPointerCapture(int32_t displayId, bool isEligible) { |
| 6115 | { // acquire lock |
| 6116 | std::scoped_lock _l(mLock); |
| 6117 | std::erase(mIneligibleDisplaysForPointerCapture, displayId); |
| 6118 | if (!isEligible) { |
| 6119 | mIneligibleDisplaysForPointerCapture.push_back(displayId); |
| 6120 | } |
| 6121 | } // release lock |
| 6122 | } |
| 6123 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6124 | std::optional<gui::Pid> InputDispatcher::findMonitorPidByTokenLocked(const sp<IBinder>& token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6125 | for (const auto& [_, monitors] : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6126 | for (const Monitor& monitor : monitors) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6127 | if (monitor.connection->getToken() == token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6128 | return monitor.pid; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6129 | } |
| 6130 | } |
| 6131 | } |
| 6132 | return std::nullopt; |
| 6133 | } |
| 6134 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6135 | std::shared_ptr<Connection> InputDispatcher::getConnectionLocked( |
| 6136 | const sp<IBinder>& inputConnectionToken) const { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 6137 | if (inputConnectionToken == nullptr) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 6138 | return nullptr; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 6139 | } |
| 6140 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 6141 | for (const auto& [token, connection] : mConnectionsByToken) { |
| 6142 | if (token == inputConnectionToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 6143 | return connection; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6144 | } |
| 6145 | } |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 6146 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 6147 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6148 | } |
| 6149 | |
Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 6150 | std::string InputDispatcher::getConnectionNameLocked(const sp<IBinder>& connectionToken) const { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6151 | std::shared_ptr<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 6152 | if (connection == nullptr) { |
| 6153 | return "<nullptr>"; |
| 6154 | } |
| 6155 | return connection->getInputChannelName(); |
| 6156 | } |
| 6157 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6158 | void InputDispatcher::removeConnectionLocked(const std::shared_ptr<Connection>& connection) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6159 | mAnrTracker.eraseToken(connection->getToken()); |
| 6160 | mConnectionsByToken.erase(connection->getToken()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6161 | } |
| 6162 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6163 | void InputDispatcher::doDispatchCycleFinishedCommand(nsecs_t finishTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6164 | const std::shared_ptr<Connection>& connection, |
| 6165 | uint32_t seq, bool handled, |
| 6166 | nsecs_t consumeTime) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6167 | // Handle post-event policy actions. |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 6168 | std::unique_ptr<const KeyEntry> fallbackKeyEntry; |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6169 | |
| 6170 | { // Start critical section |
| 6171 | auto dispatchEntryIt = |
| 6172 | std::find_if(connection->waitQueue.begin(), connection->waitQueue.end(), |
| 6173 | [seq](auto& e) { return e->seq == seq; }); |
| 6174 | if (dispatchEntryIt == connection->waitQueue.end()) { |
| 6175 | return; |
| 6176 | } |
| 6177 | |
| 6178 | DispatchEntry& dispatchEntry = **dispatchEntryIt; |
| 6179 | |
| 6180 | const nsecs_t eventDuration = finishTime - dispatchEntry.deliveryTime; |
| 6181 | if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) { |
Siarhei Vishniakou | 10bfdc9 | 2024-02-01 11:12:57 -0800 | [diff] [blame^] | 6182 | ALOGI("%s spent %" PRId64 "ms processing %s", connection->getInputChannelName().c_str(), |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6183 | ns2ms(eventDuration), dispatchEntry.eventEntry->getDescription().c_str()); |
| 6184 | } |
| 6185 | if (shouldReportFinishedEvent(dispatchEntry, *connection)) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6186 | mLatencyTracker.trackFinishedEvent(dispatchEntry.eventEntry->id, connection->getToken(), |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6187 | dispatchEntry.deliveryTime, consumeTime, finishTime); |
| 6188 | } |
| 6189 | |
| 6190 | if (dispatchEntry.eventEntry->type == EventEntry::Type::KEY) { |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6191 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(*(dispatchEntry.eventEntry)); |
| 6192 | fallbackKeyEntry = |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6193 | afterKeyEventLockedInterruptable(connection, dispatchEntry, keyEntry, handled); |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6194 | } |
| 6195 | } // End critical section: The -LockedInterruptable methods may have released the lock. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6196 | |
| 6197 | // Dequeue the event and start the next cycle. |
| 6198 | // Because the lock might have been released, it is possible that the |
| 6199 | // contents of the wait queue to have been drained, so we need to double-check |
| 6200 | // a few things. |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6201 | auto entryIt = std::find_if(connection->waitQueue.begin(), connection->waitQueue.end(), |
| 6202 | [seq](auto& e) { return e->seq == seq; }); |
| 6203 | if (entryIt != connection->waitQueue.end()) { |
| 6204 | std::unique_ptr<DispatchEntry> dispatchEntry = std::move(*entryIt); |
| 6205 | connection->waitQueue.erase(entryIt); |
| 6206 | |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6207 | const sp<IBinder>& connectionToken = connection->getToken(); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6208 | mAnrTracker.erase(dispatchEntry->timeoutTime, connectionToken); |
| 6209 | if (!connection->responsive) { |
| 6210 | connection->responsive = isConnectionResponsive(*connection); |
| 6211 | if (connection->responsive) { |
| 6212 | // The connection was unresponsive, and now it's responsive. |
| 6213 | processConnectionResponsiveLocked(*connection); |
| 6214 | } |
| 6215 | } |
| 6216 | traceWaitQueueLength(*connection); |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6217 | if (fallbackKeyEntry && connection->status == Connection::Status::NORMAL) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6218 | const InputTarget target{.connection = connection, .flags = dispatchEntry->targetFlags}; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 6219 | enqueueDispatchEntryLocked(connection, std::move(fallbackKeyEntry), target); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6220 | } |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6221 | releaseDispatchEntry(std::move(dispatchEntry)); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6222 | } |
| 6223 | |
| 6224 | // Start the next dispatch cycle for this connection. |
| 6225 | startDispatchCycleLocked(now(), connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6226 | } |
| 6227 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6228 | void InputDispatcher::sendFocusChangedCommandLocked(const sp<IBinder>& oldToken, |
| 6229 | const sp<IBinder>& newToken) { |
| 6230 | auto command = [this, oldToken, newToken]() REQUIRES(mLock) { |
| 6231 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6232 | mPolicy.notifyFocusChanged(oldToken, newToken); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6233 | }; |
| 6234 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6235 | } |
| 6236 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6237 | void InputDispatcher::sendDropWindowCommandLocked(const sp<IBinder>& token, float x, float y) { |
| 6238 | auto command = [this, token, x, y]() REQUIRES(mLock) { |
| 6239 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6240 | mPolicy.notifyDropWindow(token, x, y); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6241 | }; |
| 6242 | postCommandLocked(std::move(command)); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 6243 | } |
| 6244 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6245 | void InputDispatcher::onAnrLocked(const std::shared_ptr<Connection>& connection) { |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6246 | if (connection == nullptr) { |
| 6247 | LOG_ALWAYS_FATAL("Caller must check for nullness"); |
| 6248 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6249 | // Since we are allowing the policy to extend the timeout, maybe the waitQueue |
| 6250 | // is already healthy again. Don't raise ANR in this situation |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6251 | if (connection->waitQueue.empty()) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6252 | ALOGI("Not raising ANR because the connection %s has recovered", |
Siarhei Vishniakou | 10bfdc9 | 2024-02-01 11:12:57 -0800 | [diff] [blame^] | 6253 | connection->getInputChannelName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6254 | return; |
| 6255 | } |
| 6256 | /** |
| 6257 | * The "oldestEntry" is the entry that was first sent to the application. That entry, however, |
| 6258 | * may not be the one that caused the timeout to occur. One possibility is that window timeout |
| 6259 | * has changed. This could cause newer entries to time out before the already dispatched |
| 6260 | * entries. In that situation, the newest entries caused ANR. But in all likelihood, the app |
| 6261 | * processes the events linearly. So providing information about the oldest entry seems to be |
| 6262 | * most useful. |
| 6263 | */ |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6264 | DispatchEntry& oldestEntry = *connection->waitQueue.front(); |
| 6265 | const nsecs_t currentWait = now() - oldestEntry.deliveryTime; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6266 | std::string reason = |
| 6267 | android::base::StringPrintf("%s is not responding. Waited %" PRId64 "ms for %s", |
Siarhei Vishniakou | 10bfdc9 | 2024-02-01 11:12:57 -0800 | [diff] [blame^] | 6268 | connection->getInputChannelName().c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6269 | ns2ms(currentWait), |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6270 | oldestEntry.eventEntry->getDescription().c_str()); |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6271 | sp<IBinder> connectionToken = connection->getToken(); |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 6272 | updateLastAnrStateLocked(getWindowHandleLocked(connectionToken), reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6273 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6274 | processConnectionUnresponsiveLocked(*connection, std::move(reason)); |
| 6275 | |
| 6276 | // Stop waking up for events on this connection, it is already unresponsive |
| 6277 | cancelEventsForAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6278 | } |
| 6279 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6280 | void InputDispatcher::onAnrLocked(std::shared_ptr<InputApplicationHandle> application) { |
| 6281 | std::string reason = |
| 6282 | StringPrintf("%s does not have a focused window", application->getName().c_str()); |
| 6283 | updateLastAnrStateLocked(*application, reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6284 | |
Yabin Cui | 8eb9c55 | 2023-06-08 18:05:07 +0000 | [diff] [blame] | 6285 | auto command = [this, app = std::move(application)]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6286 | scoped_unlock unlock(mLock); |
Yabin Cui | ced952f | 2023-06-09 21:12:51 +0000 | [diff] [blame] | 6287 | mPolicy.notifyNoFocusedWindowAnr(app); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6288 | }; |
| 6289 | postCommandLocked(std::move(command)); |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 6290 | } |
| 6291 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 6292 | void InputDispatcher::updateLastAnrStateLocked(const sp<WindowInfoHandle>& window, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6293 | const std::string& reason) { |
| 6294 | const std::string windowLabel = getApplicationWindowLabel(nullptr, window); |
| 6295 | updateLastAnrStateLocked(windowLabel, reason); |
| 6296 | } |
| 6297 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6298 | void InputDispatcher::updateLastAnrStateLocked(const InputApplicationHandle& application, |
| 6299 | const std::string& reason) { |
| 6300 | const std::string windowLabel = getApplicationWindowLabel(&application, nullptr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6301 | updateLastAnrStateLocked(windowLabel, reason); |
| 6302 | } |
| 6303 | |
| 6304 | void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel, |
| 6305 | const std::string& reason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6306 | // 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] | 6307 | time_t t = time(nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6308 | struct tm tm; |
| 6309 | localtime_r(&t, &tm); |
| 6310 | char timestr[64]; |
| 6311 | strftime(timestr, sizeof(timestr), "%F %T", &tm); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6312 | mLastAnrState.clear(); |
| 6313 | mLastAnrState += INDENT "ANR:\n"; |
| 6314 | mLastAnrState += StringPrintf(INDENT2 "Time: %s\n", timestr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6315 | mLastAnrState += StringPrintf(INDENT2 "Reason: %s\n", reason.c_str()); |
| 6316 | mLastAnrState += StringPrintf(INDENT2 "Window: %s\n", windowLabel.c_str()); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6317 | dumpDispatchStateLocked(mLastAnrState); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6318 | } |
| 6319 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6320 | void InputDispatcher::doInterceptKeyBeforeDispatchingCommand(const sp<IBinder>& focusedWindowToken, |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 6321 | const KeyEntry& entry) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6322 | const KeyEvent event = createKeyEvent(entry); |
| 6323 | nsecs_t delay = 0; |
| 6324 | { // release lock |
| 6325 | scoped_unlock unlock(mLock); |
| 6326 | android::base::Timer t; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6327 | delay = mPolicy.interceptKeyBeforeDispatching(focusedWindowToken, event, entry.policyFlags); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6328 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 6329 | ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms", |
| 6330 | std::to_string(t.duration().count()).c_str()); |
| 6331 | } |
| 6332 | } // acquire lock |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6333 | |
| 6334 | if (delay < 0) { |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 6335 | entry.interceptKeyResult = KeyEntry::InterceptKeyResult::SKIP; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6336 | } else if (delay == 0) { |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 6337 | entry.interceptKeyResult = KeyEntry::InterceptKeyResult::CONTINUE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6338 | } else { |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 6339 | entry.interceptKeyResult = KeyEntry::InterceptKeyResult::TRY_AGAIN_LATER; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6340 | entry.interceptKeyWakeupTime = now() + delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6341 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6342 | } |
| 6343 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6344 | void InputDispatcher::sendWindowUnresponsiveCommandLocked(const sp<IBinder>& token, |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6345 | std::optional<gui::Pid> pid, |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6346 | std::string reason) { |
Yabin Cui | 8eb9c55 | 2023-06-08 18:05:07 +0000 | [diff] [blame] | 6347 | auto command = [this, token, pid, r = std::move(reason)]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6348 | scoped_unlock unlock(mLock); |
Yabin Cui | ced952f | 2023-06-09 21:12:51 +0000 | [diff] [blame] | 6349 | mPolicy.notifyWindowUnresponsive(token, pid, r); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6350 | }; |
| 6351 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6352 | } |
| 6353 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6354 | void InputDispatcher::sendWindowResponsiveCommandLocked(const sp<IBinder>& token, |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6355 | std::optional<gui::Pid> pid) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6356 | auto command = [this, token, pid]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6357 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6358 | mPolicy.notifyWindowResponsive(token, pid); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6359 | }; |
| 6360 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6361 | } |
| 6362 | |
| 6363 | /** |
| 6364 | * Tell the policy that a connection has become unresponsive so that it can start ANR. |
| 6365 | * Check whether the connection of interest is a monitor or a window, and add the corresponding |
| 6366 | * command entry to the command queue. |
| 6367 | */ |
| 6368 | void InputDispatcher::processConnectionUnresponsiveLocked(const Connection& connection, |
| 6369 | std::string reason) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6370 | const sp<IBinder>& connectionToken = connection.getToken(); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6371 | std::optional<gui::Pid> pid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6372 | if (connection.monitor) { |
Siarhei Vishniakou | 10bfdc9 | 2024-02-01 11:12:57 -0800 | [diff] [blame^] | 6373 | ALOGW("Monitor %s is unresponsive: %s", connection.getInputChannelName().c_str(), |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6374 | reason.c_str()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6375 | pid = findMonitorPidByTokenLocked(connectionToken); |
| 6376 | } else { |
| 6377 | // The connection is a window |
Siarhei Vishniakou | 10bfdc9 | 2024-02-01 11:12:57 -0800 | [diff] [blame^] | 6378 | ALOGW("Window %s is unresponsive: %s", connection.getInputChannelName().c_str(), |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6379 | reason.c_str()); |
| 6380 | const sp<WindowInfoHandle> handle = getWindowHandleLocked(connectionToken); |
| 6381 | if (handle != nullptr) { |
| 6382 | pid = handle->getInfo()->ownerPid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6383 | } |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6384 | } |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6385 | sendWindowUnresponsiveCommandLocked(connectionToken, pid, std::move(reason)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6386 | } |
| 6387 | |
| 6388 | /** |
| 6389 | * Tell the policy that a connection has become responsive so that it can stop ANR. |
| 6390 | */ |
| 6391 | void InputDispatcher::processConnectionResponsiveLocked(const Connection& connection) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6392 | const sp<IBinder>& connectionToken = connection.getToken(); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6393 | std::optional<gui::Pid> pid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6394 | if (connection.monitor) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6395 | pid = findMonitorPidByTokenLocked(connectionToken); |
| 6396 | } else { |
| 6397 | // The connection is a window |
| 6398 | const sp<WindowInfoHandle> handle = getWindowHandleLocked(connectionToken); |
| 6399 | if (handle != nullptr) { |
| 6400 | pid = handle->getInfo()->ownerPid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6401 | } |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6402 | } |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6403 | sendWindowResponsiveCommandLocked(connectionToken, pid); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6404 | } |
| 6405 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 6406 | std::unique_ptr<const KeyEntry> InputDispatcher::afterKeyEventLockedInterruptable( |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6407 | const std::shared_ptr<Connection>& connection, DispatchEntry& dispatchEntry, |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6408 | const KeyEntry& keyEntry, bool handled) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6409 | if (keyEntry.flags & AKEY_EVENT_FLAG_FALLBACK) { |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6410 | if (!handled) { |
| 6411 | // Report the key as unhandled, since the fallback was not handled. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6412 | mReporter->reportUnhandledKey(keyEntry.id); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6413 | } |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6414 | return {}; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6415 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6416 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6417 | // Get the fallback key state. |
| 6418 | // Clear it out after dispatching the UP. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6419 | int32_t originalKeyCode = keyEntry.keyCode; |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6420 | std::optional<int32_t> fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6421 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6422 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6423 | } |
| 6424 | |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6425 | if (handled || !dispatchEntry.hasForegroundTarget()) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6426 | // If the application handles the original key for which we previously |
| 6427 | // generated a fallback or if the window is not a foreground window, |
| 6428 | // then cancel the associated fallback key, if any. |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6429 | if (fallbackKeyCode) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6430 | // Dispatch the unhandled key to the policy with the cancel flag. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6431 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6432 | ALOGD("Unhandled key event: Asking policy to cancel fallback action. " |
| 6433 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6434 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, |
| 6435 | keyEntry.policyFlags); |
| 6436 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6437 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6438 | event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6439 | |
| 6440 | mLock.unlock(); |
| 6441 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6442 | if (const auto unhandledKeyFallback = |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6443 | mPolicy.dispatchUnhandledKey(connection->getToken(), event, |
| 6444 | keyEntry.policyFlags); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6445 | unhandledKeyFallback) { |
| 6446 | event = *unhandledKeyFallback; |
| 6447 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6448 | |
| 6449 | mLock.lock(); |
| 6450 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6451 | // Cancel the fallback key. |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6452 | if (*fallbackKeyCode != AKEYCODE_UNKNOWN) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 6453 | CancelationOptions options(CancelationOptions::Mode::CANCEL_FALLBACK_EVENTS, |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6454 | "application handled the original non-fallback key " |
| 6455 | "or is no longer a foreground target, " |
| 6456 | "canceling previously dispatched fallback key"); |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6457 | options.keyCode = *fallbackKeyCode; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6458 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6459 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6460 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6461 | } |
| 6462 | } else { |
| 6463 | // If the application did not handle a non-fallback key, first check |
| 6464 | // that we are in a good state to perform unhandled key event processing |
| 6465 | // Then ask the policy what to do with it. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6466 | bool initialDown = keyEntry.action == AKEY_EVENT_ACTION_DOWN && keyEntry.repeatCount == 0; |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6467 | if (!fallbackKeyCode && !initialDown) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6468 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6469 | ALOGD("Unhandled key event: Skipping unhandled key event processing " |
| 6470 | "since this is not an initial down. " |
| 6471 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6472 | originalKeyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 6473 | } |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6474 | return {}; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6475 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6476 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6477 | // Dispatch the unhandled key to the policy. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6478 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6479 | ALOGD("Unhandled key event: Asking policy to perform fallback action. " |
| 6480 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6481 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 6482 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6483 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6484 | |
| 6485 | mLock.unlock(); |
| 6486 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6487 | bool fallback = false; |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6488 | if (auto fb = mPolicy.dispatchUnhandledKey(connection->getToken(), event, |
| 6489 | keyEntry.policyFlags); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6490 | fb) { |
| 6491 | fallback = true; |
| 6492 | event = *fb; |
| 6493 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6494 | |
| 6495 | mLock.lock(); |
| 6496 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 6497 | if (connection->status != Connection::Status::NORMAL) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6498 | connection->inputState.removeFallbackKey(originalKeyCode); |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6499 | return {}; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6500 | } |
| 6501 | |
| 6502 | // Latch the fallback keycode for this key on an initial down. |
| 6503 | // The fallback keycode cannot change at any other point in the lifecycle. |
| 6504 | if (initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6505 | if (fallback) { |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6506 | *fallbackKeyCode = event.getKeyCode(); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6507 | } else { |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6508 | *fallbackKeyCode = AKEYCODE_UNKNOWN; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6509 | } |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6510 | connection->inputState.setFallbackKey(originalKeyCode, *fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6511 | } |
| 6512 | |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6513 | ALOG_ASSERT(fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6514 | |
| 6515 | // Cancel the fallback key if the policy decides not to send it anymore. |
| 6516 | // We will continue to dispatch the key to the policy but we will no |
| 6517 | // longer dispatch a fallback key to the application. |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6518 | if (*fallbackKeyCode != AKEYCODE_UNKNOWN && |
| 6519 | (!fallback || *fallbackKeyCode != event.getKeyCode())) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6520 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6521 | if (fallback) { |
| 6522 | ALOGD("Unhandled key event: Policy requested to send key %d" |
| 6523 | "as a fallback for %d, but on the DOWN it had requested " |
| 6524 | "to send %d instead. Fallback canceled.", |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6525 | event.getKeyCode(), originalKeyCode, *fallbackKeyCode); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6526 | } else { |
| 6527 | ALOGD("Unhandled key event: Policy did not request fallback for %d, " |
| 6528 | "but on the DOWN it had requested to send %d. " |
| 6529 | "Fallback canceled.", |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6530 | originalKeyCode, *fallbackKeyCode); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6531 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6532 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6533 | |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 6534 | CancelationOptions options(CancelationOptions::Mode::CANCEL_FALLBACK_EVENTS, |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6535 | "canceling fallback, policy no longer desires it"); |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6536 | options.keyCode = *fallbackKeyCode; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6537 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
| 6538 | |
| 6539 | fallback = false; |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6540 | *fallbackKeyCode = AKEYCODE_UNKNOWN; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6541 | if (keyEntry.action != AKEY_EVENT_ACTION_UP) { |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6542 | connection->inputState.setFallbackKey(originalKeyCode, *fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6543 | } |
| 6544 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6545 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6546 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6547 | { |
| 6548 | std::string msg; |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6549 | const std::map<int32_t, int32_t>& fallbackKeys = |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6550 | connection->inputState.getFallbackKeys(); |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6551 | for (const auto& [key, value] : fallbackKeys) { |
| 6552 | msg += StringPrintf(", %d->%d", key, value); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6553 | } |
| 6554 | ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.", |
| 6555 | fallbackKeys.size(), msg.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6556 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6557 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6558 | |
| 6559 | if (fallback) { |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6560 | // Return the fallback key that we want dispatched to the channel. |
| 6561 | std::unique_ptr<KeyEntry> newEntry = |
| 6562 | std::make_unique<KeyEntry>(mIdGenerator.nextId(), keyEntry.injectionState, |
| 6563 | event.getEventTime(), event.getDeviceId(), |
| 6564 | event.getSource(), event.getDisplayId(), |
| 6565 | keyEntry.policyFlags, keyEntry.action, |
| 6566 | event.getFlags() | AKEY_EVENT_FLAG_FALLBACK, |
| 6567 | *fallbackKeyCode, event.getScanCode(), |
| 6568 | event.getMetaState(), event.getRepeatCount(), |
| 6569 | event.getDownTime()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6570 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6571 | ALOGD("Unhandled key event: Dispatching fallback key. " |
| 6572 | "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x", |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6573 | originalKeyCode, *fallbackKeyCode, keyEntry.metaState); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6574 | } |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6575 | return newEntry; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6576 | } else { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6577 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6578 | ALOGD("Unhandled key event: No fallback key."); |
| 6579 | } |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6580 | |
| 6581 | // Report the key as unhandled, since there is no fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6582 | mReporter->reportUnhandledKey(keyEntry.id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6583 | } |
| 6584 | } |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6585 | return {}; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6586 | } |
| 6587 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6588 | void InputDispatcher::traceInboundQueueLengthLocked() { |
| 6589 | if (ATRACE_ENABLED()) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 6590 | ATRACE_INT("iq", mInboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6591 | } |
| 6592 | } |
| 6593 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6594 | void InputDispatcher::traceOutboundQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6595 | if (ATRACE_ENABLED()) { |
| 6596 | char counterName[40]; |
Siarhei Vishniakou | 10bfdc9 | 2024-02-01 11:12:57 -0800 | [diff] [blame^] | 6597 | snprintf(counterName, sizeof(counterName), "oq:%s", |
| 6598 | connection.getInputChannelName().c_str()); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6599 | ATRACE_INT(counterName, connection.outboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6600 | } |
| 6601 | } |
| 6602 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6603 | void InputDispatcher::traceWaitQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6604 | if (ATRACE_ENABLED()) { |
| 6605 | char counterName[40]; |
Siarhei Vishniakou | 10bfdc9 | 2024-02-01 11:12:57 -0800 | [diff] [blame^] | 6606 | snprintf(counterName, sizeof(counterName), "wq:%s", |
| 6607 | connection.getInputChannelName().c_str()); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6608 | ATRACE_INT(counterName, connection.waitQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6609 | } |
| 6610 | } |
| 6611 | |
Siarhei Vishniakou | 5e20f27 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 6612 | void InputDispatcher::dump(std::string& dump) const { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6613 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6614 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6615 | dump += "Input Dispatcher State:\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6616 | dumpDispatchStateLocked(dump); |
| 6617 | |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6618 | if (!mLastAnrState.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6619 | dump += "\nInput Dispatcher State at time of last ANR:\n"; |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6620 | dump += mLastAnrState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6621 | } |
| 6622 | } |
| 6623 | |
| 6624 | void InputDispatcher::monitor() { |
| 6625 | // 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] | 6626 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6627 | mLooper->wake(); |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6628 | mDispatcherIsAlive.wait(_l); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6629 | } |
| 6630 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6631 | /** |
| 6632 | * Wake up the dispatcher and wait until it processes all events and commands. |
| 6633 | * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so |
| 6634 | * this method can be safely called from any thread, as long as you've ensured that |
| 6635 | * the work you are interested in completing has already been queued. |
| 6636 | */ |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 6637 | bool InputDispatcher::waitForIdle() const { |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6638 | /** |
| 6639 | * Timeout should represent the longest possible time that a device might spend processing |
| 6640 | * events and commands. |
| 6641 | */ |
| 6642 | constexpr std::chrono::duration TIMEOUT = 100ms; |
| 6643 | std::unique_lock lock(mLock); |
| 6644 | mLooper->wake(); |
| 6645 | std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT); |
| 6646 | return result == std::cv_status::no_timeout; |
| 6647 | } |
| 6648 | |
Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 6649 | /** |
| 6650 | * Sets focus to the window identified by the token. This must be called |
| 6651 | * after updating any input window handles. |
| 6652 | * |
| 6653 | * Params: |
| 6654 | * request.token - input channel token used to identify the window that should gain focus. |
| 6655 | * request.focusedToken - the token that the caller expects currently to be focused. If the |
| 6656 | * specified token does not match the currently focused window, this request will be dropped. |
| 6657 | * If the specified focused token matches the currently focused window, the call will succeed. |
| 6658 | * Set this to "null" if this call should succeed no matter what the currently focused token is. |
| 6659 | * request.timestamp - SYSTEM_TIME_MONOTONIC timestamp in nanos set by the client (wm) |
| 6660 | * when requesting the focus change. This determines which request gets |
| 6661 | * precedence if there is a focus change request from another source such as pointer down. |
| 6662 | */ |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6663 | void InputDispatcher::setFocusedWindow(const FocusRequest& request) { |
| 6664 | { // acquire lock |
| 6665 | std::scoped_lock _l(mLock); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6666 | std::optional<FocusResolver::FocusChanges> changes = |
| 6667 | mFocusResolver.setFocusedWindow(request, getWindowHandlesLocked(request.displayId)); |
| 6668 | if (changes) { |
| 6669 | onFocusChangedLocked(*changes); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6670 | } |
| 6671 | } // release lock |
| 6672 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6673 | mLooper->wake(); |
| 6674 | } |
| 6675 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6676 | void InputDispatcher::onFocusChangedLocked(const FocusResolver::FocusChanges& changes) { |
| 6677 | if (changes.oldFocus) { |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6678 | std::shared_ptr<Connection> focusedConnection = getConnectionLocked(changes.oldFocus); |
| 6679 | if (focusedConnection) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 6680 | CancelationOptions options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6681 | "focus left window"); |
Siarhei Vishniakou | 18cbdb8 | 2024-01-31 17:58:13 -0800 | [diff] [blame] | 6682 | synthesizeCancelationEventsForConnectionLocked(focusedConnection, options); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6683 | enqueueFocusEventLocked(changes.oldFocus, /*hasFocus=*/false, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6684 | } |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6685 | } |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6686 | if (changes.newFocus) { |
Siarhei Vishniakou | c033dfb | 2023-10-03 10:45:16 -0700 | [diff] [blame] | 6687 | resetNoFocusedWindowTimeoutLocked(); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6688 | enqueueFocusEventLocked(changes.newFocus, /*hasFocus=*/true, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6689 | } |
| 6690 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6691 | // If a window has pointer capture, then it must have focus. We need to ensure that this |
| 6692 | // contract is upheld when pointer capture is being disabled due to a loss of window focus. |
| 6693 | // If the window loses focus before it loses pointer capture, then the window can be in a state |
| 6694 | // where it has pointer capture but not focus, violating the contract. Therefore we must |
| 6695 | // dispatch the pointer capture event before the focus event. Since focus events are added to |
| 6696 | // the front of the queue (above), we add the pointer capture event to the front of the queue |
| 6697 | // after the focus events are added. This ensures the pointer capture event ends up at the |
| 6698 | // front. |
| 6699 | disablePointerCaptureForcedLocked(); |
| 6700 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6701 | if (mFocusedDisplayId == changes.displayId) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6702 | sendFocusChangedCommandLocked(changes.oldFocus, changes.newFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6703 | } |
| 6704 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6705 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6706 | void InputDispatcher::disablePointerCaptureForcedLocked() { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6707 | if (!mCurrentPointerCaptureRequest.enable && !mWindowTokenWithPointerCapture) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6708 | return; |
| 6709 | } |
| 6710 | |
| 6711 | ALOGD_IF(DEBUG_FOCUS, "Disabling Pointer Capture because the window lost focus."); |
| 6712 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6713 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6714 | setPointerCaptureLocked(false); |
| 6715 | } |
| 6716 | |
| 6717 | if (!mWindowTokenWithPointerCapture) { |
| 6718 | // No need to send capture changes because no window has capture. |
| 6719 | return; |
| 6720 | } |
| 6721 | |
| 6722 | if (mPendingEvent != nullptr) { |
| 6723 | // Move the pending event to the front of the queue. This will give the chance |
| 6724 | // for the pending event to be dropped if it is a captured event. |
| 6725 | mInboundQueue.push_front(mPendingEvent); |
| 6726 | mPendingEvent = nullptr; |
| 6727 | } |
| 6728 | |
| 6729 | auto entry = std::make_unique<PointerCaptureChangedEntry>(mIdGenerator.nextId(), now(), |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6730 | mCurrentPointerCaptureRequest); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6731 | mInboundQueue.push_front(std::move(entry)); |
| 6732 | } |
| 6733 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6734 | void InputDispatcher::setPointerCaptureLocked(bool enable) { |
| 6735 | mCurrentPointerCaptureRequest.enable = enable; |
| 6736 | mCurrentPointerCaptureRequest.seq++; |
| 6737 | auto command = [this, request = mCurrentPointerCaptureRequest]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6738 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6739 | mPolicy.setPointerCapture(request); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6740 | }; |
| 6741 | postCommandLocked(std::move(command)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6742 | } |
| 6743 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6744 | void InputDispatcher::displayRemoved(int32_t displayId) { |
| 6745 | { // acquire lock |
| 6746 | std::scoped_lock _l(mLock); |
| 6747 | // Set an empty list to remove all handles from the specific display. |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 6748 | setInputWindowsLocked(/*windowInfoHandles=*/{}, displayId); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6749 | setFocusedApplicationLocked(displayId, nullptr); |
| 6750 | // Call focus resolver to clean up stale requests. This must be called after input windows |
| 6751 | // have been removed for the removed display. |
| 6752 | mFocusResolver.displayRemoved(displayId); |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 6753 | // Reset pointer capture eligibility, regardless of previous state. |
| 6754 | std::erase(mIneligibleDisplaysForPointerCapture, displayId); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 6755 | // Remove the associated touch mode state. |
| 6756 | mTouchModePerDisplay.erase(displayId); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 6757 | mVerifiersByDisplay.erase(displayId); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6758 | } // release lock |
| 6759 | |
| 6760 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6761 | mLooper->wake(); |
| 6762 | } |
| 6763 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 6764 | void InputDispatcher::onWindowInfosChanged(const gui::WindowInfosUpdate& update) { |
Siarhei Vishniakou | aeed0da | 2024-01-09 08:57:13 -0800 | [diff] [blame] | 6765 | if (auto result = validateWindowInfosUpdate(update); !result.ok()) { |
| 6766 | { |
| 6767 | // acquire lock |
| 6768 | std::scoped_lock _l(mLock); |
| 6769 | logDispatchStateLocked(); |
| 6770 | } |
| 6771 | LOG_ALWAYS_FATAL("Incorrect WindowInfosUpdate provided: %s", |
| 6772 | result.error().message().c_str()); |
| 6773 | }; |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6774 | // The listener sends the windows as a flattened array. Separate the windows by display for |
| 6775 | // more convenient parsing. |
| 6776 | std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>> handlesPerDisplay; |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 6777 | for (const auto& info : update.windowInfos) { |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6778 | handlesPerDisplay.emplace(info.displayId, std::vector<sp<WindowInfoHandle>>()); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6779 | handlesPerDisplay[info.displayId].push_back(sp<WindowInfoHandle>::make(info)); |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6780 | } |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6781 | |
| 6782 | { // acquire lock |
| 6783 | std::scoped_lock _l(mLock); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 6784 | |
| 6785 | // Ensure that we have an entry created for all existing displays so that if a displayId has |
| 6786 | // no windows, we can tell that the windows were removed from the display. |
| 6787 | for (const auto& [displayId, _] : mWindowHandlesByDisplay) { |
| 6788 | handlesPerDisplay[displayId]; |
| 6789 | } |
| 6790 | |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6791 | mDisplayInfos.clear(); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 6792 | for (const auto& displayInfo : update.displayInfos) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6793 | mDisplayInfos.emplace(displayInfo.displayId, displayInfo); |
| 6794 | } |
| 6795 | |
| 6796 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 6797 | setInputWindowsLocked(handles, displayId); |
| 6798 | } |
Patrick Williams | 9464b2c | 2023-05-23 11:22:04 -0500 | [diff] [blame] | 6799 | |
| 6800 | if (update.vsyncId < mWindowInfosVsyncId) { |
| 6801 | ALOGE("Received out of order window infos update. Last update vsync id: %" PRId64 |
| 6802 | ", current update vsync id: %" PRId64, |
| 6803 | mWindowInfosVsyncId, update.vsyncId); |
| 6804 | } |
| 6805 | mWindowInfosVsyncId = update.vsyncId; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6806 | } |
| 6807 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6808 | mLooper->wake(); |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6809 | } |
| 6810 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6811 | bool InputDispatcher::shouldDropInput( |
| 6812 | const EventEntry& entry, const sp<android::gui::WindowInfoHandle>& windowHandle) const { |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6813 | if (windowHandle->getInfo()->inputConfig.test(WindowInfo::InputConfig::DROP_INPUT) || |
| 6814 | (windowHandle->getInfo()->inputConfig.test( |
| 6815 | WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED) && |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6816 | isWindowObscuredLocked(windowHandle))) { |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6817 | ALOGW("Dropping %s event targeting %s as requested by the input configuration {%s} on " |
| 6818 | "display %" PRId32 ".", |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6819 | ftl::enum_string(entry.type).c_str(), windowHandle->getName().c_str(), |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6820 | windowHandle->getInfo()->inputConfig.string().c_str(), |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6821 | windowHandle->getInfo()->displayId); |
| 6822 | return true; |
| 6823 | } |
| 6824 | return false; |
| 6825 | } |
| 6826 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 6827 | void InputDispatcher::DispatcherWindowListener::onWindowInfosChanged( |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 6828 | const gui::WindowInfosUpdate& update) { |
| 6829 | mDispatcher.onWindowInfosChanged(update); |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 6830 | } |
| 6831 | |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6832 | void InputDispatcher::cancelCurrentTouch() { |
| 6833 | { |
| 6834 | std::scoped_lock _l(mLock); |
| 6835 | ALOGD("Canceling all ongoing pointer gestures on all displays."); |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 6836 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6837 | "cancel current touch"); |
| 6838 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 6839 | |
| 6840 | mTouchStatesByDisplay.clear(); |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6841 | } |
| 6842 | // Wake up poll loop since there might be work to do. |
| 6843 | mLooper->wake(); |
| 6844 | } |
| 6845 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6846 | void InputDispatcher::setMonitorDispatchingTimeoutForTest(std::chrono::nanoseconds timeout) { |
| 6847 | std::scoped_lock _l(mLock); |
| 6848 | mMonitorDispatchingTimeout = timeout; |
| 6849 | } |
| 6850 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6851 | void InputDispatcher::slipWallpaperTouch(ftl::Flags<InputTarget::Flags> targetFlags, |
| 6852 | const sp<WindowInfoHandle>& oldWindowHandle, |
| 6853 | const sp<WindowInfoHandle>& newWindowHandle, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 6854 | TouchState& state, int32_t deviceId, |
| 6855 | const PointerProperties& pointerProperties, |
Siarhei Vishniakou | bf88052 | 2023-05-01 11:03:22 -0700 | [diff] [blame] | 6856 | std::vector<InputTarget>& targets) const { |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 6857 | std::vector<PointerProperties> pointers{pointerProperties}; |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6858 | const bool oldHasWallpaper = oldWindowHandle->getInfo()->inputConfig.test( |
| 6859 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER); |
| 6860 | const bool newHasWallpaper = targetFlags.test(InputTarget::Flags::FOREGROUND) && |
| 6861 | newWindowHandle->getInfo()->inputConfig.test( |
| 6862 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER); |
| 6863 | const sp<WindowInfoHandle> oldWallpaper = |
| 6864 | oldHasWallpaper ? state.getWallpaperWindow() : nullptr; |
| 6865 | const sp<WindowInfoHandle> newWallpaper = |
| 6866 | newHasWallpaper ? findWallpaperWindowBelow(newWindowHandle) : nullptr; |
| 6867 | if (oldWallpaper == newWallpaper) { |
| 6868 | return; |
| 6869 | } |
| 6870 | |
| 6871 | if (oldWallpaper != nullptr) { |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 6872 | const TouchedWindow& oldTouchedWindow = state.getTouchedWindow(oldWallpaper); |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 6873 | addPointerWindowTargetLocked(oldWallpaper, InputTarget::DispatchMode::SLIPPERY_EXIT, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 6874 | oldTouchedWindow.targetFlags, getPointerIds(pointers), |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 6875 | oldTouchedWindow.getDownTimeInTarget(deviceId), targets); |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 6876 | state.removeTouchingPointerFromWindow(deviceId, pointerProperties.id, oldWallpaper); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6877 | } |
| 6878 | |
| 6879 | if (newWallpaper != nullptr) { |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 6880 | state.addOrUpdateWindow(newWallpaper, InputTarget::DispatchMode::SLIPPERY_ENTER, |
| 6881 | InputTarget::Flags::WINDOW_IS_OBSCURED | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6882 | InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 6883 | deviceId, pointers); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6884 | } |
| 6885 | } |
| 6886 | |
| 6887 | void InputDispatcher::transferWallpaperTouch(ftl::Flags<InputTarget::Flags> oldTargetFlags, |
| 6888 | ftl::Flags<InputTarget::Flags> newTargetFlags, |
| 6889 | const sp<WindowInfoHandle> fromWindowHandle, |
| 6890 | const sp<WindowInfoHandle> toWindowHandle, |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 6891 | TouchState& state, int32_t deviceId, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 6892 | const std::vector<PointerProperties>& pointers) { |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6893 | const bool oldHasWallpaper = oldTargetFlags.test(InputTarget::Flags::FOREGROUND) && |
| 6894 | fromWindowHandle->getInfo()->inputConfig.test( |
| 6895 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER); |
| 6896 | const bool newHasWallpaper = newTargetFlags.test(InputTarget::Flags::FOREGROUND) && |
| 6897 | toWindowHandle->getInfo()->inputConfig.test( |
| 6898 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER); |
| 6899 | |
| 6900 | const sp<WindowInfoHandle> oldWallpaper = |
| 6901 | oldHasWallpaper ? state.getWallpaperWindow() : nullptr; |
| 6902 | const sp<WindowInfoHandle> newWallpaper = |
| 6903 | newHasWallpaper ? findWallpaperWindowBelow(toWindowHandle) : nullptr; |
| 6904 | if (oldWallpaper == newWallpaper) { |
| 6905 | return; |
| 6906 | } |
| 6907 | |
| 6908 | if (oldWallpaper != nullptr) { |
| 6909 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
| 6910 | "transferring touch focus to another window"); |
| 6911 | state.removeWindowByToken(oldWallpaper->getToken()); |
| 6912 | synthesizeCancelationEventsForWindowLocked(oldWallpaper, options); |
| 6913 | } |
| 6914 | |
| 6915 | if (newWallpaper != nullptr) { |
| 6916 | nsecs_t downTimeInTarget = now(); |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 6917 | ftl::Flags<InputTarget::Flags> wallpaperFlags = oldTargetFlags & InputTarget::Flags::SPLIT; |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6918 | wallpaperFlags |= InputTarget::Flags::WINDOW_IS_OBSCURED | |
| 6919 | InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 6920 | state.addOrUpdateWindow(newWallpaper, InputTarget::DispatchMode::AS_IS, wallpaperFlags, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 6921 | deviceId, pointers, downTimeInTarget); |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6922 | std::shared_ptr<Connection> wallpaperConnection = |
| 6923 | getConnectionLocked(newWallpaper->getToken()); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6924 | if (wallpaperConnection != nullptr) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6925 | std::shared_ptr<Connection> toConnection = |
| 6926 | getConnectionLocked(toWindowHandle->getToken()); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6927 | toConnection->inputState.mergePointerStateTo(wallpaperConnection->inputState); |
| 6928 | synthesizePointerDownEventsForConnectionLocked(downTimeInTarget, wallpaperConnection, |
| 6929 | wallpaperFlags); |
| 6930 | } |
| 6931 | } |
| 6932 | } |
| 6933 | |
| 6934 | sp<WindowInfoHandle> InputDispatcher::findWallpaperWindowBelow( |
| 6935 | const sp<WindowInfoHandle>& windowHandle) const { |
| 6936 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
| 6937 | getWindowHandlesLocked(windowHandle->getInfo()->displayId); |
| 6938 | bool foundWindow = false; |
| 6939 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
| 6940 | if (!foundWindow && otherHandle != windowHandle) { |
| 6941 | continue; |
| 6942 | } |
| 6943 | if (windowHandle == otherHandle) { |
| 6944 | foundWindow = true; |
| 6945 | continue; |
| 6946 | } |
| 6947 | |
| 6948 | if (otherHandle->getInfo()->inputConfig.test(WindowInfo::InputConfig::IS_WALLPAPER)) { |
| 6949 | return otherHandle; |
| 6950 | } |
| 6951 | } |
| 6952 | return nullptr; |
| 6953 | } |
| 6954 | |
Siarhei Vishniakou | fa2a049 | 2023-11-14 13:13:18 -0800 | [diff] [blame] | 6955 | void InputDispatcher::setKeyRepeatConfiguration(std::chrono::nanoseconds timeout, |
| 6956 | std::chrono::nanoseconds delay) { |
Nergi Rahardi | 730cf3c | 2023-04-13 12:41:17 +0900 | [diff] [blame] | 6957 | std::scoped_lock _l(mLock); |
| 6958 | |
Siarhei Vishniakou | fa2a049 | 2023-11-14 13:13:18 -0800 | [diff] [blame] | 6959 | mConfig.keyRepeatTimeout = timeout.count(); |
| 6960 | mConfig.keyRepeatDelay = delay.count(); |
Nergi Rahardi | 730cf3c | 2023-04-13 12:41:17 +0900 | [diff] [blame] | 6961 | } |
| 6962 | |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 6963 | bool InputDispatcher::isPointerInWindow(const sp<android::IBinder>& token, int32_t displayId, |
| 6964 | DeviceId deviceId, int32_t pointerId) { |
| 6965 | std::scoped_lock _l(mLock); |
| 6966 | auto touchStateIt = mTouchStatesByDisplay.find(displayId); |
| 6967 | if (touchStateIt == mTouchStatesByDisplay.end()) { |
| 6968 | return false; |
| 6969 | } |
| 6970 | for (const TouchedWindow& window : touchStateIt->second.windows) { |
| 6971 | if (window.windowHandle->getToken() == token && |
| 6972 | (window.hasTouchingPointer(deviceId, pointerId) || |
| 6973 | window.hasHoveringPointer(deviceId, pointerId))) { |
| 6974 | return true; |
| 6975 | } |
| 6976 | } |
| 6977 | return false; |
| 6978 | } |
| 6979 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 6980 | } // namespace android::inputdispatcher |