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> |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 38 | #include <powermanager/PowerManager.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" |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 55 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 56 | #define INDENT " " |
| 57 | #define INDENT2 " " |
| 58 | #define INDENT3 " " |
| 59 | #define INDENT4 " " |
| 60 | |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 61 | using namespace android::ftl::flag_operators; |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 62 | using android::base::Error; |
Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 63 | using android::base::HwTimeoutMultiplier; |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 64 | using android::base::Result; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 65 | using android::base::StringPrintf; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 66 | using android::gui::DisplayInfo; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 67 | using android::gui::FocusRequest; |
| 68 | using android::gui::TouchOcclusionMode; |
| 69 | using android::gui::WindowInfo; |
| 70 | using android::gui::WindowInfoHandle; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 71 | using android::os::InputEventInjectionResult; |
| 72 | using android::os::InputEventInjectionSync; |
Ameer Armaly | cff4fa5 | 2023-10-04 23:45:11 +0000 | [diff] [blame] | 73 | namespace input_flags = com::android::input::flags; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 74 | |
Siarhei Vishniakou | 17f1228 | 2023-11-02 13:40:29 -0700 | [diff] [blame] | 75 | static const bool REMOVE_APP_SWITCH_DROPS = input_flags::remove_app_switch_drops(); |
| 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 | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 80 | // Temporarily releases a held mutex for the lifetime of the instance. |
| 81 | // Named to match std::scoped_lock |
| 82 | class scoped_unlock { |
| 83 | public: |
| 84 | explicit scoped_unlock(std::mutex& mutex) : mMutex(mutex) { mMutex.unlock(); } |
| 85 | ~scoped_unlock() { mMutex.lock(); } |
| 86 | |
| 87 | private: |
| 88 | std::mutex& mMutex; |
| 89 | }; |
| 90 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 91 | // Default input dispatching timeout if there is no focused application or paused window |
| 92 | // from which to determine an appropriate dispatching timeout. |
Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 93 | const std::chrono::duration DEFAULT_INPUT_DISPATCHING_TIMEOUT = std::chrono::milliseconds( |
| 94 | android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS * |
| 95 | HwTimeoutMultiplier()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 96 | |
| 97 | // Amount of time to allow for all pending events to be processed when an app switch |
| 98 | // key is on the way. This is used to preempt input dispatch and drop input events |
| 99 | // when an application takes too long to respond and the user has pressed an app switch key. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 100 | constexpr nsecs_t APP_SWITCH_TIMEOUT = 500 * 1000000LL; // 0.5sec |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 101 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 102 | const std::chrono::duration STALE_EVENT_TIMEOUT = std::chrono::seconds(10) * HwTimeoutMultiplier(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 103 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 104 | // 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] | 105 | constexpr nsecs_t SLOW_EVENT_PROCESSING_WARNING_TIMEOUT = 2000 * 1000000LL; // 2sec |
| 106 | |
| 107 | // Log a warning when an interception call takes longer than this to process. |
| 108 | constexpr std::chrono::milliseconds SLOW_INTERCEPTION_THRESHOLD = 50ms; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 109 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 110 | // Additional key latency in case a connection is still processing some motion events. |
| 111 | // This will help with the case when a user touched a button that opens a new window, |
| 112 | // and gives us the chance to dispatch the key to this new window. |
| 113 | constexpr std::chrono::nanoseconds KEY_WAITING_FOR_EVENTS_TIMEOUT = 500ms; |
| 114 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 115 | // Number of recent events to keep for debugging purposes. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 116 | constexpr size_t RECENT_QUEUE_MAX_SIZE = 10; |
| 117 | |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 118 | // Event log tags. See EventLogTags.logtags for reference. |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 119 | constexpr int LOGTAG_INPUT_INTERACTION = 62000; |
| 120 | constexpr int LOGTAG_INPUT_FOCUS = 62001; |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 121 | constexpr int LOGTAG_INPUT_CANCEL = 62003; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 122 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 123 | const ui::Transform kIdentityTransform; |
| 124 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 125 | inline nsecs_t now() { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 126 | return systemTime(SYSTEM_TIME_MONOTONIC); |
| 127 | } |
| 128 | |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 129 | bool isEmpty(const std::stringstream& ss) { |
| 130 | return ss.rdbuf()->in_avail() == 0; |
| 131 | } |
| 132 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 133 | inline const std::string binderToString(const sp<IBinder>& binder) { |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 134 | if (binder == nullptr) { |
| 135 | return "<null>"; |
| 136 | } |
| 137 | return StringPrintf("%p", binder.get()); |
| 138 | } |
| 139 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 140 | static std::string uidString(const gui::Uid& uid) { |
| 141 | return uid.toString(); |
| 142 | } |
| 143 | |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 144 | Result<void> checkKeyAction(int32_t action) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 145 | switch (action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 146 | case AKEY_EVENT_ACTION_DOWN: |
| 147 | case AKEY_EVENT_ACTION_UP: |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 148 | return {}; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 149 | default: |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 150 | return Error() << "Key event has invalid action code " << action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 154 | Result<void> validateKeyEvent(int32_t action) { |
| 155 | return checkKeyAction(action); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 156 | } |
| 157 | |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 158 | Result<void> checkMotionAction(int32_t action, int32_t actionButton, int32_t pointerCount) { |
Siarhei Vishniakou | 2f61bdc | 2022-12-02 08:55:51 -0800 | [diff] [blame] | 159 | switch (MotionEvent::getActionMasked(action)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 160 | case AMOTION_EVENT_ACTION_DOWN: |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 161 | case AMOTION_EVENT_ACTION_UP: { |
| 162 | if (pointerCount != 1) { |
| 163 | return Error() << "invalid pointer count " << pointerCount; |
| 164 | } |
| 165 | return {}; |
| 166 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 167 | case AMOTION_EVENT_ACTION_MOVE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 168 | case AMOTION_EVENT_ACTION_HOVER_ENTER: |
| 169 | case AMOTION_EVENT_ACTION_HOVER_MOVE: |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 170 | case AMOTION_EVENT_ACTION_HOVER_EXIT: { |
| 171 | if (pointerCount < 1) { |
| 172 | return Error() << "invalid pointer count " << pointerCount; |
| 173 | } |
| 174 | return {}; |
| 175 | } |
Siarhei Vishniakou | 2f61bdc | 2022-12-02 08:55:51 -0800 | [diff] [blame] | 176 | case AMOTION_EVENT_ACTION_CANCEL: |
| 177 | case AMOTION_EVENT_ACTION_OUTSIDE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 178 | case AMOTION_EVENT_ACTION_SCROLL: |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 179 | return {}; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 180 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 181 | case AMOTION_EVENT_ACTION_POINTER_UP: { |
Siarhei Vishniakou | 2f61bdc | 2022-12-02 08:55:51 -0800 | [diff] [blame] | 182 | const int32_t index = MotionEvent::getActionIndex(action); |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 183 | if (index < 0) { |
| 184 | return Error() << "invalid index " << index << " for " |
| 185 | << MotionEvent::actionToString(action); |
| 186 | } |
| 187 | if (index >= pointerCount) { |
| 188 | return Error() << "invalid index " << index << " for pointerCount " << pointerCount; |
| 189 | } |
| 190 | if (pointerCount <= 1) { |
| 191 | return Error() << "invalid pointer count " << pointerCount << " for " |
| 192 | << MotionEvent::actionToString(action); |
| 193 | } |
| 194 | return {}; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 195 | } |
| 196 | case AMOTION_EVENT_ACTION_BUTTON_PRESS: |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 197 | case AMOTION_EVENT_ACTION_BUTTON_RELEASE: { |
| 198 | if (actionButton == 0) { |
| 199 | return Error() << "action button should be nonzero for " |
| 200 | << MotionEvent::actionToString(action); |
| 201 | } |
| 202 | return {}; |
| 203 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 204 | default: |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 205 | return Error() << "invalid action " << action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 209 | int64_t millis(std::chrono::nanoseconds t) { |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 210 | return std::chrono::duration_cast<std::chrono::milliseconds>(t).count(); |
| 211 | } |
| 212 | |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 213 | Result<void> validateMotionEvent(int32_t action, int32_t actionButton, size_t pointerCount, |
| 214 | const PointerProperties* pointerProperties) { |
| 215 | Result<void> actionCheck = checkMotionAction(action, actionButton, pointerCount); |
| 216 | if (!actionCheck.ok()) { |
| 217 | return actionCheck; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 218 | } |
| 219 | if (pointerCount < 1 || pointerCount > MAX_POINTERS) { |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 220 | return Error() << "Motion event has invalid pointer count " << pointerCount |
| 221 | << "; value must be between 1 and " << MAX_POINTERS << "."; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 222 | } |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 223 | std::bitset<MAX_POINTER_ID + 1> pointerIdBits; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 224 | for (size_t i = 0; i < pointerCount; i++) { |
| 225 | int32_t id = pointerProperties[i].id; |
| 226 | if (id < 0 || id > MAX_POINTER_ID) { |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 227 | return Error() << "Motion event has invalid pointer id " << id |
| 228 | << "; value must be between 0 and " << MAX_POINTER_ID; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 229 | } |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 230 | if (pointerIdBits.test(id)) { |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 231 | return Error() << "Motion event has duplicate pointer id " << id; |
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 | pointerIdBits.set(id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 234 | } |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 235 | return {}; |
| 236 | } |
| 237 | |
| 238 | Result<void> validateInputEvent(const InputEvent& event) { |
| 239 | switch (event.getType()) { |
| 240 | case InputEventType::KEY: { |
| 241 | const KeyEvent& key = static_cast<const KeyEvent&>(event); |
| 242 | const int32_t action = key.getAction(); |
| 243 | return validateKeyEvent(action); |
| 244 | } |
| 245 | case InputEventType::MOTION: { |
| 246 | const MotionEvent& motion = static_cast<const MotionEvent&>(event); |
| 247 | const int32_t action = motion.getAction(); |
| 248 | const size_t pointerCount = motion.getPointerCount(); |
| 249 | const PointerProperties* pointerProperties = motion.getPointerProperties(); |
| 250 | const int32_t actionButton = motion.getActionButton(); |
| 251 | return validateMotionEvent(action, actionButton, pointerCount, pointerProperties); |
| 252 | } |
| 253 | default: { |
| 254 | return {}; |
| 255 | } |
| 256 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 257 | } |
| 258 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 259 | std::string dumpRegion(const Region& region) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 260 | if (region.isEmpty()) { |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 261 | return "<empty>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 262 | } |
| 263 | |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 264 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 265 | bool first = true; |
| 266 | Region::const_iterator cur = region.begin(); |
| 267 | Region::const_iterator const tail = region.end(); |
| 268 | while (cur != tail) { |
| 269 | if (first) { |
| 270 | first = false; |
| 271 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 272 | dump += "|"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 273 | } |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 274 | 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] | 275 | cur++; |
| 276 | } |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 277 | return dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 278 | } |
| 279 | |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 280 | std::string dumpQueue(const std::deque<std::unique_ptr<DispatchEntry>>& queue, |
| 281 | nsecs_t currentTime) { |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 282 | constexpr size_t maxEntries = 50; // max events to print |
| 283 | constexpr size_t skipBegin = maxEntries / 2; |
| 284 | const size_t skipEnd = queue.size() - maxEntries / 2; |
| 285 | // skip from maxEntries / 2 ... size() - maxEntries/2 |
| 286 | // only print from 0 .. skipBegin and then from skipEnd .. size() |
| 287 | |
| 288 | std::string dump; |
| 289 | for (size_t i = 0; i < queue.size(); i++) { |
| 290 | const DispatchEntry& entry = *queue[i]; |
| 291 | if (i >= skipBegin && i < skipEnd) { |
| 292 | dump += StringPrintf(INDENT4 "<skipped %zu entries>\n", skipEnd - skipBegin); |
| 293 | i = skipEnd - 1; // it will be incremented to "skipEnd" by 'continue' |
| 294 | continue; |
| 295 | } |
| 296 | dump.append(INDENT4); |
| 297 | dump += entry.eventEntry->getDescription(); |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame^] | 298 | dump += StringPrintf(", seq=%" PRIu32 ", targetFlags=%s, age=%" PRId64 "ms", entry.seq, |
| 299 | entry.targetFlags.string().c_str(), |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 300 | ns2ms(currentTime - entry.eventEntry->eventTime)); |
| 301 | if (entry.deliveryTime != 0) { |
| 302 | // This entry was delivered, so add information on how long we've been waiting |
| 303 | dump += StringPrintf(", wait=%" PRId64 "ms", ns2ms(currentTime - entry.deliveryTime)); |
| 304 | } |
| 305 | dump.append("\n"); |
| 306 | } |
| 307 | return dump; |
| 308 | } |
| 309 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 310 | /** |
| 311 | * Find the entry in std::unordered_map by key, and return it. |
| 312 | * If the entry is not found, return a default constructed entry. |
| 313 | * |
| 314 | * Useful when the entries are vectors, since an empty vector will be returned |
| 315 | * if the entry is not found. |
| 316 | * Also useful when the entries are sp<>. If an entry is not found, nullptr is returned. |
| 317 | */ |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 318 | template <typename K, typename V> |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 319 | V getValueByKey(const std::unordered_map<K, V>& map, K key) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 320 | auto it = map.find(key); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 321 | return it != map.end() ? it->second : V{}; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 322 | } |
| 323 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 324 | bool haveSameToken(const sp<WindowInfoHandle>& first, const sp<WindowInfoHandle>& second) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 325 | if (first == second) { |
| 326 | return true; |
| 327 | } |
| 328 | |
| 329 | if (first == nullptr || second == nullptr) { |
| 330 | return false; |
| 331 | } |
| 332 | |
| 333 | return first->getToken() == second->getToken(); |
| 334 | } |
| 335 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 336 | bool haveSameApplicationToken(const WindowInfo* first, const WindowInfo* second) { |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 337 | if (first == nullptr || second == nullptr) { |
| 338 | return false; |
| 339 | } |
| 340 | return first->applicationInfo.token != nullptr && |
| 341 | first->applicationInfo.token == second->applicationInfo.token; |
| 342 | } |
| 343 | |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 344 | template <typename T> |
| 345 | size_t firstMarkedBit(T set) { |
| 346 | // TODO: replace with std::countr_zero from <bit> when that's available |
| 347 | LOG_ALWAYS_FATAL_IF(set.none()); |
| 348 | size_t i = 0; |
| 349 | while (!set.test(i)) { |
| 350 | i++; |
| 351 | } |
| 352 | return i; |
| 353 | } |
| 354 | |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 355 | std::unique_ptr<DispatchEntry> createDispatchEntry( |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 356 | const InputTarget& inputTarget, std::shared_ptr<const EventEntry> eventEntry, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 357 | ftl::Flags<InputTarget::Flags> inputTargetFlags) { |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 358 | if (inputTarget.useDefaultPointerTransform()) { |
| 359 | const ui::Transform& transform = inputTarget.getDefaultPointerTransform(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 360 | return std::make_unique<DispatchEntry>(eventEntry, inputTargetFlags, transform, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 361 | inputTarget.displayTransform, |
| 362 | inputTarget.globalScaleFactor); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | ALOG_ASSERT(eventEntry->type == EventEntry::Type::MOTION); |
| 366 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); |
| 367 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 368 | std::vector<PointerCoords> pointerCoords; |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 369 | pointerCoords.resize(motionEntry.getPointerCount()); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 370 | |
| 371 | // Use the first pointer information to normalize all other pointers. This could be any pointer |
| 372 | // 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] | 373 | // uses the transform for the normalized pointer. |
| 374 | const ui::Transform& firstPointerTransform = |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 375 | inputTarget.pointerTransforms[firstMarkedBit(inputTarget.pointerIds)]; |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 376 | ui::Transform inverseFirstTransform = firstPointerTransform.inverse(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 377 | |
| 378 | // Iterate through all pointers in the event to normalize against the first. |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 379 | for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.getPointerCount(); pointerIndex++) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 380 | const PointerProperties& pointerProperties = motionEntry.pointerProperties[pointerIndex]; |
| 381 | uint32_t pointerId = uint32_t(pointerProperties.id); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 382 | const ui::Transform& currTransform = inputTarget.pointerTransforms[pointerId]; |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 383 | |
| 384 | pointerCoords[pointerIndex].copyFrom(motionEntry.pointerCoords[pointerIndex]); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 385 | // First, apply the current pointer's transform to update the coordinates into |
| 386 | // window space. |
| 387 | pointerCoords[pointerIndex].transform(currTransform); |
| 388 | // Next, apply the inverse transform of the normalized coordinates so the |
| 389 | // current coordinates are transformed into the normalized coordinate space. |
| 390 | pointerCoords[pointerIndex].transform(inverseFirstTransform); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 391 | } |
| 392 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 393 | std::unique_ptr<MotionEntry> combinedMotionEntry = |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 394 | std::make_unique<MotionEntry>(motionEntry.id, motionEntry.injectionState, |
| 395 | motionEntry.eventTime, motionEntry.deviceId, |
| 396 | motionEntry.source, motionEntry.displayId, |
| 397 | motionEntry.policyFlags, motionEntry.action, |
| 398 | motionEntry.actionButton, motionEntry.flags, |
| 399 | motionEntry.metaState, motionEntry.buttonState, |
| 400 | motionEntry.classification, motionEntry.edgeFlags, |
| 401 | motionEntry.xPrecision, motionEntry.yPrecision, |
| 402 | motionEntry.xCursorPosition, motionEntry.yCursorPosition, |
| 403 | motionEntry.downTime, motionEntry.pointerProperties, |
| 404 | pointerCoords); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 405 | |
| 406 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 407 | std::make_unique<DispatchEntry>(std::move(combinedMotionEntry), inputTargetFlags, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 408 | firstPointerTransform, inputTarget.displayTransform, |
| 409 | inputTarget.globalScaleFactor); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 410 | return dispatchEntry; |
| 411 | } |
| 412 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 413 | status_t openInputChannelPair(const std::string& name, std::shared_ptr<InputChannel>& serverChannel, |
| 414 | std::unique_ptr<InputChannel>& clientChannel) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 415 | std::unique_ptr<InputChannel> uniqueServerChannel; |
| 416 | status_t result = InputChannel::openInputChannelPair(name, uniqueServerChannel, clientChannel); |
| 417 | |
| 418 | serverChannel = std::move(uniqueServerChannel); |
| 419 | return result; |
| 420 | } |
| 421 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 422 | template <typename T> |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 423 | 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] | 424 | if (lhs == nullptr && rhs == nullptr) { |
| 425 | return true; |
| 426 | } |
| 427 | if (lhs == nullptr || rhs == nullptr) { |
| 428 | return false; |
| 429 | } |
| 430 | return *lhs == *rhs; |
| 431 | } |
| 432 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 433 | KeyEvent createKeyEvent(const KeyEntry& entry) { |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 434 | KeyEvent event; |
| 435 | event.initialize(entry.id, entry.deviceId, entry.source, entry.displayId, INVALID_HMAC, |
| 436 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, |
| 437 | entry.repeatCount, entry.downTime, entry.eventTime); |
| 438 | return event; |
| 439 | } |
| 440 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 441 | bool shouldReportMetricsForConnection(const Connection& connection) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 442 | // Do not keep track of gesture monitors. They receive every event and would disproportionately |
| 443 | // affect the statistics. |
| 444 | if (connection.monitor) { |
| 445 | return false; |
| 446 | } |
| 447 | // If the connection is experiencing ANR, let's skip it. We have separate ANR metrics |
| 448 | if (!connection.responsive) { |
| 449 | return false; |
| 450 | } |
| 451 | return true; |
| 452 | } |
| 453 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 454 | bool shouldReportFinishedEvent(const DispatchEntry& dispatchEntry, const Connection& connection) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 455 | const EventEntry& eventEntry = *dispatchEntry.eventEntry; |
| 456 | const int32_t& inputEventId = eventEntry.id; |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 457 | if (inputEventId == android::os::IInputConstants::INVALID_INPUT_EVENT_ID) { |
| 458 | return false; |
| 459 | } |
| 460 | // Only track latency for events that originated from hardware |
| 461 | if (eventEntry.isSynthesized()) { |
| 462 | return false; |
| 463 | } |
| 464 | const EventEntry::Type& inputEventEntryType = eventEntry.type; |
| 465 | if (inputEventEntryType == EventEntry::Type::KEY) { |
| 466 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 467 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
| 468 | return false; |
| 469 | } |
| 470 | } else if (inputEventEntryType == EventEntry::Type::MOTION) { |
| 471 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 472 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL || |
| 473 | motionEntry.action == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 474 | return false; |
| 475 | } |
| 476 | } else { |
| 477 | // Not a key or a motion |
| 478 | return false; |
| 479 | } |
| 480 | if (!shouldReportMetricsForConnection(connection)) { |
| 481 | return false; |
| 482 | } |
| 483 | return true; |
| 484 | } |
| 485 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 486 | /** |
| 487 | * Connection is responsive if it has no events in the waitQueue that are older than the |
| 488 | * current time. |
| 489 | */ |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 490 | bool isConnectionResponsive(const Connection& connection) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 491 | const nsecs_t currentTime = now(); |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 492 | for (const auto& dispatchEntry : connection.waitQueue) { |
| 493 | if (dispatchEntry->timeoutTime < currentTime) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 494 | return false; |
| 495 | } |
| 496 | } |
| 497 | return true; |
| 498 | } |
| 499 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 500 | // Returns true if the event type passed as argument represents a user activity. |
| 501 | bool isUserActivityEvent(const EventEntry& eventEntry) { |
| 502 | switch (eventEntry.type) { |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 503 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 504 | case EventEntry::Type::DEVICE_RESET: |
| 505 | case EventEntry::Type::DRAG: |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 506 | case EventEntry::Type::FOCUS: |
| 507 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 508 | case EventEntry::Type::SENSOR: |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 509 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 510 | return false; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 511 | case EventEntry::Type::KEY: |
| 512 | case EventEntry::Type::MOTION: |
| 513 | return true; |
| 514 | } |
| 515 | } |
| 516 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 517 | // 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] | 518 | bool windowAcceptsTouchAt(const WindowInfo& windowInfo, int32_t displayId, float x, float y, |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 519 | bool isStylus, const ui::Transform& displayTransform) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 520 | const auto inputConfig = windowInfo.inputConfig; |
| 521 | if (windowInfo.displayId != displayId || |
| 522 | inputConfig.test(WindowInfo::InputConfig::NOT_VISIBLE)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 523 | return false; |
| 524 | } |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 525 | const bool windowCanInterceptTouch = isStylus && windowInfo.interceptsStylus(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 526 | if (inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE) && !windowCanInterceptTouch) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 527 | return false; |
| 528 | } |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 529 | |
| 530 | // Window Manager works in the logical display coordinate space. When it specifies bounds for a |
| 531 | // window as (l, t, r, b), the range of x in [l, r) and y in [t, b) are considered to be inside |
| 532 | // the window. Points on the right and bottom edges should not be inside the window, so we need |
| 533 | // to be careful about performing a hit test when the display is rotated, since the "right" and |
| 534 | // "bottom" of the window will be different in the display (un-rotated) space compared to in the |
| 535 | // logical display in which WM determined the bounds. Perform the hit test in the logical |
| 536 | // display space to ensure these edges are considered correctly in all orientations. |
| 537 | const auto touchableRegion = displayTransform.transform(windowInfo.touchableRegion); |
| 538 | const auto p = displayTransform.transform(x, y); |
| 539 | if (!touchableRegion.contains(std::floor(p.x), std::floor(p.y))) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 540 | return false; |
| 541 | } |
| 542 | return true; |
| 543 | } |
| 544 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 545 | bool isPointerFromStylus(const MotionEntry& entry, int32_t pointerIndex) { |
| 546 | return isFromSource(entry.source, AINPUT_SOURCE_STYLUS) && |
Prabir Pradhan | e562696 | 2022-10-27 20:30:53 +0000 | [diff] [blame] | 547 | isStylusToolType(entry.pointerProperties[pointerIndex].toolType); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 548 | } |
| 549 | |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 550 | // Determines if the given window can be targeted as InputTarget::Flags::FOREGROUND. |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 551 | // Foreground events are only sent to "foreground targetable" windows, but not all gestures sent to |
| 552 | // such window are necessarily targeted with the flag. For example, an event with ACTION_OUTSIDE can |
| 553 | // 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] | 554 | // InputTarget::Flags::FOREGROUND. |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 555 | bool canReceiveForegroundTouches(const WindowInfo& info) { |
| 556 | // A non-touchable window can still receive touch events (e.g. in the case of |
| 557 | // STYLUS_INTERCEPTOR), so prevent such windows from receiving foreground events for touches. |
| 558 | return !info.inputConfig.test(gui::WindowInfo::InputConfig::NOT_TOUCHABLE) && !info.isSpy(); |
| 559 | } |
| 560 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 561 | bool isWindowOwnedBy(const sp<WindowInfoHandle>& windowHandle, gui::Pid pid, gui::Uid uid) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 562 | if (windowHandle == nullptr) { |
| 563 | return false; |
| 564 | } |
| 565 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 566 | if (pid == windowInfo->ownerPid && uid == windowInfo->ownerUid) { |
| 567 | return true; |
| 568 | } |
| 569 | return false; |
| 570 | } |
| 571 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 572 | // Checks targeted injection using the window's owner's uid. |
| 573 | // Returns an empty string if an entry can be sent to the given window, or an error message if the |
| 574 | // entry is a targeted injection whose uid target doesn't match the window owner. |
| 575 | std::optional<std::string> verifyTargetedInjection(const sp<WindowInfoHandle>& window, |
| 576 | const EventEntry& entry) { |
| 577 | if (entry.injectionState == nullptr || !entry.injectionState->targetUid) { |
| 578 | // The event was not injected, or the injected event does not target a window. |
| 579 | return {}; |
| 580 | } |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 581 | const auto uid = *entry.injectionState->targetUid; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 582 | if (window == nullptr) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 583 | return StringPrintf("No valid window target for injection into uid %s.", |
| 584 | uid.toString().c_str()); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 585 | } |
| 586 | if (entry.injectionState->targetUid != window->getInfo()->ownerUid) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 587 | return StringPrintf("Injected event targeted at uid %s would be dispatched to window '%s' " |
| 588 | "owned by uid %s.", |
| 589 | uid.toString().c_str(), window->getName().c_str(), |
| 590 | window->getInfo()->ownerUid.toString().c_str()); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 591 | } |
| 592 | return {}; |
| 593 | } |
| 594 | |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 595 | std::pair<float, float> resolveTouchedPosition(const MotionEntry& entry) { |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 596 | const bool isFromMouse = isFromSource(entry.source, AINPUT_SOURCE_MOUSE); |
| 597 | // Always dispatch mouse events to cursor position. |
| 598 | if (isFromMouse) { |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 599 | return {entry.xCursorPosition, entry.yCursorPosition}; |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 600 | } |
| 601 | |
Siarhei Vishniakou | 5b9766d | 2023-07-18 14:06:29 -0700 | [diff] [blame] | 602 | const int32_t pointerIndex = MotionEvent::getActionIndex(entry.action); |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 603 | return {entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 604 | entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_Y)}; |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 605 | } |
| 606 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 607 | std::optional<nsecs_t> getDownTime(const EventEntry& eventEntry) { |
| 608 | if (eventEntry.type == EventEntry::Type::KEY) { |
| 609 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 610 | return keyEntry.downTime; |
| 611 | } else if (eventEntry.type == EventEntry::Type::MOTION) { |
| 612 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 613 | return motionEntry.downTime; |
| 614 | } |
| 615 | return std::nullopt; |
| 616 | } |
| 617 | |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 618 | /** |
| 619 | * Compare the old touch state to the new touch state, and generate the corresponding touched |
| 620 | * windows (== input targets). |
| 621 | * If a window had the hovering pointer, but now it doesn't, produce HOVER_EXIT for that window. |
| 622 | * If the pointer just entered the new window, produce HOVER_ENTER. |
| 623 | * For pointers remaining in the window, produce HOVER_MOVE. |
| 624 | */ |
| 625 | std::vector<TouchedWindow> getHoveringWindowsLocked(const TouchState* oldState, |
| 626 | const TouchState& newTouchState, |
| 627 | const MotionEntry& entry) { |
| 628 | std::vector<TouchedWindow> out; |
| 629 | const int32_t maskedAction = MotionEvent::getActionMasked(entry.action); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 630 | |
| 631 | if (maskedAction == AMOTION_EVENT_ACTION_SCROLL) { |
| 632 | // ACTION_SCROLL events should not affect the hovering pointer dispatch |
| 633 | return {}; |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | // We should consider all hovering pointers here. But for now, just use the first one |
| 637 | const int32_t pointerId = entry.pointerProperties[0].id; |
| 638 | |
| 639 | std::set<sp<WindowInfoHandle>> oldWindows; |
| 640 | if (oldState != nullptr) { |
| 641 | oldWindows = oldState->getWindowsWithHoveringPointer(entry.deviceId, pointerId); |
| 642 | } |
| 643 | |
| 644 | std::set<sp<WindowInfoHandle>> newWindows = |
| 645 | newTouchState.getWindowsWithHoveringPointer(entry.deviceId, pointerId); |
| 646 | |
| 647 | // If the pointer is no longer in the new window set, send HOVER_EXIT. |
| 648 | for (const sp<WindowInfoHandle>& oldWindow : oldWindows) { |
| 649 | if (newWindows.find(oldWindow) == newWindows.end()) { |
| 650 | TouchedWindow touchedWindow; |
| 651 | touchedWindow.windowHandle = oldWindow; |
| 652 | touchedWindow.targetFlags = InputTarget::Flags::DISPATCH_AS_HOVER_EXIT; |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 653 | out.push_back(touchedWindow); |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | for (const sp<WindowInfoHandle>& newWindow : newWindows) { |
| 658 | TouchedWindow touchedWindow; |
| 659 | touchedWindow.windowHandle = newWindow; |
| 660 | if (oldWindows.find(newWindow) == oldWindows.end()) { |
| 661 | // Any windows that have this pointer now, and didn't have it before, should get |
| 662 | // HOVER_ENTER |
| 663 | touchedWindow.targetFlags = InputTarget::Flags::DISPATCH_AS_HOVER_ENTER; |
| 664 | } else { |
| 665 | // This pointer was already sent to the window. Use ACTION_HOVER_MOVE. |
Siarhei Vishniakou | c2eb850 | 2023-04-11 18:33:36 -0700 | [diff] [blame] | 666 | if (CC_UNLIKELY(maskedAction != AMOTION_EVENT_ACTION_HOVER_MOVE)) { |
Daniel Norman | 7487dfa | 2023-08-02 16:39:45 -0700 | [diff] [blame] | 667 | android::base::LogSeverity severity = android::base::LogSeverity::FATAL; |
Ameer Armaly | cff4fa5 | 2023-10-04 23:45:11 +0000 | [diff] [blame] | 668 | if (!input_flags::a11y_crash_on_inconsistent_event_stream() && |
| 669 | entry.flags & AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT) { |
Daniel Norman | 7487dfa | 2023-08-02 16:39:45 -0700 | [diff] [blame] | 670 | // The Accessibility injected touch exploration event stream |
| 671 | // has known inconsistencies, so log ERROR instead of |
| 672 | // crashing the device with FATAL. |
Daniel Norman | 7487dfa | 2023-08-02 16:39:45 -0700 | [diff] [blame] | 673 | severity = android::base::LogSeverity::ERROR; |
| 674 | } |
| 675 | LOG(severity) << "Expected ACTION_HOVER_MOVE instead of " << entry.getDescription(); |
Siarhei Vishniakou | c2eb850 | 2023-04-11 18:33:36 -0700 | [diff] [blame] | 676 | } |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 677 | touchedWindow.targetFlags = InputTarget::Flags::DISPATCH_AS_IS; |
| 678 | } |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 679 | touchedWindow.addHoveringPointer(entry.deviceId, pointerId); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 680 | if (canReceiveForegroundTouches(*newWindow->getInfo())) { |
| 681 | touchedWindow.targetFlags |= InputTarget::Flags::FOREGROUND; |
| 682 | } |
| 683 | out.push_back(touchedWindow); |
| 684 | } |
| 685 | return out; |
| 686 | } |
| 687 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 688 | template <typename T> |
| 689 | std::vector<T>& operator+=(std::vector<T>& left, const std::vector<T>& right) { |
| 690 | left.insert(left.end(), right.begin(), right.end()); |
| 691 | return left; |
| 692 | } |
| 693 | |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 694 | // Filter windows in a TouchState and targets in a vector to remove untrusted windows/targets from |
| 695 | // both. |
| 696 | void filterUntrustedTargets(TouchState& touchState, std::vector<InputTarget>& targets) { |
| 697 | std::erase_if(touchState.windows, [&](const TouchedWindow& window) { |
| 698 | if (!window.windowHandle->getInfo()->inputConfig.test( |
| 699 | WindowInfo::InputConfig::TRUSTED_OVERLAY)) { |
| 700 | // In addition to TouchState, erase this window from the input targets! We don't have a |
| 701 | // good way to do this today except by adding a nested loop. |
| 702 | // TODO(b/282025641): simplify this code once InputTargets are being identified |
| 703 | // separately from TouchedWindows. |
| 704 | std::erase_if(targets, [&](const InputTarget& target) { |
| 705 | return target.inputChannel->getConnectionToken() == window.windowHandle->getToken(); |
| 706 | }); |
| 707 | return true; |
| 708 | } |
| 709 | return false; |
| 710 | }); |
| 711 | } |
| 712 | |
Siarhei Vishniakou | ce1fd47 | 2023-09-18 18:38:07 -0700 | [diff] [blame] | 713 | /** |
| 714 | * In general, touch should be always split between windows. Some exceptions: |
| 715 | * 1. Don't split touch if all of the below is true: |
| 716 | * (a) we have an active pointer down *and* |
| 717 | * (b) a new pointer is going down that's from the same device *and* |
| 718 | * (c) the window that's receiving the current pointer does not support split touch. |
| 719 | * 2. Don't split mouse events |
| 720 | */ |
| 721 | bool shouldSplitTouch(const TouchState& touchState, const MotionEntry& entry) { |
| 722 | if (isFromSource(entry.source, AINPUT_SOURCE_MOUSE)) { |
| 723 | // We should never split mouse events |
| 724 | return false; |
| 725 | } |
| 726 | for (const TouchedWindow& touchedWindow : touchState.windows) { |
| 727 | if (touchedWindow.windowHandle->getInfo()->isSpy()) { |
| 728 | // Spy windows should not affect whether or not touch is split. |
| 729 | continue; |
| 730 | } |
| 731 | if (touchedWindow.windowHandle->getInfo()->supportsSplitTouch()) { |
| 732 | continue; |
| 733 | } |
| 734 | if (touchedWindow.windowHandle->getInfo()->inputConfig.test( |
| 735 | gui::WindowInfo::InputConfig::IS_WALLPAPER)) { |
| 736 | // Wallpaper window should not affect whether or not touch is split |
| 737 | continue; |
| 738 | } |
| 739 | |
| 740 | if (touchedWindow.hasTouchingPointers(entry.deviceId)) { |
| 741 | return false; |
| 742 | } |
| 743 | } |
| 744 | return true; |
| 745 | } |
| 746 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 747 | } // namespace |
| 748 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 749 | // --- InputDispatcher --- |
| 750 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 751 | InputDispatcher::InputDispatcher(InputDispatcherPolicyInterface& policy) |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 752 | : mPolicy(policy), |
| 753 | mPendingEvent(nullptr), |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 754 | mLastDropReason(DropReason::NOT_DROPPED), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 755 | mIdGenerator(IdGenerator::Source::INPUT_DISPATCHER), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 756 | mAppSwitchSawKeyDown(false), |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 757 | mAppSwitchDueTime(LLONG_MAX), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 758 | mNextUnblockedEvent(nullptr), |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 759 | mMonitorDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 760 | mDispatchEnabled(false), |
| 761 | mDispatchFrozen(false), |
| 762 | mInputFilterEnabled(false), |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 763 | mMaximumObscuringOpacityForTouch(1.0f), |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 764 | mFocusedDisplayId(ADISPLAY_ID_DEFAULT), |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 765 | mWindowTokenWithPointerCapture(nullptr), |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 766 | mLatencyAggregator(), |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 767 | mLatencyTracker(&mLatencyAggregator) { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 768 | mLooper = sp<Looper>::make(false); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 769 | mReporter = createInputReporter(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 770 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 771 | mWindowInfoListener = sp<DispatcherWindowListener>::make(*this); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 772 | #if defined(__ANDROID__) |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 773 | SurfaceComposerClient::getDefault()->addWindowInfosListener(mWindowInfoListener); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 774 | #endif |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 775 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | InputDispatcher::~InputDispatcher() { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 779 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 780 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 781 | resetKeyRepeatLocked(); |
| 782 | releasePendingEventLocked(); |
| 783 | drainInboundQueueLocked(); |
| 784 | mCommandQueue.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 785 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 786 | while (!mConnectionsByToken.empty()) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 787 | std::shared_ptr<Connection> connection = mConnectionsByToken.begin()->second; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 788 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), /*notify=*/false); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 789 | } |
| 790 | } |
| 791 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 792 | status_t InputDispatcher::start() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 793 | if (mThread) { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 794 | return ALREADY_EXISTS; |
| 795 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 796 | mThread = std::make_unique<InputThread>( |
| 797 | "InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); }); |
| 798 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | status_t InputDispatcher::stop() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 802 | if (mThread && mThread->isCallingThread()) { |
| 803 | ALOGE("InputDispatcher cannot be stopped from its own thread!"); |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 804 | return INVALID_OPERATION; |
| 805 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 806 | mThread.reset(); |
| 807 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 808 | } |
| 809 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 810 | void InputDispatcher::dispatchOnce() { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 811 | nsecs_t nextWakeupTime = LLONG_MAX; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 812 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 813 | std::scoped_lock _l(mLock); |
| 814 | mDispatcherIsAlive.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 815 | |
| 816 | // Run a dispatch loop if there are no pending commands. |
| 817 | // The dispatch loop might enqueue commands to run afterwards. |
| 818 | if (!haveCommandsLocked()) { |
| 819 | dispatchOnceInnerLocked(&nextWakeupTime); |
| 820 | } |
| 821 | |
| 822 | // Run all pending commands if there are any. |
| 823 | // 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] | 824 | if (runCommandsLockedInterruptable()) { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 825 | nextWakeupTime = LLONG_MIN; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 826 | } |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 827 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 828 | // If we are still waiting for ack on some events, |
| 829 | // we might have to wake up earlier to check if an app is anr'ing. |
| 830 | const nsecs_t nextAnrCheck = processAnrsLocked(); |
| 831 | nextWakeupTime = std::min(nextWakeupTime, nextAnrCheck); |
| 832 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 833 | // We are about to enter an infinitely long sleep, because we have no commands or |
| 834 | // pending or queued events |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 835 | if (nextWakeupTime == LLONG_MAX) { |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 836 | mDispatcherEnteredIdle.notify_all(); |
| 837 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 838 | } // release lock |
| 839 | |
| 840 | // Wait for callback or timeout or wake. (make sure we round up, not down) |
| 841 | nsecs_t currentTime = now(); |
| 842 | int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime); |
| 843 | mLooper->pollOnce(timeoutMillis); |
| 844 | } |
| 845 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 846 | /** |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 847 | * Raise ANR if there is no focused window. |
| 848 | * Before the ANR is raised, do a final state check: |
| 849 | * 1. The currently focused application must be the same one we are waiting for. |
| 850 | * 2. Ensure we still don't have a focused window. |
| 851 | */ |
| 852 | void InputDispatcher::processNoFocusedWindowAnrLocked() { |
| 853 | // Check if the application that we are waiting for is still focused. |
| 854 | std::shared_ptr<InputApplicationHandle> focusedApplication = |
| 855 | getValueByKey(mFocusedApplicationHandlesByDisplay, mAwaitedApplicationDisplayId); |
| 856 | if (focusedApplication == nullptr || |
| 857 | focusedApplication->getApplicationToken() != |
| 858 | mAwaitedFocusedApplication->getApplicationToken()) { |
| 859 | // Unexpected because we should have reset the ANR timer when focused application changed |
| 860 | ALOGE("Waited for a focused window, but focused application has already changed to %s", |
| 861 | focusedApplication->getName().c_str()); |
| 862 | return; // The focused application has changed. |
| 863 | } |
| 864 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 865 | const sp<WindowInfoHandle>& focusedWindowHandle = |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 866 | getFocusedWindowHandleLocked(mAwaitedApplicationDisplayId); |
| 867 | if (focusedWindowHandle != nullptr) { |
| 868 | return; // We now have a focused window. No need for ANR. |
| 869 | } |
| 870 | onAnrLocked(mAwaitedFocusedApplication); |
| 871 | } |
| 872 | |
| 873 | /** |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 874 | * Check if any of the connections' wait queues have events that are too old. |
| 875 | * If we waited for events to be ack'ed for more than the window timeout, raise an ANR. |
| 876 | * Return the time at which we should wake up next. |
| 877 | */ |
| 878 | nsecs_t InputDispatcher::processAnrsLocked() { |
| 879 | const nsecs_t currentTime = now(); |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 880 | nsecs_t nextAnrCheck = LLONG_MAX; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 881 | // Check if we are waiting for a focused window to appear. Raise ANR if waited too long |
| 882 | if (mNoFocusedWindowTimeoutTime.has_value() && mAwaitedFocusedApplication != nullptr) { |
| 883 | if (currentTime >= *mNoFocusedWindowTimeoutTime) { |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 884 | processNoFocusedWindowAnrLocked(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 885 | mAwaitedFocusedApplication.reset(); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 886 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 887 | return LLONG_MIN; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 888 | } else { |
Siarhei Vishniakou | 38a6d27 | 2020-10-20 20:29:33 -0500 | [diff] [blame] | 889 | // Keep waiting. We will drop the event when mNoFocusedWindowTimeoutTime comes. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 890 | nextAnrCheck = *mNoFocusedWindowTimeoutTime; |
| 891 | } |
| 892 | } |
| 893 | |
| 894 | // Check if any connection ANRs are due |
| 895 | nextAnrCheck = std::min(nextAnrCheck, mAnrTracker.firstTimeout()); |
| 896 | if (currentTime < nextAnrCheck) { // most likely scenario |
| 897 | return nextAnrCheck; // everything is normal. Let's check again at nextAnrCheck |
| 898 | } |
| 899 | |
| 900 | // If we reached here, we have an unresponsive connection. |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 901 | std::shared_ptr<Connection> connection = getConnectionLocked(mAnrTracker.firstToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 902 | if (connection == nullptr) { |
| 903 | ALOGE("Could not find connection for entry %" PRId64, mAnrTracker.firstTimeout()); |
| 904 | return nextAnrCheck; |
| 905 | } |
| 906 | connection->responsive = false; |
| 907 | // Stop waking up for this unresponsive connection |
| 908 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 909 | onAnrLocked(connection); |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 910 | return LLONG_MIN; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 911 | } |
| 912 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 913 | std::chrono::nanoseconds InputDispatcher::getDispatchingTimeoutLocked( |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 914 | const std::shared_ptr<Connection>& connection) { |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 915 | if (connection->monitor) { |
| 916 | return mMonitorDispatchingTimeout; |
| 917 | } |
| 918 | const sp<WindowInfoHandle> window = |
| 919 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 920 | if (window != nullptr) { |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 921 | return window->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 922 | } |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 923 | return DEFAULT_INPUT_DISPATCHING_TIMEOUT; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 924 | } |
| 925 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 926 | void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) { |
| 927 | nsecs_t currentTime = now(); |
| 928 | |
Jeff Brown | dc5992e | 2014-04-11 01:27:26 -0700 | [diff] [blame] | 929 | // Reset the key repeat timer whenever normal dispatch is suspended while the |
| 930 | // device is in a non-interactive state. This is to ensure that we abort a key |
| 931 | // repeat if the device is just coming out of sleep. |
| 932 | if (!mDispatchEnabled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 933 | resetKeyRepeatLocked(); |
| 934 | } |
| 935 | |
| 936 | // If dispatching is frozen, do not process timeouts or try to deliver any new events. |
| 937 | if (mDispatchFrozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 938 | if (DEBUG_FOCUS) { |
| 939 | ALOGD("Dispatch frozen. Waiting some more."); |
| 940 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 941 | return; |
| 942 | } |
| 943 | |
| 944 | // Optimize latency of app switches. |
| 945 | // Essentially we start a short timeout when an app switch key (HOME / ENDCALL) has |
| 946 | // been pressed. When it expires, we preempt dispatch and drop all other pending events. |
Siarhei Vishniakou | 6520a58 | 2023-10-27 21:53:45 -0700 | [diff] [blame] | 947 | bool isAppSwitchDue; |
Siarhei Vishniakou | 17f1228 | 2023-11-02 13:40:29 -0700 | [diff] [blame] | 948 | if (!REMOVE_APP_SWITCH_DROPS) { |
Siarhei Vishniakou | 6520a58 | 2023-10-27 21:53:45 -0700 | [diff] [blame] | 949 | isAppSwitchDue = mAppSwitchDueTime <= currentTime; |
| 950 | if (mAppSwitchDueTime < *nextWakeupTime) { |
| 951 | *nextWakeupTime = mAppSwitchDueTime; |
| 952 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 953 | } |
| 954 | |
| 955 | // Ready to start a new event. |
| 956 | // If we don't already have a pending event, go grab one. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 957 | if (!mPendingEvent) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 958 | if (mInboundQueue.empty()) { |
Siarhei Vishniakou | 17f1228 | 2023-11-02 13:40:29 -0700 | [diff] [blame] | 959 | if (!REMOVE_APP_SWITCH_DROPS) { |
Siarhei Vishniakou | 6520a58 | 2023-10-27 21:53:45 -0700 | [diff] [blame] | 960 | if (isAppSwitchDue) { |
| 961 | // The inbound queue is empty so the app switch key we were waiting |
| 962 | // for will never arrive. Stop waiting for it. |
| 963 | resetPendingAppSwitchLocked(false); |
| 964 | isAppSwitchDue = false; |
| 965 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | // Synthesize a key repeat if appropriate. |
| 969 | if (mKeyRepeatState.lastKeyEntry) { |
| 970 | if (currentTime >= mKeyRepeatState.nextRepeatTime) { |
| 971 | mPendingEvent = synthesizeKeyRepeatLocked(currentTime); |
| 972 | } else { |
| 973 | if (mKeyRepeatState.nextRepeatTime < *nextWakeupTime) { |
| 974 | *nextWakeupTime = mKeyRepeatState.nextRepeatTime; |
| 975 | } |
| 976 | } |
| 977 | } |
| 978 | |
| 979 | // Nothing to do if there is no pending event. |
| 980 | if (!mPendingEvent) { |
| 981 | return; |
| 982 | } |
| 983 | } else { |
| 984 | // Inbound queue has at least one entry. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 985 | mPendingEvent = mInboundQueue.front(); |
| 986 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 987 | traceInboundQueueLengthLocked(); |
| 988 | } |
| 989 | |
| 990 | // Poke user activity for this event. |
| 991 | if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 992 | pokeUserActivityLocked(*mPendingEvent); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 993 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | // Now we have an event to dispatch. |
| 997 | // 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] | 998 | ALOG_ASSERT(mPendingEvent != nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 999 | bool done = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1000 | DropReason dropReason = DropReason::NOT_DROPPED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1001 | if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1002 | dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1003 | } else if (!mDispatchEnabled) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1004 | dropReason = DropReason::DISABLED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | if (mNextUnblockedEvent == mPendingEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1008 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | switch (mPendingEvent->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1012 | case EventEntry::Type::CONFIGURATION_CHANGED: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1013 | const ConfigurationChangedEntry& typedEntry = |
| 1014 | static_cast<const ConfigurationChangedEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1015 | done = dispatchConfigurationChangedLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1016 | dropReason = DropReason::NOT_DROPPED; // configuration changes are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1017 | break; |
| 1018 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1019 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1020 | case EventEntry::Type::DEVICE_RESET: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1021 | const DeviceResetEntry& typedEntry = |
| 1022 | static_cast<const DeviceResetEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1023 | done = dispatchDeviceResetLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1024 | dropReason = DropReason::NOT_DROPPED; // device resets are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1025 | break; |
| 1026 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1027 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1028 | case EventEntry::Type::FOCUS: { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1029 | std::shared_ptr<const FocusEntry> typedEntry = |
| 1030 | std::static_pointer_cast<const FocusEntry>(mPendingEvent); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1031 | dispatchFocusLocked(currentTime, typedEntry); |
| 1032 | done = true; |
| 1033 | dropReason = DropReason::NOT_DROPPED; // focus events are never dropped |
| 1034 | break; |
| 1035 | } |
| 1036 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1037 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1038 | const auto typedEntry = std::static_pointer_cast<const TouchModeEntry>(mPendingEvent); |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1039 | dispatchTouchModeChangeLocked(currentTime, typedEntry); |
| 1040 | done = true; |
| 1041 | dropReason = DropReason::NOT_DROPPED; // touch mode events are never dropped |
| 1042 | break; |
| 1043 | } |
| 1044 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1045 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 1046 | const auto typedEntry = |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1047 | std::static_pointer_cast<const PointerCaptureChangedEntry>(mPendingEvent); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1048 | dispatchPointerCaptureChangedLocked(currentTime, typedEntry, dropReason); |
| 1049 | done = true; |
| 1050 | break; |
| 1051 | } |
| 1052 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1053 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1054 | std::shared_ptr<const DragEntry> typedEntry = |
| 1055 | std::static_pointer_cast<const DragEntry>(mPendingEvent); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1056 | dispatchDragLocked(currentTime, typedEntry); |
| 1057 | done = true; |
| 1058 | break; |
| 1059 | } |
| 1060 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1061 | case EventEntry::Type::KEY: { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1062 | std::shared_ptr<const KeyEntry> keyEntry = |
| 1063 | std::static_pointer_cast<const KeyEntry>(mPendingEvent); |
Siarhei Vishniakou | 17f1228 | 2023-11-02 13:40:29 -0700 | [diff] [blame] | 1064 | if (!REMOVE_APP_SWITCH_DROPS) { |
Siarhei Vishniakou | 6520a58 | 2023-10-27 21:53:45 -0700 | [diff] [blame] | 1065 | if (isAppSwitchDue) { |
| 1066 | if (isAppSwitchKeyEvent(*keyEntry)) { |
| 1067 | resetPendingAppSwitchLocked(true); |
| 1068 | isAppSwitchDue = false; |
| 1069 | } else if (dropReason == DropReason::NOT_DROPPED) { |
| 1070 | dropReason = DropReason::APP_SWITCH; |
| 1071 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1072 | } |
| 1073 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1074 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *keyEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1075 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1076 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1077 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 1078 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1079 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1080 | done = dispatchKeyLocked(currentTime, keyEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1081 | break; |
| 1082 | } |
| 1083 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1084 | case EventEntry::Type::MOTION: { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1085 | std::shared_ptr<const MotionEntry> motionEntry = |
| 1086 | std::static_pointer_cast<const MotionEntry>(mPendingEvent); |
Siarhei Vishniakou | 17f1228 | 2023-11-02 13:40:29 -0700 | [diff] [blame] | 1087 | if (!REMOVE_APP_SWITCH_DROPS) { |
Siarhei Vishniakou | 6520a58 | 2023-10-27 21:53:45 -0700 | [diff] [blame] | 1088 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 1089 | dropReason = DropReason::APP_SWITCH; |
| 1090 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1091 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1092 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *motionEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1093 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1094 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1095 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 1096 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1097 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1098 | done = dispatchMotionLocked(currentTime, motionEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1099 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1100 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1101 | |
| 1102 | case EventEntry::Type::SENSOR: { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1103 | std::shared_ptr<const SensorEntry> sensorEntry = |
| 1104 | std::static_pointer_cast<const SensorEntry>(mPendingEvent); |
Siarhei Vishniakou | 17f1228 | 2023-11-02 13:40:29 -0700 | [diff] [blame] | 1105 | if (!REMOVE_APP_SWITCH_DROPS) { |
Siarhei Vishniakou | 6520a58 | 2023-10-27 21:53:45 -0700 | [diff] [blame] | 1106 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 1107 | dropReason = DropReason::APP_SWITCH; |
| 1108 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1109 | } |
| 1110 | // Sensor timestamps use SYSTEM_TIME_BOOTTIME time base, so we can't use |
| 1111 | // 'currentTime' here, get SYSTEM_TIME_BOOTTIME instead. |
| 1112 | nsecs_t bootTime = systemTime(SYSTEM_TIME_BOOTTIME); |
| 1113 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(bootTime, *sensorEntry)) { |
| 1114 | dropReason = DropReason::STALE; |
| 1115 | } |
| 1116 | dispatchSensorLocked(currentTime, sensorEntry, &dropReason, nextWakeupTime); |
| 1117 | done = true; |
| 1118 | break; |
| 1119 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1120 | } |
| 1121 | |
| 1122 | if (done) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1123 | if (dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1124 | dropInboundEventLocked(*mPendingEvent, dropReason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1125 | } |
Michael Wright | 3a98172 | 2015-06-10 15:26:13 +0100 | [diff] [blame] | 1126 | mLastDropReason = dropReason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1127 | |
| 1128 | releasePendingEventLocked(); |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 1129 | *nextWakeupTime = LLONG_MIN; // force next poll to wake up immediately |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1130 | } |
| 1131 | } |
| 1132 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 1133 | bool InputDispatcher::isStaleEvent(nsecs_t currentTime, const EventEntry& entry) { |
Siarhei Vishniakou | a733311 | 2023-10-27 13:33:29 -0700 | [diff] [blame] | 1134 | return mPolicy.isStaleEvent(currentTime, entry.eventTime); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 1135 | } |
| 1136 | |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1137 | /** |
| 1138 | * Return true if the events preceding this incoming motion event should be dropped |
| 1139 | * Return false otherwise (the default behaviour) |
| 1140 | */ |
| 1141 | bool InputDispatcher::shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1142 | const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN && |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 1143 | isFromSource(motionEntry.source, AINPUT_SOURCE_CLASS_POINTER); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1144 | |
| 1145 | // Optimize case where the current application is unresponsive and the user |
| 1146 | // decides to touch a window in a different application. |
| 1147 | // If the application takes too long to catch up then we drop all events preceding |
| 1148 | // the touch into the other window. |
| 1149 | if (isPointerDownEvent && mAwaitedFocusedApplication != nullptr) { |
Siarhei Vishniakou | 9306c38 | 2022-09-30 15:30:31 -0700 | [diff] [blame] | 1150 | const int32_t displayId = motionEntry.displayId; |
| 1151 | const auto [x, y] = resolveTouchedPosition(motionEntry); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1152 | const bool isStylus = isPointerFromStylus(motionEntry, /*pointerIndex=*/0); |
Siarhei Vishniakou | 9306c38 | 2022-09-30 15:30:31 -0700 | [diff] [blame] | 1153 | |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 1154 | sp<WindowInfoHandle> touchedWindowHandle = |
| 1155 | findTouchedWindowAtLocked(displayId, x, y, isStylus); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1156 | if (touchedWindowHandle != nullptr && |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1157 | touchedWindowHandle->getApplicationToken() != |
| 1158 | mAwaitedFocusedApplication->getApplicationToken()) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1159 | // User touched a different application than the one we are waiting on. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1160 | ALOGI("Pruning input queue because user touched a different application while waiting " |
| 1161 | "for %s", |
| 1162 | mAwaitedFocusedApplication->getName().c_str()); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1163 | return true; |
| 1164 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1165 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1166 | // Alternatively, maybe there's a spy window that could handle this event. |
| 1167 | const std::vector<sp<WindowInfoHandle>> touchedSpies = |
| 1168 | findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus); |
| 1169 | for (const auto& windowHandle : touchedSpies) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 1170 | const std::shared_ptr<Connection> connection = |
| 1171 | getConnectionLocked(windowHandle->getToken()); |
Siarhei Vishniakou | 34ed4d4 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1172 | if (connection != nullptr && connection->responsive) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1173 | // This spy window could take more input. Drop all events preceding this |
| 1174 | // 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] | 1175 | 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] | 1176 | "responsive spy window that may handle the event.", |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1177 | mAwaitedFocusedApplication->getName().c_str()); |
| 1178 | return true; |
| 1179 | } |
| 1180 | } |
| 1181 | } |
| 1182 | |
| 1183 | // Prevent getting stuck: if we have a pending key event, and some motion events that have not |
| 1184 | // yet been processed by some connections, the dispatcher will wait for these motion |
| 1185 | // events to be processed before dispatching the key event. This is because these motion events |
| 1186 | // may cause a new window to be launched, which the user might expect to receive focus. |
| 1187 | // To prevent waiting forever for such events, just send the key to the currently focused window |
| 1188 | if (isPointerDownEvent && mKeyIsWaitingForEventsTimeout) { |
| 1189 | ALOGD("Received a new pointer down event, stop waiting for events to process and " |
| 1190 | "just send the pending key event to the focused window."); |
| 1191 | mKeyIsWaitingForEventsTimeout = now(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1192 | } |
| 1193 | return false; |
| 1194 | } |
| 1195 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1196 | bool InputDispatcher::enqueueInboundEventLocked(std::unique_ptr<EventEntry> newEntry) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1197 | bool needWake = mInboundQueue.empty(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1198 | mInboundQueue.push_back(std::move(newEntry)); |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1199 | const EventEntry& entry = *(mInboundQueue.back()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1200 | traceInboundQueueLengthLocked(); |
| 1201 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1202 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1203 | case EventEntry::Type::KEY: { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1204 | LOG_ALWAYS_FATAL_IF((entry.policyFlags & POLICY_FLAG_TRUSTED) == 0, |
| 1205 | "Unexpected untrusted event."); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1206 | // Optimize app switch latency. |
| 1207 | // If the application takes too long to catch up then we drop all events preceding |
| 1208 | // the app switch key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1209 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
Siarhei Vishniakou | 6520a58 | 2023-10-27 21:53:45 -0700 | [diff] [blame] | 1210 | |
Siarhei Vishniakou | 17f1228 | 2023-11-02 13:40:29 -0700 | [diff] [blame] | 1211 | if (!REMOVE_APP_SWITCH_DROPS) { |
Siarhei Vishniakou | 6520a58 | 2023-10-27 21:53:45 -0700 | [diff] [blame] | 1212 | if (isAppSwitchKeyEvent(keyEntry)) { |
| 1213 | if (keyEntry.action == AKEY_EVENT_ACTION_DOWN) { |
| 1214 | mAppSwitchSawKeyDown = true; |
| 1215 | } else if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
| 1216 | if (mAppSwitchSawKeyDown) { |
| 1217 | if (DEBUG_APP_SWITCH) { |
| 1218 | ALOGD("App switch is pending!"); |
| 1219 | } |
| 1220 | mAppSwitchDueTime = keyEntry.eventTime + APP_SWITCH_TIMEOUT; |
| 1221 | mAppSwitchSawKeyDown = false; |
| 1222 | needWake = true; |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1223 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1224 | } |
| 1225 | } |
| 1226 | } |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 1227 | // If a new up event comes in, and the pending event with same key code has been asked |
| 1228 | // to try again later because of the policy. We have to reset the intercept key wake up |
| 1229 | // time for it may have been handled in the policy and could be dropped. |
| 1230 | if (keyEntry.action == AKEY_EVENT_ACTION_UP && mPendingEvent && |
| 1231 | mPendingEvent->type == EventEntry::Type::KEY) { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1232 | const KeyEntry& pendingKey = static_cast<const KeyEntry&>(*mPendingEvent); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 1233 | if (pendingKey.keyCode == keyEntry.keyCode && |
| 1234 | pendingKey.interceptKeyResult == |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1235 | KeyEntry::InterceptKeyResult::TRY_AGAIN_LATER) { |
| 1236 | pendingKey.interceptKeyResult = KeyEntry::InterceptKeyResult::UNKNOWN; |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 1237 | pendingKey.interceptKeyWakeupTime = 0; |
| 1238 | needWake = true; |
| 1239 | } |
| 1240 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1241 | break; |
| 1242 | } |
| 1243 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1244 | case EventEntry::Type::MOTION: { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1245 | LOG_ALWAYS_FATAL_IF((entry.policyFlags & POLICY_FLAG_TRUSTED) == 0, |
| 1246 | "Unexpected untrusted event."); |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1247 | if (shouldPruneInboundQueueLocked(static_cast<const MotionEntry&>(entry))) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1248 | mNextUnblockedEvent = mInboundQueue.back(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1249 | needWake = true; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1250 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1251 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1252 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1253 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1254 | LOG_ALWAYS_FATAL("Focus events should be inserted using enqueueFocusEventLocked"); |
| 1255 | break; |
| 1256 | } |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1257 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1258 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1259 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1260 | case EventEntry::Type::SENSOR: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1261 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1262 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1263 | // nothing to do |
| 1264 | break; |
| 1265 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1266 | } |
| 1267 | |
| 1268 | return needWake; |
| 1269 | } |
| 1270 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1271 | void InputDispatcher::addRecentEventLocked(std::shared_ptr<const EventEntry> entry) { |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1272 | // Do not store sensor event in recent queue to avoid flooding the queue. |
| 1273 | if (entry->type != EventEntry::Type::SENSOR) { |
| 1274 | mRecentQueue.push_back(entry); |
| 1275 | } |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1276 | if (mRecentQueue.size() > RECENT_QUEUE_MAX_SIZE) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1277 | mRecentQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1278 | } |
| 1279 | } |
| 1280 | |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 1281 | sp<WindowInfoHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, float x, float y, |
| 1282 | bool isStylus, |
| 1283 | bool ignoreDragWindow) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1284 | // Traverse windows from front to back to find touched window. |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1285 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1286 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 1287 | if (ignoreDragWindow && haveSameToken(windowHandle, mDragState->dragWindow)) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1288 | continue; |
| 1289 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1290 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1291 | const WindowInfo& info = *windowHandle->getInfo(); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 1292 | if (!info.isSpy() && |
| 1293 | windowAcceptsTouchAt(info, displayId, x, y, isStylus, getTransformLocked(displayId))) { |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 1294 | return windowHandle; |
| 1295 | } |
| 1296 | } |
| 1297 | return nullptr; |
| 1298 | } |
| 1299 | |
| 1300 | std::vector<InputTarget> InputDispatcher::findOutsideTargetsLocked( |
Siarhei Vishniakou | 70f3d8c | 2023-09-19 15:36:52 -0700 | [diff] [blame] | 1301 | int32_t displayId, const sp<WindowInfoHandle>& touchedWindow, int32_t pointerId) const { |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 1302 | if (touchedWindow == nullptr) { |
| 1303 | return {}; |
| 1304 | } |
| 1305 | // Traverse windows from front to back until we encounter the touched window. |
| 1306 | std::vector<InputTarget> outsideTargets; |
| 1307 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
| 1308 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 1309 | if (windowHandle == touchedWindow) { |
| 1310 | // Stop iterating once we found a touched window. Any WATCH_OUTSIDE_TOUCH window |
| 1311 | // below the touched window will not get ACTION_OUTSIDE event. |
| 1312 | return outsideTargets; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1313 | } |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1314 | |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 1315 | const WindowInfo& info = *windowHandle->getInfo(); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1316 | if (info.inputConfig.test(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH)) { |
Siarhei Vishniakou | 70f3d8c | 2023-09-19 15:36:52 -0700 | [diff] [blame] | 1317 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
| 1318 | pointerIds.set(pointerId); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 1319 | addPointerWindowTargetLocked(windowHandle, InputTarget::Flags::DISPATCH_AS_OUTSIDE, |
| 1320 | pointerIds, |
| 1321 | /*firstDownTimeInTarget=*/std::nullopt, outsideTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1322 | } |
| 1323 | } |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 1324 | return outsideTargets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1325 | } |
| 1326 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1327 | std::vector<sp<WindowInfoHandle>> InputDispatcher::findTouchedSpyWindowsAtLocked( |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 1328 | int32_t displayId, float x, float y, bool isStylus) const { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1329 | // Traverse windows from front to back and gather the touched spy windows. |
| 1330 | std::vector<sp<WindowInfoHandle>> spyWindows; |
| 1331 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
| 1332 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 1333 | const WindowInfo& info = *windowHandle->getInfo(); |
| 1334 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 1335 | if (!windowAcceptsTouchAt(info, displayId, x, y, isStylus, getTransformLocked(displayId))) { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1336 | continue; |
| 1337 | } |
| 1338 | if (!info.isSpy()) { |
| 1339 | // The first touched non-spy window was found, so return the spy windows touched so far. |
| 1340 | return spyWindows; |
| 1341 | } |
| 1342 | spyWindows.push_back(windowHandle); |
| 1343 | } |
| 1344 | return spyWindows; |
| 1345 | } |
| 1346 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1347 | void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1348 | const char* reason; |
| 1349 | switch (dropReason) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1350 | case DropReason::POLICY: |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 1351 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1352 | ALOGD("Dropped event because policy consumed it."); |
| 1353 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1354 | reason = "inbound event was dropped because the policy consumed it"; |
| 1355 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1356 | case DropReason::DISABLED: |
| 1357 | if (mLastDropReason != DropReason::DISABLED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1358 | ALOGI("Dropped event because input dispatch is disabled."); |
| 1359 | } |
| 1360 | reason = "inbound event was dropped because input dispatch is disabled"; |
| 1361 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1362 | case DropReason::APP_SWITCH: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1363 | ALOGI("Dropped event because of pending overdue app switch."); |
| 1364 | reason = "inbound event was dropped because of pending overdue app switch"; |
| 1365 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1366 | case DropReason::BLOCKED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1367 | ALOGI("Dropped event because the current application is not responding and the user " |
| 1368 | "has started interacting with a different application."); |
| 1369 | reason = "inbound event was dropped because the current application is not responding " |
| 1370 | "and the user has started interacting with a different application"; |
| 1371 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1372 | case DropReason::STALE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1373 | ALOGI("Dropped event because it is stale."); |
| 1374 | reason = "inbound event was dropped because it is stale"; |
| 1375 | break; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1376 | case DropReason::NO_POINTER_CAPTURE: |
| 1377 | ALOGI("Dropped event because there is no window with Pointer Capture."); |
| 1378 | reason = "inbound event was dropped because there is no window with Pointer Capture"; |
| 1379 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1380 | case DropReason::NOT_DROPPED: { |
| 1381 | LOG_ALWAYS_FATAL("Should not be dropping a NOT_DROPPED event"); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1382 | return; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1383 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1384 | } |
| 1385 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1386 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1387 | case EventEntry::Type::KEY: { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1388 | CancelationOptions options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, reason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1389 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1390 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1391 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1392 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1393 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1394 | if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1395 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, reason); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1396 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1397 | } else { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1398 | CancelationOptions options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, |
| 1399 | reason); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1400 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1401 | } |
| 1402 | break; |
| 1403 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1404 | case EventEntry::Type::SENSOR: { |
| 1405 | break; |
| 1406 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1407 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1408 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1409 | break; |
| 1410 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1411 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1412 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1413 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 1414 | case EventEntry::Type::DEVICE_RESET: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1415 | 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] | 1416 | break; |
| 1417 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1418 | } |
| 1419 | } |
| 1420 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1421 | static bool isAppSwitchKeyCode(int32_t keyCode) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1422 | return keyCode == AKEYCODE_HOME || keyCode == AKEYCODE_ENDCALL || |
| 1423 | keyCode == AKEYCODE_APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1424 | } |
| 1425 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1426 | bool InputDispatcher::isAppSwitchKeyEvent(const KeyEntry& keyEntry) { |
| 1427 | return !(keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) && isAppSwitchKeyCode(keyEntry.keyCode) && |
| 1428 | (keyEntry.policyFlags & POLICY_FLAG_TRUSTED) && |
| 1429 | (keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1430 | } |
| 1431 | |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 1432 | bool InputDispatcher::isAppSwitchPendingLocked() const { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 1433 | return mAppSwitchDueTime != LLONG_MAX; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1434 | } |
| 1435 | |
| 1436 | void InputDispatcher::resetPendingAppSwitchLocked(bool handled) { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 1437 | mAppSwitchDueTime = LLONG_MAX; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1438 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1439 | if (DEBUG_APP_SWITCH) { |
| 1440 | if (handled) { |
| 1441 | ALOGD("App switch has arrived."); |
| 1442 | } else { |
| 1443 | ALOGD("App switch was abandoned."); |
| 1444 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1445 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1446 | } |
| 1447 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1448 | bool InputDispatcher::haveCommandsLocked() const { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1449 | return !mCommandQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1450 | } |
| 1451 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1452 | bool InputDispatcher::runCommandsLockedInterruptable() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1453 | if (mCommandQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1454 | return false; |
| 1455 | } |
| 1456 | |
| 1457 | do { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1458 | auto command = std::move(mCommandQueue.front()); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1459 | mCommandQueue.pop_front(); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1460 | // Commands are run with the lock held, but may release and re-acquire the lock from within. |
| 1461 | command(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1462 | } while (!mCommandQueue.empty()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1463 | return true; |
| 1464 | } |
| 1465 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1466 | void InputDispatcher::postCommandLocked(Command&& command) { |
| 1467 | mCommandQueue.push_back(command); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1468 | } |
| 1469 | |
| 1470 | void InputDispatcher::drainInboundQueueLocked() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1471 | while (!mInboundQueue.empty()) { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1472 | std::shared_ptr<const EventEntry> entry = mInboundQueue.front(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1473 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1474 | releaseInboundEventLocked(entry); |
| 1475 | } |
| 1476 | traceInboundQueueLengthLocked(); |
| 1477 | } |
| 1478 | |
| 1479 | void InputDispatcher::releasePendingEventLocked() { |
| 1480 | if (mPendingEvent) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1481 | releaseInboundEventLocked(mPendingEvent); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1482 | mPendingEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1483 | } |
| 1484 | } |
| 1485 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1486 | void InputDispatcher::releaseInboundEventLocked(std::shared_ptr<const EventEntry> entry) { |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 1487 | const std::shared_ptr<InjectionState>& injectionState = entry->injectionState; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1488 | if (injectionState && injectionState->injectionResult == InputEventInjectionResult::PENDING) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1489 | if (DEBUG_DISPATCH_CYCLE) { |
| 1490 | ALOGD("Injected inbound event was dropped."); |
| 1491 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1492 | setInjectionResult(*entry, InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1493 | } |
| 1494 | if (entry == mNextUnblockedEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1495 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1496 | } |
| 1497 | addRecentEventLocked(entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1498 | } |
| 1499 | |
| 1500 | void InputDispatcher::resetKeyRepeatLocked() { |
| 1501 | if (mKeyRepeatState.lastKeyEntry) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1502 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1503 | } |
| 1504 | } |
| 1505 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1506 | std::shared_ptr<KeyEntry> InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1507 | std::shared_ptr<const KeyEntry> entry = mKeyRepeatState.lastKeyEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1508 | |
Michael Wright | 2e73295 | 2014-09-24 13:26:59 -0700 | [diff] [blame] | 1509 | uint32_t policyFlags = entry->policyFlags & |
| 1510 | (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1511 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1512 | std::shared_ptr<KeyEntry> newEntry = |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 1513 | std::make_unique<KeyEntry>(mIdGenerator.nextId(), /*injectionState=*/nullptr, |
| 1514 | currentTime, entry->deviceId, entry->source, |
| 1515 | entry->displayId, policyFlags, entry->action, entry->flags, |
| 1516 | entry->keyCode, entry->scanCode, entry->metaState, |
| 1517 | entry->repeatCount + 1, entry->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1518 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1519 | newEntry->syntheticRepeat = true; |
| 1520 | mKeyRepeatState.lastKeyEntry = newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1521 | mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1522 | return newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1523 | } |
| 1524 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1525 | bool InputDispatcher::dispatchConfigurationChangedLocked(nsecs_t currentTime, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1526 | const ConfigurationChangedEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1527 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1528 | ALOGD("dispatchConfigurationChanged - eventTime=%" PRId64, entry.eventTime); |
| 1529 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1530 | |
| 1531 | // Reset key repeating in case a keyboard device was added or removed or something. |
| 1532 | resetKeyRepeatLocked(); |
| 1533 | |
| 1534 | // 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] | 1535 | auto command = [this, eventTime = entry.eventTime]() REQUIRES(mLock) { |
| 1536 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 1537 | mPolicy.notifyConfigurationChanged(eventTime); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1538 | }; |
| 1539 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1540 | return true; |
| 1541 | } |
| 1542 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1543 | bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime, |
| 1544 | const DeviceResetEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1545 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1546 | ALOGD("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry.eventTime, |
| 1547 | entry.deviceId); |
| 1548 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1549 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 1550 | // Reset key repeating in case a keyboard device was disabled or enabled. |
| 1551 | if (mKeyRepeatState.lastKeyEntry && mKeyRepeatState.lastKeyEntry->deviceId == entry.deviceId) { |
| 1552 | resetKeyRepeatLocked(); |
| 1553 | } |
| 1554 | |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1555 | CancelationOptions options(CancelationOptions::Mode::CANCEL_ALL_EVENTS, "device was reset"); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1556 | options.deviceId = entry.deviceId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1557 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 1558 | |
| 1559 | // Remove all active pointers from this device |
| 1560 | for (auto& [_, touchState] : mTouchStatesByDisplay) { |
| 1561 | touchState.removeAllPointersForDevice(entry.deviceId); |
| 1562 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1563 | return true; |
| 1564 | } |
| 1565 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1566 | void InputDispatcher::enqueueFocusEventLocked(const sp<IBinder>& windowToken, bool hasFocus, |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1567 | const std::string& reason) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1568 | if (mPendingEvent != nullptr) { |
| 1569 | // Move the pending event to the front of the queue. This will give the chance |
| 1570 | // for the pending event to get dispatched to the newly focused window |
| 1571 | mInboundQueue.push_front(mPendingEvent); |
| 1572 | mPendingEvent = nullptr; |
| 1573 | } |
| 1574 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1575 | std::unique_ptr<FocusEntry> focusEntry = |
| 1576 | std::make_unique<FocusEntry>(mIdGenerator.nextId(), now(), windowToken, hasFocus, |
| 1577 | reason); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1578 | |
| 1579 | // This event should go to the front of the queue, but behind all other focus events |
| 1580 | // Find the last focus event, and insert right after it |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1581 | auto it = std::find_if(mInboundQueue.rbegin(), mInboundQueue.rend(), |
| 1582 | [](const std::shared_ptr<const EventEntry>& event) { |
| 1583 | return event->type == EventEntry::Type::FOCUS; |
| 1584 | }); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1585 | |
| 1586 | // 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] | 1587 | mInboundQueue.insert(it.base(), std::move(focusEntry)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1588 | } |
| 1589 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1590 | void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime, |
| 1591 | std::shared_ptr<const FocusEntry> entry) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 1592 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1593 | if (channel == nullptr) { |
| 1594 | return; // Window has gone away |
| 1595 | } |
| 1596 | InputTarget target; |
| 1597 | target.inputChannel = channel; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 1598 | target.flags = InputTarget::Flags::DISPATCH_AS_IS; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1599 | entry->dispatchInProgress = true; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1600 | std::string message = std::string("Focus ") + (entry->hasFocus ? "entering " : "leaving ") + |
| 1601 | channel->getName(); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 1602 | std::string reason = std::string("reason=").append(entry->reason); |
| 1603 | android_log_event_list(LOGTAG_INPUT_FOCUS) << message << reason << LOG_ID_EVENTS; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1604 | dispatchEventLocked(currentTime, entry, {target}); |
| 1605 | } |
| 1606 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1607 | void InputDispatcher::dispatchPointerCaptureChangedLocked( |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1608 | nsecs_t currentTime, const std::shared_ptr<const PointerCaptureChangedEntry>& entry, |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1609 | DropReason& dropReason) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1610 | dropReason = DropReason::NOT_DROPPED; |
| 1611 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1612 | const bool haveWindowWithPointerCapture = mWindowTokenWithPointerCapture != nullptr; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1613 | sp<IBinder> token; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1614 | |
| 1615 | if (entry->pointerCaptureRequest.enable) { |
| 1616 | // Enable Pointer Capture. |
| 1617 | if (haveWindowWithPointerCapture && |
| 1618 | (entry->pointerCaptureRequest == mCurrentPointerCaptureRequest)) { |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 1619 | // This can happen if pointer capture is disabled and re-enabled before we notify the |
| 1620 | // app of the state change, so there is no need to notify the app. |
| 1621 | ALOGI("Skipping dispatch of Pointer Capture being enabled: no state change."); |
| 1622 | return; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1623 | } |
| 1624 | if (!mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1625 | // This can happen if a window requests capture and immediately releases capture. |
| 1626 | ALOGW("No window requested Pointer Capture."); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1627 | dropReason = DropReason::NO_POINTER_CAPTURE; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1628 | return; |
| 1629 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1630 | if (entry->pointerCaptureRequest.seq != mCurrentPointerCaptureRequest.seq) { |
| 1631 | ALOGI("Skipping dispatch of Pointer Capture being enabled: sequence number mismatch."); |
| 1632 | return; |
| 1633 | } |
| 1634 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1635 | token = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1636 | LOG_ALWAYS_FATAL_IF(!token, "Cannot find focused window for Pointer Capture."); |
| 1637 | mWindowTokenWithPointerCapture = token; |
| 1638 | } else { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1639 | // Disable Pointer Capture. |
| 1640 | // We do not check if the sequence number matches for requests to disable Pointer Capture |
| 1641 | // for two reasons: |
| 1642 | // 1. Pointer Capture can be disabled by a focus change, which means we can get two entries |
| 1643 | // to disable capture with the same sequence number: one generated by |
| 1644 | // disablePointerCaptureForcedLocked() and another as an acknowledgement of Pointer |
| 1645 | // Capture being disabled in InputReader. |
| 1646 | // 2. We respect any request to disable Pointer Capture generated by InputReader, since the |
| 1647 | // actual Pointer Capture state that affects events being generated by input devices is |
| 1648 | // in InputReader. |
| 1649 | if (!haveWindowWithPointerCapture) { |
| 1650 | // Pointer capture was already forcefully disabled because of focus change. |
| 1651 | dropReason = DropReason::NOT_DROPPED; |
| 1652 | return; |
| 1653 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1654 | token = mWindowTokenWithPointerCapture; |
| 1655 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1656 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1657 | setPointerCaptureLocked(false); |
| 1658 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1659 | } |
| 1660 | |
| 1661 | auto channel = getInputChannelLocked(token); |
| 1662 | if (channel == nullptr) { |
| 1663 | // Window has gone away, clean up Pointer Capture state. |
| 1664 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1665 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1666 | setPointerCaptureLocked(false); |
| 1667 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1668 | return; |
| 1669 | } |
| 1670 | InputTarget target; |
| 1671 | target.inputChannel = channel; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 1672 | target.flags = InputTarget::Flags::DISPATCH_AS_IS; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1673 | entry->dispatchInProgress = true; |
| 1674 | dispatchEventLocked(currentTime, entry, {target}); |
| 1675 | |
| 1676 | dropReason = DropReason::NOT_DROPPED; |
| 1677 | } |
| 1678 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1679 | void InputDispatcher::dispatchTouchModeChangeLocked( |
| 1680 | nsecs_t currentTime, const std::shared_ptr<const TouchModeEntry>& entry) { |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1681 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 1682 | getWindowHandlesLocked(entry->displayId); |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1683 | if (windowHandles.empty()) { |
| 1684 | return; |
| 1685 | } |
| 1686 | const std::vector<InputTarget> inputTargets = |
| 1687 | getInputTargetsFromWindowHandlesLocked(windowHandles); |
| 1688 | if (inputTargets.empty()) { |
| 1689 | return; |
| 1690 | } |
| 1691 | entry->dispatchInProgress = true; |
| 1692 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1693 | } |
| 1694 | |
| 1695 | std::vector<InputTarget> InputDispatcher::getInputTargetsFromWindowHandlesLocked( |
| 1696 | const std::vector<sp<WindowInfoHandle>>& windowHandles) const { |
| 1697 | std::vector<InputTarget> inputTargets; |
| 1698 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1699 | const sp<IBinder>& token = handle->getToken(); |
| 1700 | if (token == nullptr) { |
| 1701 | continue; |
| 1702 | } |
| 1703 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(token); |
| 1704 | if (channel == nullptr) { |
| 1705 | continue; // Window has gone away |
| 1706 | } |
| 1707 | InputTarget target; |
| 1708 | target.inputChannel = channel; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 1709 | target.flags = InputTarget::Flags::DISPATCH_AS_IS; |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1710 | inputTargets.push_back(target); |
| 1711 | } |
| 1712 | return inputTargets; |
| 1713 | } |
| 1714 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1715 | bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, std::shared_ptr<const KeyEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1716 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1717 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1718 | if (!entry->dispatchInProgress) { |
| 1719 | if (entry->repeatCount == 0 && entry->action == AKEY_EVENT_ACTION_DOWN && |
| 1720 | (entry->policyFlags & POLICY_FLAG_TRUSTED) && |
| 1721 | (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { |
| 1722 | if (mKeyRepeatState.lastKeyEntry && |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1723 | mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode && |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1724 | // We have seen two identical key downs in a row which indicates that the device |
| 1725 | // driver is automatically generating key repeats itself. We take note of the |
| 1726 | // repeat here, but we disable our own next key repeat timer since it is clear that |
| 1727 | // we will not need to synthesize key repeats ourselves. |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1728 | mKeyRepeatState.lastKeyEntry->deviceId == entry->deviceId) { |
| 1729 | // Make sure we don't get key down from a different device. If a different |
| 1730 | // device Id has same key pressed down, the new device Id will replace the |
| 1731 | // current one to hold the key repeat with repeat count reset. |
| 1732 | // In the future when got a KEY_UP on the device id, drop it and do not |
| 1733 | // stop the key repeat on current device. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1734 | entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; |
| 1735 | resetKeyRepeatLocked(); |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 1736 | mKeyRepeatState.nextRepeatTime = LLONG_MAX; // don't generate repeats ourselves |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1737 | } else { |
| 1738 | // Not a repeat. Save key down state in case we do see a repeat later. |
| 1739 | resetKeyRepeatLocked(); |
| 1740 | mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; |
| 1741 | } |
| 1742 | mKeyRepeatState.lastKeyEntry = entry; |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1743 | } else if (entry->action == AKEY_EVENT_ACTION_UP && mKeyRepeatState.lastKeyEntry && |
| 1744 | mKeyRepeatState.lastKeyEntry->deviceId != entry->deviceId) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1745 | // 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] | 1746 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1747 | ALOGD("deviceId=%d got KEY_UP as stale", entry->deviceId); |
| 1748 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1749 | } else if (!entry->syntheticRepeat) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1750 | resetKeyRepeatLocked(); |
| 1751 | } |
| 1752 | |
| 1753 | if (entry->repeatCount == 1) { |
| 1754 | entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; |
| 1755 | } else { |
| 1756 | entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; |
| 1757 | } |
| 1758 | |
| 1759 | entry->dispatchInProgress = true; |
| 1760 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1761 | logOutboundKeyDetails("dispatchKey - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1762 | } |
| 1763 | |
| 1764 | // 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] | 1765 | if (entry->interceptKeyResult == KeyEntry::InterceptKeyResult::TRY_AGAIN_LATER) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1766 | if (currentTime < entry->interceptKeyWakeupTime) { |
| 1767 | if (entry->interceptKeyWakeupTime < *nextWakeupTime) { |
| 1768 | *nextWakeupTime = entry->interceptKeyWakeupTime; |
| 1769 | } |
| 1770 | return false; // wait until next wakeup |
| 1771 | } |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1772 | entry->interceptKeyResult = KeyEntry::InterceptKeyResult::UNKNOWN; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1773 | entry->interceptKeyWakeupTime = 0; |
| 1774 | } |
| 1775 | |
| 1776 | // Give the policy a chance to intercept the key. |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1777 | if (entry->interceptKeyResult == KeyEntry::InterceptKeyResult::UNKNOWN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1778 | if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1779 | sp<IBinder> focusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1780 | mFocusResolver.getFocusedWindowToken(getTargetDisplayId(*entry)); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1781 | |
| 1782 | auto command = [this, focusedWindowToken, entry]() REQUIRES(mLock) { |
| 1783 | doInterceptKeyBeforeDispatchingCommand(focusedWindowToken, *entry); |
| 1784 | }; |
| 1785 | postCommandLocked(std::move(command)); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1786 | // Poke user activity for keys not passed to user |
| 1787 | pokeUserActivityLocked(*entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1788 | return false; // wait for the command to run |
| 1789 | } else { |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1790 | entry->interceptKeyResult = KeyEntry::InterceptKeyResult::CONTINUE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1791 | } |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1792 | } else if (entry->interceptKeyResult == KeyEntry::InterceptKeyResult::SKIP) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1793 | if (*dropReason == DropReason::NOT_DROPPED) { |
| 1794 | *dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1795 | } |
| 1796 | } |
| 1797 | |
| 1798 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1799 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1800 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1801 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1802 | : InputEventInjectionResult::FAILED); |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1803 | mReporter->reportDroppedKey(entry->id); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1804 | // Poke user activity for undispatched keys |
| 1805 | pokeUserActivityLocked(*entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1806 | return true; |
| 1807 | } |
| 1808 | |
| 1809 | // Identify targets. |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1810 | InputEventInjectionResult injectionResult; |
| 1811 | sp<WindowInfoHandle> focusedWindow = |
| 1812 | findFocusedWindowTargetLocked(currentTime, *entry, nextWakeupTime, |
| 1813 | /*byref*/ injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1814 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1815 | return false; |
| 1816 | } |
| 1817 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1818 | setInjectionResult(*entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1819 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1820 | return true; |
| 1821 | } |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1822 | LOG_ALWAYS_FATAL_IF(focusedWindow == nullptr); |
| 1823 | |
| 1824 | std::vector<InputTarget> inputTargets; |
| 1825 | addWindowTargetLocked(focusedWindow, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 1826 | InputTarget::Flags::FOREGROUND | InputTarget::Flags::DISPATCH_AS_IS, |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 1827 | getDownTime(*entry), inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1828 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1829 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1830 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1831 | |
| 1832 | // Dispatch the key. |
| 1833 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1834 | return true; |
| 1835 | } |
| 1836 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1837 | void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1838 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1839 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 ", " |
| 1840 | "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, " |
| 1841 | "metaState=0x%x, repeatCount=%d, downTime=%" PRId64, |
| 1842 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, |
| 1843 | entry.policyFlags, entry.action, entry.flags, entry.keyCode, entry.scanCode, |
| 1844 | entry.metaState, entry.repeatCount, entry.downTime); |
| 1845 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1846 | } |
| 1847 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1848 | void InputDispatcher::dispatchSensorLocked(nsecs_t currentTime, |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1849 | const std::shared_ptr<const SensorEntry>& entry, |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1850 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1851 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1852 | ALOGD("notifySensorEvent eventTime=%" PRId64 ", hwTimestamp=%" PRId64 ", deviceId=%d, " |
| 1853 | "source=0x%x, sensorType=%s", |
| 1854 | entry->eventTime, entry->hwTimestamp, entry->deviceId, entry->source, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1855 | ftl::enum_string(entry->sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1856 | } |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1857 | auto command = [this, entry]() REQUIRES(mLock) { |
| 1858 | scoped_unlock unlock(mLock); |
| 1859 | |
| 1860 | if (entry->accuracyChanged) { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 1861 | mPolicy.notifySensorAccuracy(entry->deviceId, entry->sensorType, entry->accuracy); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1862 | } |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 1863 | mPolicy.notifySensorEvent(entry->deviceId, entry->sensorType, entry->accuracy, |
| 1864 | entry->hwTimestamp, entry->values); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1865 | }; |
| 1866 | postCommandLocked(std::move(command)); |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1867 | } |
| 1868 | |
| 1869 | bool InputDispatcher::flushSensor(int deviceId, InputDeviceSensorType sensorType) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1870 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1871 | ALOGD("flushSensor deviceId=%d, sensorType=%s", deviceId, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1872 | ftl::enum_string(sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1873 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1874 | { // acquire lock |
| 1875 | std::scoped_lock _l(mLock); |
| 1876 | |
| 1877 | for (auto it = mInboundQueue.begin(); it != mInboundQueue.end(); it++) { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1878 | std::shared_ptr<const EventEntry> entry = *it; |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1879 | if (entry->type == EventEntry::Type::SENSOR) { |
| 1880 | it = mInboundQueue.erase(it); |
| 1881 | releaseInboundEventLocked(entry); |
| 1882 | } |
| 1883 | } |
| 1884 | } |
| 1885 | return true; |
| 1886 | } |
| 1887 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1888 | bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, |
| 1889 | std::shared_ptr<const MotionEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1890 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1891 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1892 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1893 | if (!entry->dispatchInProgress) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1894 | entry->dispatchInProgress = true; |
| 1895 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1896 | logOutboundMotionDetails("dispatchMotion - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1897 | } |
| 1898 | |
| 1899 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1900 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1901 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1902 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1903 | : InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1904 | return true; |
| 1905 | } |
| 1906 | |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 1907 | const bool isPointerEvent = isFromSource(entry->source, AINPUT_SOURCE_CLASS_POINTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1908 | |
| 1909 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1910 | std::vector<InputTarget> inputTargets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1911 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1912 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1913 | if (isPointerEvent) { |
| 1914 | // Pointer event. (eg. touchscreen) |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 1915 | |
| 1916 | if (mDragState && |
| 1917 | (entry->action & AMOTION_EVENT_ACTION_MASK) == AMOTION_EVENT_ACTION_POINTER_DOWN) { |
| 1918 | // If drag and drop ongoing and pointer down occur: pilfer drag window pointers |
| 1919 | pilferPointersLocked(mDragState->dragWindow->getToken()); |
| 1920 | } |
| 1921 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1922 | inputTargets = |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 1923 | findTouchedWindowTargetsLocked(currentTime, *entry, /*byref*/ injectionResult); |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 1924 | LOG_ALWAYS_FATAL_IF(injectionResult != InputEventInjectionResult::SUCCEEDED && |
| 1925 | !inputTargets.empty()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1926 | } else { |
| 1927 | // Non touch event. (eg. trackball) |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1928 | sp<WindowInfoHandle> focusedWindow = |
| 1929 | findFocusedWindowTargetLocked(currentTime, *entry, nextWakeupTime, injectionResult); |
| 1930 | if (injectionResult == InputEventInjectionResult::SUCCEEDED) { |
| 1931 | LOG_ALWAYS_FATAL_IF(focusedWindow == nullptr); |
| 1932 | addWindowTargetLocked(focusedWindow, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 1933 | InputTarget::Flags::FOREGROUND | |
| 1934 | InputTarget::Flags::DISPATCH_AS_IS, |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 1935 | getDownTime(*entry), inputTargets); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1936 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1937 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1938 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1939 | return false; |
| 1940 | } |
| 1941 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1942 | setInjectionResult(*entry, injectionResult); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1943 | if (injectionResult == InputEventInjectionResult::TARGET_MISMATCH) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1944 | return true; |
| 1945 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1946 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1947 | CancelationOptions::Mode mode( |
| 1948 | isPointerEvent ? CancelationOptions::Mode::CANCEL_POINTER_EVENTS |
| 1949 | : CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS); |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1950 | CancelationOptions options(mode, "input event injection failed"); |
| 1951 | synthesizeCancelationEventsForMonitorsLocked(options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1952 | return true; |
| 1953 | } |
| 1954 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1955 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1956 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1957 | |
| 1958 | // Dispatch the motion. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1959 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1960 | return true; |
| 1961 | } |
| 1962 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1963 | void InputDispatcher::enqueueDragEventLocked(const sp<WindowInfoHandle>& windowHandle, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 1964 | bool isExiting, const int32_t rawX, |
| 1965 | const int32_t rawY) { |
| 1966 | const vec2 xy = windowHandle->getInfo()->transform.transform(vec2(rawX, rawY)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1967 | std::unique_ptr<DragEntry> dragEntry = |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 1968 | std::make_unique<DragEntry>(mIdGenerator.nextId(), now(), windowHandle->getToken(), |
| 1969 | isExiting, xy.x, xy.y); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1970 | |
| 1971 | enqueueInboundEventLocked(std::move(dragEntry)); |
| 1972 | } |
| 1973 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1974 | void InputDispatcher::dispatchDragLocked(nsecs_t currentTime, |
| 1975 | std::shared_ptr<const DragEntry> entry) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1976 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
| 1977 | if (channel == nullptr) { |
| 1978 | return; // Window has gone away |
| 1979 | } |
| 1980 | InputTarget target; |
| 1981 | target.inputChannel = channel; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 1982 | target.flags = InputTarget::Flags::DISPATCH_AS_IS; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1983 | entry->dispatchInProgress = true; |
| 1984 | dispatchEventLocked(currentTime, entry, {target}); |
| 1985 | } |
| 1986 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1987 | void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1988 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 1989 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=%s, displayId=%" PRId32 |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1990 | ", policyFlags=0x%x, " |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1991 | "action=%s, actionButton=0x%x, flags=0x%x, " |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1992 | "metaState=0x%x, buttonState=0x%x," |
| 1993 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64, |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 1994 | prefix, entry.eventTime, entry.deviceId, |
| 1995 | inputEventSourceToString(entry.source).c_str(), entry.displayId, entry.policyFlags, |
| 1996 | MotionEvent::actionToString(entry.action).c_str(), entry.actionButton, entry.flags, |
| 1997 | entry.metaState, entry.buttonState, entry.edgeFlags, entry.xPrecision, |
| 1998 | entry.yPrecision, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1999 | |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 2000 | for (uint32_t i = 0; i < entry.getPointerCount(); i++) { |
Siarhei Vishniakou | 09a8fe4 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2001 | ALOGD(" Pointer %d: id=%d, toolType=%s, " |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2002 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 2003 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 2004 | "orientation=%f", |
Siarhei Vishniakou | 09a8fe4 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2005 | i, entry.pointerProperties[i].id, |
| 2006 | ftl::enum_string(entry.pointerProperties[i].toolType).c_str(), |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2007 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 2008 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 2009 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 2010 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 2011 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 2012 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 2013 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 2014 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 2015 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| 2016 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2017 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2018 | } |
| 2019 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2020 | void InputDispatcher::dispatchEventLocked(nsecs_t currentTime, |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 2021 | std::shared_ptr<const EventEntry> eventEntry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2022 | const std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2023 | ATRACE_CALL(); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2024 | if (DEBUG_DISPATCH_CYCLE) { |
| 2025 | ALOGD("dispatchEventToCurrentInputTargets"); |
| 2026 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2027 | |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 2028 | processInteractionsLocked(*eventEntry, inputTargets); |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 2029 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2030 | ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true |
| 2031 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2032 | pokeUserActivityLocked(*eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2033 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2034 | for (const InputTarget& inputTarget : inputTargets) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 2035 | std::shared_ptr<Connection> connection = |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 2036 | getConnectionLocked(inputTarget.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 2037 | if (connection != nullptr) { |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2038 | prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2039 | } else { |
Siarhei Vishniakou | 31dd155 | 2023-10-30 18:46:10 -0700 | [diff] [blame] | 2040 | if (DEBUG_DROPPED_EVENTS_VERBOSE) { |
| 2041 | LOG(INFO) << "Dropping event delivery to target with channel " |
| 2042 | << inputTarget.inputChannel->getName() |
| 2043 | << " because it is no longer registered with the input dispatcher."; |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2044 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2045 | } |
| 2046 | } |
| 2047 | } |
| 2048 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 2049 | void InputDispatcher::cancelEventsForAnrLocked(const std::shared_ptr<Connection>& connection) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2050 | // We will not be breaking any connections here, even if the policy wants us to abort dispatch. |
| 2051 | // If the policy decides to close the app, we will get a channel removal event via |
| 2052 | // unregisterInputChannel, and will clean up the connection that way. We are already not |
| 2053 | // sending new pointers to the connection when it blocked, but focused events will continue to |
| 2054 | // pile up. |
| 2055 | ALOGW("Canceling events for %s because it is unresponsive", |
| 2056 | connection->inputChannel->getName().c_str()); |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 2057 | if (connection->status == Connection::Status::NORMAL) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 2058 | CancelationOptions options(CancelationOptions::Mode::CANCEL_ALL_EVENTS, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2059 | "application not responding"); |
| 2060 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2061 | } |
| 2062 | } |
| 2063 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2064 | void InputDispatcher::resetNoFocusedWindowTimeoutLocked() { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2065 | if (DEBUG_FOCUS) { |
| 2066 | ALOGD("Resetting ANR timeouts."); |
| 2067 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2068 | |
| 2069 | // Reset input target wait timeout. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2070 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2071 | mAwaitedFocusedApplication.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2072 | } |
| 2073 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2074 | /** |
| 2075 | * Get the display id that the given event should go to. If this event specifies a valid display id, |
| 2076 | * then it should be dispatched to that display. Otherwise, the event goes to the focused display. |
| 2077 | * Focused display is the display that the user most recently interacted with. |
| 2078 | */ |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2079 | int32_t InputDispatcher::getTargetDisplayId(const EventEntry& entry) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2080 | int32_t displayId; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2081 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2082 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2083 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 2084 | displayId = keyEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2085 | break; |
| 2086 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2087 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2088 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 2089 | displayId = motionEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2090 | break; |
| 2091 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 2092 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 2093 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2094 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2095 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2096 | case EventEntry::Type::DEVICE_RESET: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2097 | case EventEntry::Type::SENSOR: |
| 2098 | case EventEntry::Type::DRAG: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2099 | 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] | 2100 | return ADISPLAY_ID_NONE; |
| 2101 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2102 | } |
| 2103 | return displayId == ADISPLAY_ID_NONE ? mFocusedDisplayId : displayId; |
| 2104 | } |
| 2105 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2106 | bool InputDispatcher::shouldWaitToSendKeyLocked(nsecs_t currentTime, |
| 2107 | const char* focusedWindowName) { |
| 2108 | if (mAnrTracker.empty()) { |
| 2109 | // already processed all events that we waited for |
| 2110 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 2111 | return false; |
| 2112 | } |
| 2113 | |
| 2114 | if (!mKeyIsWaitingForEventsTimeout.has_value()) { |
| 2115 | // Start the timer |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 2116 | // 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] | 2117 | mKeyIsWaitingForEventsTimeout = currentTime + |
| 2118 | std::chrono::duration_cast<std::chrono::nanoseconds>(KEY_WAITING_FOR_EVENTS_TIMEOUT) |
| 2119 | .count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2120 | return true; |
| 2121 | } |
| 2122 | |
| 2123 | // We still have pending events, and already started the timer |
| 2124 | if (currentTime < *mKeyIsWaitingForEventsTimeout) { |
| 2125 | return true; // Still waiting |
| 2126 | } |
| 2127 | |
| 2128 | // Waited too long, and some connection still hasn't processed all motions |
| 2129 | // Just send the key to the focused window |
| 2130 | ALOGW("Dispatching key to %s even though there are other unprocessed events", |
| 2131 | focusedWindowName); |
| 2132 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 2133 | return false; |
| 2134 | } |
| 2135 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2136 | sp<WindowInfoHandle> InputDispatcher::findFocusedWindowTargetLocked( |
| 2137 | nsecs_t currentTime, const EventEntry& entry, nsecs_t* nextWakeupTime, |
| 2138 | InputEventInjectionResult& outInjectionResult) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2139 | std::string reason; |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2140 | outInjectionResult = InputEventInjectionResult::FAILED; // Default result |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2141 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2142 | int32_t displayId = getTargetDisplayId(entry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2143 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2144 | std::shared_ptr<InputApplicationHandle> focusedApplicationHandle = |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2145 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 2146 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2147 | // If there is no currently focused window and no focused application |
| 2148 | // then drop the event. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2149 | if (focusedWindowHandle == nullptr && focusedApplicationHandle == nullptr) { |
| 2150 | ALOGI("Dropping %s event because there is no focused window or focused application in " |
| 2151 | "display %" PRId32 ".", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2152 | ftl::enum_string(entry.type).c_str(), displayId); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2153 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2154 | } |
| 2155 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2156 | // Drop key events if requested by input feature |
| 2157 | if (focusedWindowHandle != nullptr && shouldDropInput(entry, focusedWindowHandle)) { |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2158 | return nullptr; |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2159 | } |
| 2160 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2161 | // Compatibility behavior: raise ANR if there is a focused application, but no focused window. |
| 2162 | // Only start counting when we have a focused event to dispatch. The ANR is canceled if we |
| 2163 | // start interacting with another application via touch (app switch). This code can be removed |
| 2164 | // if the "no focused window ANR" is moved to the policy. Input doesn't know whether |
| 2165 | // an app is expected to have a focused window. |
| 2166 | if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) { |
| 2167 | if (!mNoFocusedWindowTimeoutTime.has_value()) { |
| 2168 | // 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] | 2169 | std::chrono::nanoseconds timeout = focusedApplicationHandle->getDispatchingTimeout( |
| 2170 | DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
| 2171 | mNoFocusedWindowTimeoutTime = currentTime + timeout.count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2172 | mAwaitedFocusedApplication = focusedApplicationHandle; |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 2173 | mAwaitedApplicationDisplayId = displayId; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2174 | ALOGW("Waiting because no window has focus but %s may eventually add a " |
| 2175 | "window when it finishes starting up. Will wait for %" PRId64 "ms", |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 2176 | mAwaitedFocusedApplication->getName().c_str(), millis(timeout)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2177 | *nextWakeupTime = *mNoFocusedWindowTimeoutTime; |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2178 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 2179 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2180 | } else if (currentTime > *mNoFocusedWindowTimeoutTime) { |
| 2181 | // Already raised ANR. Drop the event |
| 2182 | ALOGE("Dropping %s event because there is no focused window", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2183 | ftl::enum_string(entry.type).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2184 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2185 | } else { |
| 2186 | // Still waiting for the focused window |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2187 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 2188 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2189 | } |
| 2190 | } |
| 2191 | |
| 2192 | // we have a valid, non-null focused window |
| 2193 | resetNoFocusedWindowTimeoutLocked(); |
| 2194 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2195 | // Verify targeted injection. |
| 2196 | if (const auto err = verifyTargetedInjection(focusedWindowHandle, entry); err) { |
| 2197 | ALOGW("Dropping injected event: %s", (*err).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2198 | outInjectionResult = InputEventInjectionResult::TARGET_MISMATCH; |
| 2199 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2200 | } |
| 2201 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2202 | if (focusedWindowHandle->getInfo()->inputConfig.test( |
| 2203 | WindowInfo::InputConfig::PAUSE_DISPATCHING)) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2204 | ALOGI("Waiting because %s is paused", focusedWindowHandle->getName().c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2205 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 2206 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2207 | } |
| 2208 | |
| 2209 | // If the event is a key event, then we must wait for all previous events to |
| 2210 | // complete before delivering it because previous events may have the |
| 2211 | // side-effect of transferring focus to a different window and we want to |
| 2212 | // ensure that the following keys are sent to the new window. |
| 2213 | // |
| 2214 | // Suppose the user touches a button in a window then immediately presses "A". |
| 2215 | // If the button causes a pop-up window to appear then we want to ensure that |
| 2216 | // the "A" key is delivered to the new pop-up window. This is because users |
| 2217 | // often anticipate pending UI changes when typing on a keyboard. |
| 2218 | // To obtain this behavior, we must serialize key events with respect to all |
| 2219 | // prior input events. |
| 2220 | if (entry.type == EventEntry::Type::KEY) { |
| 2221 | if (shouldWaitToSendKeyLocked(currentTime, focusedWindowHandle->getName().c_str())) { |
| 2222 | *nextWakeupTime = *mKeyIsWaitingForEventsTimeout; |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2223 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 2224 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2225 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2226 | } |
| 2227 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2228 | outInjectionResult = InputEventInjectionResult::SUCCEEDED; |
| 2229 | return focusedWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2230 | } |
| 2231 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2232 | /** |
| 2233 | * Given a list of monitors, remove the ones we cannot find a connection for, and the ones |
| 2234 | * that are currently unresponsive. |
| 2235 | */ |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2236 | std::vector<Monitor> InputDispatcher::selectResponsiveMonitorsLocked( |
| 2237 | const std::vector<Monitor>& monitors) const { |
| 2238 | std::vector<Monitor> responsiveMonitors; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2239 | std::copy_if(monitors.begin(), monitors.end(), std::back_inserter(responsiveMonitors), |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2240 | [this](const Monitor& monitor) REQUIRES(mLock) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 2241 | std::shared_ptr<Connection> connection = |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2242 | getConnectionLocked(monitor.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2243 | if (connection == nullptr) { |
| 2244 | ALOGE("Could not find connection for monitor %s", |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2245 | monitor.inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2246 | return false; |
| 2247 | } |
| 2248 | if (!connection->responsive) { |
| 2249 | ALOGW("Unresponsive monitor %s will not get the new gesture", |
| 2250 | connection->inputChannel->getName().c_str()); |
| 2251 | return false; |
| 2252 | } |
| 2253 | return true; |
| 2254 | }); |
| 2255 | return responsiveMonitors; |
| 2256 | } |
| 2257 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2258 | std::vector<InputTarget> InputDispatcher::findTouchedWindowTargetsLocked( |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2259 | nsecs_t currentTime, const MotionEntry& entry, |
Siarhei Vishniakou | 4fe5739 | 2022-10-25 13:44:30 -0700 | [diff] [blame] | 2260 | InputEventInjectionResult& outInjectionResult) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2261 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2262 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2263 | std::vector<InputTarget> targets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2264 | // For security reasons, we defer updating the touch state until we are sure that |
| 2265 | // event injection will be allowed. |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2266 | const int32_t displayId = entry.displayId; |
| 2267 | const int32_t action = entry.action; |
Siarhei Vishniakou | bf88052 | 2023-05-01 11:03:22 -0700 | [diff] [blame] | 2268 | const int32_t maskedAction = MotionEvent::getActionMasked(action); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2269 | |
| 2270 | // 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] | 2271 | outInjectionResult = InputEventInjectionResult::PENDING; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2272 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2273 | // Copy current touch state into tempTouchState. |
| 2274 | // This state will be used to update mTouchStatesByDisplay at the end of this function. |
| 2275 | // 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] | 2276 | const TouchState* oldState = nullptr; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2277 | TouchState tempTouchState; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2278 | if (const auto it = mTouchStatesByDisplay.find(displayId); it != mTouchStatesByDisplay.end()) { |
| 2279 | oldState = &(it->second); |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 2280 | tempTouchState = *oldState; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2281 | } |
| 2282 | |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 2283 | bool isSplit = shouldSplitTouch(tempTouchState, entry); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2284 | |
| 2285 | const bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE || |
| 2286 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2287 | maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2288 | // A DOWN could be generated from POINTER_DOWN if the initial pointers did not land into any |
| 2289 | // touchable windows. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2290 | const bool wasDown = oldState != nullptr && oldState->isDown(entry.deviceId); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2291 | const bool isDown = (maskedAction == AMOTION_EVENT_ACTION_DOWN) || |
| 2292 | (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN && !wasDown); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2293 | const bool newGesture = isDown || maskedAction == AMOTION_EVENT_ACTION_SCROLL || |
| 2294 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2295 | maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE; |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 2296 | const bool isFromMouse = isFromSource(entry.source, AINPUT_SOURCE_MOUSE); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2297 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2298 | if (newGesture) { |
| 2299 | isSplit = false; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2300 | } |
| 2301 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2302 | if (isDown && tempTouchState.hasHoveringPointers(entry.deviceId)) { |
| 2303 | // Compatibility behaviour: ACTION_DOWN causes HOVER_EXIT to get generated. |
| 2304 | tempTouchState.clearHoveringPointers(entry.deviceId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2305 | } |
| 2306 | |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2307 | if (isHoverAction) { |
| 2308 | // For hover actions, we will treat 'tempTouchState' as a new state, so let's erase |
| 2309 | // all of the existing hovering pointers and recompute. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2310 | tempTouchState.clearHoveringPointers(entry.deviceId); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2311 | } |
| 2312 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2313 | if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { |
| 2314 | /* 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] | 2315 | const auto [x, y] = resolveTouchedPosition(entry); |
Siarhei Vishniakou | 5b9766d | 2023-07-18 14:06:29 -0700 | [diff] [blame] | 2316 | const int32_t pointerIndex = MotionEvent::getActionIndex(action); |
Siarhei Vishniakou | 70f3d8c | 2023-09-19 15:36:52 -0700 | [diff] [blame] | 2317 | const int32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2318 | // Outside targets should be added upon first dispatched DOWN event. That means, this should |
| 2319 | // 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] | 2320 | const bool isStylus = isPointerFromStylus(entry, pointerIndex); |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 2321 | sp<WindowInfoHandle> newTouchedWindowHandle = |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2322 | findTouchedWindowAtLocked(displayId, x, y, isStylus); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2323 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2324 | if (isDown) { |
Siarhei Vishniakou | 70f3d8c | 2023-09-19 15:36:52 -0700 | [diff] [blame] | 2325 | targets += findOutsideTargetsLocked(displayId, newTouchedWindowHandle, pointerId); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2326 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2327 | // Handle the case where we did not find a window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2328 | if (newTouchedWindowHandle == nullptr) { |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 2329 | 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] | 2330 | // 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] | 2331 | newTouchedWindowHandle = tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2332 | } |
| 2333 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2334 | // Verify targeted injection. |
| 2335 | if (const auto err = verifyTargetedInjection(newTouchedWindowHandle, entry); err) { |
| 2336 | ALOGW("Dropping injected touch event: %s", (*err).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2337 | outInjectionResult = os::InputEventInjectionResult::TARGET_MISMATCH; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2338 | newTouchedWindowHandle = nullptr; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2339 | return {}; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2340 | } |
| 2341 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2342 | // Figure out whether splitting will be allowed for this window. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2343 | if (newTouchedWindowHandle != nullptr) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2344 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
| 2345 | // New window supports splitting, but we should never split mouse events. |
| 2346 | isSplit = !isFromMouse; |
| 2347 | } else if (isSplit) { |
| 2348 | // New window does not support splitting but we have already split events. |
| 2349 | // Ignore the new window. |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame] | 2350 | LOG(INFO) << "Skipping " << newTouchedWindowHandle->getName() |
| 2351 | << " because it doesn't support split touch"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2352 | newTouchedWindowHandle = nullptr; |
| 2353 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2354 | } else { |
| 2355 | // 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] | 2356 | // be delivered to a new window which supports split touch. Pointers from a mouse device |
| 2357 | // should never be split. |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 2358 | isSplit = !isFromMouse; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2359 | } |
| 2360 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2361 | std::vector<sp<WindowInfoHandle>> newTouchedWindows = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2362 | findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2363 | if (newTouchedWindowHandle != nullptr) { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2364 | // Process the foreground window first so that it is the first to receive the event. |
| 2365 | newTouchedWindows.insert(newTouchedWindows.begin(), newTouchedWindowHandle); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2366 | } |
| 2367 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2368 | if (newTouchedWindows.empty()) { |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 2369 | ALOGI("Dropping event because there is no touchable window at (%.1f, %.1f) on display " |
| 2370 | "%d.", |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2371 | x, y, displayId); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2372 | outInjectionResult = InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2373 | return {}; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2374 | } |
| 2375 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2376 | for (const sp<WindowInfoHandle>& windowHandle : newTouchedWindows) { |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 2377 | if (!canWindowReceiveMotionLocked(windowHandle, entry)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2378 | continue; |
| 2379 | } |
| 2380 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2381 | if (isHoverAction) { |
Siarhei Vishniakou | b681c20 | 2023-05-01 11:22:33 -0700 | [diff] [blame] | 2382 | // The "windowHandle" is the target of this hovering pointer. |
| 2383 | tempTouchState.addHoveringPointerToWindow(windowHandle, entry.deviceId, pointerId); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2384 | } |
| 2385 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2386 | // Set target flags. |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2387 | ftl::Flags<InputTarget::Flags> targetFlags = InputTarget::Flags::DISPATCH_AS_IS; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2388 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2389 | if (canReceiveForegroundTouches(*windowHandle->getInfo())) { |
| 2390 | // 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] | 2391 | targetFlags |= InputTarget::Flags::FOREGROUND; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2392 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2393 | |
| 2394 | if (isSplit) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2395 | targetFlags |= InputTarget::Flags::SPLIT; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2396 | } |
| 2397 | if (isWindowObscuredAtPointLocked(windowHandle, x, y)) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2398 | targetFlags |= InputTarget::Flags::WINDOW_IS_OBSCURED; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2399 | } else if (isWindowObscuredLocked(windowHandle)) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2400 | targetFlags |= InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2401 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2402 | |
| 2403 | // Update the temporary touch state. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2404 | |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2405 | if (!isHoverAction) { |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2406 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
Siarhei Vishniakou | 70f3d8c | 2023-09-19 15:36:52 -0700 | [diff] [blame] | 2407 | pointerIds.set(pointerId); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2408 | const bool isDownOrPointerDown = maskedAction == AMOTION_EVENT_ACTION_DOWN || |
| 2409 | maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN; |
| 2410 | tempTouchState.addOrUpdateWindow(windowHandle, targetFlags, entry.deviceId, |
| 2411 | pointerIds, |
| 2412 | isDownOrPointerDown |
| 2413 | ? std::make_optional(entry.eventTime) |
| 2414 | : std::nullopt); |
| 2415 | // If this is the pointer going down and the touched window has a wallpaper |
| 2416 | // then also add the touched wallpaper windows so they are locked in for the |
| 2417 | // duration of the touch gesture. We do not collect wallpapers during HOVER_MOVE or |
| 2418 | // SCROLL because the wallpaper engine only supports touch events. We would need to |
| 2419 | // add a mechanism similar to View.onGenericMotionEvent to enable wallpapers to |
| 2420 | // handle these events. |
| 2421 | if (isDownOrPointerDown && targetFlags.test(InputTarget::Flags::FOREGROUND) && |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2422 | windowHandle->getInfo()->inputConfig.test( |
| 2423 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER)) { |
| 2424 | sp<WindowInfoHandle> wallpaper = findWallpaperWindowBelow(windowHandle); |
| 2425 | if (wallpaper != nullptr) { |
| 2426 | ftl::Flags<InputTarget::Flags> wallpaperFlags = |
| 2427 | InputTarget::Flags::WINDOW_IS_OBSCURED | |
| 2428 | InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED | |
| 2429 | InputTarget::Flags::DISPATCH_AS_IS; |
| 2430 | if (isSplit) { |
| 2431 | wallpaperFlags |= InputTarget::Flags::SPLIT; |
| 2432 | } |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 2433 | tempTouchState.addOrUpdateWindow(wallpaper, wallpaperFlags, entry.deviceId, |
| 2434 | pointerIds, entry.eventTime); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2435 | } |
| 2436 | } |
| 2437 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2438 | } |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 2439 | |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 2440 | // If a window is already pilfering some pointers, give it this new pointer as well and |
| 2441 | // make it pilfering. This will prevent other non-spy windows from getting this pointer, |
| 2442 | // which is a specific behaviour that we want. |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 2443 | for (TouchedWindow& touchedWindow : tempTouchState.windows) { |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 2444 | if (touchedWindow.hasTouchingPointer(entry.deviceId, pointerId) && |
| 2445 | touchedWindow.hasPilferingPointers(entry.deviceId)) { |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 2446 | // This window is already pilfering some pointers, and this new pointer is also |
| 2447 | // going to it. Therefore, take over this pointer and don't give it to anyone |
| 2448 | // else. |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 2449 | touchedWindow.addPilferingPointer(entry.deviceId, pointerId); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 2450 | } |
| 2451 | } |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 2452 | |
| 2453 | // Restrict all pilfered pointers to the pilfering windows. |
| 2454 | tempTouchState.cancelPointersForNonPilferingWindows(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2455 | } else { |
| 2456 | /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ |
| 2457 | |
| 2458 | // If the pointer is not currently down, then ignore the event. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2459 | if (!tempTouchState.isDown(entry.deviceId) && |
| 2460 | maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT) { |
Siarhei Vishniakou | 31dd155 | 2023-10-30 18:46:10 -0700 | [diff] [blame] | 2461 | if (DEBUG_DROPPED_EVENTS_VERBOSE) { |
| 2462 | LOG(INFO) << "Dropping event because the pointer for device " << entry.deviceId |
| 2463 | << " is not down or we previously dropped the pointer down event in " |
| 2464 | << "display " << displayId << ": " << entry.getDescription(); |
| 2465 | } |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2466 | outInjectionResult = InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2467 | return {}; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2468 | } |
| 2469 | |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2470 | // If the pointer is not currently hovering, then ignore the event. |
| 2471 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 2472 | const int32_t pointerId = entry.pointerProperties[0].id; |
| 2473 | if (oldState == nullptr || |
| 2474 | oldState->getWindowsWithHoveringPointer(entry.deviceId, pointerId).empty()) { |
| 2475 | LOG(INFO) << "Dropping event because the hovering pointer is not in any windows in " |
| 2476 | "display " |
| 2477 | << displayId << ": " << entry.getDescription(); |
| 2478 | outInjectionResult = InputEventInjectionResult::FAILED; |
| 2479 | return {}; |
| 2480 | } |
| 2481 | tempTouchState.removeHoveringPointer(entry.deviceId, pointerId); |
| 2482 | } |
| 2483 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2484 | addDragEventLocked(entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2485 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2486 | // Check whether touches should slip outside of the current foreground window. |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 2487 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.getPointerCount() == 1 && |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2488 | tempTouchState.isSlippery()) { |
Siarhei Vishniakou | 9306c38 | 2022-09-30 15:30:31 -0700 | [diff] [blame] | 2489 | const auto [x, y] = resolveTouchedPosition(entry); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2490 | const bool isStylus = isPointerFromStylus(entry, /*pointerIndex=*/0); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2491 | sp<WindowInfoHandle> oldTouchedWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2492 | tempTouchState.getFirstForegroundWindowHandle(); |
Siarhei Vishniakou | 0f6558d | 2023-04-21 12:05:13 -0700 | [diff] [blame] | 2493 | LOG_ALWAYS_FATAL_IF(oldTouchedWindowHandle == nullptr); |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 2494 | sp<WindowInfoHandle> newTouchedWindowHandle = |
| 2495 | findTouchedWindowAtLocked(displayId, x, y, isStylus); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2496 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2497 | // Verify targeted injection. |
| 2498 | if (const auto err = verifyTargetedInjection(newTouchedWindowHandle, entry); err) { |
| 2499 | ALOGW("Dropping injected event: %s", (*err).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2500 | outInjectionResult = os::InputEventInjectionResult::TARGET_MISMATCH; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2501 | return {}; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2502 | } |
| 2503 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2504 | // Drop touch events if requested by input feature |
| 2505 | if (newTouchedWindowHandle != nullptr && |
| 2506 | shouldDropInput(entry, newTouchedWindowHandle)) { |
| 2507 | newTouchedWindowHandle = nullptr; |
| 2508 | } |
| 2509 | |
Siarhei Vishniakou | afa08cc | 2023-05-08 22:35:50 -0700 | [diff] [blame] | 2510 | if (newTouchedWindowHandle != nullptr && |
| 2511 | !haveSameToken(oldTouchedWindowHandle, newTouchedWindowHandle)) { |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2512 | 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] | 2513 | oldTouchedWindowHandle->getName().c_str(), |
| 2514 | newTouchedWindowHandle->getName().c_str(), displayId); |
| 2515 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2516 | // Make a slippery exit from the old window. |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 2517 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2518 | const int32_t pointerId = entry.pointerProperties[0].id; |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 2519 | pointerIds.set(pointerId); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2520 | |
| 2521 | const TouchedWindow& touchedWindow = |
| 2522 | tempTouchState.getTouchedWindow(oldTouchedWindowHandle); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2523 | addPointerWindowTargetLocked(oldTouchedWindowHandle, |
| 2524 | InputTarget::Flags::DISPATCH_AS_SLIPPERY_EXIT, |
| 2525 | pointerIds, |
| 2526 | touchedWindow.getDownTimeInTarget(entry.deviceId), |
| 2527 | targets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2528 | |
| 2529 | // Make a slippery entrance into the new window. |
| 2530 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
Prabir Pradhan | 713bb3e | 2021-12-20 02:07:40 -0800 | [diff] [blame] | 2531 | isSplit = !isFromMouse; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2532 | } |
| 2533 | |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2534 | ftl::Flags<InputTarget::Flags> targetFlags = |
| 2535 | InputTarget::Flags::DISPATCH_AS_SLIPPERY_ENTER; |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2536 | if (canReceiveForegroundTouches(*newTouchedWindowHandle->getInfo())) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2537 | targetFlags |= InputTarget::Flags::FOREGROUND; |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2538 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2539 | if (isSplit) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2540 | targetFlags |= InputTarget::Flags::SPLIT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2541 | } |
| 2542 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2543 | targetFlags |= InputTarget::Flags::WINDOW_IS_OBSCURED; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2544 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2545 | targetFlags |= InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2546 | } |
| 2547 | |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 2548 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, |
| 2549 | entry.deviceId, pointerIds, entry.eventTime); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2550 | |
| 2551 | // Check if the wallpaper window should deliver the corresponding event. |
| 2552 | slipWallpaperTouch(targetFlags, oldTouchedWindowHandle, newTouchedWindowHandle, |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 2553 | tempTouchState, entry.deviceId, pointerId, targets); |
| 2554 | tempTouchState.removeTouchingPointerFromWindow(entry.deviceId, pointerId, |
| 2555 | oldTouchedWindowHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2556 | } |
| 2557 | } |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 2558 | |
| 2559 | // Update the pointerIds for non-splittable when it received pointer down. |
| 2560 | if (!isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN) { |
| 2561 | // If no split, we suppose all touched windows should receive pointer down. |
Siarhei Vishniakou | 5b9766d | 2023-07-18 14:06:29 -0700 | [diff] [blame] | 2562 | const int32_t pointerIndex = MotionEvent::getActionIndex(action); |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 2563 | for (size_t i = 0; i < tempTouchState.windows.size(); i++) { |
| 2564 | TouchedWindow& touchedWindow = tempTouchState.windows[i]; |
| 2565 | // Ignore drag window for it should just track one pointer. |
| 2566 | if (mDragState && mDragState->dragWindow == touchedWindow.windowHandle) { |
| 2567 | continue; |
| 2568 | } |
Siarhei Vishniakou | 5b9766d | 2023-07-18 14:06:29 -0700 | [diff] [blame] | 2569 | std::bitset<MAX_POINTER_ID + 1> touchingPointers; |
| 2570 | touchingPointers.set(entry.pointerProperties[pointerIndex].id); |
| 2571 | touchedWindow.addTouchingPointers(entry.deviceId, touchingPointers); |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 2572 | } |
| 2573 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2574 | } |
| 2575 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2576 | // Update dispatching for hover enter and exit. |
Sam Dubey | f886dec | 2023-01-27 13:28:19 +0000 | [diff] [blame] | 2577 | { |
| 2578 | std::vector<TouchedWindow> hoveringWindows = |
| 2579 | getHoveringWindowsLocked(oldState, tempTouchState, entry); |
| 2580 | for (const TouchedWindow& touchedWindow : hoveringWindows) { |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 2581 | std::optional<InputTarget> target = |
| 2582 | createInputTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags, |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 2583 | touchedWindow.getDownTimeInTarget(entry.deviceId)); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 2584 | if (!target) { |
| 2585 | continue; |
| 2586 | } |
| 2587 | // Hardcode to single hovering pointer for now. |
| 2588 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
| 2589 | pointerIds.set(entry.pointerProperties[0].id); |
| 2590 | target->addPointers(pointerIds, touchedWindow.windowHandle->getInfo()->transform); |
| 2591 | targets.push_back(*target); |
Sam Dubey | f886dec | 2023-01-27 13:28:19 +0000 | [diff] [blame] | 2592 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2593 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2594 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2595 | // Ensure that all touched windows are valid for injection. |
| 2596 | if (entry.injectionState != nullptr) { |
| 2597 | std::string errs; |
| 2598 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2599 | const auto err = verifyTargetedInjection(touchedWindow.windowHandle, entry); |
| 2600 | if (err) errs += "\n - " + *err; |
| 2601 | } |
| 2602 | if (!errs.empty()) { |
| 2603 | 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] | 2604 | "%s:%s", |
| 2605 | entry.injectionState->targetUid->toString().c_str(), errs.c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2606 | outInjectionResult = InputEventInjectionResult::TARGET_MISMATCH; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2607 | return {}; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2608 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2609 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2610 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2611 | // Check whether windows listening for outside touches are owned by the same UID. If the owner |
| 2612 | // 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] | 2613 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2614 | sp<WindowInfoHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2615 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2616 | if (foregroundWindowHandle) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 2617 | const auto foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2618 | for (InputTarget& target : targets) { |
| 2619 | if (target.flags.test(InputTarget::Flags::DISPATCH_AS_OUTSIDE)) { |
| 2620 | sp<WindowInfoHandle> targetWindow = |
| 2621 | getWindowHandleLocked(target.inputChannel->getConnectionToken()); |
| 2622 | if (targetWindow->getInfo()->ownerUid != foregroundWindowUid) { |
| 2623 | target.flags |= InputTarget::Flags::ZERO_COORDS; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2624 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2625 | } |
| 2626 | } |
| 2627 | } |
| 2628 | } |
| 2629 | |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 2630 | // If this is a touchpad navigation gesture, it needs to only be sent to trusted targets, as we |
| 2631 | // only want the system UI to handle these gestures. |
| 2632 | const bool isTouchpadNavGesture = isFromSource(entry.source, AINPUT_SOURCE_MOUSE) && |
| 2633 | entry.classification == MotionClassification::MULTI_FINGER_SWIPE; |
| 2634 | if (isTouchpadNavGesture) { |
| 2635 | filterUntrustedTargets(/* byref */ tempTouchState, /* byref */ targets); |
| 2636 | } |
| 2637 | |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2638 | // Output targets from the touch state. |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2639 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2640 | std::bitset<MAX_POINTER_ID + 1> touchingPointers = |
| 2641 | touchedWindow.getTouchingPointers(entry.deviceId); |
| 2642 | if (touchingPointers.none()) { |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2643 | continue; |
| 2644 | } |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2645 | addPointerWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags, |
| 2646 | touchingPointers, |
| 2647 | touchedWindow.getDownTimeInTarget(entry.deviceId), targets); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2648 | } |
Sam Dubey | 39d37cf | 2022-12-07 18:05:35 +0000 | [diff] [blame] | 2649 | |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 2650 | // During targeted injection, only allow owned targets to receive events |
| 2651 | std::erase_if(targets, [&](const InputTarget& target) { |
| 2652 | LOG_ALWAYS_FATAL_IF(target.windowHandle == nullptr); |
| 2653 | const auto err = verifyTargetedInjection(target.windowHandle, entry); |
| 2654 | if (err) { |
| 2655 | LOG(WARNING) << "Dropping injected event from " << target.windowHandle->getName() |
| 2656 | << ": " << (*err); |
| 2657 | return true; |
| 2658 | } |
| 2659 | return false; |
| 2660 | }); |
| 2661 | |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2662 | if (targets.empty()) { |
| 2663 | LOG(INFO) << "Dropping event because no targets were found: " << entry.getDescription(); |
| 2664 | outInjectionResult = InputEventInjectionResult::FAILED; |
| 2665 | return {}; |
| 2666 | } |
| 2667 | |
| 2668 | // If we only have windows getting ACTION_OUTSIDE, then drop the event, because there is no |
| 2669 | // window that is actually receiving the entire gesture. |
| 2670 | if (std::all_of(targets.begin(), targets.end(), [](const InputTarget& target) { |
| 2671 | return target.flags.test(InputTarget::Flags::DISPATCH_AS_OUTSIDE); |
| 2672 | })) { |
| 2673 | LOG(INFO) << "Dropping event because all windows would just receive ACTION_OUTSIDE: " |
| 2674 | << entry.getDescription(); |
| 2675 | outInjectionResult = InputEventInjectionResult::FAILED; |
| 2676 | return {}; |
| 2677 | } |
| 2678 | |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2679 | outInjectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2680 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2681 | for (TouchedWindow& touchedWindow : tempTouchState.windows) { |
| 2682 | // Targets that we entered in a slippery way will now become AS-IS targets |
| 2683 | if (touchedWindow.targetFlags.test(InputTarget::Flags::DISPATCH_AS_SLIPPERY_ENTER)) { |
| 2684 | touchedWindow.targetFlags.clear(InputTarget::Flags::DISPATCH_AS_SLIPPERY_ENTER); |
| 2685 | touchedWindow.targetFlags |= InputTarget::Flags::DISPATCH_AS_IS; |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2686 | } |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2687 | } |
| 2688 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2689 | // Update final pieces of touch state if the injector had permission. |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2690 | if (isHoverAction) { |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2691 | if (oldState && oldState->isDown(entry.deviceId)) { |
| 2692 | // Started hovering, but the device is already down: reject the hover event |
| 2693 | LOG(ERROR) << "Got hover event " << entry.getDescription() |
| 2694 | << " but the device is already down " << oldState->dump(); |
| 2695 | outInjectionResult = InputEventInjectionResult::FAILED; |
| 2696 | return {}; |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2697 | } |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2698 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP) { |
| 2699 | // Pointer went up. |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 2700 | tempTouchState.removeTouchingPointer(entry.deviceId, entry.pointerProperties[0].id); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2701 | } else if (maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2702 | // All pointers up or canceled. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2703 | tempTouchState.removeAllPointersForDevice(entry.deviceId); |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2704 | } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 2705 | // One pointer went up. |
Siarhei Vishniakou | 5b9766d | 2023-07-18 14:06:29 -0700 | [diff] [blame] | 2706 | const int32_t pointerIndex = MotionEvent::getActionIndex(action); |
| 2707 | const uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
| 2708 | tempTouchState.removeTouchingPointer(entry.deviceId, pointerId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2709 | } |
| 2710 | |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2711 | // Save changes unless the action was scroll in which case the temporary touch |
| 2712 | // state was only valid for this one action. |
| 2713 | if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) { |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 2714 | if (displayId >= 0) { |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2715 | tempTouchState.clearWindowsWithoutPointers(); |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2716 | mTouchStatesByDisplay[displayId] = tempTouchState; |
| 2717 | } else { |
| 2718 | mTouchStatesByDisplay.erase(displayId); |
| 2719 | } |
| 2720 | } |
| 2721 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 2722 | if (tempTouchState.windows.empty()) { |
| 2723 | mTouchStatesByDisplay.erase(displayId); |
| 2724 | } |
| 2725 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2726 | return targets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2727 | } |
| 2728 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2729 | void InputDispatcher::finishDragAndDrop(int32_t displayId, float x, float y) { |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2730 | // Prevent stylus interceptor windows from affecting drag and drop behavior for now, until we |
| 2731 | // have an explicit reason to support it. |
| 2732 | constexpr bool isStylus = false; |
| 2733 | |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 2734 | sp<WindowInfoHandle> dropWindow = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2735 | findTouchedWindowAtLocked(displayId, x, y, isStylus, /*ignoreDragWindow=*/true); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2736 | if (dropWindow) { |
| 2737 | vec2 local = dropWindow->getInfo()->transform.transform(x, y); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2738 | sendDropWindowCommandLocked(dropWindow->getToken(), local.x, local.y); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 2739 | } else { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2740 | ALOGW("No window found when drop."); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2741 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2742 | } |
| 2743 | mDragState.reset(); |
| 2744 | } |
| 2745 | |
| 2746 | void InputDispatcher::addDragEventLocked(const MotionEntry& entry) { |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 2747 | if (!mDragState || mDragState->dragWindow->getInfo()->displayId != entry.displayId) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2748 | return; |
| 2749 | } |
| 2750 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2751 | if (!mDragState->isStartDrag) { |
| 2752 | mDragState->isStartDrag = true; |
| 2753 | mDragState->isStylusButtonDownAtStart = |
| 2754 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2755 | } |
| 2756 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2757 | // Find the pointer index by id. |
| 2758 | int32_t pointerIndex = 0; |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 2759 | for (; static_cast<uint32_t>(pointerIndex) < entry.getPointerCount(); pointerIndex++) { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2760 | const PointerProperties& pointerProperties = entry.pointerProperties[pointerIndex]; |
| 2761 | if (pointerProperties.id == mDragState->pointerId) { |
| 2762 | break; |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2763 | } |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2764 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2765 | |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 2766 | if (uint32_t(pointerIndex) == entry.getPointerCount()) { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2767 | 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] | 2768 | } |
| 2769 | |
| 2770 | const int32_t maskedAction = entry.action & AMOTION_EVENT_ACTION_MASK; |
| 2771 | const int32_t x = entry.pointerCoords[pointerIndex].getX(); |
| 2772 | const int32_t y = entry.pointerCoords[pointerIndex].getY(); |
| 2773 | |
| 2774 | switch (maskedAction) { |
| 2775 | case AMOTION_EVENT_ACTION_MOVE: { |
| 2776 | // Handle the special case : stylus button no longer pressed. |
| 2777 | bool isStylusButtonDown = |
| 2778 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2779 | if (mDragState->isStylusButtonDownAtStart && !isStylusButtonDown) { |
| 2780 | finishDragAndDrop(entry.displayId, x, y); |
| 2781 | return; |
| 2782 | } |
| 2783 | |
| 2784 | // Prevent stylus interceptor windows from affecting drag and drop behavior for now, |
| 2785 | // until we have an explicit reason to support it. |
| 2786 | constexpr bool isStylus = false; |
| 2787 | |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 2788 | sp<WindowInfoHandle> hoverWindowHandle = |
| 2789 | findTouchedWindowAtLocked(entry.displayId, x, y, isStylus, |
| 2790 | /*ignoreDragWindow=*/true); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2791 | // enqueue drag exit if needed. |
| 2792 | if (hoverWindowHandle != mDragState->dragHoverWindowHandle && |
| 2793 | !haveSameToken(hoverWindowHandle, mDragState->dragHoverWindowHandle)) { |
| 2794 | if (mDragState->dragHoverWindowHandle != nullptr) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2795 | enqueueDragEventLocked(mDragState->dragHoverWindowHandle, /*isExiting=*/true, x, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2796 | y); |
| 2797 | } |
| 2798 | mDragState->dragHoverWindowHandle = hoverWindowHandle; |
| 2799 | } |
| 2800 | // enqueue drag location if needed. |
| 2801 | if (hoverWindowHandle != nullptr) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2802 | enqueueDragEventLocked(hoverWindowHandle, /*isExiting=*/false, x, y); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2803 | } |
| 2804 | break; |
| 2805 | } |
| 2806 | |
| 2807 | case AMOTION_EVENT_ACTION_POINTER_UP: |
Siarhei Vishniakou | 5b9766d | 2023-07-18 14:06:29 -0700 | [diff] [blame] | 2808 | if (MotionEvent::getActionIndex(entry.action) != pointerIndex) { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2809 | break; |
| 2810 | } |
| 2811 | // The drag pointer is up. |
| 2812 | [[fallthrough]]; |
| 2813 | case AMOTION_EVENT_ACTION_UP: |
| 2814 | finishDragAndDrop(entry.displayId, x, y); |
| 2815 | break; |
| 2816 | case AMOTION_EVENT_ACTION_CANCEL: { |
| 2817 | ALOGD("Receiving cancel when drag and drop."); |
| 2818 | sendDropWindowCommandLocked(nullptr, 0, 0); |
| 2819 | mDragState.reset(); |
| 2820 | break; |
| 2821 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2822 | } |
| 2823 | } |
| 2824 | |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2825 | std::optional<InputTarget> InputDispatcher::createInputTargetLocked( |
| 2826 | const sp<android::gui::WindowInfoHandle>& windowHandle, |
| 2827 | ftl::Flags<InputTarget::Flags> targetFlags, |
| 2828 | std::optional<nsecs_t> firstDownTimeInTarget) const { |
| 2829 | std::shared_ptr<InputChannel> inputChannel = getInputChannelLocked(windowHandle->getToken()); |
| 2830 | if (inputChannel == nullptr) { |
| 2831 | ALOGW("Not creating InputTarget for %s, no input channel", windowHandle->getName().c_str()); |
| 2832 | return {}; |
| 2833 | } |
| 2834 | InputTarget inputTarget; |
| 2835 | inputTarget.inputChannel = inputChannel; |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 2836 | inputTarget.windowHandle = windowHandle; |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2837 | inputTarget.flags = targetFlags; |
| 2838 | inputTarget.globalScaleFactor = windowHandle->getInfo()->globalScaleFactor; |
| 2839 | inputTarget.firstDownTimeInTarget = firstDownTimeInTarget; |
| 2840 | const auto& displayInfoIt = mDisplayInfos.find(windowHandle->getInfo()->displayId); |
| 2841 | if (displayInfoIt != mDisplayInfos.end()) { |
| 2842 | inputTarget.displayTransform = displayInfoIt->second.transform; |
| 2843 | } else { |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 2844 | // DisplayInfo not found for this window on display windowHandle->getInfo()->displayId. |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2845 | // TODO(b/198444055): Make this an error message after 'setInputWindows' API is removed. |
| 2846 | } |
| 2847 | return inputTarget; |
| 2848 | } |
| 2849 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2850 | void InputDispatcher::addWindowTargetLocked(const sp<WindowInfoHandle>& windowHandle, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2851 | ftl::Flags<InputTarget::Flags> targetFlags, |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2852 | std::optional<nsecs_t> firstDownTimeInTarget, |
Siarhei Vishniakou | f75cddb | 2022-10-25 10:42:16 -0700 | [diff] [blame] | 2853 | std::vector<InputTarget>& inputTargets) const { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2854 | std::vector<InputTarget>::iterator it = |
| 2855 | std::find_if(inputTargets.begin(), inputTargets.end(), |
| 2856 | [&windowHandle](const InputTarget& inputTarget) { |
| 2857 | return inputTarget.inputChannel->getConnectionToken() == |
| 2858 | windowHandle->getToken(); |
| 2859 | }); |
Chavi Weingarten | 97b8eec | 2020-01-09 18:09:08 +0000 | [diff] [blame] | 2860 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2861 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2862 | |
| 2863 | if (it == inputTargets.end()) { |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2864 | std::optional<InputTarget> target = |
| 2865 | createInputTargetLocked(windowHandle, targetFlags, firstDownTimeInTarget); |
| 2866 | if (!target) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2867 | return; |
| 2868 | } |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2869 | inputTargets.push_back(*target); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2870 | it = inputTargets.end() - 1; |
| 2871 | } |
| 2872 | |
Siarhei Vishniakou | 23d73fb | 2023-10-29 13:27:46 -0700 | [diff] [blame] | 2873 | if (it->flags != targetFlags) { |
| 2874 | LOG(ERROR) << "Flags don't match! targetFlags=" << targetFlags.string() << ", it=" << *it; |
| 2875 | } |
| 2876 | if (it->globalScaleFactor != windowInfo->globalScaleFactor) { |
| 2877 | LOG(ERROR) << "Mismatch! it->globalScaleFactor=" << it->globalScaleFactor |
| 2878 | << ", windowInfo->globalScaleFactor=" << windowInfo->globalScaleFactor; |
| 2879 | } |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2880 | } |
| 2881 | |
| 2882 | void InputDispatcher::addPointerWindowTargetLocked( |
| 2883 | const sp<android::gui::WindowInfoHandle>& windowHandle, |
| 2884 | ftl::Flags<InputTarget::Flags> targetFlags, std::bitset<MAX_POINTER_ID + 1> pointerIds, |
| 2885 | std::optional<nsecs_t> firstDownTimeInTarget, std::vector<InputTarget>& inputTargets) const |
| 2886 | REQUIRES(mLock) { |
| 2887 | if (pointerIds.none()) { |
| 2888 | for (const auto& target : inputTargets) { |
| 2889 | LOG(INFO) << "Target: " << target; |
| 2890 | } |
| 2891 | LOG(FATAL) << "No pointers specified for " << windowHandle->getName(); |
| 2892 | return; |
| 2893 | } |
| 2894 | std::vector<InputTarget>::iterator it = |
| 2895 | std::find_if(inputTargets.begin(), inputTargets.end(), |
| 2896 | [&windowHandle](const InputTarget& inputTarget) { |
| 2897 | return inputTarget.inputChannel->getConnectionToken() == |
| 2898 | windowHandle->getToken(); |
| 2899 | }); |
| 2900 | |
| 2901 | // This is a hack, because the actual entry could potentially be an ACTION_DOWN event that |
| 2902 | // causes a HOVER_EXIT to be generated. That means that the same entry of ACTION_DOWN would |
| 2903 | // have DISPATCH_AS_HOVER_EXIT and DISPATCH_AS_IS. And therefore, we have to create separate |
| 2904 | // input targets for hovering pointers and for touching pointers. |
| 2905 | // If we picked an existing input target above, but it's for HOVER_EXIT - let's use a new |
| 2906 | // target instead. |
| 2907 | if (it != inputTargets.end() && it->flags.test(InputTarget::Flags::DISPATCH_AS_HOVER_EXIT)) { |
| 2908 | // Force the code below to create a new input target |
| 2909 | it = inputTargets.end(); |
| 2910 | } |
| 2911 | |
| 2912 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 2913 | |
| 2914 | if (it == inputTargets.end()) { |
| 2915 | std::optional<InputTarget> target = |
| 2916 | createInputTargetLocked(windowHandle, targetFlags, firstDownTimeInTarget); |
| 2917 | if (!target) { |
| 2918 | return; |
| 2919 | } |
| 2920 | inputTargets.push_back(*target); |
| 2921 | it = inputTargets.end() - 1; |
| 2922 | } |
| 2923 | |
Siarhei Vishniakou | 4bd0b7c | 2023-10-27 00:51:14 -0700 | [diff] [blame] | 2924 | if (it->flags != targetFlags) { |
Siarhei Vishniakou | 23d73fb | 2023-10-29 13:27:46 -0700 | [diff] [blame] | 2925 | LOG(ERROR) << "Flags don't match! targetFlags=" << targetFlags.string() << ", it=" << *it; |
Siarhei Vishniakou | 4bd0b7c | 2023-10-27 00:51:14 -0700 | [diff] [blame] | 2926 | } |
Siarhei Vishniakou | 23d73fb | 2023-10-29 13:27:46 -0700 | [diff] [blame] | 2927 | if (it->globalScaleFactor != windowInfo->globalScaleFactor) { |
| 2928 | LOG(ERROR) << "Mismatch! it->globalScaleFactor=" << it->globalScaleFactor |
| 2929 | << ", windowInfo->globalScaleFactor=" << windowInfo->globalScaleFactor; |
| 2930 | } |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2931 | |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2932 | it->addPointers(pointerIds, windowInfo->transform); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2933 | } |
| 2934 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2935 | void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2936 | int32_t displayId) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2937 | auto monitorsIt = mGlobalMonitorsByDisplay.find(displayId); |
| 2938 | if (monitorsIt == mGlobalMonitorsByDisplay.end()) return; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2939 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2940 | for (const Monitor& monitor : selectResponsiveMonitorsLocked(monitorsIt->second)) { |
| 2941 | InputTarget target; |
| 2942 | target.inputChannel = monitor.inputChannel; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2943 | target.flags = InputTarget::Flags::DISPATCH_AS_IS; |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2944 | // target.firstDownTimeInTarget is not set for global monitors. It is only required in split |
| 2945 | // touch and global monitoring works as intended even without setting firstDownTimeInTarget |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2946 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
| 2947 | target.displayTransform = it->second.transform; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2948 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2949 | target.setDefaultPointerTransform(target.displayTransform); |
| 2950 | inputTargets.push_back(target); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2951 | } |
| 2952 | } |
| 2953 | |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2954 | /** |
| 2955 | * Indicate whether one window handle should be considered as obscuring |
| 2956 | * another window handle. We only check a few preconditions. Actually |
| 2957 | * checking the bounds is left to the caller. |
| 2958 | */ |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2959 | static bool canBeObscuredBy(const sp<WindowInfoHandle>& windowHandle, |
| 2960 | const sp<WindowInfoHandle>& otherHandle) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2961 | // Compare by token so cloned layers aren't counted |
| 2962 | if (haveSameToken(windowHandle, otherHandle)) { |
| 2963 | return false; |
| 2964 | } |
| 2965 | auto info = windowHandle->getInfo(); |
| 2966 | auto otherInfo = otherHandle->getInfo(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2967 | if (otherInfo->inputConfig.test(WindowInfo::InputConfig::NOT_VISIBLE)) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2968 | return false; |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2969 | } else if (otherInfo->alpha == 0 && |
| 2970 | otherInfo->inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE)) { |
Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame] | 2971 | // Those act as if they were invisible, so we don't need to flag them. |
| 2972 | // We do want to potentially flag touchable windows even if they have 0 |
| 2973 | // opacity, since they can consume touches and alter the effects of the |
| 2974 | // user interaction (eg. apps that rely on |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2975 | // Flags::WINDOW_IS_PARTIALLY_OBSCURED should still be told about those |
Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame] | 2976 | // windows), hence we also check for FLAG_NOT_TOUCHABLE. |
| 2977 | return false; |
Bernardo Rufino | 8007daf | 2020-09-22 09:40:01 +0000 | [diff] [blame] | 2978 | } else if (info->ownerUid == otherInfo->ownerUid) { |
| 2979 | // If ownerUid is the same we don't generate occlusion events as there |
| 2980 | // is no security boundary within an uid. |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2981 | return false; |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2982 | } else if (otherInfo->inputConfig.test(gui::WindowInfo::InputConfig::TRUSTED_OVERLAY)) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2983 | return false; |
| 2984 | } else if (otherInfo->displayId != info->displayId) { |
| 2985 | return false; |
| 2986 | } |
| 2987 | return true; |
| 2988 | } |
| 2989 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2990 | /** |
| 2991 | * Returns touch occlusion information in the form of TouchOcclusionInfo. To check if the touch is |
| 2992 | * untrusted, one should check: |
| 2993 | * |
| 2994 | * 1. If result.hasBlockingOcclusion is true. |
| 2995 | * If it's, it means the touch should be blocked due to a window with occlusion mode of |
| 2996 | * BLOCK_UNTRUSTED. |
| 2997 | * |
| 2998 | * 2. If result.obscuringOpacity > mMaximumObscuringOpacityForTouch. |
| 2999 | * If it is (and 1 is false), then the touch should be blocked because a stack of windows |
| 3000 | * (possibly only one) with occlusion mode of USE_OPACITY from one UID resulted in a composed |
| 3001 | * obscuring opacity above the threshold. Note that if there was no window of occlusion mode |
| 3002 | * USE_OPACITY, result.obscuringOpacity would've been 0 and since |
| 3003 | * mMaximumObscuringOpacityForTouch >= 0, the condition above would never be true. |
| 3004 | * |
| 3005 | * If neither of those is true, then it means the touch can be allowed. |
| 3006 | */ |
| 3007 | InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3008 | const sp<WindowInfoHandle>& windowHandle, int32_t x, int32_t y) const { |
| 3009 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 3010 | int32_t displayId = windowInfo->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3011 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3012 | TouchOcclusionInfo info; |
| 3013 | info.hasBlockingOcclusion = false; |
| 3014 | info.obscuringOpacity = 0; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3015 | info.obscuringUid = gui::Uid::INVALID; |
| 3016 | std::map<gui::Uid, float> opacityByUid; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3017 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3018 | if (windowHandle == otherHandle) { |
| 3019 | break; // All future windows are below us. Exit early. |
| 3020 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3021 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 3022 | if (canBeObscuredBy(windowHandle, otherHandle) && otherInfo->frameContainsPoint(x, y) && |
| 3023 | !haveSameApplicationToken(windowInfo, otherInfo)) { |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 3024 | if (DEBUG_TOUCH_OCCLUSION) { |
| 3025 | info.debugInfo.push_back( |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 3026 | dumpWindowForTouchOcclusion(otherInfo, /*isTouchedWindow=*/false)); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 3027 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3028 | // canBeObscuredBy() has returned true above, which means this window is untrusted, so |
| 3029 | // we perform the checks below to see if the touch can be propagated or not based on the |
| 3030 | // window's touch occlusion mode |
| 3031 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::BLOCK_UNTRUSTED) { |
| 3032 | info.hasBlockingOcclusion = true; |
| 3033 | info.obscuringUid = otherInfo->ownerUid; |
| 3034 | info.obscuringPackage = otherInfo->packageName; |
| 3035 | break; |
| 3036 | } |
| 3037 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::USE_OPACITY) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3038 | const auto uid = otherInfo->ownerUid; |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3039 | float opacity = |
| 3040 | (opacityByUid.find(uid) == opacityByUid.end()) ? 0 : opacityByUid[uid]; |
| 3041 | // Given windows A and B: |
| 3042 | // opacity(A, B) = 1 - [1 - opacity(A)] * [1 - opacity(B)] |
| 3043 | opacity = 1 - (1 - opacity) * (1 - otherInfo->alpha); |
| 3044 | opacityByUid[uid] = opacity; |
| 3045 | if (opacity > info.obscuringOpacity) { |
| 3046 | info.obscuringOpacity = opacity; |
| 3047 | info.obscuringUid = uid; |
| 3048 | info.obscuringPackage = otherInfo->packageName; |
| 3049 | } |
| 3050 | } |
| 3051 | } |
| 3052 | } |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 3053 | if (DEBUG_TOUCH_OCCLUSION) { |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 3054 | info.debugInfo.push_back(dumpWindowForTouchOcclusion(windowInfo, /*isTouchedWindow=*/true)); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 3055 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3056 | return info; |
| 3057 | } |
| 3058 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3059 | std::string InputDispatcher::dumpWindowForTouchOcclusion(const WindowInfo* info, |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 3060 | bool isTouchedWindow) const { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3061 | return StringPrintf(INDENT2 "* %spackage=%s/%s, id=%" PRId32 ", mode=%s, alpha=%.2f, " |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 3062 | "frame=[%" PRId32 ",%" PRId32 "][%" PRId32 ",%" PRId32 |
| 3063 | "], touchableRegion=%s, window={%s}, inputConfig={%s}, " |
| 3064 | "hasToken=%s, applicationInfo.name=%s, applicationInfo.token=%s\n", |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 3065 | isTouchedWindow ? "[TOUCHED] " : "", info->packageName.c_str(), |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3066 | info->ownerUid.toString().c_str(), info->id, |
Chavi Weingarten | 7f01919 | 2023-08-08 20:39:01 +0000 | [diff] [blame] | 3067 | toString(info->touchOcclusionMode).c_str(), info->alpha, info->frame.left, |
| 3068 | info->frame.top, info->frame.right, info->frame.bottom, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3069 | dumpRegion(info->touchableRegion).c_str(), info->name.c_str(), |
| 3070 | info->inputConfig.string().c_str(), toString(info->token != nullptr), |
| 3071 | info->applicationInfo.name.c_str(), |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 3072 | binderToString(info->applicationInfo.token).c_str()); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 3073 | } |
| 3074 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3075 | bool InputDispatcher::isTouchTrustedLocked(const TouchOcclusionInfo& occlusionInfo) const { |
| 3076 | if (occlusionInfo.hasBlockingOcclusion) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3077 | ALOGW("Untrusted touch due to occlusion by %s/%s", occlusionInfo.obscuringPackage.c_str(), |
| 3078 | occlusionInfo.obscuringUid.toString().c_str()); |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3079 | return false; |
| 3080 | } |
| 3081 | if (occlusionInfo.obscuringOpacity > mMaximumObscuringOpacityForTouch) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3082 | ALOGW("Untrusted touch due to occlusion by %s/%s (obscuring opacity = " |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3083 | "%.2f, maximum allowed = %.2f)", |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3084 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid.toString().c_str(), |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3085 | occlusionInfo.obscuringOpacity, mMaximumObscuringOpacityForTouch); |
| 3086 | return false; |
| 3087 | } |
| 3088 | return true; |
| 3089 | } |
| 3090 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3091 | bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3092 | int32_t x, int32_t y) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3093 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3094 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 3095 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3096 | if (windowHandle == otherHandle) { |
| 3097 | break; // All future windows are below us. Exit early. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3098 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3099 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3100 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 3101 | otherInfo->frameContainsPoint(x, y)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3102 | return true; |
| 3103 | } |
| 3104 | } |
| 3105 | return false; |
| 3106 | } |
| 3107 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3108 | bool InputDispatcher::isWindowObscuredLocked(const sp<WindowInfoHandle>& windowHandle) const { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 3109 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3110 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 3111 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 3112 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3113 | if (windowHandle == otherHandle) { |
| 3114 | break; // All future windows are below us. Exit early. |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 3115 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3116 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3117 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 3118 | otherInfo->overlaps(windowInfo)) { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 3119 | return true; |
| 3120 | } |
| 3121 | } |
| 3122 | return false; |
| 3123 | } |
| 3124 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 3125 | std::string InputDispatcher::getApplicationWindowLabel( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3126 | const InputApplicationHandle* applicationHandle, const sp<WindowInfoHandle>& windowHandle) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3127 | if (applicationHandle != nullptr) { |
| 3128 | if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 3129 | return applicationHandle->getName() + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3130 | } else { |
| 3131 | return applicationHandle->getName(); |
| 3132 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3133 | } else if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 3134 | return windowHandle->getInfo()->applicationInfo.name + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3135 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3136 | return "<unknown application or window>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3137 | } |
| 3138 | } |
| 3139 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3140 | void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3141 | if (!isUserActivityEvent(eventEntry)) { |
| 3142 | // 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] | 3143 | return; |
| 3144 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3145 | int32_t displayId = getTargetDisplayId(eventEntry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3146 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 3147 | const WindowInfo* windowDisablingUserActivityInfo = nullptr; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3148 | if (focusedWindowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3149 | const WindowInfo* info = focusedWindowHandle->getInfo(); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 3150 | if (info->inputConfig.test(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY)) { |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 3151 | windowDisablingUserActivityInfo = info; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3152 | } |
| 3153 | } |
| 3154 | |
| 3155 | int32_t eventType = USER_ACTIVITY_EVENT_OTHER; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3156 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3157 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3158 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 3159 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3160 | return; |
| 3161 | } |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 3162 | if (windowDisablingUserActivityInfo != nullptr) { |
| 3163 | if (DEBUG_DISPATCH_CYCLE) { |
| 3164 | ALOGD("Not poking user activity: disabled by window '%s'.", |
| 3165 | windowDisablingUserActivityInfo->name.c_str()); |
| 3166 | } |
| 3167 | return; |
| 3168 | } |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3169 | if (MotionEvent::isTouchEvent(motionEntry.source, motionEntry.action)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3170 | eventType = USER_ACTIVITY_EVENT_TOUCH; |
| 3171 | } |
| 3172 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3173 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3174 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3175 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 3176 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3177 | return; |
| 3178 | } |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 3179 | // If the key code is unknown, we don't consider it user activity |
| 3180 | if (keyEntry.keyCode == AKEYCODE_UNKNOWN) { |
| 3181 | return; |
| 3182 | } |
| 3183 | // Don't inhibit events that were intercepted or are not passed to |
| 3184 | // the apps, like system shortcuts |
| 3185 | if (windowDisablingUserActivityInfo != nullptr && |
| 3186 | keyEntry.interceptKeyResult != KeyEntry::InterceptKeyResult::SKIP && |
| 3187 | keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER) { |
| 3188 | if (DEBUG_DISPATCH_CYCLE) { |
| 3189 | ALOGD("Not poking user activity: disabled by window '%s'.", |
| 3190 | windowDisablingUserActivityInfo->name.c_str()); |
| 3191 | } |
| 3192 | return; |
| 3193 | } |
| 3194 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3195 | eventType = USER_ACTIVITY_EVENT_BUTTON; |
| 3196 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3197 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3198 | default: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3199 | LOG_ALWAYS_FATAL("%s events are not user activity", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3200 | ftl::enum_string(eventEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3201 | break; |
| 3202 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3203 | } |
| 3204 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3205 | auto command = [this, eventTime = eventEntry.eventTime, eventType, displayId]() |
| 3206 | REQUIRES(mLock) { |
| 3207 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 3208 | mPolicy.pokeUserActivity(eventTime, eventType, displayId); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3209 | }; |
| 3210 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3211 | } |
| 3212 | |
| 3213 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3214 | const std::shared_ptr<Connection>& connection, |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 3215 | std::shared_ptr<const EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3216 | const InputTarget& inputTarget) { |
Prabir Pradhan | 2dac8b8 | 2023-09-06 01:11:51 +0000 | [diff] [blame] | 3217 | ATRACE_NAME_IF(ATRACE_ENABLED(), |
| 3218 | StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
| 3219 | connection->getInputChannelName().c_str(), eventEntry->id)); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3220 | if (DEBUG_DISPATCH_CYCLE) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3221 | ALOGD("channel '%s' ~ prepareDispatchCycle - flags=%s, " |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 3222 | "globalScaleFactor=%f, pointerIds=%s %s", |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3223 | connection->getInputChannelName().c_str(), inputTarget.flags.string().c_str(), |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 3224 | inputTarget.globalScaleFactor, bitsetToString(inputTarget.pointerIds).c_str(), |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3225 | inputTarget.getPointerInfoString().c_str()); |
| 3226 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3227 | |
| 3228 | // Skip this event if the connection status is not normal. |
| 3229 | // 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] | 3230 | if (connection->status != Connection::Status::NORMAL) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3231 | if (DEBUG_DISPATCH_CYCLE) { |
| 3232 | ALOGD("channel '%s' ~ Dropping event because the channel status is %s", |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3233 | connection->getInputChannelName().c_str(), |
| 3234 | ftl::enum_string(connection->status).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3235 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3236 | return; |
| 3237 | } |
| 3238 | |
| 3239 | // Split a motion event if needed. |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3240 | if (inputTarget.flags.test(InputTarget::Flags::SPLIT)) { |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3241 | LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3242 | "Entry type %s should not have Flags::SPLIT", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3243 | ftl::enum_string(eventEntry->type).c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3244 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3245 | const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry); |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 3246 | if (inputTarget.pointerIds.count() != originalMotionEntry.getPointerCount()) { |
Siarhei Vishniakou | 16e4fa0 | 2023-02-16 17:48:56 -0800 | [diff] [blame] | 3247 | if (!inputTarget.firstDownTimeInTarget.has_value()) { |
| 3248 | logDispatchStateLocked(); |
| 3249 | LOG(FATAL) << "Splitting motion events requires a down time to be set for the " |
| 3250 | "target on connection " |
| 3251 | << connection->getInputChannelName() << " for " |
| 3252 | << originalMotionEntry.getDescription(); |
| 3253 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3254 | std::unique_ptr<MotionEntry> splitMotionEntry = |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3255 | splitMotionEvent(originalMotionEntry, inputTarget.pointerIds, |
| 3256 | inputTarget.firstDownTimeInTarget.value()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3257 | if (!splitMotionEntry) { |
| 3258 | return; // split event was dropped |
| 3259 | } |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3260 | if (splitMotionEntry->action == AMOTION_EVENT_ACTION_CANCEL) { |
| 3261 | std::string reason = std::string("reason=pointer cancel on split window"); |
| 3262 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 3263 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 3264 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3265 | if (DEBUG_FOCUS) { |
| 3266 | ALOGD("channel '%s' ~ Split motion event.", |
| 3267 | connection->getInputChannelName().c_str()); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3268 | logOutboundMotionDetails(" ", *splitMotionEntry); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3269 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3270 | enqueueDispatchEntriesLocked(currentTime, connection, std::move(splitMotionEntry), |
| 3271 | inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3272 | return; |
| 3273 | } |
| 3274 | } |
| 3275 | |
| 3276 | // Not splitting. Enqueue dispatch entries for the event as is. |
| 3277 | enqueueDispatchEntriesLocked(currentTime, connection, eventEntry, inputTarget); |
| 3278 | } |
| 3279 | |
| 3280 | void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3281 | const std::shared_ptr<Connection>& connection, |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 3282 | std::shared_ptr<const EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3283 | const InputTarget& inputTarget) { |
Prabir Pradhan | 2dac8b8 | 2023-09-06 01:11:51 +0000 | [diff] [blame] | 3284 | ATRACE_NAME_IF(ATRACE_ENABLED(), |
| 3285 | StringPrintf("enqueueDispatchEntriesLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
| 3286 | connection->getInputChannelName().c_str(), eventEntry->id)); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3287 | LOG_ALWAYS_FATAL_IF(!inputTarget.flags.any(InputTarget::DISPATCH_MASK), |
| 3288 | "No dispatch flags are set for %s", eventEntry->getDescription().c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3289 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 3290 | const bool wasEmpty = connection->outboundQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3291 | |
| 3292 | // Enqueue dispatch entries for the requested modes. |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3293 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3294 | InputTarget::Flags::DISPATCH_AS_HOVER_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3295 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3296 | InputTarget::Flags::DISPATCH_AS_OUTSIDE); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3297 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3298 | InputTarget::Flags::DISPATCH_AS_HOVER_ENTER); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3299 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3300 | InputTarget::Flags::DISPATCH_AS_IS); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3301 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3302 | InputTarget::Flags::DISPATCH_AS_SLIPPERY_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3303 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3304 | InputTarget::Flags::DISPATCH_AS_SLIPPERY_ENTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3305 | |
| 3306 | // If the outbound queue was previously empty, start the dispatch cycle going. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3307 | if (wasEmpty && !connection->outboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3308 | startDispatchCycleLocked(currentTime, connection); |
| 3309 | } |
| 3310 | } |
| 3311 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3312 | void InputDispatcher::enqueueDispatchEntryLocked(const std::shared_ptr<Connection>& connection, |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 3313 | std::shared_ptr<const EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3314 | const InputTarget& inputTarget, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3315 | ftl::Flags<InputTarget::Flags> dispatchMode) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3316 | ftl::Flags<InputTarget::Flags> inputTargetFlags = inputTarget.flags; |
| 3317 | if (!inputTargetFlags.any(dispatchMode)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3318 | return; |
| 3319 | } |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3320 | |
| 3321 | inputTargetFlags.clear(InputTarget::DISPATCH_MASK); |
| 3322 | inputTargetFlags |= dispatchMode; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3323 | |
| 3324 | // This is a new event. |
| 3325 | // Enqueue a new dispatch entry onto the outbound queue for this connection. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3326 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3327 | createDispatchEntry(inputTarget, eventEntry, inputTargetFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3328 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3329 | // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a |
| 3330 | // different EventEntry than what was passed in. |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame^] | 3331 | eventEntry = dispatchEntry->eventEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3332 | // Apply target flags and update the connection's input state. |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame^] | 3333 | switch (eventEntry->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3334 | case EventEntry::Type::KEY: { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame^] | 3335 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(*eventEntry); |
| 3336 | if (!connection->inputState.trackKey(keyEntry, keyEntry.flags)) { |
Siarhei Vishniakou | c94dafe | 2023-05-26 10:24:19 -0700 | [diff] [blame] | 3337 | LOG(WARNING) << "channel " << connection->getInputChannelName() |
| 3338 | << "~ dropping inconsistent event: " << *dispatchEntry; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3339 | return; // skip the inconsistent event |
| 3340 | } |
| 3341 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3342 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3343 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3344 | case EventEntry::Type::MOTION: { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame^] | 3345 | std::shared_ptr<const MotionEntry> resolvedMotion = |
| 3346 | std::static_pointer_cast<const MotionEntry>(eventEntry); |
| 3347 | { |
| 3348 | // Determine the resolved motion entry. |
| 3349 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); |
| 3350 | int32_t resolvedAction = motionEntry.action; |
| 3351 | int32_t resolvedFlags = motionEntry.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3352 | |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame^] | 3353 | if (dispatchMode.test(InputTarget::Flags::DISPATCH_AS_OUTSIDE)) { |
| 3354 | resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; |
| 3355 | } else if (dispatchMode.test(InputTarget::Flags::DISPATCH_AS_HOVER_EXIT)) { |
| 3356 | resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; |
| 3357 | } else if (dispatchMode.test(InputTarget::Flags::DISPATCH_AS_HOVER_ENTER)) { |
| 3358 | resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 3359 | } else if (dispatchMode.test(InputTarget::Flags::DISPATCH_AS_SLIPPERY_EXIT)) { |
| 3360 | resolvedAction = AMOTION_EVENT_ACTION_CANCEL; |
| 3361 | } else if (dispatchMode.test(InputTarget::Flags::DISPATCH_AS_SLIPPERY_ENTER)) { |
| 3362 | resolvedAction = AMOTION_EVENT_ACTION_DOWN; |
| 3363 | } |
| 3364 | if (resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE && |
| 3365 | !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source, |
| 3366 | motionEntry.displayId)) { |
| 3367 | if (DEBUG_DISPATCH_CYCLE) { |
| 3368 | LOG(DEBUG) << "channel '" << connection->getInputChannelName().c_str() |
| 3369 | << "' ~ enqueueDispatchEntryLocked: filling in missing hover " |
| 3370 | "enter event"; |
| 3371 | } |
| 3372 | resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 3373 | } |
| 3374 | |
| 3375 | if (resolvedAction == AMOTION_EVENT_ACTION_CANCEL) { |
| 3376 | resolvedFlags |= AMOTION_EVENT_FLAG_CANCELED; |
| 3377 | } |
| 3378 | if (dispatchEntry->targetFlags.test(InputTarget::Flags::WINDOW_IS_OBSCURED)) { |
| 3379 | resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; |
| 3380 | } |
| 3381 | if (dispatchEntry->targetFlags.test( |
| 3382 | InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED)) { |
| 3383 | resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 3384 | } |
| 3385 | |
| 3386 | dispatchEntry->resolvedFlags = resolvedFlags; |
| 3387 | if (resolvedAction != motionEntry.action) { |
| 3388 | // Generate a new MotionEntry with a new eventId using the resolved action and |
| 3389 | // flags. |
| 3390 | resolvedMotion = |
| 3391 | std::make_shared<MotionEntry>(mIdGenerator.nextId(), |
| 3392 | motionEntry.injectionState, |
| 3393 | motionEntry.eventTime, |
| 3394 | motionEntry.deviceId, motionEntry.source, |
| 3395 | motionEntry.displayId, |
| 3396 | motionEntry.policyFlags, resolvedAction, |
| 3397 | motionEntry.actionButton, resolvedFlags, |
| 3398 | motionEntry.metaState, |
| 3399 | motionEntry.buttonState, |
| 3400 | motionEntry.classification, |
| 3401 | motionEntry.edgeFlags, |
| 3402 | motionEntry.xPrecision, |
| 3403 | motionEntry.yPrecision, |
| 3404 | motionEntry.xCursorPosition, |
| 3405 | motionEntry.yCursorPosition, |
| 3406 | motionEntry.downTime, |
| 3407 | motionEntry.pointerProperties, |
| 3408 | motionEntry.pointerCoords); |
| 3409 | if (ATRACE_ENABLED()) { |
| 3410 | std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32 |
| 3411 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3412 | motionEntry.id, resolvedMotion->id); |
| 3413 | ATRACE_NAME(message.c_str()); |
| 3414 | } |
| 3415 | |
| 3416 | // Set the resolved motion entry in the DispatchEntry. |
| 3417 | dispatchEntry->eventEntry = resolvedMotion; |
| 3418 | eventEntry = resolvedMotion; |
| 3419 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3420 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3421 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3422 | // Check if we need to cancel any of the ongoing gestures. We don't support multiple |
| 3423 | // devices being active at the same time in the same window, so if a new device is |
| 3424 | // active, cancel the gesture from the old device. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3425 | std::unique_ptr<EventEntry> cancelEvent = |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame^] | 3426 | connection->inputState.cancelConflictingInputStream(*resolvedMotion); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3427 | if (cancelEvent != nullptr) { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame^] | 3428 | LOG(INFO) << "Canceling pointers for device " << resolvedMotion->deviceId << " in " |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3429 | << connection->getInputChannelName() << " with event " |
| 3430 | << cancelEvent->getDescription(); |
| 3431 | std::unique_ptr<DispatchEntry> cancelDispatchEntry = |
| 3432 | createDispatchEntry(inputTarget, std::move(cancelEvent), |
| 3433 | InputTarget::Flags::DISPATCH_AS_IS); |
| 3434 | |
| 3435 | // Send these cancel events to the queue before sending the event from the new |
| 3436 | // device. |
| 3437 | connection->outboundQueue.emplace_back(std::move(cancelDispatchEntry)); |
| 3438 | } |
| 3439 | |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame^] | 3440 | if (!connection->inputState.trackMotion(*resolvedMotion, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3441 | dispatchEntry->resolvedFlags)) { |
Siarhei Vishniakou | c94dafe | 2023-05-26 10:24:19 -0700 | [diff] [blame] | 3442 | LOG(WARNING) << "channel " << connection->getInputChannelName() |
| 3443 | << "~ dropping inconsistent event: " << *dispatchEntry; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3444 | return; // skip the inconsistent event |
| 3445 | } |
| 3446 | |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame^] | 3447 | if ((resolvedMotion->flags & AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) && |
| 3448 | (resolvedMotion->policyFlags & POLICY_FLAG_TRUSTED)) { |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 3449 | // Skip reporting pointer down outside focus to the policy. |
| 3450 | break; |
| 3451 | } |
| 3452 | |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame^] | 3453 | dispatchPointerDownOutsideFocus(resolvedMotion->source, resolvedMotion->action, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3454 | inputTarget.inputChannel->getConnectionToken()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3455 | |
| 3456 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3457 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3458 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3459 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3460 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3461 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3462 | break; |
| 3463 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3464 | case EventEntry::Type::SENSOR: { |
| 3465 | LOG_ALWAYS_FATAL("SENSOR events should not go to apps via input channel"); |
| 3466 | break; |
| 3467 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3468 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 3469 | case EventEntry::Type::DEVICE_RESET: { |
| 3470 | LOG_ALWAYS_FATAL("%s events should not go to apps", |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame^] | 3471 | ftl::enum_string(eventEntry->type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3472 | break; |
| 3473 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3474 | } |
| 3475 | |
| 3476 | // Remember that we are waiting for this dispatch to complete. |
| 3477 | if (dispatchEntry->hasForegroundTarget()) { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame^] | 3478 | incrementPendingForegroundDispatches(*eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3479 | } |
| 3480 | |
| 3481 | // Enqueue the dispatch entry. |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 3482 | connection->outboundQueue.emplace_back(std::move(dispatchEntry)); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3483 | traceOutboundQueueLength(*connection); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3484 | } |
| 3485 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3486 | /** |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3487 | * This function is for debugging and metrics collection. It has two roles. |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3488 | * |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3489 | * The first role is to log input interaction with windows, which helps determine what the user was |
| 3490 | * interacting with. For example, if user is touching launcher, we will see an input_interaction log |
| 3491 | * that user started interacting with launcher window, as well as any other window that received |
| 3492 | * that gesture, such as the wallpaper or other spy windows. A new input_interaction is only logged |
| 3493 | * when the set of tokens that received the event changes. It is not logged again as long as the |
| 3494 | * user is interacting with the same windows. |
| 3495 | * |
| 3496 | * The second role is to track input device activity for metrics collection. For each input event, |
| 3497 | * we report the set of UIDs that the input device interacted with to the policy. Unlike for the |
| 3498 | * input_interaction logs, the device interaction is reported even when the set of interaction |
| 3499 | * tokens do not change. |
| 3500 | * |
| 3501 | * For these purposes, we do not count ACTION_OUTSIDE, ACTION_UP and ACTION_CANCEL actions as |
| 3502 | * interaction. This includes up and cancel events for both keys and motions. |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3503 | */ |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3504 | void InputDispatcher::processInteractionsLocked(const EventEntry& entry, |
| 3505 | const std::vector<InputTarget>& targets) { |
| 3506 | int32_t deviceId; |
| 3507 | nsecs_t eventTime; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3508 | // Skip ACTION_UP events, and all events other than keys and motions |
| 3509 | if (entry.type == EventEntry::Type::KEY) { |
| 3510 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 3511 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
| 3512 | return; |
| 3513 | } |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3514 | deviceId = keyEntry.deviceId; |
| 3515 | eventTime = keyEntry.eventTime; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3516 | } else if (entry.type == EventEntry::Type::MOTION) { |
| 3517 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 3518 | if (motionEntry.action == AMOTION_EVENT_ACTION_UP || |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3519 | motionEntry.action == AMOTION_EVENT_ACTION_CANCEL || |
| 3520 | MotionEvent::getActionMasked(motionEntry.action) == AMOTION_EVENT_ACTION_POINTER_UP) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3521 | return; |
| 3522 | } |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3523 | deviceId = motionEntry.deviceId; |
| 3524 | eventTime = motionEntry.eventTime; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3525 | } else { |
| 3526 | return; // Not a key or a motion |
| 3527 | } |
| 3528 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3529 | std::set<gui::Uid> interactionUids; |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 3530 | std::unordered_set<sp<IBinder>, StrongPointerHash<IBinder>> newConnectionTokens; |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3531 | std::vector<std::shared_ptr<Connection>> newConnections; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3532 | for (const InputTarget& target : targets) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3533 | if (target.flags.test(InputTarget::Flags::DISPATCH_AS_OUTSIDE)) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3534 | continue; // Skip windows that receive ACTION_OUTSIDE |
| 3535 | } |
| 3536 | |
| 3537 | sp<IBinder> token = target.inputChannel->getConnectionToken(); |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3538 | std::shared_ptr<Connection> connection = getConnectionLocked(token); |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3539 | if (connection == nullptr) { |
| 3540 | continue; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3541 | } |
| 3542 | newConnectionTokens.insert(std::move(token)); |
| 3543 | newConnections.emplace_back(connection); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3544 | if (target.windowHandle) { |
| 3545 | interactionUids.emplace(target.windowHandle->getInfo()->ownerUid); |
| 3546 | } |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3547 | } |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3548 | |
| 3549 | auto command = [this, deviceId, eventTime, uids = std::move(interactionUids)]() |
| 3550 | REQUIRES(mLock) { |
| 3551 | scoped_unlock unlock(mLock); |
| 3552 | mPolicy.notifyDeviceInteraction(deviceId, eventTime, uids); |
| 3553 | }; |
| 3554 | postCommandLocked(std::move(command)); |
| 3555 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3556 | if (newConnectionTokens == mInteractionConnectionTokens) { |
| 3557 | return; // no change |
| 3558 | } |
| 3559 | mInteractionConnectionTokens = newConnectionTokens; |
| 3560 | |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3561 | std::string targetList; |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3562 | for (const std::shared_ptr<Connection>& connection : newConnections) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3563 | targetList += connection->getWindowName() + ", "; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3564 | } |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3565 | std::string message = "Interaction with: " + targetList; |
| 3566 | if (targetList.empty()) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3567 | message += "<none>"; |
| 3568 | } |
| 3569 | android_log_event_list(LOGTAG_INPUT_INTERACTION) << message << LOG_ID_EVENTS; |
| 3570 | } |
| 3571 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3572 | void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3573 | const sp<IBinder>& token) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3574 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3575 | uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK; |
| 3576 | if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3577 | return; |
| 3578 | } |
| 3579 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 3580 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3581 | if (focusedToken == token) { |
| 3582 | // ignore since token is focused |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3583 | return; |
| 3584 | } |
| 3585 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3586 | auto command = [this, token]() REQUIRES(mLock) { |
| 3587 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 3588 | mPolicy.onPointerDownOutsideFocus(token); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3589 | }; |
| 3590 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3591 | } |
| 3592 | |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3593 | status_t InputDispatcher::publishMotionEvent(Connection& connection, |
| 3594 | DispatchEntry& dispatchEntry) const { |
| 3595 | const EventEntry& eventEntry = *(dispatchEntry.eventEntry); |
| 3596 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 3597 | |
| 3598 | PointerCoords scaledCoords[MAX_POINTERS]; |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 3599 | const PointerCoords* usingCoords = motionEntry.pointerCoords.data(); |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3600 | |
| 3601 | // Set the X and Y offset and X and Y scale depending on the input source. |
| 3602 | if ((motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) && |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3603 | !(dispatchEntry.targetFlags.test(InputTarget::Flags::ZERO_COORDS))) { |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3604 | float globalScaleFactor = dispatchEntry.globalScaleFactor; |
| 3605 | if (globalScaleFactor != 1.0f) { |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 3606 | for (uint32_t i = 0; i < motionEntry.getPointerCount(); i++) { |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3607 | scaledCoords[i] = motionEntry.pointerCoords[i]; |
| 3608 | // Don't apply window scale here since we don't want scale to affect raw |
| 3609 | // coordinates. The scale will be sent back to the client and applied |
| 3610 | // later when requesting relative coordinates. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 3611 | scaledCoords[i].scale(globalScaleFactor, /*windowXScale=*/1, /*windowYScale=*/1); |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3612 | } |
| 3613 | usingCoords = scaledCoords; |
| 3614 | } |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3615 | } else if (dispatchEntry.targetFlags.test(InputTarget::Flags::ZERO_COORDS)) { |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3616 | // We don't want the dispatch target to know the coordinates |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 3617 | for (uint32_t i = 0; i < motionEntry.getPointerCount(); i++) { |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3618 | scaledCoords[i].clear(); |
| 3619 | } |
| 3620 | usingCoords = scaledCoords; |
| 3621 | } |
| 3622 | |
| 3623 | std::array<uint8_t, 32> hmac = getSignature(motionEntry, dispatchEntry); |
| 3624 | |
| 3625 | // Publish the motion event. |
| 3626 | return connection.inputPublisher |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame^] | 3627 | .publishMotionEvent(dispatchEntry.seq, motionEntry.id, motionEntry.deviceId, |
| 3628 | motionEntry.source, motionEntry.displayId, std::move(hmac), |
| 3629 | motionEntry.action, motionEntry.actionButton, |
| 3630 | dispatchEntry.resolvedFlags, motionEntry.edgeFlags, |
| 3631 | motionEntry.metaState, motionEntry.buttonState, |
| 3632 | motionEntry.classification, dispatchEntry.transform, |
| 3633 | motionEntry.xPrecision, motionEntry.yPrecision, |
| 3634 | motionEntry.xCursorPosition, motionEntry.yCursorPosition, |
| 3635 | dispatchEntry.rawTransform, motionEntry.downTime, |
| 3636 | motionEntry.eventTime, motionEntry.getPointerCount(), |
| 3637 | motionEntry.pointerProperties.data(), usingCoords); |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3638 | } |
| 3639 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3640 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3641 | const std::shared_ptr<Connection>& connection) { |
Prabir Pradhan | 2dac8b8 | 2023-09-06 01:11:51 +0000 | [diff] [blame] | 3642 | ATRACE_NAME_IF(ATRACE_ENABLED(), |
| 3643 | StringPrintf("startDispatchCycleLocked(inputChannel=%s)", |
| 3644 | connection->getInputChannelName().c_str())); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3645 | if (DEBUG_DISPATCH_CYCLE) { |
| 3646 | ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str()); |
| 3647 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3648 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3649 | while (connection->status == Connection::Status::NORMAL && !connection->outboundQueue.empty()) { |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 3650 | std::unique_ptr<DispatchEntry>& dispatchEntry = connection->outboundQueue.front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3651 | dispatchEntry->deliveryTime = currentTime; |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3652 | const std::chrono::nanoseconds timeout = getDispatchingTimeoutLocked(connection); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3653 | dispatchEntry->timeoutTime = currentTime + timeout.count(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3654 | |
| 3655 | // Publish the event. |
| 3656 | status_t status; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3657 | const EventEntry& eventEntry = *(dispatchEntry->eventEntry); |
| 3658 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3659 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3660 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 3661 | std::array<uint8_t, 32> hmac = getSignature(keyEntry, *dispatchEntry); |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 3662 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
Siarhei Vishniakou | 827d1ac | 2023-07-21 16:37:51 -0700 | [diff] [blame] | 3663 | LOG(INFO) << "Publishing " << *dispatchEntry << " to " |
| 3664 | << connection->getInputChannelName(); |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 3665 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3666 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3667 | // Publish the key event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3668 | status = connection->inputPublisher |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame^] | 3669 | .publishKeyEvent(dispatchEntry->seq, keyEntry.id, |
| 3670 | keyEntry.deviceId, keyEntry.source, |
| 3671 | keyEntry.displayId, std::move(hmac), |
| 3672 | keyEntry.action, dispatchEntry->resolvedFlags, |
| 3673 | keyEntry.keyCode, keyEntry.scanCode, |
| 3674 | keyEntry.metaState, keyEntry.repeatCount, |
| 3675 | keyEntry.downTime, keyEntry.eventTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3676 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3677 | } |
| 3678 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3679 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 3680 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
Siarhei Vishniakou | 827d1ac | 2023-07-21 16:37:51 -0700 | [diff] [blame] | 3681 | LOG(INFO) << "Publishing " << *dispatchEntry << " to " |
| 3682 | << connection->getInputChannelName(); |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 3683 | } |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3684 | status = publishMotionEvent(*connection, *dispatchEntry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3685 | break; |
| 3686 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3687 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3688 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3689 | const FocusEntry& focusEntry = static_cast<const FocusEntry&>(eventEntry); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3690 | status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3691 | focusEntry.id, |
Antonio Kantek | 3cfec7b | 2021-11-05 18:26:17 -0700 | [diff] [blame] | 3692 | focusEntry.hasFocus); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3693 | break; |
| 3694 | } |
| 3695 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3696 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 3697 | const TouchModeEntry& touchModeEntry = |
| 3698 | static_cast<const TouchModeEntry&>(eventEntry); |
| 3699 | status = connection->inputPublisher |
| 3700 | .publishTouchModeEvent(dispatchEntry->seq, touchModeEntry.id, |
| 3701 | touchModeEntry.inTouchMode); |
| 3702 | |
| 3703 | break; |
| 3704 | } |
| 3705 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3706 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 3707 | const auto& captureEntry = |
| 3708 | static_cast<const PointerCaptureChangedEntry&>(eventEntry); |
| 3709 | status = connection->inputPublisher |
| 3710 | .publishCaptureEvent(dispatchEntry->seq, captureEntry.id, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 3711 | captureEntry.pointerCaptureRequest.enable); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3712 | break; |
| 3713 | } |
| 3714 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3715 | case EventEntry::Type::DRAG: { |
| 3716 | const DragEntry& dragEntry = static_cast<const DragEntry&>(eventEntry); |
| 3717 | status = connection->inputPublisher.publishDragEvent(dispatchEntry->seq, |
| 3718 | dragEntry.id, dragEntry.x, |
| 3719 | dragEntry.y, |
| 3720 | dragEntry.isExiting); |
| 3721 | break; |
| 3722 | } |
| 3723 | |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3724 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3725 | case EventEntry::Type::DEVICE_RESET: |
| 3726 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3727 | LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3728 | ftl::enum_string(eventEntry.type).c_str()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3729 | return; |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3730 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3731 | } |
| 3732 | |
| 3733 | // Check the result. |
| 3734 | if (status) { |
| 3735 | if (status == WOULD_BLOCK) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3736 | if (connection->waitQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3737 | ALOGE("channel '%s' ~ Could not publish event because the pipe is full. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3738 | "This is unexpected because the wait queue is empty, so the pipe " |
| 3739 | "should be empty and we shouldn't have any problems writing an " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3740 | "event to it, status=%s(%d)", |
| 3741 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3742 | status); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 3743 | abortBrokenDispatchCycleLocked(currentTime, connection, /*notify=*/true); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3744 | } else { |
| 3745 | // Pipe is full and we are waiting for the app to finish process some events |
| 3746 | // before sending more events to it. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3747 | if (DEBUG_DISPATCH_CYCLE) { |
| 3748 | ALOGD("channel '%s' ~ Could not publish event because the pipe is full, " |
| 3749 | "waiting for the application to catch up", |
| 3750 | connection->getInputChannelName().c_str()); |
| 3751 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3752 | } |
| 3753 | } else { |
| 3754 | ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3755 | "status=%s(%d)", |
| 3756 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3757 | status); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 3758 | abortBrokenDispatchCycleLocked(currentTime, connection, /*notify=*/true); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3759 | } |
| 3760 | return; |
| 3761 | } |
| 3762 | |
| 3763 | // Re-enqueue the event on the wait queue. |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 3764 | const nsecs_t timeoutTime = dispatchEntry->timeoutTime; |
| 3765 | connection->waitQueue.emplace_back(std::move(dispatchEntry)); |
| 3766 | connection->outboundQueue.erase(connection->outboundQueue.begin()); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3767 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3768 | if (connection->responsive) { |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 3769 | mAnrTracker.insert(timeoutTime, connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3770 | } |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3771 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3772 | } |
| 3773 | } |
| 3774 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3775 | std::array<uint8_t, 32> InputDispatcher::sign(const VerifiedInputEvent& event) const { |
| 3776 | size_t size; |
| 3777 | switch (event.type) { |
| 3778 | case VerifiedInputEvent::Type::KEY: { |
| 3779 | size = sizeof(VerifiedKeyEvent); |
| 3780 | break; |
| 3781 | } |
| 3782 | case VerifiedInputEvent::Type::MOTION: { |
| 3783 | size = sizeof(VerifiedMotionEvent); |
| 3784 | break; |
| 3785 | } |
| 3786 | } |
| 3787 | const uint8_t* start = reinterpret_cast<const uint8_t*>(&event); |
| 3788 | return mHmacKeyManager.sign(start, size); |
| 3789 | } |
| 3790 | |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3791 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3792 | const MotionEntry& motionEntry, const DispatchEntry& dispatchEntry) const { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame^] | 3793 | const int32_t actionMasked = MotionEvent::getActionMasked(motionEntry.action); |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3794 | if (actionMasked != AMOTION_EVENT_ACTION_UP && actionMasked != AMOTION_EVENT_ACTION_DOWN) { |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3795 | // Only sign events up and down events as the purely move events |
| 3796 | // are tied to their up/down counterparts so signing would be redundant. |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3797 | return INVALID_HMAC; |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3798 | } |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3799 | |
| 3800 | VerifiedMotionEvent verifiedEvent = |
| 3801 | verifiedMotionEventFromMotionEntry(motionEntry, dispatchEntry.rawTransform); |
| 3802 | verifiedEvent.actionMasked = actionMasked; |
| 3803 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS; |
| 3804 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3805 | } |
| 3806 | |
| 3807 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3808 | const KeyEntry& keyEntry, const DispatchEntry& dispatchEntry) const { |
| 3809 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(keyEntry); |
| 3810 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_KEY_EVENT_FLAGS; |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3811 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3812 | } |
| 3813 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3814 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3815 | const std::shared_ptr<Connection>& connection, |
| 3816 | uint32_t seq, bool handled, nsecs_t consumeTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3817 | if (DEBUG_DISPATCH_CYCLE) { |
| 3818 | ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s", |
| 3819 | connection->getInputChannelName().c_str(), seq, toString(handled)); |
| 3820 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3821 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3822 | if (connection->status == Connection::Status::BROKEN || |
| 3823 | connection->status == Connection::Status::ZOMBIE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3824 | return; |
| 3825 | } |
| 3826 | |
| 3827 | // Notify other system components and prepare to start the next dispatch cycle. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3828 | auto command = [this, currentTime, connection, seq, handled, consumeTime]() REQUIRES(mLock) { |
| 3829 | doDispatchCycleFinishedCommand(currentTime, connection, seq, handled, consumeTime); |
| 3830 | }; |
| 3831 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3832 | } |
| 3833 | |
| 3834 | void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3835 | const std::shared_ptr<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3836 | bool notify) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3837 | if (DEBUG_DISPATCH_CYCLE) { |
Siarhei Vishniakou | 827d1ac | 2023-07-21 16:37:51 -0700 | [diff] [blame] | 3838 | LOG(INFO) << "channel '" << connection->getInputChannelName() << "'~ " << __func__ |
| 3839 | << " - notify=" << toString(notify); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3840 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3841 | |
| 3842 | // Clear the dispatch queues. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3843 | drainDispatchQueue(connection->outboundQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3844 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3845 | drainDispatchQueue(connection->waitQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3846 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3847 | |
| 3848 | // The connection appears to be unrecoverably broken. |
| 3849 | // Ignore already broken or zombie connections. |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3850 | if (connection->status == Connection::Status::NORMAL) { |
| 3851 | connection->status = Connection::Status::BROKEN; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3852 | |
| 3853 | if (notify) { |
| 3854 | // Notify other system components. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3855 | ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", |
| 3856 | connection->getInputChannelName().c_str()); |
| 3857 | |
| 3858 | auto command = [this, connection]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3859 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 3860 | mPolicy.notifyInputChannelBroken(connection->inputChannel->getConnectionToken()); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3861 | }; |
| 3862 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3863 | } |
| 3864 | } |
| 3865 | } |
| 3866 | |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 3867 | void InputDispatcher::drainDispatchQueue(std::deque<std::unique_ptr<DispatchEntry>>& queue) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3868 | while (!queue.empty()) { |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 3869 | releaseDispatchEntry(std::move(queue.front())); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3870 | queue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3871 | } |
| 3872 | } |
| 3873 | |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 3874 | void InputDispatcher::releaseDispatchEntry(std::unique_ptr<DispatchEntry> dispatchEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3875 | if (dispatchEntry->hasForegroundTarget()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3876 | decrementPendingForegroundDispatches(*(dispatchEntry->eventEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3877 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3878 | } |
| 3879 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3880 | int InputDispatcher::handleReceiveCallback(int events, sp<IBinder> connectionToken) { |
| 3881 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3882 | std::shared_ptr<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3883 | if (connection == nullptr) { |
| 3884 | ALOGW("Received looper callback for unknown input channel token %p. events=0x%x", |
| 3885 | connectionToken.get(), events); |
| 3886 | return 0; // remove the callback |
| 3887 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3888 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3889 | bool notify; |
| 3890 | if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { |
| 3891 | if (!(events & ALOOPER_EVENT_INPUT)) { |
| 3892 | ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. " |
| 3893 | "events=0x%x", |
| 3894 | connection->getInputChannelName().c_str(), events); |
| 3895 | return 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3896 | } |
| 3897 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3898 | nsecs_t currentTime = now(); |
| 3899 | bool gotOne = false; |
| 3900 | status_t status = OK; |
| 3901 | for (;;) { |
| 3902 | Result<InputPublisher::ConsumerResponse> result = |
| 3903 | connection->inputPublisher.receiveConsumerResponse(); |
| 3904 | if (!result.ok()) { |
| 3905 | status = result.error().code(); |
| 3906 | break; |
| 3907 | } |
| 3908 | |
| 3909 | if (std::holds_alternative<InputPublisher::Finished>(*result)) { |
| 3910 | const InputPublisher::Finished& finish = |
| 3911 | std::get<InputPublisher::Finished>(*result); |
| 3912 | finishDispatchCycleLocked(currentTime, connection, finish.seq, finish.handled, |
| 3913 | finish.consumeTime); |
| 3914 | } else if (std::holds_alternative<InputPublisher::Timeline>(*result)) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3915 | if (shouldReportMetricsForConnection(*connection)) { |
| 3916 | const InputPublisher::Timeline& timeline = |
| 3917 | std::get<InputPublisher::Timeline>(*result); |
| 3918 | mLatencyTracker |
| 3919 | .trackGraphicsLatency(timeline.inputEventId, |
| 3920 | connection->inputChannel->getConnectionToken(), |
| 3921 | std::move(timeline.graphicsTimeline)); |
| 3922 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3923 | } |
| 3924 | gotOne = true; |
| 3925 | } |
| 3926 | if (gotOne) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3927 | runCommandsLockedInterruptable(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3928 | if (status == WOULD_BLOCK) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3929 | return 1; |
| 3930 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3931 | } |
| 3932 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3933 | notify = status != DEAD_OBJECT || !connection->monitor; |
| 3934 | if (notify) { |
| 3935 | ALOGE("channel '%s' ~ Failed to receive finished signal. status=%s(%d)", |
| 3936 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3937 | status); |
| 3938 | } |
| 3939 | } else { |
| 3940 | // Monitor channels are never explicitly unregistered. |
| 3941 | // We do it automatically when the remote endpoint is closed so don't warn about them. |
| 3942 | const bool stillHaveWindowHandle = |
| 3943 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()) != nullptr; |
| 3944 | notify = !connection->monitor && stillHaveWindowHandle; |
| 3945 | if (notify) { |
| 3946 | ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred. events=0x%x", |
| 3947 | connection->getInputChannelName().c_str(), events); |
| 3948 | } |
| 3949 | } |
| 3950 | |
| 3951 | // Remove the channel. |
| 3952 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), notify); |
| 3953 | return 0; // remove the callback |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3954 | } |
| 3955 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3956 | void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3957 | const CancelationOptions& options) { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3958 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 3959 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3960 | } |
| 3961 | } |
| 3962 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3963 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3964 | const CancelationOptions& options) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 3965 | for (const auto& [_, monitors] : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3966 | for (const Monitor& monitor : monitors) { |
| 3967 | synthesizeCancelationEventsForInputChannelLocked(monitor.inputChannel, options); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3968 | } |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3969 | } |
| 3970 | } |
| 3971 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3972 | void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked( |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 3973 | const std::shared_ptr<InputChannel>& channel, const CancelationOptions& options) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3974 | std::shared_ptr<Connection> connection = getConnectionLocked(channel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3975 | if (connection == nullptr) { |
| 3976 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3977 | } |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3978 | |
| 3979 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3980 | } |
| 3981 | |
| 3982 | void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3983 | const std::shared_ptr<Connection>& connection, const CancelationOptions& options) { |
Linnan Li | 5af92f9 | 2023-07-14 14:36:22 +0800 | [diff] [blame] | 3984 | if ((options.mode == CancelationOptions::Mode::CANCEL_POINTER_EVENTS || |
| 3985 | options.mode == CancelationOptions::Mode::CANCEL_ALL_EVENTS) && |
| 3986 | mDragState && mDragState->dragWindow->getToken() == connection->inputChannel->getToken()) { |
| 3987 | LOG(INFO) << __func__ |
| 3988 | << ": Canceling drag and drop because the pointers for the drag window are being " |
| 3989 | "canceled."; |
| 3990 | sendDropWindowCommandLocked(nullptr, /*x=*/0, /*y=*/0); |
| 3991 | mDragState.reset(); |
| 3992 | } |
| 3993 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3994 | if (connection->status == Connection::Status::BROKEN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3995 | return; |
| 3996 | } |
| 3997 | |
| 3998 | nsecs_t currentTime = now(); |
| 3999 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4000 | std::vector<std::unique_ptr<EventEntry>> cancelationEvents = |
Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 4001 | connection->inputState.synthesizeCancelationEvents(currentTime, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4002 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4003 | if (cancelationEvents.empty()) { |
| 4004 | return; |
| 4005 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4006 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 4007 | 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] | 4008 | "with reality: %s, mode=%s.", |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4009 | connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason, |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 4010 | ftl::enum_string(options.mode).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4011 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4012 | |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 4013 | std::string reason = std::string("reason=").append(options.reason); |
| 4014 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 4015 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 4016 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 4017 | const bool wasEmpty = connection->outboundQueue.empty(); |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 4018 | // The target to use if we don't find a window associated with the channel. |
| 4019 | const InputTarget fallbackTarget{.inputChannel = connection->inputChannel, |
| 4020 | .flags = InputTarget::Flags::DISPATCH_AS_IS}; |
| 4021 | const auto& token = connection->inputChannel->getConnectionToken(); |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 4022 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4023 | for (size_t i = 0; i < cancelationEvents.size(); i++) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4024 | std::unique_ptr<EventEntry> cancelationEventEntry = std::move(cancelationEvents[i]); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4025 | std::vector<InputTarget> targets{}; |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4026 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4027 | switch (cancelationEventEntry->type) { |
| 4028 | case EventEntry::Type::KEY: { |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4029 | const auto& keyEntry = static_cast<const KeyEntry&>(*cancelationEventEntry); |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 4030 | const std::optional<int32_t> targetDisplay = keyEntry.displayId != ADISPLAY_ID_NONE |
| 4031 | ? std::make_optional(keyEntry.displayId) |
| 4032 | : std::nullopt; |
| 4033 | if (const auto& window = getWindowHandleLocked(token, targetDisplay); window) { |
| 4034 | addWindowTargetLocked(window, InputTarget::Flags::DISPATCH_AS_IS, |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 4035 | keyEntry.downTime, targets); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4036 | } else { |
| 4037 | targets.emplace_back(fallbackTarget); |
| 4038 | } |
| 4039 | logOutboundKeyDetails("cancel - ", keyEntry); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4040 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4041 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4042 | case EventEntry::Type::MOTION: { |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4043 | const auto& motionEntry = static_cast<const MotionEntry&>(*cancelationEventEntry); |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 4044 | const std::optional<int32_t> targetDisplay = |
| 4045 | motionEntry.displayId != ADISPLAY_ID_NONE |
| 4046 | ? std::make_optional(motionEntry.displayId) |
| 4047 | : std::nullopt; |
| 4048 | if (const auto& window = getWindowHandleLocked(token, targetDisplay); window) { |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4049 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4050 | for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.getPointerCount(); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4051 | pointerIndex++) { |
| 4052 | pointerIds.set(motionEntry.pointerProperties[pointerIndex].id); |
| 4053 | } |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 4054 | addPointerWindowTargetLocked(window, InputTarget::Flags::DISPATCH_AS_IS, |
| 4055 | pointerIds, motionEntry.downTime, targets); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4056 | } else { |
| 4057 | targets.emplace_back(fallbackTarget); |
| 4058 | const auto it = mDisplayInfos.find(motionEntry.displayId); |
| 4059 | if (it != mDisplayInfos.end()) { |
| 4060 | targets.back().displayTransform = it->second.transform; |
| 4061 | targets.back().setDefaultPointerTransform(it->second.transform); |
| 4062 | } |
| 4063 | } |
| 4064 | logOutboundMotionDetails("cancel - ", motionEntry); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4065 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4066 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4067 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 4068 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4069 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 4070 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4071 | LOG_ALWAYS_FATAL("Canceling %s events is not supported", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 4072 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4073 | break; |
| 4074 | } |
| 4075 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4076 | case EventEntry::Type::DEVICE_RESET: |
| 4077 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4078 | 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] | 4079 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4080 | break; |
| 4081 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4082 | } |
| 4083 | |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4084 | if (targets.size() != 1) LOG(FATAL) << __func__ << ": InputTarget not created"; |
| 4085 | enqueueDispatchEntryLocked(connection, std::move(cancelationEventEntry), targets[0], |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 4086 | InputTarget::Flags::DISPATCH_AS_IS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4087 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4088 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 4089 | // If the outbound queue was previously empty, start the dispatch cycle going. |
| 4090 | if (wasEmpty && !connection->outboundQueue.empty()) { |
| 4091 | startDispatchCycleLocked(currentTime, connection); |
| 4092 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4093 | } |
| 4094 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4095 | void InputDispatcher::synthesizePointerDownEventsForConnectionLocked( |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 4096 | const nsecs_t downTime, const std::shared_ptr<Connection>& connection, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4097 | ftl::Flags<InputTarget::Flags> targetFlags) { |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 4098 | if (connection->status == Connection::Status::BROKEN) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4099 | return; |
| 4100 | } |
| 4101 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4102 | std::vector<std::unique_ptr<EventEntry>> downEvents = |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4103 | connection->inputState.synthesizePointerDownEvents(downTime); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4104 | |
| 4105 | if (downEvents.empty()) { |
| 4106 | return; |
| 4107 | } |
| 4108 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4109 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4110 | ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.", |
| 4111 | connection->getInputChannelName().c_str(), downEvents.size()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4112 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4113 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4114 | sp<WindowInfoHandle> windowHandle = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4115 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4116 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 4117 | const bool wasEmpty = connection->outboundQueue.empty(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4118 | for (std::unique_ptr<EventEntry>& downEventEntry : downEvents) { |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 4119 | std::vector<InputTarget> targets{}; |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4120 | switch (downEventEntry->type) { |
| 4121 | case EventEntry::Type::MOTION: { |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 4122 | const auto& motionEntry = static_cast<const MotionEntry&>(*downEventEntry); |
| 4123 | if (windowHandle != nullptr) { |
| 4124 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4125 | for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.getPointerCount(); |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 4126 | pointerIndex++) { |
| 4127 | pointerIds.set(motionEntry.pointerProperties[pointerIndex].id); |
| 4128 | } |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 4129 | addPointerWindowTargetLocked(windowHandle, targetFlags, pointerIds, |
| 4130 | motionEntry.downTime, targets); |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 4131 | } else { |
| 4132 | targets.emplace_back(InputTarget{.inputChannel = connection->inputChannel, |
| 4133 | .flags = targetFlags}); |
| 4134 | const auto it = mDisplayInfos.find(motionEntry.displayId); |
| 4135 | if (it != mDisplayInfos.end()) { |
| 4136 | targets.back().displayTransform = it->second.transform; |
| 4137 | targets.back().setDefaultPointerTransform(it->second.transform); |
| 4138 | } |
| 4139 | } |
| 4140 | logOutboundMotionDetails("down - ", motionEntry); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4141 | break; |
| 4142 | } |
| 4143 | |
| 4144 | case EventEntry::Type::KEY: |
| 4145 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 4146 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4147 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4148 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4149 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4150 | case EventEntry::Type::SENSOR: |
| 4151 | case EventEntry::Type::DRAG: { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4152 | 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] | 4153 | ftl::enum_string(downEventEntry->type).c_str()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4154 | break; |
| 4155 | } |
| 4156 | } |
| 4157 | |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 4158 | if (targets.size() != 1) LOG(FATAL) << __func__ << ": InputTarget not created"; |
| 4159 | enqueueDispatchEntryLocked(connection, std::move(downEventEntry), targets[0], |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 4160 | InputTarget::Flags::DISPATCH_AS_IS); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4161 | } |
| 4162 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 4163 | // If the outbound queue was previously empty, start the dispatch cycle going. |
| 4164 | if (wasEmpty && !connection->outboundQueue.empty()) { |
| 4165 | startDispatchCycleLocked(downTime, connection); |
| 4166 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4167 | } |
| 4168 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4169 | void InputDispatcher::synthesizeCancelationEventsForWindowLocked( |
| 4170 | const sp<WindowInfoHandle>& windowHandle, const CancelationOptions& options) { |
| 4171 | if (windowHandle != nullptr) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 4172 | std::shared_ptr<Connection> wallpaperConnection = |
| 4173 | getConnectionLocked(windowHandle->getToken()); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4174 | if (wallpaperConnection != nullptr) { |
| 4175 | synthesizeCancelationEventsForConnectionLocked(wallpaperConnection, options); |
| 4176 | } |
| 4177 | } |
| 4178 | } |
| 4179 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4180 | std::unique_ptr<MotionEntry> InputDispatcher::splitMotionEvent( |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 4181 | const MotionEntry& originalMotionEntry, std::bitset<MAX_POINTER_ID + 1> pointerIds, |
| 4182 | nsecs_t splitDownTime) { |
| 4183 | ALOG_ASSERT(pointerIds.any()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4184 | |
| 4185 | uint32_t splitPointerIndexMap[MAX_POINTERS]; |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4186 | std::vector<PointerProperties> splitPointerProperties; |
| 4187 | std::vector<PointerCoords> splitPointerCoords; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4188 | |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4189 | uint32_t originalPointerCount = originalMotionEntry.getPointerCount(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4190 | uint32_t splitPointerCount = 0; |
| 4191 | |
| 4192 | for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4193 | originalPointerIndex++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4194 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 4195 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4196 | uint32_t pointerId = uint32_t(pointerProperties.id); |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 4197 | if (pointerIds.test(pointerId)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4198 | splitPointerIndexMap[splitPointerCount] = originalPointerIndex; |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4199 | splitPointerProperties.push_back(pointerProperties); |
| 4200 | splitPointerCoords.push_back(originalMotionEntry.pointerCoords[originalPointerIndex]); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4201 | splitPointerCount += 1; |
| 4202 | } |
| 4203 | } |
| 4204 | |
| 4205 | if (splitPointerCount != pointerIds.count()) { |
| 4206 | // This is bad. We are missing some of the pointers that we expected to deliver. |
| 4207 | // Most likely this indicates that we received an ACTION_MOVE events that has |
| 4208 | // different pointer ids than we expected based on the previous ACTION_DOWN |
| 4209 | // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers |
| 4210 | // in this way. |
| 4211 | ALOGW("Dropping split motion event because the pointer count is %d but " |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 4212 | "we expected there to be %zu pointers. This probably means we received " |
Siarhei Vishniakou | 16e4fa0 | 2023-02-16 17:48:56 -0800 | [diff] [blame] | 4213 | "a broken sequence of pointer ids from the input device: %s", |
| 4214 | splitPointerCount, pointerIds.count(), originalMotionEntry.getDescription().c_str()); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4215 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4216 | } |
| 4217 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 4218 | int32_t action = originalMotionEntry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4219 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4220 | if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN || |
| 4221 | maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
Siarhei Vishniakou | 5b9766d | 2023-07-18 14:06:29 -0700 | [diff] [blame] | 4222 | int32_t originalPointerIndex = MotionEvent::getActionIndex(action); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4223 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 4224 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4225 | uint32_t pointerId = uint32_t(pointerProperties.id); |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 4226 | if (pointerIds.test(pointerId)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4227 | if (pointerIds.count() == 1) { |
| 4228 | // The first/last pointer went down/up. |
| 4229 | action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4230 | ? AMOTION_EVENT_ACTION_DOWN |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4231 | : (originalMotionEntry.flags & AMOTION_EVENT_FLAG_CANCELED) != 0 |
| 4232 | ? AMOTION_EVENT_ACTION_CANCEL |
| 4233 | : AMOTION_EVENT_ACTION_UP; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4234 | } else { |
| 4235 | // A secondary pointer went down/up. |
| 4236 | uint32_t splitPointerIndex = 0; |
| 4237 | while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { |
| 4238 | splitPointerIndex += 1; |
| 4239 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4240 | action = maskedAction | |
| 4241 | (splitPointerIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4242 | } |
| 4243 | } else { |
| 4244 | // An unrelated pointer changed. |
| 4245 | action = AMOTION_EVENT_ACTION_MOVE; |
| 4246 | } |
| 4247 | } |
| 4248 | |
Siarhei Vishniakou | 59e302b | 2023-06-05 08:04:53 -0700 | [diff] [blame] | 4249 | if (action == AMOTION_EVENT_ACTION_DOWN && splitDownTime != originalMotionEntry.eventTime) { |
| 4250 | logDispatchStateLocked(); |
| 4251 | LOG_ALWAYS_FATAL("Split motion event has mismatching downTime and eventTime for " |
| 4252 | "ACTION_DOWN, motionEntry=%s, splitDownTime=%" PRId64, |
| 4253 | originalMotionEntry.getDescription().c_str(), splitDownTime); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4254 | } |
| 4255 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4256 | int32_t newId = mIdGenerator.nextId(); |
Prabir Pradhan | 2dac8b8 | 2023-09-06 01:11:51 +0000 | [diff] [blame] | 4257 | ATRACE_NAME_IF(ATRACE_ENABLED(), |
| 4258 | StringPrintf("Split MotionEvent(id=0x%" PRIx32 ") to MotionEvent(id=0x%" PRIx32 |
| 4259 | ").", |
| 4260 | originalMotionEntry.id, newId)); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4261 | std::unique_ptr<MotionEntry> splitMotionEntry = |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4262 | std::make_unique<MotionEntry>(newId, originalMotionEntry.injectionState, |
| 4263 | originalMotionEntry.eventTime, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4264 | originalMotionEntry.deviceId, originalMotionEntry.source, |
| 4265 | originalMotionEntry.displayId, |
| 4266 | originalMotionEntry.policyFlags, action, |
| 4267 | originalMotionEntry.actionButton, |
| 4268 | originalMotionEntry.flags, originalMotionEntry.metaState, |
| 4269 | originalMotionEntry.buttonState, |
| 4270 | originalMotionEntry.classification, |
| 4271 | originalMotionEntry.edgeFlags, |
| 4272 | originalMotionEntry.xPrecision, |
| 4273 | originalMotionEntry.yPrecision, |
| 4274 | originalMotionEntry.xCursorPosition, |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4275 | originalMotionEntry.yCursorPosition, splitDownTime, |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4276 | splitPointerProperties, splitPointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4277 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4278 | return splitMotionEntry; |
| 4279 | } |
| 4280 | |
Asmita Poddar | dd9a6cd | 2023-09-26 15:35:12 +0000 | [diff] [blame] | 4281 | void InputDispatcher::notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs& args) { |
| 4282 | std::scoped_lock _l(mLock); |
| 4283 | mLatencyTracker.setInputDevices(args.inputDeviceInfos); |
| 4284 | } |
| 4285 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4286 | void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4287 | if (debugInboundEventDetails()) { |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4288 | ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args.eventTime); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4289 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4290 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4291 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4292 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4293 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4294 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4295 | std::unique_ptr<ConfigurationChangedEntry> newEntry = |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4296 | std::make_unique<ConfigurationChangedEntry>(args.id, args.eventTime); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4297 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4298 | } // release lock |
| 4299 | |
| 4300 | if (needWake) { |
| 4301 | mLooper->wake(); |
| 4302 | } |
| 4303 | } |
| 4304 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4305 | void InputDispatcher::notifyKey(const NotifyKeyArgs& args) { |
Prabir Pradhan | 96282b0 | 2023-02-24 22:36:17 +0000 | [diff] [blame] | 4306 | ALOGD_IF(debugInboundEventDetails(), |
| 4307 | "notifyKey - id=%" PRIx32 ", eventTime=%" PRId64 |
| 4308 | ", deviceId=%d, source=%s, displayId=%" PRId32 |
| 4309 | "policyFlags=0x%x, action=%s, flags=0x%x, keyCode=%s, scanCode=0x%x, metaState=0x%x, " |
| 4310 | "downTime=%" PRId64, |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4311 | args.id, args.eventTime, args.deviceId, inputEventSourceToString(args.source).c_str(), |
| 4312 | args.displayId, args.policyFlags, KeyEvent::actionToString(args.action), args.flags, |
| 4313 | KeyEvent::getLabel(args.keyCode), args.scanCode, args.metaState, args.downTime); |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4314 | Result<void> keyCheck = validateKeyEvent(args.action); |
| 4315 | if (!keyCheck.ok()) { |
| 4316 | LOG(ERROR) << "invalid key event: " << keyCheck.error(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4317 | return; |
| 4318 | } |
| 4319 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4320 | uint32_t policyFlags = args.policyFlags; |
| 4321 | int32_t flags = args.flags; |
| 4322 | int32_t metaState = args.metaState; |
Siarhei Vishniakou | 622bd32 | 2018-10-29 18:02:27 -0700 | [diff] [blame] | 4323 | // InputDispatcher tracks and generates key repeats on behalf of |
| 4324 | // whatever notifies it, so repeatCount should always be set to 0 |
| 4325 | constexpr int32_t repeatCount = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4326 | if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { |
| 4327 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 4328 | flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
| 4329 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4330 | if (policyFlags & POLICY_FLAG_FUNCTION) { |
| 4331 | metaState |= AMETA_FUNCTION_ON; |
| 4332 | } |
| 4333 | |
| 4334 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 4335 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4336 | int32_t keyCode = args.keyCode; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4337 | KeyEvent event; |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4338 | event.initialize(args.id, args.deviceId, args.source, args.displayId, INVALID_HMAC, args.action, |
| 4339 | flags, keyCode, args.scanCode, metaState, repeatCount, args.downTime, |
| 4340 | args.eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4341 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4342 | android::base::Timer t; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4343 | mPolicy.interceptKeyBeforeQueueing(event, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4344 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4345 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4346 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4347 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4348 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4349 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4350 | { // acquire lock |
| 4351 | mLock.lock(); |
| 4352 | |
| 4353 | if (shouldSendKeyToInputFilterLocked(args)) { |
| 4354 | mLock.unlock(); |
| 4355 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4356 | policyFlags |= POLICY_FLAG_FILTERED; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4357 | if (!mPolicy.filterInputEvent(event, policyFlags)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4358 | return; // event was consumed by the filter |
| 4359 | } |
| 4360 | |
| 4361 | mLock.lock(); |
| 4362 | } |
| 4363 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4364 | std::unique_ptr<KeyEntry> newEntry = |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4365 | std::make_unique<KeyEntry>(args.id, /*injectionState=*/nullptr, args.eventTime, |
| 4366 | args.deviceId, args.source, args.displayId, policyFlags, |
| 4367 | args.action, flags, keyCode, args.scanCode, metaState, |
| 4368 | repeatCount, args.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4369 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4370 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4371 | mLock.unlock(); |
| 4372 | } // release lock |
| 4373 | |
| 4374 | if (needWake) { |
| 4375 | mLooper->wake(); |
| 4376 | } |
| 4377 | } |
| 4378 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4379 | bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs& args) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4380 | return mInputFilterEnabled; |
| 4381 | } |
| 4382 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4383 | void InputDispatcher::notifyMotion(const NotifyMotionArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4384 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 96282b0 | 2023-02-24 22:36:17 +0000 | [diff] [blame] | 4385 | ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=%s, " |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4386 | "displayId=%" PRId32 ", policyFlags=0x%x, " |
Siarhei Vishniakou | 6ebd069 | 2022-10-20 15:05:45 -0700 | [diff] [blame] | 4387 | "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] | 4388 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, xCursorPosition=%f, " |
| 4389 | "yCursorPosition=%f, downTime=%" PRId64, |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4390 | args.id, args.eventTime, args.deviceId, inputEventSourceToString(args.source).c_str(), |
| 4391 | args.displayId, args.policyFlags, MotionEvent::actionToString(args.action).c_str(), |
| 4392 | args.actionButton, args.flags, args.metaState, args.buttonState, args.edgeFlags, |
| 4393 | args.xPrecision, args.yPrecision, args.xCursorPosition, args.yCursorPosition, |
| 4394 | args.downTime); |
Siarhei Vishniakou | 3218fc0 | 2023-06-15 20:41:02 -0700 | [diff] [blame] | 4395 | for (uint32_t i = 0; i < args.getPointerCount(); i++) { |
Prabir Pradhan | 96282b0 | 2023-02-24 22:36:17 +0000 | [diff] [blame] | 4396 | ALOGD(" Pointer %d: id=%d, toolType=%s, x=%f, y=%f, pressure=%f, size=%f, " |
| 4397 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, orientation=%f", |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4398 | i, args.pointerProperties[i].id, |
| 4399 | ftl::enum_string(args.pointerProperties[i].toolType).c_str(), |
| 4400 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 4401 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 4402 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 4403 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 4404 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 4405 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 4406 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 4407 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 4408 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4409 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4410 | } |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4411 | |
Siarhei Vishniakou | 3218fc0 | 2023-06-15 20:41:02 -0700 | [diff] [blame] | 4412 | Result<void> motionCheck = |
| 4413 | validateMotionEvent(args.action, args.actionButton, args.getPointerCount(), |
| 4414 | args.pointerProperties.data()); |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4415 | if (!motionCheck.ok()) { |
| 4416 | LOG(FATAL) << "Invalid event: " << args.dump() << "; reason: " << motionCheck.error(); |
| 4417 | return; |
| 4418 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4419 | |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 4420 | if (DEBUG_VERIFY_EVENTS) { |
| 4421 | auto [it, _] = |
| 4422 | mVerifiersByDisplay.try_emplace(args.displayId, |
| 4423 | StringPrintf("display %" PRId32, args.displayId)); |
| 4424 | Result<void> result = |
Siarhei Vishniakou | 2d151ac | 2023-09-19 13:30:24 -0700 | [diff] [blame] | 4425 | it->second.processMovement(args.deviceId, args.source, args.action, |
| 4426 | args.getPointerCount(), args.pointerProperties.data(), |
| 4427 | args.pointerCoords.data(), args.flags); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 4428 | if (!result.ok()) { |
| 4429 | LOG(FATAL) << "Bad stream: " << result.error() << " caused by " << args.dump(); |
| 4430 | } |
| 4431 | } |
| 4432 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4433 | uint32_t policyFlags = args.policyFlags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4434 | policyFlags |= POLICY_FLAG_TRUSTED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4435 | |
| 4436 | android::base::Timer t; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4437 | mPolicy.interceptMotionBeforeQueueing(args.displayId, args.eventTime, policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4438 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4439 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4440 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4441 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4442 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4443 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4444 | { // acquire lock |
| 4445 | mLock.lock(); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 4446 | if (!(policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
| 4447 | // Set the flag anyway if we already have an ongoing gesture. That would allow us to |
| 4448 | // complete the processing of the current stroke. |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4449 | const auto touchStateIt = mTouchStatesByDisplay.find(args.displayId); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 4450 | if (touchStateIt != mTouchStatesByDisplay.end()) { |
| 4451 | const TouchState& touchState = touchStateIt->second; |
Linnan Li | 907ae73 | 2023-09-05 17:14:21 +0800 | [diff] [blame] | 4452 | if (touchState.hasTouchingPointers(args.deviceId) || |
| 4453 | touchState.hasHoveringPointers(args.deviceId)) { |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 4454 | policyFlags |= POLICY_FLAG_PASS_TO_USER; |
| 4455 | } |
| 4456 | } |
| 4457 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4458 | |
| 4459 | if (shouldSendMotionToInputFilterLocked(args)) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4460 | ui::Transform displayTransform; |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4461 | if (const auto it = mDisplayInfos.find(args.displayId); it != mDisplayInfos.end()) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4462 | displayTransform = it->second.transform; |
| 4463 | } |
| 4464 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4465 | mLock.unlock(); |
| 4466 | |
| 4467 | MotionEvent event; |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4468 | event.initialize(args.id, args.deviceId, args.source, args.displayId, INVALID_HMAC, |
| 4469 | args.action, args.actionButton, args.flags, args.edgeFlags, |
| 4470 | args.metaState, args.buttonState, args.classification, |
| 4471 | displayTransform, args.xPrecision, args.yPrecision, |
| 4472 | args.xCursorPosition, args.yCursorPosition, displayTransform, |
Siarhei Vishniakou | 3218fc0 | 2023-06-15 20:41:02 -0700 | [diff] [blame] | 4473 | args.downTime, args.eventTime, args.getPointerCount(), |
| 4474 | args.pointerProperties.data(), args.pointerCoords.data()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4475 | |
| 4476 | policyFlags |= POLICY_FLAG_FILTERED; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4477 | if (!mPolicy.filterInputEvent(event, policyFlags)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4478 | return; // event was consumed by the filter |
| 4479 | } |
| 4480 | |
| 4481 | mLock.lock(); |
| 4482 | } |
| 4483 | |
| 4484 | // Just enqueue a new motion event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4485 | std::unique_ptr<MotionEntry> newEntry = |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4486 | std::make_unique<MotionEntry>(args.id, /*injectionState=*/nullptr, args.eventTime, |
| 4487 | args.deviceId, args.source, args.displayId, |
| 4488 | policyFlags, args.action, args.actionButton, |
| 4489 | args.flags, args.metaState, args.buttonState, |
| 4490 | args.classification, args.edgeFlags, args.xPrecision, |
| 4491 | args.yPrecision, args.xCursorPosition, |
| 4492 | args.yCursorPosition, args.downTime, |
| 4493 | args.pointerProperties, args.pointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4494 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4495 | if (args.id != android::os::IInputConstants::INVALID_INPUT_EVENT_ID && |
| 4496 | IdGenerator::getSource(args.id) == IdGenerator::Source::INPUT_READER && |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 4497 | !mInputFilterEnabled) { |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4498 | const bool isDown = args.action == AMOTION_EVENT_ACTION_DOWN; |
Asmita Poddar | dd9a6cd | 2023-09-26 15:35:12 +0000 | [diff] [blame] | 4499 | std::set<InputDeviceUsageSource> sources = getUsageSourcesForMotionArgs(args); |
| 4500 | mLatencyTracker.trackListener(args.id, isDown, args.eventTime, args.readTime, |
| 4501 | args.deviceId, sources); |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 4502 | } |
| 4503 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4504 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4505 | mLock.unlock(); |
| 4506 | } // release lock |
| 4507 | |
| 4508 | if (needWake) { |
| 4509 | mLooper->wake(); |
| 4510 | } |
| 4511 | } |
| 4512 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4513 | void InputDispatcher::notifySensor(const NotifySensorArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4514 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4515 | ALOGD("notifySensor - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 4516 | " sensorType=%s", |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4517 | args.id, args.eventTime, args.deviceId, args.source, |
| 4518 | ftl::enum_string(args.sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4519 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4520 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4521 | bool needWake = false; |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4522 | { // acquire lock |
| 4523 | mLock.lock(); |
| 4524 | |
| 4525 | // Just enqueue a new sensor event. |
| 4526 | std::unique_ptr<SensorEntry> newEntry = |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4527 | std::make_unique<SensorEntry>(args.id, args.eventTime, args.deviceId, args.source, |
| 4528 | /* policyFlags=*/0, args.hwTimestamp, args.sensorType, |
| 4529 | args.accuracy, args.accuracyChanged, args.values); |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4530 | |
| 4531 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
| 4532 | mLock.unlock(); |
| 4533 | } // release lock |
| 4534 | |
| 4535 | if (needWake) { |
| 4536 | mLooper->wake(); |
| 4537 | } |
| 4538 | } |
| 4539 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4540 | void InputDispatcher::notifyVibratorState(const NotifyVibratorStateArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4541 | if (debugInboundEventDetails()) { |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4542 | ALOGD("notifyVibratorState - eventTime=%" PRId64 ", device=%d, isOn=%d", args.eventTime, |
| 4543 | args.deviceId, args.isOn); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4544 | } |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4545 | mPolicy.notifyVibratorState(args.deviceId, args.isOn); |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 4546 | } |
| 4547 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4548 | bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs& args) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4549 | return mInputFilterEnabled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4550 | } |
| 4551 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4552 | void InputDispatcher::notifySwitch(const NotifySwitchArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4553 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4554 | ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, " |
| 4555 | "switchMask=0x%08x", |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4556 | args.eventTime, args.policyFlags, args.switchValues, args.switchMask); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4557 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4558 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4559 | uint32_t policyFlags = args.policyFlags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4560 | policyFlags |= POLICY_FLAG_TRUSTED; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4561 | mPolicy.notifySwitch(args.eventTime, args.switchValues, args.switchMask, policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4562 | } |
| 4563 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4564 | void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4565 | if (debugInboundEventDetails()) { |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4566 | ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args.eventTime, |
| 4567 | args.deviceId); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4568 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4569 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4570 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4571 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4572 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4573 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4574 | std::unique_ptr<DeviceResetEntry> newEntry = |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4575 | std::make_unique<DeviceResetEntry>(args.id, args.eventTime, args.deviceId); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4576 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Siarhei Vishniakou | 1160ecd | 2023-06-28 15:57:47 -0700 | [diff] [blame] | 4577 | |
| 4578 | for (auto& [_, verifier] : mVerifiersByDisplay) { |
| 4579 | verifier.resetDevice(args.deviceId); |
| 4580 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4581 | } // release lock |
| 4582 | |
| 4583 | if (needWake) { |
| 4584 | mLooper->wake(); |
| 4585 | } |
| 4586 | } |
| 4587 | |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4588 | void InputDispatcher::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4589 | if (debugInboundEventDetails()) { |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4590 | ALOGD("notifyPointerCaptureChanged - eventTime=%" PRId64 ", enabled=%s", args.eventTime, |
| 4591 | args.request.enable ? "true" : "false"); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4592 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4593 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4594 | bool needWake = false; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4595 | { // acquire lock |
| 4596 | std::scoped_lock _l(mLock); |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4597 | auto entry = |
| 4598 | std::make_unique<PointerCaptureChangedEntry>(args.id, args.eventTime, args.request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4599 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 4600 | } // release lock |
| 4601 | |
| 4602 | if (needWake) { |
| 4603 | mLooper->wake(); |
| 4604 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4605 | } |
| 4606 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4607 | InputEventInjectionResult InputDispatcher::injectInputEvent(const InputEvent* event, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 4608 | std::optional<gui::Uid> targetUid, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4609 | InputEventInjectionSync syncMode, |
| 4610 | std::chrono::milliseconds timeout, |
| 4611 | uint32_t policyFlags) { |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4612 | Result<void> eventValidation = validateInputEvent(*event); |
| 4613 | if (!eventValidation.ok()) { |
| 4614 | LOG(INFO) << "Injection failed: invalid event: " << eventValidation.error(); |
| 4615 | return InputEventInjectionResult::FAILED; |
| 4616 | } |
| 4617 | |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4618 | if (debugInboundEventDetails()) { |
Siarhei Vishniakou | 827d1ac | 2023-07-21 16:37:51 -0700 | [diff] [blame] | 4619 | LOG(INFO) << __func__ << ": targetUid=" << toString(targetUid, &uidString) |
| 4620 | << ", syncMode=" << ftl::enum_string(syncMode) << ", timeout=" << timeout.count() |
| 4621 | << "ms, policyFlags=0x" << std::hex << policyFlags << std::dec |
| 4622 | << ", event=" << *event; |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4623 | } |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 4624 | 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] | 4625 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4626 | policyFlags |= POLICY_FLAG_INJECTED | POLICY_FLAG_TRUSTED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4627 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4628 | // 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] | 4629 | // that have gone through the InputFilter. If the event passed through the InputFilter, assign |
| 4630 | // the provided device id. If the InputFilter is accessibility, and it modifies or synthesizes |
| 4631 | // the injected event, it is responsible for setting POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY. |
| 4632 | // For those events, we will set FLAG_IS_ACCESSIBILITY_EVENT to allow apps to distinguish them |
| 4633 | // from events that originate from actual hardware. |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 4634 | DeviceId resolvedDeviceId = VIRTUAL_KEYBOARD_ID; |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4635 | if (policyFlags & POLICY_FLAG_FILTERED) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4636 | resolvedDeviceId = event->getDeviceId(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4637 | } |
| 4638 | |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4639 | const bool isAsync = syncMode == InputEventInjectionSync::NONE; |
| 4640 | auto injectionState = std::make_shared<InjectionState>(targetUid, isAsync); |
| 4641 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4642 | std::queue<std::unique_ptr<EventEntry>> injectedEntries; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4643 | switch (event->getType()) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4644 | case InputEventType::KEY: { |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4645 | const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event); |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4646 | const int32_t action = incomingKey.getAction(); |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4647 | int32_t flags = incomingKey.getFlags(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4648 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4649 | flags |= AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4650 | } |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4651 | int32_t keyCode = incomingKey.getKeyCode(); |
| 4652 | int32_t metaState = incomingKey.getMetaState(); |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4653 | KeyEvent keyEvent; |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4654 | keyEvent.initialize(incomingKey.getId(), resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4655 | incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode, |
| 4656 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), |
| 4657 | incomingKey.getDownTime(), incomingKey.getEventTime()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4658 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4659 | if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) { |
| 4660 | policyFlags |= POLICY_FLAG_VIRTUAL; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4661 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4662 | |
| 4663 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 4664 | android::base::Timer t; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4665 | mPolicy.interceptKeyBeforeQueueing(keyEvent, /*byref*/ policyFlags); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4666 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4667 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
| 4668 | std::to_string(t.duration().count()).c_str()); |
| 4669 | } |
| 4670 | } |
| 4671 | |
| 4672 | mLock.lock(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4673 | std::unique_ptr<KeyEntry> injectedEntry = |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4674 | std::make_unique<KeyEntry>(incomingKey.getId(), injectionState, |
| 4675 | incomingKey.getEventTime(), resolvedDeviceId, |
| 4676 | incomingKey.getSource(), incomingKey.getDisplayId(), |
| 4677 | policyFlags, action, flags, keyCode, |
| 4678 | incomingKey.getScanCode(), metaState, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4679 | incomingKey.getRepeatCount(), |
| 4680 | incomingKey.getDownTime()); |
| 4681 | injectedEntries.push(std::move(injectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4682 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4683 | } |
| 4684 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4685 | case InputEventType::MOTION: { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4686 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4687 | const bool isPointerEvent = |
| 4688 | isFromSource(event->getSource(), AINPUT_SOURCE_CLASS_POINTER); |
| 4689 | // If a pointer event has no displayId specified, inject it to the default display. |
| 4690 | const uint32_t displayId = isPointerEvent && (event->getDisplayId() == ADISPLAY_ID_NONE) |
| 4691 | ? ADISPLAY_ID_DEFAULT |
| 4692 | : event->getDisplayId(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4693 | int32_t flags = motionEvent.getFlags(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4694 | |
| 4695 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4696 | nsecs_t eventTime = motionEvent.getEventTime(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4697 | android::base::Timer t; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4698 | mPolicy.interceptMotionBeforeQueueing(displayId, eventTime, /*byref*/ policyFlags); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4699 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4700 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
| 4701 | std::to_string(t.duration().count()).c_str()); |
| 4702 | } |
| 4703 | } |
| 4704 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4705 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4706 | flags |= AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4707 | } |
| 4708 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4709 | mLock.lock(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4710 | const nsecs_t* sampleEventTimes = motionEvent.getSampleEventTimes(); |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4711 | const size_t pointerCount = motionEvent.getPointerCount(); |
| 4712 | const std::vector<PointerProperties> |
| 4713 | pointerProperties(motionEvent.getPointerProperties(), |
| 4714 | motionEvent.getPointerProperties() + pointerCount); |
| 4715 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4716 | const PointerCoords* samplePointerCoords = motionEvent.getSamplePointerCoords(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4717 | std::unique_ptr<MotionEntry> injectedEntry = |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4718 | std::make_unique<MotionEntry>(motionEvent.getId(), injectionState, |
| 4719 | *sampleEventTimes, resolvedDeviceId, |
| 4720 | motionEvent.getSource(), displayId, policyFlags, |
| 4721 | motionEvent.getAction(), |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4722 | motionEvent.getActionButton(), flags, |
| 4723 | motionEvent.getMetaState(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4724 | motionEvent.getButtonState(), |
| 4725 | motionEvent.getClassification(), |
| 4726 | motionEvent.getEdgeFlags(), |
| 4727 | motionEvent.getXPrecision(), |
| 4728 | motionEvent.getYPrecision(), |
| 4729 | motionEvent.getRawXCursorPosition(), |
| 4730 | motionEvent.getRawYCursorPosition(), |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4731 | motionEvent.getDownTime(), pointerProperties, |
| 4732 | std::vector<PointerCoords>(samplePointerCoords, |
| 4733 | samplePointerCoords + |
| 4734 | pointerCount)); |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4735 | transformMotionEntryForInjectionLocked(*injectedEntry, motionEvent.getTransform()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4736 | injectedEntries.push(std::move(injectedEntry)); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4737 | for (size_t i = motionEvent.getHistorySize(); i > 0; i--) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4738 | sampleEventTimes += 1; |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4739 | samplePointerCoords += motionEvent.getPointerCount(); |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4740 | std::unique_ptr<MotionEntry> nextInjectedEntry = std::make_unique< |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4741 | MotionEntry>(motionEvent.getId(), injectionState, *sampleEventTimes, |
| 4742 | resolvedDeviceId, motionEvent.getSource(), displayId, |
| 4743 | policyFlags, motionEvent.getAction(), |
| 4744 | motionEvent.getActionButton(), flags, |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4745 | motionEvent.getMetaState(), motionEvent.getButtonState(), |
| 4746 | motionEvent.getClassification(), motionEvent.getEdgeFlags(), |
| 4747 | motionEvent.getXPrecision(), motionEvent.getYPrecision(), |
| 4748 | motionEvent.getRawXCursorPosition(), |
| 4749 | motionEvent.getRawYCursorPosition(), motionEvent.getDownTime(), |
| 4750 | pointerProperties, |
| 4751 | std::vector<PointerCoords>(samplePointerCoords, |
| 4752 | samplePointerCoords + |
| 4753 | pointerCount)); |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4754 | transformMotionEntryForInjectionLocked(*nextInjectedEntry, |
| 4755 | motionEvent.getTransform()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4756 | injectedEntries.push(std::move(nextInjectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4757 | } |
| 4758 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4759 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4760 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4761 | default: |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4762 | LOG(WARNING) << "Cannot inject " << ftl::enum_string(event->getType()) << " events"; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4763 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4764 | } |
| 4765 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4766 | bool needWake = false; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4767 | while (!injectedEntries.empty()) { |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 4768 | if (DEBUG_INJECTION) { |
Siarhei Vishniakou | 827d1ac | 2023-07-21 16:37:51 -0700 | [diff] [blame] | 4769 | LOG(INFO) << "Injecting " << injectedEntries.front()->getDescription(); |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 4770 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4771 | needWake |= enqueueInboundEventLocked(std::move(injectedEntries.front())); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4772 | injectedEntries.pop(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4773 | } |
| 4774 | |
| 4775 | mLock.unlock(); |
| 4776 | |
| 4777 | if (needWake) { |
| 4778 | mLooper->wake(); |
| 4779 | } |
| 4780 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4781 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4782 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4783 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4784 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4785 | if (syncMode == InputEventInjectionSync::NONE) { |
| 4786 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4787 | } else { |
| 4788 | for (;;) { |
| 4789 | injectionResult = injectionState->injectionResult; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4790 | if (injectionResult != InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4791 | break; |
| 4792 | } |
| 4793 | |
| 4794 | nsecs_t remainingTimeout = endTime - now(); |
| 4795 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4796 | if (DEBUG_INJECTION) { |
| 4797 | ALOGD("injectInputEvent - Timed out waiting for injection result " |
| 4798 | "to become available."); |
| 4799 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4800 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4801 | break; |
| 4802 | } |
| 4803 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4804 | mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4805 | } |
| 4806 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4807 | if (injectionResult == InputEventInjectionResult::SUCCEEDED && |
| 4808 | syncMode == InputEventInjectionSync::WAIT_FOR_FINISHED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4809 | while (injectionState->pendingForegroundDispatches != 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4810 | if (DEBUG_INJECTION) { |
| 4811 | ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", |
| 4812 | injectionState->pendingForegroundDispatches); |
| 4813 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4814 | nsecs_t remainingTimeout = endTime - now(); |
| 4815 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4816 | if (DEBUG_INJECTION) { |
| 4817 | ALOGD("injectInputEvent - Timed out waiting for pending foreground " |
| 4818 | "dispatches to finish."); |
| 4819 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4820 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4821 | break; |
| 4822 | } |
| 4823 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4824 | mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4825 | } |
| 4826 | } |
| 4827 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4828 | } // release lock |
| 4829 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4830 | if (DEBUG_INJECTION) { |
Siarhei Vishniakou | 827d1ac | 2023-07-21 16:37:51 -0700 | [diff] [blame] | 4831 | LOG(INFO) << "injectInputEvent - Finished with result " |
| 4832 | << ftl::enum_string(injectionResult); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4833 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4834 | |
| 4835 | return injectionResult; |
| 4836 | } |
| 4837 | |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4838 | std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4839 | std::array<uint8_t, 32> calculatedHmac; |
| 4840 | std::unique_ptr<VerifiedInputEvent> result; |
| 4841 | switch (event.getType()) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4842 | case InputEventType::KEY: { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4843 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event); |
| 4844 | VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent); |
| 4845 | result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4846 | calculatedHmac = sign(verifiedKeyEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4847 | break; |
| 4848 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4849 | case InputEventType::MOTION: { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4850 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event); |
| 4851 | VerifiedMotionEvent verifiedMotionEvent = |
| 4852 | verifiedMotionEventFromMotionEvent(motionEvent); |
| 4853 | result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4854 | calculatedHmac = sign(verifiedMotionEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4855 | break; |
| 4856 | } |
| 4857 | default: { |
| 4858 | ALOGE("Cannot verify events of type %" PRId32, event.getType()); |
| 4859 | return nullptr; |
| 4860 | } |
| 4861 | } |
| 4862 | if (calculatedHmac == INVALID_HMAC) { |
| 4863 | return nullptr; |
| 4864 | } |
tyiu | 1573a67 | 2023-02-21 22:38:32 +0000 | [diff] [blame] | 4865 | if (0 != CRYPTO_memcmp(calculatedHmac.data(), event.getHmac().data(), calculatedHmac.size())) { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4866 | return nullptr; |
| 4867 | } |
| 4868 | return result; |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4869 | } |
| 4870 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 4871 | void InputDispatcher::setInjectionResult(const EventEntry& entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4872 | InputEventInjectionResult injectionResult) { |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4873 | if (!entry.injectionState) { |
| 4874 | // Not an injected event. |
| 4875 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4876 | } |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4877 | |
| 4878 | InjectionState& injectionState = *entry.injectionState; |
| 4879 | if (DEBUG_INJECTION) { |
| 4880 | LOG(INFO) << "Setting input event injection result to " |
| 4881 | << ftl::enum_string(injectionResult); |
| 4882 | } |
| 4883 | |
| 4884 | if (injectionState.injectionIsAsync && !(entry.policyFlags & POLICY_FLAG_FILTERED)) { |
| 4885 | // Log the outcome since the injector did not wait for the injection result. |
| 4886 | switch (injectionResult) { |
| 4887 | case InputEventInjectionResult::SUCCEEDED: |
| 4888 | ALOGV("Asynchronous input event injection succeeded."); |
| 4889 | break; |
| 4890 | case InputEventInjectionResult::TARGET_MISMATCH: |
| 4891 | ALOGV("Asynchronous input event injection target mismatch."); |
| 4892 | break; |
| 4893 | case InputEventInjectionResult::FAILED: |
| 4894 | ALOGW("Asynchronous input event injection failed."); |
| 4895 | break; |
| 4896 | case InputEventInjectionResult::TIMED_OUT: |
| 4897 | ALOGW("Asynchronous input event injection timed out."); |
| 4898 | break; |
| 4899 | case InputEventInjectionResult::PENDING: |
| 4900 | ALOGE("Setting result to 'PENDING' for asynchronous injection"); |
| 4901 | break; |
| 4902 | } |
| 4903 | } |
| 4904 | |
| 4905 | injectionState.injectionResult = injectionResult; |
| 4906 | mInjectionResultAvailable.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4907 | } |
| 4908 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4909 | void InputDispatcher::transformMotionEntryForInjectionLocked( |
| 4910 | MotionEntry& entry, const ui::Transform& injectedTransform) const { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4911 | // Input injection works in the logical display coordinate space, but the input pipeline works |
| 4912 | // display space, so we need to transform the injected events accordingly. |
| 4913 | const auto it = mDisplayInfos.find(entry.displayId); |
| 4914 | if (it == mDisplayInfos.end()) return; |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4915 | const auto& transformToDisplay = it->second.transform.inverse() * injectedTransform; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4916 | |
Prabir Pradhan | d9a2ebe | 2022-07-20 19:25:13 +0000 | [diff] [blame] | 4917 | if (entry.xCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION && |
| 4918 | entry.yCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) { |
| 4919 | const vec2 cursor = |
| 4920 | MotionEvent::calculateTransformedXY(entry.source, transformToDisplay, |
| 4921 | {entry.xCursorPosition, entry.yCursorPosition}); |
| 4922 | entry.xCursorPosition = cursor.x; |
| 4923 | entry.yCursorPosition = cursor.y; |
| 4924 | } |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4925 | for (uint32_t i = 0; i < entry.getPointerCount(); i++) { |
Prabir Pradhan | 8e6ce22 | 2022-02-24 09:08:54 -0800 | [diff] [blame] | 4926 | entry.pointerCoords[i] = |
| 4927 | MotionEvent::calculateTransformedCoords(entry.source, transformToDisplay, |
| 4928 | entry.pointerCoords[i]); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4929 | } |
| 4930 | } |
| 4931 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 4932 | void InputDispatcher::incrementPendingForegroundDispatches(const EventEntry& entry) { |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4933 | if (entry.injectionState) { |
| 4934 | entry.injectionState->pendingForegroundDispatches += 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4935 | } |
| 4936 | } |
| 4937 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 4938 | void InputDispatcher::decrementPendingForegroundDispatches(const EventEntry& entry) { |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4939 | if (entry.injectionState) { |
| 4940 | entry.injectionState->pendingForegroundDispatches -= 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4941 | |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4942 | if (entry.injectionState->pendingForegroundDispatches == 0) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4943 | mInjectionSyncFinished.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4944 | } |
| 4945 | } |
| 4946 | } |
| 4947 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4948 | const std::vector<sp<WindowInfoHandle>>& InputDispatcher::getWindowHandlesLocked( |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4949 | int32_t displayId) const { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4950 | static const std::vector<sp<WindowInfoHandle>> EMPTY_WINDOW_HANDLES; |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4951 | auto it = mWindowHandlesByDisplay.find(displayId); |
| 4952 | return it != mWindowHandlesByDisplay.end() ? it->second : EMPTY_WINDOW_HANDLES; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4953 | } |
| 4954 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4955 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 4956 | const sp<IBinder>& windowHandleToken, std::optional<int32_t> displayId) const { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4957 | if (windowHandleToken == nullptr) { |
| 4958 | return nullptr; |
| 4959 | } |
| 4960 | |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 4961 | if (!displayId) { |
| 4962 | // Look through all displays. |
| 4963 | for (auto& it : mWindowHandlesByDisplay) { |
| 4964 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 4965 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 4966 | if (windowHandle->getToken() == windowHandleToken) { |
| 4967 | return windowHandle; |
| 4968 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4969 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4970 | } |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4971 | return nullptr; |
| 4972 | } |
| 4973 | |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 4974 | // Only look through the requested display. |
| 4975 | for (const sp<WindowInfoHandle>& windowHandle : getWindowHandlesLocked(*displayId)) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4976 | if (windowHandle->getToken() == windowHandleToken) { |
| 4977 | return windowHandle; |
| 4978 | } |
| 4979 | } |
| 4980 | return nullptr; |
| 4981 | } |
| 4982 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4983 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
| 4984 | const sp<WindowInfoHandle>& windowHandle) const { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4985 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4986 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 4987 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4988 | if (handle->getId() == windowHandle->getId() && |
| 4989 | handle->getToken() == windowHandle->getToken()) { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4990 | if (windowHandle->getInfo()->displayId != it.first) { |
| 4991 | ALOGE("Found window %s in display %" PRId32 |
| 4992 | ", but it should belong to display %" PRId32, |
| 4993 | windowHandle->getName().c_str(), it.first, |
| 4994 | windowHandle->getInfo()->displayId); |
| 4995 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4996 | return handle; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4997 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4998 | } |
| 4999 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 5000 | return nullptr; |
| 5001 | } |
| 5002 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5003 | sp<WindowInfoHandle> InputDispatcher::getFocusedWindowHandleLocked(int displayId) const { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 5004 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(displayId); |
| 5005 | return getWindowHandleLocked(focusedToken, displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5006 | } |
| 5007 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5008 | ui::Transform InputDispatcher::getTransformLocked(int32_t displayId) const { |
| 5009 | auto displayInfoIt = mDisplayInfos.find(displayId); |
| 5010 | return displayInfoIt != mDisplayInfos.end() ? displayInfoIt->second.transform |
| 5011 | : kIdentityTransform; |
| 5012 | } |
| 5013 | |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5014 | bool InputDispatcher::canWindowReceiveMotionLocked(const sp<WindowInfoHandle>& window, |
| 5015 | const MotionEntry& motionEntry) const { |
| 5016 | const WindowInfo& info = *window->getInfo(); |
| 5017 | |
| 5018 | // Skip spy window targets that are not valid for targeted injection. |
| 5019 | if (const auto err = verifyTargetedInjection(window, motionEntry); err) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5020 | return false; |
| 5021 | } |
| 5022 | |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5023 | if (info.inputConfig.test(WindowInfo::InputConfig::PAUSE_DISPATCHING)) { |
| 5024 | ALOGI("Not sending touch event to %s because it is paused", window->getName().c_str()); |
| 5025 | return false; |
| 5026 | } |
| 5027 | |
| 5028 | if (info.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) { |
| 5029 | ALOGW("Not sending touch gesture to %s because it has config NO_INPUT_CHANNEL", |
| 5030 | window->getName().c_str()); |
| 5031 | return false; |
| 5032 | } |
| 5033 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 5034 | std::shared_ptr<Connection> connection = getConnectionLocked(window->getToken()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5035 | if (connection == nullptr) { |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5036 | ALOGW("Not sending touch to %s because there's no corresponding connection", |
| 5037 | window->getName().c_str()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5038 | return false; |
| 5039 | } |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5040 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5041 | if (!connection->responsive) { |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5042 | 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] | 5043 | return false; |
| 5044 | } |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5045 | |
| 5046 | // Drop events that can't be trusted due to occlusion |
| 5047 | const auto [x, y] = resolveTouchedPosition(motionEntry); |
| 5048 | TouchOcclusionInfo occlusionInfo = computeTouchOcclusionInfoLocked(window, x, y); |
| 5049 | if (!isTouchTrustedLocked(occlusionInfo)) { |
| 5050 | if (DEBUG_TOUCH_OCCLUSION) { |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 5051 | ALOGD("Stack of obscuring windows during untrusted touch (%.1f, %.1f):", x, y); |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5052 | for (const auto& log : occlusionInfo.debugInfo) { |
| 5053 | ALOGD("%s", log.c_str()); |
| 5054 | } |
| 5055 | } |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 5056 | ALOGW("Dropping untrusted touch event due to %s/%s", occlusionInfo.obscuringPackage.c_str(), |
| 5057 | occlusionInfo.obscuringUid.toString().c_str()); |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5058 | return false; |
| 5059 | } |
| 5060 | |
| 5061 | // Drop touch events if requested by input feature |
| 5062 | if (shouldDropInput(motionEntry, window)) { |
| 5063 | return false; |
| 5064 | } |
| 5065 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5066 | return true; |
| 5067 | } |
| 5068 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5069 | std::shared_ptr<InputChannel> InputDispatcher::getInputChannelLocked( |
| 5070 | const sp<IBinder>& token) const { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5071 | auto connectionIt = mConnectionsByToken.find(token); |
| 5072 | if (connectionIt == mConnectionsByToken.end()) { |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 5073 | return nullptr; |
| 5074 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5075 | return connectionIt->second->inputChannel; |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 5076 | } |
| 5077 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5078 | void InputDispatcher::updateWindowHandlesForDisplayLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5079 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
| 5080 | if (windowInfoHandles.empty()) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5081 | // Remove all handles on a display if there are no windows left. |
| 5082 | mWindowHandlesByDisplay.erase(displayId); |
| 5083 | return; |
| 5084 | } |
| 5085 | |
| 5086 | // Since we compare the pointer of input window handles across window updates, we need |
| 5087 | // 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] | 5088 | const std::vector<sp<WindowInfoHandle>>& oldHandles = getWindowHandlesLocked(displayId); |
| 5089 | std::unordered_map<int32_t /*id*/, sp<WindowInfoHandle>> oldHandlesById; |
| 5090 | for (const sp<WindowInfoHandle>& handle : oldHandles) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5091 | oldHandlesById[handle->getId()] = handle; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5092 | } |
| 5093 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5094 | std::vector<sp<WindowInfoHandle>> newHandles; |
| 5095 | for (const sp<WindowInfoHandle>& handle : windowInfoHandles) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5096 | const WindowInfo* info = handle->getInfo(); |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 5097 | if (getInputChannelLocked(handle->getToken()) == nullptr) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5098 | const bool noInputChannel = |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 5099 | info->inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5100 | const bool canReceiveInput = |
| 5101 | !info->inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE) || |
| 5102 | !info->inputConfig.test(WindowInfo::InputConfig::NOT_FOCUSABLE); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5103 | if (canReceiveInput && !noInputChannel) { |
John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 5104 | ALOGV("Window handle %s has no registered input channel", |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5105 | handle->getName().c_str()); |
Robert Carr | 2984b7a | 2020-04-13 17:06:45 -0700 | [diff] [blame] | 5106 | continue; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5107 | } |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5108 | } |
| 5109 | |
| 5110 | if (info->displayId != displayId) { |
| 5111 | ALOGE("Window %s updated by wrong display %d, should belong to display %d", |
| 5112 | handle->getName().c_str(), displayId, info->displayId); |
| 5113 | continue; |
| 5114 | } |
| 5115 | |
Robert Carr | edd1360 | 2020-04-13 17:24:34 -0700 | [diff] [blame] | 5116 | if ((oldHandlesById.find(handle->getId()) != oldHandlesById.end()) && |
| 5117 | (oldHandlesById.at(handle->getId())->getToken() == handle->getToken())) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5118 | const sp<WindowInfoHandle>& oldHandle = oldHandlesById.at(handle->getId()); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5119 | oldHandle->updateFrom(handle); |
| 5120 | newHandles.push_back(oldHandle); |
| 5121 | } else { |
| 5122 | newHandles.push_back(handle); |
| 5123 | } |
| 5124 | } |
| 5125 | |
| 5126 | // Insert or replace |
| 5127 | mWindowHandlesByDisplay[displayId] = newHandles; |
| 5128 | } |
| 5129 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5130 | /** |
| 5131 | * Called from InputManagerService, update window handle list by displayId that can receive input. |
| 5132 | * A window handle contains information about InputChannel, Touch Region, Types, Focused,... |
| 5133 | * If set an empty list, remove all handles from the specific display. |
| 5134 | * For focused handle, check if need to change and send a cancel event to previous one. |
| 5135 | * For removed handle, check if need to send a cancel event if already in touch. |
| 5136 | */ |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5137 | void InputDispatcher::setInputWindowsLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5138 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5139 | if (DEBUG_FOCUS) { |
| 5140 | std::string windowList; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5141 | for (const sp<WindowInfoHandle>& iwh : windowInfoHandles) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5142 | windowList += iwh->getName() + " "; |
| 5143 | } |
| 5144 | ALOGD("setInputWindows displayId=%" PRId32 " %s", displayId, windowList.c_str()); |
| 5145 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5146 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 5147 | // Check preconditions for new input windows |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5148 | for (const sp<WindowInfoHandle>& window : windowInfoHandles) { |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 5149 | const WindowInfo& info = *window->getInfo(); |
| 5150 | |
| 5151 | // 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] | 5152 | const bool noInputWindow = info.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5153 | if (noInputWindow && window->getToken() != nullptr) { |
| 5154 | ALOGE("%s has feature NO_INPUT_WINDOW, but a non-null token. Clearing", |
| 5155 | window->getName().c_str()); |
| 5156 | window->releaseChannel(); |
| 5157 | } |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 5158 | |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 5159 | // Ensure all spy windows are trusted overlays |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5160 | LOG_ALWAYS_FATAL_IF(info.isSpy() && |
| 5161 | !info.inputConfig.test( |
| 5162 | WindowInfo::InputConfig::TRUSTED_OVERLAY), |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 5163 | "%s has feature SPY, but is not a trusted overlay.", |
| 5164 | window->getName().c_str()); |
| 5165 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 5166 | // Ensure all stylus interceptors are trusted overlays |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5167 | LOG_ALWAYS_FATAL_IF(info.interceptsStylus() && |
| 5168 | !info.inputConfig.test( |
| 5169 | WindowInfo::InputConfig::TRUSTED_OVERLAY), |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 5170 | "%s has feature INTERCEPTS_STYLUS, but is not a trusted overlay.", |
| 5171 | window->getName().c_str()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5172 | } |
| 5173 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5174 | // Copy old handles for release if they are no longer present. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5175 | const std::vector<sp<WindowInfoHandle>> oldWindowHandles = getWindowHandlesLocked(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5176 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5177 | updateWindowHandlesForDisplayLocked(windowInfoHandles, displayId); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5178 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5179 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5180 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5181 | std::optional<FocusResolver::FocusChanges> changes = |
| 5182 | mFocusResolver.setInputWindows(displayId, windowHandles); |
| 5183 | if (changes) { |
| 5184 | onFocusChangedLocked(*changes); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5185 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5186 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5187 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 5188 | mTouchStatesByDisplay.find(displayId); |
| 5189 | if (stateIt != mTouchStatesByDisplay.end()) { |
| 5190 | TouchState& state = stateIt->second; |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5191 | for (size_t i = 0; i < state.windows.size();) { |
| 5192 | TouchedWindow& touchedWindow = state.windows[i]; |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 5193 | if (getWindowHandleLocked(touchedWindow.windowHandle) == nullptr) { |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 5194 | LOG(INFO) << "Touched window was removed: " << touchedWindow.windowHandle->getName() |
| 5195 | << " in display %" << displayId; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5196 | std::shared_ptr<InputChannel> touchedInputChannel = |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5197 | getInputChannelLocked(touchedWindow.windowHandle->getToken()); |
| 5198 | if (touchedInputChannel != nullptr) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 5199 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5200 | "touched window was removed"); |
| 5201 | synthesizeCancelationEventsForInputChannelLocked(touchedInputChannel, options); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5202 | // Since we are about to drop the touch, cancel the events for the wallpaper as |
| 5203 | // well. |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5204 | if (touchedWindow.targetFlags.test(InputTarget::Flags::FOREGROUND) && |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5205 | touchedWindow.windowHandle->getInfo()->inputConfig.test( |
| 5206 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER)) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5207 | sp<WindowInfoHandle> wallpaper = state.getWallpaperWindow(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5208 | synthesizeCancelationEventsForWindowLocked(wallpaper, options); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5209 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5210 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5211 | state.windows.erase(state.windows.begin() + i); |
| 5212 | } else { |
| 5213 | ++i; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5214 | } |
| 5215 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5216 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5217 | // 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] | 5218 | // could just clear the state here. |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 5219 | if (mDragState && mDragState->dragWindow->getInfo()->displayId == displayId && |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5220 | std::find(windowHandles.begin(), windowHandles.end(), mDragState->dragWindow) == |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5221 | windowHandles.end()) { |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 5222 | ALOGI("Drag window went away: %s", mDragState->dragWindow->getName().c_str()); |
| 5223 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5224 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5225 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5226 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 5227 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5228 | // Release information for windows that are no longer present. |
| 5229 | // This ensures that unused input channels are released promptly. |
| 5230 | // Otherwise, they might stick around until the window handle is destroyed |
| 5231 | // which might not happen until the next GC. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5232 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 5233 | if (getWindowHandleLocked(oldWindowHandle) == nullptr) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5234 | if (DEBUG_FOCUS) { |
| 5235 | ALOGD("Window went away: %s", oldWindowHandle->getName().c_str()); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 5236 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5237 | oldWindowHandle->releaseChannel(); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 5238 | } |
chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 5239 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5240 | } |
| 5241 | |
| 5242 | void InputDispatcher::setFocusedApplication( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5243 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5244 | if (DEBUG_FOCUS) { |
| 5245 | ALOGD("setFocusedApplication displayId=%" PRId32 " %s", displayId, |
| 5246 | inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>"); |
| 5247 | } |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 5248 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5249 | std::scoped_lock _l(mLock); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 5250 | setFocusedApplicationLocked(displayId, inputApplicationHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5251 | } // release lock |
| 5252 | |
| 5253 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5254 | mLooper->wake(); |
| 5255 | } |
| 5256 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 5257 | void InputDispatcher::setFocusedApplicationLocked( |
| 5258 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
| 5259 | std::shared_ptr<InputApplicationHandle> oldFocusedApplicationHandle = |
| 5260 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 5261 | |
| 5262 | if (sharedPointersEqual(oldFocusedApplicationHandle, inputApplicationHandle)) { |
| 5263 | return; // This application is already focused. No need to wake up or change anything. |
| 5264 | } |
| 5265 | |
| 5266 | // Set the new application handle. |
| 5267 | if (inputApplicationHandle != nullptr) { |
| 5268 | mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle; |
| 5269 | } else { |
| 5270 | mFocusedApplicationHandlesByDisplay.erase(displayId); |
| 5271 | } |
| 5272 | |
| 5273 | // No matter what the old focused application was, stop waiting on it because it is |
| 5274 | // no longer focused. |
| 5275 | resetNoFocusedWindowTimeoutLocked(); |
| 5276 | } |
| 5277 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5278 | /** |
| 5279 | * Sets the focused display, which is responsible for receiving focus-dispatched input events where |
| 5280 | * the display not specified. |
| 5281 | * |
| 5282 | * We track any unreleased events for each window. If a window loses the ability to receive the |
| 5283 | * released event, we will send a cancel event to it. So when the focused display is changed, we |
| 5284 | * cancel all the unreleased display-unspecified events for the focused window on the old focused |
| 5285 | * display. The display-specified events won't be affected. |
| 5286 | */ |
| 5287 | void InputDispatcher::setFocusedDisplay(int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5288 | if (DEBUG_FOCUS) { |
| 5289 | ALOGD("setFocusedDisplay displayId=%" PRId32, displayId); |
| 5290 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5291 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5292 | std::scoped_lock _l(mLock); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5293 | |
| 5294 | if (mFocusedDisplayId != displayId) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5295 | sp<IBinder> oldFocusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5296 | mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5297 | if (oldFocusedWindowToken != nullptr) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5298 | std::shared_ptr<InputChannel> inputChannel = |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5299 | getInputChannelLocked(oldFocusedWindowToken); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5300 | if (inputChannel != nullptr) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5301 | CancelationOptions |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 5302 | options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5303 | "The display which contains this window no longer has focus."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5304 | options.displayId = ADISPLAY_ID_NONE; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5305 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
| 5306 | } |
| 5307 | } |
| 5308 | mFocusedDisplayId = displayId; |
| 5309 | |
Chris Ye | 3c2d6f5 | 2020-08-09 10:39:48 -0700 | [diff] [blame] | 5310 | // Find new focused window and validate |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5311 | sp<IBinder> newFocusedWindowToken = mFocusResolver.getFocusedWindowToken(displayId); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5312 | sendFocusChangedCommandLocked(oldFocusedWindowToken, newFocusedWindowToken); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5313 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5314 | if (newFocusedWindowToken == nullptr) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5315 | ALOGW("Focused display #%" PRId32 " does not have a focused window.", displayId); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5316 | if (mFocusResolver.hasFocusedWindowTokens()) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5317 | ALOGE("But another display has a focused window\n%s", |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5318 | mFocusResolver.dumpFocusedWindows().c_str()); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5319 | } |
| 5320 | } |
| 5321 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5322 | } // release lock |
| 5323 | |
| 5324 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5325 | mLooper->wake(); |
| 5326 | } |
| 5327 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5328 | void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5329 | if (DEBUG_FOCUS) { |
| 5330 | ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); |
| 5331 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5332 | |
| 5333 | bool changed; |
| 5334 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5335 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5336 | |
| 5337 | if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { |
| 5338 | if (mDispatchFrozen && !frozen) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5339 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5340 | } |
| 5341 | |
| 5342 | if (mDispatchEnabled && !enabled) { |
| 5343 | resetAndDropEverythingLocked("dispatcher is being disabled"); |
| 5344 | } |
| 5345 | |
| 5346 | mDispatchEnabled = enabled; |
| 5347 | mDispatchFrozen = frozen; |
| 5348 | changed = true; |
| 5349 | } else { |
| 5350 | changed = false; |
| 5351 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5352 | } // release lock |
| 5353 | |
| 5354 | if (changed) { |
| 5355 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5356 | mLooper->wake(); |
| 5357 | } |
| 5358 | } |
| 5359 | |
| 5360 | void InputDispatcher::setInputFilterEnabled(bool enabled) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5361 | if (DEBUG_FOCUS) { |
| 5362 | ALOGD("setInputFilterEnabled: enabled=%d", enabled); |
| 5363 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5364 | |
| 5365 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5366 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5367 | |
| 5368 | if (mInputFilterEnabled == enabled) { |
| 5369 | return; |
| 5370 | } |
| 5371 | |
| 5372 | mInputFilterEnabled = enabled; |
| 5373 | resetAndDropEverythingLocked("input filter is being enabled or disabled"); |
| 5374 | } // release lock |
| 5375 | |
| 5376 | // Wake up poll loop since there might be work to do to drop everything. |
| 5377 | mLooper->wake(); |
| 5378 | } |
| 5379 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5380 | bool InputDispatcher::setInTouchMode(bool inTouchMode, gui::Pid pid, gui::Uid uid, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 5381 | bool hasPermission, int32_t displayId) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5382 | bool needWake = false; |
| 5383 | { |
| 5384 | std::scoped_lock lock(mLock); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5385 | ALOGD_IF(DEBUG_TOUCH_MODE, |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5386 | "Request to change touch mode to %s (calling pid=%s, uid=%s, " |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5387 | "hasPermission=%s, target displayId=%d, mTouchModePerDisplay[displayId]=%s)", |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5388 | toString(inTouchMode), pid.toString().c_str(), uid.toString().c_str(), |
| 5389 | toString(hasPermission), displayId, |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5390 | mTouchModePerDisplay.count(displayId) == 0 |
| 5391 | ? "not set" |
| 5392 | : std::to_string(mTouchModePerDisplay[displayId]).c_str()); |
| 5393 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5394 | auto touchModeIt = mTouchModePerDisplay.find(displayId); |
| 5395 | if (touchModeIt != mTouchModePerDisplay.end() && touchModeIt->second == inTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5396 | return false; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5397 | } |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5398 | if (!hasPermission) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 5399 | if (!focusedWindowIsOwnedByLocked(pid, uid) && |
| 5400 | !recentWindowsAreOwnedByLocked(pid, uid)) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5401 | 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] | 5402 | "window nor none of the previously interacted window", |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5403 | pid.toString().c_str(), uid.toString().c_str()); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5404 | return false; |
| 5405 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5406 | } |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5407 | mTouchModePerDisplay[displayId] = inTouchMode; |
| 5408 | auto entry = std::make_unique<TouchModeEntry>(mIdGenerator.nextId(), now(), inTouchMode, |
| 5409 | displayId); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5410 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 5411 | } // release lock |
| 5412 | |
| 5413 | if (needWake) { |
| 5414 | mLooper->wake(); |
| 5415 | } |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5416 | return true; |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 5417 | } |
| 5418 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5419 | bool InputDispatcher::focusedWindowIsOwnedByLocked(gui::Pid pid, gui::Uid uid) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 5420 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
| 5421 | if (focusedToken == nullptr) { |
| 5422 | return false; |
| 5423 | } |
| 5424 | sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(focusedToken); |
| 5425 | return isWindowOwnedBy(windowHandle, pid, uid); |
| 5426 | } |
| 5427 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5428 | bool InputDispatcher::recentWindowsAreOwnedByLocked(gui::Pid pid, gui::Uid uid) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 5429 | return std::find_if(mInteractionConnectionTokens.begin(), mInteractionConnectionTokens.end(), |
| 5430 | [&](const sp<IBinder>& connectionToken) REQUIRES(mLock) { |
| 5431 | const sp<WindowInfoHandle> windowHandle = |
| 5432 | getWindowHandleLocked(connectionToken); |
| 5433 | return isWindowOwnedBy(windowHandle, pid, uid); |
| 5434 | }) != mInteractionConnectionTokens.end(); |
| 5435 | } |
| 5436 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 5437 | void InputDispatcher::setMaximumObscuringOpacityForTouch(float opacity) { |
| 5438 | if (opacity < 0 || opacity > 1) { |
| 5439 | LOG_ALWAYS_FATAL("Maximum obscuring opacity for touch should be >= 0 and <= 1"); |
| 5440 | return; |
| 5441 | } |
| 5442 | |
| 5443 | std::scoped_lock lock(mLock); |
| 5444 | mMaximumObscuringOpacityForTouch = opacity; |
| 5445 | } |
| 5446 | |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5447 | std::tuple<TouchState*, TouchedWindow*, int32_t /*displayId*/> |
| 5448 | InputDispatcher::findTouchStateWindowAndDisplayLocked(const sp<IBinder>& token) { |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5449 | for (auto& [displayId, state] : mTouchStatesByDisplay) { |
| 5450 | for (TouchedWindow& w : state.windows) { |
| 5451 | if (w.windowHandle->getToken() == token) { |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5452 | return std::make_tuple(&state, &w, displayId); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5453 | } |
| 5454 | } |
| 5455 | } |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5456 | return std::make_tuple(nullptr, nullptr, ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5457 | } |
| 5458 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5459 | bool InputDispatcher::transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken, |
| 5460 | bool isDragDrop) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 5461 | if (fromToken == toToken) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5462 | if (DEBUG_FOCUS) { |
| 5463 | ALOGD("Trivial transfer to same window."); |
| 5464 | } |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 5465 | return true; |
| 5466 | } |
| 5467 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5468 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5469 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5470 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5471 | // Find the target touch state and touched window by fromToken. |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5472 | auto [state, touchedWindow, displayId] = findTouchStateWindowAndDisplayLocked(fromToken); |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5473 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5474 | if (state == nullptr || touchedWindow == nullptr) { |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5475 | ALOGD("Touch transfer failed because from window is not being touched."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5476 | return false; |
| 5477 | } |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5478 | std::set<int32_t> deviceIds = touchedWindow->getTouchingDeviceIds(); |
| 5479 | if (deviceIds.size() != 1) { |
Siarhei Vishniakou | 827d1ac | 2023-07-21 16:37:51 -0700 | [diff] [blame] | 5480 | LOG(INFO) << "Can't transfer touch. Currently touching devices: " << dumpSet(deviceIds) |
| 5481 | << " for window: " << touchedWindow->dump(); |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5482 | return false; |
| 5483 | } |
| 5484 | const int32_t deviceId = *deviceIds.begin(); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5485 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5486 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(toToken, displayId); |
| 5487 | if (toWindowHandle == nullptr) { |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5488 | ALOGW("Cannot transfer touch because to window not found."); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5489 | return false; |
| 5490 | } |
| 5491 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5492 | if (DEBUG_FOCUS) { |
| 5493 | ALOGD("transferTouchFocus: fromWindowHandle=%s, toWindowHandle=%s", |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5494 | touchedWindow->windowHandle->getName().c_str(), |
| 5495 | toWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5496 | } |
| 5497 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5498 | // Erase old window. |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5499 | ftl::Flags<InputTarget::Flags> oldTargetFlags = touchedWindow->targetFlags; |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5500 | std::bitset<MAX_POINTER_ID + 1> pointerIds = touchedWindow->getTouchingPointers(deviceId); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5501 | sp<WindowInfoHandle> fromWindowHandle = touchedWindow->windowHandle; |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5502 | state->removeWindowByToken(fromToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5503 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5504 | // Add new window. |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 5505 | nsecs_t downTimeInTarget = now(); |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5506 | ftl::Flags<InputTarget::Flags> newTargetFlags = |
| 5507 | oldTargetFlags & (InputTarget::Flags::SPLIT | InputTarget::Flags::DISPATCH_AS_IS); |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 5508 | if (canReceiveForegroundTouches(*toWindowHandle->getInfo())) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5509 | newTargetFlags |= InputTarget::Flags::FOREGROUND; |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 5510 | } |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5511 | state->addOrUpdateWindow(toWindowHandle, newTargetFlags, deviceId, pointerIds, |
| 5512 | downTimeInTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5513 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5514 | // Store the dragging window. |
| 5515 | if (isDragDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 5516 | if (pointerIds.count() != 1) { |
| 5517 | ALOGW("The drag and drop cannot be started when there is no pointer or more than 1" |
| 5518 | " pointer on the window."); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 5519 | return false; |
| 5520 | } |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 5521 | // Track the pointer id for drag window and generate the drag state. |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 5522 | const size_t id = firstMarkedBit(pointerIds); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 5523 | mDragState = std::make_unique<DragState>(toWindowHandle, id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5524 | } |
| 5525 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5526 | // Synthesize cancel for old window and down for new window. |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 5527 | std::shared_ptr<Connection> fromConnection = getConnectionLocked(fromToken); |
| 5528 | std::shared_ptr<Connection> toConnection = getConnectionLocked(toToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5529 | if (fromConnection != nullptr && toConnection != nullptr) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5530 | fromConnection->inputState.mergePointerStateTo(toConnection->inputState); |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5531 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
| 5532 | "transferring touch from this window to another window"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5533 | synthesizeCancelationEventsForConnectionLocked(fromConnection, options); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5534 | synthesizePointerDownEventsForConnectionLocked(downTimeInTarget, toConnection, |
| 5535 | newTargetFlags); |
| 5536 | |
| 5537 | // Check if the wallpaper window should deliver the corresponding event. |
| 5538 | transferWallpaperTouch(oldTargetFlags, newTargetFlags, fromWindowHandle, toWindowHandle, |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5539 | *state, deviceId, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5540 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5541 | } // release lock |
| 5542 | |
| 5543 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5544 | mLooper->wake(); |
| 5545 | return true; |
| 5546 | } |
| 5547 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5548 | /** |
| 5549 | * Get the touched foreground window on the given display. |
| 5550 | * Return null if there are no windows touched on that display, or if more than one foreground |
| 5551 | * window is being touched. |
| 5552 | */ |
| 5553 | sp<WindowInfoHandle> InputDispatcher::findTouchedForegroundWindowLocked(int32_t displayId) const { |
| 5554 | auto stateIt = mTouchStatesByDisplay.find(displayId); |
| 5555 | if (stateIt == mTouchStatesByDisplay.end()) { |
| 5556 | ALOGI("No touch state on display %" PRId32, displayId); |
| 5557 | return nullptr; |
| 5558 | } |
| 5559 | |
| 5560 | const TouchState& state = stateIt->second; |
| 5561 | sp<WindowInfoHandle> touchedForegroundWindow; |
| 5562 | // If multiple foreground windows are touched, return nullptr |
| 5563 | for (const TouchedWindow& window : state.windows) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5564 | if (window.targetFlags.test(InputTarget::Flags::FOREGROUND)) { |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5565 | if (touchedForegroundWindow != nullptr) { |
| 5566 | ALOGI("Two or more foreground windows: %s and %s", |
| 5567 | touchedForegroundWindow->getName().c_str(), |
| 5568 | window.windowHandle->getName().c_str()); |
| 5569 | return nullptr; |
| 5570 | } |
| 5571 | touchedForegroundWindow = window.windowHandle; |
| 5572 | } |
| 5573 | } |
| 5574 | return touchedForegroundWindow; |
| 5575 | } |
| 5576 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5577 | // Binder call |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5578 | bool InputDispatcher::transferTouch(const sp<IBinder>& destChannelToken, int32_t displayId) { |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5579 | sp<IBinder> fromToken; |
| 5580 | { // acquire lock |
| 5581 | std::scoped_lock _l(mLock); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5582 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(destChannelToken, displayId); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5583 | if (toWindowHandle == nullptr) { |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5584 | ALOGW("Could not find window associated with token=%p on display %" PRId32, |
| 5585 | destChannelToken.get(), displayId); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5586 | return false; |
| 5587 | } |
| 5588 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5589 | sp<WindowInfoHandle> from = findTouchedForegroundWindowLocked(displayId); |
| 5590 | if (from == nullptr) { |
| 5591 | ALOGE("Could not find a source window in %s for %p", __func__, destChannelToken.get()); |
| 5592 | return false; |
| 5593 | } |
| 5594 | |
| 5595 | fromToken = from->getToken(); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5596 | } // release lock |
| 5597 | |
| 5598 | return transferTouchFocus(fromToken, destChannelToken); |
| 5599 | } |
| 5600 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5601 | void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5602 | if (DEBUG_FOCUS) { |
| 5603 | ALOGD("Resetting and dropping all events (%s).", reason); |
| 5604 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5605 | |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 5606 | CancelationOptions options(CancelationOptions::Mode::CANCEL_ALL_EVENTS, reason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5607 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 5608 | |
| 5609 | resetKeyRepeatLocked(); |
| 5610 | releasePendingEventLocked(); |
| 5611 | drainInboundQueueLocked(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5612 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5613 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5614 | mAnrTracker.clear(); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5615 | mTouchStatesByDisplay.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5616 | } |
| 5617 | |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 5618 | void InputDispatcher::logDispatchStateLocked() const { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5619 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5620 | dumpDispatchStateLocked(dump); |
| 5621 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5622 | std::istringstream stream(dump); |
| 5623 | std::string line; |
| 5624 | |
| 5625 | while (std::getline(stream, line, '\n')) { |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 5626 | ALOGI("%s", line.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5627 | } |
| 5628 | } |
| 5629 | |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 5630 | std::string InputDispatcher::dumpPointerCaptureStateLocked() const { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5631 | std::string dump; |
| 5632 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5633 | dump += StringPrintf(INDENT "Pointer Capture Requested: %s\n", |
| 5634 | toString(mCurrentPointerCaptureRequest.enable)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5635 | |
| 5636 | std::string windowName = "None"; |
| 5637 | if (mWindowTokenWithPointerCapture) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5638 | const sp<WindowInfoHandle> captureWindowHandle = |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5639 | getWindowHandleLocked(mWindowTokenWithPointerCapture); |
| 5640 | windowName = captureWindowHandle ? captureWindowHandle->getName().c_str() |
| 5641 | : "token has capture without window"; |
| 5642 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5643 | 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] | 5644 | |
| 5645 | return dump; |
| 5646 | } |
| 5647 | |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 5648 | void InputDispatcher::dumpDispatchStateLocked(std::string& dump) const { |
Siarhei Vishniakou | 043a3ec | 2019-05-01 11:30:46 -0700 | [diff] [blame] | 5649 | dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled)); |
| 5650 | dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen)); |
| 5651 | dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5652 | dump += StringPrintf(INDENT "FocusedDisplayId: %" PRId32 "\n", mFocusedDisplayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5653 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5654 | if (!mFocusedApplicationHandlesByDisplay.empty()) { |
| 5655 | dump += StringPrintf(INDENT "FocusedApplications:\n"); |
| 5656 | for (auto& it : mFocusedApplicationHandlesByDisplay) { |
| 5657 | const int32_t displayId = it.first; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5658 | const std::shared_ptr<InputApplicationHandle>& applicationHandle = it.second; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5659 | const std::chrono::duration timeout = |
| 5660 | applicationHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5661 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5662 | ", name='%s', dispatchingTimeout=%" PRId64 "ms\n", |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5663 | displayId, applicationHandle->getName().c_str(), millis(timeout)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5664 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5665 | } else { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5666 | dump += StringPrintf(INDENT "FocusedApplications: <none>\n"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5667 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5668 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5669 | dump += mFocusResolver.dump(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5670 | dump += dumpPointerCaptureStateLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5671 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5672 | if (!mTouchStatesByDisplay.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5673 | dump += StringPrintf(INDENT "TouchStatesByDisplay:\n"); |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5674 | for (const auto& [displayId, state] : mTouchStatesByDisplay) { |
Siarhei Vishniakou | 6e1e987 | 2022-11-08 17:51:35 -0800 | [diff] [blame] | 5675 | std::string touchStateDump = addLinePrefix(state.dump(), INDENT2); |
| 5676 | dump += INDENT2 + std::to_string(displayId) + " : " + touchStateDump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5677 | } |
| 5678 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5679 | dump += INDENT "TouchStates: <no displays touched>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5680 | } |
| 5681 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5682 | if (mDragState) { |
| 5683 | dump += StringPrintf(INDENT "DragState:\n"); |
| 5684 | mDragState->dump(dump, INDENT2); |
| 5685 | } |
| 5686 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5687 | if (!mWindowHandlesByDisplay.empty()) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5688 | for (const auto& [displayId, windowHandles] : mWindowHandlesByDisplay) { |
| 5689 | dump += StringPrintf(INDENT "Display: %" PRId32 "\n", displayId); |
| 5690 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
| 5691 | const auto& displayInfo = it->second; |
| 5692 | dump += StringPrintf(INDENT2 "logicalSize=%dx%d\n", displayInfo.logicalWidth, |
| 5693 | displayInfo.logicalHeight); |
| 5694 | displayInfo.transform.dump(dump, "transform", INDENT4); |
| 5695 | } else { |
| 5696 | dump += INDENT2 "No DisplayInfo found!\n"; |
| 5697 | } |
| 5698 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5699 | if (!windowHandles.empty()) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5700 | dump += INDENT2 "Windows:\n"; |
| 5701 | for (size_t i = 0; i < windowHandles.size(); i++) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5702 | const sp<WindowInfoHandle>& windowHandle = windowHandles[i]; |
| 5703 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5704 | |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5705 | dump += StringPrintf(INDENT3 "%zu: name='%s', id=%" PRId32 ", displayId=%d, " |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5706 | "inputConfig=%s, alpha=%.2f, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5707 | "frame=[%d,%d][%d,%d], globalScale=%f, " |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5708 | "applicationInfo.name=%s, " |
| 5709 | "applicationInfo.token=%s, " |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 5710 | "touchableRegion=", |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5711 | i, windowInfo->name.c_str(), windowInfo->id, |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5712 | windowInfo->displayId, |
| 5713 | windowInfo->inputConfig.string().c_str(), |
Chavi Weingarten | 7f01919 | 2023-08-08 20:39:01 +0000 | [diff] [blame] | 5714 | windowInfo->alpha, windowInfo->frame.left, |
| 5715 | windowInfo->frame.top, windowInfo->frame.right, |
| 5716 | windowInfo->frame.bottom, windowInfo->globalScaleFactor, |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5717 | windowInfo->applicationInfo.name.c_str(), |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 5718 | binderToString(windowInfo->applicationInfo.token).c_str()); |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 5719 | dump += dumpRegion(windowInfo->touchableRegion); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5720 | dump += StringPrintf(", ownerPid=%s, ownerUid=%s, dispatchingTimeout=%" PRId64 |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5721 | "ms, hasToken=%s, " |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5722 | "touchOcclusionMode=%s\n", |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5723 | windowInfo->ownerPid.toString().c_str(), |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 5724 | windowInfo->ownerUid.toString().c_str(), |
Bernardo Rufino | c2f1fad | 2020-11-04 17:30:57 +0000 | [diff] [blame] | 5725 | millis(windowInfo->dispatchingTimeout), |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 5726 | binderToString(windowInfo->token).c_str(), |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5727 | toString(windowInfo->touchOcclusionMode).c_str()); |
chaviw | 85b4420 | 2020-07-24 11:46:21 -0700 | [diff] [blame] | 5728 | windowInfo->transform.dump(dump, "transform", INDENT4); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5729 | } |
| 5730 | } else { |
| 5731 | dump += INDENT2 "Windows: <none>\n"; |
| 5732 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5733 | } |
| 5734 | } else { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5735 | dump += INDENT "Displays: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5736 | } |
| 5737 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5738 | if (!mGlobalMonitorsByDisplay.empty()) { |
| 5739 | for (const auto& [displayId, monitors] : mGlobalMonitorsByDisplay) { |
| 5740 | dump += StringPrintf(INDENT "Global monitors on display %d:\n", displayId); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5741 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5742 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5743 | } else { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5744 | dump += INDENT "Global Monitors: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5745 | } |
| 5746 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5747 | const nsecs_t currentTime = now(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5748 | |
| 5749 | // Dump recently dispatched or dropped events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5750 | if (!mRecentQueue.empty()) { |
| 5751 | dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size()); |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 5752 | for (const std::shared_ptr<const EventEntry>& entry : mRecentQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5753 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5754 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5755 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5756 | } |
| 5757 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5758 | dump += INDENT "RecentQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5759 | } |
| 5760 | |
| 5761 | // Dump event currently being dispatched. |
| 5762 | if (mPendingEvent) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5763 | dump += INDENT "PendingEvent:\n"; |
| 5764 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5765 | dump += mPendingEvent->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5766 | dump += StringPrintf(", age=%" PRId64 "ms\n", |
| 5767 | ns2ms(currentTime - mPendingEvent->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5768 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5769 | dump += INDENT "PendingEvent: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5770 | } |
| 5771 | |
| 5772 | // Dump inbound events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5773 | if (!mInboundQueue.empty()) { |
| 5774 | dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size()); |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 5775 | for (const std::shared_ptr<const EventEntry>& entry : mInboundQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5776 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5777 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5778 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5779 | } |
| 5780 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5781 | dump += INDENT "InboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5782 | } |
| 5783 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5784 | if (!mCommandQueue.empty()) { |
| 5785 | dump += StringPrintf(INDENT "CommandQueue: size=%zu\n", mCommandQueue.size()); |
| 5786 | } else { |
| 5787 | dump += INDENT "CommandQueue: <empty>\n"; |
| 5788 | } |
| 5789 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5790 | if (!mConnectionsByToken.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5791 | dump += INDENT "Connections:\n"; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5792 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5793 | dump += StringPrintf(INDENT2 "%i: channelName='%s', windowName='%s', " |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5794 | "status=%s, monitor=%s, responsive=%s\n", |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5795 | connection->inputChannel->getFd().get(), |
| 5796 | connection->getInputChannelName().c_str(), |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5797 | connection->getWindowName().c_str(), |
| 5798 | ftl::enum_string(connection->status).c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5799 | toString(connection->monitor), toString(connection->responsive)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5800 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5801 | if (!connection->outboundQueue.empty()) { |
| 5802 | dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n", |
| 5803 | connection->outboundQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5804 | dump += dumpQueue(connection->outboundQueue, currentTime); |
| 5805 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5806 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5807 | dump += INDENT3 "OutboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5808 | } |
| 5809 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5810 | if (!connection->waitQueue.empty()) { |
| 5811 | dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n", |
| 5812 | connection->waitQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5813 | dump += dumpQueue(connection->waitQueue, currentTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5814 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5815 | dump += INDENT3 "WaitQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5816 | } |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 5817 | std::stringstream inputStateDump; |
| 5818 | inputStateDump << connection->inputState; |
| 5819 | if (!isEmpty(inputStateDump)) { |
| 5820 | dump += INDENT3 "InputState: "; |
| 5821 | dump += inputStateDump.str() + "\n"; |
| 5822 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5823 | } |
| 5824 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5825 | dump += INDENT "Connections: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5826 | } |
| 5827 | |
Siarhei Vishniakou | 6520a58 | 2023-10-27 21:53:45 -0700 | [diff] [blame] | 5828 | dump += "input_flags::remove_app_switch_drops() = "; |
Siarhei Vishniakou | 17f1228 | 2023-11-02 13:40:29 -0700 | [diff] [blame] | 5829 | dump += toString(REMOVE_APP_SWITCH_DROPS); |
Siarhei Vishniakou | 6520a58 | 2023-10-27 21:53:45 -0700 | [diff] [blame] | 5830 | dump += "\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5831 | if (isAppSwitchPendingLocked()) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5832 | dump += StringPrintf(INDENT "AppSwitch: pending, due in %" PRId64 "ms\n", |
| 5833 | ns2ms(mAppSwitchDueTime - now())); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5834 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5835 | dump += INDENT "AppSwitch: not pending\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5836 | } |
| 5837 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5838 | if (!mTouchModePerDisplay.empty()) { |
| 5839 | dump += INDENT "TouchModePerDisplay:\n"; |
| 5840 | for (const auto& [displayId, touchMode] : mTouchModePerDisplay) { |
| 5841 | dump += StringPrintf(INDENT2 "Display: %" PRId32 " TouchMode: %s\n", displayId, |
| 5842 | std::to_string(touchMode).c_str()); |
| 5843 | } |
| 5844 | } else { |
| 5845 | dump += INDENT "TouchModePerDisplay: <none>\n"; |
| 5846 | } |
| 5847 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5848 | dump += INDENT "Configuration:\n"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5849 | dump += StringPrintf(INDENT2 "KeyRepeatDelay: %" PRId64 "ms\n", ns2ms(mConfig.keyRepeatDelay)); |
| 5850 | dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n", |
| 5851 | ns2ms(mConfig.keyRepeatTimeout)); |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 5852 | dump += mLatencyTracker.dump(INDENT2); |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 5853 | dump += mLatencyAggregator.dump(INDENT2); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5854 | } |
| 5855 | |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 5856 | void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) const { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5857 | const size_t numMonitors = monitors.size(); |
| 5858 | for (size_t i = 0; i < numMonitors; i++) { |
| 5859 | const Monitor& monitor = monitors[i]; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5860 | const std::shared_ptr<InputChannel>& channel = monitor.inputChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5861 | dump += StringPrintf(INDENT2 "%zu: '%s', ", i, channel->getName().c_str()); |
| 5862 | dump += "\n"; |
| 5863 | } |
| 5864 | } |
| 5865 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5866 | class LooperEventCallback : public LooperCallback { |
| 5867 | public: |
| 5868 | LooperEventCallback(std::function<int(int events)> callback) : mCallback(callback) {} |
| 5869 | int handleEvent(int /*fd*/, int events, void* /*data*/) override { return mCallback(events); } |
| 5870 | |
| 5871 | private: |
| 5872 | std::function<int(int events)> mCallback; |
| 5873 | }; |
| 5874 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5875 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputChannel(const std::string& name) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5876 | if (DEBUG_CHANNEL_CREATION) { |
| 5877 | ALOGD("channel '%s' ~ createInputChannel", name.c_str()); |
| 5878 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5879 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5880 | std::unique_ptr<InputChannel> serverChannel; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5881 | std::unique_ptr<InputChannel> clientChannel; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5882 | status_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5883 | |
| 5884 | if (result) { |
| 5885 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5886 | } |
| 5887 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5888 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5889 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5890 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5891 | int fd = serverChannel->getFd(); |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 5892 | std::shared_ptr<Connection> connection = |
| 5893 | std::make_shared<Connection>(std::move(serverChannel), /*monitor=*/false, |
| 5894 | mIdGenerator); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5895 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5896 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5897 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5898 | } |
| 5899 | mConnectionsByToken.emplace(token, connection); |
| 5900 | |
| 5901 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5902 | this, std::placeholders::_1, token); |
| 5903 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5904 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, sp<LooperEventCallback>::make(callback), |
| 5905 | nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5906 | } // release lock |
| 5907 | |
| 5908 | // Wake the looper because some connections have changed. |
| 5909 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5910 | return clientChannel; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5911 | } |
| 5912 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5913 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputMonitor(int32_t displayId, |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5914 | const std::string& name, |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5915 | gui::Pid pid) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5916 | std::shared_ptr<InputChannel> serverChannel; |
| 5917 | std::unique_ptr<InputChannel> clientChannel; |
| 5918 | status_t result = openInputChannelPair(name, serverChannel, clientChannel); |
| 5919 | if (result) { |
| 5920 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5921 | } |
| 5922 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5923 | { // acquire lock |
| 5924 | std::scoped_lock _l(mLock); |
| 5925 | |
| 5926 | if (displayId < 0) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5927 | return base::Error(BAD_VALUE) << "Attempted to create input monitor with name " << name |
| 5928 | << " without a specified display."; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5929 | } |
| 5930 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 5931 | std::shared_ptr<Connection> connection = |
| 5932 | std::make_shared<Connection>(serverChannel, /*monitor=*/true, mIdGenerator); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5933 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5934 | const int fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5935 | |
| 5936 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5937 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5938 | } |
| 5939 | mConnectionsByToken.emplace(token, connection); |
| 5940 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5941 | this, std::placeholders::_1, token); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5942 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5943 | mGlobalMonitorsByDisplay[displayId].emplace_back(serverChannel, pid); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5944 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5945 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, sp<LooperEventCallback>::make(callback), |
| 5946 | nullptr); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5947 | } |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5948 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5949 | // Wake the looper because some connections have changed. |
| 5950 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5951 | return clientChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5952 | } |
| 5953 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5954 | status_t InputDispatcher::removeInputChannel(const sp<IBinder>& connectionToken) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5955 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5956 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5957 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5958 | status_t status = removeInputChannelLocked(connectionToken, /*notify=*/false); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5959 | if (status) { |
| 5960 | return status; |
| 5961 | } |
| 5962 | } // release lock |
| 5963 | |
| 5964 | // Wake the poll loop because removing the connection may have changed the current |
| 5965 | // synchronization state. |
| 5966 | mLooper->wake(); |
| 5967 | return OK; |
| 5968 | } |
| 5969 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5970 | status_t InputDispatcher::removeInputChannelLocked(const sp<IBinder>& connectionToken, |
| 5971 | bool notify) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 5972 | std::shared_ptr<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5973 | if (connection == nullptr) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5974 | // 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] | 5975 | return BAD_VALUE; |
| 5976 | } |
| 5977 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5978 | removeConnectionLocked(connection); |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 5979 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5980 | if (connection->monitor) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5981 | removeMonitorChannelLocked(connectionToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5982 | } |
| 5983 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5984 | mLooper->removeFd(connection->inputChannel->getFd()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5985 | |
| 5986 | nsecs_t currentTime = now(); |
| 5987 | abortBrokenDispatchCycleLocked(currentTime, connection, notify); |
| 5988 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5989 | connection->status = Connection::Status::ZOMBIE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5990 | return OK; |
| 5991 | } |
| 5992 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5993 | void InputDispatcher::removeMonitorChannelLocked(const sp<IBinder>& connectionToken) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5994 | for (auto it = mGlobalMonitorsByDisplay.begin(); it != mGlobalMonitorsByDisplay.end();) { |
| 5995 | auto& [displayId, monitors] = *it; |
| 5996 | std::erase_if(monitors, [connectionToken](const Monitor& monitor) { |
| 5997 | return monitor.inputChannel->getConnectionToken() == connectionToken; |
| 5998 | }); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5999 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6000 | if (monitors.empty()) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6001 | it = mGlobalMonitorsByDisplay.erase(it); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6002 | } else { |
| 6003 | ++it; |
| 6004 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6005 | } |
| 6006 | } |
| 6007 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6008 | status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6009 | std::scoped_lock _l(mLock); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 6010 | return pilferPointersLocked(token); |
| 6011 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6012 | |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 6013 | status_t InputDispatcher::pilferPointersLocked(const sp<IBinder>& token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6014 | const std::shared_ptr<InputChannel> requestingChannel = getInputChannelLocked(token); |
| 6015 | if (!requestingChannel) { |
Siarhei Vishniakou | 8384e0d | 2023-09-18 18:48:27 -0700 | [diff] [blame] | 6016 | LOG(WARNING) |
| 6017 | << "Attempted to pilfer pointers from an un-registered channel or invalid token"; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6018 | return BAD_VALUE; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6019 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6020 | |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 6021 | auto [statePtr, windowPtr, displayId] = findTouchStateWindowAndDisplayLocked(token); |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 6022 | if (statePtr == nullptr || windowPtr == nullptr) { |
Siarhei Vishniakou | 8384e0d | 2023-09-18 18:48:27 -0700 | [diff] [blame] | 6023 | LOG(WARNING) |
| 6024 | << "Attempted to pilfer points from a channel without any on-going pointer streams." |
| 6025 | " Ignoring."; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6026 | return BAD_VALUE; |
| 6027 | } |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 6028 | std::set<int32_t> deviceIds = windowPtr->getTouchingDeviceIds(); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 6029 | if (deviceIds.empty()) { |
| 6030 | LOG(WARNING) << "Can't pilfer: no touching devices in window: " << windowPtr->dump(); |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 6031 | return BAD_VALUE; |
| 6032 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6033 | |
Siarhei Vishniakou | 8384e0d | 2023-09-18 18:48:27 -0700 | [diff] [blame] | 6034 | for (const DeviceId deviceId : deviceIds) { |
| 6035 | TouchState& state = *statePtr; |
| 6036 | TouchedWindow& window = *windowPtr; |
| 6037 | // Send cancel events to all the input channels we're stealing from. |
| 6038 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
| 6039 | "input channel stole pointer stream"); |
| 6040 | options.deviceId = deviceId; |
| 6041 | options.displayId = displayId; |
| 6042 | std::bitset<MAX_POINTER_ID + 1> pointerIds = window.getTouchingPointers(deviceId); |
| 6043 | options.pointerIds = pointerIds; |
| 6044 | std::string canceledWindows; |
| 6045 | for (const TouchedWindow& w : state.windows) { |
| 6046 | const std::shared_ptr<InputChannel> channel = |
| 6047 | getInputChannelLocked(w.windowHandle->getToken()); |
| 6048 | if (channel != nullptr && channel->getConnectionToken() != token) { |
| 6049 | synthesizeCancelationEventsForInputChannelLocked(channel, options); |
| 6050 | canceledWindows += canceledWindows.empty() ? "[" : ", "; |
| 6051 | canceledWindows += channel->getName(); |
| 6052 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6053 | } |
Siarhei Vishniakou | 8384e0d | 2023-09-18 18:48:27 -0700 | [diff] [blame] | 6054 | canceledWindows += canceledWindows.empty() ? "[]" : "]"; |
| 6055 | LOG(INFO) << "Channel " << requestingChannel->getName() |
| 6056 | << " is stealing input gesture for device " << deviceId << " from " |
| 6057 | << canceledWindows; |
| 6058 | |
| 6059 | // Prevent the gesture from being sent to any other windows. |
| 6060 | // This only blocks relevant pointers to be sent to other windows |
| 6061 | window.addPilferingPointers(deviceId, pointerIds); |
| 6062 | |
| 6063 | state.cancelPointersForWindowsExcept(deviceId, pointerIds, token); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6064 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6065 | return OK; |
| 6066 | } |
| 6067 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6068 | void InputDispatcher::requestPointerCapture(const sp<IBinder>& windowToken, bool enabled) { |
| 6069 | { // acquire lock |
| 6070 | std::scoped_lock _l(mLock); |
| 6071 | if (DEBUG_FOCUS) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 6072 | const sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(windowToken); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6073 | ALOGI("Request to %s Pointer Capture from: %s.", enabled ? "enable" : "disable", |
| 6074 | windowHandle != nullptr ? windowHandle->getName().c_str() |
| 6075 | : "token without window"); |
| 6076 | } |
| 6077 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6078 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6079 | if (focusedToken != windowToken) { |
| 6080 | ALOGW("Ignoring request to %s Pointer Capture: window does not have focus.", |
| 6081 | enabled ? "enable" : "disable"); |
| 6082 | return; |
| 6083 | } |
| 6084 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6085 | if (enabled == mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6086 | ALOGW("Ignoring request to %s Pointer Capture: " |
| 6087 | "window has %s requested pointer capture.", |
| 6088 | enabled ? "enable" : "disable", enabled ? "already" : "not"); |
| 6089 | return; |
| 6090 | } |
| 6091 | |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 6092 | if (enabled) { |
| 6093 | if (std::find(mIneligibleDisplaysForPointerCapture.begin(), |
| 6094 | mIneligibleDisplaysForPointerCapture.end(), |
| 6095 | mFocusedDisplayId) != mIneligibleDisplaysForPointerCapture.end()) { |
| 6096 | ALOGW("Ignoring request to enable Pointer Capture: display is not eligible"); |
| 6097 | return; |
| 6098 | } |
| 6099 | } |
| 6100 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6101 | setPointerCaptureLocked(enabled); |
| 6102 | } // release lock |
| 6103 | |
| 6104 | // Wake the thread to process command entries. |
| 6105 | mLooper->wake(); |
| 6106 | } |
| 6107 | |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 6108 | void InputDispatcher::setDisplayEligibilityForPointerCapture(int32_t displayId, bool isEligible) { |
| 6109 | { // acquire lock |
| 6110 | std::scoped_lock _l(mLock); |
| 6111 | std::erase(mIneligibleDisplaysForPointerCapture, displayId); |
| 6112 | if (!isEligible) { |
| 6113 | mIneligibleDisplaysForPointerCapture.push_back(displayId); |
| 6114 | } |
| 6115 | } // release lock |
| 6116 | } |
| 6117 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6118 | std::optional<gui::Pid> InputDispatcher::findMonitorPidByTokenLocked(const sp<IBinder>& token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6119 | for (const auto& [_, monitors] : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6120 | for (const Monitor& monitor : monitors) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 6121 | if (monitor.inputChannel->getConnectionToken() == token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6122 | return monitor.pid; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6123 | } |
| 6124 | } |
| 6125 | } |
| 6126 | return std::nullopt; |
| 6127 | } |
| 6128 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6129 | std::shared_ptr<Connection> InputDispatcher::getConnectionLocked( |
| 6130 | const sp<IBinder>& inputConnectionToken) const { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 6131 | if (inputConnectionToken == nullptr) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 6132 | return nullptr; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 6133 | } |
| 6134 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 6135 | for (const auto& [token, connection] : mConnectionsByToken) { |
| 6136 | if (token == inputConnectionToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 6137 | return connection; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6138 | } |
| 6139 | } |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 6140 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 6141 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6142 | } |
| 6143 | |
Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 6144 | std::string InputDispatcher::getConnectionNameLocked(const sp<IBinder>& connectionToken) const { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6145 | std::shared_ptr<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 6146 | if (connection == nullptr) { |
| 6147 | return "<nullptr>"; |
| 6148 | } |
| 6149 | return connection->getInputChannelName(); |
| 6150 | } |
| 6151 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6152 | void InputDispatcher::removeConnectionLocked(const std::shared_ptr<Connection>& connection) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6153 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 6154 | mConnectionsByToken.erase(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6155 | } |
| 6156 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6157 | void InputDispatcher::doDispatchCycleFinishedCommand(nsecs_t finishTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6158 | const std::shared_ptr<Connection>& connection, |
| 6159 | uint32_t seq, bool handled, |
| 6160 | nsecs_t consumeTime) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6161 | // Handle post-event policy actions. |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 6162 | std::unique_ptr<const KeyEntry> fallbackKeyEntry; |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6163 | |
| 6164 | { // Start critical section |
| 6165 | auto dispatchEntryIt = |
| 6166 | std::find_if(connection->waitQueue.begin(), connection->waitQueue.end(), |
| 6167 | [seq](auto& e) { return e->seq == seq; }); |
| 6168 | if (dispatchEntryIt == connection->waitQueue.end()) { |
| 6169 | return; |
| 6170 | } |
| 6171 | |
| 6172 | DispatchEntry& dispatchEntry = **dispatchEntryIt; |
| 6173 | |
| 6174 | const nsecs_t eventDuration = finishTime - dispatchEntry.deliveryTime; |
| 6175 | if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) { |
| 6176 | ALOGI("%s spent %" PRId64 "ms processing %s", connection->getWindowName().c_str(), |
| 6177 | ns2ms(eventDuration), dispatchEntry.eventEntry->getDescription().c_str()); |
| 6178 | } |
| 6179 | if (shouldReportFinishedEvent(dispatchEntry, *connection)) { |
| 6180 | mLatencyTracker.trackFinishedEvent(dispatchEntry.eventEntry->id, |
| 6181 | connection->inputChannel->getConnectionToken(), |
| 6182 | dispatchEntry.deliveryTime, consumeTime, finishTime); |
| 6183 | } |
| 6184 | |
| 6185 | if (dispatchEntry.eventEntry->type == EventEntry::Type::KEY) { |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6186 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(*(dispatchEntry.eventEntry)); |
| 6187 | fallbackKeyEntry = |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6188 | afterKeyEventLockedInterruptable(connection, dispatchEntry, keyEntry, handled); |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6189 | } |
| 6190 | } // End critical section: The -LockedInterruptable methods may have released the lock. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6191 | |
| 6192 | // Dequeue the event and start the next cycle. |
| 6193 | // Because the lock might have been released, it is possible that the |
| 6194 | // contents of the wait queue to have been drained, so we need to double-check |
| 6195 | // a few things. |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6196 | auto entryIt = std::find_if(connection->waitQueue.begin(), connection->waitQueue.end(), |
| 6197 | [seq](auto& e) { return e->seq == seq; }); |
| 6198 | if (entryIt != connection->waitQueue.end()) { |
| 6199 | std::unique_ptr<DispatchEntry> dispatchEntry = std::move(*entryIt); |
| 6200 | connection->waitQueue.erase(entryIt); |
| 6201 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6202 | const sp<IBinder>& connectionToken = connection->inputChannel->getConnectionToken(); |
| 6203 | mAnrTracker.erase(dispatchEntry->timeoutTime, connectionToken); |
| 6204 | if (!connection->responsive) { |
| 6205 | connection->responsive = isConnectionResponsive(*connection); |
| 6206 | if (connection->responsive) { |
| 6207 | // The connection was unresponsive, and now it's responsive. |
| 6208 | processConnectionResponsiveLocked(*connection); |
| 6209 | } |
| 6210 | } |
| 6211 | traceWaitQueueLength(*connection); |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6212 | if (fallbackKeyEntry && connection->status == Connection::Status::NORMAL) { |
| 6213 | const InputTarget target{.inputChannel = connection->inputChannel, |
| 6214 | .flags = dispatchEntry->targetFlags}; |
| 6215 | enqueueDispatchEntryLocked(connection, std::move(fallbackKeyEntry), target, |
| 6216 | InputTarget::Flags::DISPATCH_AS_IS); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6217 | } |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6218 | releaseDispatchEntry(std::move(dispatchEntry)); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6219 | } |
| 6220 | |
| 6221 | // Start the next dispatch cycle for this connection. |
| 6222 | startDispatchCycleLocked(now(), connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6223 | } |
| 6224 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6225 | void InputDispatcher::sendFocusChangedCommandLocked(const sp<IBinder>& oldToken, |
| 6226 | const sp<IBinder>& newToken) { |
| 6227 | auto command = [this, oldToken, newToken]() REQUIRES(mLock) { |
| 6228 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6229 | mPolicy.notifyFocusChanged(oldToken, newToken); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6230 | }; |
| 6231 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6232 | } |
| 6233 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6234 | void InputDispatcher::sendDropWindowCommandLocked(const sp<IBinder>& token, float x, float y) { |
| 6235 | auto command = [this, token, x, y]() REQUIRES(mLock) { |
| 6236 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6237 | mPolicy.notifyDropWindow(token, x, y); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6238 | }; |
| 6239 | postCommandLocked(std::move(command)); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 6240 | } |
| 6241 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6242 | void InputDispatcher::onAnrLocked(const std::shared_ptr<Connection>& connection) { |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6243 | if (connection == nullptr) { |
| 6244 | LOG_ALWAYS_FATAL("Caller must check for nullness"); |
| 6245 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6246 | // Since we are allowing the policy to extend the timeout, maybe the waitQueue |
| 6247 | // is already healthy again. Don't raise ANR in this situation |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6248 | if (connection->waitQueue.empty()) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6249 | ALOGI("Not raising ANR because the connection %s has recovered", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6250 | connection->inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6251 | return; |
| 6252 | } |
| 6253 | /** |
| 6254 | * The "oldestEntry" is the entry that was first sent to the application. That entry, however, |
| 6255 | * may not be the one that caused the timeout to occur. One possibility is that window timeout |
| 6256 | * has changed. This could cause newer entries to time out before the already dispatched |
| 6257 | * entries. In that situation, the newest entries caused ANR. But in all likelihood, the app |
| 6258 | * processes the events linearly. So providing information about the oldest entry seems to be |
| 6259 | * most useful. |
| 6260 | */ |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6261 | DispatchEntry& oldestEntry = *connection->waitQueue.front(); |
| 6262 | const nsecs_t currentWait = now() - oldestEntry.deliveryTime; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6263 | std::string reason = |
| 6264 | android::base::StringPrintf("%s is not responding. Waited %" PRId64 "ms for %s", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6265 | connection->inputChannel->getName().c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6266 | ns2ms(currentWait), |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6267 | oldestEntry.eventEntry->getDescription().c_str()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6268 | sp<IBinder> connectionToken = connection->inputChannel->getConnectionToken(); |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 6269 | updateLastAnrStateLocked(getWindowHandleLocked(connectionToken), reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6270 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6271 | processConnectionUnresponsiveLocked(*connection, std::move(reason)); |
| 6272 | |
| 6273 | // Stop waking up for events on this connection, it is already unresponsive |
| 6274 | cancelEventsForAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6275 | } |
| 6276 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6277 | void InputDispatcher::onAnrLocked(std::shared_ptr<InputApplicationHandle> application) { |
| 6278 | std::string reason = |
| 6279 | StringPrintf("%s does not have a focused window", application->getName().c_str()); |
| 6280 | updateLastAnrStateLocked(*application, reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6281 | |
Yabin Cui | 8eb9c55 | 2023-06-08 18:05:07 +0000 | [diff] [blame] | 6282 | auto command = [this, app = std::move(application)]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6283 | scoped_unlock unlock(mLock); |
Yabin Cui | ced952f | 2023-06-09 21:12:51 +0000 | [diff] [blame] | 6284 | mPolicy.notifyNoFocusedWindowAnr(app); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6285 | }; |
| 6286 | postCommandLocked(std::move(command)); |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 6287 | } |
| 6288 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 6289 | void InputDispatcher::updateLastAnrStateLocked(const sp<WindowInfoHandle>& window, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6290 | const std::string& reason) { |
| 6291 | const std::string windowLabel = getApplicationWindowLabel(nullptr, window); |
| 6292 | updateLastAnrStateLocked(windowLabel, reason); |
| 6293 | } |
| 6294 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6295 | void InputDispatcher::updateLastAnrStateLocked(const InputApplicationHandle& application, |
| 6296 | const std::string& reason) { |
| 6297 | const std::string windowLabel = getApplicationWindowLabel(&application, nullptr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6298 | updateLastAnrStateLocked(windowLabel, reason); |
| 6299 | } |
| 6300 | |
| 6301 | void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel, |
| 6302 | const std::string& reason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6303 | // 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] | 6304 | time_t t = time(nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6305 | struct tm tm; |
| 6306 | localtime_r(&t, &tm); |
| 6307 | char timestr[64]; |
| 6308 | strftime(timestr, sizeof(timestr), "%F %T", &tm); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6309 | mLastAnrState.clear(); |
| 6310 | mLastAnrState += INDENT "ANR:\n"; |
| 6311 | mLastAnrState += StringPrintf(INDENT2 "Time: %s\n", timestr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6312 | mLastAnrState += StringPrintf(INDENT2 "Reason: %s\n", reason.c_str()); |
| 6313 | mLastAnrState += StringPrintf(INDENT2 "Window: %s\n", windowLabel.c_str()); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6314 | dumpDispatchStateLocked(mLastAnrState); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6315 | } |
| 6316 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6317 | void InputDispatcher::doInterceptKeyBeforeDispatchingCommand(const sp<IBinder>& focusedWindowToken, |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 6318 | const KeyEntry& entry) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6319 | const KeyEvent event = createKeyEvent(entry); |
| 6320 | nsecs_t delay = 0; |
| 6321 | { // release lock |
| 6322 | scoped_unlock unlock(mLock); |
| 6323 | android::base::Timer t; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6324 | delay = mPolicy.interceptKeyBeforeDispatching(focusedWindowToken, event, entry.policyFlags); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6325 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 6326 | ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms", |
| 6327 | std::to_string(t.duration().count()).c_str()); |
| 6328 | } |
| 6329 | } // acquire lock |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6330 | |
| 6331 | if (delay < 0) { |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 6332 | entry.interceptKeyResult = KeyEntry::InterceptKeyResult::SKIP; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6333 | } else if (delay == 0) { |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 6334 | entry.interceptKeyResult = KeyEntry::InterceptKeyResult::CONTINUE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6335 | } else { |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 6336 | entry.interceptKeyResult = KeyEntry::InterceptKeyResult::TRY_AGAIN_LATER; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6337 | entry.interceptKeyWakeupTime = now() + delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6338 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6339 | } |
| 6340 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6341 | void InputDispatcher::sendWindowUnresponsiveCommandLocked(const sp<IBinder>& token, |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6342 | std::optional<gui::Pid> pid, |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6343 | std::string reason) { |
Yabin Cui | 8eb9c55 | 2023-06-08 18:05:07 +0000 | [diff] [blame] | 6344 | auto command = [this, token, pid, r = std::move(reason)]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6345 | scoped_unlock unlock(mLock); |
Yabin Cui | ced952f | 2023-06-09 21:12:51 +0000 | [diff] [blame] | 6346 | mPolicy.notifyWindowUnresponsive(token, pid, r); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6347 | }; |
| 6348 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6349 | } |
| 6350 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6351 | void InputDispatcher::sendWindowResponsiveCommandLocked(const sp<IBinder>& token, |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6352 | std::optional<gui::Pid> pid) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6353 | auto command = [this, token, pid]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6354 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6355 | mPolicy.notifyWindowResponsive(token, pid); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6356 | }; |
| 6357 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6358 | } |
| 6359 | |
| 6360 | /** |
| 6361 | * Tell the policy that a connection has become unresponsive so that it can start ANR. |
| 6362 | * Check whether the connection of interest is a monitor or a window, and add the corresponding |
| 6363 | * command entry to the command queue. |
| 6364 | */ |
| 6365 | void InputDispatcher::processConnectionUnresponsiveLocked(const Connection& connection, |
| 6366 | std::string reason) { |
| 6367 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6368 | std::optional<gui::Pid> pid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6369 | if (connection.monitor) { |
| 6370 | ALOGW("Monitor %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 6371 | reason.c_str()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6372 | pid = findMonitorPidByTokenLocked(connectionToken); |
| 6373 | } else { |
| 6374 | // The connection is a window |
| 6375 | ALOGW("Window %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 6376 | reason.c_str()); |
| 6377 | const sp<WindowInfoHandle> handle = getWindowHandleLocked(connectionToken); |
| 6378 | if (handle != nullptr) { |
| 6379 | pid = handle->getInfo()->ownerPid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6380 | } |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6381 | } |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6382 | sendWindowUnresponsiveCommandLocked(connectionToken, pid, std::move(reason)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6383 | } |
| 6384 | |
| 6385 | /** |
| 6386 | * Tell the policy that a connection has become responsive so that it can stop ANR. |
| 6387 | */ |
| 6388 | void InputDispatcher::processConnectionResponsiveLocked(const Connection& connection) { |
| 6389 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6390 | std::optional<gui::Pid> pid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6391 | if (connection.monitor) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6392 | pid = findMonitorPidByTokenLocked(connectionToken); |
| 6393 | } else { |
| 6394 | // The connection is a window |
| 6395 | const sp<WindowInfoHandle> handle = getWindowHandleLocked(connectionToken); |
| 6396 | if (handle != nullptr) { |
| 6397 | pid = handle->getInfo()->ownerPid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6398 | } |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6399 | } |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6400 | sendWindowResponsiveCommandLocked(connectionToken, pid); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6401 | } |
| 6402 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 6403 | std::unique_ptr<const KeyEntry> InputDispatcher::afterKeyEventLockedInterruptable( |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6404 | const std::shared_ptr<Connection>& connection, DispatchEntry& dispatchEntry, |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6405 | const KeyEntry& keyEntry, bool handled) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6406 | if (keyEntry.flags & AKEY_EVENT_FLAG_FALLBACK) { |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6407 | if (!handled) { |
| 6408 | // Report the key as unhandled, since the fallback was not handled. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6409 | mReporter->reportUnhandledKey(keyEntry.id); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6410 | } |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6411 | return {}; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6412 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6413 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6414 | // Get the fallback key state. |
| 6415 | // Clear it out after dispatching the UP. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6416 | int32_t originalKeyCode = keyEntry.keyCode; |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6417 | std::optional<int32_t> fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6418 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6419 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6420 | } |
| 6421 | |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6422 | if (handled || !dispatchEntry.hasForegroundTarget()) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6423 | // If the application handles the original key for which we previously |
| 6424 | // generated a fallback or if the window is not a foreground window, |
| 6425 | // then cancel the associated fallback key, if any. |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6426 | if (fallbackKeyCode) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6427 | // Dispatch the unhandled key to the policy with the cancel flag. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6428 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6429 | ALOGD("Unhandled key event: Asking policy to cancel fallback action. " |
| 6430 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6431 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, |
| 6432 | keyEntry.policyFlags); |
| 6433 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6434 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6435 | event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6436 | |
| 6437 | mLock.unlock(); |
| 6438 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6439 | if (const auto unhandledKeyFallback = |
| 6440 | mPolicy.dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), |
| 6441 | event, keyEntry.policyFlags); |
| 6442 | unhandledKeyFallback) { |
| 6443 | event = *unhandledKeyFallback; |
| 6444 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6445 | |
| 6446 | mLock.lock(); |
| 6447 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6448 | // Cancel the fallback key. |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6449 | if (*fallbackKeyCode != AKEYCODE_UNKNOWN) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 6450 | CancelationOptions options(CancelationOptions::Mode::CANCEL_FALLBACK_EVENTS, |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6451 | "application handled the original non-fallback key " |
| 6452 | "or is no longer a foreground target, " |
| 6453 | "canceling previously dispatched fallback key"); |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6454 | options.keyCode = *fallbackKeyCode; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6455 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6456 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6457 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6458 | } |
| 6459 | } else { |
| 6460 | // If the application did not handle a non-fallback key, first check |
| 6461 | // that we are in a good state to perform unhandled key event processing |
| 6462 | // Then ask the policy what to do with it. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6463 | bool initialDown = keyEntry.action == AKEY_EVENT_ACTION_DOWN && keyEntry.repeatCount == 0; |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6464 | if (!fallbackKeyCode && !initialDown) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6465 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6466 | ALOGD("Unhandled key event: Skipping unhandled key event processing " |
| 6467 | "since this is not an initial down. " |
| 6468 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6469 | originalKeyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 6470 | } |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6471 | return {}; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6472 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6473 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6474 | // Dispatch the unhandled key to the policy. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6475 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6476 | ALOGD("Unhandled key event: Asking policy to perform fallback action. " |
| 6477 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6478 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 6479 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6480 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6481 | |
| 6482 | mLock.unlock(); |
| 6483 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6484 | bool fallback = false; |
| 6485 | if (auto fb = mPolicy.dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), |
| 6486 | event, keyEntry.policyFlags); |
| 6487 | fb) { |
| 6488 | fallback = true; |
| 6489 | event = *fb; |
| 6490 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6491 | |
| 6492 | mLock.lock(); |
| 6493 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 6494 | if (connection->status != Connection::Status::NORMAL) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6495 | connection->inputState.removeFallbackKey(originalKeyCode); |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6496 | return {}; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6497 | } |
| 6498 | |
| 6499 | // Latch the fallback keycode for this key on an initial down. |
| 6500 | // The fallback keycode cannot change at any other point in the lifecycle. |
| 6501 | if (initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6502 | if (fallback) { |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6503 | *fallbackKeyCode = event.getKeyCode(); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6504 | } else { |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6505 | *fallbackKeyCode = AKEYCODE_UNKNOWN; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6506 | } |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6507 | connection->inputState.setFallbackKey(originalKeyCode, *fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6508 | } |
| 6509 | |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6510 | ALOG_ASSERT(fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6511 | |
| 6512 | // Cancel the fallback key if the policy decides not to send it anymore. |
| 6513 | // We will continue to dispatch the key to the policy but we will no |
| 6514 | // longer dispatch a fallback key to the application. |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6515 | if (*fallbackKeyCode != AKEYCODE_UNKNOWN && |
| 6516 | (!fallback || *fallbackKeyCode != event.getKeyCode())) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6517 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6518 | if (fallback) { |
| 6519 | ALOGD("Unhandled key event: Policy requested to send key %d" |
| 6520 | "as a fallback for %d, but on the DOWN it had requested " |
| 6521 | "to send %d instead. Fallback canceled.", |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6522 | event.getKeyCode(), originalKeyCode, *fallbackKeyCode); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6523 | } else { |
| 6524 | ALOGD("Unhandled key event: Policy did not request fallback for %d, " |
| 6525 | "but on the DOWN it had requested to send %d. " |
| 6526 | "Fallback canceled.", |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6527 | originalKeyCode, *fallbackKeyCode); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6528 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6529 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6530 | |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 6531 | CancelationOptions options(CancelationOptions::Mode::CANCEL_FALLBACK_EVENTS, |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6532 | "canceling fallback, policy no longer desires it"); |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6533 | options.keyCode = *fallbackKeyCode; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6534 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
| 6535 | |
| 6536 | fallback = false; |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6537 | *fallbackKeyCode = AKEYCODE_UNKNOWN; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6538 | if (keyEntry.action != AKEY_EVENT_ACTION_UP) { |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6539 | connection->inputState.setFallbackKey(originalKeyCode, *fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6540 | } |
| 6541 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6542 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6543 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6544 | { |
| 6545 | std::string msg; |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6546 | const std::map<int32_t, int32_t>& fallbackKeys = |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6547 | connection->inputState.getFallbackKeys(); |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6548 | for (const auto& [key, value] : fallbackKeys) { |
| 6549 | msg += StringPrintf(", %d->%d", key, value); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6550 | } |
| 6551 | ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.", |
| 6552 | fallbackKeys.size(), msg.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6553 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6554 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6555 | |
| 6556 | if (fallback) { |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6557 | // Return the fallback key that we want dispatched to the channel. |
| 6558 | std::unique_ptr<KeyEntry> newEntry = |
| 6559 | std::make_unique<KeyEntry>(mIdGenerator.nextId(), keyEntry.injectionState, |
| 6560 | event.getEventTime(), event.getDeviceId(), |
| 6561 | event.getSource(), event.getDisplayId(), |
| 6562 | keyEntry.policyFlags, keyEntry.action, |
| 6563 | event.getFlags() | AKEY_EVENT_FLAG_FALLBACK, |
| 6564 | *fallbackKeyCode, event.getScanCode(), |
| 6565 | event.getMetaState(), event.getRepeatCount(), |
| 6566 | event.getDownTime()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6567 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6568 | ALOGD("Unhandled key event: Dispatching fallback key. " |
| 6569 | "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x", |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6570 | originalKeyCode, *fallbackKeyCode, keyEntry.metaState); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6571 | } |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6572 | return newEntry; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6573 | } else { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6574 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6575 | ALOGD("Unhandled key event: No fallback key."); |
| 6576 | } |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6577 | |
| 6578 | // Report the key as unhandled, since there is no fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6579 | mReporter->reportUnhandledKey(keyEntry.id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6580 | } |
| 6581 | } |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6582 | return {}; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6583 | } |
| 6584 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6585 | void InputDispatcher::traceInboundQueueLengthLocked() { |
| 6586 | if (ATRACE_ENABLED()) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 6587 | ATRACE_INT("iq", mInboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6588 | } |
| 6589 | } |
| 6590 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6591 | void InputDispatcher::traceOutboundQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6592 | if (ATRACE_ENABLED()) { |
| 6593 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6594 | snprintf(counterName, sizeof(counterName), "oq:%s", connection.getWindowName().c_str()); |
| 6595 | ATRACE_INT(counterName, connection.outboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6596 | } |
| 6597 | } |
| 6598 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6599 | void InputDispatcher::traceWaitQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6600 | if (ATRACE_ENABLED()) { |
| 6601 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6602 | snprintf(counterName, sizeof(counterName), "wq:%s", connection.getWindowName().c_str()); |
| 6603 | ATRACE_INT(counterName, connection.waitQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6604 | } |
| 6605 | } |
| 6606 | |
Siarhei Vishniakou | 5e20f27 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 6607 | void InputDispatcher::dump(std::string& dump) const { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6608 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6609 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6610 | dump += "Input Dispatcher State:\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6611 | dumpDispatchStateLocked(dump); |
| 6612 | |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6613 | if (!mLastAnrState.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6614 | dump += "\nInput Dispatcher State at time of last ANR:\n"; |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6615 | dump += mLastAnrState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6616 | } |
| 6617 | } |
| 6618 | |
| 6619 | void InputDispatcher::monitor() { |
| 6620 | // 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] | 6621 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6622 | mLooper->wake(); |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6623 | mDispatcherIsAlive.wait(_l); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6624 | } |
| 6625 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6626 | /** |
| 6627 | * Wake up the dispatcher and wait until it processes all events and commands. |
| 6628 | * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so |
| 6629 | * this method can be safely called from any thread, as long as you've ensured that |
| 6630 | * the work you are interested in completing has already been queued. |
| 6631 | */ |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 6632 | bool InputDispatcher::waitForIdle() const { |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6633 | /** |
| 6634 | * Timeout should represent the longest possible time that a device might spend processing |
| 6635 | * events and commands. |
| 6636 | */ |
| 6637 | constexpr std::chrono::duration TIMEOUT = 100ms; |
| 6638 | std::unique_lock lock(mLock); |
| 6639 | mLooper->wake(); |
| 6640 | std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT); |
| 6641 | return result == std::cv_status::no_timeout; |
| 6642 | } |
| 6643 | |
Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 6644 | /** |
| 6645 | * Sets focus to the window identified by the token. This must be called |
| 6646 | * after updating any input window handles. |
| 6647 | * |
| 6648 | * Params: |
| 6649 | * request.token - input channel token used to identify the window that should gain focus. |
| 6650 | * request.focusedToken - the token that the caller expects currently to be focused. If the |
| 6651 | * specified token does not match the currently focused window, this request will be dropped. |
| 6652 | * If the specified focused token matches the currently focused window, the call will succeed. |
| 6653 | * Set this to "null" if this call should succeed no matter what the currently focused token is. |
| 6654 | * request.timestamp - SYSTEM_TIME_MONOTONIC timestamp in nanos set by the client (wm) |
| 6655 | * when requesting the focus change. This determines which request gets |
| 6656 | * precedence if there is a focus change request from another source such as pointer down. |
| 6657 | */ |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6658 | void InputDispatcher::setFocusedWindow(const FocusRequest& request) { |
| 6659 | { // acquire lock |
| 6660 | std::scoped_lock _l(mLock); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6661 | std::optional<FocusResolver::FocusChanges> changes = |
| 6662 | mFocusResolver.setFocusedWindow(request, getWindowHandlesLocked(request.displayId)); |
| 6663 | if (changes) { |
| 6664 | onFocusChangedLocked(*changes); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6665 | } |
| 6666 | } // release lock |
| 6667 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6668 | mLooper->wake(); |
| 6669 | } |
| 6670 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6671 | void InputDispatcher::onFocusChangedLocked(const FocusResolver::FocusChanges& changes) { |
| 6672 | if (changes.oldFocus) { |
| 6673 | std::shared_ptr<InputChannel> focusedInputChannel = getInputChannelLocked(changes.oldFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6674 | if (focusedInputChannel) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 6675 | CancelationOptions options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6676 | "focus left window"); |
| 6677 | synthesizeCancelationEventsForInputChannelLocked(focusedInputChannel, options); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6678 | enqueueFocusEventLocked(changes.oldFocus, /*hasFocus=*/false, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6679 | } |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6680 | } |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6681 | if (changes.newFocus) { |
Siarhei Vishniakou | c033dfb | 2023-10-03 10:45:16 -0700 | [diff] [blame] | 6682 | resetNoFocusedWindowTimeoutLocked(); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6683 | enqueueFocusEventLocked(changes.newFocus, /*hasFocus=*/true, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6684 | } |
| 6685 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6686 | // If a window has pointer capture, then it must have focus. We need to ensure that this |
| 6687 | // contract is upheld when pointer capture is being disabled due to a loss of window focus. |
| 6688 | // If the window loses focus before it loses pointer capture, then the window can be in a state |
| 6689 | // where it has pointer capture but not focus, violating the contract. Therefore we must |
| 6690 | // dispatch the pointer capture event before the focus event. Since focus events are added to |
| 6691 | // the front of the queue (above), we add the pointer capture event to the front of the queue |
| 6692 | // after the focus events are added. This ensures the pointer capture event ends up at the |
| 6693 | // front. |
| 6694 | disablePointerCaptureForcedLocked(); |
| 6695 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6696 | if (mFocusedDisplayId == changes.displayId) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6697 | sendFocusChangedCommandLocked(changes.oldFocus, changes.newFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6698 | } |
| 6699 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6700 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6701 | void InputDispatcher::disablePointerCaptureForcedLocked() { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6702 | if (!mCurrentPointerCaptureRequest.enable && !mWindowTokenWithPointerCapture) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6703 | return; |
| 6704 | } |
| 6705 | |
| 6706 | ALOGD_IF(DEBUG_FOCUS, "Disabling Pointer Capture because the window lost focus."); |
| 6707 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6708 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6709 | setPointerCaptureLocked(false); |
| 6710 | } |
| 6711 | |
| 6712 | if (!mWindowTokenWithPointerCapture) { |
| 6713 | // No need to send capture changes because no window has capture. |
| 6714 | return; |
| 6715 | } |
| 6716 | |
| 6717 | if (mPendingEvent != nullptr) { |
| 6718 | // Move the pending event to the front of the queue. This will give the chance |
| 6719 | // for the pending event to be dropped if it is a captured event. |
| 6720 | mInboundQueue.push_front(mPendingEvent); |
| 6721 | mPendingEvent = nullptr; |
| 6722 | } |
| 6723 | |
| 6724 | auto entry = std::make_unique<PointerCaptureChangedEntry>(mIdGenerator.nextId(), now(), |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6725 | mCurrentPointerCaptureRequest); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6726 | mInboundQueue.push_front(std::move(entry)); |
| 6727 | } |
| 6728 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6729 | void InputDispatcher::setPointerCaptureLocked(bool enable) { |
| 6730 | mCurrentPointerCaptureRequest.enable = enable; |
| 6731 | mCurrentPointerCaptureRequest.seq++; |
| 6732 | auto command = [this, request = mCurrentPointerCaptureRequest]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6733 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6734 | mPolicy.setPointerCapture(request); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6735 | }; |
| 6736 | postCommandLocked(std::move(command)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6737 | } |
| 6738 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6739 | void InputDispatcher::displayRemoved(int32_t displayId) { |
| 6740 | { // acquire lock |
| 6741 | std::scoped_lock _l(mLock); |
| 6742 | // Set an empty list to remove all handles from the specific display. |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 6743 | setInputWindowsLocked(/*windowInfoHandles=*/{}, displayId); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6744 | setFocusedApplicationLocked(displayId, nullptr); |
| 6745 | // Call focus resolver to clean up stale requests. This must be called after input windows |
| 6746 | // have been removed for the removed display. |
| 6747 | mFocusResolver.displayRemoved(displayId); |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 6748 | // Reset pointer capture eligibility, regardless of previous state. |
| 6749 | std::erase(mIneligibleDisplaysForPointerCapture, displayId); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 6750 | // Remove the associated touch mode state. |
| 6751 | mTouchModePerDisplay.erase(displayId); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 6752 | mVerifiersByDisplay.erase(displayId); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6753 | } // release lock |
| 6754 | |
| 6755 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6756 | mLooper->wake(); |
| 6757 | } |
| 6758 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 6759 | void InputDispatcher::onWindowInfosChanged(const gui::WindowInfosUpdate& update) { |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6760 | // The listener sends the windows as a flattened array. Separate the windows by display for |
| 6761 | // more convenient parsing. |
| 6762 | std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>> handlesPerDisplay; |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 6763 | for (const auto& info : update.windowInfos) { |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6764 | handlesPerDisplay.emplace(info.displayId, std::vector<sp<WindowInfoHandle>>()); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6765 | handlesPerDisplay[info.displayId].push_back(sp<WindowInfoHandle>::make(info)); |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6766 | } |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6767 | |
| 6768 | { // acquire lock |
| 6769 | std::scoped_lock _l(mLock); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 6770 | |
| 6771 | // Ensure that we have an entry created for all existing displays so that if a displayId has |
| 6772 | // no windows, we can tell that the windows were removed from the display. |
| 6773 | for (const auto& [displayId, _] : mWindowHandlesByDisplay) { |
| 6774 | handlesPerDisplay[displayId]; |
| 6775 | } |
| 6776 | |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6777 | mDisplayInfos.clear(); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 6778 | for (const auto& displayInfo : update.displayInfos) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6779 | mDisplayInfos.emplace(displayInfo.displayId, displayInfo); |
| 6780 | } |
| 6781 | |
| 6782 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 6783 | setInputWindowsLocked(handles, displayId); |
| 6784 | } |
Patrick Williams | 9464b2c | 2023-05-23 11:22:04 -0500 | [diff] [blame] | 6785 | |
| 6786 | if (update.vsyncId < mWindowInfosVsyncId) { |
| 6787 | ALOGE("Received out of order window infos update. Last update vsync id: %" PRId64 |
| 6788 | ", current update vsync id: %" PRId64, |
| 6789 | mWindowInfosVsyncId, update.vsyncId); |
| 6790 | } |
| 6791 | mWindowInfosVsyncId = update.vsyncId; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6792 | } |
| 6793 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6794 | mLooper->wake(); |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6795 | } |
| 6796 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6797 | bool InputDispatcher::shouldDropInput( |
| 6798 | const EventEntry& entry, const sp<android::gui::WindowInfoHandle>& windowHandle) const { |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6799 | if (windowHandle->getInfo()->inputConfig.test(WindowInfo::InputConfig::DROP_INPUT) || |
| 6800 | (windowHandle->getInfo()->inputConfig.test( |
| 6801 | WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED) && |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6802 | isWindowObscuredLocked(windowHandle))) { |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6803 | ALOGW("Dropping %s event targeting %s as requested by the input configuration {%s} on " |
| 6804 | "display %" PRId32 ".", |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6805 | ftl::enum_string(entry.type).c_str(), windowHandle->getName().c_str(), |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6806 | windowHandle->getInfo()->inputConfig.string().c_str(), |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6807 | windowHandle->getInfo()->displayId); |
| 6808 | return true; |
| 6809 | } |
| 6810 | return false; |
| 6811 | } |
| 6812 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 6813 | void InputDispatcher::DispatcherWindowListener::onWindowInfosChanged( |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 6814 | const gui::WindowInfosUpdate& update) { |
| 6815 | mDispatcher.onWindowInfosChanged(update); |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 6816 | } |
| 6817 | |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6818 | void InputDispatcher::cancelCurrentTouch() { |
| 6819 | { |
| 6820 | std::scoped_lock _l(mLock); |
| 6821 | ALOGD("Canceling all ongoing pointer gestures on all displays."); |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 6822 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6823 | "cancel current touch"); |
| 6824 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 6825 | |
| 6826 | mTouchStatesByDisplay.clear(); |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6827 | } |
| 6828 | // Wake up poll loop since there might be work to do. |
| 6829 | mLooper->wake(); |
| 6830 | } |
| 6831 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6832 | void InputDispatcher::setMonitorDispatchingTimeoutForTest(std::chrono::nanoseconds timeout) { |
| 6833 | std::scoped_lock _l(mLock); |
| 6834 | mMonitorDispatchingTimeout = timeout; |
| 6835 | } |
| 6836 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6837 | void InputDispatcher::slipWallpaperTouch(ftl::Flags<InputTarget::Flags> targetFlags, |
| 6838 | const sp<WindowInfoHandle>& oldWindowHandle, |
| 6839 | const sp<WindowInfoHandle>& newWindowHandle, |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 6840 | TouchState& state, int32_t deviceId, int32_t pointerId, |
Siarhei Vishniakou | bf88052 | 2023-05-01 11:03:22 -0700 | [diff] [blame] | 6841 | std::vector<InputTarget>& targets) const { |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 6842 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
| 6843 | pointerIds.set(pointerId); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6844 | const bool oldHasWallpaper = oldWindowHandle->getInfo()->inputConfig.test( |
| 6845 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER); |
| 6846 | const bool newHasWallpaper = targetFlags.test(InputTarget::Flags::FOREGROUND) && |
| 6847 | newWindowHandle->getInfo()->inputConfig.test( |
| 6848 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER); |
| 6849 | const sp<WindowInfoHandle> oldWallpaper = |
| 6850 | oldHasWallpaper ? state.getWallpaperWindow() : nullptr; |
| 6851 | const sp<WindowInfoHandle> newWallpaper = |
| 6852 | newHasWallpaper ? findWallpaperWindowBelow(newWindowHandle) : nullptr; |
| 6853 | if (oldWallpaper == newWallpaper) { |
| 6854 | return; |
| 6855 | } |
| 6856 | |
| 6857 | if (oldWallpaper != nullptr) { |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 6858 | const TouchedWindow& oldTouchedWindow = state.getTouchedWindow(oldWallpaper); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 6859 | addPointerWindowTargetLocked(oldWallpaper, |
| 6860 | oldTouchedWindow.targetFlags | |
| 6861 | InputTarget::Flags::DISPATCH_AS_SLIPPERY_EXIT, |
| 6862 | pointerIds, oldTouchedWindow.getDownTimeInTarget(deviceId), |
| 6863 | targets); |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 6864 | state.removeTouchingPointerFromWindow(deviceId, pointerId, oldWallpaper); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6865 | } |
| 6866 | |
| 6867 | if (newWallpaper != nullptr) { |
| 6868 | state.addOrUpdateWindow(newWallpaper, |
| 6869 | InputTarget::Flags::DISPATCH_AS_SLIPPERY_ENTER | |
| 6870 | InputTarget::Flags::WINDOW_IS_OBSCURED | |
| 6871 | InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED, |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 6872 | deviceId, pointerIds); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6873 | } |
| 6874 | } |
| 6875 | |
| 6876 | void InputDispatcher::transferWallpaperTouch(ftl::Flags<InputTarget::Flags> oldTargetFlags, |
| 6877 | ftl::Flags<InputTarget::Flags> newTargetFlags, |
| 6878 | const sp<WindowInfoHandle> fromWindowHandle, |
| 6879 | const sp<WindowInfoHandle> toWindowHandle, |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 6880 | TouchState& state, int32_t deviceId, |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 6881 | std::bitset<MAX_POINTER_ID + 1> pointerIds) { |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6882 | const bool oldHasWallpaper = oldTargetFlags.test(InputTarget::Flags::FOREGROUND) && |
| 6883 | fromWindowHandle->getInfo()->inputConfig.test( |
| 6884 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER); |
| 6885 | const bool newHasWallpaper = newTargetFlags.test(InputTarget::Flags::FOREGROUND) && |
| 6886 | toWindowHandle->getInfo()->inputConfig.test( |
| 6887 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER); |
| 6888 | |
| 6889 | const sp<WindowInfoHandle> oldWallpaper = |
| 6890 | oldHasWallpaper ? state.getWallpaperWindow() : nullptr; |
| 6891 | const sp<WindowInfoHandle> newWallpaper = |
| 6892 | newHasWallpaper ? findWallpaperWindowBelow(toWindowHandle) : nullptr; |
| 6893 | if (oldWallpaper == newWallpaper) { |
| 6894 | return; |
| 6895 | } |
| 6896 | |
| 6897 | if (oldWallpaper != nullptr) { |
| 6898 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
| 6899 | "transferring touch focus to another window"); |
| 6900 | state.removeWindowByToken(oldWallpaper->getToken()); |
| 6901 | synthesizeCancelationEventsForWindowLocked(oldWallpaper, options); |
| 6902 | } |
| 6903 | |
| 6904 | if (newWallpaper != nullptr) { |
| 6905 | nsecs_t downTimeInTarget = now(); |
| 6906 | ftl::Flags<InputTarget::Flags> wallpaperFlags = |
| 6907 | oldTargetFlags & (InputTarget::Flags::SPLIT | InputTarget::Flags::DISPATCH_AS_IS); |
| 6908 | wallpaperFlags |= InputTarget::Flags::WINDOW_IS_OBSCURED | |
| 6909 | InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED; |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 6910 | state.addOrUpdateWindow(newWallpaper, wallpaperFlags, deviceId, pointerIds, |
| 6911 | downTimeInTarget); |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6912 | std::shared_ptr<Connection> wallpaperConnection = |
| 6913 | getConnectionLocked(newWallpaper->getToken()); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6914 | if (wallpaperConnection != nullptr) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6915 | std::shared_ptr<Connection> toConnection = |
| 6916 | getConnectionLocked(toWindowHandle->getToken()); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6917 | toConnection->inputState.mergePointerStateTo(wallpaperConnection->inputState); |
| 6918 | synthesizePointerDownEventsForConnectionLocked(downTimeInTarget, wallpaperConnection, |
| 6919 | wallpaperFlags); |
| 6920 | } |
| 6921 | } |
| 6922 | } |
| 6923 | |
| 6924 | sp<WindowInfoHandle> InputDispatcher::findWallpaperWindowBelow( |
| 6925 | const sp<WindowInfoHandle>& windowHandle) const { |
| 6926 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
| 6927 | getWindowHandlesLocked(windowHandle->getInfo()->displayId); |
| 6928 | bool foundWindow = false; |
| 6929 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
| 6930 | if (!foundWindow && otherHandle != windowHandle) { |
| 6931 | continue; |
| 6932 | } |
| 6933 | if (windowHandle == otherHandle) { |
| 6934 | foundWindow = true; |
| 6935 | continue; |
| 6936 | } |
| 6937 | |
| 6938 | if (otherHandle->getInfo()->inputConfig.test(WindowInfo::InputConfig::IS_WALLPAPER)) { |
| 6939 | return otherHandle; |
| 6940 | } |
| 6941 | } |
| 6942 | return nullptr; |
| 6943 | } |
| 6944 | |
Nergi Rahardi | 730cf3c | 2023-04-13 12:41:17 +0900 | [diff] [blame] | 6945 | void InputDispatcher::setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) { |
| 6946 | std::scoped_lock _l(mLock); |
| 6947 | |
| 6948 | mConfig.keyRepeatTimeout = timeout; |
| 6949 | mConfig.keyRepeatDelay = delay; |
| 6950 | } |
| 6951 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 6952 | } // namespace android::inputdispatcher |