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) { |
| 731 | return target.inputChannel->getConnectionToken() == window.windowHandle->getToken(); |
| 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; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 873 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), /*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 |
| 993 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
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 | } |
| 1003 | const sp<WindowInfoHandle> window = |
| 1004 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1005 | if (window != nullptr) { |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 1006 | return window->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1007 | } |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 1008 | return DEFAULT_INPUT_DISPATCHING_TIMEOUT; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1009 | } |
| 1010 | |
Siarhei Vishniakou | 6950596 | 2023-12-28 12:07:04 -0800 | [diff] [blame] | 1011 | void InputDispatcher::dispatchOnceInnerLocked(nsecs_t& nextWakeupTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1012 | nsecs_t currentTime = now(); |
| 1013 | |
Jeff Brown | dc5992e | 2014-04-11 01:27:26 -0700 | [diff] [blame] | 1014 | // Reset the key repeat timer whenever normal dispatch is suspended while the |
| 1015 | // device is in a non-interactive state. This is to ensure that we abort a key |
| 1016 | // repeat if the device is just coming out of sleep. |
| 1017 | if (!mDispatchEnabled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1018 | resetKeyRepeatLocked(); |
| 1019 | } |
| 1020 | |
| 1021 | // If dispatching is frozen, do not process timeouts or try to deliver any new events. |
| 1022 | if (mDispatchFrozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1023 | if (DEBUG_FOCUS) { |
| 1024 | ALOGD("Dispatch frozen. Waiting some more."); |
| 1025 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1026 | return; |
| 1027 | } |
| 1028 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1029 | // Ready to start a new event. |
| 1030 | // If we don't already have a pending event, go grab one. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1031 | if (!mPendingEvent) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1032 | if (mInboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1033 | // Synthesize a key repeat if appropriate. |
| 1034 | if (mKeyRepeatState.lastKeyEntry) { |
| 1035 | if (currentTime >= mKeyRepeatState.nextRepeatTime) { |
| 1036 | mPendingEvent = synthesizeKeyRepeatLocked(currentTime); |
| 1037 | } else { |
Siarhei Vishniakou | 6950596 | 2023-12-28 12:07:04 -0800 | [diff] [blame] | 1038 | nextWakeupTime = std::min(nextWakeupTime, mKeyRepeatState.nextRepeatTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1039 | } |
| 1040 | } |
| 1041 | |
| 1042 | // Nothing to do if there is no pending event. |
| 1043 | if (!mPendingEvent) { |
| 1044 | return; |
| 1045 | } |
| 1046 | } else { |
| 1047 | // Inbound queue has at least one entry. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1048 | mPendingEvent = mInboundQueue.front(); |
| 1049 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1050 | traceInboundQueueLengthLocked(); |
| 1051 | } |
| 1052 | |
| 1053 | // Poke user activity for this event. |
| 1054 | if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1055 | pokeUserActivityLocked(*mPendingEvent); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1056 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | // Now we have an event to dispatch. |
| 1060 | // 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] | 1061 | ALOG_ASSERT(mPendingEvent != nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1062 | bool done = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1063 | DropReason dropReason = DropReason::NOT_DROPPED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1064 | if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1065 | dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1066 | } else if (!mDispatchEnabled) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1067 | dropReason = DropReason::DISABLED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1068 | } |
| 1069 | |
| 1070 | if (mNextUnblockedEvent == mPendingEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1071 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | switch (mPendingEvent->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1075 | case EventEntry::Type::CONFIGURATION_CHANGED: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1076 | const ConfigurationChangedEntry& typedEntry = |
| 1077 | static_cast<const ConfigurationChangedEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1078 | done = dispatchConfigurationChangedLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1079 | dropReason = DropReason::NOT_DROPPED; // configuration changes are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1080 | break; |
| 1081 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1082 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1083 | case EventEntry::Type::DEVICE_RESET: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1084 | const DeviceResetEntry& typedEntry = |
| 1085 | static_cast<const DeviceResetEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1086 | done = dispatchDeviceResetLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1087 | dropReason = DropReason::NOT_DROPPED; // device resets are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1088 | break; |
| 1089 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1090 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1091 | case EventEntry::Type::FOCUS: { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1092 | std::shared_ptr<const FocusEntry> typedEntry = |
| 1093 | std::static_pointer_cast<const FocusEntry>(mPendingEvent); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1094 | dispatchFocusLocked(currentTime, typedEntry); |
| 1095 | done = true; |
| 1096 | dropReason = DropReason::NOT_DROPPED; // focus events are never dropped |
| 1097 | break; |
| 1098 | } |
| 1099 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1100 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1101 | const auto typedEntry = std::static_pointer_cast<const TouchModeEntry>(mPendingEvent); |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1102 | dispatchTouchModeChangeLocked(currentTime, typedEntry); |
| 1103 | done = true; |
| 1104 | dropReason = DropReason::NOT_DROPPED; // touch mode events are never dropped |
| 1105 | break; |
| 1106 | } |
| 1107 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1108 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 1109 | const auto typedEntry = |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1110 | std::static_pointer_cast<const PointerCaptureChangedEntry>(mPendingEvent); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1111 | dispatchPointerCaptureChangedLocked(currentTime, typedEntry, dropReason); |
| 1112 | done = true; |
| 1113 | break; |
| 1114 | } |
| 1115 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1116 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1117 | std::shared_ptr<const DragEntry> typedEntry = |
| 1118 | std::static_pointer_cast<const DragEntry>(mPendingEvent); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1119 | dispatchDragLocked(currentTime, typedEntry); |
| 1120 | done = true; |
| 1121 | break; |
| 1122 | } |
| 1123 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1124 | case EventEntry::Type::KEY: { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1125 | std::shared_ptr<const KeyEntry> keyEntry = |
| 1126 | std::static_pointer_cast<const KeyEntry>(mPendingEvent); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1127 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *keyEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1128 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1129 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1130 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 1131 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1132 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1133 | done = dispatchKeyLocked(currentTime, keyEntry, &dropReason, nextWakeupTime); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 1134 | if (done && mTracer) { |
| 1135 | ensureEventTraced(*keyEntry); |
| 1136 | mTracer->eventProcessingComplete(*keyEntry->traceTracker); |
| 1137 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1138 | break; |
| 1139 | } |
| 1140 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1141 | case EventEntry::Type::MOTION: { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1142 | std::shared_ptr<const MotionEntry> motionEntry = |
| 1143 | std::static_pointer_cast<const MotionEntry>(mPendingEvent); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1144 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *motionEntry)) { |
Siarhei Vishniakou | 6b71b63 | 2023-10-27 21:34:46 -0700 | [diff] [blame] | 1145 | // The event is stale. However, only drop stale events if there isn't an ongoing |
| 1146 | // gesture. That would allow us to complete the processing of the current stroke. |
| 1147 | const auto touchStateIt = mTouchStatesByDisplay.find(motionEntry->displayId); |
| 1148 | if (touchStateIt != mTouchStatesByDisplay.end()) { |
| 1149 | const TouchState& touchState = touchStateIt->second; |
| 1150 | if (!touchState.hasTouchingPointers(motionEntry->deviceId) && |
| 1151 | !touchState.hasHoveringPointers(motionEntry->deviceId)) { |
| 1152 | dropReason = DropReason::STALE; |
| 1153 | } |
| 1154 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1155 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1156 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
Siarhei Vishniakou | 99e407b | 2023-12-26 18:09:32 -0800 | [diff] [blame] | 1157 | if (!isFromSource(motionEntry->source, AINPUT_SOURCE_CLASS_POINTER)) { |
| 1158 | // Only drop events that are focus-dispatched. |
| 1159 | dropReason = DropReason::BLOCKED; |
| 1160 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1161 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1162 | done = dispatchMotionLocked(currentTime, motionEntry, &dropReason, nextWakeupTime); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 1163 | if (done && mTracer) { |
| 1164 | ensureEventTraced(*motionEntry); |
| 1165 | mTracer->eventProcessingComplete(*motionEntry->traceTracker); |
| 1166 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1167 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1168 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1169 | |
| 1170 | case EventEntry::Type::SENSOR: { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1171 | std::shared_ptr<const SensorEntry> sensorEntry = |
| 1172 | std::static_pointer_cast<const SensorEntry>(mPendingEvent); |
Siarhei Vishniakou | e2404a1 | 2024-01-16 18:38:39 -0800 | [diff] [blame] | 1173 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1174 | // Sensor timestamps use SYSTEM_TIME_BOOTTIME time base, so we can't use |
| 1175 | // 'currentTime' here, get SYSTEM_TIME_BOOTTIME instead. |
| 1176 | nsecs_t bootTime = systemTime(SYSTEM_TIME_BOOTTIME); |
| 1177 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(bootTime, *sensorEntry)) { |
| 1178 | dropReason = DropReason::STALE; |
| 1179 | } |
| 1180 | dispatchSensorLocked(currentTime, sensorEntry, &dropReason, nextWakeupTime); |
| 1181 | done = true; |
| 1182 | break; |
| 1183 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1184 | } |
| 1185 | |
| 1186 | if (done) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1187 | if (dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1188 | dropInboundEventLocked(*mPendingEvent, dropReason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1189 | } |
Michael Wright | 3a98172 | 2015-06-10 15:26:13 +0100 | [diff] [blame] | 1190 | mLastDropReason = dropReason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1191 | |
| 1192 | releasePendingEventLocked(); |
Siarhei Vishniakou | 6950596 | 2023-12-28 12:07:04 -0800 | [diff] [blame] | 1193 | nextWakeupTime = LLONG_MIN; // force next poll to wake up immediately |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1194 | } |
| 1195 | } |
| 1196 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 1197 | bool InputDispatcher::isStaleEvent(nsecs_t currentTime, const EventEntry& entry) { |
Siarhei Vishniakou | a733311 | 2023-10-27 13:33:29 -0700 | [diff] [blame] | 1198 | return mPolicy.isStaleEvent(currentTime, entry.eventTime); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 1199 | } |
| 1200 | |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1201 | /** |
| 1202 | * Return true if the events preceding this incoming motion event should be dropped |
| 1203 | * Return false otherwise (the default behaviour) |
| 1204 | */ |
| 1205 | bool InputDispatcher::shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1206 | const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN && |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 1207 | isFromSource(motionEntry.source, AINPUT_SOURCE_CLASS_POINTER); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1208 | |
| 1209 | // Optimize case where the current application is unresponsive and the user |
| 1210 | // decides to touch a window in a different application. |
| 1211 | // If the application takes too long to catch up then we drop all events preceding |
| 1212 | // the touch into the other window. |
| 1213 | if (isPointerDownEvent && mAwaitedFocusedApplication != nullptr) { |
Siarhei Vishniakou | 9306c38 | 2022-09-30 15:30:31 -0700 | [diff] [blame] | 1214 | const int32_t displayId = motionEntry.displayId; |
| 1215 | const auto [x, y] = resolveTouchedPosition(motionEntry); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1216 | const bool isStylus = isPointerFromStylus(motionEntry, /*pointerIndex=*/0); |
Siarhei Vishniakou | 9306c38 | 2022-09-30 15:30:31 -0700 | [diff] [blame] | 1217 | |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 1218 | sp<WindowInfoHandle> touchedWindowHandle = |
| 1219 | findTouchedWindowAtLocked(displayId, x, y, isStylus); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1220 | if (touchedWindowHandle != nullptr && |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1221 | touchedWindowHandle->getApplicationToken() != |
| 1222 | mAwaitedFocusedApplication->getApplicationToken()) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1223 | // User touched a different application than the one we are waiting on. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1224 | ALOGI("Pruning input queue because user touched a different application while waiting " |
| 1225 | "for %s", |
| 1226 | mAwaitedFocusedApplication->getName().c_str()); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1227 | return true; |
| 1228 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1229 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1230 | // Alternatively, maybe there's a spy window that could handle this event. |
| 1231 | const std::vector<sp<WindowInfoHandle>> touchedSpies = |
| 1232 | findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus); |
| 1233 | for (const auto& windowHandle : touchedSpies) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 1234 | const std::shared_ptr<Connection> connection = |
| 1235 | getConnectionLocked(windowHandle->getToken()); |
Siarhei Vishniakou | 34ed4d4 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1236 | if (connection != nullptr && connection->responsive) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1237 | // This spy window could take more input. Drop all events preceding this |
| 1238 | // 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] | 1239 | 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] | 1240 | "responsive spy window that may handle the event.", |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1241 | mAwaitedFocusedApplication->getName().c_str()); |
| 1242 | return true; |
| 1243 | } |
| 1244 | } |
| 1245 | } |
| 1246 | |
| 1247 | // Prevent getting stuck: if we have a pending key event, and some motion events that have not |
| 1248 | // yet been processed by some connections, the dispatcher will wait for these motion |
| 1249 | // events to be processed before dispatching the key event. This is because these motion events |
| 1250 | // may cause a new window to be launched, which the user might expect to receive focus. |
| 1251 | // To prevent waiting forever for such events, just send the key to the currently focused window |
| 1252 | if (isPointerDownEvent && mKeyIsWaitingForEventsTimeout) { |
| 1253 | ALOGD("Received a new pointer down event, stop waiting for events to process and " |
| 1254 | "just send the pending key event to the focused window."); |
| 1255 | mKeyIsWaitingForEventsTimeout = now(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1256 | } |
| 1257 | return false; |
| 1258 | } |
| 1259 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1260 | bool InputDispatcher::enqueueInboundEventLocked(std::unique_ptr<EventEntry> newEntry) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1261 | bool needWake = mInboundQueue.empty(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1262 | mInboundQueue.push_back(std::move(newEntry)); |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1263 | const EventEntry& entry = *(mInboundQueue.back()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1264 | traceInboundQueueLengthLocked(); |
| 1265 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1266 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1267 | case EventEntry::Type::KEY: { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1268 | LOG_ALWAYS_FATAL_IF((entry.policyFlags & POLICY_FLAG_TRUSTED) == 0, |
| 1269 | "Unexpected untrusted event."); |
Siarhei Vishniakou | e2404a1 | 2024-01-16 18:38:39 -0800 | [diff] [blame] | 1270 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1271 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 1272 | if (mTracer) { |
| 1273 | ensureEventTraced(keyEntry); |
| 1274 | } |
Siarhei Vishniakou | 6520a58 | 2023-10-27 21:53:45 -0700 | [diff] [blame] | 1275 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 1276 | // If a new up event comes in, and the pending event with same key code has been asked |
| 1277 | // to try again later because of the policy. We have to reset the intercept key wake up |
| 1278 | // time for it may have been handled in the policy and could be dropped. |
| 1279 | if (keyEntry.action == AKEY_EVENT_ACTION_UP && mPendingEvent && |
| 1280 | mPendingEvent->type == EventEntry::Type::KEY) { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1281 | const KeyEntry& pendingKey = static_cast<const KeyEntry&>(*mPendingEvent); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 1282 | if (pendingKey.keyCode == keyEntry.keyCode && |
| 1283 | pendingKey.interceptKeyResult == |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1284 | KeyEntry::InterceptKeyResult::TRY_AGAIN_LATER) { |
| 1285 | pendingKey.interceptKeyResult = KeyEntry::InterceptKeyResult::UNKNOWN; |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 1286 | pendingKey.interceptKeyWakeupTime = 0; |
| 1287 | needWake = true; |
| 1288 | } |
| 1289 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1290 | break; |
| 1291 | } |
| 1292 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1293 | case EventEntry::Type::MOTION: { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1294 | LOG_ALWAYS_FATAL_IF((entry.policyFlags & POLICY_FLAG_TRUSTED) == 0, |
| 1295 | "Unexpected untrusted event."); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 1296 | const auto& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1297 | if (mTracer) { |
| 1298 | ensureEventTraced(motionEntry); |
| 1299 | } |
| 1300 | if (shouldPruneInboundQueueLocked(motionEntry)) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1301 | mNextUnblockedEvent = mInboundQueue.back(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1302 | needWake = true; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1303 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1304 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1305 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1306 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1307 | LOG_ALWAYS_FATAL("Focus events should be inserted using enqueueFocusEventLocked"); |
| 1308 | break; |
| 1309 | } |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1310 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1311 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1312 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1313 | case EventEntry::Type::SENSOR: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1314 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1315 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1316 | // nothing to do |
| 1317 | break; |
| 1318 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1319 | } |
| 1320 | |
| 1321 | return needWake; |
| 1322 | } |
| 1323 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1324 | void InputDispatcher::addRecentEventLocked(std::shared_ptr<const EventEntry> entry) { |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1325 | // Do not store sensor event in recent queue to avoid flooding the queue. |
| 1326 | if (entry->type != EventEntry::Type::SENSOR) { |
| 1327 | mRecentQueue.push_back(entry); |
| 1328 | } |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1329 | if (mRecentQueue.size() > RECENT_QUEUE_MAX_SIZE) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1330 | mRecentQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1331 | } |
| 1332 | } |
| 1333 | |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 1334 | sp<WindowInfoHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, float x, float y, |
| 1335 | bool isStylus, |
| 1336 | bool ignoreDragWindow) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1337 | // Traverse windows from front to back to find touched window. |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1338 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1339 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 1340 | if (ignoreDragWindow && haveSameToken(windowHandle, mDragState->dragWindow)) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1341 | continue; |
| 1342 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1343 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1344 | const WindowInfo& info = *windowHandle->getInfo(); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 1345 | if (!info.isSpy() && |
| 1346 | windowAcceptsTouchAt(info, displayId, x, y, isStylus, getTransformLocked(displayId))) { |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 1347 | return windowHandle; |
| 1348 | } |
| 1349 | } |
| 1350 | return nullptr; |
| 1351 | } |
| 1352 | |
| 1353 | std::vector<InputTarget> InputDispatcher::findOutsideTargetsLocked( |
Siarhei Vishniakou | 70f3d8c | 2023-09-19 15:36:52 -0700 | [diff] [blame] | 1354 | int32_t displayId, const sp<WindowInfoHandle>& touchedWindow, int32_t pointerId) const { |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 1355 | if (touchedWindow == nullptr) { |
| 1356 | return {}; |
| 1357 | } |
| 1358 | // Traverse windows from front to back until we encounter the touched window. |
| 1359 | std::vector<InputTarget> outsideTargets; |
| 1360 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
| 1361 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 1362 | if (windowHandle == touchedWindow) { |
| 1363 | // Stop iterating once we found a touched window. Any WATCH_OUTSIDE_TOUCH window |
| 1364 | // below the touched window will not get ACTION_OUTSIDE event. |
| 1365 | return outsideTargets; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1366 | } |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1367 | |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 1368 | const WindowInfo& info = *windowHandle->getInfo(); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1369 | if (info.inputConfig.test(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH)) { |
Siarhei Vishniakou | 70f3d8c | 2023-09-19 15:36:52 -0700 | [diff] [blame] | 1370 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
| 1371 | pointerIds.set(pointerId); |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 1372 | addPointerWindowTargetLocked(windowHandle, InputTarget::DispatchMode::OUTSIDE, |
| 1373 | ftl::Flags<InputTarget::Flags>(), pointerIds, |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 1374 | /*firstDownTimeInTarget=*/std::nullopt, outsideTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1375 | } |
| 1376 | } |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 1377 | return outsideTargets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1378 | } |
| 1379 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1380 | std::vector<sp<WindowInfoHandle>> InputDispatcher::findTouchedSpyWindowsAtLocked( |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 1381 | int32_t displayId, float x, float y, bool isStylus) const { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1382 | // Traverse windows from front to back and gather the touched spy windows. |
| 1383 | std::vector<sp<WindowInfoHandle>> spyWindows; |
| 1384 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
| 1385 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 1386 | const WindowInfo& info = *windowHandle->getInfo(); |
| 1387 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 1388 | if (!windowAcceptsTouchAt(info, displayId, x, y, isStylus, getTransformLocked(displayId))) { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1389 | continue; |
| 1390 | } |
| 1391 | if (!info.isSpy()) { |
| 1392 | // The first touched non-spy window was found, so return the spy windows touched so far. |
| 1393 | return spyWindows; |
| 1394 | } |
| 1395 | spyWindows.push_back(windowHandle); |
| 1396 | } |
| 1397 | return spyWindows; |
| 1398 | } |
| 1399 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1400 | void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1401 | const char* reason; |
| 1402 | switch (dropReason) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1403 | case DropReason::POLICY: |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 1404 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1405 | ALOGD("Dropped event because policy consumed it."); |
| 1406 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1407 | reason = "inbound event was dropped because the policy consumed it"; |
| 1408 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1409 | case DropReason::DISABLED: |
| 1410 | if (mLastDropReason != DropReason::DISABLED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1411 | ALOGI("Dropped event because input dispatch is disabled."); |
| 1412 | } |
| 1413 | reason = "inbound event was dropped because input dispatch is disabled"; |
| 1414 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1415 | case DropReason::BLOCKED: |
Siarhei Vishniakou | 99e407b | 2023-12-26 18:09:32 -0800 | [diff] [blame] | 1416 | LOG(INFO) << "Dropping because the current application is not responding and the user " |
| 1417 | "has started interacting with a different application: " |
| 1418 | << entry.getDescription(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1419 | reason = "inbound event was dropped because the current application is not responding " |
| 1420 | "and the user has started interacting with a different application"; |
| 1421 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1422 | case DropReason::STALE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1423 | ALOGI("Dropped event because it is stale."); |
| 1424 | reason = "inbound event was dropped because it is stale"; |
| 1425 | break; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1426 | case DropReason::NO_POINTER_CAPTURE: |
| 1427 | ALOGI("Dropped event because there is no window with Pointer Capture."); |
| 1428 | reason = "inbound event was dropped because there is no window with Pointer Capture"; |
| 1429 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1430 | case DropReason::NOT_DROPPED: { |
| 1431 | LOG_ALWAYS_FATAL("Should not be dropping a NOT_DROPPED event"); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1432 | return; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1433 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1434 | } |
| 1435 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1436 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1437 | case EventEntry::Type::KEY: { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1438 | CancelationOptions options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, reason); |
Hu Guo | 3cfa738 | 2023-11-15 09:50:04 +0000 | [diff] [blame] | 1439 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 1440 | options.displayId = keyEntry.displayId; |
| 1441 | options.deviceId = keyEntry.deviceId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1442 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1443 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1444 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1445 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1446 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1447 | if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1448 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, reason); |
Hu Guo | 3cfa738 | 2023-11-15 09:50:04 +0000 | [diff] [blame] | 1449 | options.displayId = motionEntry.displayId; |
| 1450 | options.deviceId = motionEntry.deviceId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1451 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1452 | } else { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1453 | CancelationOptions options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, |
| 1454 | reason); |
Hu Guo | 3cfa738 | 2023-11-15 09:50:04 +0000 | [diff] [blame] | 1455 | options.displayId = motionEntry.displayId; |
| 1456 | options.deviceId = motionEntry.deviceId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1457 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1458 | } |
| 1459 | break; |
| 1460 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1461 | case EventEntry::Type::SENSOR: { |
| 1462 | break; |
| 1463 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1464 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1465 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1466 | break; |
| 1467 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1468 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1469 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1470 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 1471 | case EventEntry::Type::DEVICE_RESET: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1472 | 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] | 1473 | break; |
| 1474 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1475 | } |
| 1476 | } |
| 1477 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1478 | bool InputDispatcher::haveCommandsLocked() const { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1479 | return !mCommandQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1480 | } |
| 1481 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1482 | bool InputDispatcher::runCommandsLockedInterruptable() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1483 | if (mCommandQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1484 | return false; |
| 1485 | } |
| 1486 | |
| 1487 | do { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1488 | auto command = std::move(mCommandQueue.front()); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1489 | mCommandQueue.pop_front(); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1490 | // Commands are run with the lock held, but may release and re-acquire the lock from within. |
| 1491 | command(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1492 | } while (!mCommandQueue.empty()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1493 | return true; |
| 1494 | } |
| 1495 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1496 | void InputDispatcher::postCommandLocked(Command&& command) { |
| 1497 | mCommandQueue.push_back(command); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1498 | } |
| 1499 | |
| 1500 | void InputDispatcher::drainInboundQueueLocked() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1501 | while (!mInboundQueue.empty()) { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1502 | std::shared_ptr<const EventEntry> entry = mInboundQueue.front(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1503 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1504 | releaseInboundEventLocked(entry); |
| 1505 | } |
| 1506 | traceInboundQueueLengthLocked(); |
| 1507 | } |
| 1508 | |
| 1509 | void InputDispatcher::releasePendingEventLocked() { |
| 1510 | if (mPendingEvent) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1511 | releaseInboundEventLocked(mPendingEvent); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1512 | mPendingEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1513 | } |
| 1514 | } |
| 1515 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1516 | void InputDispatcher::releaseInboundEventLocked(std::shared_ptr<const EventEntry> entry) { |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 1517 | const std::shared_ptr<InjectionState>& injectionState = entry->injectionState; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1518 | if (injectionState && injectionState->injectionResult == InputEventInjectionResult::PENDING) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1519 | if (DEBUG_DISPATCH_CYCLE) { |
| 1520 | ALOGD("Injected inbound event was dropped."); |
| 1521 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1522 | setInjectionResult(*entry, InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1523 | } |
| 1524 | if (entry == mNextUnblockedEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1525 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1526 | } |
| 1527 | addRecentEventLocked(entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1528 | } |
| 1529 | |
| 1530 | void InputDispatcher::resetKeyRepeatLocked() { |
| 1531 | if (mKeyRepeatState.lastKeyEntry) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1532 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1533 | } |
| 1534 | } |
| 1535 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1536 | std::shared_ptr<KeyEntry> InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1537 | std::shared_ptr<const KeyEntry> entry = mKeyRepeatState.lastKeyEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1538 | |
Michael Wright | 2e73295 | 2014-09-24 13:26:59 -0700 | [diff] [blame] | 1539 | uint32_t policyFlags = entry->policyFlags & |
| 1540 | (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1541 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1542 | std::shared_ptr<KeyEntry> newEntry = |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 1543 | std::make_unique<KeyEntry>(mIdGenerator.nextId(), /*injectionState=*/nullptr, |
| 1544 | currentTime, entry->deviceId, entry->source, |
| 1545 | entry->displayId, policyFlags, entry->action, entry->flags, |
| 1546 | entry->keyCode, entry->scanCode, entry->metaState, |
| 1547 | entry->repeatCount + 1, entry->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1548 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1549 | newEntry->syntheticRepeat = true; |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 1550 | if (mTracer) { |
| 1551 | newEntry->traceTracker = mTracer->traceInboundEvent(*newEntry); |
| 1552 | } |
| 1553 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1554 | mKeyRepeatState.lastKeyEntry = newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1555 | mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1556 | return newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1557 | } |
| 1558 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1559 | bool InputDispatcher::dispatchConfigurationChangedLocked(nsecs_t currentTime, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1560 | const ConfigurationChangedEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1561 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1562 | ALOGD("dispatchConfigurationChanged - eventTime=%" PRId64, entry.eventTime); |
| 1563 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1564 | |
| 1565 | // Reset key repeating in case a keyboard device was added or removed or something. |
| 1566 | resetKeyRepeatLocked(); |
| 1567 | |
| 1568 | // 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] | 1569 | auto command = [this, eventTime = entry.eventTime]() REQUIRES(mLock) { |
| 1570 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 1571 | mPolicy.notifyConfigurationChanged(eventTime); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1572 | }; |
| 1573 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1574 | return true; |
| 1575 | } |
| 1576 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1577 | bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime, |
| 1578 | const DeviceResetEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1579 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1580 | ALOGD("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry.eventTime, |
| 1581 | entry.deviceId); |
| 1582 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1583 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 1584 | // Reset key repeating in case a keyboard device was disabled or enabled. |
| 1585 | if (mKeyRepeatState.lastKeyEntry && mKeyRepeatState.lastKeyEntry->deviceId == entry.deviceId) { |
| 1586 | resetKeyRepeatLocked(); |
| 1587 | } |
| 1588 | |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1589 | CancelationOptions options(CancelationOptions::Mode::CANCEL_ALL_EVENTS, "device was reset"); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1590 | options.deviceId = entry.deviceId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1591 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 1592 | |
| 1593 | // Remove all active pointers from this device |
| 1594 | for (auto& [_, touchState] : mTouchStatesByDisplay) { |
| 1595 | touchState.removeAllPointersForDevice(entry.deviceId); |
| 1596 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1597 | return true; |
| 1598 | } |
| 1599 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1600 | void InputDispatcher::enqueueFocusEventLocked(const sp<IBinder>& windowToken, bool hasFocus, |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1601 | const std::string& reason) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1602 | if (mPendingEvent != nullptr) { |
| 1603 | // Move the pending event to the front of the queue. This will give the chance |
| 1604 | // for the pending event to get dispatched to the newly focused window |
| 1605 | mInboundQueue.push_front(mPendingEvent); |
| 1606 | mPendingEvent = nullptr; |
| 1607 | } |
| 1608 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1609 | std::unique_ptr<FocusEntry> focusEntry = |
| 1610 | std::make_unique<FocusEntry>(mIdGenerator.nextId(), now(), windowToken, hasFocus, |
| 1611 | reason); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1612 | |
| 1613 | // This event should go to the front of the queue, but behind all other focus events |
| 1614 | // Find the last focus event, and insert right after it |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1615 | auto it = std::find_if(mInboundQueue.rbegin(), mInboundQueue.rend(), |
| 1616 | [](const std::shared_ptr<const EventEntry>& event) { |
| 1617 | return event->type == EventEntry::Type::FOCUS; |
| 1618 | }); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1619 | |
| 1620 | // 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] | 1621 | mInboundQueue.insert(it.base(), std::move(focusEntry)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1622 | } |
| 1623 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1624 | void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime, |
| 1625 | std::shared_ptr<const FocusEntry> entry) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 1626 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1627 | if (channel == nullptr) { |
| 1628 | return; // Window has gone away |
| 1629 | } |
| 1630 | InputTarget target; |
| 1631 | target.inputChannel = channel; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1632 | entry->dispatchInProgress = true; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1633 | std::string message = std::string("Focus ") + (entry->hasFocus ? "entering " : "leaving ") + |
| 1634 | channel->getName(); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 1635 | std::string reason = std::string("reason=").append(entry->reason); |
| 1636 | android_log_event_list(LOGTAG_INPUT_FOCUS) << message << reason << LOG_ID_EVENTS; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1637 | dispatchEventLocked(currentTime, entry, {target}); |
| 1638 | } |
| 1639 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1640 | void InputDispatcher::dispatchPointerCaptureChangedLocked( |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1641 | nsecs_t currentTime, const std::shared_ptr<const PointerCaptureChangedEntry>& entry, |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1642 | DropReason& dropReason) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1643 | dropReason = DropReason::NOT_DROPPED; |
| 1644 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1645 | const bool haveWindowWithPointerCapture = mWindowTokenWithPointerCapture != nullptr; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1646 | sp<IBinder> token; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1647 | |
| 1648 | if (entry->pointerCaptureRequest.enable) { |
| 1649 | // Enable Pointer Capture. |
| 1650 | if (haveWindowWithPointerCapture && |
| 1651 | (entry->pointerCaptureRequest == mCurrentPointerCaptureRequest)) { |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 1652 | // This can happen if pointer capture is disabled and re-enabled before we notify the |
| 1653 | // app of the state change, so there is no need to notify the app. |
| 1654 | ALOGI("Skipping dispatch of Pointer Capture being enabled: no state change."); |
| 1655 | return; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1656 | } |
| 1657 | if (!mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1658 | // This can happen if a window requests capture and immediately releases capture. |
| 1659 | ALOGW("No window requested Pointer Capture."); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1660 | dropReason = DropReason::NO_POINTER_CAPTURE; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1661 | return; |
| 1662 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1663 | if (entry->pointerCaptureRequest.seq != mCurrentPointerCaptureRequest.seq) { |
| 1664 | ALOGI("Skipping dispatch of Pointer Capture being enabled: sequence number mismatch."); |
| 1665 | return; |
| 1666 | } |
| 1667 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1668 | token = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1669 | LOG_ALWAYS_FATAL_IF(!token, "Cannot find focused window for Pointer Capture."); |
| 1670 | mWindowTokenWithPointerCapture = token; |
| 1671 | } else { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1672 | // Disable Pointer Capture. |
| 1673 | // We do not check if the sequence number matches for requests to disable Pointer Capture |
| 1674 | // for two reasons: |
| 1675 | // 1. Pointer Capture can be disabled by a focus change, which means we can get two entries |
| 1676 | // to disable capture with the same sequence number: one generated by |
| 1677 | // disablePointerCaptureForcedLocked() and another as an acknowledgement of Pointer |
| 1678 | // Capture being disabled in InputReader. |
| 1679 | // 2. We respect any request to disable Pointer Capture generated by InputReader, since the |
| 1680 | // actual Pointer Capture state that affects events being generated by input devices is |
| 1681 | // in InputReader. |
| 1682 | if (!haveWindowWithPointerCapture) { |
| 1683 | // Pointer capture was already forcefully disabled because of focus change. |
| 1684 | dropReason = DropReason::NOT_DROPPED; |
| 1685 | return; |
| 1686 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1687 | token = mWindowTokenWithPointerCapture; |
| 1688 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1689 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1690 | setPointerCaptureLocked(false); |
| 1691 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1692 | } |
| 1693 | |
| 1694 | auto channel = getInputChannelLocked(token); |
| 1695 | if (channel == nullptr) { |
| 1696 | // Window has gone away, clean up Pointer Capture state. |
| 1697 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1698 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1699 | setPointerCaptureLocked(false); |
| 1700 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1701 | return; |
| 1702 | } |
| 1703 | InputTarget target; |
| 1704 | target.inputChannel = channel; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1705 | entry->dispatchInProgress = true; |
| 1706 | dispatchEventLocked(currentTime, entry, {target}); |
| 1707 | |
| 1708 | dropReason = DropReason::NOT_DROPPED; |
| 1709 | } |
| 1710 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1711 | void InputDispatcher::dispatchTouchModeChangeLocked( |
| 1712 | nsecs_t currentTime, const std::shared_ptr<const TouchModeEntry>& entry) { |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1713 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 1714 | getWindowHandlesLocked(entry->displayId); |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1715 | if (windowHandles.empty()) { |
| 1716 | return; |
| 1717 | } |
| 1718 | const std::vector<InputTarget> inputTargets = |
| 1719 | getInputTargetsFromWindowHandlesLocked(windowHandles); |
| 1720 | if (inputTargets.empty()) { |
| 1721 | return; |
| 1722 | } |
| 1723 | entry->dispatchInProgress = true; |
| 1724 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1725 | } |
| 1726 | |
| 1727 | std::vector<InputTarget> InputDispatcher::getInputTargetsFromWindowHandlesLocked( |
| 1728 | const std::vector<sp<WindowInfoHandle>>& windowHandles) const { |
| 1729 | std::vector<InputTarget> inputTargets; |
| 1730 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1731 | const sp<IBinder>& token = handle->getToken(); |
| 1732 | if (token == nullptr) { |
| 1733 | continue; |
| 1734 | } |
| 1735 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(token); |
| 1736 | if (channel == nullptr) { |
| 1737 | continue; // Window has gone away |
| 1738 | } |
| 1739 | InputTarget target; |
| 1740 | target.inputChannel = channel; |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1741 | inputTargets.push_back(target); |
| 1742 | } |
| 1743 | return inputTargets; |
| 1744 | } |
| 1745 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1746 | bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, std::shared_ptr<const KeyEntry> entry, |
Siarhei Vishniakou | 6950596 | 2023-12-28 12:07:04 -0800 | [diff] [blame] | 1747 | DropReason* dropReason, nsecs_t& nextWakeupTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1748 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1749 | if (!entry->dispatchInProgress) { |
| 1750 | if (entry->repeatCount == 0 && entry->action == AKEY_EVENT_ACTION_DOWN && |
| 1751 | (entry->policyFlags & POLICY_FLAG_TRUSTED) && |
| 1752 | (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { |
| 1753 | if (mKeyRepeatState.lastKeyEntry && |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1754 | mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode && |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1755 | // We have seen two identical key downs in a row which indicates that the device |
| 1756 | // driver is automatically generating key repeats itself. We take note of the |
| 1757 | // repeat here, but we disable our own next key repeat timer since it is clear that |
| 1758 | // we will not need to synthesize key repeats ourselves. |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1759 | mKeyRepeatState.lastKeyEntry->deviceId == entry->deviceId) { |
| 1760 | // Make sure we don't get key down from a different device. If a different |
| 1761 | // device Id has same key pressed down, the new device Id will replace the |
| 1762 | // current one to hold the key repeat with repeat count reset. |
| 1763 | // In the future when got a KEY_UP on the device id, drop it and do not |
| 1764 | // stop the key repeat on current device. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1765 | entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; |
| 1766 | resetKeyRepeatLocked(); |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 1767 | mKeyRepeatState.nextRepeatTime = LLONG_MAX; // don't generate repeats ourselves |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1768 | } else { |
| 1769 | // Not a repeat. Save key down state in case we do see a repeat later. |
| 1770 | resetKeyRepeatLocked(); |
| 1771 | mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; |
| 1772 | } |
| 1773 | mKeyRepeatState.lastKeyEntry = entry; |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1774 | } else if (entry->action == AKEY_EVENT_ACTION_UP && mKeyRepeatState.lastKeyEntry && |
| 1775 | mKeyRepeatState.lastKeyEntry->deviceId != entry->deviceId) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1776 | // 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] | 1777 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1778 | ALOGD("deviceId=%d got KEY_UP as stale", entry->deviceId); |
| 1779 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1780 | } else if (!entry->syntheticRepeat) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1781 | resetKeyRepeatLocked(); |
| 1782 | } |
| 1783 | |
| 1784 | if (entry->repeatCount == 1) { |
| 1785 | entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; |
| 1786 | } else { |
| 1787 | entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; |
| 1788 | } |
| 1789 | |
| 1790 | entry->dispatchInProgress = true; |
| 1791 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1792 | logOutboundKeyDetails("dispatchKey - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1793 | } |
| 1794 | |
| 1795 | // 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] | 1796 | if (entry->interceptKeyResult == KeyEntry::InterceptKeyResult::TRY_AGAIN_LATER) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1797 | if (currentTime < entry->interceptKeyWakeupTime) { |
Siarhei Vishniakou | 6950596 | 2023-12-28 12:07:04 -0800 | [diff] [blame] | 1798 | nextWakeupTime = std::min(nextWakeupTime, entry->interceptKeyWakeupTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1799 | return false; // wait until next wakeup |
| 1800 | } |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1801 | entry->interceptKeyResult = KeyEntry::InterceptKeyResult::UNKNOWN; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1802 | entry->interceptKeyWakeupTime = 0; |
| 1803 | } |
| 1804 | |
| 1805 | // Give the policy a chance to intercept the key. |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1806 | if (entry->interceptKeyResult == KeyEntry::InterceptKeyResult::UNKNOWN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1807 | if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1808 | sp<IBinder> focusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1809 | mFocusResolver.getFocusedWindowToken(getTargetDisplayId(*entry)); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1810 | |
| 1811 | auto command = [this, focusedWindowToken, entry]() REQUIRES(mLock) { |
| 1812 | doInterceptKeyBeforeDispatchingCommand(focusedWindowToken, *entry); |
| 1813 | }; |
| 1814 | postCommandLocked(std::move(command)); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1815 | // Poke user activity for keys not passed to user |
| 1816 | pokeUserActivityLocked(*entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1817 | return false; // wait for the command to run |
| 1818 | } else { |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1819 | entry->interceptKeyResult = KeyEntry::InterceptKeyResult::CONTINUE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1820 | } |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1821 | } else if (entry->interceptKeyResult == KeyEntry::InterceptKeyResult::SKIP) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1822 | if (*dropReason == DropReason::NOT_DROPPED) { |
| 1823 | *dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1824 | } |
| 1825 | } |
| 1826 | |
| 1827 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1828 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1829 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1830 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1831 | : InputEventInjectionResult::FAILED); |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1832 | mReporter->reportDroppedKey(entry->id); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1833 | // Poke user activity for undispatched keys |
| 1834 | pokeUserActivityLocked(*entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1835 | return true; |
| 1836 | } |
| 1837 | |
| 1838 | // Identify targets. |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1839 | InputEventInjectionResult injectionResult; |
| 1840 | sp<WindowInfoHandle> focusedWindow = |
| 1841 | findFocusedWindowTargetLocked(currentTime, *entry, nextWakeupTime, |
| 1842 | /*byref*/ injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1843 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1844 | return false; |
| 1845 | } |
| 1846 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1847 | setInjectionResult(*entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1848 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1849 | return true; |
| 1850 | } |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1851 | LOG_ALWAYS_FATAL_IF(focusedWindow == nullptr); |
| 1852 | |
| 1853 | std::vector<InputTarget> inputTargets; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 1854 | addWindowTargetLocked(focusedWindow, InputTarget::DispatchMode::AS_IS, |
| 1855 | InputTarget::Flags::FOREGROUND, getDownTime(*entry), inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1856 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1857 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1858 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1859 | |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 1860 | if (mTracer) { |
| 1861 | ensureEventTraced(*entry); |
| 1862 | for (const auto& target : inputTargets) { |
| 1863 | mTracer->dispatchToTargetHint(*entry->traceTracker, target); |
| 1864 | } |
| 1865 | } |
| 1866 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1867 | // Dispatch the key. |
| 1868 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1869 | return true; |
| 1870 | } |
| 1871 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1872 | void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1873 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1874 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 ", " |
| 1875 | "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, " |
| 1876 | "metaState=0x%x, repeatCount=%d, downTime=%" PRId64, |
| 1877 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, |
| 1878 | entry.policyFlags, entry.action, entry.flags, entry.keyCode, entry.scanCode, |
| 1879 | entry.metaState, entry.repeatCount, entry.downTime); |
| 1880 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1881 | } |
| 1882 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1883 | void InputDispatcher::dispatchSensorLocked(nsecs_t currentTime, |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1884 | const std::shared_ptr<const SensorEntry>& entry, |
Siarhei Vishniakou | 6950596 | 2023-12-28 12:07:04 -0800 | [diff] [blame] | 1885 | DropReason* dropReason, nsecs_t& nextWakeupTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1886 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1887 | ALOGD("notifySensorEvent eventTime=%" PRId64 ", hwTimestamp=%" PRId64 ", deviceId=%d, " |
| 1888 | "source=0x%x, sensorType=%s", |
| 1889 | entry->eventTime, entry->hwTimestamp, entry->deviceId, entry->source, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1890 | ftl::enum_string(entry->sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1891 | } |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1892 | auto command = [this, entry]() REQUIRES(mLock) { |
| 1893 | scoped_unlock unlock(mLock); |
| 1894 | |
| 1895 | if (entry->accuracyChanged) { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 1896 | mPolicy.notifySensorAccuracy(entry->deviceId, entry->sensorType, entry->accuracy); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1897 | } |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 1898 | mPolicy.notifySensorEvent(entry->deviceId, entry->sensorType, entry->accuracy, |
| 1899 | entry->hwTimestamp, entry->values); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1900 | }; |
| 1901 | postCommandLocked(std::move(command)); |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1902 | } |
| 1903 | |
| 1904 | bool InputDispatcher::flushSensor(int deviceId, InputDeviceSensorType sensorType) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1905 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1906 | ALOGD("flushSensor deviceId=%d, sensorType=%s", deviceId, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1907 | ftl::enum_string(sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1908 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1909 | { // acquire lock |
| 1910 | std::scoped_lock _l(mLock); |
| 1911 | |
| 1912 | for (auto it = mInboundQueue.begin(); it != mInboundQueue.end(); it++) { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1913 | std::shared_ptr<const EventEntry> entry = *it; |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1914 | if (entry->type == EventEntry::Type::SENSOR) { |
| 1915 | it = mInboundQueue.erase(it); |
| 1916 | releaseInboundEventLocked(entry); |
| 1917 | } |
| 1918 | } |
| 1919 | } |
| 1920 | return true; |
| 1921 | } |
| 1922 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1923 | bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, |
| 1924 | std::shared_ptr<const MotionEntry> entry, |
Siarhei Vishniakou | 6950596 | 2023-12-28 12:07:04 -0800 | [diff] [blame] | 1925 | DropReason* dropReason, nsecs_t& nextWakeupTime) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1926 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1927 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1928 | if (!entry->dispatchInProgress) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1929 | entry->dispatchInProgress = true; |
| 1930 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1931 | logOutboundMotionDetails("dispatchMotion - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1932 | } |
| 1933 | |
| 1934 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1935 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1936 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1937 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1938 | : InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1939 | return true; |
| 1940 | } |
| 1941 | |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 1942 | const bool isPointerEvent = isFromSource(entry->source, AINPUT_SOURCE_CLASS_POINTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1943 | |
| 1944 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1945 | std::vector<InputTarget> inputTargets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1946 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1947 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1948 | if (isPointerEvent) { |
| 1949 | // Pointer event. (eg. touchscreen) |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 1950 | |
| 1951 | if (mDragState && |
| 1952 | (entry->action & AMOTION_EVENT_ACTION_MASK) == AMOTION_EVENT_ACTION_POINTER_DOWN) { |
| 1953 | // If drag and drop ongoing and pointer down occur: pilfer drag window pointers |
| 1954 | pilferPointersLocked(mDragState->dragWindow->getToken()); |
| 1955 | } |
| 1956 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1957 | inputTargets = |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 1958 | findTouchedWindowTargetsLocked(currentTime, *entry, /*byref*/ injectionResult); |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 1959 | LOG_ALWAYS_FATAL_IF(injectionResult != InputEventInjectionResult::SUCCEEDED && |
| 1960 | !inputTargets.empty()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1961 | } else { |
| 1962 | // Non touch event. (eg. trackball) |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1963 | sp<WindowInfoHandle> focusedWindow = |
| 1964 | findFocusedWindowTargetLocked(currentTime, *entry, nextWakeupTime, injectionResult); |
| 1965 | if (injectionResult == InputEventInjectionResult::SUCCEEDED) { |
| 1966 | LOG_ALWAYS_FATAL_IF(focusedWindow == nullptr); |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 1967 | addWindowTargetLocked(focusedWindow, InputTarget::DispatchMode::AS_IS, |
| 1968 | InputTarget::Flags::FOREGROUND, getDownTime(*entry), |
| 1969 | inputTargets); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1970 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1971 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1972 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1973 | return false; |
| 1974 | } |
| 1975 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1976 | setInjectionResult(*entry, injectionResult); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1977 | if (injectionResult == InputEventInjectionResult::TARGET_MISMATCH) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1978 | return true; |
| 1979 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1980 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1981 | CancelationOptions::Mode mode( |
| 1982 | isPointerEvent ? CancelationOptions::Mode::CANCEL_POINTER_EVENTS |
| 1983 | : CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS); |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1984 | CancelationOptions options(mode, "input event injection failed"); |
| 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) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2017 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
| 2018 | if (channel == nullptr) { |
| 2019 | return; // Window has gone away |
| 2020 | } |
| 2021 | InputTarget target; |
| 2022 | target.inputChannel = channel; |
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 | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 2075 | std::shared_ptr<Connection> connection = |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 2076 | getConnectionLocked(inputTarget.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 2077 | if (connection != nullptr) { |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2078 | prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2079 | } else { |
Siarhei Vishniakou | 31dd155 | 2023-10-30 18:46:10 -0700 | [diff] [blame] | 2080 | if (DEBUG_DROPPED_EVENTS_VERBOSE) { |
| 2081 | LOG(INFO) << "Dropping event delivery to target with channel " |
| 2082 | << inputTarget.inputChannel->getName() |
| 2083 | << " because it is no longer registered with the input dispatcher."; |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2084 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2085 | } |
| 2086 | } |
| 2087 | } |
| 2088 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 2089 | void InputDispatcher::cancelEventsForAnrLocked(const std::shared_ptr<Connection>& connection) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2090 | // We will not be breaking any connections here, even if the policy wants us to abort dispatch. |
| 2091 | // If the policy decides to close the app, we will get a channel removal event via |
| 2092 | // unregisterInputChannel, and will clean up the connection that way. We are already not |
| 2093 | // sending new pointers to the connection when it blocked, but focused events will continue to |
| 2094 | // pile up. |
| 2095 | ALOGW("Canceling events for %s because it is unresponsive", |
| 2096 | connection->inputChannel->getName().c_str()); |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 2097 | if (connection->status == Connection::Status::NORMAL) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 2098 | CancelationOptions options(CancelationOptions::Mode::CANCEL_ALL_EVENTS, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2099 | "application not responding"); |
| 2100 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2101 | } |
| 2102 | } |
| 2103 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2104 | void InputDispatcher::resetNoFocusedWindowTimeoutLocked() { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2105 | if (DEBUG_FOCUS) { |
| 2106 | ALOGD("Resetting ANR timeouts."); |
| 2107 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2108 | |
| 2109 | // Reset input target wait timeout. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2110 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2111 | mAwaitedFocusedApplication.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2112 | } |
| 2113 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2114 | /** |
| 2115 | * Get the display id that the given event should go to. If this event specifies a valid display id, |
| 2116 | * then it should be dispatched to that display. Otherwise, the event goes to the focused display. |
| 2117 | * Focused display is the display that the user most recently interacted with. |
| 2118 | */ |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2119 | int32_t InputDispatcher::getTargetDisplayId(const EventEntry& entry) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2120 | int32_t displayId; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2121 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2122 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2123 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 2124 | displayId = keyEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2125 | break; |
| 2126 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2127 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2128 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 2129 | displayId = motionEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2130 | break; |
| 2131 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 2132 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 2133 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2134 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2135 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2136 | case EventEntry::Type::DEVICE_RESET: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2137 | case EventEntry::Type::SENSOR: |
| 2138 | case EventEntry::Type::DRAG: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2139 | 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] | 2140 | return ADISPLAY_ID_NONE; |
| 2141 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2142 | } |
| 2143 | return displayId == ADISPLAY_ID_NONE ? mFocusedDisplayId : displayId; |
| 2144 | } |
| 2145 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2146 | bool InputDispatcher::shouldWaitToSendKeyLocked(nsecs_t currentTime, |
| 2147 | const char* focusedWindowName) { |
| 2148 | if (mAnrTracker.empty()) { |
| 2149 | // already processed all events that we waited for |
| 2150 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 2151 | return false; |
| 2152 | } |
| 2153 | |
| 2154 | if (!mKeyIsWaitingForEventsTimeout.has_value()) { |
| 2155 | // Start the timer |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 2156 | // 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] | 2157 | mKeyIsWaitingForEventsTimeout = currentTime + |
| 2158 | std::chrono::duration_cast<std::chrono::nanoseconds>(KEY_WAITING_FOR_EVENTS_TIMEOUT) |
| 2159 | .count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2160 | return true; |
| 2161 | } |
| 2162 | |
| 2163 | // We still have pending events, and already started the timer |
| 2164 | if (currentTime < *mKeyIsWaitingForEventsTimeout) { |
| 2165 | return true; // Still waiting |
| 2166 | } |
| 2167 | |
| 2168 | // Waited too long, and some connection still hasn't processed all motions |
| 2169 | // Just send the key to the focused window |
| 2170 | ALOGW("Dispatching key to %s even though there are other unprocessed events", |
| 2171 | focusedWindowName); |
| 2172 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 2173 | return false; |
| 2174 | } |
| 2175 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2176 | sp<WindowInfoHandle> InputDispatcher::findFocusedWindowTargetLocked( |
Siarhei Vishniakou | 6950596 | 2023-12-28 12:07:04 -0800 | [diff] [blame] | 2177 | nsecs_t currentTime, const EventEntry& entry, nsecs_t& nextWakeupTime, |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2178 | InputEventInjectionResult& outInjectionResult) { |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2179 | outInjectionResult = InputEventInjectionResult::FAILED; // Default result |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2180 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2181 | int32_t displayId = getTargetDisplayId(entry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2182 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2183 | std::shared_ptr<InputApplicationHandle> focusedApplicationHandle = |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2184 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 2185 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2186 | // If there is no currently focused window and no focused application |
| 2187 | // then drop the event. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2188 | if (focusedWindowHandle == nullptr && focusedApplicationHandle == nullptr) { |
| 2189 | ALOGI("Dropping %s event because there is no focused window or focused application in " |
| 2190 | "display %" PRId32 ".", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2191 | ftl::enum_string(entry.type).c_str(), displayId); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2192 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2193 | } |
| 2194 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2195 | // Drop key events if requested by input feature |
| 2196 | if (focusedWindowHandle != nullptr && shouldDropInput(entry, focusedWindowHandle)) { |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2197 | return nullptr; |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2198 | } |
| 2199 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2200 | // Compatibility behavior: raise ANR if there is a focused application, but no focused window. |
| 2201 | // Only start counting when we have a focused event to dispatch. The ANR is canceled if we |
| 2202 | // start interacting with another application via touch (app switch). This code can be removed |
| 2203 | // if the "no focused window ANR" is moved to the policy. Input doesn't know whether |
| 2204 | // an app is expected to have a focused window. |
| 2205 | if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) { |
| 2206 | if (!mNoFocusedWindowTimeoutTime.has_value()) { |
| 2207 | // 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] | 2208 | std::chrono::nanoseconds timeout = focusedApplicationHandle->getDispatchingTimeout( |
| 2209 | DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
| 2210 | mNoFocusedWindowTimeoutTime = currentTime + timeout.count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2211 | mAwaitedFocusedApplication = focusedApplicationHandle; |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 2212 | mAwaitedApplicationDisplayId = displayId; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2213 | ALOGW("Waiting because no window has focus but %s may eventually add a " |
| 2214 | "window when it finishes starting up. Will wait for %" PRId64 "ms", |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 2215 | mAwaitedFocusedApplication->getName().c_str(), millis(timeout)); |
Siarhei Vishniakou | 6950596 | 2023-12-28 12:07:04 -0800 | [diff] [blame] | 2216 | nextWakeupTime = std::min(nextWakeupTime, *mNoFocusedWindowTimeoutTime); |
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 | } else if (currentTime > *mNoFocusedWindowTimeoutTime) { |
| 2220 | // Already raised ANR. Drop the event |
| 2221 | ALOGE("Dropping %s event because there is no focused window", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2222 | ftl::enum_string(entry.type).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2223 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2224 | } else { |
| 2225 | // Still waiting for the focused window |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2226 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 2227 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2228 | } |
| 2229 | } |
| 2230 | |
| 2231 | // we have a valid, non-null focused window |
| 2232 | resetNoFocusedWindowTimeoutLocked(); |
| 2233 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2234 | // Verify targeted injection. |
| 2235 | if (const auto err = verifyTargetedInjection(focusedWindowHandle, entry); err) { |
| 2236 | ALOGW("Dropping injected event: %s", (*err).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2237 | outInjectionResult = InputEventInjectionResult::TARGET_MISMATCH; |
| 2238 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2239 | } |
| 2240 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2241 | if (focusedWindowHandle->getInfo()->inputConfig.test( |
| 2242 | WindowInfo::InputConfig::PAUSE_DISPATCHING)) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2243 | ALOGI("Waiting because %s is paused", focusedWindowHandle->getName().c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2244 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 2245 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2246 | } |
| 2247 | |
| 2248 | // If the event is a key event, then we must wait for all previous events to |
| 2249 | // complete before delivering it because previous events may have the |
| 2250 | // side-effect of transferring focus to a different window and we want to |
| 2251 | // ensure that the following keys are sent to the new window. |
| 2252 | // |
| 2253 | // Suppose the user touches a button in a window then immediately presses "A". |
| 2254 | // If the button causes a pop-up window to appear then we want to ensure that |
| 2255 | // the "A" key is delivered to the new pop-up window. This is because users |
| 2256 | // often anticipate pending UI changes when typing on a keyboard. |
| 2257 | // To obtain this behavior, we must serialize key events with respect to all |
| 2258 | // prior input events. |
| 2259 | if (entry.type == EventEntry::Type::KEY) { |
| 2260 | if (shouldWaitToSendKeyLocked(currentTime, focusedWindowHandle->getName().c_str())) { |
Siarhei Vishniakou | 6950596 | 2023-12-28 12:07:04 -0800 | [diff] [blame] | 2261 | nextWakeupTime = std::min(nextWakeupTime, *mKeyIsWaitingForEventsTimeout); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2262 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 2263 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2264 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2265 | } |
| 2266 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2267 | outInjectionResult = InputEventInjectionResult::SUCCEEDED; |
| 2268 | return focusedWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2269 | } |
| 2270 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2271 | /** |
| 2272 | * Given a list of monitors, remove the ones we cannot find a connection for, and the ones |
| 2273 | * that are currently unresponsive. |
| 2274 | */ |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2275 | std::vector<Monitor> InputDispatcher::selectResponsiveMonitorsLocked( |
| 2276 | const std::vector<Monitor>& monitors) const { |
| 2277 | std::vector<Monitor> responsiveMonitors; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2278 | std::copy_if(monitors.begin(), monitors.end(), std::back_inserter(responsiveMonitors), |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2279 | [this](const Monitor& monitor) REQUIRES(mLock) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 2280 | std::shared_ptr<Connection> connection = |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2281 | getConnectionLocked(monitor.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2282 | if (connection == nullptr) { |
| 2283 | ALOGE("Could not find connection for monitor %s", |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2284 | monitor.inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2285 | return false; |
| 2286 | } |
| 2287 | if (!connection->responsive) { |
| 2288 | ALOGW("Unresponsive monitor %s will not get the new gesture", |
| 2289 | connection->inputChannel->getName().c_str()); |
| 2290 | return false; |
| 2291 | } |
| 2292 | return true; |
| 2293 | }); |
| 2294 | return responsiveMonitors; |
| 2295 | } |
| 2296 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2297 | std::vector<InputTarget> InputDispatcher::findTouchedWindowTargetsLocked( |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2298 | nsecs_t currentTime, const MotionEntry& entry, |
Siarhei Vishniakou | 4fe5739 | 2022-10-25 13:44:30 -0700 | [diff] [blame] | 2299 | InputEventInjectionResult& outInjectionResult) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2300 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2301 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2302 | std::vector<InputTarget> targets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2303 | // For security reasons, we defer updating the touch state until we are sure that |
| 2304 | // event injection will be allowed. |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2305 | const int32_t displayId = entry.displayId; |
| 2306 | const int32_t action = entry.action; |
Siarhei Vishniakou | bf88052 | 2023-05-01 11:03:22 -0700 | [diff] [blame] | 2307 | const int32_t maskedAction = MotionEvent::getActionMasked(action); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2308 | |
| 2309 | // 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] | 2310 | outInjectionResult = InputEventInjectionResult::PENDING; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2311 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2312 | // Copy current touch state into tempTouchState. |
| 2313 | // This state will be used to update mTouchStatesByDisplay at the end of this function. |
| 2314 | // 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] | 2315 | const TouchState* oldState = nullptr; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2316 | TouchState tempTouchState; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2317 | if (const auto it = mTouchStatesByDisplay.find(displayId); it != mTouchStatesByDisplay.end()) { |
| 2318 | oldState = &(it->second); |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 2319 | tempTouchState = *oldState; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2320 | } |
| 2321 | |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 2322 | bool isSplit = shouldSplitTouch(tempTouchState, entry); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2323 | |
| 2324 | const bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE || |
| 2325 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2326 | maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2327 | // A DOWN could be generated from POINTER_DOWN if the initial pointers did not land into any |
| 2328 | // touchable windows. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2329 | const bool wasDown = oldState != nullptr && oldState->isDown(entry.deviceId); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2330 | const bool isDown = (maskedAction == AMOTION_EVENT_ACTION_DOWN) || |
| 2331 | (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN && !wasDown); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2332 | const bool newGesture = isDown || maskedAction == AMOTION_EVENT_ACTION_SCROLL || |
| 2333 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2334 | maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE; |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 2335 | const bool isFromMouse = isFromSource(entry.source, AINPUT_SOURCE_MOUSE); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2336 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2337 | if (newGesture) { |
| 2338 | isSplit = false; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2339 | } |
| 2340 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2341 | if (isDown && tempTouchState.hasHoveringPointers(entry.deviceId)) { |
| 2342 | // Compatibility behaviour: ACTION_DOWN causes HOVER_EXIT to get generated. |
| 2343 | tempTouchState.clearHoveringPointers(entry.deviceId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2344 | } |
| 2345 | |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2346 | if (isHoverAction) { |
Siarhei Vishniakou | 7be50c9 | 2023-11-17 17:09:08 -0800 | [diff] [blame] | 2347 | if (wasDown) { |
| 2348 | // Started hovering, but the device is already down: reject the hover event |
| 2349 | LOG(ERROR) << "Got hover event " << entry.getDescription() |
| 2350 | << " but the device is already down " << oldState->dump(); |
| 2351 | outInjectionResult = InputEventInjectionResult::FAILED; |
| 2352 | return {}; |
| 2353 | } |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2354 | // For hover actions, we will treat 'tempTouchState' as a new state, so let's erase |
| 2355 | // all of the existing hovering pointers and recompute. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2356 | tempTouchState.clearHoveringPointers(entry.deviceId); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2357 | } |
| 2358 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2359 | if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { |
| 2360 | /* 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] | 2361 | const auto [x, y] = resolveTouchedPosition(entry); |
Siarhei Vishniakou | 5b9766d | 2023-07-18 14:06:29 -0700 | [diff] [blame] | 2362 | const int32_t pointerIndex = MotionEvent::getActionIndex(action); |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2363 | const PointerProperties& pointer = entry.pointerProperties[pointerIndex]; |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2364 | // Outside targets should be added upon first dispatched DOWN event. That means, this should |
| 2365 | // 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] | 2366 | const bool isStylus = isPointerFromStylus(entry, pointerIndex); |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 2367 | sp<WindowInfoHandle> newTouchedWindowHandle = |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2368 | findTouchedWindowAtLocked(displayId, x, y, isStylus); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2369 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2370 | if (isDown) { |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2371 | targets += findOutsideTargetsLocked(displayId, newTouchedWindowHandle, pointer.id); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2372 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2373 | // Handle the case where we did not find a window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2374 | if (newTouchedWindowHandle == nullptr) { |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 2375 | 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] | 2376 | // 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] | 2377 | newTouchedWindowHandle = tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2378 | } |
| 2379 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2380 | // Verify targeted injection. |
| 2381 | if (const auto err = verifyTargetedInjection(newTouchedWindowHandle, entry); err) { |
| 2382 | ALOGW("Dropping injected touch event: %s", (*err).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2383 | outInjectionResult = os::InputEventInjectionResult::TARGET_MISMATCH; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2384 | newTouchedWindowHandle = nullptr; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2385 | return {}; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2386 | } |
| 2387 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2388 | // Figure out whether splitting will be allowed for this window. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2389 | if (newTouchedWindowHandle != nullptr) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2390 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
| 2391 | // New window supports splitting, but we should never split mouse events. |
| 2392 | isSplit = !isFromMouse; |
| 2393 | } else if (isSplit) { |
| 2394 | // New window does not support splitting but we have already split events. |
| 2395 | // Ignore the new window. |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame] | 2396 | LOG(INFO) << "Skipping " << newTouchedWindowHandle->getName() |
| 2397 | << " because it doesn't support split touch"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2398 | newTouchedWindowHandle = nullptr; |
| 2399 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2400 | } else { |
| 2401 | // 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] | 2402 | // be delivered to a new window which supports split touch. Pointers from a mouse device |
| 2403 | // should never be split. |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 2404 | isSplit = !isFromMouse; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2405 | } |
| 2406 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2407 | std::vector<sp<WindowInfoHandle>> newTouchedWindows = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2408 | findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2409 | if (newTouchedWindowHandle != nullptr) { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2410 | // Process the foreground window first so that it is the first to receive the event. |
| 2411 | newTouchedWindows.insert(newTouchedWindows.begin(), newTouchedWindowHandle); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2412 | } |
| 2413 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2414 | if (newTouchedWindows.empty()) { |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 2415 | ALOGI("Dropping event because there is no touchable window at (%.1f, %.1f) on display " |
| 2416 | "%d.", |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2417 | x, y, displayId); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2418 | outInjectionResult = InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2419 | return {}; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2420 | } |
| 2421 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2422 | for (const sp<WindowInfoHandle>& windowHandle : newTouchedWindows) { |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 2423 | if (!canWindowReceiveMotionLocked(windowHandle, entry)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2424 | continue; |
| 2425 | } |
| 2426 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2427 | if (isHoverAction) { |
Siarhei Vishniakou | b681c20 | 2023-05-01 11:22:33 -0700 | [diff] [blame] | 2428 | // The "windowHandle" is the target of this hovering pointer. |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2429 | tempTouchState.addHoveringPointerToWindow(windowHandle, entry.deviceId, pointer); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2430 | } |
| 2431 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2432 | // Set target flags. |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2433 | ftl::Flags<InputTarget::Flags> targetFlags; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2434 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2435 | if (canReceiveForegroundTouches(*windowHandle->getInfo())) { |
| 2436 | // 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] | 2437 | targetFlags |= InputTarget::Flags::FOREGROUND; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2438 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2439 | |
| 2440 | if (isSplit) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2441 | targetFlags |= InputTarget::Flags::SPLIT; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2442 | } |
| 2443 | if (isWindowObscuredAtPointLocked(windowHandle, x, y)) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2444 | targetFlags |= InputTarget::Flags::WINDOW_IS_OBSCURED; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2445 | } else if (isWindowObscuredLocked(windowHandle)) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2446 | targetFlags |= InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2447 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2448 | |
| 2449 | // Update the temporary touch state. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2450 | |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2451 | if (!isHoverAction) { |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2452 | const bool isDownOrPointerDown = maskedAction == AMOTION_EVENT_ACTION_DOWN || |
| 2453 | maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2454 | tempTouchState.addOrUpdateWindow(windowHandle, InputTarget::DispatchMode::AS_IS, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2455 | targetFlags, entry.deviceId, {pointer}, |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2456 | isDownOrPointerDown |
| 2457 | ? std::make_optional(entry.eventTime) |
| 2458 | : std::nullopt); |
| 2459 | // If this is the pointer going down and the touched window has a wallpaper |
| 2460 | // then also add the touched wallpaper windows so they are locked in for the |
| 2461 | // duration of the touch gesture. We do not collect wallpapers during HOVER_MOVE or |
| 2462 | // SCROLL because the wallpaper engine only supports touch events. We would need to |
| 2463 | // add a mechanism similar to View.onGenericMotionEvent to enable wallpapers to |
| 2464 | // handle these events. |
| 2465 | if (isDownOrPointerDown && targetFlags.test(InputTarget::Flags::FOREGROUND) && |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2466 | windowHandle->getInfo()->inputConfig.test( |
| 2467 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER)) { |
| 2468 | sp<WindowInfoHandle> wallpaper = findWallpaperWindowBelow(windowHandle); |
| 2469 | if (wallpaper != nullptr) { |
| 2470 | ftl::Flags<InputTarget::Flags> wallpaperFlags = |
| 2471 | InputTarget::Flags::WINDOW_IS_OBSCURED | |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2472 | InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED; |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2473 | if (isSplit) { |
| 2474 | wallpaperFlags |= InputTarget::Flags::SPLIT; |
| 2475 | } |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2476 | tempTouchState.addOrUpdateWindow(wallpaper, |
| 2477 | InputTarget::DispatchMode::AS_IS, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2478 | wallpaperFlags, entry.deviceId, {pointer}, |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2479 | entry.eventTime); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2480 | } |
| 2481 | } |
| 2482 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2483 | } |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 2484 | |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 2485 | // If a window is already pilfering some pointers, give it this new pointer as well and |
| 2486 | // make it pilfering. This will prevent other non-spy windows from getting this pointer, |
| 2487 | // which is a specific behaviour that we want. |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 2488 | for (TouchedWindow& touchedWindow : tempTouchState.windows) { |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2489 | if (touchedWindow.hasTouchingPointer(entry.deviceId, pointer.id) && |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 2490 | touchedWindow.hasPilferingPointers(entry.deviceId)) { |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 2491 | // This window is already pilfering some pointers, and this new pointer is also |
| 2492 | // going to it. Therefore, take over this pointer and don't give it to anyone |
| 2493 | // else. |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2494 | touchedWindow.addPilferingPointer(entry.deviceId, pointer.id); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 2495 | } |
| 2496 | } |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 2497 | |
| 2498 | // Restrict all pilfered pointers to the pilfering windows. |
| 2499 | tempTouchState.cancelPointersForNonPilferingWindows(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2500 | } else { |
| 2501 | /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ |
| 2502 | |
| 2503 | // If the pointer is not currently down, then ignore the event. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2504 | if (!tempTouchState.isDown(entry.deviceId) && |
| 2505 | maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT) { |
Siarhei Vishniakou | 31dd155 | 2023-10-30 18:46:10 -0700 | [diff] [blame] | 2506 | if (DEBUG_DROPPED_EVENTS_VERBOSE) { |
| 2507 | LOG(INFO) << "Dropping event because the pointer for device " << entry.deviceId |
| 2508 | << " is not down or we previously dropped the pointer down event in " |
| 2509 | << "display " << displayId << ": " << entry.getDescription(); |
| 2510 | } |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2511 | outInjectionResult = InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2512 | return {}; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2513 | } |
| 2514 | |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2515 | // If the pointer is not currently hovering, then ignore the event. |
| 2516 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 2517 | const int32_t pointerId = entry.pointerProperties[0].id; |
| 2518 | if (oldState == nullptr || |
| 2519 | oldState->getWindowsWithHoveringPointer(entry.deviceId, pointerId).empty()) { |
| 2520 | LOG(INFO) << "Dropping event because the hovering pointer is not in any windows in " |
| 2521 | "display " |
| 2522 | << displayId << ": " << entry.getDescription(); |
| 2523 | outInjectionResult = InputEventInjectionResult::FAILED; |
| 2524 | return {}; |
| 2525 | } |
| 2526 | tempTouchState.removeHoveringPointer(entry.deviceId, pointerId); |
| 2527 | } |
| 2528 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2529 | addDragEventLocked(entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2530 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2531 | // Check whether touches should slip outside of the current foreground window. |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 2532 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.getPointerCount() == 1 && |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2533 | tempTouchState.isSlippery()) { |
Siarhei Vishniakou | 9306c38 | 2022-09-30 15:30:31 -0700 | [diff] [blame] | 2534 | const auto [x, y] = resolveTouchedPosition(entry); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2535 | const bool isStylus = isPointerFromStylus(entry, /*pointerIndex=*/0); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2536 | sp<WindowInfoHandle> oldTouchedWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2537 | tempTouchState.getFirstForegroundWindowHandle(); |
Siarhei Vishniakou | 0f6558d | 2023-04-21 12:05:13 -0700 | [diff] [blame] | 2538 | LOG_ALWAYS_FATAL_IF(oldTouchedWindowHandle == nullptr); |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 2539 | sp<WindowInfoHandle> newTouchedWindowHandle = |
| 2540 | findTouchedWindowAtLocked(displayId, x, y, isStylus); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2541 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2542 | // Verify targeted injection. |
| 2543 | if (const auto err = verifyTargetedInjection(newTouchedWindowHandle, entry); err) { |
| 2544 | ALOGW("Dropping injected event: %s", (*err).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2545 | outInjectionResult = os::InputEventInjectionResult::TARGET_MISMATCH; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2546 | return {}; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2547 | } |
| 2548 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2549 | // Drop touch events if requested by input feature |
| 2550 | if (newTouchedWindowHandle != nullptr && |
| 2551 | shouldDropInput(entry, newTouchedWindowHandle)) { |
| 2552 | newTouchedWindowHandle = nullptr; |
| 2553 | } |
| 2554 | |
Siarhei Vishniakou | afa08cc | 2023-05-08 22:35:50 -0700 | [diff] [blame] | 2555 | if (newTouchedWindowHandle != nullptr && |
| 2556 | !haveSameToken(oldTouchedWindowHandle, newTouchedWindowHandle)) { |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2557 | 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] | 2558 | oldTouchedWindowHandle->getName().c_str(), |
| 2559 | newTouchedWindowHandle->getName().c_str(), displayId); |
| 2560 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2561 | // Make a slippery exit from the old window. |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 2562 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2563 | const PointerProperties& pointer = entry.pointerProperties[0]; |
| 2564 | pointerIds.set(pointer.id); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2565 | |
| 2566 | const TouchedWindow& touchedWindow = |
| 2567 | tempTouchState.getTouchedWindow(oldTouchedWindowHandle); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2568 | addPointerWindowTargetLocked(oldTouchedWindowHandle, |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2569 | InputTarget::DispatchMode::SLIPPERY_EXIT, |
| 2570 | ftl::Flags<InputTarget::Flags>(), pointerIds, |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2571 | touchedWindow.getDownTimeInTarget(entry.deviceId), |
| 2572 | targets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2573 | |
| 2574 | // Make a slippery entrance into the new window. |
| 2575 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
Prabir Pradhan | 713bb3e | 2021-12-20 02:07:40 -0800 | [diff] [blame] | 2576 | isSplit = !isFromMouse; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2577 | } |
| 2578 | |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2579 | ftl::Flags<InputTarget::Flags> targetFlags; |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2580 | if (canReceiveForegroundTouches(*newTouchedWindowHandle->getInfo())) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2581 | targetFlags |= InputTarget::Flags::FOREGROUND; |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2582 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2583 | if (isSplit) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2584 | targetFlags |= InputTarget::Flags::SPLIT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2585 | } |
| 2586 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2587 | targetFlags |= InputTarget::Flags::WINDOW_IS_OBSCURED; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2588 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2589 | targetFlags |= InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2590 | } |
| 2591 | |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2592 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, |
| 2593 | InputTarget::DispatchMode::SLIPPERY_ENTER, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2594 | targetFlags, entry.deviceId, {pointer}, |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2595 | entry.eventTime); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2596 | |
| 2597 | // Check if the wallpaper window should deliver the corresponding event. |
| 2598 | slipWallpaperTouch(targetFlags, oldTouchedWindowHandle, newTouchedWindowHandle, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2599 | tempTouchState, entry.deviceId, pointer, targets); |
| 2600 | tempTouchState.removeTouchingPointerFromWindow(entry.deviceId, pointer.id, |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 2601 | oldTouchedWindowHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2602 | } |
| 2603 | } |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 2604 | |
| 2605 | // Update the pointerIds for non-splittable when it received pointer down. |
| 2606 | if (!isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN) { |
| 2607 | // If no split, we suppose all touched windows should receive pointer down. |
Siarhei Vishniakou | 5b9766d | 2023-07-18 14:06:29 -0700 | [diff] [blame] | 2608 | const int32_t pointerIndex = MotionEvent::getActionIndex(action); |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2609 | std::vector<PointerProperties> touchingPointers{entry.pointerProperties[pointerIndex]}; |
| 2610 | for (TouchedWindow& touchedWindow : tempTouchState.windows) { |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 2611 | // Ignore drag window for it should just track one pointer. |
| 2612 | if (mDragState && mDragState->dragWindow == touchedWindow.windowHandle) { |
| 2613 | continue; |
| 2614 | } |
Siarhei Vishniakou | 5b9766d | 2023-07-18 14:06:29 -0700 | [diff] [blame] | 2615 | touchedWindow.addTouchingPointers(entry.deviceId, touchingPointers); |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 2616 | } |
| 2617 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2618 | } |
| 2619 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2620 | // Update dispatching for hover enter and exit. |
Sam Dubey | f886dec | 2023-01-27 13:28:19 +0000 | [diff] [blame] | 2621 | { |
| 2622 | std::vector<TouchedWindow> hoveringWindows = |
| 2623 | getHoveringWindowsLocked(oldState, tempTouchState, entry); |
| 2624 | for (const TouchedWindow& touchedWindow : hoveringWindows) { |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 2625 | std::optional<InputTarget> target = |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2626 | createInputTargetLocked(touchedWindow.windowHandle, touchedWindow.dispatchMode, |
| 2627 | touchedWindow.targetFlags, |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 2628 | touchedWindow.getDownTimeInTarget(entry.deviceId)); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 2629 | if (!target) { |
| 2630 | continue; |
| 2631 | } |
| 2632 | // Hardcode to single hovering pointer for now. |
| 2633 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
| 2634 | pointerIds.set(entry.pointerProperties[0].id); |
| 2635 | target->addPointers(pointerIds, touchedWindow.windowHandle->getInfo()->transform); |
| 2636 | targets.push_back(*target); |
Sam Dubey | f886dec | 2023-01-27 13:28:19 +0000 | [diff] [blame] | 2637 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2638 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2639 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2640 | // Ensure that all touched windows are valid for injection. |
| 2641 | if (entry.injectionState != nullptr) { |
| 2642 | std::string errs; |
| 2643 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2644 | const auto err = verifyTargetedInjection(touchedWindow.windowHandle, entry); |
| 2645 | if (err) errs += "\n - " + *err; |
| 2646 | } |
| 2647 | if (!errs.empty()) { |
| 2648 | 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] | 2649 | "%s:%s", |
| 2650 | entry.injectionState->targetUid->toString().c_str(), errs.c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2651 | outInjectionResult = InputEventInjectionResult::TARGET_MISMATCH; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2652 | return {}; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2653 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2654 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2655 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2656 | // Check whether windows listening for outside touches are owned by the same UID. If the owner |
| 2657 | // 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] | 2658 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2659 | sp<WindowInfoHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2660 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2661 | if (foregroundWindowHandle) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 2662 | const auto foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2663 | for (InputTarget& target : targets) { |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2664 | if (target.dispatchMode == InputTarget::DispatchMode::OUTSIDE) { |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2665 | sp<WindowInfoHandle> targetWindow = |
| 2666 | getWindowHandleLocked(target.inputChannel->getConnectionToken()); |
| 2667 | if (targetWindow->getInfo()->ownerUid != foregroundWindowUid) { |
| 2668 | target.flags |= InputTarget::Flags::ZERO_COORDS; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2669 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2670 | } |
| 2671 | } |
| 2672 | } |
| 2673 | } |
| 2674 | |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 2675 | // If this is a touchpad navigation gesture, it needs to only be sent to trusted targets, as we |
| 2676 | // only want the system UI to handle these gestures. |
| 2677 | const bool isTouchpadNavGesture = isFromSource(entry.source, AINPUT_SOURCE_MOUSE) && |
| 2678 | entry.classification == MotionClassification::MULTI_FINGER_SWIPE; |
| 2679 | if (isTouchpadNavGesture) { |
| 2680 | filterUntrustedTargets(/* byref */ tempTouchState, /* byref */ targets); |
| 2681 | } |
| 2682 | |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2683 | // Output targets from the touch state. |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2684 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2685 | std::vector<PointerProperties> touchingPointers = |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2686 | touchedWindow.getTouchingPointers(entry.deviceId); |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2687 | if (touchingPointers.empty()) { |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2688 | continue; |
| 2689 | } |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2690 | addPointerWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.dispatchMode, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 2691 | touchedWindow.targetFlags, getPointerIds(touchingPointers), |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2692 | touchedWindow.getDownTimeInTarget(entry.deviceId), targets); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2693 | } |
Sam Dubey | 39d37cf | 2022-12-07 18:05:35 +0000 | [diff] [blame] | 2694 | |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 2695 | // During targeted injection, only allow owned targets to receive events |
| 2696 | std::erase_if(targets, [&](const InputTarget& target) { |
| 2697 | LOG_ALWAYS_FATAL_IF(target.windowHandle == nullptr); |
| 2698 | const auto err = verifyTargetedInjection(target.windowHandle, entry); |
| 2699 | if (err) { |
| 2700 | LOG(WARNING) << "Dropping injected event from " << target.windowHandle->getName() |
| 2701 | << ": " << (*err); |
| 2702 | return true; |
| 2703 | } |
| 2704 | return false; |
| 2705 | }); |
| 2706 | |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2707 | if (targets.empty()) { |
| 2708 | LOG(INFO) << "Dropping event because no targets were found: " << entry.getDescription(); |
| 2709 | outInjectionResult = InputEventInjectionResult::FAILED; |
| 2710 | return {}; |
| 2711 | } |
| 2712 | |
| 2713 | // If we only have windows getting ACTION_OUTSIDE, then drop the event, because there is no |
| 2714 | // window that is actually receiving the entire gesture. |
| 2715 | if (std::all_of(targets.begin(), targets.end(), [](const InputTarget& target) { |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2716 | return target.dispatchMode == InputTarget::DispatchMode::OUTSIDE; |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2717 | })) { |
| 2718 | LOG(INFO) << "Dropping event because all windows would just receive ACTION_OUTSIDE: " |
| 2719 | << entry.getDescription(); |
| 2720 | outInjectionResult = InputEventInjectionResult::FAILED; |
| 2721 | return {}; |
| 2722 | } |
| 2723 | |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2724 | outInjectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2725 | |
Prabir Pradhan | 502a725 | 2023-12-01 16:11:24 +0000 | [diff] [blame] | 2726 | // Now that we have generated all of the input targets for this event, reset the dispatch |
| 2727 | // mode for all touched window to AS_IS. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2728 | for (TouchedWindow& touchedWindow : tempTouchState.windows) { |
Prabir Pradhan | 502a725 | 2023-12-01 16:11:24 +0000 | [diff] [blame] | 2729 | touchedWindow.dispatchMode = InputTarget::DispatchMode::AS_IS; |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2730 | } |
| 2731 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2732 | // Update final pieces of touch state if the injector had permission. |
Siarhei Vishniakou | 7be50c9 | 2023-11-17 17:09:08 -0800 | [diff] [blame] | 2733 | if (maskedAction == AMOTION_EVENT_ACTION_UP) { |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2734 | // Pointer went up. |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 2735 | tempTouchState.removeTouchingPointer(entry.deviceId, entry.pointerProperties[0].id); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2736 | } else if (maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2737 | // All pointers up or canceled. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2738 | tempTouchState.removeAllPointersForDevice(entry.deviceId); |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2739 | } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 2740 | // One pointer went up. |
Siarhei Vishniakou | 5b9766d | 2023-07-18 14:06:29 -0700 | [diff] [blame] | 2741 | const int32_t pointerIndex = MotionEvent::getActionIndex(action); |
| 2742 | const uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
| 2743 | tempTouchState.removeTouchingPointer(entry.deviceId, pointerId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2744 | } |
| 2745 | |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2746 | // Save changes unless the action was scroll in which case the temporary touch |
| 2747 | // state was only valid for this one action. |
| 2748 | if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) { |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 2749 | if (displayId >= 0) { |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2750 | tempTouchState.clearWindowsWithoutPointers(); |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2751 | mTouchStatesByDisplay[displayId] = tempTouchState; |
| 2752 | } else { |
| 2753 | mTouchStatesByDisplay.erase(displayId); |
| 2754 | } |
| 2755 | } |
| 2756 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 2757 | if (tempTouchState.windows.empty()) { |
| 2758 | mTouchStatesByDisplay.erase(displayId); |
| 2759 | } |
| 2760 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2761 | return targets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2762 | } |
| 2763 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2764 | void InputDispatcher::finishDragAndDrop(int32_t displayId, float x, float y) { |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2765 | // Prevent stylus interceptor windows from affecting drag and drop behavior for now, until we |
| 2766 | // have an explicit reason to support it. |
| 2767 | constexpr bool isStylus = false; |
| 2768 | |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 2769 | sp<WindowInfoHandle> dropWindow = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2770 | findTouchedWindowAtLocked(displayId, x, y, isStylus, /*ignoreDragWindow=*/true); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2771 | if (dropWindow) { |
| 2772 | vec2 local = dropWindow->getInfo()->transform.transform(x, y); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2773 | sendDropWindowCommandLocked(dropWindow->getToken(), local.x, local.y); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 2774 | } else { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2775 | ALOGW("No window found when drop."); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2776 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2777 | } |
| 2778 | mDragState.reset(); |
| 2779 | } |
| 2780 | |
| 2781 | void InputDispatcher::addDragEventLocked(const MotionEntry& entry) { |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 2782 | if (!mDragState || mDragState->dragWindow->getInfo()->displayId != entry.displayId) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2783 | return; |
| 2784 | } |
| 2785 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2786 | if (!mDragState->isStartDrag) { |
| 2787 | mDragState->isStartDrag = true; |
| 2788 | mDragState->isStylusButtonDownAtStart = |
| 2789 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2790 | } |
| 2791 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2792 | // Find the pointer index by id. |
| 2793 | int32_t pointerIndex = 0; |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 2794 | for (; static_cast<uint32_t>(pointerIndex) < entry.getPointerCount(); pointerIndex++) { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2795 | const PointerProperties& pointerProperties = entry.pointerProperties[pointerIndex]; |
| 2796 | if (pointerProperties.id == mDragState->pointerId) { |
| 2797 | break; |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2798 | } |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2799 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2800 | |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 2801 | if (uint32_t(pointerIndex) == entry.getPointerCount()) { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2802 | 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] | 2803 | } |
| 2804 | |
| 2805 | const int32_t maskedAction = entry.action & AMOTION_EVENT_ACTION_MASK; |
| 2806 | const int32_t x = entry.pointerCoords[pointerIndex].getX(); |
| 2807 | const int32_t y = entry.pointerCoords[pointerIndex].getY(); |
| 2808 | |
| 2809 | switch (maskedAction) { |
| 2810 | case AMOTION_EVENT_ACTION_MOVE: { |
| 2811 | // Handle the special case : stylus button no longer pressed. |
| 2812 | bool isStylusButtonDown = |
| 2813 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2814 | if (mDragState->isStylusButtonDownAtStart && !isStylusButtonDown) { |
| 2815 | finishDragAndDrop(entry.displayId, x, y); |
| 2816 | return; |
| 2817 | } |
| 2818 | |
| 2819 | // Prevent stylus interceptor windows from affecting drag and drop behavior for now, |
| 2820 | // until we have an explicit reason to support it. |
| 2821 | constexpr bool isStylus = false; |
| 2822 | |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 2823 | sp<WindowInfoHandle> hoverWindowHandle = |
| 2824 | findTouchedWindowAtLocked(entry.displayId, x, y, isStylus, |
| 2825 | /*ignoreDragWindow=*/true); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2826 | // enqueue drag exit if needed. |
| 2827 | if (hoverWindowHandle != mDragState->dragHoverWindowHandle && |
| 2828 | !haveSameToken(hoverWindowHandle, mDragState->dragHoverWindowHandle)) { |
| 2829 | if (mDragState->dragHoverWindowHandle != nullptr) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2830 | enqueueDragEventLocked(mDragState->dragHoverWindowHandle, /*isExiting=*/true, x, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2831 | y); |
| 2832 | } |
| 2833 | mDragState->dragHoverWindowHandle = hoverWindowHandle; |
| 2834 | } |
| 2835 | // enqueue drag location if needed. |
| 2836 | if (hoverWindowHandle != nullptr) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2837 | enqueueDragEventLocked(hoverWindowHandle, /*isExiting=*/false, x, y); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2838 | } |
| 2839 | break; |
| 2840 | } |
| 2841 | |
| 2842 | case AMOTION_EVENT_ACTION_POINTER_UP: |
Siarhei Vishniakou | 5b9766d | 2023-07-18 14:06:29 -0700 | [diff] [blame] | 2843 | if (MotionEvent::getActionIndex(entry.action) != pointerIndex) { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2844 | break; |
| 2845 | } |
| 2846 | // The drag pointer is up. |
| 2847 | [[fallthrough]]; |
| 2848 | case AMOTION_EVENT_ACTION_UP: |
| 2849 | finishDragAndDrop(entry.displayId, x, y); |
| 2850 | break; |
| 2851 | case AMOTION_EVENT_ACTION_CANCEL: { |
| 2852 | ALOGD("Receiving cancel when drag and drop."); |
| 2853 | sendDropWindowCommandLocked(nullptr, 0, 0); |
| 2854 | mDragState.reset(); |
| 2855 | break; |
| 2856 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2857 | } |
| 2858 | } |
| 2859 | |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2860 | std::optional<InputTarget> InputDispatcher::createInputTargetLocked( |
| 2861 | const sp<android::gui::WindowInfoHandle>& windowHandle, |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2862 | InputTarget::DispatchMode dispatchMode, ftl::Flags<InputTarget::Flags> targetFlags, |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2863 | std::optional<nsecs_t> firstDownTimeInTarget) const { |
| 2864 | std::shared_ptr<InputChannel> inputChannel = getInputChannelLocked(windowHandle->getToken()); |
| 2865 | if (inputChannel == nullptr) { |
| 2866 | ALOGW("Not creating InputTarget for %s, no input channel", windowHandle->getName().c_str()); |
| 2867 | return {}; |
| 2868 | } |
| 2869 | InputTarget inputTarget; |
| 2870 | inputTarget.inputChannel = inputChannel; |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 2871 | inputTarget.windowHandle = windowHandle; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2872 | inputTarget.dispatchMode = dispatchMode; |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2873 | inputTarget.flags = targetFlags; |
| 2874 | inputTarget.globalScaleFactor = windowHandle->getInfo()->globalScaleFactor; |
| 2875 | inputTarget.firstDownTimeInTarget = firstDownTimeInTarget; |
| 2876 | const auto& displayInfoIt = mDisplayInfos.find(windowHandle->getInfo()->displayId); |
| 2877 | if (displayInfoIt != mDisplayInfos.end()) { |
| 2878 | inputTarget.displayTransform = displayInfoIt->second.transform; |
| 2879 | } else { |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 2880 | // DisplayInfo not found for this window on display windowHandle->getInfo()->displayId. |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2881 | // TODO(b/198444055): Make this an error message after 'setInputWindows' API is removed. |
| 2882 | } |
| 2883 | return inputTarget; |
| 2884 | } |
| 2885 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2886 | void InputDispatcher::addWindowTargetLocked(const sp<WindowInfoHandle>& windowHandle, |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2887 | InputTarget::DispatchMode dispatchMode, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2888 | ftl::Flags<InputTarget::Flags> targetFlags, |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2889 | std::optional<nsecs_t> firstDownTimeInTarget, |
Siarhei Vishniakou | f75cddb | 2022-10-25 10:42:16 -0700 | [diff] [blame] | 2890 | std::vector<InputTarget>& inputTargets) const { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2891 | std::vector<InputTarget>::iterator it = |
| 2892 | std::find_if(inputTargets.begin(), inputTargets.end(), |
| 2893 | [&windowHandle](const InputTarget& inputTarget) { |
| 2894 | return inputTarget.inputChannel->getConnectionToken() == |
| 2895 | windowHandle->getToken(); |
| 2896 | }); |
Chavi Weingarten | 97b8eec | 2020-01-09 18:09:08 +0000 | [diff] [blame] | 2897 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2898 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2899 | |
| 2900 | if (it == inputTargets.end()) { |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2901 | std::optional<InputTarget> target = |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2902 | createInputTargetLocked(windowHandle, dispatchMode, targetFlags, |
| 2903 | firstDownTimeInTarget); |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2904 | if (!target) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2905 | return; |
| 2906 | } |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2907 | inputTargets.push_back(*target); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2908 | it = inputTargets.end() - 1; |
| 2909 | } |
| 2910 | |
Siarhei Vishniakou | 23d73fb | 2023-10-29 13:27:46 -0700 | [diff] [blame] | 2911 | if (it->flags != targetFlags) { |
| 2912 | LOG(ERROR) << "Flags don't match! targetFlags=" << targetFlags.string() << ", it=" << *it; |
| 2913 | } |
| 2914 | if (it->globalScaleFactor != windowInfo->globalScaleFactor) { |
| 2915 | LOG(ERROR) << "Mismatch! it->globalScaleFactor=" << it->globalScaleFactor |
| 2916 | << ", windowInfo->globalScaleFactor=" << windowInfo->globalScaleFactor; |
| 2917 | } |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2918 | } |
| 2919 | |
| 2920 | void InputDispatcher::addPointerWindowTargetLocked( |
| 2921 | const sp<android::gui::WindowInfoHandle>& windowHandle, |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2922 | InputTarget::DispatchMode dispatchMode, ftl::Flags<InputTarget::Flags> targetFlags, |
| 2923 | std::bitset<MAX_POINTER_ID + 1> pointerIds, std::optional<nsecs_t> firstDownTimeInTarget, |
| 2924 | std::vector<InputTarget>& inputTargets) const REQUIRES(mLock) { |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2925 | if (pointerIds.none()) { |
| 2926 | for (const auto& target : inputTargets) { |
| 2927 | LOG(INFO) << "Target: " << target; |
| 2928 | } |
| 2929 | LOG(FATAL) << "No pointers specified for " << windowHandle->getName(); |
| 2930 | return; |
| 2931 | } |
| 2932 | std::vector<InputTarget>::iterator it = |
| 2933 | std::find_if(inputTargets.begin(), inputTargets.end(), |
| 2934 | [&windowHandle](const InputTarget& inputTarget) { |
| 2935 | return inputTarget.inputChannel->getConnectionToken() == |
| 2936 | windowHandle->getToken(); |
| 2937 | }); |
| 2938 | |
| 2939 | // This is a hack, because the actual entry could potentially be an ACTION_DOWN event that |
| 2940 | // causes a HOVER_EXIT to be generated. That means that the same entry of ACTION_DOWN would |
| 2941 | // have DISPATCH_AS_HOVER_EXIT and DISPATCH_AS_IS. And therefore, we have to create separate |
| 2942 | // input targets for hovering pointers and for touching pointers. |
| 2943 | // If we picked an existing input target above, but it's for HOVER_EXIT - let's use a new |
| 2944 | // target instead. |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2945 | if (it != inputTargets.end() && it->dispatchMode == InputTarget::DispatchMode::HOVER_EXIT) { |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2946 | // Force the code below to create a new input target |
| 2947 | it = inputTargets.end(); |
| 2948 | } |
| 2949 | |
| 2950 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 2951 | |
| 2952 | if (it == inputTargets.end()) { |
| 2953 | std::optional<InputTarget> target = |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2954 | createInputTargetLocked(windowHandle, dispatchMode, targetFlags, |
| 2955 | firstDownTimeInTarget); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2956 | if (!target) { |
| 2957 | return; |
| 2958 | } |
| 2959 | inputTargets.push_back(*target); |
| 2960 | it = inputTargets.end() - 1; |
| 2961 | } |
| 2962 | |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2963 | if (it->dispatchMode != dispatchMode) { |
| 2964 | LOG(ERROR) << __func__ << ": DispatchMode doesn't match! ignoring new mode=" |
| 2965 | << ftl::enum_string(dispatchMode) << ", it=" << *it; |
| 2966 | } |
Siarhei Vishniakou | 4bd0b7c | 2023-10-27 00:51:14 -0700 | [diff] [blame] | 2967 | if (it->flags != targetFlags) { |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2968 | LOG(ERROR) << __func__ << ": Flags don't match! new targetFlags=" << targetFlags.string() |
| 2969 | << ", it=" << *it; |
Siarhei Vishniakou | 4bd0b7c | 2023-10-27 00:51:14 -0700 | [diff] [blame] | 2970 | } |
Siarhei Vishniakou | 23d73fb | 2023-10-29 13:27:46 -0700 | [diff] [blame] | 2971 | if (it->globalScaleFactor != windowInfo->globalScaleFactor) { |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2972 | LOG(ERROR) << __func__ << ": Mismatch! it->globalScaleFactor=" << it->globalScaleFactor |
Siarhei Vishniakou | 23d73fb | 2023-10-29 13:27:46 -0700 | [diff] [blame] | 2973 | << ", windowInfo->globalScaleFactor=" << windowInfo->globalScaleFactor; |
| 2974 | } |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2975 | |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2976 | it->addPointers(pointerIds, windowInfo->transform); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2977 | } |
| 2978 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2979 | void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2980 | int32_t displayId) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2981 | auto monitorsIt = mGlobalMonitorsByDisplay.find(displayId); |
| 2982 | if (monitorsIt == mGlobalMonitorsByDisplay.end()) return; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2983 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2984 | for (const Monitor& monitor : selectResponsiveMonitorsLocked(monitorsIt->second)) { |
| 2985 | InputTarget target; |
| 2986 | target.inputChannel = monitor.inputChannel; |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2987 | // target.firstDownTimeInTarget is not set for global monitors. It is only required in split |
| 2988 | // touch and global monitoring works as intended even without setting firstDownTimeInTarget |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2989 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
| 2990 | target.displayTransform = it->second.transform; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2991 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2992 | target.setDefaultPointerTransform(target.displayTransform); |
| 2993 | inputTargets.push_back(target); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2994 | } |
| 2995 | } |
| 2996 | |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2997 | /** |
| 2998 | * Indicate whether one window handle should be considered as obscuring |
| 2999 | * another window handle. We only check a few preconditions. Actually |
| 3000 | * checking the bounds is left to the caller. |
| 3001 | */ |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3002 | static bool canBeObscuredBy(const sp<WindowInfoHandle>& windowHandle, |
| 3003 | const sp<WindowInfoHandle>& otherHandle) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3004 | // Compare by token so cloned layers aren't counted |
| 3005 | if (haveSameToken(windowHandle, otherHandle)) { |
| 3006 | return false; |
| 3007 | } |
| 3008 | auto info = windowHandle->getInfo(); |
| 3009 | auto otherInfo = otherHandle->getInfo(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 3010 | if (otherInfo->inputConfig.test(WindowInfo::InputConfig::NOT_VISIBLE)) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3011 | return false; |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 3012 | } else if (otherInfo->alpha == 0 && |
| 3013 | otherInfo->inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE)) { |
Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame] | 3014 | // Those act as if they were invisible, so we don't need to flag them. |
| 3015 | // We do want to potentially flag touchable windows even if they have 0 |
| 3016 | // opacity, since they can consume touches and alter the effects of the |
| 3017 | // user interaction (eg. apps that rely on |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3018 | // Flags::WINDOW_IS_PARTIALLY_OBSCURED should still be told about those |
Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame] | 3019 | // windows), hence we also check for FLAG_NOT_TOUCHABLE. |
| 3020 | return false; |
Bernardo Rufino | 8007daf | 2020-09-22 09:40:01 +0000 | [diff] [blame] | 3021 | } else if (info->ownerUid == otherInfo->ownerUid) { |
| 3022 | // If ownerUid is the same we don't generate occlusion events as there |
| 3023 | // is no security boundary within an uid. |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3024 | return false; |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 3025 | } else if (otherInfo->inputConfig.test(gui::WindowInfo::InputConfig::TRUSTED_OVERLAY)) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3026 | return false; |
| 3027 | } else if (otherInfo->displayId != info->displayId) { |
| 3028 | return false; |
| 3029 | } |
| 3030 | return true; |
| 3031 | } |
| 3032 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3033 | /** |
| 3034 | * Returns touch occlusion information in the form of TouchOcclusionInfo. To check if the touch is |
| 3035 | * untrusted, one should check: |
| 3036 | * |
| 3037 | * 1. If result.hasBlockingOcclusion is true. |
| 3038 | * If it's, it means the touch should be blocked due to a window with occlusion mode of |
| 3039 | * BLOCK_UNTRUSTED. |
| 3040 | * |
| 3041 | * 2. If result.obscuringOpacity > mMaximumObscuringOpacityForTouch. |
| 3042 | * If it is (and 1 is false), then the touch should be blocked because a stack of windows |
| 3043 | * (possibly only one) with occlusion mode of USE_OPACITY from one UID resulted in a composed |
| 3044 | * obscuring opacity above the threshold. Note that if there was no window of occlusion mode |
| 3045 | * USE_OPACITY, result.obscuringOpacity would've been 0 and since |
| 3046 | * mMaximumObscuringOpacityForTouch >= 0, the condition above would never be true. |
| 3047 | * |
| 3048 | * If neither of those is true, then it means the touch can be allowed. |
| 3049 | */ |
| 3050 | InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3051 | const sp<WindowInfoHandle>& windowHandle, int32_t x, int32_t y) const { |
| 3052 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 3053 | int32_t displayId = windowInfo->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3054 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3055 | TouchOcclusionInfo info; |
| 3056 | info.hasBlockingOcclusion = false; |
| 3057 | info.obscuringOpacity = 0; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3058 | info.obscuringUid = gui::Uid::INVALID; |
| 3059 | std::map<gui::Uid, float> opacityByUid; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3060 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3061 | if (windowHandle == otherHandle) { |
| 3062 | break; // All future windows are below us. Exit early. |
| 3063 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3064 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 3065 | if (canBeObscuredBy(windowHandle, otherHandle) && otherInfo->frameContainsPoint(x, y) && |
| 3066 | !haveSameApplicationToken(windowInfo, otherInfo)) { |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 3067 | if (DEBUG_TOUCH_OCCLUSION) { |
| 3068 | info.debugInfo.push_back( |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 3069 | dumpWindowForTouchOcclusion(otherInfo, /*isTouchedWindow=*/false)); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 3070 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3071 | // canBeObscuredBy() has returned true above, which means this window is untrusted, so |
| 3072 | // we perform the checks below to see if the touch can be propagated or not based on the |
| 3073 | // window's touch occlusion mode |
| 3074 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::BLOCK_UNTRUSTED) { |
| 3075 | info.hasBlockingOcclusion = true; |
| 3076 | info.obscuringUid = otherInfo->ownerUid; |
| 3077 | info.obscuringPackage = otherInfo->packageName; |
| 3078 | break; |
| 3079 | } |
| 3080 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::USE_OPACITY) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3081 | const auto uid = otherInfo->ownerUid; |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3082 | float opacity = |
| 3083 | (opacityByUid.find(uid) == opacityByUid.end()) ? 0 : opacityByUid[uid]; |
| 3084 | // Given windows A and B: |
| 3085 | // opacity(A, B) = 1 - [1 - opacity(A)] * [1 - opacity(B)] |
| 3086 | opacity = 1 - (1 - opacity) * (1 - otherInfo->alpha); |
| 3087 | opacityByUid[uid] = opacity; |
| 3088 | if (opacity > info.obscuringOpacity) { |
| 3089 | info.obscuringOpacity = opacity; |
| 3090 | info.obscuringUid = uid; |
| 3091 | info.obscuringPackage = otherInfo->packageName; |
| 3092 | } |
| 3093 | } |
| 3094 | } |
| 3095 | } |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 3096 | if (DEBUG_TOUCH_OCCLUSION) { |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 3097 | info.debugInfo.push_back(dumpWindowForTouchOcclusion(windowInfo, /*isTouchedWindow=*/true)); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 3098 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3099 | return info; |
| 3100 | } |
| 3101 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3102 | std::string InputDispatcher::dumpWindowForTouchOcclusion(const WindowInfo* info, |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 3103 | bool isTouchedWindow) const { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3104 | return StringPrintf(INDENT2 "* %spackage=%s/%s, id=%" PRId32 ", mode=%s, alpha=%.2f, " |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 3105 | "frame=[%" PRId32 ",%" PRId32 "][%" PRId32 ",%" PRId32 |
| 3106 | "], touchableRegion=%s, window={%s}, inputConfig={%s}, " |
| 3107 | "hasToken=%s, applicationInfo.name=%s, applicationInfo.token=%s\n", |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 3108 | isTouchedWindow ? "[TOUCHED] " : "", info->packageName.c_str(), |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3109 | info->ownerUid.toString().c_str(), info->id, |
Chavi Weingarten | 7f01919 | 2023-08-08 20:39:01 +0000 | [diff] [blame] | 3110 | toString(info->touchOcclusionMode).c_str(), info->alpha, info->frame.left, |
| 3111 | info->frame.top, info->frame.right, info->frame.bottom, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3112 | dumpRegion(info->touchableRegion).c_str(), info->name.c_str(), |
| 3113 | info->inputConfig.string().c_str(), toString(info->token != nullptr), |
| 3114 | info->applicationInfo.name.c_str(), |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 3115 | binderToString(info->applicationInfo.token).c_str()); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 3116 | } |
| 3117 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3118 | bool InputDispatcher::isTouchTrustedLocked(const TouchOcclusionInfo& occlusionInfo) const { |
| 3119 | if (occlusionInfo.hasBlockingOcclusion) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3120 | ALOGW("Untrusted touch due to occlusion by %s/%s", occlusionInfo.obscuringPackage.c_str(), |
| 3121 | occlusionInfo.obscuringUid.toString().c_str()); |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3122 | return false; |
| 3123 | } |
| 3124 | if (occlusionInfo.obscuringOpacity > mMaximumObscuringOpacityForTouch) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3125 | ALOGW("Untrusted touch due to occlusion by %s/%s (obscuring opacity = " |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3126 | "%.2f, maximum allowed = %.2f)", |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3127 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid.toString().c_str(), |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3128 | occlusionInfo.obscuringOpacity, mMaximumObscuringOpacityForTouch); |
| 3129 | return false; |
| 3130 | } |
| 3131 | return true; |
| 3132 | } |
| 3133 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3134 | bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3135 | int32_t x, int32_t y) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3136 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3137 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 3138 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3139 | if (windowHandle == otherHandle) { |
| 3140 | break; // All future windows are below us. Exit early. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3141 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3142 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3143 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 3144 | otherInfo->frameContainsPoint(x, y)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3145 | return true; |
| 3146 | } |
| 3147 | } |
| 3148 | return false; |
| 3149 | } |
| 3150 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3151 | bool InputDispatcher::isWindowObscuredLocked(const sp<WindowInfoHandle>& windowHandle) const { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 3152 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3153 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 3154 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 3155 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3156 | if (windowHandle == otherHandle) { |
| 3157 | break; // All future windows are below us. Exit early. |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 3158 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3159 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3160 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 3161 | otherInfo->overlaps(windowInfo)) { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 3162 | return true; |
| 3163 | } |
| 3164 | } |
| 3165 | return false; |
| 3166 | } |
| 3167 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 3168 | std::string InputDispatcher::getApplicationWindowLabel( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3169 | const InputApplicationHandle* applicationHandle, const sp<WindowInfoHandle>& windowHandle) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3170 | if (applicationHandle != nullptr) { |
| 3171 | if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 3172 | return applicationHandle->getName() + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3173 | } else { |
| 3174 | return applicationHandle->getName(); |
| 3175 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3176 | } else if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 3177 | return windowHandle->getInfo()->applicationInfo.name + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3178 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3179 | return "<unknown application or window>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3180 | } |
| 3181 | } |
| 3182 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3183 | void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3184 | if (!isUserActivityEvent(eventEntry)) { |
| 3185 | // Not poking user activity if the event type does not represent a user activity |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3186 | return; |
| 3187 | } |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 3188 | |
| 3189 | const int32_t eventType = getUserActivityEventType(eventEntry); |
| 3190 | if (input_flags::rate_limit_user_activity_poke_in_dispatcher()) { |
| 3191 | // Note that we're directly getting the time diff between the current event and the previous |
| 3192 | // event. This is assuming that the first user event always happens at a timestamp that is |
| 3193 | // greater than `mMinTimeBetweenUserActivityPokes` (otherwise, the first user event will |
| 3194 | // wrongly be dropped). In real life, `mMinTimeBetweenUserActivityPokes` is a much smaller |
| 3195 | // value than the potential first user activity event time, so this is ok. |
| 3196 | std::chrono::nanoseconds timeSinceLastEvent = |
| 3197 | std::chrono::nanoseconds(eventEntry.eventTime - mLastUserActivityTimes[eventType]); |
| 3198 | if (timeSinceLastEvent < mMinTimeBetweenUserActivityPokes) { |
| 3199 | return; |
| 3200 | } |
| 3201 | } |
| 3202 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3203 | int32_t displayId = getTargetDisplayId(eventEntry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3204 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 3205 | const WindowInfo* windowDisablingUserActivityInfo = nullptr; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3206 | if (focusedWindowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3207 | const WindowInfo* info = focusedWindowHandle->getInfo(); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 3208 | if (info->inputConfig.test(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY)) { |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 3209 | windowDisablingUserActivityInfo = info; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3210 | } |
| 3211 | } |
| 3212 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3213 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3214 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3215 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 3216 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3217 | return; |
| 3218 | } |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 3219 | if (windowDisablingUserActivityInfo != nullptr) { |
| 3220 | if (DEBUG_DISPATCH_CYCLE) { |
| 3221 | ALOGD("Not poking user activity: disabled by window '%s'.", |
| 3222 | windowDisablingUserActivityInfo->name.c_str()); |
| 3223 | } |
| 3224 | return; |
| 3225 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3226 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3227 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3228 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3229 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 3230 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3231 | return; |
| 3232 | } |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 3233 | // If the key code is unknown, we don't consider it user activity |
| 3234 | if (keyEntry.keyCode == AKEYCODE_UNKNOWN) { |
| 3235 | return; |
| 3236 | } |
| 3237 | // Don't inhibit events that were intercepted or are not passed to |
| 3238 | // the apps, like system shortcuts |
| 3239 | if (windowDisablingUserActivityInfo != nullptr && |
| 3240 | keyEntry.interceptKeyResult != KeyEntry::InterceptKeyResult::SKIP && |
| 3241 | keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER) { |
| 3242 | if (DEBUG_DISPATCH_CYCLE) { |
| 3243 | ALOGD("Not poking user activity: disabled by window '%s'.", |
| 3244 | windowDisablingUserActivityInfo->name.c_str()); |
| 3245 | } |
| 3246 | return; |
| 3247 | } |
| 3248 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3249 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3250 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3251 | default: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3252 | LOG_ALWAYS_FATAL("%s events are not user activity", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3253 | ftl::enum_string(eventEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3254 | break; |
| 3255 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3256 | } |
| 3257 | |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 3258 | mLastUserActivityTimes[eventType] = eventEntry.eventTime; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3259 | auto command = [this, eventTime = eventEntry.eventTime, eventType, displayId]() |
| 3260 | REQUIRES(mLock) { |
| 3261 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 3262 | mPolicy.pokeUserActivity(eventTime, eventType, displayId); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3263 | }; |
| 3264 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3265 | } |
| 3266 | |
| 3267 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3268 | const std::shared_ptr<Connection>& connection, |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 3269 | std::shared_ptr<const EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3270 | const InputTarget& inputTarget) { |
Prabir Pradhan | 2dac8b8 | 2023-09-06 01:11:51 +0000 | [diff] [blame] | 3271 | ATRACE_NAME_IF(ATRACE_ENABLED(), |
| 3272 | StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
| 3273 | connection->getInputChannelName().c_str(), eventEntry->id)); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3274 | if (DEBUG_DISPATCH_CYCLE) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3275 | ALOGD("channel '%s' ~ prepareDispatchCycle - flags=%s, " |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 3276 | "globalScaleFactor=%f, pointerIds=%s %s", |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3277 | connection->getInputChannelName().c_str(), inputTarget.flags.string().c_str(), |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 3278 | inputTarget.globalScaleFactor, bitsetToString(inputTarget.pointerIds).c_str(), |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3279 | inputTarget.getPointerInfoString().c_str()); |
| 3280 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3281 | |
| 3282 | // Skip this event if the connection status is not normal. |
| 3283 | // We don't want to enqueue additional outbound events if the connection is broken. |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3284 | if (connection->status != Connection::Status::NORMAL) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3285 | if (DEBUG_DISPATCH_CYCLE) { |
| 3286 | ALOGD("channel '%s' ~ Dropping event because the channel status is %s", |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3287 | connection->getInputChannelName().c_str(), |
| 3288 | ftl::enum_string(connection->status).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3289 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3290 | return; |
| 3291 | } |
| 3292 | |
| 3293 | // Split a motion event if needed. |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3294 | if (inputTarget.flags.test(InputTarget::Flags::SPLIT)) { |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3295 | LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3296 | "Entry type %s should not have Flags::SPLIT", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3297 | ftl::enum_string(eventEntry->type).c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3298 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3299 | const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry); |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 3300 | if (inputTarget.pointerIds.count() != originalMotionEntry.getPointerCount()) { |
Siarhei Vishniakou | 16e4fa0 | 2023-02-16 17:48:56 -0800 | [diff] [blame] | 3301 | if (!inputTarget.firstDownTimeInTarget.has_value()) { |
| 3302 | logDispatchStateLocked(); |
| 3303 | LOG(FATAL) << "Splitting motion events requires a down time to be set for the " |
| 3304 | "target on connection " |
| 3305 | << connection->getInputChannelName() << " for " |
| 3306 | << originalMotionEntry.getDescription(); |
| 3307 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3308 | std::unique_ptr<MotionEntry> splitMotionEntry = |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3309 | splitMotionEvent(originalMotionEntry, inputTarget.pointerIds, |
| 3310 | inputTarget.firstDownTimeInTarget.value()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3311 | if (!splitMotionEntry) { |
| 3312 | return; // split event was dropped |
| 3313 | } |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3314 | if (splitMotionEntry->action == AMOTION_EVENT_ACTION_CANCEL) { |
| 3315 | std::string reason = std::string("reason=pointer cancel on split window"); |
| 3316 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 3317 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 3318 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3319 | if (DEBUG_FOCUS) { |
| 3320 | ALOGD("channel '%s' ~ Split motion event.", |
| 3321 | connection->getInputChannelName().c_str()); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3322 | logOutboundMotionDetails(" ", *splitMotionEntry); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3323 | } |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3324 | enqueueDispatchEntryAndStartDispatchCycleLocked(currentTime, connection, |
| 3325 | std::move(splitMotionEntry), |
| 3326 | inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3327 | return; |
| 3328 | } |
| 3329 | } |
| 3330 | |
| 3331 | // Not splitting. Enqueue dispatch entries for the event as is. |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3332 | enqueueDispatchEntryAndStartDispatchCycleLocked(currentTime, connection, eventEntry, |
| 3333 | inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3334 | } |
| 3335 | |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3336 | void InputDispatcher::enqueueDispatchEntryAndStartDispatchCycleLocked( |
| 3337 | nsecs_t currentTime, const std::shared_ptr<Connection>& connection, |
| 3338 | std::shared_ptr<const EventEntry> eventEntry, const InputTarget& inputTarget) { |
Prabir Pradhan | 2dac8b8 | 2023-09-06 01:11:51 +0000 | [diff] [blame] | 3339 | ATRACE_NAME_IF(ATRACE_ENABLED(), |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3340 | StringPrintf("enqueueDispatchEntryAndStartDispatchCycleLocked(inputChannel=%s, " |
| 3341 | "id=0x%" PRIx32 ")", |
Prabir Pradhan | 2dac8b8 | 2023-09-06 01:11:51 +0000 | [diff] [blame] | 3342 | connection->getInputChannelName().c_str(), eventEntry->id)); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3343 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 3344 | const bool wasEmpty = connection->outboundQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3345 | |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3346 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3347 | |
| 3348 | // If the outbound queue was previously empty, start the dispatch cycle going. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3349 | if (wasEmpty && !connection->outboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3350 | startDispatchCycleLocked(currentTime, connection); |
| 3351 | } |
| 3352 | } |
| 3353 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3354 | void InputDispatcher::enqueueDispatchEntryLocked(const std::shared_ptr<Connection>& connection, |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 3355 | std::shared_ptr<const EventEntry> eventEntry, |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3356 | const InputTarget& inputTarget) { |
Prabir Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame^] | 3357 | // 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] | 3358 | // This is a new event. |
| 3359 | // Enqueue a new dispatch entry onto the outbound queue for this connection. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3360 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Prabir Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame^] | 3361 | createDispatchEntry(inputTarget, eventEntry, inputTarget.flags, mWindowInfosVsyncId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3362 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3363 | // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a |
| 3364 | // different EventEntry than what was passed in. |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3365 | eventEntry = dispatchEntry->eventEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3366 | // Apply target flags and update the connection's input state. |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3367 | switch (eventEntry->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3368 | case EventEntry::Type::KEY: { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3369 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(*eventEntry); |
| 3370 | if (!connection->inputState.trackKey(keyEntry, keyEntry.flags)) { |
Siarhei Vishniakou | c94dafe | 2023-05-26 10:24:19 -0700 | [diff] [blame] | 3371 | LOG(WARNING) << "channel " << connection->getInputChannelName() |
| 3372 | << "~ dropping inconsistent event: " << *dispatchEntry; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3373 | return; // skip the inconsistent event |
| 3374 | } |
| 3375 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3376 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3377 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3378 | case EventEntry::Type::MOTION: { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3379 | std::shared_ptr<const MotionEntry> resolvedMotion = |
| 3380 | std::static_pointer_cast<const MotionEntry>(eventEntry); |
| 3381 | { |
| 3382 | // Determine the resolved motion entry. |
| 3383 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); |
| 3384 | int32_t resolvedAction = motionEntry.action; |
| 3385 | int32_t resolvedFlags = motionEntry.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3386 | |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3387 | if (inputTarget.dispatchMode == InputTarget::DispatchMode::OUTSIDE) { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3388 | resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3389 | } else if (inputTarget.dispatchMode == InputTarget::DispatchMode::HOVER_EXIT) { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3390 | resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3391 | } else if (inputTarget.dispatchMode == InputTarget::DispatchMode::HOVER_ENTER) { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3392 | resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3393 | } else if (inputTarget.dispatchMode == InputTarget::DispatchMode::SLIPPERY_EXIT) { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3394 | resolvedAction = AMOTION_EVENT_ACTION_CANCEL; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3395 | } else if (inputTarget.dispatchMode == InputTarget::DispatchMode::SLIPPERY_ENTER) { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3396 | resolvedAction = AMOTION_EVENT_ACTION_DOWN; |
| 3397 | } |
| 3398 | if (resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE && |
| 3399 | !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source, |
| 3400 | motionEntry.displayId)) { |
| 3401 | if (DEBUG_DISPATCH_CYCLE) { |
| 3402 | LOG(DEBUG) << "channel '" << connection->getInputChannelName().c_str() |
| 3403 | << "' ~ enqueueDispatchEntryLocked: filling in missing hover " |
| 3404 | "enter event"; |
| 3405 | } |
| 3406 | resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 3407 | } |
| 3408 | |
| 3409 | if (resolvedAction == AMOTION_EVENT_ACTION_CANCEL) { |
| 3410 | resolvedFlags |= AMOTION_EVENT_FLAG_CANCELED; |
| 3411 | } |
| 3412 | if (dispatchEntry->targetFlags.test(InputTarget::Flags::WINDOW_IS_OBSCURED)) { |
| 3413 | resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; |
| 3414 | } |
| 3415 | if (dispatchEntry->targetFlags.test( |
| 3416 | InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED)) { |
| 3417 | resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 3418 | } |
| 3419 | |
| 3420 | dispatchEntry->resolvedFlags = resolvedFlags; |
| 3421 | if (resolvedAction != motionEntry.action) { |
Siarhei Vishniakou | e9ef6bc | 2023-12-21 19:47:20 -0800 | [diff] [blame] | 3422 | std::optional<std::vector<PointerProperties>> usingProperties; |
| 3423 | std::optional<std::vector<PointerCoords>> usingCoords; |
| 3424 | if (resolvedAction == AMOTION_EVENT_ACTION_HOVER_EXIT || |
| 3425 | resolvedAction == AMOTION_EVENT_ACTION_CANCEL) { |
| 3426 | // This is a HOVER_EXIT or an ACTION_CANCEL event that was synthesized by |
| 3427 | // the dispatcher, and therefore the coordinates of this event are currently |
| 3428 | // incorrect. These events should use the coordinates of the last dispatched |
| 3429 | // ACTION_MOVE or HOVER_MOVE. We need to query InputState to get this data. |
| 3430 | const bool hovering = resolvedAction == AMOTION_EVENT_ACTION_HOVER_EXIT; |
| 3431 | std::optional<std::pair<std::vector<PointerProperties>, |
| 3432 | std::vector<PointerCoords>>> |
| 3433 | pointerInfo = |
| 3434 | connection->inputState.getPointersOfLastEvent(motionEntry, |
| 3435 | hovering); |
| 3436 | if (pointerInfo) { |
| 3437 | usingProperties = pointerInfo->first; |
| 3438 | usingCoords = pointerInfo->second; |
| 3439 | } |
| 3440 | } |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3441 | // Generate a new MotionEntry with a new eventId using the resolved action and |
| 3442 | // flags. |
Siarhei Vishniakou | e9ef6bc | 2023-12-21 19:47:20 -0800 | [diff] [blame] | 3443 | resolvedMotion = std::make_shared< |
| 3444 | MotionEntry>(mIdGenerator.nextId(), motionEntry.injectionState, |
| 3445 | motionEntry.eventTime, motionEntry.deviceId, |
| 3446 | motionEntry.source, motionEntry.displayId, |
| 3447 | motionEntry.policyFlags, resolvedAction, |
| 3448 | motionEntry.actionButton, resolvedFlags, |
| 3449 | motionEntry.metaState, motionEntry.buttonState, |
| 3450 | motionEntry.classification, motionEntry.edgeFlags, |
| 3451 | motionEntry.xPrecision, motionEntry.yPrecision, |
| 3452 | motionEntry.xCursorPosition, motionEntry.yCursorPosition, |
| 3453 | motionEntry.downTime, |
| 3454 | usingProperties.value_or(motionEntry.pointerProperties), |
| 3455 | usingCoords.value_or(motionEntry.pointerCoords)); |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3456 | if (ATRACE_ENABLED()) { |
| 3457 | std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32 |
| 3458 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3459 | motionEntry.id, resolvedMotion->id); |
| 3460 | ATRACE_NAME(message.c_str()); |
| 3461 | } |
| 3462 | |
| 3463 | // Set the resolved motion entry in the DispatchEntry. |
| 3464 | dispatchEntry->eventEntry = resolvedMotion; |
| 3465 | eventEntry = resolvedMotion; |
| 3466 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3467 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3468 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3469 | // Check if we need to cancel any of the ongoing gestures. We don't support multiple |
| 3470 | // devices being active at the same time in the same window, so if a new device is |
| 3471 | // active, cancel the gesture from the old device. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3472 | std::unique_ptr<EventEntry> cancelEvent = |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3473 | connection->inputState.cancelConflictingInputStream(*resolvedMotion); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3474 | if (cancelEvent != nullptr) { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3475 | LOG(INFO) << "Canceling pointers for device " << resolvedMotion->deviceId << " in " |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3476 | << connection->getInputChannelName() << " with event " |
| 3477 | << cancelEvent->getDescription(); |
| 3478 | std::unique_ptr<DispatchEntry> cancelDispatchEntry = |
| 3479 | createDispatchEntry(inputTarget, std::move(cancelEvent), |
Prabir Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame^] | 3480 | ftl::Flags<InputTarget::Flags>(), mWindowInfosVsyncId); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3481 | |
| 3482 | // Send these cancel events to the queue before sending the event from the new |
| 3483 | // device. |
| 3484 | connection->outboundQueue.emplace_back(std::move(cancelDispatchEntry)); |
| 3485 | } |
| 3486 | |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3487 | if (!connection->inputState.trackMotion(*resolvedMotion, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3488 | dispatchEntry->resolvedFlags)) { |
Siarhei Vishniakou | c94dafe | 2023-05-26 10:24:19 -0700 | [diff] [blame] | 3489 | LOG(WARNING) << "channel " << connection->getInputChannelName() |
| 3490 | << "~ dropping inconsistent event: " << *dispatchEntry; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3491 | return; // skip the inconsistent event |
| 3492 | } |
| 3493 | |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3494 | if ((resolvedMotion->flags & AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) && |
| 3495 | (resolvedMotion->policyFlags & POLICY_FLAG_TRUSTED)) { |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 3496 | // Skip reporting pointer down outside focus to the policy. |
| 3497 | break; |
| 3498 | } |
| 3499 | |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3500 | dispatchPointerDownOutsideFocus(resolvedMotion->source, resolvedMotion->action, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3501 | inputTarget.inputChannel->getConnectionToken()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3502 | |
| 3503 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3504 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3505 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3506 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3507 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3508 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3509 | break; |
| 3510 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3511 | case EventEntry::Type::SENSOR: { |
| 3512 | LOG_ALWAYS_FATAL("SENSOR events should not go to apps via input channel"); |
| 3513 | break; |
| 3514 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3515 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 3516 | case EventEntry::Type::DEVICE_RESET: { |
| 3517 | LOG_ALWAYS_FATAL("%s events should not go to apps", |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3518 | ftl::enum_string(eventEntry->type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3519 | break; |
| 3520 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3521 | } |
| 3522 | |
| 3523 | // Remember that we are waiting for this dispatch to complete. |
| 3524 | if (dispatchEntry->hasForegroundTarget()) { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3525 | incrementPendingForegroundDispatches(*eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3526 | } |
| 3527 | |
| 3528 | // Enqueue the dispatch entry. |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 3529 | connection->outboundQueue.emplace_back(std::move(dispatchEntry)); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3530 | traceOutboundQueueLength(*connection); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3531 | } |
| 3532 | |
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 | * This function is for debugging and metrics collection. It has two roles. |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3535 | * |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3536 | * The first role is to log input interaction with windows, which helps determine what the user was |
| 3537 | * interacting with. For example, if user is touching launcher, we will see an input_interaction log |
| 3538 | * that user started interacting with launcher window, as well as any other window that received |
| 3539 | * that gesture, such as the wallpaper or other spy windows. A new input_interaction is only logged |
| 3540 | * when the set of tokens that received the event changes. It is not logged again as long as the |
| 3541 | * user is interacting with the same windows. |
| 3542 | * |
| 3543 | * The second role is to track input device activity for metrics collection. For each input event, |
| 3544 | * we report the set of UIDs that the input device interacted with to the policy. Unlike for the |
| 3545 | * input_interaction logs, the device interaction is reported even when the set of interaction |
| 3546 | * tokens do not change. |
| 3547 | * |
| 3548 | * For these purposes, we do not count ACTION_OUTSIDE, ACTION_UP and ACTION_CANCEL actions as |
| 3549 | * interaction. This includes up and cancel events for both keys and motions. |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3550 | */ |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3551 | void InputDispatcher::processInteractionsLocked(const EventEntry& entry, |
| 3552 | const std::vector<InputTarget>& targets) { |
| 3553 | int32_t deviceId; |
| 3554 | nsecs_t eventTime; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3555 | // Skip ACTION_UP events, and all events other than keys and motions |
| 3556 | if (entry.type == EventEntry::Type::KEY) { |
| 3557 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 3558 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
| 3559 | return; |
| 3560 | } |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3561 | deviceId = keyEntry.deviceId; |
| 3562 | eventTime = keyEntry.eventTime; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3563 | } else if (entry.type == EventEntry::Type::MOTION) { |
| 3564 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 3565 | if (motionEntry.action == AMOTION_EVENT_ACTION_UP || |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3566 | motionEntry.action == AMOTION_EVENT_ACTION_CANCEL || |
| 3567 | MotionEvent::getActionMasked(motionEntry.action) == AMOTION_EVENT_ACTION_POINTER_UP) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3568 | return; |
| 3569 | } |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3570 | deviceId = motionEntry.deviceId; |
| 3571 | eventTime = motionEntry.eventTime; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3572 | } else { |
| 3573 | return; // Not a key or a motion |
| 3574 | } |
| 3575 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3576 | std::set<gui::Uid> interactionUids; |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 3577 | std::unordered_set<sp<IBinder>, StrongPointerHash<IBinder>> newConnectionTokens; |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3578 | std::vector<std::shared_ptr<Connection>> newConnections; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3579 | for (const InputTarget& target : targets) { |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3580 | if (target.dispatchMode == InputTarget::DispatchMode::OUTSIDE) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3581 | continue; // Skip windows that receive ACTION_OUTSIDE |
| 3582 | } |
| 3583 | |
| 3584 | sp<IBinder> token = target.inputChannel->getConnectionToken(); |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3585 | std::shared_ptr<Connection> connection = getConnectionLocked(token); |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3586 | if (connection == nullptr) { |
| 3587 | continue; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3588 | } |
| 3589 | newConnectionTokens.insert(std::move(token)); |
| 3590 | newConnections.emplace_back(connection); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3591 | if (target.windowHandle) { |
| 3592 | interactionUids.emplace(target.windowHandle->getInfo()->ownerUid); |
| 3593 | } |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3594 | } |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3595 | |
| 3596 | auto command = [this, deviceId, eventTime, uids = std::move(interactionUids)]() |
| 3597 | REQUIRES(mLock) { |
| 3598 | scoped_unlock unlock(mLock); |
| 3599 | mPolicy.notifyDeviceInteraction(deviceId, eventTime, uids); |
| 3600 | }; |
| 3601 | postCommandLocked(std::move(command)); |
| 3602 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3603 | if (newConnectionTokens == mInteractionConnectionTokens) { |
| 3604 | return; // no change |
| 3605 | } |
| 3606 | mInteractionConnectionTokens = newConnectionTokens; |
| 3607 | |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3608 | std::string targetList; |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3609 | for (const std::shared_ptr<Connection>& connection : newConnections) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3610 | targetList += connection->getWindowName() + ", "; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3611 | } |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3612 | std::string message = "Interaction with: " + targetList; |
| 3613 | if (targetList.empty()) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3614 | message += "<none>"; |
| 3615 | } |
| 3616 | android_log_event_list(LOGTAG_INPUT_INTERACTION) << message << LOG_ID_EVENTS; |
| 3617 | } |
| 3618 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3619 | void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3620 | const sp<IBinder>& token) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3621 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3622 | uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK; |
| 3623 | if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3624 | return; |
| 3625 | } |
| 3626 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 3627 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3628 | if (focusedToken == token) { |
| 3629 | // ignore since token is focused |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3630 | return; |
| 3631 | } |
| 3632 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3633 | auto command = [this, token]() REQUIRES(mLock) { |
| 3634 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 3635 | mPolicy.onPointerDownOutsideFocus(token); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3636 | }; |
| 3637 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3638 | } |
| 3639 | |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3640 | status_t InputDispatcher::publishMotionEvent(Connection& connection, |
| 3641 | DispatchEntry& dispatchEntry) const { |
| 3642 | const EventEntry& eventEntry = *(dispatchEntry.eventEntry); |
| 3643 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 3644 | |
| 3645 | PointerCoords scaledCoords[MAX_POINTERS]; |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 3646 | const PointerCoords* usingCoords = motionEntry.pointerCoords.data(); |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3647 | |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 3648 | // TODO(b/316355518): Do not modify coords before dispatch. |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3649 | // Set the X and Y offset and X and Y scale depending on the input source. |
| 3650 | if ((motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) && |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3651 | !(dispatchEntry.targetFlags.test(InputTarget::Flags::ZERO_COORDS))) { |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3652 | float globalScaleFactor = dispatchEntry.globalScaleFactor; |
| 3653 | if (globalScaleFactor != 1.0f) { |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 3654 | for (uint32_t i = 0; i < motionEntry.getPointerCount(); i++) { |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3655 | scaledCoords[i] = motionEntry.pointerCoords[i]; |
| 3656 | // Don't apply window scale here since we don't want scale to affect raw |
| 3657 | // coordinates. The scale will be sent back to the client and applied |
| 3658 | // later when requesting relative coordinates. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 3659 | scaledCoords[i].scale(globalScaleFactor, /*windowXScale=*/1, /*windowYScale=*/1); |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3660 | } |
| 3661 | usingCoords = scaledCoords; |
| 3662 | } |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3663 | } else if (dispatchEntry.targetFlags.test(InputTarget::Flags::ZERO_COORDS)) { |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3664 | // We don't want the dispatch target to know the coordinates |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 3665 | for (uint32_t i = 0; i < motionEntry.getPointerCount(); i++) { |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3666 | scaledCoords[i].clear(); |
| 3667 | } |
| 3668 | usingCoords = scaledCoords; |
| 3669 | } |
| 3670 | |
| 3671 | std::array<uint8_t, 32> hmac = getSignature(motionEntry, dispatchEntry); |
| 3672 | |
| 3673 | // Publish the motion event. |
| 3674 | return connection.inputPublisher |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3675 | .publishMotionEvent(dispatchEntry.seq, motionEntry.id, motionEntry.deviceId, |
| 3676 | motionEntry.source, motionEntry.displayId, std::move(hmac), |
| 3677 | motionEntry.action, motionEntry.actionButton, |
| 3678 | dispatchEntry.resolvedFlags, motionEntry.edgeFlags, |
| 3679 | motionEntry.metaState, motionEntry.buttonState, |
| 3680 | motionEntry.classification, dispatchEntry.transform, |
| 3681 | motionEntry.xPrecision, motionEntry.yPrecision, |
| 3682 | motionEntry.xCursorPosition, motionEntry.yCursorPosition, |
| 3683 | dispatchEntry.rawTransform, motionEntry.downTime, |
| 3684 | motionEntry.eventTime, motionEntry.getPointerCount(), |
| 3685 | motionEntry.pointerProperties.data(), usingCoords); |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3686 | } |
| 3687 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3688 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3689 | const std::shared_ptr<Connection>& connection) { |
Prabir Pradhan | 2dac8b8 | 2023-09-06 01:11:51 +0000 | [diff] [blame] | 3690 | ATRACE_NAME_IF(ATRACE_ENABLED(), |
| 3691 | StringPrintf("startDispatchCycleLocked(inputChannel=%s)", |
| 3692 | connection->getInputChannelName().c_str())); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3693 | if (DEBUG_DISPATCH_CYCLE) { |
| 3694 | ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str()); |
| 3695 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3696 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3697 | while (connection->status == Connection::Status::NORMAL && !connection->outboundQueue.empty()) { |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 3698 | std::unique_ptr<DispatchEntry>& dispatchEntry = connection->outboundQueue.front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3699 | dispatchEntry->deliveryTime = currentTime; |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3700 | const std::chrono::nanoseconds timeout = getDispatchingTimeoutLocked(connection); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3701 | dispatchEntry->timeoutTime = currentTime + timeout.count(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3702 | |
| 3703 | // Publish the event. |
| 3704 | status_t status; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3705 | const EventEntry& eventEntry = *(dispatchEntry->eventEntry); |
| 3706 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3707 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3708 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 3709 | std::array<uint8_t, 32> hmac = getSignature(keyEntry, *dispatchEntry); |
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 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3714 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3715 | // Publish the key event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3716 | status = connection->inputPublisher |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3717 | .publishKeyEvent(dispatchEntry->seq, keyEntry.id, |
| 3718 | keyEntry.deviceId, keyEntry.source, |
| 3719 | keyEntry.displayId, std::move(hmac), |
| 3720 | keyEntry.action, dispatchEntry->resolvedFlags, |
| 3721 | keyEntry.keyCode, keyEntry.scanCode, |
| 3722 | keyEntry.metaState, keyEntry.repeatCount, |
| 3723 | keyEntry.downTime, keyEntry.eventTime); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 3724 | if (mTracer) { |
| 3725 | mTracer->traceEventDispatch(*dispatchEntry, keyEntry.traceTracker.get()); |
| 3726 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3727 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3728 | } |
| 3729 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3730 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 3731 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
Siarhei Vishniakou | 827d1ac | 2023-07-21 16:37:51 -0700 | [diff] [blame] | 3732 | LOG(INFO) << "Publishing " << *dispatchEntry << " to " |
| 3733 | << connection->getInputChannelName(); |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 3734 | } |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 3735 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3736 | status = publishMotionEvent(*connection, *dispatchEntry); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 3737 | if (mTracer) { |
| 3738 | mTracer->traceEventDispatch(*dispatchEntry, motionEntry.traceTracker.get()); |
| 3739 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3740 | break; |
| 3741 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3742 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3743 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3744 | const FocusEntry& focusEntry = static_cast<const FocusEntry&>(eventEntry); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3745 | status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3746 | focusEntry.id, |
Antonio Kantek | 3cfec7b | 2021-11-05 18:26:17 -0700 | [diff] [blame] | 3747 | focusEntry.hasFocus); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3748 | break; |
| 3749 | } |
| 3750 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3751 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 3752 | const TouchModeEntry& touchModeEntry = |
| 3753 | static_cast<const TouchModeEntry&>(eventEntry); |
| 3754 | status = connection->inputPublisher |
| 3755 | .publishTouchModeEvent(dispatchEntry->seq, touchModeEntry.id, |
| 3756 | touchModeEntry.inTouchMode); |
| 3757 | |
| 3758 | break; |
| 3759 | } |
| 3760 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3761 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 3762 | const auto& captureEntry = |
| 3763 | static_cast<const PointerCaptureChangedEntry&>(eventEntry); |
| 3764 | status = connection->inputPublisher |
| 3765 | .publishCaptureEvent(dispatchEntry->seq, captureEntry.id, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 3766 | captureEntry.pointerCaptureRequest.enable); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3767 | break; |
| 3768 | } |
| 3769 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3770 | case EventEntry::Type::DRAG: { |
| 3771 | const DragEntry& dragEntry = static_cast<const DragEntry&>(eventEntry); |
| 3772 | status = connection->inputPublisher.publishDragEvent(dispatchEntry->seq, |
| 3773 | dragEntry.id, dragEntry.x, |
| 3774 | dragEntry.y, |
| 3775 | dragEntry.isExiting); |
| 3776 | break; |
| 3777 | } |
| 3778 | |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3779 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3780 | case EventEntry::Type::DEVICE_RESET: |
| 3781 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3782 | LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3783 | ftl::enum_string(eventEntry.type).c_str()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3784 | return; |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3785 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3786 | } |
| 3787 | |
| 3788 | // Check the result. |
| 3789 | if (status) { |
| 3790 | if (status == WOULD_BLOCK) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3791 | if (connection->waitQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3792 | ALOGE("channel '%s' ~ Could not publish event because the pipe is full. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3793 | "This is unexpected because the wait queue is empty, so the pipe " |
| 3794 | "should be empty and we shouldn't have any problems writing an " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3795 | "event to it, status=%s(%d)", |
| 3796 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3797 | status); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 3798 | abortBrokenDispatchCycleLocked(currentTime, connection, /*notify=*/true); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3799 | } else { |
| 3800 | // Pipe is full and we are waiting for the app to finish process some events |
| 3801 | // before sending more events to it. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3802 | if (DEBUG_DISPATCH_CYCLE) { |
| 3803 | ALOGD("channel '%s' ~ Could not publish event because the pipe is full, " |
| 3804 | "waiting for the application to catch up", |
| 3805 | connection->getInputChannelName().c_str()); |
| 3806 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3807 | } |
| 3808 | } else { |
| 3809 | ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3810 | "status=%s(%d)", |
| 3811 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3812 | status); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 3813 | abortBrokenDispatchCycleLocked(currentTime, connection, /*notify=*/true); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3814 | } |
| 3815 | return; |
| 3816 | } |
| 3817 | |
| 3818 | // Re-enqueue the event on the wait queue. |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 3819 | const nsecs_t timeoutTime = dispatchEntry->timeoutTime; |
| 3820 | connection->waitQueue.emplace_back(std::move(dispatchEntry)); |
| 3821 | connection->outboundQueue.erase(connection->outboundQueue.begin()); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3822 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3823 | if (connection->responsive) { |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 3824 | mAnrTracker.insert(timeoutTime, connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3825 | } |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3826 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3827 | } |
| 3828 | } |
| 3829 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3830 | std::array<uint8_t, 32> InputDispatcher::sign(const VerifiedInputEvent& event) const { |
| 3831 | size_t size; |
| 3832 | switch (event.type) { |
| 3833 | case VerifiedInputEvent::Type::KEY: { |
| 3834 | size = sizeof(VerifiedKeyEvent); |
| 3835 | break; |
| 3836 | } |
| 3837 | case VerifiedInputEvent::Type::MOTION: { |
| 3838 | size = sizeof(VerifiedMotionEvent); |
| 3839 | break; |
| 3840 | } |
| 3841 | } |
| 3842 | const uint8_t* start = reinterpret_cast<const uint8_t*>(&event); |
| 3843 | return mHmacKeyManager.sign(start, size); |
| 3844 | } |
| 3845 | |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3846 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3847 | const MotionEntry& motionEntry, const DispatchEntry& dispatchEntry) const { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3848 | const int32_t actionMasked = MotionEvent::getActionMasked(motionEntry.action); |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3849 | if (actionMasked != AMOTION_EVENT_ACTION_UP && actionMasked != AMOTION_EVENT_ACTION_DOWN) { |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3850 | // Only sign events up and down events as the purely move events |
| 3851 | // are tied to their up/down counterparts so signing would be redundant. |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3852 | return INVALID_HMAC; |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3853 | } |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3854 | |
| 3855 | VerifiedMotionEvent verifiedEvent = |
| 3856 | verifiedMotionEventFromMotionEntry(motionEntry, dispatchEntry.rawTransform); |
| 3857 | verifiedEvent.actionMasked = actionMasked; |
| 3858 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS; |
| 3859 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3860 | } |
| 3861 | |
| 3862 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3863 | const KeyEntry& keyEntry, const DispatchEntry& dispatchEntry) const { |
| 3864 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(keyEntry); |
| 3865 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_KEY_EVENT_FLAGS; |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3866 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3867 | } |
| 3868 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3869 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3870 | const std::shared_ptr<Connection>& connection, |
| 3871 | uint32_t seq, bool handled, nsecs_t consumeTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3872 | if (DEBUG_DISPATCH_CYCLE) { |
| 3873 | ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s", |
| 3874 | connection->getInputChannelName().c_str(), seq, toString(handled)); |
| 3875 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3876 | |
Prabir Pradhan | 98ca4a2 | 2024-01-09 23:51:50 +0000 | [diff] [blame] | 3877 | if (connection->status != Connection::Status::NORMAL) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3878 | return; |
| 3879 | } |
| 3880 | |
| 3881 | // Notify other system components and prepare to start the next dispatch cycle. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3882 | auto command = [this, currentTime, connection, seq, handled, consumeTime]() REQUIRES(mLock) { |
| 3883 | doDispatchCycleFinishedCommand(currentTime, connection, seq, handled, consumeTime); |
| 3884 | }; |
| 3885 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3886 | } |
| 3887 | |
| 3888 | void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3889 | const std::shared_ptr<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3890 | bool notify) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3891 | if (DEBUG_DISPATCH_CYCLE) { |
Siarhei Vishniakou | 827d1ac | 2023-07-21 16:37:51 -0700 | [diff] [blame] | 3892 | LOG(INFO) << "channel '" << connection->getInputChannelName() << "'~ " << __func__ |
| 3893 | << " - notify=" << toString(notify); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3894 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3895 | |
| 3896 | // Clear the dispatch queues. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3897 | drainDispatchQueue(connection->outboundQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3898 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3899 | drainDispatchQueue(connection->waitQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3900 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3901 | |
| 3902 | // The connection appears to be unrecoverably broken. |
| 3903 | // Ignore already broken or zombie connections. |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3904 | if (connection->status == Connection::Status::NORMAL) { |
| 3905 | connection->status = Connection::Status::BROKEN; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3906 | |
| 3907 | if (notify) { |
| 3908 | // Notify other system components. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3909 | ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", |
| 3910 | connection->getInputChannelName().c_str()); |
| 3911 | |
| 3912 | auto command = [this, connection]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3913 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 3914 | mPolicy.notifyInputChannelBroken(connection->inputChannel->getConnectionToken()); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3915 | }; |
| 3916 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3917 | } |
| 3918 | } |
| 3919 | } |
| 3920 | |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 3921 | void InputDispatcher::drainDispatchQueue(std::deque<std::unique_ptr<DispatchEntry>>& queue) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3922 | while (!queue.empty()) { |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 3923 | releaseDispatchEntry(std::move(queue.front())); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3924 | queue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3925 | } |
| 3926 | } |
| 3927 | |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 3928 | void InputDispatcher::releaseDispatchEntry(std::unique_ptr<DispatchEntry> dispatchEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3929 | if (dispatchEntry->hasForegroundTarget()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3930 | decrementPendingForegroundDispatches(*(dispatchEntry->eventEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3931 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3932 | } |
| 3933 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3934 | int InputDispatcher::handleReceiveCallback(int events, sp<IBinder> connectionToken) { |
| 3935 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3936 | std::shared_ptr<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3937 | if (connection == nullptr) { |
| 3938 | ALOGW("Received looper callback for unknown input channel token %p. events=0x%x", |
| 3939 | connectionToken.get(), events); |
| 3940 | return 0; // remove the callback |
| 3941 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3942 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3943 | bool notify; |
| 3944 | if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { |
| 3945 | if (!(events & ALOOPER_EVENT_INPUT)) { |
| 3946 | ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. " |
| 3947 | "events=0x%x", |
| 3948 | connection->getInputChannelName().c_str(), events); |
| 3949 | return 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3950 | } |
| 3951 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3952 | nsecs_t currentTime = now(); |
| 3953 | bool gotOne = false; |
| 3954 | status_t status = OK; |
| 3955 | for (;;) { |
| 3956 | Result<InputPublisher::ConsumerResponse> result = |
| 3957 | connection->inputPublisher.receiveConsumerResponse(); |
| 3958 | if (!result.ok()) { |
| 3959 | status = result.error().code(); |
| 3960 | break; |
| 3961 | } |
| 3962 | |
| 3963 | if (std::holds_alternative<InputPublisher::Finished>(*result)) { |
| 3964 | const InputPublisher::Finished& finish = |
| 3965 | std::get<InputPublisher::Finished>(*result); |
| 3966 | finishDispatchCycleLocked(currentTime, connection, finish.seq, finish.handled, |
| 3967 | finish.consumeTime); |
| 3968 | } else if (std::holds_alternative<InputPublisher::Timeline>(*result)) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3969 | if (shouldReportMetricsForConnection(*connection)) { |
| 3970 | const InputPublisher::Timeline& timeline = |
| 3971 | std::get<InputPublisher::Timeline>(*result); |
| 3972 | mLatencyTracker |
| 3973 | .trackGraphicsLatency(timeline.inputEventId, |
| 3974 | connection->inputChannel->getConnectionToken(), |
| 3975 | std::move(timeline.graphicsTimeline)); |
| 3976 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3977 | } |
| 3978 | gotOne = true; |
| 3979 | } |
| 3980 | if (gotOne) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3981 | runCommandsLockedInterruptable(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3982 | if (status == WOULD_BLOCK) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3983 | return 1; |
| 3984 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3985 | } |
| 3986 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3987 | notify = status != DEAD_OBJECT || !connection->monitor; |
| 3988 | if (notify) { |
| 3989 | ALOGE("channel '%s' ~ Failed to receive finished signal. status=%s(%d)", |
| 3990 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3991 | status); |
| 3992 | } |
| 3993 | } else { |
| 3994 | // Monitor channels are never explicitly unregistered. |
| 3995 | // We do it automatically when the remote endpoint is closed so don't warn about them. |
| 3996 | const bool stillHaveWindowHandle = |
| 3997 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()) != nullptr; |
| 3998 | notify = !connection->monitor && stillHaveWindowHandle; |
| 3999 | if (notify) { |
| 4000 | ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred. events=0x%x", |
| 4001 | connection->getInputChannelName().c_str(), events); |
| 4002 | } |
| 4003 | } |
| 4004 | |
| 4005 | // Remove the channel. |
| 4006 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), notify); |
| 4007 | return 0; // remove the callback |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4008 | } |
| 4009 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4010 | void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4011 | const CancelationOptions& options) { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4012 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 4013 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4014 | } |
| 4015 | } |
| 4016 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4017 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 4018 | const CancelationOptions& options) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 4019 | for (const auto& [_, monitors] : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4020 | for (const Monitor& monitor : monitors) { |
| 4021 | synthesizeCancelationEventsForInputChannelLocked(monitor.inputChannel, options); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4022 | } |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 4023 | } |
| 4024 | } |
| 4025 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4026 | void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked( |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4027 | const std::shared_ptr<InputChannel>& channel, const CancelationOptions& options) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 4028 | std::shared_ptr<Connection> connection = getConnectionLocked(channel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4029 | if (connection == nullptr) { |
| 4030 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4031 | } |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4032 | |
| 4033 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4034 | } |
| 4035 | |
| 4036 | void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 4037 | const std::shared_ptr<Connection>& connection, const CancelationOptions& options) { |
Prabir Pradhan | b13da8f | 2024-01-09 23:10:13 +0000 | [diff] [blame] | 4038 | if (connection->status != Connection::Status::NORMAL) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4039 | return; |
| 4040 | } |
| 4041 | |
| 4042 | nsecs_t currentTime = now(); |
| 4043 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4044 | std::vector<std::unique_ptr<EventEntry>> cancelationEvents = |
Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 4045 | connection->inputState.synthesizeCancelationEvents(currentTime, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4046 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4047 | if (cancelationEvents.empty()) { |
| 4048 | return; |
| 4049 | } |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 4050 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4051 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 4052 | 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] | 4053 | "with reality: %s, mode=%s.", |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4054 | connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason, |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 4055 | ftl::enum_string(options.mode).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4056 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4057 | |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 4058 | std::string reason = std::string("reason=").append(options.reason); |
| 4059 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 4060 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 4061 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 4062 | const bool wasEmpty = connection->outboundQueue.empty(); |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 4063 | // The target to use if we don't find a window associated with the channel. |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 4064 | const InputTarget fallbackTarget{.inputChannel = connection->inputChannel}; |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 4065 | const auto& token = connection->inputChannel->getConnectionToken(); |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 4066 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4067 | for (size_t i = 0; i < cancelationEvents.size(); i++) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4068 | std::unique_ptr<EventEntry> cancelationEventEntry = std::move(cancelationEvents[i]); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4069 | std::vector<InputTarget> targets{}; |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4070 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4071 | switch (cancelationEventEntry->type) { |
| 4072 | case EventEntry::Type::KEY: { |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4073 | const auto& keyEntry = static_cast<const KeyEntry&>(*cancelationEventEntry); |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 4074 | const std::optional<int32_t> targetDisplay = keyEntry.displayId != ADISPLAY_ID_NONE |
| 4075 | ? std::make_optional(keyEntry.displayId) |
| 4076 | : std::nullopt; |
| 4077 | if (const auto& window = getWindowHandleLocked(token, targetDisplay); window) { |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 4078 | addWindowTargetLocked(window, InputTarget::DispatchMode::AS_IS, |
| 4079 | /*targetFlags=*/{}, keyEntry.downTime, targets); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4080 | } else { |
| 4081 | targets.emplace_back(fallbackTarget); |
| 4082 | } |
| 4083 | logOutboundKeyDetails("cancel - ", keyEntry); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4084 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4085 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4086 | case EventEntry::Type::MOTION: { |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4087 | const auto& motionEntry = static_cast<const MotionEntry&>(*cancelationEventEntry); |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 4088 | const std::optional<int32_t> targetDisplay = |
| 4089 | motionEntry.displayId != ADISPLAY_ID_NONE |
| 4090 | ? std::make_optional(motionEntry.displayId) |
| 4091 | : std::nullopt; |
| 4092 | if (const auto& window = getWindowHandleLocked(token, targetDisplay); window) { |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4093 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4094 | for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.getPointerCount(); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4095 | pointerIndex++) { |
| 4096 | pointerIds.set(motionEntry.pointerProperties[pointerIndex].id); |
| 4097 | } |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 4098 | if (mDragState && mDragState->dragWindow->getToken() == token && |
| 4099 | pointerIds.test(mDragState->pointerId)) { |
| 4100 | LOG(INFO) << __func__ |
| 4101 | << ": Canceling drag and drop because the pointers for the drag " |
| 4102 | "window are being canceled."; |
| 4103 | sendDropWindowCommandLocked(nullptr, /*x=*/0, /*y=*/0); |
| 4104 | mDragState.reset(); |
| 4105 | } |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 4106 | addPointerWindowTargetLocked(window, InputTarget::DispatchMode::AS_IS, |
| 4107 | ftl::Flags<InputTarget::Flags>(), pointerIds, |
| 4108 | motionEntry.downTime, targets); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4109 | } else { |
| 4110 | targets.emplace_back(fallbackTarget); |
| 4111 | const auto it = mDisplayInfos.find(motionEntry.displayId); |
| 4112 | if (it != mDisplayInfos.end()) { |
| 4113 | targets.back().displayTransform = it->second.transform; |
| 4114 | targets.back().setDefaultPointerTransform(it->second.transform); |
| 4115 | } |
| 4116 | } |
| 4117 | logOutboundMotionDetails("cancel - ", motionEntry); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4118 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4119 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4120 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 4121 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4122 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 4123 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4124 | LOG_ALWAYS_FATAL("Canceling %s events is not supported", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 4125 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4126 | break; |
| 4127 | } |
| 4128 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4129 | case EventEntry::Type::DEVICE_RESET: |
| 4130 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4131 | 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] | 4132 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4133 | break; |
| 4134 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4135 | } |
| 4136 | |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4137 | if (targets.size() != 1) LOG(FATAL) << __func__ << ": InputTarget not created"; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 4138 | enqueueDispatchEntryLocked(connection, std::move(cancelationEventEntry), targets[0]); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4139 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4140 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 4141 | // If the outbound queue was previously empty, start the dispatch cycle going. |
| 4142 | if (wasEmpty && !connection->outboundQueue.empty()) { |
| 4143 | startDispatchCycleLocked(currentTime, connection); |
| 4144 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4145 | } |
| 4146 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4147 | void InputDispatcher::synthesizePointerDownEventsForConnectionLocked( |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 4148 | const nsecs_t downTime, const std::shared_ptr<Connection>& connection, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4149 | ftl::Flags<InputTarget::Flags> targetFlags) { |
Prabir Pradhan | 98ca4a2 | 2024-01-09 23:51:50 +0000 | [diff] [blame] | 4150 | if (connection->status != Connection::Status::NORMAL) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4151 | return; |
| 4152 | } |
| 4153 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4154 | std::vector<std::unique_ptr<EventEntry>> downEvents = |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4155 | connection->inputState.synthesizePointerDownEvents(downTime); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4156 | |
| 4157 | if (downEvents.empty()) { |
| 4158 | return; |
| 4159 | } |
| 4160 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4161 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4162 | ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.", |
| 4163 | connection->getInputChannelName().c_str(), downEvents.size()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4164 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4165 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4166 | sp<WindowInfoHandle> windowHandle = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4167 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4168 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 4169 | const bool wasEmpty = connection->outboundQueue.empty(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4170 | for (std::unique_ptr<EventEntry>& downEventEntry : downEvents) { |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 4171 | std::vector<InputTarget> targets{}; |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4172 | switch (downEventEntry->type) { |
| 4173 | case EventEntry::Type::MOTION: { |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 4174 | const auto& motionEntry = static_cast<const MotionEntry&>(*downEventEntry); |
| 4175 | if (windowHandle != nullptr) { |
| 4176 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4177 | for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.getPointerCount(); |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 4178 | pointerIndex++) { |
| 4179 | pointerIds.set(motionEntry.pointerProperties[pointerIndex].id); |
| 4180 | } |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 4181 | addPointerWindowTargetLocked(windowHandle, InputTarget::DispatchMode::AS_IS, |
| 4182 | targetFlags, pointerIds, motionEntry.downTime, |
| 4183 | targets); |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 4184 | } else { |
| 4185 | targets.emplace_back(InputTarget{.inputChannel = connection->inputChannel, |
| 4186 | .flags = targetFlags}); |
| 4187 | const auto it = mDisplayInfos.find(motionEntry.displayId); |
| 4188 | if (it != mDisplayInfos.end()) { |
| 4189 | targets.back().displayTransform = it->second.transform; |
| 4190 | targets.back().setDefaultPointerTransform(it->second.transform); |
| 4191 | } |
| 4192 | } |
| 4193 | logOutboundMotionDetails("down - ", motionEntry); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4194 | break; |
| 4195 | } |
| 4196 | |
| 4197 | case EventEntry::Type::KEY: |
| 4198 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 4199 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4200 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4201 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4202 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4203 | case EventEntry::Type::SENSOR: |
| 4204 | case EventEntry::Type::DRAG: { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4205 | 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] | 4206 | ftl::enum_string(downEventEntry->type).c_str()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4207 | break; |
| 4208 | } |
| 4209 | } |
| 4210 | |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 4211 | if (targets.size() != 1) LOG(FATAL) << __func__ << ": InputTarget not created"; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 4212 | enqueueDispatchEntryLocked(connection, std::move(downEventEntry), targets[0]); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4213 | } |
| 4214 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 4215 | // If the outbound queue was previously empty, start the dispatch cycle going. |
| 4216 | if (wasEmpty && !connection->outboundQueue.empty()) { |
| 4217 | startDispatchCycleLocked(downTime, connection); |
| 4218 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4219 | } |
| 4220 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4221 | void InputDispatcher::synthesizeCancelationEventsForWindowLocked( |
| 4222 | const sp<WindowInfoHandle>& windowHandle, const CancelationOptions& options) { |
| 4223 | if (windowHandle != nullptr) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 4224 | std::shared_ptr<Connection> wallpaperConnection = |
| 4225 | getConnectionLocked(windowHandle->getToken()); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4226 | if (wallpaperConnection != nullptr) { |
| 4227 | synthesizeCancelationEventsForConnectionLocked(wallpaperConnection, options); |
| 4228 | } |
| 4229 | } |
| 4230 | } |
| 4231 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4232 | std::unique_ptr<MotionEntry> InputDispatcher::splitMotionEvent( |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 4233 | const MotionEntry& originalMotionEntry, std::bitset<MAX_POINTER_ID + 1> pointerIds, |
| 4234 | nsecs_t splitDownTime) { |
| 4235 | ALOG_ASSERT(pointerIds.any()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4236 | |
| 4237 | uint32_t splitPointerIndexMap[MAX_POINTERS]; |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4238 | std::vector<PointerProperties> splitPointerProperties; |
| 4239 | std::vector<PointerCoords> splitPointerCoords; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4240 | |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4241 | uint32_t originalPointerCount = originalMotionEntry.getPointerCount(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4242 | uint32_t splitPointerCount = 0; |
| 4243 | |
| 4244 | for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4245 | originalPointerIndex++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4246 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 4247 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4248 | uint32_t pointerId = uint32_t(pointerProperties.id); |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 4249 | if (pointerIds.test(pointerId)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4250 | splitPointerIndexMap[splitPointerCount] = originalPointerIndex; |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4251 | splitPointerProperties.push_back(pointerProperties); |
| 4252 | splitPointerCoords.push_back(originalMotionEntry.pointerCoords[originalPointerIndex]); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4253 | splitPointerCount += 1; |
| 4254 | } |
| 4255 | } |
| 4256 | |
| 4257 | if (splitPointerCount != pointerIds.count()) { |
| 4258 | // This is bad. We are missing some of the pointers that we expected to deliver. |
| 4259 | // Most likely this indicates that we received an ACTION_MOVE events that has |
| 4260 | // different pointer ids than we expected based on the previous ACTION_DOWN |
| 4261 | // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers |
| 4262 | // in this way. |
| 4263 | ALOGW("Dropping split motion event because the pointer count is %d but " |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 4264 | "we expected there to be %zu pointers. This probably means we received " |
Siarhei Vishniakou | 16e4fa0 | 2023-02-16 17:48:56 -0800 | [diff] [blame] | 4265 | "a broken sequence of pointer ids from the input device: %s", |
| 4266 | splitPointerCount, pointerIds.count(), originalMotionEntry.getDescription().c_str()); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4267 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4268 | } |
| 4269 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 4270 | int32_t action = originalMotionEntry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4271 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4272 | if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN || |
| 4273 | maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
Siarhei Vishniakou | 5b9766d | 2023-07-18 14:06:29 -0700 | [diff] [blame] | 4274 | int32_t originalPointerIndex = MotionEvent::getActionIndex(action); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4275 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 4276 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4277 | uint32_t pointerId = uint32_t(pointerProperties.id); |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 4278 | if (pointerIds.test(pointerId)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4279 | if (pointerIds.count() == 1) { |
| 4280 | // The first/last pointer went down/up. |
| 4281 | action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4282 | ? AMOTION_EVENT_ACTION_DOWN |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4283 | : (originalMotionEntry.flags & AMOTION_EVENT_FLAG_CANCELED) != 0 |
| 4284 | ? AMOTION_EVENT_ACTION_CANCEL |
| 4285 | : AMOTION_EVENT_ACTION_UP; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4286 | } else { |
| 4287 | // A secondary pointer went down/up. |
| 4288 | uint32_t splitPointerIndex = 0; |
| 4289 | while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { |
| 4290 | splitPointerIndex += 1; |
| 4291 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4292 | action = maskedAction | |
| 4293 | (splitPointerIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4294 | } |
| 4295 | } else { |
| 4296 | // An unrelated pointer changed. |
| 4297 | action = AMOTION_EVENT_ACTION_MOVE; |
| 4298 | } |
| 4299 | } |
| 4300 | |
Siarhei Vishniakou | 59e302b | 2023-06-05 08:04:53 -0700 | [diff] [blame] | 4301 | if (action == AMOTION_EVENT_ACTION_DOWN && splitDownTime != originalMotionEntry.eventTime) { |
| 4302 | logDispatchStateLocked(); |
| 4303 | LOG_ALWAYS_FATAL("Split motion event has mismatching downTime and eventTime for " |
| 4304 | "ACTION_DOWN, motionEntry=%s, splitDownTime=%" PRId64, |
| 4305 | originalMotionEntry.getDescription().c_str(), splitDownTime); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4306 | } |
| 4307 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4308 | int32_t newId = mIdGenerator.nextId(); |
Prabir Pradhan | 2dac8b8 | 2023-09-06 01:11:51 +0000 | [diff] [blame] | 4309 | ATRACE_NAME_IF(ATRACE_ENABLED(), |
| 4310 | StringPrintf("Split MotionEvent(id=0x%" PRIx32 ") to MotionEvent(id=0x%" PRIx32 |
| 4311 | ").", |
| 4312 | originalMotionEntry.id, newId)); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4313 | std::unique_ptr<MotionEntry> splitMotionEntry = |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4314 | std::make_unique<MotionEntry>(newId, originalMotionEntry.injectionState, |
| 4315 | originalMotionEntry.eventTime, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4316 | originalMotionEntry.deviceId, originalMotionEntry.source, |
| 4317 | originalMotionEntry.displayId, |
| 4318 | originalMotionEntry.policyFlags, action, |
| 4319 | originalMotionEntry.actionButton, |
| 4320 | originalMotionEntry.flags, originalMotionEntry.metaState, |
| 4321 | originalMotionEntry.buttonState, |
| 4322 | originalMotionEntry.classification, |
| 4323 | originalMotionEntry.edgeFlags, |
| 4324 | originalMotionEntry.xPrecision, |
| 4325 | originalMotionEntry.yPrecision, |
| 4326 | originalMotionEntry.xCursorPosition, |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4327 | originalMotionEntry.yCursorPosition, splitDownTime, |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4328 | splitPointerProperties, splitPointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4329 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4330 | return splitMotionEntry; |
| 4331 | } |
| 4332 | |
Asmita Poddar | dd9a6cd | 2023-09-26 15:35:12 +0000 | [diff] [blame] | 4333 | void InputDispatcher::notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs& args) { |
| 4334 | std::scoped_lock _l(mLock); |
| 4335 | mLatencyTracker.setInputDevices(args.inputDeviceInfos); |
| 4336 | } |
| 4337 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4338 | void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4339 | if (debugInboundEventDetails()) { |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4340 | ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args.eventTime); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4341 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4342 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4343 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4344 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4345 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4346 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4347 | std::unique_ptr<ConfigurationChangedEntry> newEntry = |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4348 | std::make_unique<ConfigurationChangedEntry>(args.id, args.eventTime); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4349 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4350 | } // release lock |
| 4351 | |
| 4352 | if (needWake) { |
| 4353 | mLooper->wake(); |
| 4354 | } |
| 4355 | } |
| 4356 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4357 | void InputDispatcher::notifyKey(const NotifyKeyArgs& args) { |
Prabir Pradhan | 96282b0 | 2023-02-24 22:36:17 +0000 | [diff] [blame] | 4358 | ALOGD_IF(debugInboundEventDetails(), |
| 4359 | "notifyKey - id=%" PRIx32 ", eventTime=%" PRId64 |
| 4360 | ", deviceId=%d, source=%s, displayId=%" PRId32 |
| 4361 | "policyFlags=0x%x, action=%s, flags=0x%x, keyCode=%s, scanCode=0x%x, metaState=0x%x, " |
| 4362 | "downTime=%" PRId64, |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4363 | args.id, args.eventTime, args.deviceId, inputEventSourceToString(args.source).c_str(), |
| 4364 | args.displayId, args.policyFlags, KeyEvent::actionToString(args.action), args.flags, |
| 4365 | KeyEvent::getLabel(args.keyCode), args.scanCode, args.metaState, args.downTime); |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4366 | Result<void> keyCheck = validateKeyEvent(args.action); |
| 4367 | if (!keyCheck.ok()) { |
| 4368 | LOG(ERROR) << "invalid key event: " << keyCheck.error(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4369 | return; |
| 4370 | } |
| 4371 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4372 | uint32_t policyFlags = args.policyFlags; |
| 4373 | int32_t flags = args.flags; |
| 4374 | int32_t metaState = args.metaState; |
Siarhei Vishniakou | 622bd32 | 2018-10-29 18:02:27 -0700 | [diff] [blame] | 4375 | // InputDispatcher tracks and generates key repeats on behalf of |
| 4376 | // whatever notifies it, so repeatCount should always be set to 0 |
| 4377 | constexpr int32_t repeatCount = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4378 | if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { |
| 4379 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 4380 | flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
| 4381 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4382 | if (policyFlags & POLICY_FLAG_FUNCTION) { |
| 4383 | metaState |= AMETA_FUNCTION_ON; |
| 4384 | } |
| 4385 | |
| 4386 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 4387 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4388 | int32_t keyCode = args.keyCode; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4389 | KeyEvent event; |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4390 | event.initialize(args.id, args.deviceId, args.source, args.displayId, INVALID_HMAC, args.action, |
| 4391 | flags, keyCode, args.scanCode, metaState, repeatCount, args.downTime, |
| 4392 | args.eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4393 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4394 | android::base::Timer t; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4395 | mPolicy.interceptKeyBeforeQueueing(event, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4396 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4397 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4398 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4399 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4400 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4401 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4402 | { // acquire lock |
| 4403 | mLock.lock(); |
| 4404 | |
| 4405 | if (shouldSendKeyToInputFilterLocked(args)) { |
| 4406 | mLock.unlock(); |
| 4407 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4408 | policyFlags |= POLICY_FLAG_FILTERED; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4409 | if (!mPolicy.filterInputEvent(event, policyFlags)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4410 | return; // event was consumed by the filter |
| 4411 | } |
| 4412 | |
| 4413 | mLock.lock(); |
| 4414 | } |
| 4415 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4416 | std::unique_ptr<KeyEntry> newEntry = |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4417 | std::make_unique<KeyEntry>(args.id, /*injectionState=*/nullptr, args.eventTime, |
| 4418 | args.deviceId, args.source, args.displayId, policyFlags, |
| 4419 | args.action, flags, keyCode, args.scanCode, metaState, |
| 4420 | repeatCount, args.downTime); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 4421 | if (mTracer) { |
| 4422 | newEntry->traceTracker = mTracer->traceInboundEvent(*newEntry); |
| 4423 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4424 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4425 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4426 | mLock.unlock(); |
| 4427 | } // release lock |
| 4428 | |
| 4429 | if (needWake) { |
| 4430 | mLooper->wake(); |
| 4431 | } |
| 4432 | } |
| 4433 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4434 | bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs& args) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4435 | return mInputFilterEnabled; |
| 4436 | } |
| 4437 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4438 | void InputDispatcher::notifyMotion(const NotifyMotionArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4439 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 96282b0 | 2023-02-24 22:36:17 +0000 | [diff] [blame] | 4440 | ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=%s, " |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4441 | "displayId=%" PRId32 ", policyFlags=0x%x, " |
Siarhei Vishniakou | 6ebd069 | 2022-10-20 15:05:45 -0700 | [diff] [blame] | 4442 | "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] | 4443 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, xCursorPosition=%f, " |
| 4444 | "yCursorPosition=%f, downTime=%" PRId64, |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4445 | args.id, args.eventTime, args.deviceId, inputEventSourceToString(args.source).c_str(), |
| 4446 | args.displayId, args.policyFlags, MotionEvent::actionToString(args.action).c_str(), |
| 4447 | args.actionButton, args.flags, args.metaState, args.buttonState, args.edgeFlags, |
| 4448 | args.xPrecision, args.yPrecision, args.xCursorPosition, args.yCursorPosition, |
| 4449 | args.downTime); |
Siarhei Vishniakou | 3218fc0 | 2023-06-15 20:41:02 -0700 | [diff] [blame] | 4450 | for (uint32_t i = 0; i < args.getPointerCount(); i++) { |
Prabir Pradhan | 96282b0 | 2023-02-24 22:36:17 +0000 | [diff] [blame] | 4451 | ALOGD(" Pointer %d: id=%d, toolType=%s, x=%f, y=%f, pressure=%f, size=%f, " |
| 4452 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, orientation=%f", |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4453 | i, args.pointerProperties[i].id, |
| 4454 | ftl::enum_string(args.pointerProperties[i].toolType).c_str(), |
| 4455 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 4456 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 4457 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 4458 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 4459 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 4460 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 4461 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 4462 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 4463 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4464 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4465 | } |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4466 | |
Siarhei Vishniakou | 3218fc0 | 2023-06-15 20:41:02 -0700 | [diff] [blame] | 4467 | Result<void> motionCheck = |
| 4468 | validateMotionEvent(args.action, args.actionButton, args.getPointerCount(), |
| 4469 | args.pointerProperties.data()); |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4470 | if (!motionCheck.ok()) { |
| 4471 | LOG(FATAL) << "Invalid event: " << args.dump() << "; reason: " << motionCheck.error(); |
| 4472 | return; |
| 4473 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4474 | |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 4475 | if (DEBUG_VERIFY_EVENTS) { |
| 4476 | auto [it, _] = |
| 4477 | mVerifiersByDisplay.try_emplace(args.displayId, |
| 4478 | StringPrintf("display %" PRId32, args.displayId)); |
| 4479 | Result<void> result = |
Siarhei Vishniakou | 2d151ac | 2023-09-19 13:30:24 -0700 | [diff] [blame] | 4480 | it->second.processMovement(args.deviceId, args.source, args.action, |
| 4481 | args.getPointerCount(), args.pointerProperties.data(), |
| 4482 | args.pointerCoords.data(), args.flags); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 4483 | if (!result.ok()) { |
| 4484 | LOG(FATAL) << "Bad stream: " << result.error() << " caused by " << args.dump(); |
| 4485 | } |
| 4486 | } |
| 4487 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4488 | uint32_t policyFlags = args.policyFlags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4489 | policyFlags |= POLICY_FLAG_TRUSTED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4490 | |
| 4491 | android::base::Timer t; |
Yeabkal Wubshit | 88a9041 | 2023-12-21 18:23:04 -0800 | [diff] [blame] | 4492 | mPolicy.interceptMotionBeforeQueueing(args.displayId, args.source, args.action, args.eventTime, |
| 4493 | policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4494 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4495 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4496 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4497 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4498 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4499 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4500 | { // acquire lock |
| 4501 | mLock.lock(); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 4502 | if (!(policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
| 4503 | // Set the flag anyway if we already have an ongoing gesture. That would allow us to |
| 4504 | // complete the processing of the current stroke. |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4505 | const auto touchStateIt = mTouchStatesByDisplay.find(args.displayId); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 4506 | if (touchStateIt != mTouchStatesByDisplay.end()) { |
| 4507 | const TouchState& touchState = touchStateIt->second; |
Linnan Li | 907ae73 | 2023-09-05 17:14:21 +0800 | [diff] [blame] | 4508 | if (touchState.hasTouchingPointers(args.deviceId) || |
| 4509 | touchState.hasHoveringPointers(args.deviceId)) { |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 4510 | policyFlags |= POLICY_FLAG_PASS_TO_USER; |
| 4511 | } |
| 4512 | } |
| 4513 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4514 | |
| 4515 | if (shouldSendMotionToInputFilterLocked(args)) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4516 | ui::Transform displayTransform; |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4517 | if (const auto it = mDisplayInfos.find(args.displayId); it != mDisplayInfos.end()) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4518 | displayTransform = it->second.transform; |
| 4519 | } |
| 4520 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4521 | mLock.unlock(); |
| 4522 | |
| 4523 | MotionEvent event; |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4524 | event.initialize(args.id, args.deviceId, args.source, args.displayId, INVALID_HMAC, |
| 4525 | args.action, args.actionButton, args.flags, args.edgeFlags, |
| 4526 | args.metaState, args.buttonState, args.classification, |
| 4527 | displayTransform, args.xPrecision, args.yPrecision, |
| 4528 | args.xCursorPosition, args.yCursorPosition, displayTransform, |
Siarhei Vishniakou | 3218fc0 | 2023-06-15 20:41:02 -0700 | [diff] [blame] | 4529 | args.downTime, args.eventTime, args.getPointerCount(), |
| 4530 | args.pointerProperties.data(), args.pointerCoords.data()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4531 | |
| 4532 | policyFlags |= POLICY_FLAG_FILTERED; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4533 | if (!mPolicy.filterInputEvent(event, policyFlags)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4534 | return; // event was consumed by the filter |
| 4535 | } |
| 4536 | |
| 4537 | mLock.lock(); |
| 4538 | } |
| 4539 | |
| 4540 | // Just enqueue a new motion event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4541 | std::unique_ptr<MotionEntry> newEntry = |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4542 | std::make_unique<MotionEntry>(args.id, /*injectionState=*/nullptr, args.eventTime, |
| 4543 | args.deviceId, args.source, args.displayId, |
| 4544 | policyFlags, args.action, args.actionButton, |
| 4545 | args.flags, args.metaState, args.buttonState, |
| 4546 | args.classification, args.edgeFlags, args.xPrecision, |
| 4547 | args.yPrecision, args.xCursorPosition, |
| 4548 | args.yCursorPosition, args.downTime, |
| 4549 | args.pointerProperties, args.pointerCoords); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 4550 | if (mTracer) { |
| 4551 | newEntry->traceTracker = mTracer->traceInboundEvent(*newEntry); |
| 4552 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4553 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4554 | if (args.id != android::os::IInputConstants::INVALID_INPUT_EVENT_ID && |
| 4555 | IdGenerator::getSource(args.id) == IdGenerator::Source::INPUT_READER && |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 4556 | !mInputFilterEnabled) { |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4557 | const bool isDown = args.action == AMOTION_EVENT_ACTION_DOWN; |
Asmita Poddar | dd9a6cd | 2023-09-26 15:35:12 +0000 | [diff] [blame] | 4558 | std::set<InputDeviceUsageSource> sources = getUsageSourcesForMotionArgs(args); |
| 4559 | mLatencyTracker.trackListener(args.id, isDown, args.eventTime, args.readTime, |
| 4560 | args.deviceId, sources); |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 4561 | } |
| 4562 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4563 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4564 | mLock.unlock(); |
| 4565 | } // release lock |
| 4566 | |
| 4567 | if (needWake) { |
| 4568 | mLooper->wake(); |
| 4569 | } |
| 4570 | } |
| 4571 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4572 | void InputDispatcher::notifySensor(const NotifySensorArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4573 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4574 | ALOGD("notifySensor - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 4575 | " sensorType=%s", |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4576 | args.id, args.eventTime, args.deviceId, args.source, |
| 4577 | ftl::enum_string(args.sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4578 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4579 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4580 | bool needWake = false; |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4581 | { // acquire lock |
| 4582 | mLock.lock(); |
| 4583 | |
| 4584 | // Just enqueue a new sensor event. |
| 4585 | std::unique_ptr<SensorEntry> newEntry = |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4586 | std::make_unique<SensorEntry>(args.id, args.eventTime, args.deviceId, args.source, |
| 4587 | /* policyFlags=*/0, args.hwTimestamp, args.sensorType, |
| 4588 | args.accuracy, args.accuracyChanged, args.values); |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4589 | |
| 4590 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
| 4591 | mLock.unlock(); |
| 4592 | } // release lock |
| 4593 | |
| 4594 | if (needWake) { |
| 4595 | mLooper->wake(); |
| 4596 | } |
| 4597 | } |
| 4598 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4599 | void InputDispatcher::notifyVibratorState(const NotifyVibratorStateArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4600 | if (debugInboundEventDetails()) { |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4601 | ALOGD("notifyVibratorState - eventTime=%" PRId64 ", device=%d, isOn=%d", args.eventTime, |
| 4602 | args.deviceId, args.isOn); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4603 | } |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4604 | mPolicy.notifyVibratorState(args.deviceId, args.isOn); |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 4605 | } |
| 4606 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4607 | bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs& args) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4608 | return mInputFilterEnabled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4609 | } |
| 4610 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4611 | void InputDispatcher::notifySwitch(const NotifySwitchArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4612 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4613 | ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, " |
| 4614 | "switchMask=0x%08x", |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4615 | args.eventTime, args.policyFlags, args.switchValues, args.switchMask); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4616 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4617 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4618 | uint32_t policyFlags = args.policyFlags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4619 | policyFlags |= POLICY_FLAG_TRUSTED; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4620 | mPolicy.notifySwitch(args.eventTime, args.switchValues, args.switchMask, policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4621 | } |
| 4622 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4623 | void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4624 | if (debugInboundEventDetails()) { |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4625 | ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args.eventTime, |
| 4626 | args.deviceId); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4627 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4628 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4629 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4630 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4631 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4632 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4633 | std::unique_ptr<DeviceResetEntry> newEntry = |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4634 | std::make_unique<DeviceResetEntry>(args.id, args.eventTime, args.deviceId); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4635 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Siarhei Vishniakou | 1160ecd | 2023-06-28 15:57:47 -0700 | [diff] [blame] | 4636 | |
| 4637 | for (auto& [_, verifier] : mVerifiersByDisplay) { |
| 4638 | verifier.resetDevice(args.deviceId); |
| 4639 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4640 | } // release lock |
| 4641 | |
| 4642 | if (needWake) { |
| 4643 | mLooper->wake(); |
| 4644 | } |
| 4645 | } |
| 4646 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4647 | void InputDispatcher::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4648 | if (debugInboundEventDetails()) { |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4649 | ALOGD("notifyPointerCaptureChanged - eventTime=%" PRId64 ", enabled=%s", args.eventTime, |
| 4650 | args.request.enable ? "true" : "false"); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4651 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4652 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4653 | bool needWake = false; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4654 | { // acquire lock |
| 4655 | std::scoped_lock _l(mLock); |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4656 | auto entry = |
| 4657 | std::make_unique<PointerCaptureChangedEntry>(args.id, args.eventTime, args.request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4658 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 4659 | } // release lock |
| 4660 | |
| 4661 | if (needWake) { |
| 4662 | mLooper->wake(); |
| 4663 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4664 | } |
| 4665 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4666 | InputEventInjectionResult InputDispatcher::injectInputEvent(const InputEvent* event, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 4667 | std::optional<gui::Uid> targetUid, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4668 | InputEventInjectionSync syncMode, |
| 4669 | std::chrono::milliseconds timeout, |
| 4670 | uint32_t policyFlags) { |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4671 | Result<void> eventValidation = validateInputEvent(*event); |
| 4672 | if (!eventValidation.ok()) { |
| 4673 | LOG(INFO) << "Injection failed: invalid event: " << eventValidation.error(); |
| 4674 | return InputEventInjectionResult::FAILED; |
| 4675 | } |
| 4676 | |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4677 | if (debugInboundEventDetails()) { |
Siarhei Vishniakou | 827d1ac | 2023-07-21 16:37:51 -0700 | [diff] [blame] | 4678 | LOG(INFO) << __func__ << ": targetUid=" << toString(targetUid, &uidString) |
| 4679 | << ", syncMode=" << ftl::enum_string(syncMode) << ", timeout=" << timeout.count() |
| 4680 | << "ms, policyFlags=0x" << std::hex << policyFlags << std::dec |
| 4681 | << ", event=" << *event; |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4682 | } |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 4683 | 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] | 4684 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4685 | policyFlags |= POLICY_FLAG_INJECTED | POLICY_FLAG_TRUSTED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4686 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4687 | // 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] | 4688 | // that have gone through the InputFilter. If the event passed through the InputFilter, assign |
| 4689 | // the provided device id. If the InputFilter is accessibility, and it modifies or synthesizes |
| 4690 | // the injected event, it is responsible for setting POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY. |
| 4691 | // For those events, we will set FLAG_IS_ACCESSIBILITY_EVENT to allow apps to distinguish them |
| 4692 | // from events that originate from actual hardware. |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 4693 | DeviceId resolvedDeviceId = VIRTUAL_KEYBOARD_ID; |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4694 | if (policyFlags & POLICY_FLAG_FILTERED) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4695 | resolvedDeviceId = event->getDeviceId(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4696 | } |
| 4697 | |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4698 | const bool isAsync = syncMode == InputEventInjectionSync::NONE; |
| 4699 | auto injectionState = std::make_shared<InjectionState>(targetUid, isAsync); |
| 4700 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4701 | std::queue<std::unique_ptr<EventEntry>> injectedEntries; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4702 | switch (event->getType()) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4703 | case InputEventType::KEY: { |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4704 | const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event); |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4705 | const int32_t action = incomingKey.getAction(); |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4706 | int32_t flags = incomingKey.getFlags(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4707 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4708 | flags |= AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4709 | } |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4710 | int32_t keyCode = incomingKey.getKeyCode(); |
| 4711 | int32_t metaState = incomingKey.getMetaState(); |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4712 | KeyEvent keyEvent; |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4713 | keyEvent.initialize(incomingKey.getId(), resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4714 | incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode, |
| 4715 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), |
| 4716 | incomingKey.getDownTime(), incomingKey.getEventTime()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4717 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4718 | if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) { |
| 4719 | policyFlags |= POLICY_FLAG_VIRTUAL; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4720 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4721 | |
| 4722 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 4723 | android::base::Timer t; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4724 | mPolicy.interceptKeyBeforeQueueing(keyEvent, /*byref*/ policyFlags); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4725 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4726 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
| 4727 | std::to_string(t.duration().count()).c_str()); |
| 4728 | } |
| 4729 | } |
| 4730 | |
| 4731 | mLock.lock(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4732 | std::unique_ptr<KeyEntry> injectedEntry = |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4733 | std::make_unique<KeyEntry>(incomingKey.getId(), injectionState, |
| 4734 | incomingKey.getEventTime(), resolvedDeviceId, |
| 4735 | incomingKey.getSource(), incomingKey.getDisplayId(), |
| 4736 | policyFlags, action, flags, keyCode, |
| 4737 | incomingKey.getScanCode(), metaState, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4738 | incomingKey.getRepeatCount(), |
| 4739 | incomingKey.getDownTime()); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 4740 | if (mTracer) { |
| 4741 | injectedEntry->traceTracker = mTracer->traceInboundEvent(*injectedEntry); |
| 4742 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4743 | injectedEntries.push(std::move(injectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4744 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4745 | } |
| 4746 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4747 | case InputEventType::MOTION: { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4748 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4749 | const bool isPointerEvent = |
| 4750 | isFromSource(event->getSource(), AINPUT_SOURCE_CLASS_POINTER); |
| 4751 | // If a pointer event has no displayId specified, inject it to the default display. |
| 4752 | const uint32_t displayId = isPointerEvent && (event->getDisplayId() == ADISPLAY_ID_NONE) |
| 4753 | ? ADISPLAY_ID_DEFAULT |
| 4754 | : event->getDisplayId(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4755 | int32_t flags = motionEvent.getFlags(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4756 | |
| 4757 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4758 | nsecs_t eventTime = motionEvent.getEventTime(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4759 | android::base::Timer t; |
Yeabkal Wubshit | 88a9041 | 2023-12-21 18:23:04 -0800 | [diff] [blame] | 4760 | mPolicy.interceptMotionBeforeQueueing(displayId, motionEvent.getSource(), |
| 4761 | motionEvent.getAction(), eventTime, |
| 4762 | /*byref*/ policyFlags); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4763 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4764 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
| 4765 | std::to_string(t.duration().count()).c_str()); |
| 4766 | } |
| 4767 | } |
| 4768 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4769 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4770 | flags |= AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4771 | } |
| 4772 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4773 | mLock.lock(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4774 | const nsecs_t* sampleEventTimes = motionEvent.getSampleEventTimes(); |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4775 | const size_t pointerCount = motionEvent.getPointerCount(); |
| 4776 | const std::vector<PointerProperties> |
| 4777 | pointerProperties(motionEvent.getPointerProperties(), |
| 4778 | motionEvent.getPointerProperties() + pointerCount); |
| 4779 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4780 | const PointerCoords* samplePointerCoords = motionEvent.getSamplePointerCoords(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4781 | std::unique_ptr<MotionEntry> injectedEntry = |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4782 | std::make_unique<MotionEntry>(motionEvent.getId(), injectionState, |
| 4783 | *sampleEventTimes, resolvedDeviceId, |
| 4784 | motionEvent.getSource(), displayId, policyFlags, |
| 4785 | motionEvent.getAction(), |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4786 | motionEvent.getActionButton(), flags, |
| 4787 | motionEvent.getMetaState(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4788 | motionEvent.getButtonState(), |
| 4789 | motionEvent.getClassification(), |
| 4790 | motionEvent.getEdgeFlags(), |
| 4791 | motionEvent.getXPrecision(), |
| 4792 | motionEvent.getYPrecision(), |
| 4793 | motionEvent.getRawXCursorPosition(), |
| 4794 | motionEvent.getRawYCursorPosition(), |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4795 | motionEvent.getDownTime(), pointerProperties, |
| 4796 | std::vector<PointerCoords>(samplePointerCoords, |
| 4797 | samplePointerCoords + |
| 4798 | pointerCount)); |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4799 | transformMotionEntryForInjectionLocked(*injectedEntry, motionEvent.getTransform()); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 4800 | if (mTracer) { |
| 4801 | injectedEntry->traceTracker = mTracer->traceInboundEvent(*injectedEntry); |
| 4802 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4803 | injectedEntries.push(std::move(injectedEntry)); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4804 | for (size_t i = motionEvent.getHistorySize(); i > 0; i--) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4805 | sampleEventTimes += 1; |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4806 | samplePointerCoords += motionEvent.getPointerCount(); |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4807 | std::unique_ptr<MotionEntry> nextInjectedEntry = std::make_unique< |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4808 | MotionEntry>(motionEvent.getId(), injectionState, *sampleEventTimes, |
| 4809 | resolvedDeviceId, motionEvent.getSource(), displayId, |
| 4810 | policyFlags, motionEvent.getAction(), |
| 4811 | motionEvent.getActionButton(), flags, |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4812 | motionEvent.getMetaState(), motionEvent.getButtonState(), |
| 4813 | motionEvent.getClassification(), motionEvent.getEdgeFlags(), |
| 4814 | motionEvent.getXPrecision(), motionEvent.getYPrecision(), |
| 4815 | motionEvent.getRawXCursorPosition(), |
| 4816 | motionEvent.getRawYCursorPosition(), motionEvent.getDownTime(), |
| 4817 | pointerProperties, |
| 4818 | std::vector<PointerCoords>(samplePointerCoords, |
| 4819 | samplePointerCoords + |
| 4820 | pointerCount)); |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4821 | transformMotionEntryForInjectionLocked(*nextInjectedEntry, |
| 4822 | motionEvent.getTransform()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4823 | injectedEntries.push(std::move(nextInjectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4824 | } |
| 4825 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4826 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4827 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4828 | default: |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4829 | LOG(WARNING) << "Cannot inject " << ftl::enum_string(event->getType()) << " events"; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4830 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4831 | } |
| 4832 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4833 | bool needWake = false; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4834 | while (!injectedEntries.empty()) { |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 4835 | if (DEBUG_INJECTION) { |
Siarhei Vishniakou | 827d1ac | 2023-07-21 16:37:51 -0700 | [diff] [blame] | 4836 | LOG(INFO) << "Injecting " << injectedEntries.front()->getDescription(); |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 4837 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4838 | needWake |= enqueueInboundEventLocked(std::move(injectedEntries.front())); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4839 | injectedEntries.pop(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4840 | } |
| 4841 | |
| 4842 | mLock.unlock(); |
| 4843 | |
| 4844 | if (needWake) { |
| 4845 | mLooper->wake(); |
| 4846 | } |
| 4847 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4848 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4849 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4850 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4851 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4852 | if (syncMode == InputEventInjectionSync::NONE) { |
| 4853 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4854 | } else { |
| 4855 | for (;;) { |
| 4856 | injectionResult = injectionState->injectionResult; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4857 | if (injectionResult != InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4858 | break; |
| 4859 | } |
| 4860 | |
| 4861 | nsecs_t remainingTimeout = endTime - now(); |
| 4862 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4863 | if (DEBUG_INJECTION) { |
| 4864 | ALOGD("injectInputEvent - Timed out waiting for injection result " |
| 4865 | "to become available."); |
| 4866 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4867 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4868 | break; |
| 4869 | } |
| 4870 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4871 | mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4872 | } |
| 4873 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4874 | if (injectionResult == InputEventInjectionResult::SUCCEEDED && |
| 4875 | syncMode == InputEventInjectionSync::WAIT_FOR_FINISHED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4876 | while (injectionState->pendingForegroundDispatches != 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4877 | if (DEBUG_INJECTION) { |
| 4878 | ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", |
| 4879 | injectionState->pendingForegroundDispatches); |
| 4880 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4881 | nsecs_t remainingTimeout = endTime - now(); |
| 4882 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4883 | if (DEBUG_INJECTION) { |
| 4884 | ALOGD("injectInputEvent - Timed out waiting for pending foreground " |
| 4885 | "dispatches to finish."); |
| 4886 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4887 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4888 | break; |
| 4889 | } |
| 4890 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4891 | mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4892 | } |
| 4893 | } |
| 4894 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4895 | } // release lock |
| 4896 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4897 | if (DEBUG_INJECTION) { |
Siarhei Vishniakou | 827d1ac | 2023-07-21 16:37:51 -0700 | [diff] [blame] | 4898 | LOG(INFO) << "injectInputEvent - Finished with result " |
| 4899 | << ftl::enum_string(injectionResult); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4900 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4901 | |
| 4902 | return injectionResult; |
| 4903 | } |
| 4904 | |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4905 | std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4906 | std::array<uint8_t, 32> calculatedHmac; |
| 4907 | std::unique_ptr<VerifiedInputEvent> result; |
| 4908 | switch (event.getType()) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4909 | case InputEventType::KEY: { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4910 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event); |
| 4911 | VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent); |
| 4912 | result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4913 | calculatedHmac = sign(verifiedKeyEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4914 | break; |
| 4915 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4916 | case InputEventType::MOTION: { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4917 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event); |
| 4918 | VerifiedMotionEvent verifiedMotionEvent = |
| 4919 | verifiedMotionEventFromMotionEvent(motionEvent); |
| 4920 | result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4921 | calculatedHmac = sign(verifiedMotionEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4922 | break; |
| 4923 | } |
| 4924 | default: { |
| 4925 | ALOGE("Cannot verify events of type %" PRId32, event.getType()); |
| 4926 | return nullptr; |
| 4927 | } |
| 4928 | } |
| 4929 | if (calculatedHmac == INVALID_HMAC) { |
| 4930 | return nullptr; |
| 4931 | } |
tyiu | 1573a67 | 2023-02-21 22:38:32 +0000 | [diff] [blame] | 4932 | if (0 != CRYPTO_memcmp(calculatedHmac.data(), event.getHmac().data(), calculatedHmac.size())) { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4933 | return nullptr; |
| 4934 | } |
| 4935 | return result; |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4936 | } |
| 4937 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 4938 | void InputDispatcher::setInjectionResult(const EventEntry& entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4939 | InputEventInjectionResult injectionResult) { |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4940 | if (!entry.injectionState) { |
| 4941 | // Not an injected event. |
| 4942 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4943 | } |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4944 | |
| 4945 | InjectionState& injectionState = *entry.injectionState; |
| 4946 | if (DEBUG_INJECTION) { |
| 4947 | LOG(INFO) << "Setting input event injection result to " |
| 4948 | << ftl::enum_string(injectionResult); |
| 4949 | } |
| 4950 | |
| 4951 | if (injectionState.injectionIsAsync && !(entry.policyFlags & POLICY_FLAG_FILTERED)) { |
| 4952 | // Log the outcome since the injector did not wait for the injection result. |
| 4953 | switch (injectionResult) { |
| 4954 | case InputEventInjectionResult::SUCCEEDED: |
| 4955 | ALOGV("Asynchronous input event injection succeeded."); |
| 4956 | break; |
| 4957 | case InputEventInjectionResult::TARGET_MISMATCH: |
| 4958 | ALOGV("Asynchronous input event injection target mismatch."); |
| 4959 | break; |
| 4960 | case InputEventInjectionResult::FAILED: |
| 4961 | ALOGW("Asynchronous input event injection failed."); |
| 4962 | break; |
| 4963 | case InputEventInjectionResult::TIMED_OUT: |
| 4964 | ALOGW("Asynchronous input event injection timed out."); |
| 4965 | break; |
| 4966 | case InputEventInjectionResult::PENDING: |
| 4967 | ALOGE("Setting result to 'PENDING' for asynchronous injection"); |
| 4968 | break; |
| 4969 | } |
| 4970 | } |
| 4971 | |
| 4972 | injectionState.injectionResult = injectionResult; |
| 4973 | mInjectionResultAvailable.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4974 | } |
| 4975 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4976 | void InputDispatcher::transformMotionEntryForInjectionLocked( |
| 4977 | MotionEntry& entry, const ui::Transform& injectedTransform) const { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4978 | // Input injection works in the logical display coordinate space, but the input pipeline works |
| 4979 | // display space, so we need to transform the injected events accordingly. |
| 4980 | const auto it = mDisplayInfos.find(entry.displayId); |
| 4981 | if (it == mDisplayInfos.end()) return; |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4982 | const auto& transformToDisplay = it->second.transform.inverse() * injectedTransform; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4983 | |
Prabir Pradhan | d9a2ebe | 2022-07-20 19:25:13 +0000 | [diff] [blame] | 4984 | if (entry.xCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION && |
| 4985 | entry.yCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) { |
| 4986 | const vec2 cursor = |
| 4987 | MotionEvent::calculateTransformedXY(entry.source, transformToDisplay, |
| 4988 | {entry.xCursorPosition, entry.yCursorPosition}); |
| 4989 | entry.xCursorPosition = cursor.x; |
| 4990 | entry.yCursorPosition = cursor.y; |
| 4991 | } |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4992 | for (uint32_t i = 0; i < entry.getPointerCount(); i++) { |
Prabir Pradhan | 8e6ce22 | 2022-02-24 09:08:54 -0800 | [diff] [blame] | 4993 | entry.pointerCoords[i] = |
| 4994 | MotionEvent::calculateTransformedCoords(entry.source, transformToDisplay, |
| 4995 | entry.pointerCoords[i]); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4996 | } |
| 4997 | } |
| 4998 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 4999 | void InputDispatcher::incrementPendingForegroundDispatches(const EventEntry& entry) { |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 5000 | if (entry.injectionState) { |
| 5001 | entry.injectionState->pendingForegroundDispatches += 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5002 | } |
| 5003 | } |
| 5004 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 5005 | void InputDispatcher::decrementPendingForegroundDispatches(const EventEntry& entry) { |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 5006 | if (entry.injectionState) { |
| 5007 | entry.injectionState->pendingForegroundDispatches -= 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5008 | |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 5009 | if (entry.injectionState->pendingForegroundDispatches == 0) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5010 | mInjectionSyncFinished.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5011 | } |
| 5012 | } |
| 5013 | } |
| 5014 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5015 | const std::vector<sp<WindowInfoHandle>>& InputDispatcher::getWindowHandlesLocked( |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5016 | int32_t displayId) const { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5017 | static const std::vector<sp<WindowInfoHandle>> EMPTY_WINDOW_HANDLES; |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5018 | auto it = mWindowHandlesByDisplay.find(displayId); |
| 5019 | return it != mWindowHandlesByDisplay.end() ? it->second : EMPTY_WINDOW_HANDLES; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5020 | } |
| 5021 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5022 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 5023 | const sp<IBinder>& windowHandleToken, std::optional<int32_t> displayId) const { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 5024 | if (windowHandleToken == nullptr) { |
| 5025 | return nullptr; |
| 5026 | } |
| 5027 | |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 5028 | if (!displayId) { |
| 5029 | // Look through all displays. |
| 5030 | for (auto& it : mWindowHandlesByDisplay) { |
| 5031 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 5032 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 5033 | if (windowHandle->getToken() == windowHandleToken) { |
| 5034 | return windowHandle; |
| 5035 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5036 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5037 | } |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5038 | return nullptr; |
| 5039 | } |
| 5040 | |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 5041 | // Only look through the requested display. |
| 5042 | for (const sp<WindowInfoHandle>& windowHandle : getWindowHandlesLocked(*displayId)) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5043 | if (windowHandle->getToken() == windowHandleToken) { |
| 5044 | return windowHandle; |
| 5045 | } |
| 5046 | } |
| 5047 | return nullptr; |
| 5048 | } |
| 5049 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5050 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
| 5051 | const sp<WindowInfoHandle>& windowHandle) const { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 5052 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5053 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 5054 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 5055 | if (handle->getId() == windowHandle->getId() && |
| 5056 | handle->getToken() == windowHandle->getToken()) { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 5057 | if (windowHandle->getInfo()->displayId != it.first) { |
| 5058 | ALOGE("Found window %s in display %" PRId32 |
| 5059 | ", but it should belong to display %" PRId32, |
| 5060 | windowHandle->getName().c_str(), it.first, |
| 5061 | windowHandle->getInfo()->displayId); |
| 5062 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 5063 | return handle; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5064 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5065 | } |
| 5066 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 5067 | return nullptr; |
| 5068 | } |
| 5069 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5070 | sp<WindowInfoHandle> InputDispatcher::getFocusedWindowHandleLocked(int displayId) const { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 5071 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(displayId); |
| 5072 | return getWindowHandleLocked(focusedToken, displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5073 | } |
| 5074 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5075 | ui::Transform InputDispatcher::getTransformLocked(int32_t displayId) const { |
| 5076 | auto displayInfoIt = mDisplayInfos.find(displayId); |
| 5077 | return displayInfoIt != mDisplayInfos.end() ? displayInfoIt->second.transform |
| 5078 | : kIdentityTransform; |
| 5079 | } |
| 5080 | |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5081 | bool InputDispatcher::canWindowReceiveMotionLocked(const sp<WindowInfoHandle>& window, |
| 5082 | const MotionEntry& motionEntry) const { |
| 5083 | const WindowInfo& info = *window->getInfo(); |
| 5084 | |
| 5085 | // Skip spy window targets that are not valid for targeted injection. |
| 5086 | if (const auto err = verifyTargetedInjection(window, motionEntry); err) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5087 | return false; |
| 5088 | } |
| 5089 | |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5090 | if (info.inputConfig.test(WindowInfo::InputConfig::PAUSE_DISPATCHING)) { |
| 5091 | ALOGI("Not sending touch event to %s because it is paused", window->getName().c_str()); |
| 5092 | return false; |
| 5093 | } |
| 5094 | |
| 5095 | if (info.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) { |
| 5096 | ALOGW("Not sending touch gesture to %s because it has config NO_INPUT_CHANNEL", |
| 5097 | window->getName().c_str()); |
| 5098 | return false; |
| 5099 | } |
| 5100 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 5101 | std::shared_ptr<Connection> connection = getConnectionLocked(window->getToken()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5102 | if (connection == nullptr) { |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5103 | ALOGW("Not sending touch to %s because there's no corresponding connection", |
| 5104 | window->getName().c_str()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5105 | return false; |
| 5106 | } |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5107 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5108 | if (!connection->responsive) { |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5109 | 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] | 5110 | return false; |
| 5111 | } |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5112 | |
| 5113 | // Drop events that can't be trusted due to occlusion |
| 5114 | const auto [x, y] = resolveTouchedPosition(motionEntry); |
| 5115 | TouchOcclusionInfo occlusionInfo = computeTouchOcclusionInfoLocked(window, x, y); |
| 5116 | if (!isTouchTrustedLocked(occlusionInfo)) { |
| 5117 | if (DEBUG_TOUCH_OCCLUSION) { |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 5118 | ALOGD("Stack of obscuring windows during untrusted touch (%.1f, %.1f):", x, y); |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5119 | for (const auto& log : occlusionInfo.debugInfo) { |
| 5120 | ALOGD("%s", log.c_str()); |
| 5121 | } |
| 5122 | } |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 5123 | ALOGW("Dropping untrusted touch event due to %s/%s", occlusionInfo.obscuringPackage.c_str(), |
| 5124 | occlusionInfo.obscuringUid.toString().c_str()); |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5125 | return false; |
| 5126 | } |
| 5127 | |
| 5128 | // Drop touch events if requested by input feature |
| 5129 | if (shouldDropInput(motionEntry, window)) { |
| 5130 | return false; |
| 5131 | } |
| 5132 | |
Siarhei Vishniakou | f77f60a | 2023-10-23 17:26:05 -0700 | [diff] [blame] | 5133 | // Ignore touches if stylus is down anywhere on screen |
| 5134 | if (info.inputConfig.test(WindowInfo::InputConfig::GLOBAL_STYLUS_BLOCKS_TOUCH) && |
| 5135 | isStylusActiveInDisplay(info.displayId, mTouchStatesByDisplay)) { |
| 5136 | LOG(INFO) << "Dropping touch from " << window->getName() << " because stylus is active"; |
| 5137 | return false; |
| 5138 | } |
| 5139 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5140 | return true; |
| 5141 | } |
| 5142 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5143 | std::shared_ptr<InputChannel> InputDispatcher::getInputChannelLocked( |
| 5144 | const sp<IBinder>& token) const { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5145 | auto connectionIt = mConnectionsByToken.find(token); |
| 5146 | if (connectionIt == mConnectionsByToken.end()) { |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 5147 | return nullptr; |
| 5148 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5149 | return connectionIt->second->inputChannel; |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 5150 | } |
| 5151 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5152 | void InputDispatcher::updateWindowHandlesForDisplayLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5153 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
| 5154 | if (windowInfoHandles.empty()) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5155 | // Remove all handles on a display if there are no windows left. |
| 5156 | mWindowHandlesByDisplay.erase(displayId); |
| 5157 | return; |
| 5158 | } |
| 5159 | |
| 5160 | // Since we compare the pointer of input window handles across window updates, we need |
| 5161 | // 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] | 5162 | const std::vector<sp<WindowInfoHandle>>& oldHandles = getWindowHandlesLocked(displayId); |
| 5163 | std::unordered_map<int32_t /*id*/, sp<WindowInfoHandle>> oldHandlesById; |
| 5164 | for (const sp<WindowInfoHandle>& handle : oldHandles) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5165 | oldHandlesById[handle->getId()] = handle; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5166 | } |
| 5167 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5168 | std::vector<sp<WindowInfoHandle>> newHandles; |
| 5169 | for (const sp<WindowInfoHandle>& handle : windowInfoHandles) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5170 | const WindowInfo* info = handle->getInfo(); |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 5171 | if (getInputChannelLocked(handle->getToken()) == nullptr) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5172 | const bool noInputChannel = |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 5173 | info->inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5174 | const bool canReceiveInput = |
| 5175 | !info->inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE) || |
| 5176 | !info->inputConfig.test(WindowInfo::InputConfig::NOT_FOCUSABLE); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5177 | if (canReceiveInput && !noInputChannel) { |
John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 5178 | ALOGV("Window handle %s has no registered input channel", |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5179 | handle->getName().c_str()); |
Robert Carr | 2984b7a | 2020-04-13 17:06:45 -0700 | [diff] [blame] | 5180 | continue; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5181 | } |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5182 | } |
| 5183 | |
| 5184 | if (info->displayId != displayId) { |
| 5185 | ALOGE("Window %s updated by wrong display %d, should belong to display %d", |
| 5186 | handle->getName().c_str(), displayId, info->displayId); |
| 5187 | continue; |
| 5188 | } |
| 5189 | |
Robert Carr | edd1360 | 2020-04-13 17:24:34 -0700 | [diff] [blame] | 5190 | if ((oldHandlesById.find(handle->getId()) != oldHandlesById.end()) && |
| 5191 | (oldHandlesById.at(handle->getId())->getToken() == handle->getToken())) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5192 | const sp<WindowInfoHandle>& oldHandle = oldHandlesById.at(handle->getId()); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5193 | oldHandle->updateFrom(handle); |
| 5194 | newHandles.push_back(oldHandle); |
| 5195 | } else { |
| 5196 | newHandles.push_back(handle); |
| 5197 | } |
| 5198 | } |
| 5199 | |
| 5200 | // Insert or replace |
| 5201 | mWindowHandlesByDisplay[displayId] = newHandles; |
| 5202 | } |
| 5203 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5204 | /** |
| 5205 | * Called from InputManagerService, update window handle list by displayId that can receive input. |
| 5206 | * A window handle contains information about InputChannel, Touch Region, Types, Focused,... |
| 5207 | * If set an empty list, remove all handles from the specific display. |
| 5208 | * For focused handle, check if need to change and send a cancel event to previous one. |
| 5209 | * For removed handle, check if need to send a cancel event if already in touch. |
| 5210 | */ |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5211 | void InputDispatcher::setInputWindowsLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5212 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5213 | if (DEBUG_FOCUS) { |
| 5214 | std::string windowList; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5215 | for (const sp<WindowInfoHandle>& iwh : windowInfoHandles) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5216 | windowList += iwh->getName() + " "; |
| 5217 | } |
Siarhei Vishniakou | 366fb5b | 2023-12-06 11:23:41 -0800 | [diff] [blame] | 5218 | LOG(INFO) << "setInputWindows displayId=" << displayId << " " << windowList; |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5219 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5220 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 5221 | // Check preconditions for new input windows |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5222 | for (const sp<WindowInfoHandle>& window : windowInfoHandles) { |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 5223 | const WindowInfo& info = *window->getInfo(); |
| 5224 | |
| 5225 | // 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] | 5226 | const bool noInputWindow = info.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5227 | if (noInputWindow && window->getToken() != nullptr) { |
| 5228 | ALOGE("%s has feature NO_INPUT_WINDOW, but a non-null token. Clearing", |
| 5229 | window->getName().c_str()); |
| 5230 | window->releaseChannel(); |
| 5231 | } |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 5232 | |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 5233 | // Ensure all spy windows are trusted overlays |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5234 | LOG_ALWAYS_FATAL_IF(info.isSpy() && |
| 5235 | !info.inputConfig.test( |
| 5236 | WindowInfo::InputConfig::TRUSTED_OVERLAY), |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 5237 | "%s has feature SPY, but is not a trusted overlay.", |
| 5238 | window->getName().c_str()); |
| 5239 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 5240 | // Ensure all stylus interceptors are trusted overlays |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5241 | LOG_ALWAYS_FATAL_IF(info.interceptsStylus() && |
| 5242 | !info.inputConfig.test( |
| 5243 | WindowInfo::InputConfig::TRUSTED_OVERLAY), |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 5244 | "%s has feature INTERCEPTS_STYLUS, but is not a trusted overlay.", |
| 5245 | window->getName().c_str()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5246 | } |
| 5247 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5248 | // Copy old handles for release if they are no longer present. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5249 | const std::vector<sp<WindowInfoHandle>> oldWindowHandles = getWindowHandlesLocked(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5250 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5251 | updateWindowHandlesForDisplayLocked(windowInfoHandles, displayId); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5252 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5253 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5254 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5255 | std::optional<FocusResolver::FocusChanges> changes = |
| 5256 | mFocusResolver.setInputWindows(displayId, windowHandles); |
| 5257 | if (changes) { |
| 5258 | onFocusChangedLocked(*changes); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5259 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5260 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5261 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 5262 | mTouchStatesByDisplay.find(displayId); |
| 5263 | if (stateIt != mTouchStatesByDisplay.end()) { |
| 5264 | TouchState& state = stateIt->second; |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5265 | for (size_t i = 0; i < state.windows.size();) { |
| 5266 | TouchedWindow& touchedWindow = state.windows[i]; |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 5267 | if (getWindowHandleLocked(touchedWindow.windowHandle) == nullptr) { |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 5268 | LOG(INFO) << "Touched window was removed: " << touchedWindow.windowHandle->getName() |
| 5269 | << " in display %" << displayId; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5270 | std::shared_ptr<InputChannel> touchedInputChannel = |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5271 | getInputChannelLocked(touchedWindow.windowHandle->getToken()); |
| 5272 | if (touchedInputChannel != nullptr) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 5273 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5274 | "touched window was removed"); |
| 5275 | synthesizeCancelationEventsForInputChannelLocked(touchedInputChannel, options); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5276 | // Since we are about to drop the touch, cancel the events for the wallpaper as |
| 5277 | // well. |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5278 | if (touchedWindow.targetFlags.test(InputTarget::Flags::FOREGROUND) && |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5279 | touchedWindow.windowHandle->getInfo()->inputConfig.test( |
| 5280 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER)) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5281 | sp<WindowInfoHandle> wallpaper = state.getWallpaperWindow(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5282 | synthesizeCancelationEventsForWindowLocked(wallpaper, options); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5283 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5284 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5285 | state.windows.erase(state.windows.begin() + i); |
| 5286 | } else { |
| 5287 | ++i; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5288 | } |
| 5289 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5290 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5291 | // 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] | 5292 | // could just clear the state here. |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 5293 | if (mDragState && mDragState->dragWindow->getInfo()->displayId == displayId && |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5294 | std::find(windowHandles.begin(), windowHandles.end(), mDragState->dragWindow) == |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5295 | windowHandles.end()) { |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 5296 | ALOGI("Drag window went away: %s", mDragState->dragWindow->getName().c_str()); |
| 5297 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5298 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5299 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5300 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 5301 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5302 | // Release information for windows that are no longer present. |
| 5303 | // This ensures that unused input channels are released promptly. |
| 5304 | // Otherwise, they might stick around until the window handle is destroyed |
| 5305 | // which might not happen until the next GC. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5306 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 5307 | if (getWindowHandleLocked(oldWindowHandle) == nullptr) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5308 | if (DEBUG_FOCUS) { |
| 5309 | ALOGD("Window went away: %s", oldWindowHandle->getName().c_str()); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 5310 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5311 | oldWindowHandle->releaseChannel(); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 5312 | } |
chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 5313 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5314 | } |
| 5315 | |
| 5316 | void InputDispatcher::setFocusedApplication( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5317 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5318 | if (DEBUG_FOCUS) { |
| 5319 | ALOGD("setFocusedApplication displayId=%" PRId32 " %s", displayId, |
| 5320 | inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>"); |
| 5321 | } |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 5322 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5323 | std::scoped_lock _l(mLock); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 5324 | setFocusedApplicationLocked(displayId, inputApplicationHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5325 | } // release lock |
| 5326 | |
| 5327 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5328 | mLooper->wake(); |
| 5329 | } |
| 5330 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 5331 | void InputDispatcher::setFocusedApplicationLocked( |
| 5332 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
| 5333 | std::shared_ptr<InputApplicationHandle> oldFocusedApplicationHandle = |
| 5334 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 5335 | |
| 5336 | if (sharedPointersEqual(oldFocusedApplicationHandle, inputApplicationHandle)) { |
| 5337 | return; // This application is already focused. No need to wake up or change anything. |
| 5338 | } |
| 5339 | |
| 5340 | // Set the new application handle. |
| 5341 | if (inputApplicationHandle != nullptr) { |
| 5342 | mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle; |
| 5343 | } else { |
| 5344 | mFocusedApplicationHandlesByDisplay.erase(displayId); |
| 5345 | } |
| 5346 | |
| 5347 | // No matter what the old focused application was, stop waiting on it because it is |
| 5348 | // no longer focused. |
| 5349 | resetNoFocusedWindowTimeoutLocked(); |
| 5350 | } |
| 5351 | |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 5352 | void InputDispatcher::setMinTimeBetweenUserActivityPokes(std::chrono::milliseconds interval) { |
| 5353 | if (interval.count() < 0) { |
| 5354 | LOG_ALWAYS_FATAL("Minimum time between user activity pokes should be >= 0"); |
| 5355 | } |
| 5356 | std::scoped_lock _l(mLock); |
| 5357 | mMinTimeBetweenUserActivityPokes = interval; |
| 5358 | } |
| 5359 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5360 | /** |
| 5361 | * Sets the focused display, which is responsible for receiving focus-dispatched input events where |
| 5362 | * the display not specified. |
| 5363 | * |
| 5364 | * We track any unreleased events for each window. If a window loses the ability to receive the |
| 5365 | * released event, we will send a cancel event to it. So when the focused display is changed, we |
| 5366 | * cancel all the unreleased display-unspecified events for the focused window on the old focused |
| 5367 | * display. The display-specified events won't be affected. |
| 5368 | */ |
| 5369 | void InputDispatcher::setFocusedDisplay(int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5370 | if (DEBUG_FOCUS) { |
| 5371 | ALOGD("setFocusedDisplay displayId=%" PRId32, displayId); |
| 5372 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5373 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5374 | std::scoped_lock _l(mLock); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5375 | |
| 5376 | if (mFocusedDisplayId != displayId) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5377 | sp<IBinder> oldFocusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5378 | mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5379 | if (oldFocusedWindowToken != nullptr) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5380 | std::shared_ptr<InputChannel> inputChannel = |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5381 | getInputChannelLocked(oldFocusedWindowToken); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5382 | if (inputChannel != nullptr) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5383 | CancelationOptions |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 5384 | options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5385 | "The display which contains this window no longer has focus."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5386 | options.displayId = ADISPLAY_ID_NONE; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5387 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
| 5388 | } |
| 5389 | } |
| 5390 | mFocusedDisplayId = displayId; |
| 5391 | |
Chris Ye | 3c2d6f5 | 2020-08-09 10:39:48 -0700 | [diff] [blame] | 5392 | // Find new focused window and validate |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5393 | sp<IBinder> newFocusedWindowToken = mFocusResolver.getFocusedWindowToken(displayId); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5394 | sendFocusChangedCommandLocked(oldFocusedWindowToken, newFocusedWindowToken); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5395 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5396 | if (newFocusedWindowToken == nullptr) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5397 | ALOGW("Focused display #%" PRId32 " does not have a focused window.", displayId); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5398 | if (mFocusResolver.hasFocusedWindowTokens()) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5399 | ALOGE("But another display has a focused window\n%s", |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5400 | mFocusResolver.dumpFocusedWindows().c_str()); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5401 | } |
| 5402 | } |
| 5403 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5404 | } // release lock |
| 5405 | |
| 5406 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5407 | mLooper->wake(); |
| 5408 | } |
| 5409 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5410 | void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5411 | if (DEBUG_FOCUS) { |
| 5412 | ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); |
| 5413 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5414 | |
| 5415 | bool changed; |
| 5416 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5417 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5418 | |
| 5419 | if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { |
| 5420 | if (mDispatchFrozen && !frozen) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5421 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5422 | } |
| 5423 | |
| 5424 | if (mDispatchEnabled && !enabled) { |
| 5425 | resetAndDropEverythingLocked("dispatcher is being disabled"); |
| 5426 | } |
| 5427 | |
| 5428 | mDispatchEnabled = enabled; |
| 5429 | mDispatchFrozen = frozen; |
| 5430 | changed = true; |
| 5431 | } else { |
| 5432 | changed = false; |
| 5433 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5434 | } // release lock |
| 5435 | |
| 5436 | if (changed) { |
| 5437 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5438 | mLooper->wake(); |
| 5439 | } |
| 5440 | } |
| 5441 | |
| 5442 | void InputDispatcher::setInputFilterEnabled(bool enabled) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5443 | if (DEBUG_FOCUS) { |
| 5444 | ALOGD("setInputFilterEnabled: enabled=%d", enabled); |
| 5445 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5446 | |
| 5447 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5448 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5449 | |
| 5450 | if (mInputFilterEnabled == enabled) { |
| 5451 | return; |
| 5452 | } |
| 5453 | |
| 5454 | mInputFilterEnabled = enabled; |
| 5455 | resetAndDropEverythingLocked("input filter is being enabled or disabled"); |
| 5456 | } // release lock |
| 5457 | |
| 5458 | // Wake up poll loop since there might be work to do to drop everything. |
| 5459 | mLooper->wake(); |
| 5460 | } |
| 5461 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5462 | bool InputDispatcher::setInTouchMode(bool inTouchMode, gui::Pid pid, gui::Uid uid, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 5463 | bool hasPermission, int32_t displayId) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5464 | bool needWake = false; |
| 5465 | { |
| 5466 | std::scoped_lock lock(mLock); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5467 | ALOGD_IF(DEBUG_TOUCH_MODE, |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5468 | "Request to change touch mode to %s (calling pid=%s, uid=%s, " |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5469 | "hasPermission=%s, target displayId=%d, mTouchModePerDisplay[displayId]=%s)", |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5470 | toString(inTouchMode), pid.toString().c_str(), uid.toString().c_str(), |
| 5471 | toString(hasPermission), displayId, |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5472 | mTouchModePerDisplay.count(displayId) == 0 |
| 5473 | ? "not set" |
| 5474 | : std::to_string(mTouchModePerDisplay[displayId]).c_str()); |
| 5475 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5476 | auto touchModeIt = mTouchModePerDisplay.find(displayId); |
| 5477 | if (touchModeIt != mTouchModePerDisplay.end() && touchModeIt->second == inTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5478 | return false; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5479 | } |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5480 | if (!hasPermission) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 5481 | if (!focusedWindowIsOwnedByLocked(pid, uid) && |
| 5482 | !recentWindowsAreOwnedByLocked(pid, uid)) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5483 | 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] | 5484 | "window nor none of the previously interacted window", |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5485 | pid.toString().c_str(), uid.toString().c_str()); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5486 | return false; |
| 5487 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5488 | } |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5489 | mTouchModePerDisplay[displayId] = inTouchMode; |
| 5490 | auto entry = std::make_unique<TouchModeEntry>(mIdGenerator.nextId(), now(), inTouchMode, |
| 5491 | displayId); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5492 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 5493 | } // release lock |
| 5494 | |
| 5495 | if (needWake) { |
| 5496 | mLooper->wake(); |
| 5497 | } |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5498 | return true; |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 5499 | } |
| 5500 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5501 | bool InputDispatcher::focusedWindowIsOwnedByLocked(gui::Pid pid, gui::Uid uid) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 5502 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
| 5503 | if (focusedToken == nullptr) { |
| 5504 | return false; |
| 5505 | } |
| 5506 | sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(focusedToken); |
| 5507 | return isWindowOwnedBy(windowHandle, pid, uid); |
| 5508 | } |
| 5509 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5510 | bool InputDispatcher::recentWindowsAreOwnedByLocked(gui::Pid pid, gui::Uid uid) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 5511 | return std::find_if(mInteractionConnectionTokens.begin(), mInteractionConnectionTokens.end(), |
| 5512 | [&](const sp<IBinder>& connectionToken) REQUIRES(mLock) { |
| 5513 | const sp<WindowInfoHandle> windowHandle = |
| 5514 | getWindowHandleLocked(connectionToken); |
| 5515 | return isWindowOwnedBy(windowHandle, pid, uid); |
| 5516 | }) != mInteractionConnectionTokens.end(); |
| 5517 | } |
| 5518 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 5519 | void InputDispatcher::setMaximumObscuringOpacityForTouch(float opacity) { |
| 5520 | if (opacity < 0 || opacity > 1) { |
| 5521 | LOG_ALWAYS_FATAL("Maximum obscuring opacity for touch should be >= 0 and <= 1"); |
| 5522 | return; |
| 5523 | } |
| 5524 | |
| 5525 | std::scoped_lock lock(mLock); |
| 5526 | mMaximumObscuringOpacityForTouch = opacity; |
| 5527 | } |
| 5528 | |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5529 | std::tuple<TouchState*, TouchedWindow*, int32_t /*displayId*/> |
| 5530 | InputDispatcher::findTouchStateWindowAndDisplayLocked(const sp<IBinder>& token) { |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5531 | for (auto& [displayId, state] : mTouchStatesByDisplay) { |
| 5532 | for (TouchedWindow& w : state.windows) { |
| 5533 | if (w.windowHandle->getToken() == token) { |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5534 | return std::make_tuple(&state, &w, displayId); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5535 | } |
| 5536 | } |
| 5537 | } |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5538 | return std::make_tuple(nullptr, nullptr, ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5539 | } |
| 5540 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5541 | bool InputDispatcher::transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken, |
| 5542 | bool isDragDrop) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 5543 | if (fromToken == toToken) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5544 | if (DEBUG_FOCUS) { |
| 5545 | ALOGD("Trivial transfer to same window."); |
| 5546 | } |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 5547 | return true; |
| 5548 | } |
| 5549 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5550 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5551 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5552 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5553 | // Find the target touch state and touched window by fromToken. |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5554 | auto [state, touchedWindow, displayId] = findTouchStateWindowAndDisplayLocked(fromToken); |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5555 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5556 | if (state == nullptr || touchedWindow == nullptr) { |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5557 | ALOGD("Touch transfer failed because from window is not being touched."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5558 | return false; |
| 5559 | } |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5560 | std::set<int32_t> deviceIds = touchedWindow->getTouchingDeviceIds(); |
| 5561 | if (deviceIds.size() != 1) { |
Siarhei Vishniakou | 827d1ac | 2023-07-21 16:37:51 -0700 | [diff] [blame] | 5562 | LOG(INFO) << "Can't transfer touch. Currently touching devices: " << dumpSet(deviceIds) |
| 5563 | << " for window: " << touchedWindow->dump(); |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5564 | return false; |
| 5565 | } |
| 5566 | const int32_t deviceId = *deviceIds.begin(); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5567 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5568 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(toToken, displayId); |
| 5569 | if (toWindowHandle == nullptr) { |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5570 | ALOGW("Cannot transfer touch because to window not found."); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5571 | return false; |
| 5572 | } |
| 5573 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5574 | if (DEBUG_FOCUS) { |
| 5575 | ALOGD("transferTouchFocus: fromWindowHandle=%s, toWindowHandle=%s", |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5576 | touchedWindow->windowHandle->getName().c_str(), |
| 5577 | toWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5578 | } |
| 5579 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5580 | // Erase old window. |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5581 | ftl::Flags<InputTarget::Flags> oldTargetFlags = touchedWindow->targetFlags; |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 5582 | std::vector<PointerProperties> pointers = touchedWindow->getTouchingPointers(deviceId); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5583 | sp<WindowInfoHandle> fromWindowHandle = touchedWindow->windowHandle; |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5584 | state->removeWindowByToken(fromToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5585 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5586 | // Add new window. |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 5587 | nsecs_t downTimeInTarget = now(); |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5588 | ftl::Flags<InputTarget::Flags> newTargetFlags = |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 5589 | oldTargetFlags & (InputTarget::Flags::SPLIT); |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 5590 | if (canReceiveForegroundTouches(*toWindowHandle->getInfo())) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5591 | newTargetFlags |= InputTarget::Flags::FOREGROUND; |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 5592 | } |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 5593 | state->addOrUpdateWindow(toWindowHandle, InputTarget::DispatchMode::AS_IS, newTargetFlags, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 5594 | deviceId, pointers, downTimeInTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5595 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5596 | // Store the dragging window. |
| 5597 | if (isDragDrop) { |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 5598 | if (pointers.size() != 1) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 5599 | ALOGW("The drag and drop cannot be started when there is no pointer or more than 1" |
| 5600 | " pointer on the window."); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 5601 | return false; |
| 5602 | } |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 5603 | // Track the pointer id for drag window and generate the drag state. |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 5604 | const size_t id = pointers.begin()->id; |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 5605 | mDragState = std::make_unique<DragState>(toWindowHandle, id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5606 | } |
| 5607 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5608 | // Synthesize cancel for old window and down for new window. |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 5609 | std::shared_ptr<Connection> fromConnection = getConnectionLocked(fromToken); |
| 5610 | std::shared_ptr<Connection> toConnection = getConnectionLocked(toToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5611 | if (fromConnection != nullptr && toConnection != nullptr) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5612 | fromConnection->inputState.mergePointerStateTo(toConnection->inputState); |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5613 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
| 5614 | "transferring touch from this window to another window"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5615 | synthesizeCancelationEventsForConnectionLocked(fromConnection, options); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5616 | synthesizePointerDownEventsForConnectionLocked(downTimeInTarget, toConnection, |
| 5617 | newTargetFlags); |
| 5618 | |
| 5619 | // Check if the wallpaper window should deliver the corresponding event. |
| 5620 | transferWallpaperTouch(oldTargetFlags, newTargetFlags, fromWindowHandle, toWindowHandle, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 5621 | *state, deviceId, pointers); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5622 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5623 | } // release lock |
| 5624 | |
| 5625 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5626 | mLooper->wake(); |
| 5627 | return true; |
| 5628 | } |
| 5629 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5630 | /** |
| 5631 | * Get the touched foreground window on the given display. |
| 5632 | * Return null if there are no windows touched on that display, or if more than one foreground |
| 5633 | * window is being touched. |
| 5634 | */ |
| 5635 | sp<WindowInfoHandle> InputDispatcher::findTouchedForegroundWindowLocked(int32_t displayId) const { |
| 5636 | auto stateIt = mTouchStatesByDisplay.find(displayId); |
| 5637 | if (stateIt == mTouchStatesByDisplay.end()) { |
| 5638 | ALOGI("No touch state on display %" PRId32, displayId); |
| 5639 | return nullptr; |
| 5640 | } |
| 5641 | |
| 5642 | const TouchState& state = stateIt->second; |
| 5643 | sp<WindowInfoHandle> touchedForegroundWindow; |
| 5644 | // If multiple foreground windows are touched, return nullptr |
| 5645 | for (const TouchedWindow& window : state.windows) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5646 | if (window.targetFlags.test(InputTarget::Flags::FOREGROUND)) { |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5647 | if (touchedForegroundWindow != nullptr) { |
| 5648 | ALOGI("Two or more foreground windows: %s and %s", |
| 5649 | touchedForegroundWindow->getName().c_str(), |
| 5650 | window.windowHandle->getName().c_str()); |
| 5651 | return nullptr; |
| 5652 | } |
| 5653 | touchedForegroundWindow = window.windowHandle; |
| 5654 | } |
| 5655 | } |
| 5656 | return touchedForegroundWindow; |
| 5657 | } |
| 5658 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5659 | // Binder call |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5660 | bool InputDispatcher::transferTouch(const sp<IBinder>& destChannelToken, int32_t displayId) { |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5661 | sp<IBinder> fromToken; |
| 5662 | { // acquire lock |
| 5663 | std::scoped_lock _l(mLock); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5664 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(destChannelToken, displayId); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5665 | if (toWindowHandle == nullptr) { |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5666 | ALOGW("Could not find window associated with token=%p on display %" PRId32, |
| 5667 | destChannelToken.get(), displayId); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5668 | return false; |
| 5669 | } |
| 5670 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5671 | sp<WindowInfoHandle> from = findTouchedForegroundWindowLocked(displayId); |
| 5672 | if (from == nullptr) { |
| 5673 | ALOGE("Could not find a source window in %s for %p", __func__, destChannelToken.get()); |
| 5674 | return false; |
| 5675 | } |
| 5676 | |
| 5677 | fromToken = from->getToken(); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5678 | } // release lock |
| 5679 | |
| 5680 | return transferTouchFocus(fromToken, destChannelToken); |
| 5681 | } |
| 5682 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5683 | void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5684 | if (DEBUG_FOCUS) { |
| 5685 | ALOGD("Resetting and dropping all events (%s).", reason); |
| 5686 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5687 | |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 5688 | CancelationOptions options(CancelationOptions::Mode::CANCEL_ALL_EVENTS, reason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5689 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 5690 | |
| 5691 | resetKeyRepeatLocked(); |
| 5692 | releasePendingEventLocked(); |
| 5693 | drainInboundQueueLocked(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5694 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5695 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5696 | mAnrTracker.clear(); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5697 | mTouchStatesByDisplay.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5698 | } |
| 5699 | |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 5700 | void InputDispatcher::logDispatchStateLocked() const { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5701 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5702 | dumpDispatchStateLocked(dump); |
| 5703 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5704 | std::istringstream stream(dump); |
| 5705 | std::string line; |
| 5706 | |
| 5707 | while (std::getline(stream, line, '\n')) { |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 5708 | ALOGI("%s", line.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5709 | } |
| 5710 | } |
| 5711 | |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 5712 | std::string InputDispatcher::dumpPointerCaptureStateLocked() const { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5713 | std::string dump; |
| 5714 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5715 | dump += StringPrintf(INDENT "Pointer Capture Requested: %s\n", |
| 5716 | toString(mCurrentPointerCaptureRequest.enable)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5717 | |
| 5718 | std::string windowName = "None"; |
| 5719 | if (mWindowTokenWithPointerCapture) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5720 | const sp<WindowInfoHandle> captureWindowHandle = |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5721 | getWindowHandleLocked(mWindowTokenWithPointerCapture); |
| 5722 | windowName = captureWindowHandle ? captureWindowHandle->getName().c_str() |
| 5723 | : "token has capture without window"; |
| 5724 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5725 | 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] | 5726 | |
| 5727 | return dump; |
| 5728 | } |
| 5729 | |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 5730 | void InputDispatcher::dumpDispatchStateLocked(std::string& dump) const { |
Siarhei Vishniakou | 043a3ec | 2019-05-01 11:30:46 -0700 | [diff] [blame] | 5731 | dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled)); |
| 5732 | dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen)); |
| 5733 | dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5734 | dump += StringPrintf(INDENT "FocusedDisplayId: %" PRId32 "\n", mFocusedDisplayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5735 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5736 | if (!mFocusedApplicationHandlesByDisplay.empty()) { |
| 5737 | dump += StringPrintf(INDENT "FocusedApplications:\n"); |
| 5738 | for (auto& it : mFocusedApplicationHandlesByDisplay) { |
| 5739 | const int32_t displayId = it.first; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5740 | const std::shared_ptr<InputApplicationHandle>& applicationHandle = it.second; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5741 | const std::chrono::duration timeout = |
| 5742 | applicationHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5743 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5744 | ", name='%s', dispatchingTimeout=%" PRId64 "ms\n", |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5745 | displayId, applicationHandle->getName().c_str(), millis(timeout)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5746 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5747 | } else { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5748 | dump += StringPrintf(INDENT "FocusedApplications: <none>\n"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5749 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5750 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5751 | dump += mFocusResolver.dump(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5752 | dump += dumpPointerCaptureStateLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5753 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5754 | if (!mTouchStatesByDisplay.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5755 | dump += StringPrintf(INDENT "TouchStatesByDisplay:\n"); |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5756 | for (const auto& [displayId, state] : mTouchStatesByDisplay) { |
Siarhei Vishniakou | 6e1e987 | 2022-11-08 17:51:35 -0800 | [diff] [blame] | 5757 | std::string touchStateDump = addLinePrefix(state.dump(), INDENT2); |
| 5758 | dump += INDENT2 + std::to_string(displayId) + " : " + touchStateDump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5759 | } |
| 5760 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5761 | dump += INDENT "TouchStates: <no displays touched>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5762 | } |
| 5763 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5764 | if (mDragState) { |
| 5765 | dump += StringPrintf(INDENT "DragState:\n"); |
| 5766 | mDragState->dump(dump, INDENT2); |
| 5767 | } |
| 5768 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5769 | if (!mWindowHandlesByDisplay.empty()) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5770 | for (const auto& [displayId, windowHandles] : mWindowHandlesByDisplay) { |
| 5771 | dump += StringPrintf(INDENT "Display: %" PRId32 "\n", displayId); |
| 5772 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
| 5773 | const auto& displayInfo = it->second; |
| 5774 | dump += StringPrintf(INDENT2 "logicalSize=%dx%d\n", displayInfo.logicalWidth, |
| 5775 | displayInfo.logicalHeight); |
| 5776 | displayInfo.transform.dump(dump, "transform", INDENT4); |
| 5777 | } else { |
| 5778 | dump += INDENT2 "No DisplayInfo found!\n"; |
| 5779 | } |
| 5780 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5781 | if (!windowHandles.empty()) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5782 | dump += INDENT2 "Windows:\n"; |
| 5783 | for (size_t i = 0; i < windowHandles.size(); i++) { |
Siarhei Vishniakou | 366fb5b | 2023-12-06 11:23:41 -0800 | [diff] [blame] | 5784 | dump += StringPrintf(INDENT3 "%zu: %s", i, |
| 5785 | streamableToString(*windowHandles[i]).c_str()); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5786 | } |
| 5787 | } else { |
| 5788 | dump += INDENT2 "Windows: <none>\n"; |
| 5789 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5790 | } |
| 5791 | } else { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5792 | dump += INDENT "Displays: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5793 | } |
| 5794 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5795 | if (!mGlobalMonitorsByDisplay.empty()) { |
| 5796 | for (const auto& [displayId, monitors] : mGlobalMonitorsByDisplay) { |
| 5797 | dump += StringPrintf(INDENT "Global monitors on display %d:\n", displayId); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5798 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5799 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5800 | } else { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5801 | dump += INDENT "Global Monitors: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5802 | } |
| 5803 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5804 | const nsecs_t currentTime = now(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5805 | |
| 5806 | // Dump recently dispatched or dropped events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5807 | if (!mRecentQueue.empty()) { |
| 5808 | dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size()); |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 5809 | for (const std::shared_ptr<const EventEntry>& entry : mRecentQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5810 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5811 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5812 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5813 | } |
| 5814 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5815 | dump += INDENT "RecentQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5816 | } |
| 5817 | |
| 5818 | // Dump event currently being dispatched. |
| 5819 | if (mPendingEvent) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5820 | dump += INDENT "PendingEvent:\n"; |
| 5821 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5822 | dump += mPendingEvent->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5823 | dump += StringPrintf(", age=%" PRId64 "ms\n", |
| 5824 | ns2ms(currentTime - mPendingEvent->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5825 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5826 | dump += INDENT "PendingEvent: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5827 | } |
| 5828 | |
| 5829 | // Dump inbound events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5830 | if (!mInboundQueue.empty()) { |
| 5831 | dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size()); |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 5832 | for (const std::shared_ptr<const EventEntry>& entry : mInboundQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5833 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5834 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5835 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5836 | } |
| 5837 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5838 | dump += INDENT "InboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5839 | } |
| 5840 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5841 | if (!mCommandQueue.empty()) { |
| 5842 | dump += StringPrintf(INDENT "CommandQueue: size=%zu\n", mCommandQueue.size()); |
| 5843 | } else { |
| 5844 | dump += INDENT "CommandQueue: <empty>\n"; |
| 5845 | } |
| 5846 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5847 | if (!mConnectionsByToken.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5848 | dump += INDENT "Connections:\n"; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5849 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5850 | dump += StringPrintf(INDENT2 "%i: channelName='%s', windowName='%s', " |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5851 | "status=%s, monitor=%s, responsive=%s\n", |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5852 | connection->inputChannel->getFd().get(), |
| 5853 | connection->getInputChannelName().c_str(), |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5854 | connection->getWindowName().c_str(), |
| 5855 | ftl::enum_string(connection->status).c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5856 | toString(connection->monitor), toString(connection->responsive)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5857 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5858 | if (!connection->outboundQueue.empty()) { |
| 5859 | dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n", |
| 5860 | connection->outboundQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5861 | dump += dumpQueue(connection->outboundQueue, currentTime); |
| 5862 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5863 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5864 | dump += INDENT3 "OutboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5865 | } |
| 5866 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5867 | if (!connection->waitQueue.empty()) { |
| 5868 | dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n", |
| 5869 | connection->waitQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5870 | dump += dumpQueue(connection->waitQueue, currentTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5871 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5872 | dump += INDENT3 "WaitQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5873 | } |
Siarhei Vishniakou | 366fb5b | 2023-12-06 11:23:41 -0800 | [diff] [blame] | 5874 | std::string inputStateDump = streamableToString(connection->inputState); |
| 5875 | if (!inputStateDump.empty()) { |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 5876 | dump += INDENT3 "InputState: "; |
Siarhei Vishniakou | 366fb5b | 2023-12-06 11:23:41 -0800 | [diff] [blame] | 5877 | dump += inputStateDump + "\n"; |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 5878 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5879 | } |
| 5880 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5881 | dump += INDENT "Connections: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5882 | } |
| 5883 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5884 | if (!mTouchModePerDisplay.empty()) { |
| 5885 | dump += INDENT "TouchModePerDisplay:\n"; |
| 5886 | for (const auto& [displayId, touchMode] : mTouchModePerDisplay) { |
| 5887 | dump += StringPrintf(INDENT2 "Display: %" PRId32 " TouchMode: %s\n", displayId, |
| 5888 | std::to_string(touchMode).c_str()); |
| 5889 | } |
| 5890 | } else { |
| 5891 | dump += INDENT "TouchModePerDisplay: <none>\n"; |
| 5892 | } |
| 5893 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5894 | dump += INDENT "Configuration:\n"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5895 | dump += StringPrintf(INDENT2 "KeyRepeatDelay: %" PRId64 "ms\n", ns2ms(mConfig.keyRepeatDelay)); |
| 5896 | dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n", |
| 5897 | ns2ms(mConfig.keyRepeatTimeout)); |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 5898 | dump += mLatencyTracker.dump(INDENT2); |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 5899 | dump += mLatencyAggregator.dump(INDENT2); |
Prabir Pradhan | abcdf5c | 2023-12-15 07:30:22 +0000 | [diff] [blame] | 5900 | dump += INDENT "InputTracer: "; |
| 5901 | dump += mTracer == nullptr ? "Disabled" : "Enabled"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5902 | } |
| 5903 | |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 5904 | void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) const { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5905 | const size_t numMonitors = monitors.size(); |
| 5906 | for (size_t i = 0; i < numMonitors; i++) { |
| 5907 | const Monitor& monitor = monitors[i]; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5908 | const std::shared_ptr<InputChannel>& channel = monitor.inputChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5909 | dump += StringPrintf(INDENT2 "%zu: '%s', ", i, channel->getName().c_str()); |
| 5910 | dump += "\n"; |
| 5911 | } |
| 5912 | } |
| 5913 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5914 | class LooperEventCallback : public LooperCallback { |
| 5915 | public: |
| 5916 | LooperEventCallback(std::function<int(int events)> callback) : mCallback(callback) {} |
| 5917 | int handleEvent(int /*fd*/, int events, void* /*data*/) override { return mCallback(events); } |
| 5918 | |
| 5919 | private: |
| 5920 | std::function<int(int events)> mCallback; |
| 5921 | }; |
| 5922 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5923 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputChannel(const std::string& name) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5924 | if (DEBUG_CHANNEL_CREATION) { |
| 5925 | ALOGD("channel '%s' ~ createInputChannel", name.c_str()); |
| 5926 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5927 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5928 | std::unique_ptr<InputChannel> serverChannel; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5929 | std::unique_ptr<InputChannel> clientChannel; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5930 | status_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5931 | |
| 5932 | if (result) { |
| 5933 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5934 | } |
| 5935 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5936 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5937 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5938 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Tomasz Wasilczyk | 3202460 | 2023-11-16 10:17:54 -0800 | [diff] [blame] | 5939 | auto&& fd = serverChannel->getFd(); |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 5940 | std::shared_ptr<Connection> connection = |
| 5941 | std::make_shared<Connection>(std::move(serverChannel), /*monitor=*/false, |
| 5942 | mIdGenerator); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5943 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5944 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5945 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5946 | } |
| 5947 | mConnectionsByToken.emplace(token, connection); |
| 5948 | |
| 5949 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5950 | this, std::placeholders::_1, token); |
| 5951 | |
Tomasz Wasilczyk | 3202460 | 2023-11-16 10:17:54 -0800 | [diff] [blame] | 5952 | mLooper->addFd(fd.get(), 0, ALOOPER_EVENT_INPUT, sp<LooperEventCallback>::make(callback), |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5953 | nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5954 | } // release lock |
| 5955 | |
| 5956 | // Wake the looper because some connections have changed. |
| 5957 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5958 | return clientChannel; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5959 | } |
| 5960 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5961 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputMonitor(int32_t displayId, |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5962 | const std::string& name, |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5963 | gui::Pid pid) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5964 | std::shared_ptr<InputChannel> serverChannel; |
| 5965 | std::unique_ptr<InputChannel> clientChannel; |
| 5966 | status_t result = openInputChannelPair(name, serverChannel, clientChannel); |
| 5967 | if (result) { |
| 5968 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5969 | } |
| 5970 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5971 | { // acquire lock |
| 5972 | std::scoped_lock _l(mLock); |
| 5973 | |
| 5974 | if (displayId < 0) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5975 | return base::Error(BAD_VALUE) << "Attempted to create input monitor with name " << name |
| 5976 | << " without a specified display."; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5977 | } |
| 5978 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 5979 | std::shared_ptr<Connection> connection = |
| 5980 | std::make_shared<Connection>(serverChannel, /*monitor=*/true, mIdGenerator); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5981 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Tomasz Wasilczyk | 3202460 | 2023-11-16 10:17:54 -0800 | [diff] [blame] | 5982 | auto&& fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5983 | |
| 5984 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5985 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5986 | } |
| 5987 | mConnectionsByToken.emplace(token, connection); |
| 5988 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5989 | this, std::placeholders::_1, token); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5990 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5991 | mGlobalMonitorsByDisplay[displayId].emplace_back(serverChannel, pid); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5992 | |
Tomasz Wasilczyk | 3202460 | 2023-11-16 10:17:54 -0800 | [diff] [blame] | 5993 | mLooper->addFd(fd.get(), 0, ALOOPER_EVENT_INPUT, sp<LooperEventCallback>::make(callback), |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5994 | nullptr); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5995 | } |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5996 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5997 | // Wake the looper because some connections have changed. |
| 5998 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5999 | return clientChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6000 | } |
| 6001 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 6002 | status_t InputDispatcher::removeInputChannel(const sp<IBinder>& connectionToken) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6003 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6004 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6005 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6006 | status_t status = removeInputChannelLocked(connectionToken, /*notify=*/false); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6007 | if (status) { |
| 6008 | return status; |
| 6009 | } |
| 6010 | } // release lock |
| 6011 | |
| 6012 | // Wake the poll loop because removing the connection may have changed the current |
| 6013 | // synchronization state. |
| 6014 | mLooper->wake(); |
| 6015 | return OK; |
| 6016 | } |
| 6017 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 6018 | status_t InputDispatcher::removeInputChannelLocked(const sp<IBinder>& connectionToken, |
| 6019 | bool notify) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6020 | std::shared_ptr<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 6021 | if (connection == nullptr) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 6022 | // 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] | 6023 | return BAD_VALUE; |
| 6024 | } |
| 6025 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6026 | removeConnectionLocked(connection); |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 6027 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6028 | if (connection->monitor) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 6029 | removeMonitorChannelLocked(connectionToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6030 | } |
| 6031 | |
Tomasz Wasilczyk | 3202460 | 2023-11-16 10:17:54 -0800 | [diff] [blame] | 6032 | mLooper->removeFd(connection->inputChannel->getFd().get()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6033 | |
| 6034 | nsecs_t currentTime = now(); |
| 6035 | abortBrokenDispatchCycleLocked(currentTime, connection, notify); |
| 6036 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 6037 | connection->status = Connection::Status::ZOMBIE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6038 | return OK; |
| 6039 | } |
| 6040 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 6041 | void InputDispatcher::removeMonitorChannelLocked(const sp<IBinder>& connectionToken) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6042 | for (auto it = mGlobalMonitorsByDisplay.begin(); it != mGlobalMonitorsByDisplay.end();) { |
| 6043 | auto& [displayId, monitors] = *it; |
| 6044 | std::erase_if(monitors, [connectionToken](const Monitor& monitor) { |
| 6045 | return monitor.inputChannel->getConnectionToken() == connectionToken; |
| 6046 | }); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6047 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6048 | if (monitors.empty()) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6049 | it = mGlobalMonitorsByDisplay.erase(it); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6050 | } else { |
| 6051 | ++it; |
| 6052 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6053 | } |
| 6054 | } |
| 6055 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6056 | status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6057 | std::scoped_lock _l(mLock); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 6058 | return pilferPointersLocked(token); |
| 6059 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6060 | |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 6061 | status_t InputDispatcher::pilferPointersLocked(const sp<IBinder>& token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6062 | const std::shared_ptr<InputChannel> requestingChannel = getInputChannelLocked(token); |
| 6063 | if (!requestingChannel) { |
Siarhei Vishniakou | 8384e0d | 2023-09-18 18:48:27 -0700 | [diff] [blame] | 6064 | LOG(WARNING) |
| 6065 | << "Attempted to pilfer pointers from an un-registered channel or invalid token"; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6066 | return BAD_VALUE; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6067 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6068 | |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 6069 | auto [statePtr, windowPtr, displayId] = findTouchStateWindowAndDisplayLocked(token); |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 6070 | if (statePtr == nullptr || windowPtr == nullptr) { |
Siarhei Vishniakou | 8384e0d | 2023-09-18 18:48:27 -0700 | [diff] [blame] | 6071 | LOG(WARNING) |
| 6072 | << "Attempted to pilfer points from a channel without any on-going pointer streams." |
| 6073 | " Ignoring."; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6074 | return BAD_VALUE; |
| 6075 | } |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 6076 | std::set<int32_t> deviceIds = windowPtr->getTouchingDeviceIds(); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 6077 | if (deviceIds.empty()) { |
| 6078 | LOG(WARNING) << "Can't pilfer: no touching devices in window: " << windowPtr->dump(); |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 6079 | return BAD_VALUE; |
| 6080 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6081 | |
Siarhei Vishniakou | 8384e0d | 2023-09-18 18:48:27 -0700 | [diff] [blame] | 6082 | for (const DeviceId deviceId : deviceIds) { |
| 6083 | TouchState& state = *statePtr; |
| 6084 | TouchedWindow& window = *windowPtr; |
| 6085 | // Send cancel events to all the input channels we're stealing from. |
| 6086 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
| 6087 | "input channel stole pointer stream"); |
| 6088 | options.deviceId = deviceId; |
| 6089 | options.displayId = displayId; |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 6090 | std::vector<PointerProperties> pointers = window.getTouchingPointers(deviceId); |
| 6091 | std::bitset<MAX_POINTER_ID + 1> pointerIds = getPointerIds(pointers); |
Siarhei Vishniakou | 8384e0d | 2023-09-18 18:48:27 -0700 | [diff] [blame] | 6092 | options.pointerIds = pointerIds; |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 6093 | |
Siarhei Vishniakou | 8384e0d | 2023-09-18 18:48:27 -0700 | [diff] [blame] | 6094 | std::string canceledWindows; |
| 6095 | for (const TouchedWindow& w : state.windows) { |
| 6096 | const std::shared_ptr<InputChannel> channel = |
| 6097 | getInputChannelLocked(w.windowHandle->getToken()); |
| 6098 | if (channel != nullptr && channel->getConnectionToken() != token) { |
| 6099 | synthesizeCancelationEventsForInputChannelLocked(channel, options); |
| 6100 | canceledWindows += canceledWindows.empty() ? "[" : ", "; |
| 6101 | canceledWindows += channel->getName(); |
| 6102 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6103 | } |
Siarhei Vishniakou | 8384e0d | 2023-09-18 18:48:27 -0700 | [diff] [blame] | 6104 | canceledWindows += canceledWindows.empty() ? "[]" : "]"; |
| 6105 | LOG(INFO) << "Channel " << requestingChannel->getName() |
| 6106 | << " is stealing input gesture for device " << deviceId << " from " |
| 6107 | << canceledWindows; |
| 6108 | |
| 6109 | // Prevent the gesture from being sent to any other windows. |
| 6110 | // This only blocks relevant pointers to be sent to other windows |
| 6111 | window.addPilferingPointers(deviceId, pointerIds); |
| 6112 | |
| 6113 | state.cancelPointersForWindowsExcept(deviceId, pointerIds, token); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6114 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6115 | return OK; |
| 6116 | } |
| 6117 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6118 | void InputDispatcher::requestPointerCapture(const sp<IBinder>& windowToken, bool enabled) { |
| 6119 | { // acquire lock |
| 6120 | std::scoped_lock _l(mLock); |
| 6121 | if (DEBUG_FOCUS) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 6122 | const sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(windowToken); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6123 | ALOGI("Request to %s Pointer Capture from: %s.", enabled ? "enable" : "disable", |
| 6124 | windowHandle != nullptr ? windowHandle->getName().c_str() |
| 6125 | : "token without window"); |
| 6126 | } |
| 6127 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6128 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6129 | if (focusedToken != windowToken) { |
| 6130 | ALOGW("Ignoring request to %s Pointer Capture: window does not have focus.", |
| 6131 | enabled ? "enable" : "disable"); |
| 6132 | return; |
| 6133 | } |
| 6134 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6135 | if (enabled == mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6136 | ALOGW("Ignoring request to %s Pointer Capture: " |
| 6137 | "window has %s requested pointer capture.", |
| 6138 | enabled ? "enable" : "disable", enabled ? "already" : "not"); |
| 6139 | return; |
| 6140 | } |
| 6141 | |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 6142 | if (enabled) { |
| 6143 | if (std::find(mIneligibleDisplaysForPointerCapture.begin(), |
| 6144 | mIneligibleDisplaysForPointerCapture.end(), |
| 6145 | mFocusedDisplayId) != mIneligibleDisplaysForPointerCapture.end()) { |
| 6146 | ALOGW("Ignoring request to enable Pointer Capture: display is not eligible"); |
| 6147 | return; |
| 6148 | } |
| 6149 | } |
| 6150 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6151 | setPointerCaptureLocked(enabled); |
| 6152 | } // release lock |
| 6153 | |
| 6154 | // Wake the thread to process command entries. |
| 6155 | mLooper->wake(); |
| 6156 | } |
| 6157 | |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 6158 | void InputDispatcher::setDisplayEligibilityForPointerCapture(int32_t displayId, bool isEligible) { |
| 6159 | { // acquire lock |
| 6160 | std::scoped_lock _l(mLock); |
| 6161 | std::erase(mIneligibleDisplaysForPointerCapture, displayId); |
| 6162 | if (!isEligible) { |
| 6163 | mIneligibleDisplaysForPointerCapture.push_back(displayId); |
| 6164 | } |
| 6165 | } // release lock |
| 6166 | } |
| 6167 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6168 | std::optional<gui::Pid> InputDispatcher::findMonitorPidByTokenLocked(const sp<IBinder>& token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6169 | for (const auto& [_, monitors] : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6170 | for (const Monitor& monitor : monitors) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 6171 | if (monitor.inputChannel->getConnectionToken() == token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6172 | return monitor.pid; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6173 | } |
| 6174 | } |
| 6175 | } |
| 6176 | return std::nullopt; |
| 6177 | } |
| 6178 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6179 | std::shared_ptr<Connection> InputDispatcher::getConnectionLocked( |
| 6180 | const sp<IBinder>& inputConnectionToken) const { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 6181 | if (inputConnectionToken == nullptr) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 6182 | return nullptr; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 6183 | } |
| 6184 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 6185 | for (const auto& [token, connection] : mConnectionsByToken) { |
| 6186 | if (token == inputConnectionToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 6187 | return connection; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6188 | } |
| 6189 | } |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 6190 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 6191 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6192 | } |
| 6193 | |
Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 6194 | std::string InputDispatcher::getConnectionNameLocked(const sp<IBinder>& connectionToken) const { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6195 | std::shared_ptr<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 6196 | if (connection == nullptr) { |
| 6197 | return "<nullptr>"; |
| 6198 | } |
| 6199 | return connection->getInputChannelName(); |
| 6200 | } |
| 6201 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6202 | void InputDispatcher::removeConnectionLocked(const std::shared_ptr<Connection>& connection) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6203 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 6204 | mConnectionsByToken.erase(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6205 | } |
| 6206 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6207 | void InputDispatcher::doDispatchCycleFinishedCommand(nsecs_t finishTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6208 | const std::shared_ptr<Connection>& connection, |
| 6209 | uint32_t seq, bool handled, |
| 6210 | nsecs_t consumeTime) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6211 | // Handle post-event policy actions. |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 6212 | std::unique_ptr<const KeyEntry> fallbackKeyEntry; |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6213 | |
| 6214 | { // Start critical section |
| 6215 | auto dispatchEntryIt = |
| 6216 | std::find_if(connection->waitQueue.begin(), connection->waitQueue.end(), |
| 6217 | [seq](auto& e) { return e->seq == seq; }); |
| 6218 | if (dispatchEntryIt == connection->waitQueue.end()) { |
| 6219 | return; |
| 6220 | } |
| 6221 | |
| 6222 | DispatchEntry& dispatchEntry = **dispatchEntryIt; |
| 6223 | |
| 6224 | const nsecs_t eventDuration = finishTime - dispatchEntry.deliveryTime; |
| 6225 | if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) { |
| 6226 | ALOGI("%s spent %" PRId64 "ms processing %s", connection->getWindowName().c_str(), |
| 6227 | ns2ms(eventDuration), dispatchEntry.eventEntry->getDescription().c_str()); |
| 6228 | } |
| 6229 | if (shouldReportFinishedEvent(dispatchEntry, *connection)) { |
| 6230 | mLatencyTracker.trackFinishedEvent(dispatchEntry.eventEntry->id, |
| 6231 | connection->inputChannel->getConnectionToken(), |
| 6232 | dispatchEntry.deliveryTime, consumeTime, finishTime); |
| 6233 | } |
| 6234 | |
| 6235 | if (dispatchEntry.eventEntry->type == EventEntry::Type::KEY) { |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6236 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(*(dispatchEntry.eventEntry)); |
| 6237 | fallbackKeyEntry = |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6238 | afterKeyEventLockedInterruptable(connection, dispatchEntry, keyEntry, handled); |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6239 | } |
| 6240 | } // End critical section: The -LockedInterruptable methods may have released the lock. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6241 | |
| 6242 | // Dequeue the event and start the next cycle. |
| 6243 | // Because the lock might have been released, it is possible that the |
| 6244 | // contents of the wait queue to have been drained, so we need to double-check |
| 6245 | // a few things. |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6246 | auto entryIt = std::find_if(connection->waitQueue.begin(), connection->waitQueue.end(), |
| 6247 | [seq](auto& e) { return e->seq == seq; }); |
| 6248 | if (entryIt != connection->waitQueue.end()) { |
| 6249 | std::unique_ptr<DispatchEntry> dispatchEntry = std::move(*entryIt); |
| 6250 | connection->waitQueue.erase(entryIt); |
| 6251 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6252 | const sp<IBinder>& connectionToken = connection->inputChannel->getConnectionToken(); |
| 6253 | mAnrTracker.erase(dispatchEntry->timeoutTime, connectionToken); |
| 6254 | if (!connection->responsive) { |
| 6255 | connection->responsive = isConnectionResponsive(*connection); |
| 6256 | if (connection->responsive) { |
| 6257 | // The connection was unresponsive, and now it's responsive. |
| 6258 | processConnectionResponsiveLocked(*connection); |
| 6259 | } |
| 6260 | } |
| 6261 | traceWaitQueueLength(*connection); |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6262 | if (fallbackKeyEntry && connection->status == Connection::Status::NORMAL) { |
| 6263 | const InputTarget target{.inputChannel = connection->inputChannel, |
| 6264 | .flags = dispatchEntry->targetFlags}; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 6265 | enqueueDispatchEntryLocked(connection, std::move(fallbackKeyEntry), target); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6266 | } |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6267 | releaseDispatchEntry(std::move(dispatchEntry)); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6268 | } |
| 6269 | |
| 6270 | // Start the next dispatch cycle for this connection. |
| 6271 | startDispatchCycleLocked(now(), connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6272 | } |
| 6273 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6274 | void InputDispatcher::sendFocusChangedCommandLocked(const sp<IBinder>& oldToken, |
| 6275 | const sp<IBinder>& newToken) { |
| 6276 | auto command = [this, oldToken, newToken]() REQUIRES(mLock) { |
| 6277 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6278 | mPolicy.notifyFocusChanged(oldToken, newToken); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6279 | }; |
| 6280 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6281 | } |
| 6282 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6283 | void InputDispatcher::sendDropWindowCommandLocked(const sp<IBinder>& token, float x, float y) { |
| 6284 | auto command = [this, token, x, y]() REQUIRES(mLock) { |
| 6285 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6286 | mPolicy.notifyDropWindow(token, x, y); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6287 | }; |
| 6288 | postCommandLocked(std::move(command)); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 6289 | } |
| 6290 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6291 | void InputDispatcher::onAnrLocked(const std::shared_ptr<Connection>& connection) { |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6292 | if (connection == nullptr) { |
| 6293 | LOG_ALWAYS_FATAL("Caller must check for nullness"); |
| 6294 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6295 | // Since we are allowing the policy to extend the timeout, maybe the waitQueue |
| 6296 | // is already healthy again. Don't raise ANR in this situation |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6297 | if (connection->waitQueue.empty()) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6298 | ALOGI("Not raising ANR because the connection %s has recovered", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6299 | connection->inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6300 | return; |
| 6301 | } |
| 6302 | /** |
| 6303 | * The "oldestEntry" is the entry that was first sent to the application. That entry, however, |
| 6304 | * may not be the one that caused the timeout to occur. One possibility is that window timeout |
| 6305 | * has changed. This could cause newer entries to time out before the already dispatched |
| 6306 | * entries. In that situation, the newest entries caused ANR. But in all likelihood, the app |
| 6307 | * processes the events linearly. So providing information about the oldest entry seems to be |
| 6308 | * most useful. |
| 6309 | */ |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6310 | DispatchEntry& oldestEntry = *connection->waitQueue.front(); |
| 6311 | const nsecs_t currentWait = now() - oldestEntry.deliveryTime; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6312 | std::string reason = |
| 6313 | android::base::StringPrintf("%s is not responding. Waited %" PRId64 "ms for %s", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6314 | connection->inputChannel->getName().c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6315 | ns2ms(currentWait), |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6316 | oldestEntry.eventEntry->getDescription().c_str()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6317 | sp<IBinder> connectionToken = connection->inputChannel->getConnectionToken(); |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 6318 | updateLastAnrStateLocked(getWindowHandleLocked(connectionToken), reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6319 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6320 | processConnectionUnresponsiveLocked(*connection, std::move(reason)); |
| 6321 | |
| 6322 | // Stop waking up for events on this connection, it is already unresponsive |
| 6323 | cancelEventsForAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6324 | } |
| 6325 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6326 | void InputDispatcher::onAnrLocked(std::shared_ptr<InputApplicationHandle> application) { |
| 6327 | std::string reason = |
| 6328 | StringPrintf("%s does not have a focused window", application->getName().c_str()); |
| 6329 | updateLastAnrStateLocked(*application, reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6330 | |
Yabin Cui | 8eb9c55 | 2023-06-08 18:05:07 +0000 | [diff] [blame] | 6331 | auto command = [this, app = std::move(application)]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6332 | scoped_unlock unlock(mLock); |
Yabin Cui | ced952f | 2023-06-09 21:12:51 +0000 | [diff] [blame] | 6333 | mPolicy.notifyNoFocusedWindowAnr(app); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6334 | }; |
| 6335 | postCommandLocked(std::move(command)); |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 6336 | } |
| 6337 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 6338 | void InputDispatcher::updateLastAnrStateLocked(const sp<WindowInfoHandle>& window, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6339 | const std::string& reason) { |
| 6340 | const std::string windowLabel = getApplicationWindowLabel(nullptr, window); |
| 6341 | updateLastAnrStateLocked(windowLabel, reason); |
| 6342 | } |
| 6343 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6344 | void InputDispatcher::updateLastAnrStateLocked(const InputApplicationHandle& application, |
| 6345 | const std::string& reason) { |
| 6346 | const std::string windowLabel = getApplicationWindowLabel(&application, nullptr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6347 | updateLastAnrStateLocked(windowLabel, reason); |
| 6348 | } |
| 6349 | |
| 6350 | void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel, |
| 6351 | const std::string& reason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6352 | // 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] | 6353 | time_t t = time(nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6354 | struct tm tm; |
| 6355 | localtime_r(&t, &tm); |
| 6356 | char timestr[64]; |
| 6357 | strftime(timestr, sizeof(timestr), "%F %T", &tm); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6358 | mLastAnrState.clear(); |
| 6359 | mLastAnrState += INDENT "ANR:\n"; |
| 6360 | mLastAnrState += StringPrintf(INDENT2 "Time: %s\n", timestr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6361 | mLastAnrState += StringPrintf(INDENT2 "Reason: %s\n", reason.c_str()); |
| 6362 | mLastAnrState += StringPrintf(INDENT2 "Window: %s\n", windowLabel.c_str()); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6363 | dumpDispatchStateLocked(mLastAnrState); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6364 | } |
| 6365 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6366 | void InputDispatcher::doInterceptKeyBeforeDispatchingCommand(const sp<IBinder>& focusedWindowToken, |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 6367 | const KeyEntry& entry) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6368 | const KeyEvent event = createKeyEvent(entry); |
| 6369 | nsecs_t delay = 0; |
| 6370 | { // release lock |
| 6371 | scoped_unlock unlock(mLock); |
| 6372 | android::base::Timer t; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6373 | delay = mPolicy.interceptKeyBeforeDispatching(focusedWindowToken, event, entry.policyFlags); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6374 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 6375 | ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms", |
| 6376 | std::to_string(t.duration().count()).c_str()); |
| 6377 | } |
| 6378 | } // acquire lock |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6379 | |
| 6380 | if (delay < 0) { |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 6381 | entry.interceptKeyResult = KeyEntry::InterceptKeyResult::SKIP; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6382 | } else if (delay == 0) { |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 6383 | entry.interceptKeyResult = KeyEntry::InterceptKeyResult::CONTINUE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6384 | } else { |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 6385 | entry.interceptKeyResult = KeyEntry::InterceptKeyResult::TRY_AGAIN_LATER; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6386 | entry.interceptKeyWakeupTime = now() + delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6387 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6388 | } |
| 6389 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6390 | void InputDispatcher::sendWindowUnresponsiveCommandLocked(const sp<IBinder>& token, |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6391 | std::optional<gui::Pid> pid, |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6392 | std::string reason) { |
Yabin Cui | 8eb9c55 | 2023-06-08 18:05:07 +0000 | [diff] [blame] | 6393 | auto command = [this, token, pid, r = std::move(reason)]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6394 | scoped_unlock unlock(mLock); |
Yabin Cui | ced952f | 2023-06-09 21:12:51 +0000 | [diff] [blame] | 6395 | mPolicy.notifyWindowUnresponsive(token, pid, r); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6396 | }; |
| 6397 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6398 | } |
| 6399 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6400 | void InputDispatcher::sendWindowResponsiveCommandLocked(const sp<IBinder>& token, |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6401 | std::optional<gui::Pid> pid) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6402 | auto command = [this, token, pid]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6403 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6404 | mPolicy.notifyWindowResponsive(token, pid); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6405 | }; |
| 6406 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6407 | } |
| 6408 | |
| 6409 | /** |
| 6410 | * Tell the policy that a connection has become unresponsive so that it can start ANR. |
| 6411 | * Check whether the connection of interest is a monitor or a window, and add the corresponding |
| 6412 | * command entry to the command queue. |
| 6413 | */ |
| 6414 | void InputDispatcher::processConnectionUnresponsiveLocked(const Connection& connection, |
| 6415 | std::string reason) { |
| 6416 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6417 | std::optional<gui::Pid> pid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6418 | if (connection.monitor) { |
| 6419 | ALOGW("Monitor %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 6420 | reason.c_str()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6421 | pid = findMonitorPidByTokenLocked(connectionToken); |
| 6422 | } else { |
| 6423 | // The connection is a window |
| 6424 | ALOGW("Window %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 6425 | reason.c_str()); |
| 6426 | const sp<WindowInfoHandle> handle = getWindowHandleLocked(connectionToken); |
| 6427 | if (handle != nullptr) { |
| 6428 | pid = handle->getInfo()->ownerPid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6429 | } |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6430 | } |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6431 | sendWindowUnresponsiveCommandLocked(connectionToken, pid, std::move(reason)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6432 | } |
| 6433 | |
| 6434 | /** |
| 6435 | * Tell the policy that a connection has become responsive so that it can stop ANR. |
| 6436 | */ |
| 6437 | void InputDispatcher::processConnectionResponsiveLocked(const Connection& connection) { |
| 6438 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6439 | std::optional<gui::Pid> pid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6440 | if (connection.monitor) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6441 | pid = findMonitorPidByTokenLocked(connectionToken); |
| 6442 | } else { |
| 6443 | // The connection is a window |
| 6444 | const sp<WindowInfoHandle> handle = getWindowHandleLocked(connectionToken); |
| 6445 | if (handle != nullptr) { |
| 6446 | pid = handle->getInfo()->ownerPid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6447 | } |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6448 | } |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6449 | sendWindowResponsiveCommandLocked(connectionToken, pid); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6450 | } |
| 6451 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 6452 | std::unique_ptr<const KeyEntry> InputDispatcher::afterKeyEventLockedInterruptable( |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6453 | const std::shared_ptr<Connection>& connection, DispatchEntry& dispatchEntry, |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6454 | const KeyEntry& keyEntry, bool handled) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6455 | if (keyEntry.flags & AKEY_EVENT_FLAG_FALLBACK) { |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6456 | if (!handled) { |
| 6457 | // Report the key as unhandled, since the fallback was not handled. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6458 | mReporter->reportUnhandledKey(keyEntry.id); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6459 | } |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6460 | return {}; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6461 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6462 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6463 | // Get the fallback key state. |
| 6464 | // Clear it out after dispatching the UP. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6465 | int32_t originalKeyCode = keyEntry.keyCode; |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6466 | std::optional<int32_t> fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6467 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6468 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6469 | } |
| 6470 | |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6471 | if (handled || !dispatchEntry.hasForegroundTarget()) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6472 | // If the application handles the original key for which we previously |
| 6473 | // generated a fallback or if the window is not a foreground window, |
| 6474 | // then cancel the associated fallback key, if any. |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6475 | if (fallbackKeyCode) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6476 | // Dispatch the unhandled key to the policy with the cancel flag. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6477 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6478 | ALOGD("Unhandled key event: Asking policy to cancel fallback action. " |
| 6479 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6480 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, |
| 6481 | 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 | event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6485 | |
| 6486 | mLock.unlock(); |
| 6487 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6488 | if (const auto unhandledKeyFallback = |
| 6489 | mPolicy.dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), |
| 6490 | event, keyEntry.policyFlags); |
| 6491 | unhandledKeyFallback) { |
| 6492 | event = *unhandledKeyFallback; |
| 6493 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6494 | |
| 6495 | mLock.lock(); |
| 6496 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6497 | // Cancel the fallback key. |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6498 | if (*fallbackKeyCode != AKEYCODE_UNKNOWN) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 6499 | CancelationOptions options(CancelationOptions::Mode::CANCEL_FALLBACK_EVENTS, |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6500 | "application handled the original non-fallback key " |
| 6501 | "or is no longer a foreground target, " |
| 6502 | "canceling previously dispatched fallback key"); |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6503 | options.keyCode = *fallbackKeyCode; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6504 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6505 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6506 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6507 | } |
| 6508 | } else { |
| 6509 | // If the application did not handle a non-fallback key, first check |
| 6510 | // that we are in a good state to perform unhandled key event processing |
| 6511 | // Then ask the policy what to do with it. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6512 | bool initialDown = keyEntry.action == AKEY_EVENT_ACTION_DOWN && keyEntry.repeatCount == 0; |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6513 | if (!fallbackKeyCode && !initialDown) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6514 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6515 | ALOGD("Unhandled key event: Skipping unhandled key event processing " |
| 6516 | "since this is not an initial down. " |
| 6517 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6518 | originalKeyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 6519 | } |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6520 | return {}; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6521 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6522 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6523 | // Dispatch the unhandled key to the policy. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6524 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6525 | ALOGD("Unhandled key event: Asking policy to perform fallback action. " |
| 6526 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6527 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 6528 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6529 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6530 | |
| 6531 | mLock.unlock(); |
| 6532 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6533 | bool fallback = false; |
| 6534 | if (auto fb = mPolicy.dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), |
| 6535 | event, keyEntry.policyFlags); |
| 6536 | fb) { |
| 6537 | fallback = true; |
| 6538 | event = *fb; |
| 6539 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6540 | |
| 6541 | mLock.lock(); |
| 6542 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 6543 | if (connection->status != Connection::Status::NORMAL) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6544 | connection->inputState.removeFallbackKey(originalKeyCode); |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6545 | return {}; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6546 | } |
| 6547 | |
| 6548 | // Latch the fallback keycode for this key on an initial down. |
| 6549 | // The fallback keycode cannot change at any other point in the lifecycle. |
| 6550 | if (initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6551 | if (fallback) { |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6552 | *fallbackKeyCode = event.getKeyCode(); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6553 | } else { |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6554 | *fallbackKeyCode = AKEYCODE_UNKNOWN; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6555 | } |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6556 | connection->inputState.setFallbackKey(originalKeyCode, *fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6557 | } |
| 6558 | |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6559 | ALOG_ASSERT(fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6560 | |
| 6561 | // Cancel the fallback key if the policy decides not to send it anymore. |
| 6562 | // We will continue to dispatch the key to the policy but we will no |
| 6563 | // longer dispatch a fallback key to the application. |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6564 | if (*fallbackKeyCode != AKEYCODE_UNKNOWN && |
| 6565 | (!fallback || *fallbackKeyCode != event.getKeyCode())) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6566 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6567 | if (fallback) { |
| 6568 | ALOGD("Unhandled key event: Policy requested to send key %d" |
| 6569 | "as a fallback for %d, but on the DOWN it had requested " |
| 6570 | "to send %d instead. Fallback canceled.", |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6571 | event.getKeyCode(), originalKeyCode, *fallbackKeyCode); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6572 | } else { |
| 6573 | ALOGD("Unhandled key event: Policy did not request fallback for %d, " |
| 6574 | "but on the DOWN it had requested to send %d. " |
| 6575 | "Fallback canceled.", |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6576 | originalKeyCode, *fallbackKeyCode); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6577 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6578 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6579 | |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 6580 | CancelationOptions options(CancelationOptions::Mode::CANCEL_FALLBACK_EVENTS, |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6581 | "canceling fallback, policy no longer desires it"); |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6582 | options.keyCode = *fallbackKeyCode; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6583 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
| 6584 | |
| 6585 | fallback = false; |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6586 | *fallbackKeyCode = AKEYCODE_UNKNOWN; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6587 | if (keyEntry.action != AKEY_EVENT_ACTION_UP) { |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6588 | connection->inputState.setFallbackKey(originalKeyCode, *fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6589 | } |
| 6590 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6591 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6592 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6593 | { |
| 6594 | std::string msg; |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6595 | const std::map<int32_t, int32_t>& fallbackKeys = |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6596 | connection->inputState.getFallbackKeys(); |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6597 | for (const auto& [key, value] : fallbackKeys) { |
| 6598 | msg += StringPrintf(", %d->%d", key, value); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6599 | } |
| 6600 | ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.", |
| 6601 | fallbackKeys.size(), msg.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6602 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6603 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6604 | |
| 6605 | if (fallback) { |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6606 | // Return the fallback key that we want dispatched to the channel. |
| 6607 | std::unique_ptr<KeyEntry> newEntry = |
| 6608 | std::make_unique<KeyEntry>(mIdGenerator.nextId(), keyEntry.injectionState, |
| 6609 | event.getEventTime(), event.getDeviceId(), |
| 6610 | event.getSource(), event.getDisplayId(), |
| 6611 | keyEntry.policyFlags, keyEntry.action, |
| 6612 | event.getFlags() | AKEY_EVENT_FLAG_FALLBACK, |
| 6613 | *fallbackKeyCode, event.getScanCode(), |
| 6614 | event.getMetaState(), event.getRepeatCount(), |
| 6615 | event.getDownTime()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6616 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6617 | ALOGD("Unhandled key event: Dispatching fallback key. " |
| 6618 | "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x", |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6619 | originalKeyCode, *fallbackKeyCode, keyEntry.metaState); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6620 | } |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6621 | return newEntry; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6622 | } else { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6623 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6624 | ALOGD("Unhandled key event: No fallback key."); |
| 6625 | } |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6626 | |
| 6627 | // Report the key as unhandled, since there is no fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6628 | mReporter->reportUnhandledKey(keyEntry.id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6629 | } |
| 6630 | } |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6631 | return {}; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6632 | } |
| 6633 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6634 | void InputDispatcher::traceInboundQueueLengthLocked() { |
| 6635 | if (ATRACE_ENABLED()) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 6636 | ATRACE_INT("iq", mInboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6637 | } |
| 6638 | } |
| 6639 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6640 | void InputDispatcher::traceOutboundQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6641 | if (ATRACE_ENABLED()) { |
| 6642 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6643 | snprintf(counterName, sizeof(counterName), "oq:%s", connection.getWindowName().c_str()); |
| 6644 | ATRACE_INT(counterName, connection.outboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6645 | } |
| 6646 | } |
| 6647 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6648 | void InputDispatcher::traceWaitQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6649 | if (ATRACE_ENABLED()) { |
| 6650 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6651 | snprintf(counterName, sizeof(counterName), "wq:%s", connection.getWindowName().c_str()); |
| 6652 | ATRACE_INT(counterName, connection.waitQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6653 | } |
| 6654 | } |
| 6655 | |
Siarhei Vishniakou | 5e20f27 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 6656 | void InputDispatcher::dump(std::string& dump) const { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6657 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6658 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6659 | dump += "Input Dispatcher State:\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6660 | dumpDispatchStateLocked(dump); |
| 6661 | |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6662 | if (!mLastAnrState.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6663 | dump += "\nInput Dispatcher State at time of last ANR:\n"; |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6664 | dump += mLastAnrState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6665 | } |
| 6666 | } |
| 6667 | |
| 6668 | void InputDispatcher::monitor() { |
| 6669 | // 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] | 6670 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6671 | mLooper->wake(); |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6672 | mDispatcherIsAlive.wait(_l); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6673 | } |
| 6674 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6675 | /** |
| 6676 | * Wake up the dispatcher and wait until it processes all events and commands. |
| 6677 | * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so |
| 6678 | * this method can be safely called from any thread, as long as you've ensured that |
| 6679 | * the work you are interested in completing has already been queued. |
| 6680 | */ |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 6681 | bool InputDispatcher::waitForIdle() const { |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6682 | /** |
| 6683 | * Timeout should represent the longest possible time that a device might spend processing |
| 6684 | * events and commands. |
| 6685 | */ |
| 6686 | constexpr std::chrono::duration TIMEOUT = 100ms; |
| 6687 | std::unique_lock lock(mLock); |
| 6688 | mLooper->wake(); |
| 6689 | std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT); |
| 6690 | return result == std::cv_status::no_timeout; |
| 6691 | } |
| 6692 | |
Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 6693 | /** |
| 6694 | * Sets focus to the window identified by the token. This must be called |
| 6695 | * after updating any input window handles. |
| 6696 | * |
| 6697 | * Params: |
| 6698 | * request.token - input channel token used to identify the window that should gain focus. |
| 6699 | * request.focusedToken - the token that the caller expects currently to be focused. If the |
| 6700 | * specified token does not match the currently focused window, this request will be dropped. |
| 6701 | * If the specified focused token matches the currently focused window, the call will succeed. |
| 6702 | * Set this to "null" if this call should succeed no matter what the currently focused token is. |
| 6703 | * request.timestamp - SYSTEM_TIME_MONOTONIC timestamp in nanos set by the client (wm) |
| 6704 | * when requesting the focus change. This determines which request gets |
| 6705 | * precedence if there is a focus change request from another source such as pointer down. |
| 6706 | */ |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6707 | void InputDispatcher::setFocusedWindow(const FocusRequest& request) { |
| 6708 | { // acquire lock |
| 6709 | std::scoped_lock _l(mLock); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6710 | std::optional<FocusResolver::FocusChanges> changes = |
| 6711 | mFocusResolver.setFocusedWindow(request, getWindowHandlesLocked(request.displayId)); |
| 6712 | if (changes) { |
| 6713 | onFocusChangedLocked(*changes); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6714 | } |
| 6715 | } // release lock |
| 6716 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6717 | mLooper->wake(); |
| 6718 | } |
| 6719 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6720 | void InputDispatcher::onFocusChangedLocked(const FocusResolver::FocusChanges& changes) { |
| 6721 | if (changes.oldFocus) { |
| 6722 | std::shared_ptr<InputChannel> focusedInputChannel = getInputChannelLocked(changes.oldFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6723 | if (focusedInputChannel) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 6724 | CancelationOptions options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6725 | "focus left window"); |
| 6726 | synthesizeCancelationEventsForInputChannelLocked(focusedInputChannel, options); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6727 | enqueueFocusEventLocked(changes.oldFocus, /*hasFocus=*/false, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6728 | } |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6729 | } |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6730 | if (changes.newFocus) { |
Siarhei Vishniakou | c033dfb | 2023-10-03 10:45:16 -0700 | [diff] [blame] | 6731 | resetNoFocusedWindowTimeoutLocked(); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6732 | enqueueFocusEventLocked(changes.newFocus, /*hasFocus=*/true, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6733 | } |
| 6734 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6735 | // If a window has pointer capture, then it must have focus. We need to ensure that this |
| 6736 | // contract is upheld when pointer capture is being disabled due to a loss of window focus. |
| 6737 | // If the window loses focus before it loses pointer capture, then the window can be in a state |
| 6738 | // where it has pointer capture but not focus, violating the contract. Therefore we must |
| 6739 | // dispatch the pointer capture event before the focus event. Since focus events are added to |
| 6740 | // the front of the queue (above), we add the pointer capture event to the front of the queue |
| 6741 | // after the focus events are added. This ensures the pointer capture event ends up at the |
| 6742 | // front. |
| 6743 | disablePointerCaptureForcedLocked(); |
| 6744 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6745 | if (mFocusedDisplayId == changes.displayId) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6746 | sendFocusChangedCommandLocked(changes.oldFocus, changes.newFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6747 | } |
| 6748 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6749 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6750 | void InputDispatcher::disablePointerCaptureForcedLocked() { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6751 | if (!mCurrentPointerCaptureRequest.enable && !mWindowTokenWithPointerCapture) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6752 | return; |
| 6753 | } |
| 6754 | |
| 6755 | ALOGD_IF(DEBUG_FOCUS, "Disabling Pointer Capture because the window lost focus."); |
| 6756 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6757 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6758 | setPointerCaptureLocked(false); |
| 6759 | } |
| 6760 | |
| 6761 | if (!mWindowTokenWithPointerCapture) { |
| 6762 | // No need to send capture changes because no window has capture. |
| 6763 | return; |
| 6764 | } |
| 6765 | |
| 6766 | if (mPendingEvent != nullptr) { |
| 6767 | // Move the pending event to the front of the queue. This will give the chance |
| 6768 | // for the pending event to be dropped if it is a captured event. |
| 6769 | mInboundQueue.push_front(mPendingEvent); |
| 6770 | mPendingEvent = nullptr; |
| 6771 | } |
| 6772 | |
| 6773 | auto entry = std::make_unique<PointerCaptureChangedEntry>(mIdGenerator.nextId(), now(), |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6774 | mCurrentPointerCaptureRequest); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6775 | mInboundQueue.push_front(std::move(entry)); |
| 6776 | } |
| 6777 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6778 | void InputDispatcher::setPointerCaptureLocked(bool enable) { |
| 6779 | mCurrentPointerCaptureRequest.enable = enable; |
| 6780 | mCurrentPointerCaptureRequest.seq++; |
| 6781 | auto command = [this, request = mCurrentPointerCaptureRequest]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6782 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6783 | mPolicy.setPointerCapture(request); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6784 | }; |
| 6785 | postCommandLocked(std::move(command)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6786 | } |
| 6787 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6788 | void InputDispatcher::displayRemoved(int32_t displayId) { |
| 6789 | { // acquire lock |
| 6790 | std::scoped_lock _l(mLock); |
| 6791 | // Set an empty list to remove all handles from the specific display. |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 6792 | setInputWindowsLocked(/*windowInfoHandles=*/{}, displayId); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6793 | setFocusedApplicationLocked(displayId, nullptr); |
| 6794 | // Call focus resolver to clean up stale requests. This must be called after input windows |
| 6795 | // have been removed for the removed display. |
| 6796 | mFocusResolver.displayRemoved(displayId); |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 6797 | // Reset pointer capture eligibility, regardless of previous state. |
| 6798 | std::erase(mIneligibleDisplaysForPointerCapture, displayId); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 6799 | // Remove the associated touch mode state. |
| 6800 | mTouchModePerDisplay.erase(displayId); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 6801 | mVerifiersByDisplay.erase(displayId); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6802 | } // release lock |
| 6803 | |
| 6804 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6805 | mLooper->wake(); |
| 6806 | } |
| 6807 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 6808 | void InputDispatcher::onWindowInfosChanged(const gui::WindowInfosUpdate& update) { |
Siarhei Vishniakou | aeed0da | 2024-01-09 08:57:13 -0800 | [diff] [blame] | 6809 | if (auto result = validateWindowInfosUpdate(update); !result.ok()) { |
| 6810 | { |
| 6811 | // acquire lock |
| 6812 | std::scoped_lock _l(mLock); |
| 6813 | logDispatchStateLocked(); |
| 6814 | } |
| 6815 | LOG_ALWAYS_FATAL("Incorrect WindowInfosUpdate provided: %s", |
| 6816 | result.error().message().c_str()); |
| 6817 | }; |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6818 | // The listener sends the windows as a flattened array. Separate the windows by display for |
| 6819 | // more convenient parsing. |
| 6820 | std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>> handlesPerDisplay; |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 6821 | for (const auto& info : update.windowInfos) { |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6822 | handlesPerDisplay.emplace(info.displayId, std::vector<sp<WindowInfoHandle>>()); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6823 | handlesPerDisplay[info.displayId].push_back(sp<WindowInfoHandle>::make(info)); |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6824 | } |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6825 | |
| 6826 | { // acquire lock |
| 6827 | std::scoped_lock _l(mLock); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 6828 | |
| 6829 | // Ensure that we have an entry created for all existing displays so that if a displayId has |
| 6830 | // no windows, we can tell that the windows were removed from the display. |
| 6831 | for (const auto& [displayId, _] : mWindowHandlesByDisplay) { |
| 6832 | handlesPerDisplay[displayId]; |
| 6833 | } |
| 6834 | |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6835 | mDisplayInfos.clear(); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 6836 | for (const auto& displayInfo : update.displayInfos) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6837 | mDisplayInfos.emplace(displayInfo.displayId, displayInfo); |
| 6838 | } |
| 6839 | |
| 6840 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 6841 | setInputWindowsLocked(handles, displayId); |
| 6842 | } |
Patrick Williams | 9464b2c | 2023-05-23 11:22:04 -0500 | [diff] [blame] | 6843 | |
| 6844 | if (update.vsyncId < mWindowInfosVsyncId) { |
| 6845 | ALOGE("Received out of order window infos update. Last update vsync id: %" PRId64 |
| 6846 | ", current update vsync id: %" PRId64, |
| 6847 | mWindowInfosVsyncId, update.vsyncId); |
| 6848 | } |
| 6849 | mWindowInfosVsyncId = update.vsyncId; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6850 | } |
| 6851 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6852 | mLooper->wake(); |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6853 | } |
| 6854 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6855 | bool InputDispatcher::shouldDropInput( |
| 6856 | const EventEntry& entry, const sp<android::gui::WindowInfoHandle>& windowHandle) const { |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6857 | if (windowHandle->getInfo()->inputConfig.test(WindowInfo::InputConfig::DROP_INPUT) || |
| 6858 | (windowHandle->getInfo()->inputConfig.test( |
| 6859 | WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED) && |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6860 | isWindowObscuredLocked(windowHandle))) { |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6861 | ALOGW("Dropping %s event targeting %s as requested by the input configuration {%s} on " |
| 6862 | "display %" PRId32 ".", |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6863 | ftl::enum_string(entry.type).c_str(), windowHandle->getName().c_str(), |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6864 | windowHandle->getInfo()->inputConfig.string().c_str(), |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6865 | windowHandle->getInfo()->displayId); |
| 6866 | return true; |
| 6867 | } |
| 6868 | return false; |
| 6869 | } |
| 6870 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 6871 | void InputDispatcher::DispatcherWindowListener::onWindowInfosChanged( |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 6872 | const gui::WindowInfosUpdate& update) { |
| 6873 | mDispatcher.onWindowInfosChanged(update); |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 6874 | } |
| 6875 | |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6876 | void InputDispatcher::cancelCurrentTouch() { |
| 6877 | { |
| 6878 | std::scoped_lock _l(mLock); |
| 6879 | ALOGD("Canceling all ongoing pointer gestures on all displays."); |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 6880 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6881 | "cancel current touch"); |
| 6882 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 6883 | |
| 6884 | mTouchStatesByDisplay.clear(); |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6885 | } |
| 6886 | // Wake up poll loop since there might be work to do. |
| 6887 | mLooper->wake(); |
| 6888 | } |
| 6889 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6890 | void InputDispatcher::setMonitorDispatchingTimeoutForTest(std::chrono::nanoseconds timeout) { |
| 6891 | std::scoped_lock _l(mLock); |
| 6892 | mMonitorDispatchingTimeout = timeout; |
| 6893 | } |
| 6894 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6895 | void InputDispatcher::slipWallpaperTouch(ftl::Flags<InputTarget::Flags> targetFlags, |
| 6896 | const sp<WindowInfoHandle>& oldWindowHandle, |
| 6897 | const sp<WindowInfoHandle>& newWindowHandle, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 6898 | TouchState& state, int32_t deviceId, |
| 6899 | const PointerProperties& pointerProperties, |
Siarhei Vishniakou | bf88052 | 2023-05-01 11:03:22 -0700 | [diff] [blame] | 6900 | std::vector<InputTarget>& targets) const { |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 6901 | std::vector<PointerProperties> pointers{pointerProperties}; |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6902 | const bool oldHasWallpaper = oldWindowHandle->getInfo()->inputConfig.test( |
| 6903 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER); |
| 6904 | const bool newHasWallpaper = targetFlags.test(InputTarget::Flags::FOREGROUND) && |
| 6905 | newWindowHandle->getInfo()->inputConfig.test( |
| 6906 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER); |
| 6907 | const sp<WindowInfoHandle> oldWallpaper = |
| 6908 | oldHasWallpaper ? state.getWallpaperWindow() : nullptr; |
| 6909 | const sp<WindowInfoHandle> newWallpaper = |
| 6910 | newHasWallpaper ? findWallpaperWindowBelow(newWindowHandle) : nullptr; |
| 6911 | if (oldWallpaper == newWallpaper) { |
| 6912 | return; |
| 6913 | } |
| 6914 | |
| 6915 | if (oldWallpaper != nullptr) { |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 6916 | const TouchedWindow& oldTouchedWindow = state.getTouchedWindow(oldWallpaper); |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 6917 | addPointerWindowTargetLocked(oldWallpaper, InputTarget::DispatchMode::SLIPPERY_EXIT, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 6918 | oldTouchedWindow.targetFlags, getPointerIds(pointers), |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 6919 | oldTouchedWindow.getDownTimeInTarget(deviceId), targets); |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 6920 | state.removeTouchingPointerFromWindow(deviceId, pointerProperties.id, oldWallpaper); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6921 | } |
| 6922 | |
| 6923 | if (newWallpaper != nullptr) { |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 6924 | state.addOrUpdateWindow(newWallpaper, InputTarget::DispatchMode::SLIPPERY_ENTER, |
| 6925 | InputTarget::Flags::WINDOW_IS_OBSCURED | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6926 | InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 6927 | deviceId, pointers); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6928 | } |
| 6929 | } |
| 6930 | |
| 6931 | void InputDispatcher::transferWallpaperTouch(ftl::Flags<InputTarget::Flags> oldTargetFlags, |
| 6932 | ftl::Flags<InputTarget::Flags> newTargetFlags, |
| 6933 | const sp<WindowInfoHandle> fromWindowHandle, |
| 6934 | const sp<WindowInfoHandle> toWindowHandle, |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 6935 | TouchState& state, int32_t deviceId, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 6936 | const std::vector<PointerProperties>& pointers) { |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6937 | const bool oldHasWallpaper = oldTargetFlags.test(InputTarget::Flags::FOREGROUND) && |
| 6938 | fromWindowHandle->getInfo()->inputConfig.test( |
| 6939 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER); |
| 6940 | const bool newHasWallpaper = newTargetFlags.test(InputTarget::Flags::FOREGROUND) && |
| 6941 | toWindowHandle->getInfo()->inputConfig.test( |
| 6942 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER); |
| 6943 | |
| 6944 | const sp<WindowInfoHandle> oldWallpaper = |
| 6945 | oldHasWallpaper ? state.getWallpaperWindow() : nullptr; |
| 6946 | const sp<WindowInfoHandle> newWallpaper = |
| 6947 | newHasWallpaper ? findWallpaperWindowBelow(toWindowHandle) : nullptr; |
| 6948 | if (oldWallpaper == newWallpaper) { |
| 6949 | return; |
| 6950 | } |
| 6951 | |
| 6952 | if (oldWallpaper != nullptr) { |
| 6953 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
| 6954 | "transferring touch focus to another window"); |
| 6955 | state.removeWindowByToken(oldWallpaper->getToken()); |
| 6956 | synthesizeCancelationEventsForWindowLocked(oldWallpaper, options); |
| 6957 | } |
| 6958 | |
| 6959 | if (newWallpaper != nullptr) { |
| 6960 | nsecs_t downTimeInTarget = now(); |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 6961 | ftl::Flags<InputTarget::Flags> wallpaperFlags = oldTargetFlags & InputTarget::Flags::SPLIT; |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6962 | wallpaperFlags |= InputTarget::Flags::WINDOW_IS_OBSCURED | |
| 6963 | InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 6964 | state.addOrUpdateWindow(newWallpaper, InputTarget::DispatchMode::AS_IS, wallpaperFlags, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 6965 | deviceId, pointers, downTimeInTarget); |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6966 | std::shared_ptr<Connection> wallpaperConnection = |
| 6967 | getConnectionLocked(newWallpaper->getToken()); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6968 | if (wallpaperConnection != nullptr) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6969 | std::shared_ptr<Connection> toConnection = |
| 6970 | getConnectionLocked(toWindowHandle->getToken()); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6971 | toConnection->inputState.mergePointerStateTo(wallpaperConnection->inputState); |
| 6972 | synthesizePointerDownEventsForConnectionLocked(downTimeInTarget, wallpaperConnection, |
| 6973 | wallpaperFlags); |
| 6974 | } |
| 6975 | } |
| 6976 | } |
| 6977 | |
| 6978 | sp<WindowInfoHandle> InputDispatcher::findWallpaperWindowBelow( |
| 6979 | const sp<WindowInfoHandle>& windowHandle) const { |
| 6980 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
| 6981 | getWindowHandlesLocked(windowHandle->getInfo()->displayId); |
| 6982 | bool foundWindow = false; |
| 6983 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
| 6984 | if (!foundWindow && otherHandle != windowHandle) { |
| 6985 | continue; |
| 6986 | } |
| 6987 | if (windowHandle == otherHandle) { |
| 6988 | foundWindow = true; |
| 6989 | continue; |
| 6990 | } |
| 6991 | |
| 6992 | if (otherHandle->getInfo()->inputConfig.test(WindowInfo::InputConfig::IS_WALLPAPER)) { |
| 6993 | return otherHandle; |
| 6994 | } |
| 6995 | } |
| 6996 | return nullptr; |
| 6997 | } |
| 6998 | |
Siarhei Vishniakou | fa2a049 | 2023-11-14 13:13:18 -0800 | [diff] [blame] | 6999 | void InputDispatcher::setKeyRepeatConfiguration(std::chrono::nanoseconds timeout, |
| 7000 | std::chrono::nanoseconds delay) { |
Nergi Rahardi | 730cf3c | 2023-04-13 12:41:17 +0900 | [diff] [blame] | 7001 | std::scoped_lock _l(mLock); |
| 7002 | |
Siarhei Vishniakou | fa2a049 | 2023-11-14 13:13:18 -0800 | [diff] [blame] | 7003 | mConfig.keyRepeatTimeout = timeout.count(); |
| 7004 | mConfig.keyRepeatDelay = delay.count(); |
Nergi Rahardi | 730cf3c | 2023-04-13 12:41:17 +0900 | [diff] [blame] | 7005 | } |
| 7006 | |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 7007 | bool InputDispatcher::isPointerInWindow(const sp<android::IBinder>& token, int32_t displayId, |
| 7008 | DeviceId deviceId, int32_t pointerId) { |
| 7009 | std::scoped_lock _l(mLock); |
| 7010 | auto touchStateIt = mTouchStatesByDisplay.find(displayId); |
| 7011 | if (touchStateIt == mTouchStatesByDisplay.end()) { |
| 7012 | return false; |
| 7013 | } |
| 7014 | for (const TouchedWindow& window : touchStateIt->second.windows) { |
| 7015 | if (window.windowHandle->getToken() == token && |
| 7016 | (window.hasTouchingPointer(deviceId, pointerId) || |
| 7017 | window.hasHoveringPointer(deviceId, pointerId))) { |
| 7018 | return true; |
| 7019 | } |
| 7020 | } |
| 7021 | return false; |
| 7022 | } |
| 7023 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 7024 | } // namespace android::inputdispatcher |