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 | 6773db6 | 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(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 76 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 77 | namespace android::inputdispatcher { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 78 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 79 | namespace { |
Prabir Pradhan | 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 | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 129 | inline const std::string binderToString(const sp<IBinder>& binder) { |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 130 | if (binder == nullptr) { |
| 131 | return "<null>"; |
| 132 | } |
| 133 | return StringPrintf("%p", binder.get()); |
| 134 | } |
| 135 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 136 | static std::string uidString(const gui::Uid& uid) { |
| 137 | return uid.toString(); |
| 138 | } |
| 139 | |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 140 | Result<void> checkKeyAction(int32_t action) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 141 | switch (action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 142 | case AKEY_EVENT_ACTION_DOWN: |
| 143 | case AKEY_EVENT_ACTION_UP: |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 144 | return {}; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 145 | default: |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 146 | return Error() << "Key event has invalid action code " << action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 150 | Result<void> validateKeyEvent(int32_t action) { |
| 151 | return checkKeyAction(action); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 152 | } |
| 153 | |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 154 | Result<void> checkMotionAction(int32_t action, int32_t actionButton, int32_t pointerCount) { |
Siarhei Vishniakou | 2f61bdc | 2022-12-02 08:55:51 -0800 | [diff] [blame] | 155 | switch (MotionEvent::getActionMasked(action)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 156 | case AMOTION_EVENT_ACTION_DOWN: |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 157 | case AMOTION_EVENT_ACTION_UP: { |
| 158 | if (pointerCount != 1) { |
| 159 | return Error() << "invalid pointer count " << pointerCount; |
| 160 | } |
| 161 | return {}; |
| 162 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 163 | case AMOTION_EVENT_ACTION_MOVE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 164 | case AMOTION_EVENT_ACTION_HOVER_ENTER: |
| 165 | case AMOTION_EVENT_ACTION_HOVER_MOVE: |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 166 | case AMOTION_EVENT_ACTION_HOVER_EXIT: { |
| 167 | if (pointerCount < 1) { |
| 168 | return Error() << "invalid pointer count " << pointerCount; |
| 169 | } |
| 170 | return {}; |
| 171 | } |
Siarhei Vishniakou | 2f61bdc | 2022-12-02 08:55:51 -0800 | [diff] [blame] | 172 | case AMOTION_EVENT_ACTION_CANCEL: |
| 173 | case AMOTION_EVENT_ACTION_OUTSIDE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 174 | case AMOTION_EVENT_ACTION_SCROLL: |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 175 | return {}; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 176 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 177 | case AMOTION_EVENT_ACTION_POINTER_UP: { |
Siarhei Vishniakou | 2f61bdc | 2022-12-02 08:55:51 -0800 | [diff] [blame] | 178 | const int32_t index = MotionEvent::getActionIndex(action); |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 179 | if (index < 0) { |
| 180 | return Error() << "invalid index " << index << " for " |
| 181 | << MotionEvent::actionToString(action); |
| 182 | } |
| 183 | if (index >= pointerCount) { |
| 184 | return Error() << "invalid index " << index << " for pointerCount " << pointerCount; |
| 185 | } |
| 186 | if (pointerCount <= 1) { |
| 187 | return Error() << "invalid pointer count " << pointerCount << " for " |
| 188 | << MotionEvent::actionToString(action); |
| 189 | } |
| 190 | return {}; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 191 | } |
| 192 | case AMOTION_EVENT_ACTION_BUTTON_PRESS: |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 193 | case AMOTION_EVENT_ACTION_BUTTON_RELEASE: { |
| 194 | if (actionButton == 0) { |
| 195 | return Error() << "action button should be nonzero for " |
| 196 | << MotionEvent::actionToString(action); |
| 197 | } |
| 198 | return {}; |
| 199 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 200 | default: |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 201 | return Error() << "invalid action " << action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 205 | int64_t millis(std::chrono::nanoseconds t) { |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 206 | return std::chrono::duration_cast<std::chrono::milliseconds>(t).count(); |
| 207 | } |
| 208 | |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 209 | Result<void> validateMotionEvent(int32_t action, int32_t actionButton, size_t pointerCount, |
| 210 | const PointerProperties* pointerProperties) { |
| 211 | Result<void> actionCheck = checkMotionAction(action, actionButton, pointerCount); |
| 212 | if (!actionCheck.ok()) { |
| 213 | return actionCheck; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 214 | } |
| 215 | if (pointerCount < 1 || pointerCount > MAX_POINTERS) { |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 216 | return Error() << "Motion event has invalid pointer count " << pointerCount |
| 217 | << "; value must be between 1 and " << MAX_POINTERS << "."; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 218 | } |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 219 | std::bitset<MAX_POINTER_ID + 1> pointerIdBits; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 220 | for (size_t i = 0; i < pointerCount; i++) { |
| 221 | int32_t id = pointerProperties[i].id; |
| 222 | if (id < 0 || id > MAX_POINTER_ID) { |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 223 | return Error() << "Motion event has invalid pointer id " << id |
| 224 | << "; value must be between 0 and " << MAX_POINTER_ID; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 225 | } |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 226 | if (pointerIdBits.test(id)) { |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 227 | return Error() << "Motion event has duplicate pointer id " << id; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 228 | } |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 229 | pointerIdBits.set(id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 230 | } |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 231 | return {}; |
| 232 | } |
| 233 | |
| 234 | Result<void> validateInputEvent(const InputEvent& event) { |
| 235 | switch (event.getType()) { |
| 236 | case InputEventType::KEY: { |
| 237 | const KeyEvent& key = static_cast<const KeyEvent&>(event); |
| 238 | const int32_t action = key.getAction(); |
| 239 | return validateKeyEvent(action); |
| 240 | } |
| 241 | case InputEventType::MOTION: { |
| 242 | const MotionEvent& motion = static_cast<const MotionEvent&>(event); |
| 243 | const int32_t action = motion.getAction(); |
| 244 | const size_t pointerCount = motion.getPointerCount(); |
| 245 | const PointerProperties* pointerProperties = motion.getPointerProperties(); |
| 246 | const int32_t actionButton = motion.getActionButton(); |
| 247 | return validateMotionEvent(action, actionButton, pointerCount, pointerProperties); |
| 248 | } |
| 249 | default: { |
| 250 | return {}; |
| 251 | } |
| 252 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 253 | } |
| 254 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 255 | std::string dumpRegion(const Region& region) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 256 | if (region.isEmpty()) { |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 257 | return "<empty>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 258 | } |
| 259 | |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 260 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 261 | bool first = true; |
| 262 | Region::const_iterator cur = region.begin(); |
| 263 | Region::const_iterator const tail = region.end(); |
| 264 | while (cur != tail) { |
| 265 | if (first) { |
| 266 | first = false; |
| 267 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 268 | dump += "|"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 269 | } |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 270 | 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] | 271 | cur++; |
| 272 | } |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 273 | return dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 274 | } |
| 275 | |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 276 | std::string dumpQueue(const std::deque<std::unique_ptr<DispatchEntry>>& queue, |
| 277 | nsecs_t currentTime) { |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 278 | constexpr size_t maxEntries = 50; // max events to print |
| 279 | constexpr size_t skipBegin = maxEntries / 2; |
| 280 | const size_t skipEnd = queue.size() - maxEntries / 2; |
| 281 | // skip from maxEntries / 2 ... size() - maxEntries/2 |
| 282 | // only print from 0 .. skipBegin and then from skipEnd .. size() |
| 283 | |
| 284 | std::string dump; |
| 285 | for (size_t i = 0; i < queue.size(); i++) { |
| 286 | const DispatchEntry& entry = *queue[i]; |
| 287 | if (i >= skipBegin && i < skipEnd) { |
| 288 | dump += StringPrintf(INDENT4 "<skipped %zu entries>\n", skipEnd - skipBegin); |
| 289 | i = skipEnd - 1; // it will be incremented to "skipEnd" by 'continue' |
| 290 | continue; |
| 291 | } |
| 292 | dump.append(INDENT4); |
| 293 | dump += entry.eventEntry->getDescription(); |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 294 | dump += StringPrintf(", seq=%" PRIu32 ", targetFlags=%s, age=%" PRId64 "ms", entry.seq, |
| 295 | entry.targetFlags.string().c_str(), |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 296 | ns2ms(currentTime - entry.eventEntry->eventTime)); |
| 297 | if (entry.deliveryTime != 0) { |
| 298 | // This entry was delivered, so add information on how long we've been waiting |
| 299 | dump += StringPrintf(", wait=%" PRId64 "ms", ns2ms(currentTime - entry.deliveryTime)); |
| 300 | } |
| 301 | dump.append("\n"); |
| 302 | } |
| 303 | return dump; |
| 304 | } |
| 305 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 306 | /** |
| 307 | * Find the entry in std::unordered_map by key, and return it. |
| 308 | * If the entry is not found, return a default constructed entry. |
| 309 | * |
| 310 | * Useful when the entries are vectors, since an empty vector will be returned |
| 311 | * if the entry is not found. |
| 312 | * Also useful when the entries are sp<>. If an entry is not found, nullptr is returned. |
| 313 | */ |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 314 | template <typename K, typename V> |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 315 | V getValueByKey(const std::unordered_map<K, V>& map, K key) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 316 | auto it = map.find(key); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 317 | return it != map.end() ? it->second : V{}; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 318 | } |
| 319 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 320 | bool haveSameToken(const sp<WindowInfoHandle>& first, const sp<WindowInfoHandle>& second) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 321 | if (first == second) { |
| 322 | return true; |
| 323 | } |
| 324 | |
| 325 | if (first == nullptr || second == nullptr) { |
| 326 | return false; |
| 327 | } |
| 328 | |
| 329 | return first->getToken() == second->getToken(); |
| 330 | } |
| 331 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 332 | bool haveSameApplicationToken(const WindowInfo* first, const WindowInfo* second) { |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 333 | if (first == nullptr || second == nullptr) { |
| 334 | return false; |
| 335 | } |
| 336 | return first->applicationInfo.token != nullptr && |
| 337 | first->applicationInfo.token == second->applicationInfo.token; |
| 338 | } |
| 339 | |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 340 | template <typename T> |
| 341 | size_t firstMarkedBit(T set) { |
| 342 | // TODO: replace with std::countr_zero from <bit> when that's available |
| 343 | LOG_ALWAYS_FATAL_IF(set.none()); |
| 344 | size_t i = 0; |
| 345 | while (!set.test(i)) { |
| 346 | i++; |
| 347 | } |
| 348 | return i; |
| 349 | } |
| 350 | |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 351 | std::unique_ptr<DispatchEntry> createDispatchEntry( |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 352 | const InputTarget& inputTarget, std::shared_ptr<const EventEntry> eventEntry, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 353 | ftl::Flags<InputTarget::Flags> inputTargetFlags) { |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 354 | if (inputTarget.useDefaultPointerTransform()) { |
| 355 | const ui::Transform& transform = inputTarget.getDefaultPointerTransform(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 356 | return std::make_unique<DispatchEntry>(eventEntry, inputTargetFlags, transform, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 357 | inputTarget.displayTransform, |
| 358 | inputTarget.globalScaleFactor); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | ALOG_ASSERT(eventEntry->type == EventEntry::Type::MOTION); |
| 362 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); |
| 363 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 364 | std::vector<PointerCoords> pointerCoords; |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 365 | pointerCoords.resize(motionEntry.getPointerCount()); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 366 | |
| 367 | // Use the first pointer information to normalize all other pointers. This could be any pointer |
| 368 | // 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] | 369 | // uses the transform for the normalized pointer. |
| 370 | const ui::Transform& firstPointerTransform = |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 371 | inputTarget.pointerTransforms[firstMarkedBit(inputTarget.pointerIds)]; |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 372 | ui::Transform inverseFirstTransform = firstPointerTransform.inverse(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 373 | |
| 374 | // Iterate through all pointers in the event to normalize against the first. |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 375 | for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.getPointerCount(); pointerIndex++) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 376 | const PointerProperties& pointerProperties = motionEntry.pointerProperties[pointerIndex]; |
| 377 | uint32_t pointerId = uint32_t(pointerProperties.id); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 378 | const ui::Transform& currTransform = inputTarget.pointerTransforms[pointerId]; |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 379 | |
| 380 | pointerCoords[pointerIndex].copyFrom(motionEntry.pointerCoords[pointerIndex]); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 381 | // First, apply the current pointer's transform to update the coordinates into |
| 382 | // window space. |
| 383 | pointerCoords[pointerIndex].transform(currTransform); |
| 384 | // Next, apply the inverse transform of the normalized coordinates so the |
| 385 | // current coordinates are transformed into the normalized coordinate space. |
| 386 | pointerCoords[pointerIndex].transform(inverseFirstTransform); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 389 | std::unique_ptr<MotionEntry> combinedMotionEntry = |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 390 | std::make_unique<MotionEntry>(motionEntry.id, motionEntry.injectionState, |
| 391 | motionEntry.eventTime, motionEntry.deviceId, |
| 392 | motionEntry.source, motionEntry.displayId, |
| 393 | motionEntry.policyFlags, motionEntry.action, |
| 394 | motionEntry.actionButton, motionEntry.flags, |
| 395 | motionEntry.metaState, motionEntry.buttonState, |
| 396 | motionEntry.classification, motionEntry.edgeFlags, |
| 397 | motionEntry.xPrecision, motionEntry.yPrecision, |
| 398 | motionEntry.xCursorPosition, motionEntry.yCursorPosition, |
| 399 | motionEntry.downTime, motionEntry.pointerProperties, |
| 400 | pointerCoords); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 401 | |
| 402 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 403 | std::make_unique<DispatchEntry>(std::move(combinedMotionEntry), inputTargetFlags, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 404 | firstPointerTransform, inputTarget.displayTransform, |
| 405 | inputTarget.globalScaleFactor); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 406 | return dispatchEntry; |
| 407 | } |
| 408 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 409 | status_t openInputChannelPair(const std::string& name, std::shared_ptr<InputChannel>& serverChannel, |
| 410 | std::unique_ptr<InputChannel>& clientChannel) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 411 | std::unique_ptr<InputChannel> uniqueServerChannel; |
| 412 | status_t result = InputChannel::openInputChannelPair(name, uniqueServerChannel, clientChannel); |
| 413 | |
| 414 | serverChannel = std::move(uniqueServerChannel); |
| 415 | return result; |
| 416 | } |
| 417 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 418 | template <typename T> |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 419 | 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] | 420 | if (lhs == nullptr && rhs == nullptr) { |
| 421 | return true; |
| 422 | } |
| 423 | if (lhs == nullptr || rhs == nullptr) { |
| 424 | return false; |
| 425 | } |
| 426 | return *lhs == *rhs; |
| 427 | } |
| 428 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 429 | KeyEvent createKeyEvent(const KeyEntry& entry) { |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 430 | KeyEvent event; |
| 431 | event.initialize(entry.id, entry.deviceId, entry.source, entry.displayId, INVALID_HMAC, |
| 432 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, |
| 433 | entry.repeatCount, entry.downTime, entry.eventTime); |
| 434 | return event; |
| 435 | } |
| 436 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 437 | bool shouldReportMetricsForConnection(const Connection& connection) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 438 | // Do not keep track of gesture monitors. They receive every event and would disproportionately |
| 439 | // affect the statistics. |
| 440 | if (connection.monitor) { |
| 441 | return false; |
| 442 | } |
| 443 | // If the connection is experiencing ANR, let's skip it. We have separate ANR metrics |
| 444 | if (!connection.responsive) { |
| 445 | return false; |
| 446 | } |
| 447 | return true; |
| 448 | } |
| 449 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 450 | bool shouldReportFinishedEvent(const DispatchEntry& dispatchEntry, const Connection& connection) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 451 | const EventEntry& eventEntry = *dispatchEntry.eventEntry; |
| 452 | const int32_t& inputEventId = eventEntry.id; |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 453 | if (inputEventId == android::os::IInputConstants::INVALID_INPUT_EVENT_ID) { |
| 454 | return false; |
| 455 | } |
| 456 | // Only track latency for events that originated from hardware |
| 457 | if (eventEntry.isSynthesized()) { |
| 458 | return false; |
| 459 | } |
| 460 | const EventEntry::Type& inputEventEntryType = eventEntry.type; |
| 461 | if (inputEventEntryType == EventEntry::Type::KEY) { |
| 462 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 463 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
| 464 | return false; |
| 465 | } |
| 466 | } else if (inputEventEntryType == EventEntry::Type::MOTION) { |
| 467 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 468 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL || |
| 469 | motionEntry.action == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 470 | return false; |
| 471 | } |
| 472 | } else { |
| 473 | // Not a key or a motion |
| 474 | return false; |
| 475 | } |
| 476 | if (!shouldReportMetricsForConnection(connection)) { |
| 477 | return false; |
| 478 | } |
| 479 | return true; |
| 480 | } |
| 481 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 482 | /** |
| 483 | * Connection is responsive if it has no events in the waitQueue that are older than the |
| 484 | * current time. |
| 485 | */ |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 486 | bool isConnectionResponsive(const Connection& connection) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 487 | const nsecs_t currentTime = now(); |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 488 | for (const auto& dispatchEntry : connection.waitQueue) { |
| 489 | if (dispatchEntry->timeoutTime < currentTime) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 490 | return false; |
| 491 | } |
| 492 | } |
| 493 | return true; |
| 494 | } |
| 495 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 496 | // Returns true if the event type passed as argument represents a user activity. |
| 497 | bool isUserActivityEvent(const EventEntry& eventEntry) { |
| 498 | switch (eventEntry.type) { |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 499 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 500 | case EventEntry::Type::DEVICE_RESET: |
| 501 | case EventEntry::Type::DRAG: |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 502 | case EventEntry::Type::FOCUS: |
| 503 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 504 | case EventEntry::Type::SENSOR: |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 505 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 506 | return false; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 507 | case EventEntry::Type::KEY: |
| 508 | case EventEntry::Type::MOTION: |
| 509 | return true; |
| 510 | } |
| 511 | } |
| 512 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 513 | // 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] | 514 | bool windowAcceptsTouchAt(const WindowInfo& windowInfo, int32_t displayId, float x, float y, |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 515 | bool isStylus, const ui::Transform& displayTransform) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 516 | const auto inputConfig = windowInfo.inputConfig; |
| 517 | if (windowInfo.displayId != displayId || |
| 518 | inputConfig.test(WindowInfo::InputConfig::NOT_VISIBLE)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 519 | return false; |
| 520 | } |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 521 | const bool windowCanInterceptTouch = isStylus && windowInfo.interceptsStylus(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 522 | if (inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE) && !windowCanInterceptTouch) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 523 | return false; |
| 524 | } |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 525 | |
| 526 | // Window Manager works in the logical display coordinate space. When it specifies bounds for a |
| 527 | // window as (l, t, r, b), the range of x in [l, r) and y in [t, b) are considered to be inside |
| 528 | // the window. Points on the right and bottom edges should not be inside the window, so we need |
| 529 | // to be careful about performing a hit test when the display is rotated, since the "right" and |
| 530 | // "bottom" of the window will be different in the display (un-rotated) space compared to in the |
| 531 | // logical display in which WM determined the bounds. Perform the hit test in the logical |
| 532 | // display space to ensure these edges are considered correctly in all orientations. |
| 533 | const auto touchableRegion = displayTransform.transform(windowInfo.touchableRegion); |
| 534 | const auto p = displayTransform.transform(x, y); |
| 535 | if (!touchableRegion.contains(std::floor(p.x), std::floor(p.y))) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 536 | return false; |
| 537 | } |
| 538 | return true; |
| 539 | } |
| 540 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 541 | bool isPointerFromStylus(const MotionEntry& entry, int32_t pointerIndex) { |
| 542 | return isFromSource(entry.source, AINPUT_SOURCE_STYLUS) && |
Prabir Pradhan | e562696 | 2022-10-27 20:30:53 +0000 | [diff] [blame] | 543 | isStylusToolType(entry.pointerProperties[pointerIndex].toolType); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 544 | } |
| 545 | |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 546 | // Determines if the given window can be targeted as InputTarget::Flags::FOREGROUND. |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 547 | // Foreground events are only sent to "foreground targetable" windows, but not all gestures sent to |
| 548 | // such window are necessarily targeted with the flag. For example, an event with ACTION_OUTSIDE can |
| 549 | // 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] | 550 | // InputTarget::Flags::FOREGROUND. |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 551 | bool canReceiveForegroundTouches(const WindowInfo& info) { |
| 552 | // A non-touchable window can still receive touch events (e.g. in the case of |
| 553 | // STYLUS_INTERCEPTOR), so prevent such windows from receiving foreground events for touches. |
| 554 | return !info.inputConfig.test(gui::WindowInfo::InputConfig::NOT_TOUCHABLE) && !info.isSpy(); |
| 555 | } |
| 556 | |
Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 557 | bool isWindowOwnedBy(const sp<WindowInfoHandle>& windowHandle, gui::Pid pid, gui::Uid uid) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 558 | if (windowHandle == nullptr) { |
| 559 | return false; |
| 560 | } |
| 561 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 562 | if (pid == windowInfo->ownerPid && uid == windowInfo->ownerUid) { |
| 563 | return true; |
| 564 | } |
| 565 | return false; |
| 566 | } |
| 567 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 568 | // Checks targeted injection using the window's owner's uid. |
| 569 | // Returns an empty string if an entry can be sent to the given window, or an error message if the |
| 570 | // entry is a targeted injection whose uid target doesn't match the window owner. |
| 571 | std::optional<std::string> verifyTargetedInjection(const sp<WindowInfoHandle>& window, |
| 572 | const EventEntry& entry) { |
| 573 | if (entry.injectionState == nullptr || !entry.injectionState->targetUid) { |
| 574 | // The event was not injected, or the injected event does not target a window. |
| 575 | return {}; |
| 576 | } |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 577 | const auto uid = *entry.injectionState->targetUid; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 578 | if (window == nullptr) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 579 | return StringPrintf("No valid window target for injection into uid %s.", |
| 580 | uid.toString().c_str()); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 581 | } |
| 582 | if (entry.injectionState->targetUid != window->getInfo()->ownerUid) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 583 | return StringPrintf("Injected event targeted at uid %s would be dispatched to window '%s' " |
| 584 | "owned by uid %s.", |
| 585 | uid.toString().c_str(), window->getName().c_str(), |
| 586 | window->getInfo()->ownerUid.toString().c_str()); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 587 | } |
| 588 | return {}; |
| 589 | } |
| 590 | |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 591 | std::pair<float, float> resolveTouchedPosition(const MotionEntry& entry) { |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 592 | const bool isFromMouse = isFromSource(entry.source, AINPUT_SOURCE_MOUSE); |
| 593 | // Always dispatch mouse events to cursor position. |
| 594 | if (isFromMouse) { |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 595 | return {entry.xCursorPosition, entry.yCursorPosition}; |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 596 | } |
| 597 | |
Siarhei Vishniakou | 5b9766d | 2023-07-18 14:06:29 -0700 | [diff] [blame] | 598 | const int32_t pointerIndex = MotionEvent::getActionIndex(entry.action); |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 599 | return {entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 600 | entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_Y)}; |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 601 | } |
| 602 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 603 | std::optional<nsecs_t> getDownTime(const EventEntry& eventEntry) { |
| 604 | if (eventEntry.type == EventEntry::Type::KEY) { |
| 605 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 606 | return keyEntry.downTime; |
| 607 | } else if (eventEntry.type == EventEntry::Type::MOTION) { |
| 608 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 609 | return motionEntry.downTime; |
| 610 | } |
| 611 | return std::nullopt; |
| 612 | } |
| 613 | |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 614 | /** |
| 615 | * Compare the old touch state to the new touch state, and generate the corresponding touched |
| 616 | * windows (== input targets). |
| 617 | * If a window had the hovering pointer, but now it doesn't, produce HOVER_EXIT for that window. |
| 618 | * If the pointer just entered the new window, produce HOVER_ENTER. |
| 619 | * For pointers remaining in the window, produce HOVER_MOVE. |
| 620 | */ |
| 621 | std::vector<TouchedWindow> getHoveringWindowsLocked(const TouchState* oldState, |
| 622 | const TouchState& newTouchState, |
| 623 | const MotionEntry& entry) { |
| 624 | std::vector<TouchedWindow> out; |
| 625 | const int32_t maskedAction = MotionEvent::getActionMasked(entry.action); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 626 | |
| 627 | if (maskedAction == AMOTION_EVENT_ACTION_SCROLL) { |
| 628 | // ACTION_SCROLL events should not affect the hovering pointer dispatch |
| 629 | return {}; |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | // We should consider all hovering pointers here. But for now, just use the first one |
| 633 | const int32_t pointerId = entry.pointerProperties[0].id; |
| 634 | |
| 635 | std::set<sp<WindowInfoHandle>> oldWindows; |
| 636 | if (oldState != nullptr) { |
| 637 | oldWindows = oldState->getWindowsWithHoveringPointer(entry.deviceId, pointerId); |
| 638 | } |
| 639 | |
| 640 | std::set<sp<WindowInfoHandle>> newWindows = |
| 641 | newTouchState.getWindowsWithHoveringPointer(entry.deviceId, pointerId); |
| 642 | |
| 643 | // If the pointer is no longer in the new window set, send HOVER_EXIT. |
| 644 | for (const sp<WindowInfoHandle>& oldWindow : oldWindows) { |
| 645 | if (newWindows.find(oldWindow) == newWindows.end()) { |
| 646 | TouchedWindow touchedWindow; |
| 647 | touchedWindow.windowHandle = oldWindow; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 648 | touchedWindow.dispatchMode = InputTarget::DispatchMode::HOVER_EXIT; |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 649 | out.push_back(touchedWindow); |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | for (const sp<WindowInfoHandle>& newWindow : newWindows) { |
| 654 | TouchedWindow touchedWindow; |
| 655 | touchedWindow.windowHandle = newWindow; |
| 656 | if (oldWindows.find(newWindow) == oldWindows.end()) { |
| 657 | // Any windows that have this pointer now, and didn't have it before, should get |
| 658 | // HOVER_ENTER |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 659 | touchedWindow.dispatchMode = InputTarget::DispatchMode::HOVER_ENTER; |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 660 | } else { |
| 661 | // This pointer was already sent to the window. Use ACTION_HOVER_MOVE. |
Siarhei Vishniakou | c2eb850 | 2023-04-11 18:33:36 -0700 | [diff] [blame] | 662 | if (CC_UNLIKELY(maskedAction != AMOTION_EVENT_ACTION_HOVER_MOVE)) { |
Daniel Norman | 2f99cdb | 2023-08-02 16:39:45 -0700 | [diff] [blame] | 663 | android::base::LogSeverity severity = android::base::LogSeverity::FATAL; |
Ameer Armaly | cff4fa5 | 2023-10-04 23:45:11 +0000 | [diff] [blame] | 664 | if (!input_flags::a11y_crash_on_inconsistent_event_stream() && |
| 665 | entry.flags & AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT) { |
Daniel Norman | 2f99cdb | 2023-08-02 16:39:45 -0700 | [diff] [blame] | 666 | // The Accessibility injected touch exploration event stream |
| 667 | // has known inconsistencies, so log ERROR instead of |
| 668 | // crashing the device with FATAL. |
Daniel Norman | 2f99cdb | 2023-08-02 16:39:45 -0700 | [diff] [blame] | 669 | severity = android::base::LogSeverity::ERROR; |
| 670 | } |
| 671 | LOG(severity) << "Expected ACTION_HOVER_MOVE instead of " << entry.getDescription(); |
Siarhei Vishniakou | c2eb850 | 2023-04-11 18:33:36 -0700 | [diff] [blame] | 672 | } |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 673 | touchedWindow.dispatchMode = InputTarget::DispatchMode::AS_IS; |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 674 | } |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 675 | touchedWindow.addHoveringPointer(entry.deviceId, pointerId); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 676 | if (canReceiveForegroundTouches(*newWindow->getInfo())) { |
| 677 | touchedWindow.targetFlags |= InputTarget::Flags::FOREGROUND; |
| 678 | } |
| 679 | out.push_back(touchedWindow); |
| 680 | } |
| 681 | return out; |
| 682 | } |
| 683 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 684 | template <typename T> |
| 685 | std::vector<T>& operator+=(std::vector<T>& left, const std::vector<T>& right) { |
| 686 | left.insert(left.end(), right.begin(), right.end()); |
| 687 | return left; |
| 688 | } |
| 689 | |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 690 | // Filter windows in a TouchState and targets in a vector to remove untrusted windows/targets from |
| 691 | // both. |
| 692 | void filterUntrustedTargets(TouchState& touchState, std::vector<InputTarget>& targets) { |
| 693 | std::erase_if(touchState.windows, [&](const TouchedWindow& window) { |
| 694 | if (!window.windowHandle->getInfo()->inputConfig.test( |
| 695 | WindowInfo::InputConfig::TRUSTED_OVERLAY)) { |
| 696 | // In addition to TouchState, erase this window from the input targets! We don't have a |
| 697 | // good way to do this today except by adding a nested loop. |
| 698 | // TODO(b/282025641): simplify this code once InputTargets are being identified |
| 699 | // separately from TouchedWindows. |
| 700 | std::erase_if(targets, [&](const InputTarget& target) { |
| 701 | return target.inputChannel->getConnectionToken() == window.windowHandle->getToken(); |
| 702 | }); |
| 703 | return true; |
| 704 | } |
| 705 | return false; |
| 706 | }); |
| 707 | } |
| 708 | |
Siarhei Vishniakou | ce1fd47 | 2023-09-18 18:38:07 -0700 | [diff] [blame] | 709 | /** |
| 710 | * In general, touch should be always split between windows. Some exceptions: |
| 711 | * 1. Don't split touch if all of the below is true: |
| 712 | * (a) we have an active pointer down *and* |
| 713 | * (b) a new pointer is going down that's from the same device *and* |
| 714 | * (c) the window that's receiving the current pointer does not support split touch. |
| 715 | * 2. Don't split mouse events |
| 716 | */ |
| 717 | bool shouldSplitTouch(const TouchState& touchState, const MotionEntry& entry) { |
| 718 | if (isFromSource(entry.source, AINPUT_SOURCE_MOUSE)) { |
| 719 | // We should never split mouse events |
| 720 | return false; |
| 721 | } |
| 722 | for (const TouchedWindow& touchedWindow : touchState.windows) { |
| 723 | if (touchedWindow.windowHandle->getInfo()->isSpy()) { |
| 724 | // Spy windows should not affect whether or not touch is split. |
| 725 | continue; |
| 726 | } |
| 727 | if (touchedWindow.windowHandle->getInfo()->supportsSplitTouch()) { |
| 728 | continue; |
| 729 | } |
| 730 | if (touchedWindow.windowHandle->getInfo()->inputConfig.test( |
| 731 | gui::WindowInfo::InputConfig::IS_WALLPAPER)) { |
| 732 | // Wallpaper window should not affect whether or not touch is split |
| 733 | continue; |
| 734 | } |
| 735 | |
| 736 | if (touchedWindow.hasTouchingPointers(entry.deviceId)) { |
| 737 | return false; |
| 738 | } |
| 739 | } |
| 740 | return true; |
| 741 | } |
| 742 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 743 | } // namespace |
| 744 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 745 | // --- InputDispatcher --- |
| 746 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 747 | InputDispatcher::InputDispatcher(InputDispatcherPolicyInterface& policy) |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 748 | : mPolicy(policy), |
| 749 | mPendingEvent(nullptr), |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 750 | mLastDropReason(DropReason::NOT_DROPPED), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 751 | mIdGenerator(IdGenerator::Source::INPUT_DISPATCHER), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 752 | mAppSwitchSawKeyDown(false), |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 753 | mAppSwitchDueTime(LLONG_MAX), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 754 | mNextUnblockedEvent(nullptr), |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 755 | mMonitorDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 756 | mDispatchEnabled(false), |
| 757 | mDispatchFrozen(false), |
| 758 | mInputFilterEnabled(false), |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 759 | mMaximumObscuringOpacityForTouch(1.0f), |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 760 | mFocusedDisplayId(ADISPLAY_ID_DEFAULT), |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 761 | mWindowTokenWithPointerCapture(nullptr), |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 762 | mLatencyAggregator(), |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 763 | mLatencyTracker(&mLatencyAggregator) { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 764 | mLooper = sp<Looper>::make(false); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 765 | mReporter = createInputReporter(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 766 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 767 | mWindowInfoListener = sp<DispatcherWindowListener>::make(*this); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 768 | #if defined(__ANDROID__) |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 769 | SurfaceComposerClient::getDefault()->addWindowInfosListener(mWindowInfoListener); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 770 | #endif |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 771 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | InputDispatcher::~InputDispatcher() { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 775 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 776 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 777 | resetKeyRepeatLocked(); |
| 778 | releasePendingEventLocked(); |
| 779 | drainInboundQueueLocked(); |
| 780 | mCommandQueue.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 781 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 782 | while (!mConnectionsByToken.empty()) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 783 | std::shared_ptr<Connection> connection = mConnectionsByToken.begin()->second; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 784 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), /*notify=*/false); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 785 | } |
| 786 | } |
| 787 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 788 | status_t InputDispatcher::start() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 789 | if (mThread) { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 790 | return ALREADY_EXISTS; |
| 791 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 792 | mThread = std::make_unique<InputThread>( |
| 793 | "InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); }); |
| 794 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | status_t InputDispatcher::stop() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 798 | if (mThread && mThread->isCallingThread()) { |
| 799 | ALOGE("InputDispatcher cannot be stopped from its own thread!"); |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 800 | return INVALID_OPERATION; |
| 801 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 802 | mThread.reset(); |
| 803 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 804 | } |
| 805 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 806 | void InputDispatcher::dispatchOnce() { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 807 | nsecs_t nextWakeupTime = LLONG_MAX; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 808 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 809 | std::scoped_lock _l(mLock); |
| 810 | mDispatcherIsAlive.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 811 | |
| 812 | // Run a dispatch loop if there are no pending commands. |
| 813 | // The dispatch loop might enqueue commands to run afterwards. |
| 814 | if (!haveCommandsLocked()) { |
| 815 | dispatchOnceInnerLocked(&nextWakeupTime); |
| 816 | } |
| 817 | |
| 818 | // Run all pending commands if there are any. |
| 819 | // 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] | 820 | if (runCommandsLockedInterruptable()) { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 821 | nextWakeupTime = LLONG_MIN; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 822 | } |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 823 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 824 | // If we are still waiting for ack on some events, |
| 825 | // we might have to wake up earlier to check if an app is anr'ing. |
| 826 | const nsecs_t nextAnrCheck = processAnrsLocked(); |
| 827 | nextWakeupTime = std::min(nextWakeupTime, nextAnrCheck); |
| 828 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 829 | // We are about to enter an infinitely long sleep, because we have no commands or |
| 830 | // pending or queued events |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 831 | if (nextWakeupTime == LLONG_MAX) { |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 832 | mDispatcherEnteredIdle.notify_all(); |
| 833 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 834 | } // release lock |
| 835 | |
| 836 | // Wait for callback or timeout or wake. (make sure we round up, not down) |
| 837 | nsecs_t currentTime = now(); |
| 838 | int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime); |
| 839 | mLooper->pollOnce(timeoutMillis); |
| 840 | } |
| 841 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 842 | /** |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 843 | * Raise ANR if there is no focused window. |
| 844 | * Before the ANR is raised, do a final state check: |
| 845 | * 1. The currently focused application must be the same one we are waiting for. |
| 846 | * 2. Ensure we still don't have a focused window. |
| 847 | */ |
| 848 | void InputDispatcher::processNoFocusedWindowAnrLocked() { |
| 849 | // Check if the application that we are waiting for is still focused. |
| 850 | std::shared_ptr<InputApplicationHandle> focusedApplication = |
| 851 | getValueByKey(mFocusedApplicationHandlesByDisplay, mAwaitedApplicationDisplayId); |
| 852 | if (focusedApplication == nullptr || |
| 853 | focusedApplication->getApplicationToken() != |
| 854 | mAwaitedFocusedApplication->getApplicationToken()) { |
| 855 | // Unexpected because we should have reset the ANR timer when focused application changed |
| 856 | ALOGE("Waited for a focused window, but focused application has already changed to %s", |
| 857 | focusedApplication->getName().c_str()); |
| 858 | return; // The focused application has changed. |
| 859 | } |
| 860 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 861 | const sp<WindowInfoHandle>& focusedWindowHandle = |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 862 | getFocusedWindowHandleLocked(mAwaitedApplicationDisplayId); |
| 863 | if (focusedWindowHandle != nullptr) { |
| 864 | return; // We now have a focused window. No need for ANR. |
| 865 | } |
| 866 | onAnrLocked(mAwaitedFocusedApplication); |
| 867 | } |
| 868 | |
| 869 | /** |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 870 | * Check if any of the connections' wait queues have events that are too old. |
| 871 | * If we waited for events to be ack'ed for more than the window timeout, raise an ANR. |
| 872 | * Return the time at which we should wake up next. |
| 873 | */ |
| 874 | nsecs_t InputDispatcher::processAnrsLocked() { |
| 875 | const nsecs_t currentTime = now(); |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 876 | nsecs_t nextAnrCheck = LLONG_MAX; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 877 | // Check if we are waiting for a focused window to appear. Raise ANR if waited too long |
| 878 | if (mNoFocusedWindowTimeoutTime.has_value() && mAwaitedFocusedApplication != nullptr) { |
| 879 | if (currentTime >= *mNoFocusedWindowTimeoutTime) { |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 880 | processNoFocusedWindowAnrLocked(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 881 | mAwaitedFocusedApplication.reset(); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 882 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 883 | return LLONG_MIN; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 884 | } else { |
Siarhei Vishniakou | 38a6d27 | 2020-10-20 20:29:33 -0500 | [diff] [blame] | 885 | // Keep waiting. We will drop the event when mNoFocusedWindowTimeoutTime comes. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 886 | nextAnrCheck = *mNoFocusedWindowTimeoutTime; |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | // Check if any connection ANRs are due |
| 891 | nextAnrCheck = std::min(nextAnrCheck, mAnrTracker.firstTimeout()); |
| 892 | if (currentTime < nextAnrCheck) { // most likely scenario |
| 893 | return nextAnrCheck; // everything is normal. Let's check again at nextAnrCheck |
| 894 | } |
| 895 | |
| 896 | // If we reached here, we have an unresponsive connection. |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 897 | std::shared_ptr<Connection> connection = getConnectionLocked(mAnrTracker.firstToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 898 | if (connection == nullptr) { |
| 899 | ALOGE("Could not find connection for entry %" PRId64, mAnrTracker.firstTimeout()); |
| 900 | return nextAnrCheck; |
| 901 | } |
| 902 | connection->responsive = false; |
| 903 | // Stop waking up for this unresponsive connection |
| 904 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 905 | onAnrLocked(connection); |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 906 | return LLONG_MIN; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 907 | } |
| 908 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 909 | std::chrono::nanoseconds InputDispatcher::getDispatchingTimeoutLocked( |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 910 | const std::shared_ptr<Connection>& connection) { |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 911 | if (connection->monitor) { |
| 912 | return mMonitorDispatchingTimeout; |
| 913 | } |
| 914 | const sp<WindowInfoHandle> window = |
| 915 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 916 | if (window != nullptr) { |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 917 | return window->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 918 | } |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 919 | return DEFAULT_INPUT_DISPATCHING_TIMEOUT; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 920 | } |
| 921 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 922 | void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) { |
| 923 | nsecs_t currentTime = now(); |
| 924 | |
Jeff Brown | dc5992e | 2014-04-11 01:27:26 -0700 | [diff] [blame] | 925 | // Reset the key repeat timer whenever normal dispatch is suspended while the |
| 926 | // device is in a non-interactive state. This is to ensure that we abort a key |
| 927 | // repeat if the device is just coming out of sleep. |
| 928 | if (!mDispatchEnabled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 929 | resetKeyRepeatLocked(); |
| 930 | } |
| 931 | |
| 932 | // If dispatching is frozen, do not process timeouts or try to deliver any new events. |
| 933 | if (mDispatchFrozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 934 | if (DEBUG_FOCUS) { |
| 935 | ALOGD("Dispatch frozen. Waiting some more."); |
| 936 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 937 | return; |
| 938 | } |
| 939 | |
| 940 | // Optimize latency of app switches. |
| 941 | // Essentially we start a short timeout when an app switch key (HOME / ENDCALL) has |
| 942 | // 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] | 943 | bool isAppSwitchDue; |
Siarhei Vishniakou | 17f1228 | 2023-11-02 13:40:29 -0700 | [diff] [blame] | 944 | if (!REMOVE_APP_SWITCH_DROPS) { |
Siarhei Vishniakou | 6520a58 | 2023-10-27 21:53:45 -0700 | [diff] [blame] | 945 | isAppSwitchDue = mAppSwitchDueTime <= currentTime; |
| 946 | if (mAppSwitchDueTime < *nextWakeupTime) { |
| 947 | *nextWakeupTime = mAppSwitchDueTime; |
| 948 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | // Ready to start a new event. |
| 952 | // If we don't already have a pending event, go grab one. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 953 | if (!mPendingEvent) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 954 | if (mInboundQueue.empty()) { |
Siarhei Vishniakou | 17f1228 | 2023-11-02 13:40:29 -0700 | [diff] [blame] | 955 | if (!REMOVE_APP_SWITCH_DROPS) { |
Siarhei Vishniakou | 6520a58 | 2023-10-27 21:53:45 -0700 | [diff] [blame] | 956 | if (isAppSwitchDue) { |
| 957 | // The inbound queue is empty so the app switch key we were waiting |
| 958 | // for will never arrive. Stop waiting for it. |
| 959 | resetPendingAppSwitchLocked(false); |
| 960 | isAppSwitchDue = false; |
| 961 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | // Synthesize a key repeat if appropriate. |
| 965 | if (mKeyRepeatState.lastKeyEntry) { |
| 966 | if (currentTime >= mKeyRepeatState.nextRepeatTime) { |
| 967 | mPendingEvent = synthesizeKeyRepeatLocked(currentTime); |
| 968 | } else { |
| 969 | if (mKeyRepeatState.nextRepeatTime < *nextWakeupTime) { |
| 970 | *nextWakeupTime = mKeyRepeatState.nextRepeatTime; |
| 971 | } |
| 972 | } |
| 973 | } |
| 974 | |
| 975 | // Nothing to do if there is no pending event. |
| 976 | if (!mPendingEvent) { |
| 977 | return; |
| 978 | } |
| 979 | } else { |
| 980 | // Inbound queue has at least one entry. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 981 | mPendingEvent = mInboundQueue.front(); |
| 982 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 983 | traceInboundQueueLengthLocked(); |
| 984 | } |
| 985 | |
| 986 | // Poke user activity for this event. |
| 987 | if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 988 | pokeUserActivityLocked(*mPendingEvent); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 989 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | // Now we have an event to dispatch. |
| 993 | // 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] | 994 | ALOG_ASSERT(mPendingEvent != nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 995 | bool done = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 996 | DropReason dropReason = DropReason::NOT_DROPPED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 997 | if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 998 | dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 999 | } else if (!mDispatchEnabled) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1000 | dropReason = DropReason::DISABLED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1001 | } |
| 1002 | |
| 1003 | if (mNextUnblockedEvent == mPendingEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1004 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | switch (mPendingEvent->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1008 | case EventEntry::Type::CONFIGURATION_CHANGED: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1009 | const ConfigurationChangedEntry& typedEntry = |
| 1010 | static_cast<const ConfigurationChangedEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1011 | done = dispatchConfigurationChangedLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1012 | dropReason = DropReason::NOT_DROPPED; // configuration changes are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1013 | break; |
| 1014 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1015 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1016 | case EventEntry::Type::DEVICE_RESET: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1017 | const DeviceResetEntry& typedEntry = |
| 1018 | static_cast<const DeviceResetEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1019 | done = dispatchDeviceResetLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1020 | dropReason = DropReason::NOT_DROPPED; // device resets are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1021 | break; |
| 1022 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1023 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1024 | case EventEntry::Type::FOCUS: { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1025 | std::shared_ptr<const FocusEntry> typedEntry = |
| 1026 | std::static_pointer_cast<const FocusEntry>(mPendingEvent); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1027 | dispatchFocusLocked(currentTime, typedEntry); |
| 1028 | done = true; |
| 1029 | dropReason = DropReason::NOT_DROPPED; // focus events are never dropped |
| 1030 | break; |
| 1031 | } |
| 1032 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1033 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1034 | const auto typedEntry = std::static_pointer_cast<const TouchModeEntry>(mPendingEvent); |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1035 | dispatchTouchModeChangeLocked(currentTime, typedEntry); |
| 1036 | done = true; |
| 1037 | dropReason = DropReason::NOT_DROPPED; // touch mode events are never dropped |
| 1038 | break; |
| 1039 | } |
| 1040 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1041 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 1042 | const auto typedEntry = |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1043 | std::static_pointer_cast<const PointerCaptureChangedEntry>(mPendingEvent); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1044 | dispatchPointerCaptureChangedLocked(currentTime, typedEntry, dropReason); |
| 1045 | done = true; |
| 1046 | break; |
| 1047 | } |
| 1048 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1049 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1050 | std::shared_ptr<const DragEntry> typedEntry = |
| 1051 | std::static_pointer_cast<const DragEntry>(mPendingEvent); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1052 | dispatchDragLocked(currentTime, typedEntry); |
| 1053 | done = true; |
| 1054 | break; |
| 1055 | } |
| 1056 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1057 | case EventEntry::Type::KEY: { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1058 | std::shared_ptr<const KeyEntry> keyEntry = |
| 1059 | std::static_pointer_cast<const KeyEntry>(mPendingEvent); |
Siarhei Vishniakou | 17f1228 | 2023-11-02 13:40:29 -0700 | [diff] [blame] | 1060 | if (!REMOVE_APP_SWITCH_DROPS) { |
Siarhei Vishniakou | 6520a58 | 2023-10-27 21:53:45 -0700 | [diff] [blame] | 1061 | if (isAppSwitchDue) { |
| 1062 | if (isAppSwitchKeyEvent(*keyEntry)) { |
| 1063 | resetPendingAppSwitchLocked(true); |
| 1064 | isAppSwitchDue = false; |
| 1065 | } else if (dropReason == DropReason::NOT_DROPPED) { |
| 1066 | dropReason = DropReason::APP_SWITCH; |
| 1067 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1068 | } |
| 1069 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1070 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *keyEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1071 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1072 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1073 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 1074 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1075 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1076 | done = dispatchKeyLocked(currentTime, keyEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1077 | break; |
| 1078 | } |
| 1079 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1080 | case EventEntry::Type::MOTION: { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1081 | std::shared_ptr<const MotionEntry> motionEntry = |
| 1082 | std::static_pointer_cast<const MotionEntry>(mPendingEvent); |
Siarhei Vishniakou | 17f1228 | 2023-11-02 13:40:29 -0700 | [diff] [blame] | 1083 | if (!REMOVE_APP_SWITCH_DROPS) { |
Siarhei Vishniakou | 6520a58 | 2023-10-27 21:53:45 -0700 | [diff] [blame] | 1084 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 1085 | dropReason = DropReason::APP_SWITCH; |
| 1086 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1087 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1088 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *motionEntry)) { |
Siarhei Vishniakou | 6b71b63 | 2023-10-27 21:34:46 -0700 | [diff] [blame] | 1089 | // The event is stale. However, only drop stale events if there isn't an ongoing |
| 1090 | // gesture. That would allow us to complete the processing of the current stroke. |
| 1091 | const auto touchStateIt = mTouchStatesByDisplay.find(motionEntry->displayId); |
| 1092 | if (touchStateIt != mTouchStatesByDisplay.end()) { |
| 1093 | const TouchState& touchState = touchStateIt->second; |
| 1094 | if (!touchState.hasTouchingPointers(motionEntry->deviceId) && |
| 1095 | !touchState.hasHoveringPointers(motionEntry->deviceId)) { |
| 1096 | dropReason = DropReason::STALE; |
| 1097 | } |
| 1098 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1099 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1100 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
Siarhei Vishniakou | b32ea72 | 2023-12-27 23:31:32 +0000 | [diff] [blame] | 1101 | if (!isFromSource(motionEntry->source, AINPUT_SOURCE_CLASS_POINTER)) { |
| 1102 | // Only drop events that are focus-dispatched. |
| 1103 | dropReason = DropReason::BLOCKED; |
| 1104 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1105 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1106 | done = dispatchMotionLocked(currentTime, motionEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1107 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1108 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1109 | |
| 1110 | case EventEntry::Type::SENSOR: { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1111 | std::shared_ptr<const SensorEntry> sensorEntry = |
| 1112 | std::static_pointer_cast<const SensorEntry>(mPendingEvent); |
Siarhei Vishniakou | 17f1228 | 2023-11-02 13:40:29 -0700 | [diff] [blame] | 1113 | if (!REMOVE_APP_SWITCH_DROPS) { |
Siarhei Vishniakou | 6520a58 | 2023-10-27 21:53:45 -0700 | [diff] [blame] | 1114 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 1115 | dropReason = DropReason::APP_SWITCH; |
| 1116 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1117 | } |
| 1118 | // Sensor timestamps use SYSTEM_TIME_BOOTTIME time base, so we can't use |
| 1119 | // 'currentTime' here, get SYSTEM_TIME_BOOTTIME instead. |
| 1120 | nsecs_t bootTime = systemTime(SYSTEM_TIME_BOOTTIME); |
| 1121 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(bootTime, *sensorEntry)) { |
| 1122 | dropReason = DropReason::STALE; |
| 1123 | } |
| 1124 | dispatchSensorLocked(currentTime, sensorEntry, &dropReason, nextWakeupTime); |
| 1125 | done = true; |
| 1126 | break; |
| 1127 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1128 | } |
| 1129 | |
| 1130 | if (done) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1131 | if (dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1132 | dropInboundEventLocked(*mPendingEvent, dropReason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1133 | } |
Michael Wright | 3a98172 | 2015-06-10 15:26:13 +0100 | [diff] [blame] | 1134 | mLastDropReason = dropReason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1135 | |
| 1136 | releasePendingEventLocked(); |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 1137 | *nextWakeupTime = LLONG_MIN; // force next poll to wake up immediately |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1138 | } |
| 1139 | } |
| 1140 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 1141 | bool InputDispatcher::isStaleEvent(nsecs_t currentTime, const EventEntry& entry) { |
Siarhei Vishniakou | a733311 | 2023-10-27 13:33:29 -0700 | [diff] [blame] | 1142 | return mPolicy.isStaleEvent(currentTime, entry.eventTime); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 1143 | } |
| 1144 | |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1145 | /** |
| 1146 | * Return true if the events preceding this incoming motion event should be dropped |
| 1147 | * Return false otherwise (the default behaviour) |
| 1148 | */ |
| 1149 | bool InputDispatcher::shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1150 | const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN && |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 1151 | isFromSource(motionEntry.source, AINPUT_SOURCE_CLASS_POINTER); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1152 | |
| 1153 | // Optimize case where the current application is unresponsive and the user |
| 1154 | // decides to touch a window in a different application. |
| 1155 | // If the application takes too long to catch up then we drop all events preceding |
| 1156 | // the touch into the other window. |
| 1157 | if (isPointerDownEvent && mAwaitedFocusedApplication != nullptr) { |
Siarhei Vishniakou | 9306c38 | 2022-09-30 15:30:31 -0700 | [diff] [blame] | 1158 | const int32_t displayId = motionEntry.displayId; |
| 1159 | const auto [x, y] = resolveTouchedPosition(motionEntry); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1160 | const bool isStylus = isPointerFromStylus(motionEntry, /*pointerIndex=*/0); |
Siarhei Vishniakou | 9306c38 | 2022-09-30 15:30:31 -0700 | [diff] [blame] | 1161 | |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 1162 | sp<WindowInfoHandle> touchedWindowHandle = |
| 1163 | findTouchedWindowAtLocked(displayId, x, y, isStylus); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1164 | if (touchedWindowHandle != nullptr && |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1165 | touchedWindowHandle->getApplicationToken() != |
| 1166 | mAwaitedFocusedApplication->getApplicationToken()) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1167 | // User touched a different application than the one we are waiting on. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1168 | ALOGI("Pruning input queue because user touched a different application while waiting " |
| 1169 | "for %s", |
| 1170 | mAwaitedFocusedApplication->getName().c_str()); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1171 | return true; |
| 1172 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1173 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1174 | // Alternatively, maybe there's a spy window that could handle this event. |
| 1175 | const std::vector<sp<WindowInfoHandle>> touchedSpies = |
| 1176 | findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus); |
| 1177 | for (const auto& windowHandle : touchedSpies) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 1178 | const std::shared_ptr<Connection> connection = |
| 1179 | getConnectionLocked(windowHandle->getToken()); |
Siarhei Vishniakou | 34ed4d4 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1180 | if (connection != nullptr && connection->responsive) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1181 | // This spy window could take more input. Drop all events preceding this |
| 1182 | // 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] | 1183 | 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] | 1184 | "responsive spy window that may handle the event.", |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1185 | mAwaitedFocusedApplication->getName().c_str()); |
| 1186 | return true; |
| 1187 | } |
| 1188 | } |
| 1189 | } |
| 1190 | |
| 1191 | // Prevent getting stuck: if we have a pending key event, and some motion events that have not |
| 1192 | // yet been processed by some connections, the dispatcher will wait for these motion |
| 1193 | // events to be processed before dispatching the key event. This is because these motion events |
| 1194 | // may cause a new window to be launched, which the user might expect to receive focus. |
| 1195 | // To prevent waiting forever for such events, just send the key to the currently focused window |
| 1196 | if (isPointerDownEvent && mKeyIsWaitingForEventsTimeout) { |
| 1197 | ALOGD("Received a new pointer down event, stop waiting for events to process and " |
| 1198 | "just send the pending key event to the focused window."); |
| 1199 | mKeyIsWaitingForEventsTimeout = now(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1200 | } |
| 1201 | return false; |
| 1202 | } |
| 1203 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1204 | bool InputDispatcher::enqueueInboundEventLocked(std::unique_ptr<EventEntry> newEntry) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1205 | bool needWake = mInboundQueue.empty(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1206 | mInboundQueue.push_back(std::move(newEntry)); |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1207 | const EventEntry& entry = *(mInboundQueue.back()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1208 | traceInboundQueueLengthLocked(); |
| 1209 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1210 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1211 | case EventEntry::Type::KEY: { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1212 | LOG_ALWAYS_FATAL_IF((entry.policyFlags & POLICY_FLAG_TRUSTED) == 0, |
| 1213 | "Unexpected untrusted event."); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1214 | // Optimize app switch latency. |
| 1215 | // If the application takes too long to catch up then we drop all events preceding |
| 1216 | // the app switch key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1217 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
Siarhei Vishniakou | 6520a58 | 2023-10-27 21:53:45 -0700 | [diff] [blame] | 1218 | |
Siarhei Vishniakou | 17f1228 | 2023-11-02 13:40:29 -0700 | [diff] [blame] | 1219 | if (!REMOVE_APP_SWITCH_DROPS) { |
Siarhei Vishniakou | 6520a58 | 2023-10-27 21:53:45 -0700 | [diff] [blame] | 1220 | if (isAppSwitchKeyEvent(keyEntry)) { |
| 1221 | if (keyEntry.action == AKEY_EVENT_ACTION_DOWN) { |
| 1222 | mAppSwitchSawKeyDown = true; |
| 1223 | } else if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
| 1224 | if (mAppSwitchSawKeyDown) { |
| 1225 | if (DEBUG_APP_SWITCH) { |
| 1226 | ALOGD("App switch is pending!"); |
| 1227 | } |
| 1228 | mAppSwitchDueTime = keyEntry.eventTime + APP_SWITCH_TIMEOUT; |
| 1229 | mAppSwitchSawKeyDown = false; |
| 1230 | needWake = true; |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1231 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1232 | } |
| 1233 | } |
| 1234 | } |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 1235 | // If a new up event comes in, and the pending event with same key code has been asked |
| 1236 | // to try again later because of the policy. We have to reset the intercept key wake up |
| 1237 | // time for it may have been handled in the policy and could be dropped. |
| 1238 | if (keyEntry.action == AKEY_EVENT_ACTION_UP && mPendingEvent && |
| 1239 | mPendingEvent->type == EventEntry::Type::KEY) { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1240 | const KeyEntry& pendingKey = static_cast<const KeyEntry&>(*mPendingEvent); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 1241 | if (pendingKey.keyCode == keyEntry.keyCode && |
| 1242 | pendingKey.interceptKeyResult == |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1243 | KeyEntry::InterceptKeyResult::TRY_AGAIN_LATER) { |
| 1244 | pendingKey.interceptKeyResult = KeyEntry::InterceptKeyResult::UNKNOWN; |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 1245 | pendingKey.interceptKeyWakeupTime = 0; |
| 1246 | needWake = true; |
| 1247 | } |
| 1248 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1249 | break; |
| 1250 | } |
| 1251 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1252 | case EventEntry::Type::MOTION: { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1253 | LOG_ALWAYS_FATAL_IF((entry.policyFlags & POLICY_FLAG_TRUSTED) == 0, |
| 1254 | "Unexpected untrusted event."); |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1255 | if (shouldPruneInboundQueueLocked(static_cast<const MotionEntry&>(entry))) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1256 | mNextUnblockedEvent = mInboundQueue.back(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1257 | needWake = true; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1258 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1259 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1260 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1261 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1262 | LOG_ALWAYS_FATAL("Focus events should be inserted using enqueueFocusEventLocked"); |
| 1263 | break; |
| 1264 | } |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1265 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1266 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1267 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1268 | case EventEntry::Type::SENSOR: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1269 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1270 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1271 | // nothing to do |
| 1272 | break; |
| 1273 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1274 | } |
| 1275 | |
| 1276 | return needWake; |
| 1277 | } |
| 1278 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1279 | void InputDispatcher::addRecentEventLocked(std::shared_ptr<const EventEntry> entry) { |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1280 | // Do not store sensor event in recent queue to avoid flooding the queue. |
| 1281 | if (entry->type != EventEntry::Type::SENSOR) { |
| 1282 | mRecentQueue.push_back(entry); |
| 1283 | } |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1284 | if (mRecentQueue.size() > RECENT_QUEUE_MAX_SIZE) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1285 | mRecentQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1286 | } |
| 1287 | } |
| 1288 | |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 1289 | sp<WindowInfoHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, float x, float y, |
| 1290 | bool isStylus, |
| 1291 | bool ignoreDragWindow) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1292 | // Traverse windows from front to back to find touched window. |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1293 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1294 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 1295 | if (ignoreDragWindow && haveSameToken(windowHandle, mDragState->dragWindow)) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1296 | continue; |
| 1297 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1298 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1299 | const WindowInfo& info = *windowHandle->getInfo(); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 1300 | if (!info.isSpy() && |
| 1301 | windowAcceptsTouchAt(info, displayId, x, y, isStylus, getTransformLocked(displayId))) { |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 1302 | return windowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1303 | } |
| 1304 | } |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 1305 | return nullptr; |
| 1306 | } |
| 1307 | |
| 1308 | std::vector<InputTarget> InputDispatcher::findOutsideTargetsLocked( |
Siarhei Vishniakou | 70f3d8c | 2023-09-19 15:36:52 -0700 | [diff] [blame] | 1309 | int32_t displayId, const sp<WindowInfoHandle>& touchedWindow, int32_t pointerId) const { |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 1310 | if (touchedWindow == nullptr) { |
| 1311 | return {}; |
| 1312 | } |
| 1313 | // Traverse windows from front to back until we encounter the touched window. |
| 1314 | std::vector<InputTarget> outsideTargets; |
| 1315 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
| 1316 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 1317 | if (windowHandle == touchedWindow) { |
| 1318 | // Stop iterating once we found a touched window. Any WATCH_OUTSIDE_TOUCH window |
| 1319 | // below the touched window will not get ACTION_OUTSIDE event. |
| 1320 | return outsideTargets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1321 | } |
| 1322 | |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 1323 | const WindowInfo& info = *windowHandle->getInfo(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1324 | if (info.inputConfig.test(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH)) { |
Siarhei Vishniakou | 70f3d8c | 2023-09-19 15:36:52 -0700 | [diff] [blame] | 1325 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
| 1326 | pointerIds.set(pointerId); |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 1327 | addPointerWindowTargetLocked(windowHandle, InputTarget::DispatchMode::OUTSIDE, |
| 1328 | ftl::Flags<InputTarget::Flags>(), pointerIds, |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 1329 | /*firstDownTimeInTarget=*/std::nullopt, outsideTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1330 | } |
| 1331 | } |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 1332 | return outsideTargets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1333 | } |
| 1334 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1335 | std::vector<sp<WindowInfoHandle>> InputDispatcher::findTouchedSpyWindowsAtLocked( |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 1336 | int32_t displayId, float x, float y, bool isStylus) const { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1337 | // Traverse windows from front to back and gather the touched spy windows. |
| 1338 | std::vector<sp<WindowInfoHandle>> spyWindows; |
| 1339 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
| 1340 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 1341 | const WindowInfo& info = *windowHandle->getInfo(); |
| 1342 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 1343 | if (!windowAcceptsTouchAt(info, displayId, x, y, isStylus, getTransformLocked(displayId))) { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1344 | continue; |
| 1345 | } |
| 1346 | if (!info.isSpy()) { |
| 1347 | // The first touched non-spy window was found, so return the spy windows touched so far. |
| 1348 | return spyWindows; |
| 1349 | } |
| 1350 | spyWindows.push_back(windowHandle); |
| 1351 | } |
| 1352 | return spyWindows; |
| 1353 | } |
| 1354 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1355 | void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1356 | const char* reason; |
| 1357 | switch (dropReason) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1358 | case DropReason::POLICY: |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 1359 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1360 | ALOGD("Dropped event because policy consumed it."); |
| 1361 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1362 | reason = "inbound event was dropped because the policy consumed it"; |
| 1363 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1364 | case DropReason::DISABLED: |
| 1365 | if (mLastDropReason != DropReason::DISABLED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1366 | ALOGI("Dropped event because input dispatch is disabled."); |
| 1367 | } |
| 1368 | reason = "inbound event was dropped because input dispatch is disabled"; |
| 1369 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1370 | case DropReason::APP_SWITCH: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1371 | ALOGI("Dropped event because of pending overdue app switch."); |
| 1372 | reason = "inbound event was dropped because of pending overdue app switch"; |
| 1373 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1374 | case DropReason::BLOCKED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1375 | ALOGI("Dropped event because the current application is not responding and the user " |
| 1376 | "has started interacting with a different application."); |
| 1377 | reason = "inbound event was dropped because the current application is not responding " |
| 1378 | "and the user has started interacting with a different application"; |
| 1379 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1380 | case DropReason::STALE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1381 | ALOGI("Dropped event because it is stale."); |
| 1382 | reason = "inbound event was dropped because it is stale"; |
| 1383 | break; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1384 | case DropReason::NO_POINTER_CAPTURE: |
| 1385 | ALOGI("Dropped event because there is no window with Pointer Capture."); |
| 1386 | reason = "inbound event was dropped because there is no window with Pointer Capture"; |
| 1387 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1388 | case DropReason::NOT_DROPPED: { |
| 1389 | LOG_ALWAYS_FATAL("Should not be dropping a NOT_DROPPED event"); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1390 | return; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1391 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1392 | } |
| 1393 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1394 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1395 | case EventEntry::Type::KEY: { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1396 | CancelationOptions options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, reason); |
Hu Guo | 3cfa738 | 2023-11-15 09:50:04 +0000 | [diff] [blame] | 1397 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 1398 | options.displayId = keyEntry.displayId; |
| 1399 | options.deviceId = keyEntry.deviceId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1400 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1401 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1402 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1403 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1404 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1405 | if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1406 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, reason); |
Hu Guo | 3cfa738 | 2023-11-15 09:50:04 +0000 | [diff] [blame] | 1407 | options.displayId = motionEntry.displayId; |
| 1408 | options.deviceId = motionEntry.deviceId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1409 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1410 | } else { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1411 | CancelationOptions options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, |
| 1412 | reason); |
Hu Guo | 3cfa738 | 2023-11-15 09:50:04 +0000 | [diff] [blame] | 1413 | options.displayId = motionEntry.displayId; |
| 1414 | options.deviceId = motionEntry.deviceId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1415 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1416 | } |
| 1417 | break; |
| 1418 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1419 | case EventEntry::Type::SENSOR: { |
| 1420 | break; |
| 1421 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1422 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1423 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1424 | break; |
| 1425 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1426 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1427 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1428 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 1429 | case EventEntry::Type::DEVICE_RESET: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1430 | 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] | 1431 | break; |
| 1432 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1433 | } |
| 1434 | } |
| 1435 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1436 | static bool isAppSwitchKeyCode(int32_t keyCode) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1437 | return keyCode == AKEYCODE_HOME || keyCode == AKEYCODE_ENDCALL || |
| 1438 | keyCode == AKEYCODE_APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1439 | } |
| 1440 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1441 | bool InputDispatcher::isAppSwitchKeyEvent(const KeyEntry& keyEntry) { |
| 1442 | return !(keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) && isAppSwitchKeyCode(keyEntry.keyCode) && |
| 1443 | (keyEntry.policyFlags & POLICY_FLAG_TRUSTED) && |
| 1444 | (keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1445 | } |
| 1446 | |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 1447 | bool InputDispatcher::isAppSwitchPendingLocked() const { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 1448 | return mAppSwitchDueTime != LLONG_MAX; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1449 | } |
| 1450 | |
| 1451 | void InputDispatcher::resetPendingAppSwitchLocked(bool handled) { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 1452 | mAppSwitchDueTime = LLONG_MAX; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1453 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1454 | if (DEBUG_APP_SWITCH) { |
| 1455 | if (handled) { |
| 1456 | ALOGD("App switch has arrived."); |
| 1457 | } else { |
| 1458 | ALOGD("App switch was abandoned."); |
| 1459 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1460 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1461 | } |
| 1462 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1463 | bool InputDispatcher::haveCommandsLocked() const { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1464 | return !mCommandQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1465 | } |
| 1466 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1467 | bool InputDispatcher::runCommandsLockedInterruptable() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1468 | if (mCommandQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1469 | return false; |
| 1470 | } |
| 1471 | |
| 1472 | do { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1473 | auto command = std::move(mCommandQueue.front()); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1474 | mCommandQueue.pop_front(); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1475 | // Commands are run with the lock held, but may release and re-acquire the lock from within. |
| 1476 | command(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1477 | } while (!mCommandQueue.empty()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1478 | return true; |
| 1479 | } |
| 1480 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1481 | void InputDispatcher::postCommandLocked(Command&& command) { |
| 1482 | mCommandQueue.push_back(command); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1483 | } |
| 1484 | |
| 1485 | void InputDispatcher::drainInboundQueueLocked() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1486 | while (!mInboundQueue.empty()) { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1487 | std::shared_ptr<const EventEntry> entry = mInboundQueue.front(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1488 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1489 | releaseInboundEventLocked(entry); |
| 1490 | } |
| 1491 | traceInboundQueueLengthLocked(); |
| 1492 | } |
| 1493 | |
| 1494 | void InputDispatcher::releasePendingEventLocked() { |
| 1495 | if (mPendingEvent) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1496 | releaseInboundEventLocked(mPendingEvent); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1497 | mPendingEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1498 | } |
| 1499 | } |
| 1500 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1501 | void InputDispatcher::releaseInboundEventLocked(std::shared_ptr<const EventEntry> entry) { |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 1502 | const std::shared_ptr<InjectionState>& injectionState = entry->injectionState; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1503 | if (injectionState && injectionState->injectionResult == InputEventInjectionResult::PENDING) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1504 | if (DEBUG_DISPATCH_CYCLE) { |
| 1505 | ALOGD("Injected inbound event was dropped."); |
| 1506 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1507 | setInjectionResult(*entry, InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1508 | } |
| 1509 | if (entry == mNextUnblockedEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1510 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1511 | } |
| 1512 | addRecentEventLocked(entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1513 | } |
| 1514 | |
| 1515 | void InputDispatcher::resetKeyRepeatLocked() { |
| 1516 | if (mKeyRepeatState.lastKeyEntry) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1517 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1518 | } |
| 1519 | } |
| 1520 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1521 | std::shared_ptr<KeyEntry> InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1522 | std::shared_ptr<const KeyEntry> entry = mKeyRepeatState.lastKeyEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1523 | |
Michael Wright | 2e73295 | 2014-09-24 13:26:59 -0700 | [diff] [blame] | 1524 | uint32_t policyFlags = entry->policyFlags & |
| 1525 | (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1526 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1527 | std::shared_ptr<KeyEntry> newEntry = |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 1528 | std::make_unique<KeyEntry>(mIdGenerator.nextId(), /*injectionState=*/nullptr, |
| 1529 | currentTime, entry->deviceId, entry->source, |
| 1530 | entry->displayId, policyFlags, entry->action, entry->flags, |
| 1531 | entry->keyCode, entry->scanCode, entry->metaState, |
| 1532 | entry->repeatCount + 1, entry->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1533 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1534 | newEntry->syntheticRepeat = true; |
| 1535 | mKeyRepeatState.lastKeyEntry = newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1536 | mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1537 | return newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1538 | } |
| 1539 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1540 | bool InputDispatcher::dispatchConfigurationChangedLocked(nsecs_t currentTime, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1541 | const ConfigurationChangedEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1542 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1543 | ALOGD("dispatchConfigurationChanged - eventTime=%" PRId64, entry.eventTime); |
| 1544 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1545 | |
| 1546 | // Reset key repeating in case a keyboard device was added or removed or something. |
| 1547 | resetKeyRepeatLocked(); |
| 1548 | |
| 1549 | // 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] | 1550 | auto command = [this, eventTime = entry.eventTime]() REQUIRES(mLock) { |
| 1551 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 1552 | mPolicy.notifyConfigurationChanged(eventTime); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1553 | }; |
| 1554 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1555 | return true; |
| 1556 | } |
| 1557 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1558 | bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime, |
| 1559 | const DeviceResetEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1560 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1561 | ALOGD("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry.eventTime, |
| 1562 | entry.deviceId); |
| 1563 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1564 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 1565 | // Reset key repeating in case a keyboard device was disabled or enabled. |
| 1566 | if (mKeyRepeatState.lastKeyEntry && mKeyRepeatState.lastKeyEntry->deviceId == entry.deviceId) { |
| 1567 | resetKeyRepeatLocked(); |
| 1568 | } |
| 1569 | |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1570 | CancelationOptions options(CancelationOptions::Mode::CANCEL_ALL_EVENTS, "device was reset"); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1571 | options.deviceId = entry.deviceId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1572 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 1573 | |
| 1574 | // Remove all active pointers from this device |
| 1575 | for (auto& [_, touchState] : mTouchStatesByDisplay) { |
| 1576 | touchState.removeAllPointersForDevice(entry.deviceId); |
| 1577 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1578 | return true; |
| 1579 | } |
| 1580 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1581 | void InputDispatcher::enqueueFocusEventLocked(const sp<IBinder>& windowToken, bool hasFocus, |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1582 | const std::string& reason) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1583 | if (mPendingEvent != nullptr) { |
| 1584 | // Move the pending event to the front of the queue. This will give the chance |
| 1585 | // for the pending event to get dispatched to the newly focused window |
| 1586 | mInboundQueue.push_front(mPendingEvent); |
| 1587 | mPendingEvent = nullptr; |
| 1588 | } |
| 1589 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1590 | std::unique_ptr<FocusEntry> focusEntry = |
| 1591 | std::make_unique<FocusEntry>(mIdGenerator.nextId(), now(), windowToken, hasFocus, |
| 1592 | reason); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1593 | |
| 1594 | // This event should go to the front of the queue, but behind all other focus events |
| 1595 | // Find the last focus event, and insert right after it |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1596 | auto it = std::find_if(mInboundQueue.rbegin(), mInboundQueue.rend(), |
| 1597 | [](const std::shared_ptr<const EventEntry>& event) { |
| 1598 | return event->type == EventEntry::Type::FOCUS; |
| 1599 | }); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1600 | |
| 1601 | // 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] | 1602 | mInboundQueue.insert(it.base(), std::move(focusEntry)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1603 | } |
| 1604 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1605 | void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime, |
| 1606 | std::shared_ptr<const FocusEntry> entry) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 1607 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1608 | if (channel == nullptr) { |
| 1609 | return; // Window has gone away |
| 1610 | } |
| 1611 | InputTarget target; |
| 1612 | target.inputChannel = channel; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1613 | entry->dispatchInProgress = true; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1614 | std::string message = std::string("Focus ") + (entry->hasFocus ? "entering " : "leaving ") + |
| 1615 | channel->getName(); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 1616 | std::string reason = std::string("reason=").append(entry->reason); |
| 1617 | android_log_event_list(LOGTAG_INPUT_FOCUS) << message << reason << LOG_ID_EVENTS; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1618 | dispatchEventLocked(currentTime, entry, {target}); |
| 1619 | } |
| 1620 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1621 | void InputDispatcher::dispatchPointerCaptureChangedLocked( |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1622 | nsecs_t currentTime, const std::shared_ptr<const PointerCaptureChangedEntry>& entry, |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1623 | DropReason& dropReason) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1624 | dropReason = DropReason::NOT_DROPPED; |
| 1625 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1626 | const bool haveWindowWithPointerCapture = mWindowTokenWithPointerCapture != nullptr; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1627 | sp<IBinder> token; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1628 | |
| 1629 | if (entry->pointerCaptureRequest.enable) { |
| 1630 | // Enable Pointer Capture. |
| 1631 | if (haveWindowWithPointerCapture && |
| 1632 | (entry->pointerCaptureRequest == mCurrentPointerCaptureRequest)) { |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 1633 | // This can happen if pointer capture is disabled and re-enabled before we notify the |
| 1634 | // app of the state change, so there is no need to notify the app. |
| 1635 | ALOGI("Skipping dispatch of Pointer Capture being enabled: no state change."); |
| 1636 | return; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1637 | } |
| 1638 | if (!mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1639 | // This can happen if a window requests capture and immediately releases capture. |
| 1640 | ALOGW("No window requested Pointer Capture."); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1641 | dropReason = DropReason::NO_POINTER_CAPTURE; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1642 | return; |
| 1643 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1644 | if (entry->pointerCaptureRequest.seq != mCurrentPointerCaptureRequest.seq) { |
| 1645 | ALOGI("Skipping dispatch of Pointer Capture being enabled: sequence number mismatch."); |
| 1646 | return; |
| 1647 | } |
| 1648 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1649 | token = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1650 | LOG_ALWAYS_FATAL_IF(!token, "Cannot find focused window for Pointer Capture."); |
| 1651 | mWindowTokenWithPointerCapture = token; |
| 1652 | } else { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1653 | // Disable Pointer Capture. |
| 1654 | // We do not check if the sequence number matches for requests to disable Pointer Capture |
| 1655 | // for two reasons: |
| 1656 | // 1. Pointer Capture can be disabled by a focus change, which means we can get two entries |
| 1657 | // to disable capture with the same sequence number: one generated by |
| 1658 | // disablePointerCaptureForcedLocked() and another as an acknowledgement of Pointer |
| 1659 | // Capture being disabled in InputReader. |
| 1660 | // 2. We respect any request to disable Pointer Capture generated by InputReader, since the |
| 1661 | // actual Pointer Capture state that affects events being generated by input devices is |
| 1662 | // in InputReader. |
| 1663 | if (!haveWindowWithPointerCapture) { |
| 1664 | // Pointer capture was already forcefully disabled because of focus change. |
| 1665 | dropReason = DropReason::NOT_DROPPED; |
| 1666 | return; |
| 1667 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1668 | token = mWindowTokenWithPointerCapture; |
| 1669 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1670 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1671 | setPointerCaptureLocked(false); |
| 1672 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1673 | } |
| 1674 | |
| 1675 | auto channel = getInputChannelLocked(token); |
| 1676 | if (channel == nullptr) { |
| 1677 | // Window has gone away, clean up Pointer Capture state. |
| 1678 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1679 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1680 | setPointerCaptureLocked(false); |
| 1681 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1682 | return; |
| 1683 | } |
| 1684 | InputTarget target; |
| 1685 | target.inputChannel = channel; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1686 | entry->dispatchInProgress = true; |
| 1687 | dispatchEventLocked(currentTime, entry, {target}); |
| 1688 | |
| 1689 | dropReason = DropReason::NOT_DROPPED; |
| 1690 | } |
| 1691 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1692 | void InputDispatcher::dispatchTouchModeChangeLocked( |
| 1693 | nsecs_t currentTime, const std::shared_ptr<const TouchModeEntry>& entry) { |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1694 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 1695 | getWindowHandlesLocked(entry->displayId); |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1696 | if (windowHandles.empty()) { |
| 1697 | return; |
| 1698 | } |
| 1699 | const std::vector<InputTarget> inputTargets = |
| 1700 | getInputTargetsFromWindowHandlesLocked(windowHandles); |
| 1701 | if (inputTargets.empty()) { |
| 1702 | return; |
| 1703 | } |
| 1704 | entry->dispatchInProgress = true; |
| 1705 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1706 | } |
| 1707 | |
| 1708 | std::vector<InputTarget> InputDispatcher::getInputTargetsFromWindowHandlesLocked( |
| 1709 | const std::vector<sp<WindowInfoHandle>>& windowHandles) const { |
| 1710 | std::vector<InputTarget> inputTargets; |
| 1711 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1712 | const sp<IBinder>& token = handle->getToken(); |
| 1713 | if (token == nullptr) { |
| 1714 | continue; |
| 1715 | } |
| 1716 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(token); |
| 1717 | if (channel == nullptr) { |
| 1718 | continue; // Window has gone away |
| 1719 | } |
| 1720 | InputTarget target; |
| 1721 | target.inputChannel = channel; |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1722 | inputTargets.push_back(target); |
| 1723 | } |
| 1724 | return inputTargets; |
| 1725 | } |
| 1726 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1727 | bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, std::shared_ptr<const KeyEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1728 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1729 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1730 | if (!entry->dispatchInProgress) { |
| 1731 | if (entry->repeatCount == 0 && entry->action == AKEY_EVENT_ACTION_DOWN && |
| 1732 | (entry->policyFlags & POLICY_FLAG_TRUSTED) && |
| 1733 | (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { |
| 1734 | if (mKeyRepeatState.lastKeyEntry && |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1735 | mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode && |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1736 | // We have seen two identical key downs in a row which indicates that the device |
| 1737 | // driver is automatically generating key repeats itself. We take note of the |
| 1738 | // repeat here, but we disable our own next key repeat timer since it is clear that |
| 1739 | // we will not need to synthesize key repeats ourselves. |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1740 | mKeyRepeatState.lastKeyEntry->deviceId == entry->deviceId) { |
| 1741 | // Make sure we don't get key down from a different device. If a different |
| 1742 | // device Id has same key pressed down, the new device Id will replace the |
| 1743 | // current one to hold the key repeat with repeat count reset. |
| 1744 | // In the future when got a KEY_UP on the device id, drop it and do not |
| 1745 | // stop the key repeat on current device. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1746 | entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; |
| 1747 | resetKeyRepeatLocked(); |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 1748 | mKeyRepeatState.nextRepeatTime = LLONG_MAX; // don't generate repeats ourselves |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1749 | } else { |
| 1750 | // Not a repeat. Save key down state in case we do see a repeat later. |
| 1751 | resetKeyRepeatLocked(); |
| 1752 | mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; |
| 1753 | } |
| 1754 | mKeyRepeatState.lastKeyEntry = entry; |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1755 | } else if (entry->action == AKEY_EVENT_ACTION_UP && mKeyRepeatState.lastKeyEntry && |
| 1756 | mKeyRepeatState.lastKeyEntry->deviceId != entry->deviceId) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1757 | // 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] | 1758 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1759 | ALOGD("deviceId=%d got KEY_UP as stale", entry->deviceId); |
| 1760 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1761 | } else if (!entry->syntheticRepeat) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1762 | resetKeyRepeatLocked(); |
| 1763 | } |
| 1764 | |
| 1765 | if (entry->repeatCount == 1) { |
| 1766 | entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; |
| 1767 | } else { |
| 1768 | entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; |
| 1769 | } |
| 1770 | |
| 1771 | entry->dispatchInProgress = true; |
| 1772 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1773 | logOutboundKeyDetails("dispatchKey - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1774 | } |
| 1775 | |
| 1776 | // 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] | 1777 | if (entry->interceptKeyResult == KeyEntry::InterceptKeyResult::TRY_AGAIN_LATER) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1778 | if (currentTime < entry->interceptKeyWakeupTime) { |
| 1779 | if (entry->interceptKeyWakeupTime < *nextWakeupTime) { |
| 1780 | *nextWakeupTime = entry->interceptKeyWakeupTime; |
| 1781 | } |
| 1782 | return false; // wait until next wakeup |
| 1783 | } |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1784 | entry->interceptKeyResult = KeyEntry::InterceptKeyResult::UNKNOWN; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1785 | entry->interceptKeyWakeupTime = 0; |
| 1786 | } |
| 1787 | |
| 1788 | // Give the policy a chance to intercept the key. |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1789 | if (entry->interceptKeyResult == KeyEntry::InterceptKeyResult::UNKNOWN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1790 | if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1791 | sp<IBinder> focusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1792 | mFocusResolver.getFocusedWindowToken(getTargetDisplayId(*entry)); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1793 | |
| 1794 | auto command = [this, focusedWindowToken, entry]() REQUIRES(mLock) { |
| 1795 | doInterceptKeyBeforeDispatchingCommand(focusedWindowToken, *entry); |
| 1796 | }; |
| 1797 | postCommandLocked(std::move(command)); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1798 | // Poke user activity for keys not passed to user |
| 1799 | pokeUserActivityLocked(*entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1800 | return false; // wait for the command to run |
| 1801 | } else { |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1802 | entry->interceptKeyResult = KeyEntry::InterceptKeyResult::CONTINUE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1803 | } |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1804 | } else if (entry->interceptKeyResult == KeyEntry::InterceptKeyResult::SKIP) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1805 | if (*dropReason == DropReason::NOT_DROPPED) { |
| 1806 | *dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1807 | } |
| 1808 | } |
| 1809 | |
| 1810 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1811 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1812 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1813 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1814 | : InputEventInjectionResult::FAILED); |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1815 | mReporter->reportDroppedKey(entry->id); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1816 | // Poke user activity for undispatched keys |
| 1817 | pokeUserActivityLocked(*entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1818 | return true; |
| 1819 | } |
| 1820 | |
| 1821 | // Identify targets. |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1822 | InputEventInjectionResult injectionResult; |
| 1823 | sp<WindowInfoHandle> focusedWindow = |
| 1824 | findFocusedWindowTargetLocked(currentTime, *entry, nextWakeupTime, |
| 1825 | /*byref*/ injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1826 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1827 | return false; |
| 1828 | } |
| 1829 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1830 | setInjectionResult(*entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1831 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1832 | return true; |
| 1833 | } |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1834 | LOG_ALWAYS_FATAL_IF(focusedWindow == nullptr); |
| 1835 | |
| 1836 | std::vector<InputTarget> inputTargets; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 1837 | addWindowTargetLocked(focusedWindow, InputTarget::DispatchMode::AS_IS, |
| 1838 | InputTarget::Flags::FOREGROUND, getDownTime(*entry), inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1839 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1840 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1841 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1842 | |
| 1843 | // Dispatch the key. |
| 1844 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1845 | return true; |
| 1846 | } |
| 1847 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1848 | void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1849 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1850 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 ", " |
| 1851 | "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, " |
| 1852 | "metaState=0x%x, repeatCount=%d, downTime=%" PRId64, |
| 1853 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, |
| 1854 | entry.policyFlags, entry.action, entry.flags, entry.keyCode, entry.scanCode, |
| 1855 | entry.metaState, entry.repeatCount, entry.downTime); |
| 1856 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1857 | } |
| 1858 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1859 | void InputDispatcher::dispatchSensorLocked(nsecs_t currentTime, |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1860 | const std::shared_ptr<const SensorEntry>& entry, |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1861 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1862 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1863 | ALOGD("notifySensorEvent eventTime=%" PRId64 ", hwTimestamp=%" PRId64 ", deviceId=%d, " |
| 1864 | "source=0x%x, sensorType=%s", |
| 1865 | entry->eventTime, entry->hwTimestamp, entry->deviceId, entry->source, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1866 | ftl::enum_string(entry->sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1867 | } |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1868 | auto command = [this, entry]() REQUIRES(mLock) { |
| 1869 | scoped_unlock unlock(mLock); |
| 1870 | |
| 1871 | if (entry->accuracyChanged) { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 1872 | mPolicy.notifySensorAccuracy(entry->deviceId, entry->sensorType, entry->accuracy); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1873 | } |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 1874 | mPolicy.notifySensorEvent(entry->deviceId, entry->sensorType, entry->accuracy, |
| 1875 | entry->hwTimestamp, entry->values); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1876 | }; |
| 1877 | postCommandLocked(std::move(command)); |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1878 | } |
| 1879 | |
| 1880 | bool InputDispatcher::flushSensor(int deviceId, InputDeviceSensorType sensorType) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1881 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1882 | ALOGD("flushSensor deviceId=%d, sensorType=%s", deviceId, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1883 | ftl::enum_string(sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1884 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1885 | { // acquire lock |
| 1886 | std::scoped_lock _l(mLock); |
| 1887 | |
| 1888 | for (auto it = mInboundQueue.begin(); it != mInboundQueue.end(); it++) { |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1889 | std::shared_ptr<const EventEntry> entry = *it; |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1890 | if (entry->type == EventEntry::Type::SENSOR) { |
| 1891 | it = mInboundQueue.erase(it); |
| 1892 | releaseInboundEventLocked(entry); |
| 1893 | } |
| 1894 | } |
| 1895 | } |
| 1896 | return true; |
| 1897 | } |
| 1898 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1899 | bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, |
| 1900 | std::shared_ptr<const MotionEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1901 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1902 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1903 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1904 | if (!entry->dispatchInProgress) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1905 | entry->dispatchInProgress = true; |
| 1906 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1907 | logOutboundMotionDetails("dispatchMotion - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1908 | } |
| 1909 | |
| 1910 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1911 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1912 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1913 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1914 | : InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1915 | return true; |
| 1916 | } |
| 1917 | |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 1918 | const bool isPointerEvent = isFromSource(entry->source, AINPUT_SOURCE_CLASS_POINTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1919 | |
| 1920 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1921 | std::vector<InputTarget> inputTargets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1922 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1923 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1924 | if (isPointerEvent) { |
| 1925 | // Pointer event. (eg. touchscreen) |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 1926 | |
| 1927 | if (mDragState && |
| 1928 | (entry->action & AMOTION_EVENT_ACTION_MASK) == AMOTION_EVENT_ACTION_POINTER_DOWN) { |
| 1929 | // If drag and drop ongoing and pointer down occur: pilfer drag window pointers |
| 1930 | pilferPointersLocked(mDragState->dragWindow->getToken()); |
| 1931 | } |
| 1932 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1933 | inputTargets = |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 1934 | findTouchedWindowTargetsLocked(currentTime, *entry, /*byref*/ injectionResult); |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 1935 | LOG_ALWAYS_FATAL_IF(injectionResult != InputEventInjectionResult::SUCCEEDED && |
| 1936 | !inputTargets.empty()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1937 | } else { |
| 1938 | // Non touch event. (eg. trackball) |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1939 | sp<WindowInfoHandle> focusedWindow = |
| 1940 | findFocusedWindowTargetLocked(currentTime, *entry, nextWakeupTime, injectionResult); |
| 1941 | if (injectionResult == InputEventInjectionResult::SUCCEEDED) { |
| 1942 | LOG_ALWAYS_FATAL_IF(focusedWindow == nullptr); |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 1943 | addWindowTargetLocked(focusedWindow, InputTarget::DispatchMode::AS_IS, |
| 1944 | InputTarget::Flags::FOREGROUND, getDownTime(*entry), |
| 1945 | inputTargets); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1946 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1947 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1948 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1949 | return false; |
| 1950 | } |
| 1951 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1952 | setInjectionResult(*entry, injectionResult); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1953 | if (injectionResult == InputEventInjectionResult::TARGET_MISMATCH) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1954 | return true; |
| 1955 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1956 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1957 | CancelationOptions::Mode mode( |
| 1958 | isPointerEvent ? CancelationOptions::Mode::CANCEL_POINTER_EVENTS |
| 1959 | : CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS); |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1960 | CancelationOptions options(mode, "input event injection failed"); |
| 1961 | synthesizeCancelationEventsForMonitorsLocked(options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1962 | return true; |
| 1963 | } |
| 1964 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1965 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1966 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1967 | |
| 1968 | // Dispatch the motion. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1969 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1970 | return true; |
| 1971 | } |
| 1972 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1973 | void InputDispatcher::enqueueDragEventLocked(const sp<WindowInfoHandle>& windowHandle, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 1974 | bool isExiting, const int32_t rawX, |
| 1975 | const int32_t rawY) { |
| 1976 | const vec2 xy = windowHandle->getInfo()->transform.transform(vec2(rawX, rawY)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1977 | std::unique_ptr<DragEntry> dragEntry = |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 1978 | std::make_unique<DragEntry>(mIdGenerator.nextId(), now(), windowHandle->getToken(), |
| 1979 | isExiting, xy.x, xy.y); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1980 | |
| 1981 | enqueueInboundEventLocked(std::move(dragEntry)); |
| 1982 | } |
| 1983 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 1984 | void InputDispatcher::dispatchDragLocked(nsecs_t currentTime, |
| 1985 | std::shared_ptr<const DragEntry> entry) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1986 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
| 1987 | if (channel == nullptr) { |
| 1988 | return; // Window has gone away |
| 1989 | } |
| 1990 | InputTarget target; |
| 1991 | target.inputChannel = channel; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1992 | entry->dispatchInProgress = true; |
| 1993 | dispatchEventLocked(currentTime, entry, {target}); |
| 1994 | } |
| 1995 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1996 | void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1997 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 1998 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=%s, displayId=%" PRId32 |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1999 | ", policyFlags=0x%x, " |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2000 | "action=%s, actionButton=0x%x, flags=0x%x, " |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2001 | "metaState=0x%x, buttonState=0x%x," |
| 2002 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64, |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 2003 | prefix, entry.eventTime, entry.deviceId, |
| 2004 | inputEventSourceToString(entry.source).c_str(), entry.displayId, entry.policyFlags, |
| 2005 | MotionEvent::actionToString(entry.action).c_str(), entry.actionButton, entry.flags, |
| 2006 | entry.metaState, entry.buttonState, entry.edgeFlags, entry.xPrecision, |
| 2007 | entry.yPrecision, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2008 | |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 2009 | for (uint32_t i = 0; i < entry.getPointerCount(); i++) { |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2010 | ALOGD(" Pointer %d: id=%d, toolType=%s, " |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2011 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 2012 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 2013 | "orientation=%f", |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2014 | i, entry.pointerProperties[i].id, |
| 2015 | ftl::enum_string(entry.pointerProperties[i].toolType).c_str(), |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2016 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 2017 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 2018 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 2019 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 2020 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 2021 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 2022 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 2023 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 2024 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| 2025 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2026 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2027 | } |
| 2028 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2029 | void InputDispatcher::dispatchEventLocked(nsecs_t currentTime, |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 2030 | std::shared_ptr<const EventEntry> eventEntry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2031 | const std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2032 | ATRACE_CALL(); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2033 | if (DEBUG_DISPATCH_CYCLE) { |
| 2034 | ALOGD("dispatchEventToCurrentInputTargets"); |
| 2035 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2036 | |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 2037 | processInteractionsLocked(*eventEntry, inputTargets); |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 2038 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2039 | ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true |
| 2040 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2041 | pokeUserActivityLocked(*eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2042 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2043 | for (const InputTarget& inputTarget : inputTargets) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 2044 | std::shared_ptr<Connection> connection = |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 2045 | getConnectionLocked(inputTarget.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 2046 | if (connection != nullptr) { |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2047 | prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2048 | } else { |
Siarhei Vishniakou | 31dd155 | 2023-10-30 18:46:10 -0700 | [diff] [blame] | 2049 | if (DEBUG_DROPPED_EVENTS_VERBOSE) { |
| 2050 | LOG(INFO) << "Dropping event delivery to target with channel " |
| 2051 | << inputTarget.inputChannel->getName() |
| 2052 | << " because it is no longer registered with the input dispatcher."; |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2053 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2054 | } |
| 2055 | } |
| 2056 | } |
| 2057 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 2058 | void InputDispatcher::cancelEventsForAnrLocked(const std::shared_ptr<Connection>& connection) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2059 | // We will not be breaking any connections here, even if the policy wants us to abort dispatch. |
| 2060 | // If the policy decides to close the app, we will get a channel removal event via |
| 2061 | // unregisterInputChannel, and will clean up the connection that way. We are already not |
| 2062 | // sending new pointers to the connection when it blocked, but focused events will continue to |
| 2063 | // pile up. |
| 2064 | ALOGW("Canceling events for %s because it is unresponsive", |
| 2065 | connection->inputChannel->getName().c_str()); |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 2066 | if (connection->status == Connection::Status::NORMAL) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 2067 | CancelationOptions options(CancelationOptions::Mode::CANCEL_ALL_EVENTS, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2068 | "application not responding"); |
| 2069 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2070 | } |
| 2071 | } |
| 2072 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2073 | void InputDispatcher::resetNoFocusedWindowTimeoutLocked() { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2074 | if (DEBUG_FOCUS) { |
| 2075 | ALOGD("Resetting ANR timeouts."); |
| 2076 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2077 | |
| 2078 | // Reset input target wait timeout. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2079 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2080 | mAwaitedFocusedApplication.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2081 | } |
| 2082 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2083 | /** |
| 2084 | * Get the display id that the given event should go to. If this event specifies a valid display id, |
| 2085 | * then it should be dispatched to that display. Otherwise, the event goes to the focused display. |
| 2086 | * Focused display is the display that the user most recently interacted with. |
| 2087 | */ |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2088 | int32_t InputDispatcher::getTargetDisplayId(const EventEntry& entry) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2089 | int32_t displayId; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2090 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2091 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2092 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 2093 | displayId = keyEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2094 | break; |
| 2095 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2096 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2097 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 2098 | displayId = motionEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2099 | break; |
| 2100 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 2101 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 2102 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2103 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2104 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2105 | case EventEntry::Type::DEVICE_RESET: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2106 | case EventEntry::Type::SENSOR: |
| 2107 | case EventEntry::Type::DRAG: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2108 | 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] | 2109 | return ADISPLAY_ID_NONE; |
| 2110 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2111 | } |
| 2112 | return displayId == ADISPLAY_ID_NONE ? mFocusedDisplayId : displayId; |
| 2113 | } |
| 2114 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2115 | bool InputDispatcher::shouldWaitToSendKeyLocked(nsecs_t currentTime, |
| 2116 | const char* focusedWindowName) { |
| 2117 | if (mAnrTracker.empty()) { |
| 2118 | // already processed all events that we waited for |
| 2119 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 2120 | return false; |
| 2121 | } |
| 2122 | |
| 2123 | if (!mKeyIsWaitingForEventsTimeout.has_value()) { |
| 2124 | // Start the timer |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 2125 | // 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] | 2126 | mKeyIsWaitingForEventsTimeout = currentTime + |
| 2127 | std::chrono::duration_cast<std::chrono::nanoseconds>(KEY_WAITING_FOR_EVENTS_TIMEOUT) |
| 2128 | .count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2129 | return true; |
| 2130 | } |
| 2131 | |
| 2132 | // We still have pending events, and already started the timer |
| 2133 | if (currentTime < *mKeyIsWaitingForEventsTimeout) { |
| 2134 | return true; // Still waiting |
| 2135 | } |
| 2136 | |
| 2137 | // Waited too long, and some connection still hasn't processed all motions |
| 2138 | // Just send the key to the focused window |
| 2139 | ALOGW("Dispatching key to %s even though there are other unprocessed events", |
| 2140 | focusedWindowName); |
| 2141 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 2142 | return false; |
| 2143 | } |
| 2144 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2145 | sp<WindowInfoHandle> InputDispatcher::findFocusedWindowTargetLocked( |
| 2146 | nsecs_t currentTime, const EventEntry& entry, nsecs_t* nextWakeupTime, |
| 2147 | InputEventInjectionResult& outInjectionResult) { |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2148 | outInjectionResult = InputEventInjectionResult::FAILED; // Default result |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2149 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2150 | int32_t displayId = getTargetDisplayId(entry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2151 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2152 | std::shared_ptr<InputApplicationHandle> focusedApplicationHandle = |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2153 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 2154 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2155 | // If there is no currently focused window and no focused application |
| 2156 | // then drop the event. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2157 | if (focusedWindowHandle == nullptr && focusedApplicationHandle == nullptr) { |
| 2158 | ALOGI("Dropping %s event because there is no focused window or focused application in " |
| 2159 | "display %" PRId32 ".", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2160 | ftl::enum_string(entry.type).c_str(), displayId); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2161 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2162 | } |
| 2163 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2164 | // Drop key events if requested by input feature |
| 2165 | if (focusedWindowHandle != nullptr && shouldDropInput(entry, focusedWindowHandle)) { |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2166 | return nullptr; |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2167 | } |
| 2168 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2169 | // Compatibility behavior: raise ANR if there is a focused application, but no focused window. |
| 2170 | // Only start counting when we have a focused event to dispatch. The ANR is canceled if we |
| 2171 | // start interacting with another application via touch (app switch). This code can be removed |
| 2172 | // if the "no focused window ANR" is moved to the policy. Input doesn't know whether |
| 2173 | // an app is expected to have a focused window. |
| 2174 | if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) { |
| 2175 | if (!mNoFocusedWindowTimeoutTime.has_value()) { |
| 2176 | // 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] | 2177 | std::chrono::nanoseconds timeout = focusedApplicationHandle->getDispatchingTimeout( |
| 2178 | DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
| 2179 | mNoFocusedWindowTimeoutTime = currentTime + timeout.count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2180 | mAwaitedFocusedApplication = focusedApplicationHandle; |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 2181 | mAwaitedApplicationDisplayId = displayId; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2182 | ALOGW("Waiting because no window has focus but %s may eventually add a " |
| 2183 | "window when it finishes starting up. Will wait for %" PRId64 "ms", |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 2184 | mAwaitedFocusedApplication->getName().c_str(), millis(timeout)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2185 | *nextWakeupTime = *mNoFocusedWindowTimeoutTime; |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2186 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 2187 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2188 | } else if (currentTime > *mNoFocusedWindowTimeoutTime) { |
| 2189 | // Already raised ANR. Drop the event |
| 2190 | ALOGE("Dropping %s event because there is no focused window", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2191 | ftl::enum_string(entry.type).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2192 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2193 | } else { |
| 2194 | // Still waiting for the focused window |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2195 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 2196 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2197 | } |
| 2198 | } |
| 2199 | |
| 2200 | // we have a valid, non-null focused window |
| 2201 | resetNoFocusedWindowTimeoutLocked(); |
| 2202 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2203 | // Verify targeted injection. |
| 2204 | if (const auto err = verifyTargetedInjection(focusedWindowHandle, entry); err) { |
| 2205 | ALOGW("Dropping injected event: %s", (*err).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2206 | outInjectionResult = InputEventInjectionResult::TARGET_MISMATCH; |
| 2207 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2208 | } |
| 2209 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2210 | if (focusedWindowHandle->getInfo()->inputConfig.test( |
| 2211 | WindowInfo::InputConfig::PAUSE_DISPATCHING)) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2212 | ALOGI("Waiting because %s is paused", focusedWindowHandle->getName().c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2213 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 2214 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2215 | } |
| 2216 | |
| 2217 | // If the event is a key event, then we must wait for all previous events to |
| 2218 | // complete before delivering it because previous events may have the |
| 2219 | // side-effect of transferring focus to a different window and we want to |
| 2220 | // ensure that the following keys are sent to the new window. |
| 2221 | // |
| 2222 | // Suppose the user touches a button in a window then immediately presses "A". |
| 2223 | // If the button causes a pop-up window to appear then we want to ensure that |
| 2224 | // the "A" key is delivered to the new pop-up window. This is because users |
| 2225 | // often anticipate pending UI changes when typing on a keyboard. |
| 2226 | // To obtain this behavior, we must serialize key events with respect to all |
| 2227 | // prior input events. |
| 2228 | if (entry.type == EventEntry::Type::KEY) { |
| 2229 | if (shouldWaitToSendKeyLocked(currentTime, focusedWindowHandle->getName().c_str())) { |
| 2230 | *nextWakeupTime = *mKeyIsWaitingForEventsTimeout; |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2231 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 2232 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2233 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2234 | } |
| 2235 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2236 | outInjectionResult = InputEventInjectionResult::SUCCEEDED; |
| 2237 | return focusedWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2238 | } |
| 2239 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2240 | /** |
| 2241 | * Given a list of monitors, remove the ones we cannot find a connection for, and the ones |
| 2242 | * that are currently unresponsive. |
| 2243 | */ |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2244 | std::vector<Monitor> InputDispatcher::selectResponsiveMonitorsLocked( |
| 2245 | const std::vector<Monitor>& monitors) const { |
| 2246 | std::vector<Monitor> responsiveMonitors; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2247 | std::copy_if(monitors.begin(), monitors.end(), std::back_inserter(responsiveMonitors), |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2248 | [this](const Monitor& monitor) REQUIRES(mLock) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 2249 | std::shared_ptr<Connection> connection = |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2250 | getConnectionLocked(monitor.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2251 | if (connection == nullptr) { |
| 2252 | ALOGE("Could not find connection for monitor %s", |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2253 | monitor.inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2254 | return false; |
| 2255 | } |
| 2256 | if (!connection->responsive) { |
| 2257 | ALOGW("Unresponsive monitor %s will not get the new gesture", |
| 2258 | connection->inputChannel->getName().c_str()); |
| 2259 | return false; |
| 2260 | } |
| 2261 | return true; |
| 2262 | }); |
| 2263 | return responsiveMonitors; |
| 2264 | } |
| 2265 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2266 | std::vector<InputTarget> InputDispatcher::findTouchedWindowTargetsLocked( |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2267 | nsecs_t currentTime, const MotionEntry& entry, |
Siarhei Vishniakou | 4fe5739 | 2022-10-25 13:44:30 -0700 | [diff] [blame] | 2268 | InputEventInjectionResult& outInjectionResult) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2269 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2270 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2271 | std::vector<InputTarget> targets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2272 | // For security reasons, we defer updating the touch state until we are sure that |
| 2273 | // event injection will be allowed. |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2274 | const int32_t displayId = entry.displayId; |
| 2275 | const int32_t action = entry.action; |
Siarhei Vishniakou | bf88052 | 2023-05-01 11:03:22 -0700 | [diff] [blame] | 2276 | const int32_t maskedAction = MotionEvent::getActionMasked(action); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2277 | |
| 2278 | // 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] | 2279 | outInjectionResult = InputEventInjectionResult::PENDING; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2280 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2281 | // Copy current touch state into tempTouchState. |
| 2282 | // This state will be used to update mTouchStatesByDisplay at the end of this function. |
| 2283 | // 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] | 2284 | const TouchState* oldState = nullptr; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2285 | TouchState tempTouchState; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2286 | if (const auto it = mTouchStatesByDisplay.find(displayId); it != mTouchStatesByDisplay.end()) { |
| 2287 | oldState = &(it->second); |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 2288 | tempTouchState = *oldState; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2289 | } |
| 2290 | |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 2291 | bool isSplit = shouldSplitTouch(tempTouchState, entry); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2292 | |
| 2293 | const bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE || |
| 2294 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2295 | maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2296 | // A DOWN could be generated from POINTER_DOWN if the initial pointers did not land into any |
| 2297 | // touchable windows. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2298 | const bool wasDown = oldState != nullptr && oldState->isDown(entry.deviceId); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2299 | const bool isDown = (maskedAction == AMOTION_EVENT_ACTION_DOWN) || |
| 2300 | (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN && !wasDown); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2301 | const bool newGesture = isDown || maskedAction == AMOTION_EVENT_ACTION_SCROLL || |
| 2302 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2303 | maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE; |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 2304 | const bool isFromMouse = isFromSource(entry.source, AINPUT_SOURCE_MOUSE); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2305 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2306 | if (newGesture) { |
| 2307 | isSplit = false; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2308 | } |
| 2309 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2310 | if (isDown && tempTouchState.hasHoveringPointers(entry.deviceId)) { |
| 2311 | // Compatibility behaviour: ACTION_DOWN causes HOVER_EXIT to get generated. |
| 2312 | tempTouchState.clearHoveringPointers(entry.deviceId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2313 | } |
| 2314 | |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2315 | if (isHoverAction) { |
Siarhei Vishniakou | 7be50c9 | 2023-11-17 17:09:08 -0800 | [diff] [blame] | 2316 | if (wasDown) { |
| 2317 | // Started hovering, but the device is already down: reject the hover event |
| 2318 | LOG(ERROR) << "Got hover event " << entry.getDescription() |
| 2319 | << " but the device is already down " << oldState->dump(); |
| 2320 | outInjectionResult = InputEventInjectionResult::FAILED; |
| 2321 | return {}; |
| 2322 | } |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2323 | // For hover actions, we will treat 'tempTouchState' as a new state, so let's erase |
| 2324 | // all of the existing hovering pointers and recompute. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2325 | tempTouchState.clearHoveringPointers(entry.deviceId); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2326 | } |
| 2327 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2328 | if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { |
| 2329 | /* 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] | 2330 | const auto [x, y] = resolveTouchedPosition(entry); |
Siarhei Vishniakou | 5b9766d | 2023-07-18 14:06:29 -0700 | [diff] [blame] | 2331 | const int32_t pointerIndex = MotionEvent::getActionIndex(action); |
Siarhei Vishniakou | 70f3d8c | 2023-09-19 15:36:52 -0700 | [diff] [blame] | 2332 | const int32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2333 | // Outside targets should be added upon first dispatched DOWN event. That means, this should |
| 2334 | // 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] | 2335 | const bool isStylus = isPointerFromStylus(entry, pointerIndex); |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 2336 | sp<WindowInfoHandle> newTouchedWindowHandle = |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2337 | findTouchedWindowAtLocked(displayId, x, y, isStylus); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2338 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2339 | if (isDown) { |
Siarhei Vishniakou | 70f3d8c | 2023-09-19 15:36:52 -0700 | [diff] [blame] | 2340 | targets += findOutsideTargetsLocked(displayId, newTouchedWindowHandle, pointerId); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2341 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2342 | // Handle the case where we did not find a window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2343 | if (newTouchedWindowHandle == nullptr) { |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 2344 | 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] | 2345 | // 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] | 2346 | newTouchedWindowHandle = tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2347 | } |
| 2348 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2349 | // Verify targeted injection. |
| 2350 | if (const auto err = verifyTargetedInjection(newTouchedWindowHandle, entry); err) { |
| 2351 | ALOGW("Dropping injected touch event: %s", (*err).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2352 | outInjectionResult = os::InputEventInjectionResult::TARGET_MISMATCH; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2353 | newTouchedWindowHandle = nullptr; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2354 | return {}; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2355 | } |
| 2356 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2357 | // Figure out whether splitting will be allowed for this window. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2358 | if (newTouchedWindowHandle != nullptr) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2359 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
| 2360 | // New window supports splitting, but we should never split mouse events. |
| 2361 | isSplit = !isFromMouse; |
| 2362 | } else if (isSplit) { |
| 2363 | // New window does not support splitting but we have already split events. |
| 2364 | // Ignore the new window. |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame] | 2365 | LOG(INFO) << "Skipping " << newTouchedWindowHandle->getName() |
| 2366 | << " because it doesn't support split touch"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2367 | newTouchedWindowHandle = nullptr; |
| 2368 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2369 | } else { |
| 2370 | // 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] | 2371 | // be delivered to a new window which supports split touch. Pointers from a mouse device |
| 2372 | // should never be split. |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 2373 | isSplit = !isFromMouse; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2374 | } |
| 2375 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2376 | std::vector<sp<WindowInfoHandle>> newTouchedWindows = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2377 | findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2378 | if (newTouchedWindowHandle != nullptr) { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2379 | // Process the foreground window first so that it is the first to receive the event. |
| 2380 | newTouchedWindows.insert(newTouchedWindows.begin(), newTouchedWindowHandle); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2381 | } |
| 2382 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2383 | if (newTouchedWindows.empty()) { |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 2384 | ALOGI("Dropping event because there is no touchable window at (%.1f, %.1f) on display " |
| 2385 | "%d.", |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2386 | x, y, displayId); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2387 | outInjectionResult = InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2388 | return {}; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2389 | } |
| 2390 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2391 | for (const sp<WindowInfoHandle>& windowHandle : newTouchedWindows) { |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 2392 | if (!canWindowReceiveMotionLocked(windowHandle, entry)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2393 | continue; |
| 2394 | } |
| 2395 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2396 | if (isHoverAction) { |
Siarhei Vishniakou | b681c20 | 2023-05-01 11:22:33 -0700 | [diff] [blame] | 2397 | // The "windowHandle" is the target of this hovering pointer. |
| 2398 | tempTouchState.addHoveringPointerToWindow(windowHandle, entry.deviceId, pointerId); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2399 | } |
| 2400 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2401 | // Set target flags. |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2402 | ftl::Flags<InputTarget::Flags> targetFlags; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2403 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2404 | if (canReceiveForegroundTouches(*windowHandle->getInfo())) { |
| 2405 | // 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] | 2406 | targetFlags |= InputTarget::Flags::FOREGROUND; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2407 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2408 | |
| 2409 | if (isSplit) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2410 | targetFlags |= InputTarget::Flags::SPLIT; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2411 | } |
| 2412 | if (isWindowObscuredAtPointLocked(windowHandle, x, y)) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2413 | targetFlags |= InputTarget::Flags::WINDOW_IS_OBSCURED; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2414 | } else if (isWindowObscuredLocked(windowHandle)) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2415 | targetFlags |= InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2416 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2417 | |
| 2418 | // Update the temporary touch state. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2419 | |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2420 | if (!isHoverAction) { |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2421 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
Siarhei Vishniakou | 70f3d8c | 2023-09-19 15:36:52 -0700 | [diff] [blame] | 2422 | pointerIds.set(pointerId); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2423 | const bool isDownOrPointerDown = maskedAction == AMOTION_EVENT_ACTION_DOWN || |
| 2424 | maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2425 | tempTouchState.addOrUpdateWindow(windowHandle, InputTarget::DispatchMode::AS_IS, |
| 2426 | targetFlags, entry.deviceId, pointerIds, |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2427 | isDownOrPointerDown |
| 2428 | ? std::make_optional(entry.eventTime) |
| 2429 | : std::nullopt); |
| 2430 | // If this is the pointer going down and the touched window has a wallpaper |
| 2431 | // then also add the touched wallpaper windows so they are locked in for the |
| 2432 | // duration of the touch gesture. We do not collect wallpapers during HOVER_MOVE or |
| 2433 | // SCROLL because the wallpaper engine only supports touch events. We would need to |
| 2434 | // add a mechanism similar to View.onGenericMotionEvent to enable wallpapers to |
| 2435 | // handle these events. |
| 2436 | if (isDownOrPointerDown && targetFlags.test(InputTarget::Flags::FOREGROUND) && |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2437 | windowHandle->getInfo()->inputConfig.test( |
| 2438 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER)) { |
| 2439 | sp<WindowInfoHandle> wallpaper = findWallpaperWindowBelow(windowHandle); |
| 2440 | if (wallpaper != nullptr) { |
| 2441 | ftl::Flags<InputTarget::Flags> wallpaperFlags = |
| 2442 | InputTarget::Flags::WINDOW_IS_OBSCURED | |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2443 | InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED; |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2444 | if (isSplit) { |
| 2445 | wallpaperFlags |= InputTarget::Flags::SPLIT; |
| 2446 | } |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2447 | tempTouchState.addOrUpdateWindow(wallpaper, |
| 2448 | InputTarget::DispatchMode::AS_IS, |
| 2449 | wallpaperFlags, entry.deviceId, pointerIds, |
| 2450 | entry.eventTime); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2451 | } |
| 2452 | } |
| 2453 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2454 | } |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 2455 | |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 2456 | // If a window is already pilfering some pointers, give it this new pointer as well and |
| 2457 | // make it pilfering. This will prevent other non-spy windows from getting this pointer, |
| 2458 | // which is a specific behaviour that we want. |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 2459 | for (TouchedWindow& touchedWindow : tempTouchState.windows) { |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 2460 | if (touchedWindow.hasTouchingPointer(entry.deviceId, pointerId) && |
| 2461 | touchedWindow.hasPilferingPointers(entry.deviceId)) { |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 2462 | // This window is already pilfering some pointers, and this new pointer is also |
| 2463 | // going to it. Therefore, take over this pointer and don't give it to anyone |
| 2464 | // else. |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 2465 | touchedWindow.addPilferingPointer(entry.deviceId, pointerId); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 2466 | } |
| 2467 | } |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 2468 | |
| 2469 | // Restrict all pilfered pointers to the pilfering windows. |
| 2470 | tempTouchState.cancelPointersForNonPilferingWindows(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2471 | } else { |
| 2472 | /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ |
| 2473 | |
| 2474 | // If the pointer is not currently down, then ignore the event. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2475 | if (!tempTouchState.isDown(entry.deviceId) && |
| 2476 | maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT) { |
Siarhei Vishniakou | 31dd155 | 2023-10-30 18:46:10 -0700 | [diff] [blame] | 2477 | if (DEBUG_DROPPED_EVENTS_VERBOSE) { |
| 2478 | LOG(INFO) << "Dropping event because the pointer for device " << entry.deviceId |
| 2479 | << " is not down or we previously dropped the pointer down event in " |
| 2480 | << "display " << displayId << ": " << entry.getDescription(); |
| 2481 | } |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2482 | outInjectionResult = InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2483 | return {}; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2484 | } |
| 2485 | |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2486 | // If the pointer is not currently hovering, then ignore the event. |
| 2487 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 2488 | const int32_t pointerId = entry.pointerProperties[0].id; |
| 2489 | if (oldState == nullptr || |
| 2490 | oldState->getWindowsWithHoveringPointer(entry.deviceId, pointerId).empty()) { |
| 2491 | LOG(INFO) << "Dropping event because the hovering pointer is not in any windows in " |
| 2492 | "display " |
| 2493 | << displayId << ": " << entry.getDescription(); |
| 2494 | outInjectionResult = InputEventInjectionResult::FAILED; |
| 2495 | return {}; |
| 2496 | } |
| 2497 | tempTouchState.removeHoveringPointer(entry.deviceId, pointerId); |
| 2498 | } |
| 2499 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2500 | addDragEventLocked(entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2501 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2502 | // Check whether touches should slip outside of the current foreground window. |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 2503 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.getPointerCount() == 1 && |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2504 | tempTouchState.isSlippery()) { |
Siarhei Vishniakou | 9306c38 | 2022-09-30 15:30:31 -0700 | [diff] [blame] | 2505 | const auto [x, y] = resolveTouchedPosition(entry); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2506 | const bool isStylus = isPointerFromStylus(entry, /*pointerIndex=*/0); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2507 | sp<WindowInfoHandle> oldTouchedWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2508 | tempTouchState.getFirstForegroundWindowHandle(); |
Siarhei Vishniakou | 0f6558d | 2023-04-21 12:05:13 -0700 | [diff] [blame] | 2509 | LOG_ALWAYS_FATAL_IF(oldTouchedWindowHandle == nullptr); |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 2510 | sp<WindowInfoHandle> newTouchedWindowHandle = |
| 2511 | findTouchedWindowAtLocked(displayId, x, y, isStylus); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2512 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2513 | // Verify targeted injection. |
| 2514 | if (const auto err = verifyTargetedInjection(newTouchedWindowHandle, entry); err) { |
| 2515 | ALOGW("Dropping injected event: %s", (*err).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2516 | outInjectionResult = os::InputEventInjectionResult::TARGET_MISMATCH; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2517 | return {}; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2518 | } |
| 2519 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2520 | // Drop touch events if requested by input feature |
| 2521 | if (newTouchedWindowHandle != nullptr && |
| 2522 | shouldDropInput(entry, newTouchedWindowHandle)) { |
| 2523 | newTouchedWindowHandle = nullptr; |
| 2524 | } |
| 2525 | |
Siarhei Vishniakou | afa08cc | 2023-05-08 22:35:50 -0700 | [diff] [blame] | 2526 | if (newTouchedWindowHandle != nullptr && |
| 2527 | !haveSameToken(oldTouchedWindowHandle, newTouchedWindowHandle)) { |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2528 | 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] | 2529 | oldTouchedWindowHandle->getName().c_str(), |
| 2530 | newTouchedWindowHandle->getName().c_str(), displayId); |
| 2531 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2532 | // Make a slippery exit from the old window. |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 2533 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2534 | const int32_t pointerId = entry.pointerProperties[0].id; |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 2535 | pointerIds.set(pointerId); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2536 | |
| 2537 | const TouchedWindow& touchedWindow = |
| 2538 | tempTouchState.getTouchedWindow(oldTouchedWindowHandle); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2539 | addPointerWindowTargetLocked(oldTouchedWindowHandle, |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2540 | InputTarget::DispatchMode::SLIPPERY_EXIT, |
| 2541 | ftl::Flags<InputTarget::Flags>(), pointerIds, |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2542 | touchedWindow.getDownTimeInTarget(entry.deviceId), |
| 2543 | targets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2544 | |
| 2545 | // Make a slippery entrance into the new window. |
| 2546 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
Prabir Pradhan | 713bb3e | 2021-12-20 02:07:40 -0800 | [diff] [blame] | 2547 | isSplit = !isFromMouse; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2548 | } |
| 2549 | |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2550 | ftl::Flags<InputTarget::Flags> targetFlags; |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2551 | if (canReceiveForegroundTouches(*newTouchedWindowHandle->getInfo())) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2552 | targetFlags |= InputTarget::Flags::FOREGROUND; |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2553 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2554 | if (isSplit) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2555 | targetFlags |= InputTarget::Flags::SPLIT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2556 | } |
| 2557 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2558 | targetFlags |= InputTarget::Flags::WINDOW_IS_OBSCURED; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2559 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2560 | targetFlags |= InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2561 | } |
| 2562 | |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2563 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, |
| 2564 | InputTarget::DispatchMode::SLIPPERY_ENTER, |
| 2565 | targetFlags, entry.deviceId, pointerIds, |
| 2566 | entry.eventTime); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2567 | |
| 2568 | // Check if the wallpaper window should deliver the corresponding event. |
| 2569 | slipWallpaperTouch(targetFlags, oldTouchedWindowHandle, newTouchedWindowHandle, |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 2570 | tempTouchState, entry.deviceId, pointerId, targets); |
| 2571 | tempTouchState.removeTouchingPointerFromWindow(entry.deviceId, pointerId, |
| 2572 | oldTouchedWindowHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2573 | } |
| 2574 | } |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 2575 | |
| 2576 | // Update the pointerIds for non-splittable when it received pointer down. |
| 2577 | if (!isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN) { |
| 2578 | // If no split, we suppose all touched windows should receive pointer down. |
Siarhei Vishniakou | 5b9766d | 2023-07-18 14:06:29 -0700 | [diff] [blame] | 2579 | const int32_t pointerIndex = MotionEvent::getActionIndex(action); |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 2580 | for (size_t i = 0; i < tempTouchState.windows.size(); i++) { |
| 2581 | TouchedWindow& touchedWindow = tempTouchState.windows[i]; |
| 2582 | // Ignore drag window for it should just track one pointer. |
| 2583 | if (mDragState && mDragState->dragWindow == touchedWindow.windowHandle) { |
| 2584 | continue; |
| 2585 | } |
Siarhei Vishniakou | 5b9766d | 2023-07-18 14:06:29 -0700 | [diff] [blame] | 2586 | std::bitset<MAX_POINTER_ID + 1> touchingPointers; |
| 2587 | touchingPointers.set(entry.pointerProperties[pointerIndex].id); |
| 2588 | touchedWindow.addTouchingPointers(entry.deviceId, touchingPointers); |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 2589 | } |
| 2590 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2591 | } |
| 2592 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2593 | // Update dispatching for hover enter and exit. |
Sam Dubey | f886dec | 2023-01-27 13:28:19 +0000 | [diff] [blame] | 2594 | { |
| 2595 | std::vector<TouchedWindow> hoveringWindows = |
| 2596 | getHoveringWindowsLocked(oldState, tempTouchState, entry); |
| 2597 | for (const TouchedWindow& touchedWindow : hoveringWindows) { |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 2598 | std::optional<InputTarget> target = |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2599 | createInputTargetLocked(touchedWindow.windowHandle, touchedWindow.dispatchMode, |
| 2600 | touchedWindow.targetFlags, |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 2601 | touchedWindow.getDownTimeInTarget(entry.deviceId)); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 2602 | if (!target) { |
| 2603 | continue; |
| 2604 | } |
| 2605 | // Hardcode to single hovering pointer for now. |
| 2606 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
| 2607 | pointerIds.set(entry.pointerProperties[0].id); |
| 2608 | target->addPointers(pointerIds, touchedWindow.windowHandle->getInfo()->transform); |
| 2609 | targets.push_back(*target); |
Sam Dubey | f886dec | 2023-01-27 13:28:19 +0000 | [diff] [blame] | 2610 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2611 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2612 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2613 | // Ensure that all touched windows are valid for injection. |
| 2614 | if (entry.injectionState != nullptr) { |
| 2615 | std::string errs; |
| 2616 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2617 | const auto err = verifyTargetedInjection(touchedWindow.windowHandle, entry); |
| 2618 | if (err) errs += "\n - " + *err; |
| 2619 | } |
| 2620 | if (!errs.empty()) { |
| 2621 | 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] | 2622 | "%s:%s", |
| 2623 | entry.injectionState->targetUid->toString().c_str(), errs.c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2624 | outInjectionResult = InputEventInjectionResult::TARGET_MISMATCH; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2625 | return {}; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2626 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2627 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2628 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2629 | // Check whether windows listening for outside touches are owned by the same UID. If the owner |
| 2630 | // 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] | 2631 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2632 | sp<WindowInfoHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2633 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2634 | if (foregroundWindowHandle) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 2635 | const auto foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2636 | for (InputTarget& target : targets) { |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2637 | if (target.dispatchMode == InputTarget::DispatchMode::OUTSIDE) { |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2638 | sp<WindowInfoHandle> targetWindow = |
| 2639 | getWindowHandleLocked(target.inputChannel->getConnectionToken()); |
| 2640 | if (targetWindow->getInfo()->ownerUid != foregroundWindowUid) { |
| 2641 | target.flags |= InputTarget::Flags::ZERO_COORDS; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2642 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2643 | } |
| 2644 | } |
| 2645 | } |
| 2646 | } |
| 2647 | |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 2648 | // If this is a touchpad navigation gesture, it needs to only be sent to trusted targets, as we |
| 2649 | // only want the system UI to handle these gestures. |
| 2650 | const bool isTouchpadNavGesture = isFromSource(entry.source, AINPUT_SOURCE_MOUSE) && |
| 2651 | entry.classification == MotionClassification::MULTI_FINGER_SWIPE; |
| 2652 | if (isTouchpadNavGesture) { |
| 2653 | filterUntrustedTargets(/* byref */ tempTouchState, /* byref */ targets); |
| 2654 | } |
| 2655 | |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2656 | // Output targets from the touch state. |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2657 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2658 | std::bitset<MAX_POINTER_ID + 1> touchingPointers = |
| 2659 | touchedWindow.getTouchingPointers(entry.deviceId); |
| 2660 | if (touchingPointers.none()) { |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2661 | continue; |
| 2662 | } |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2663 | addPointerWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.dispatchMode, |
| 2664 | touchedWindow.targetFlags, touchingPointers, |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2665 | touchedWindow.getDownTimeInTarget(entry.deviceId), targets); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2666 | } |
Sam Dubey | 39d37cf | 2022-12-07 18:05:35 +0000 | [diff] [blame] | 2667 | |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 2668 | // During targeted injection, only allow owned targets to receive events |
| 2669 | std::erase_if(targets, [&](const InputTarget& target) { |
| 2670 | LOG_ALWAYS_FATAL_IF(target.windowHandle == nullptr); |
| 2671 | const auto err = verifyTargetedInjection(target.windowHandle, entry); |
| 2672 | if (err) { |
| 2673 | LOG(WARNING) << "Dropping injected event from " << target.windowHandle->getName() |
| 2674 | << ": " << (*err); |
| 2675 | return true; |
| 2676 | } |
| 2677 | return false; |
| 2678 | }); |
| 2679 | |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2680 | if (targets.empty()) { |
| 2681 | LOG(INFO) << "Dropping event because no targets were found: " << entry.getDescription(); |
| 2682 | outInjectionResult = InputEventInjectionResult::FAILED; |
| 2683 | return {}; |
| 2684 | } |
| 2685 | |
| 2686 | // If we only have windows getting ACTION_OUTSIDE, then drop the event, because there is no |
| 2687 | // window that is actually receiving the entire gesture. |
| 2688 | if (std::all_of(targets.begin(), targets.end(), [](const InputTarget& target) { |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2689 | return target.dispatchMode == InputTarget::DispatchMode::OUTSIDE; |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2690 | })) { |
| 2691 | LOG(INFO) << "Dropping event because all windows would just receive ACTION_OUTSIDE: " |
| 2692 | << entry.getDescription(); |
| 2693 | outInjectionResult = InputEventInjectionResult::FAILED; |
| 2694 | return {}; |
| 2695 | } |
| 2696 | |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2697 | outInjectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2698 | |
Prabir Pradhan | 502a725 | 2023-12-01 16:11:24 +0000 | [diff] [blame] | 2699 | // Now that we have generated all of the input targets for this event, reset the dispatch |
| 2700 | // mode for all touched window to AS_IS. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2701 | for (TouchedWindow& touchedWindow : tempTouchState.windows) { |
Prabir Pradhan | 502a725 | 2023-12-01 16:11:24 +0000 | [diff] [blame] | 2702 | touchedWindow.dispatchMode = InputTarget::DispatchMode::AS_IS; |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2703 | } |
| 2704 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2705 | // Update final pieces of touch state if the injector had permission. |
Siarhei Vishniakou | 7be50c9 | 2023-11-17 17:09:08 -0800 | [diff] [blame] | 2706 | if (maskedAction == AMOTION_EVENT_ACTION_UP) { |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2707 | // Pointer went up. |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 2708 | tempTouchState.removeTouchingPointer(entry.deviceId, entry.pointerProperties[0].id); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2709 | } else if (maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2710 | // All pointers up or canceled. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2711 | tempTouchState.removeAllPointersForDevice(entry.deviceId); |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2712 | } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 2713 | // One pointer went up. |
Siarhei Vishniakou | 5b9766d | 2023-07-18 14:06:29 -0700 | [diff] [blame] | 2714 | const int32_t pointerIndex = MotionEvent::getActionIndex(action); |
| 2715 | const uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
| 2716 | tempTouchState.removeTouchingPointer(entry.deviceId, pointerId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2717 | } |
| 2718 | |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2719 | // Save changes unless the action was scroll in which case the temporary touch |
| 2720 | // state was only valid for this one action. |
| 2721 | if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) { |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 2722 | if (displayId >= 0) { |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2723 | tempTouchState.clearWindowsWithoutPointers(); |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2724 | mTouchStatesByDisplay[displayId] = tempTouchState; |
| 2725 | } else { |
| 2726 | mTouchStatesByDisplay.erase(displayId); |
| 2727 | } |
| 2728 | } |
| 2729 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 2730 | if (tempTouchState.windows.empty()) { |
| 2731 | mTouchStatesByDisplay.erase(displayId); |
| 2732 | } |
| 2733 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2734 | return targets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2735 | } |
| 2736 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2737 | void InputDispatcher::finishDragAndDrop(int32_t displayId, float x, float y) { |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2738 | // Prevent stylus interceptor windows from affecting drag and drop behavior for now, until we |
| 2739 | // have an explicit reason to support it. |
| 2740 | constexpr bool isStylus = false; |
| 2741 | |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 2742 | sp<WindowInfoHandle> dropWindow = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2743 | findTouchedWindowAtLocked(displayId, x, y, isStylus, /*ignoreDragWindow=*/true); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2744 | if (dropWindow) { |
| 2745 | vec2 local = dropWindow->getInfo()->transform.transform(x, y); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2746 | sendDropWindowCommandLocked(dropWindow->getToken(), local.x, local.y); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 2747 | } else { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2748 | ALOGW("No window found when drop."); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2749 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2750 | } |
| 2751 | mDragState.reset(); |
| 2752 | } |
| 2753 | |
| 2754 | void InputDispatcher::addDragEventLocked(const MotionEntry& entry) { |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 2755 | if (!mDragState || mDragState->dragWindow->getInfo()->displayId != entry.displayId) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2756 | return; |
| 2757 | } |
| 2758 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2759 | if (!mDragState->isStartDrag) { |
| 2760 | mDragState->isStartDrag = true; |
| 2761 | mDragState->isStylusButtonDownAtStart = |
| 2762 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2763 | } |
| 2764 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2765 | // Find the pointer index by id. |
| 2766 | int32_t pointerIndex = 0; |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 2767 | for (; static_cast<uint32_t>(pointerIndex) < entry.getPointerCount(); pointerIndex++) { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2768 | const PointerProperties& pointerProperties = entry.pointerProperties[pointerIndex]; |
| 2769 | if (pointerProperties.id == mDragState->pointerId) { |
| 2770 | break; |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2771 | } |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2772 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2773 | |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 2774 | if (uint32_t(pointerIndex) == entry.getPointerCount()) { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2775 | 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] | 2776 | } |
| 2777 | |
| 2778 | const int32_t maskedAction = entry.action & AMOTION_EVENT_ACTION_MASK; |
| 2779 | const int32_t x = entry.pointerCoords[pointerIndex].getX(); |
| 2780 | const int32_t y = entry.pointerCoords[pointerIndex].getY(); |
| 2781 | |
| 2782 | switch (maskedAction) { |
| 2783 | case AMOTION_EVENT_ACTION_MOVE: { |
| 2784 | // Handle the special case : stylus button no longer pressed. |
| 2785 | bool isStylusButtonDown = |
| 2786 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2787 | if (mDragState->isStylusButtonDownAtStart && !isStylusButtonDown) { |
| 2788 | finishDragAndDrop(entry.displayId, x, y); |
| 2789 | return; |
| 2790 | } |
| 2791 | |
| 2792 | // Prevent stylus interceptor windows from affecting drag and drop behavior for now, |
| 2793 | // until we have an explicit reason to support it. |
| 2794 | constexpr bool isStylus = false; |
| 2795 | |
Siarhei Vishniakou | e1ada27 | 2022-11-03 10:47:08 -0700 | [diff] [blame] | 2796 | sp<WindowInfoHandle> hoverWindowHandle = |
| 2797 | findTouchedWindowAtLocked(entry.displayId, x, y, isStylus, |
| 2798 | /*ignoreDragWindow=*/true); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2799 | // enqueue drag exit if needed. |
| 2800 | if (hoverWindowHandle != mDragState->dragHoverWindowHandle && |
| 2801 | !haveSameToken(hoverWindowHandle, mDragState->dragHoverWindowHandle)) { |
| 2802 | if (mDragState->dragHoverWindowHandle != nullptr) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2803 | enqueueDragEventLocked(mDragState->dragHoverWindowHandle, /*isExiting=*/true, x, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2804 | y); |
| 2805 | } |
| 2806 | mDragState->dragHoverWindowHandle = hoverWindowHandle; |
| 2807 | } |
| 2808 | // enqueue drag location if needed. |
| 2809 | if (hoverWindowHandle != nullptr) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2810 | enqueueDragEventLocked(hoverWindowHandle, /*isExiting=*/false, x, y); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2811 | } |
| 2812 | break; |
| 2813 | } |
| 2814 | |
| 2815 | case AMOTION_EVENT_ACTION_POINTER_UP: |
Siarhei Vishniakou | 5b9766d | 2023-07-18 14:06:29 -0700 | [diff] [blame] | 2816 | if (MotionEvent::getActionIndex(entry.action) != pointerIndex) { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2817 | break; |
| 2818 | } |
| 2819 | // The drag pointer is up. |
| 2820 | [[fallthrough]]; |
| 2821 | case AMOTION_EVENT_ACTION_UP: |
| 2822 | finishDragAndDrop(entry.displayId, x, y); |
| 2823 | break; |
| 2824 | case AMOTION_EVENT_ACTION_CANCEL: { |
| 2825 | ALOGD("Receiving cancel when drag and drop."); |
| 2826 | sendDropWindowCommandLocked(nullptr, 0, 0); |
| 2827 | mDragState.reset(); |
| 2828 | break; |
| 2829 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2830 | } |
| 2831 | } |
| 2832 | |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2833 | std::optional<InputTarget> InputDispatcher::createInputTargetLocked( |
| 2834 | const sp<android::gui::WindowInfoHandle>& windowHandle, |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2835 | InputTarget::DispatchMode dispatchMode, ftl::Flags<InputTarget::Flags> targetFlags, |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2836 | std::optional<nsecs_t> firstDownTimeInTarget) const { |
| 2837 | std::shared_ptr<InputChannel> inputChannel = getInputChannelLocked(windowHandle->getToken()); |
| 2838 | if (inputChannel == nullptr) { |
| 2839 | ALOGW("Not creating InputTarget for %s, no input channel", windowHandle->getName().c_str()); |
| 2840 | return {}; |
| 2841 | } |
| 2842 | InputTarget inputTarget; |
| 2843 | inputTarget.inputChannel = inputChannel; |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 2844 | inputTarget.windowHandle = windowHandle; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2845 | inputTarget.dispatchMode = dispatchMode; |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2846 | inputTarget.flags = targetFlags; |
| 2847 | inputTarget.globalScaleFactor = windowHandle->getInfo()->globalScaleFactor; |
| 2848 | inputTarget.firstDownTimeInTarget = firstDownTimeInTarget; |
| 2849 | const auto& displayInfoIt = mDisplayInfos.find(windowHandle->getInfo()->displayId); |
| 2850 | if (displayInfoIt != mDisplayInfos.end()) { |
| 2851 | inputTarget.displayTransform = displayInfoIt->second.transform; |
| 2852 | } else { |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 2853 | // DisplayInfo not found for this window on display windowHandle->getInfo()->displayId. |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2854 | // TODO(b/198444055): Make this an error message after 'setInputWindows' API is removed. |
| 2855 | } |
| 2856 | return inputTarget; |
| 2857 | } |
| 2858 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2859 | void InputDispatcher::addWindowTargetLocked(const sp<WindowInfoHandle>& windowHandle, |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2860 | InputTarget::DispatchMode dispatchMode, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2861 | ftl::Flags<InputTarget::Flags> targetFlags, |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2862 | std::optional<nsecs_t> firstDownTimeInTarget, |
Siarhei Vishniakou | f75cddb | 2022-10-25 10:42:16 -0700 | [diff] [blame] | 2863 | std::vector<InputTarget>& inputTargets) const { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2864 | std::vector<InputTarget>::iterator it = |
| 2865 | std::find_if(inputTargets.begin(), inputTargets.end(), |
| 2866 | [&windowHandle](const InputTarget& inputTarget) { |
| 2867 | return inputTarget.inputChannel->getConnectionToken() == |
| 2868 | windowHandle->getToken(); |
| 2869 | }); |
Chavi Weingarten | 97b8eec | 2020-01-09 18:09:08 +0000 | [diff] [blame] | 2870 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2871 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2872 | |
| 2873 | if (it == inputTargets.end()) { |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2874 | std::optional<InputTarget> target = |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2875 | createInputTargetLocked(windowHandle, dispatchMode, targetFlags, |
| 2876 | firstDownTimeInTarget); |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2877 | if (!target) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2878 | return; |
| 2879 | } |
Siarhei Vishniakou | 6c377b3 | 2023-05-15 17:03:39 -0700 | [diff] [blame] | 2880 | inputTargets.push_back(*target); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2881 | it = inputTargets.end() - 1; |
| 2882 | } |
| 2883 | |
Siarhei Vishniakou | 23d73fb | 2023-10-29 13:27:46 -0700 | [diff] [blame] | 2884 | if (it->flags != targetFlags) { |
| 2885 | LOG(ERROR) << "Flags don't match! targetFlags=" << targetFlags.string() << ", it=" << *it; |
| 2886 | } |
| 2887 | if (it->globalScaleFactor != windowInfo->globalScaleFactor) { |
| 2888 | LOG(ERROR) << "Mismatch! it->globalScaleFactor=" << it->globalScaleFactor |
| 2889 | << ", windowInfo->globalScaleFactor=" << windowInfo->globalScaleFactor; |
| 2890 | } |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2891 | } |
| 2892 | |
| 2893 | void InputDispatcher::addPointerWindowTargetLocked( |
| 2894 | const sp<android::gui::WindowInfoHandle>& windowHandle, |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2895 | InputTarget::DispatchMode dispatchMode, ftl::Flags<InputTarget::Flags> targetFlags, |
| 2896 | std::bitset<MAX_POINTER_ID + 1> pointerIds, std::optional<nsecs_t> firstDownTimeInTarget, |
| 2897 | std::vector<InputTarget>& inputTargets) const REQUIRES(mLock) { |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2898 | if (pointerIds.none()) { |
| 2899 | for (const auto& target : inputTargets) { |
| 2900 | LOG(INFO) << "Target: " << target; |
| 2901 | } |
| 2902 | LOG(FATAL) << "No pointers specified for " << windowHandle->getName(); |
| 2903 | return; |
| 2904 | } |
| 2905 | std::vector<InputTarget>::iterator it = |
| 2906 | std::find_if(inputTargets.begin(), inputTargets.end(), |
| 2907 | [&windowHandle](const InputTarget& inputTarget) { |
| 2908 | return inputTarget.inputChannel->getConnectionToken() == |
| 2909 | windowHandle->getToken(); |
| 2910 | }); |
| 2911 | |
| 2912 | // This is a hack, because the actual entry could potentially be an ACTION_DOWN event that |
| 2913 | // causes a HOVER_EXIT to be generated. That means that the same entry of ACTION_DOWN would |
| 2914 | // have DISPATCH_AS_HOVER_EXIT and DISPATCH_AS_IS. And therefore, we have to create separate |
| 2915 | // input targets for hovering pointers and for touching pointers. |
| 2916 | // If we picked an existing input target above, but it's for HOVER_EXIT - let's use a new |
| 2917 | // target instead. |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2918 | if (it != inputTargets.end() && it->dispatchMode == InputTarget::DispatchMode::HOVER_EXIT) { |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2919 | // Force the code below to create a new input target |
| 2920 | it = inputTargets.end(); |
| 2921 | } |
| 2922 | |
| 2923 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 2924 | |
| 2925 | if (it == inputTargets.end()) { |
| 2926 | std::optional<InputTarget> target = |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2927 | createInputTargetLocked(windowHandle, dispatchMode, targetFlags, |
| 2928 | firstDownTimeInTarget); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2929 | if (!target) { |
| 2930 | return; |
| 2931 | } |
| 2932 | inputTargets.push_back(*target); |
| 2933 | it = inputTargets.end() - 1; |
| 2934 | } |
| 2935 | |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2936 | if (it->dispatchMode != dispatchMode) { |
| 2937 | LOG(ERROR) << __func__ << ": DispatchMode doesn't match! ignoring new mode=" |
| 2938 | << ftl::enum_string(dispatchMode) << ", it=" << *it; |
| 2939 | } |
Siarhei Vishniakou | 4bd0b7c | 2023-10-27 00:51:14 -0700 | [diff] [blame] | 2940 | if (it->flags != targetFlags) { |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2941 | LOG(ERROR) << __func__ << ": Flags don't match! new targetFlags=" << targetFlags.string() |
| 2942 | << ", it=" << *it; |
Siarhei Vishniakou | 4bd0b7c | 2023-10-27 00:51:14 -0700 | [diff] [blame] | 2943 | } |
Siarhei Vishniakou | 23d73fb | 2023-10-29 13:27:46 -0700 | [diff] [blame] | 2944 | if (it->globalScaleFactor != windowInfo->globalScaleFactor) { |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 2945 | LOG(ERROR) << __func__ << ": Mismatch! it->globalScaleFactor=" << it->globalScaleFactor |
Siarhei Vishniakou | 23d73fb | 2023-10-29 13:27:46 -0700 | [diff] [blame] | 2946 | << ", windowInfo->globalScaleFactor=" << windowInfo->globalScaleFactor; |
| 2947 | } |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2948 | |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2949 | it->addPointers(pointerIds, windowInfo->transform); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2950 | } |
| 2951 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2952 | void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2953 | int32_t displayId) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2954 | auto monitorsIt = mGlobalMonitorsByDisplay.find(displayId); |
| 2955 | if (monitorsIt == mGlobalMonitorsByDisplay.end()) return; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2956 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2957 | for (const Monitor& monitor : selectResponsiveMonitorsLocked(monitorsIt->second)) { |
| 2958 | InputTarget target; |
| 2959 | target.inputChannel = monitor.inputChannel; |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2960 | // target.firstDownTimeInTarget is not set for global monitors. It is only required in split |
| 2961 | // touch and global monitoring works as intended even without setting firstDownTimeInTarget |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2962 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
| 2963 | target.displayTransform = it->second.transform; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2964 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2965 | target.setDefaultPointerTransform(target.displayTransform); |
| 2966 | inputTargets.push_back(target); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2967 | } |
| 2968 | } |
| 2969 | |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2970 | /** |
| 2971 | * Indicate whether one window handle should be considered as obscuring |
| 2972 | * another window handle. We only check a few preconditions. Actually |
| 2973 | * checking the bounds is left to the caller. |
| 2974 | */ |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2975 | static bool canBeObscuredBy(const sp<WindowInfoHandle>& windowHandle, |
| 2976 | const sp<WindowInfoHandle>& otherHandle) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2977 | // Compare by token so cloned layers aren't counted |
| 2978 | if (haveSameToken(windowHandle, otherHandle)) { |
| 2979 | return false; |
| 2980 | } |
| 2981 | auto info = windowHandle->getInfo(); |
| 2982 | auto otherInfo = otherHandle->getInfo(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2983 | if (otherInfo->inputConfig.test(WindowInfo::InputConfig::NOT_VISIBLE)) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2984 | return false; |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2985 | } else if (otherInfo->alpha == 0 && |
| 2986 | otherInfo->inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE)) { |
Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame] | 2987 | // Those act as if they were invisible, so we don't need to flag them. |
| 2988 | // We do want to potentially flag touchable windows even if they have 0 |
| 2989 | // opacity, since they can consume touches and alter the effects of the |
| 2990 | // user interaction (eg. apps that rely on |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2991 | // Flags::WINDOW_IS_PARTIALLY_OBSCURED should still be told about those |
Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame] | 2992 | // windows), hence we also check for FLAG_NOT_TOUCHABLE. |
| 2993 | return false; |
Bernardo Rufino | 8007daf | 2020-09-22 09:40:01 +0000 | [diff] [blame] | 2994 | } else if (info->ownerUid == otherInfo->ownerUid) { |
| 2995 | // If ownerUid is the same we don't generate occlusion events as there |
| 2996 | // is no security boundary within an uid. |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2997 | return false; |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2998 | } else if (otherInfo->inputConfig.test(gui::WindowInfo::InputConfig::TRUSTED_OVERLAY)) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2999 | return false; |
| 3000 | } else if (otherInfo->displayId != info->displayId) { |
| 3001 | return false; |
| 3002 | } |
| 3003 | return true; |
| 3004 | } |
| 3005 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3006 | /** |
| 3007 | * Returns touch occlusion information in the form of TouchOcclusionInfo. To check if the touch is |
| 3008 | * untrusted, one should check: |
| 3009 | * |
| 3010 | * 1. If result.hasBlockingOcclusion is true. |
| 3011 | * If it's, it means the touch should be blocked due to a window with occlusion mode of |
| 3012 | * BLOCK_UNTRUSTED. |
| 3013 | * |
| 3014 | * 2. If result.obscuringOpacity > mMaximumObscuringOpacityForTouch. |
| 3015 | * If it is (and 1 is false), then the touch should be blocked because a stack of windows |
| 3016 | * (possibly only one) with occlusion mode of USE_OPACITY from one UID resulted in a composed |
| 3017 | * obscuring opacity above the threshold. Note that if there was no window of occlusion mode |
| 3018 | * USE_OPACITY, result.obscuringOpacity would've been 0 and since |
| 3019 | * mMaximumObscuringOpacityForTouch >= 0, the condition above would never be true. |
| 3020 | * |
| 3021 | * If neither of those is true, then it means the touch can be allowed. |
| 3022 | */ |
| 3023 | InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3024 | const sp<WindowInfoHandle>& windowHandle, int32_t x, int32_t y) const { |
| 3025 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 3026 | int32_t displayId = windowInfo->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3027 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3028 | TouchOcclusionInfo info; |
| 3029 | info.hasBlockingOcclusion = false; |
| 3030 | info.obscuringOpacity = 0; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3031 | info.obscuringUid = gui::Uid::INVALID; |
| 3032 | std::map<gui::Uid, float> opacityByUid; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3033 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3034 | if (windowHandle == otherHandle) { |
| 3035 | break; // All future windows are below us. Exit early. |
| 3036 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3037 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 3038 | if (canBeObscuredBy(windowHandle, otherHandle) && otherInfo->frameContainsPoint(x, y) && |
| 3039 | !haveSameApplicationToken(windowInfo, otherInfo)) { |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 3040 | if (DEBUG_TOUCH_OCCLUSION) { |
| 3041 | info.debugInfo.push_back( |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 3042 | dumpWindowForTouchOcclusion(otherInfo, /*isTouchedWindow=*/false)); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 3043 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3044 | // canBeObscuredBy() has returned true above, which means this window is untrusted, so |
| 3045 | // we perform the checks below to see if the touch can be propagated or not based on the |
| 3046 | // window's touch occlusion mode |
| 3047 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::BLOCK_UNTRUSTED) { |
| 3048 | info.hasBlockingOcclusion = true; |
| 3049 | info.obscuringUid = otherInfo->ownerUid; |
| 3050 | info.obscuringPackage = otherInfo->packageName; |
| 3051 | break; |
| 3052 | } |
| 3053 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::USE_OPACITY) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3054 | const auto uid = otherInfo->ownerUid; |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3055 | float opacity = |
| 3056 | (opacityByUid.find(uid) == opacityByUid.end()) ? 0 : opacityByUid[uid]; |
| 3057 | // Given windows A and B: |
| 3058 | // opacity(A, B) = 1 - [1 - opacity(A)] * [1 - opacity(B)] |
| 3059 | opacity = 1 - (1 - opacity) * (1 - otherInfo->alpha); |
| 3060 | opacityByUid[uid] = opacity; |
| 3061 | if (opacity > info.obscuringOpacity) { |
| 3062 | info.obscuringOpacity = opacity; |
| 3063 | info.obscuringUid = uid; |
| 3064 | info.obscuringPackage = otherInfo->packageName; |
| 3065 | } |
| 3066 | } |
| 3067 | } |
| 3068 | } |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 3069 | if (DEBUG_TOUCH_OCCLUSION) { |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 3070 | info.debugInfo.push_back(dumpWindowForTouchOcclusion(windowInfo, /*isTouchedWindow=*/true)); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 3071 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3072 | return info; |
| 3073 | } |
| 3074 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3075 | std::string InputDispatcher::dumpWindowForTouchOcclusion(const WindowInfo* info, |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 3076 | bool isTouchedWindow) const { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3077 | return StringPrintf(INDENT2 "* %spackage=%s/%s, id=%" PRId32 ", mode=%s, alpha=%.2f, " |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 3078 | "frame=[%" PRId32 ",%" PRId32 "][%" PRId32 ",%" PRId32 |
| 3079 | "], touchableRegion=%s, window={%s}, inputConfig={%s}, " |
| 3080 | "hasToken=%s, applicationInfo.name=%s, applicationInfo.token=%s\n", |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 3081 | isTouchedWindow ? "[TOUCHED] " : "", info->packageName.c_str(), |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3082 | info->ownerUid.toString().c_str(), info->id, |
Chavi Weingarten | 7f01919 | 2023-08-08 20:39:01 +0000 | [diff] [blame] | 3083 | toString(info->touchOcclusionMode).c_str(), info->alpha, info->frame.left, |
| 3084 | info->frame.top, info->frame.right, info->frame.bottom, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3085 | dumpRegion(info->touchableRegion).c_str(), info->name.c_str(), |
| 3086 | info->inputConfig.string().c_str(), toString(info->token != nullptr), |
| 3087 | info->applicationInfo.name.c_str(), |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 3088 | binderToString(info->applicationInfo.token).c_str()); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 3089 | } |
| 3090 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3091 | bool InputDispatcher::isTouchTrustedLocked(const TouchOcclusionInfo& occlusionInfo) const { |
| 3092 | if (occlusionInfo.hasBlockingOcclusion) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3093 | ALOGW("Untrusted touch due to occlusion by %s/%s", occlusionInfo.obscuringPackage.c_str(), |
| 3094 | occlusionInfo.obscuringUid.toString().c_str()); |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3095 | return false; |
| 3096 | } |
| 3097 | if (occlusionInfo.obscuringOpacity > mMaximumObscuringOpacityForTouch) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3098 | ALOGW("Untrusted touch due to occlusion by %s/%s (obscuring opacity = " |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3099 | "%.2f, maximum allowed = %.2f)", |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3100 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid.toString().c_str(), |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 3101 | occlusionInfo.obscuringOpacity, mMaximumObscuringOpacityForTouch); |
| 3102 | return false; |
| 3103 | } |
| 3104 | return true; |
| 3105 | } |
| 3106 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3107 | bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3108 | int32_t x, int32_t y) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [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 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3112 | if (windowHandle == otherHandle) { |
| 3113 | break; // All future windows are below us. Exit early. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3114 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3115 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3116 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 3117 | otherInfo->frameContainsPoint(x, y)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3118 | return true; |
| 3119 | } |
| 3120 | } |
| 3121 | return false; |
| 3122 | } |
| 3123 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3124 | bool InputDispatcher::isWindowObscuredLocked(const sp<WindowInfoHandle>& windowHandle) const { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 3125 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3126 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 3127 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 3128 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3129 | if (windowHandle == otherHandle) { |
| 3130 | break; // All future windows are below us. Exit early. |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 3131 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3132 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 3133 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 3134 | otherInfo->overlaps(windowInfo)) { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 3135 | return true; |
| 3136 | } |
| 3137 | } |
| 3138 | return false; |
| 3139 | } |
| 3140 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 3141 | std::string InputDispatcher::getApplicationWindowLabel( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3142 | const InputApplicationHandle* applicationHandle, const sp<WindowInfoHandle>& windowHandle) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3143 | if (applicationHandle != nullptr) { |
| 3144 | if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 3145 | return applicationHandle->getName() + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3146 | } else { |
| 3147 | return applicationHandle->getName(); |
| 3148 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3149 | } else if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 3150 | return windowHandle->getInfo()->applicationInfo.name + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3151 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 3152 | return "<unknown application or window>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3153 | } |
| 3154 | } |
| 3155 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3156 | void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3157 | if (!isUserActivityEvent(eventEntry)) { |
| 3158 | // 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] | 3159 | return; |
| 3160 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3161 | int32_t displayId = getTargetDisplayId(eventEntry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3162 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 3163 | const WindowInfo* windowDisablingUserActivityInfo = nullptr; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 3164 | if (focusedWindowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3165 | const WindowInfo* info = focusedWindowHandle->getInfo(); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 3166 | if (info->inputConfig.test(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY)) { |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 3167 | windowDisablingUserActivityInfo = info; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3168 | } |
| 3169 | } |
| 3170 | |
| 3171 | int32_t eventType = USER_ACTIVITY_EVENT_OTHER; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3172 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3173 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3174 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 3175 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3176 | return; |
| 3177 | } |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 3178 | if (windowDisablingUserActivityInfo != nullptr) { |
| 3179 | if (DEBUG_DISPATCH_CYCLE) { |
| 3180 | ALOGD("Not poking user activity: disabled by window '%s'.", |
| 3181 | windowDisablingUserActivityInfo->name.c_str()); |
| 3182 | } |
| 3183 | return; |
| 3184 | } |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3185 | if (MotionEvent::isTouchEvent(motionEntry.source, motionEntry.action)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3186 | eventType = USER_ACTIVITY_EVENT_TOUCH; |
| 3187 | } |
| 3188 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3189 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3190 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3191 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 3192 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3193 | return; |
| 3194 | } |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 3195 | // If the key code is unknown, we don't consider it user activity |
| 3196 | if (keyEntry.keyCode == AKEYCODE_UNKNOWN) { |
| 3197 | return; |
| 3198 | } |
| 3199 | // Don't inhibit events that were intercepted or are not passed to |
| 3200 | // the apps, like system shortcuts |
| 3201 | if (windowDisablingUserActivityInfo != nullptr && |
| 3202 | keyEntry.interceptKeyResult != KeyEntry::InterceptKeyResult::SKIP && |
| 3203 | keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER) { |
| 3204 | if (DEBUG_DISPATCH_CYCLE) { |
| 3205 | ALOGD("Not poking user activity: disabled by window '%s'.", |
| 3206 | windowDisablingUserActivityInfo->name.c_str()); |
| 3207 | } |
| 3208 | return; |
| 3209 | } |
| 3210 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3211 | eventType = USER_ACTIVITY_EVENT_BUTTON; |
| 3212 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3213 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3214 | default: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3215 | LOG_ALWAYS_FATAL("%s events are not user activity", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3216 | ftl::enum_string(eventEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3217 | break; |
| 3218 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3219 | } |
| 3220 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3221 | auto command = [this, eventTime = eventEntry.eventTime, eventType, displayId]() |
| 3222 | REQUIRES(mLock) { |
| 3223 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 3224 | mPolicy.pokeUserActivity(eventTime, eventType, displayId); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3225 | }; |
| 3226 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3227 | } |
| 3228 | |
| 3229 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3230 | const std::shared_ptr<Connection>& connection, |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 3231 | std::shared_ptr<const EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3232 | const InputTarget& inputTarget) { |
Prabir Pradhan | 2dac8b8 | 2023-09-06 01:11:51 +0000 | [diff] [blame] | 3233 | ATRACE_NAME_IF(ATRACE_ENABLED(), |
| 3234 | StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
| 3235 | connection->getInputChannelName().c_str(), eventEntry->id)); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3236 | if (DEBUG_DISPATCH_CYCLE) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3237 | ALOGD("channel '%s' ~ prepareDispatchCycle - flags=%s, " |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 3238 | "globalScaleFactor=%f, pointerIds=%s %s", |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3239 | connection->getInputChannelName().c_str(), inputTarget.flags.string().c_str(), |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 3240 | inputTarget.globalScaleFactor, bitsetToString(inputTarget.pointerIds).c_str(), |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3241 | inputTarget.getPointerInfoString().c_str()); |
| 3242 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3243 | |
| 3244 | // Skip this event if the connection status is not normal. |
| 3245 | // 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] | 3246 | if (connection->status != Connection::Status::NORMAL) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3247 | if (DEBUG_DISPATCH_CYCLE) { |
| 3248 | ALOGD("channel '%s' ~ Dropping event because the channel status is %s", |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3249 | connection->getInputChannelName().c_str(), |
| 3250 | ftl::enum_string(connection->status).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3251 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3252 | return; |
| 3253 | } |
| 3254 | |
| 3255 | // Split a motion event if needed. |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3256 | if (inputTarget.flags.test(InputTarget::Flags::SPLIT)) { |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3257 | LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3258 | "Entry type %s should not have Flags::SPLIT", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3259 | ftl::enum_string(eventEntry->type).c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3260 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3261 | const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry); |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 3262 | if (inputTarget.pointerIds.count() != originalMotionEntry.getPointerCount()) { |
Siarhei Vishniakou | 16e4fa0 | 2023-02-16 17:48:56 -0800 | [diff] [blame] | 3263 | if (!inputTarget.firstDownTimeInTarget.has_value()) { |
| 3264 | logDispatchStateLocked(); |
| 3265 | LOG(FATAL) << "Splitting motion events requires a down time to be set for the " |
| 3266 | "target on connection " |
| 3267 | << connection->getInputChannelName() << " for " |
| 3268 | << originalMotionEntry.getDescription(); |
| 3269 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3270 | std::unique_ptr<MotionEntry> splitMotionEntry = |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3271 | splitMotionEvent(originalMotionEntry, inputTarget.pointerIds, |
| 3272 | inputTarget.firstDownTimeInTarget.value()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3273 | if (!splitMotionEntry) { |
| 3274 | return; // split event was dropped |
| 3275 | } |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3276 | if (splitMotionEntry->action == AMOTION_EVENT_ACTION_CANCEL) { |
| 3277 | std::string reason = std::string("reason=pointer cancel on split window"); |
| 3278 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 3279 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 3280 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3281 | if (DEBUG_FOCUS) { |
| 3282 | ALOGD("channel '%s' ~ Split motion event.", |
| 3283 | connection->getInputChannelName().c_str()); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3284 | logOutboundMotionDetails(" ", *splitMotionEntry); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3285 | } |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3286 | enqueueDispatchEntryAndStartDispatchCycleLocked(currentTime, connection, |
| 3287 | std::move(splitMotionEntry), |
| 3288 | inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3289 | return; |
| 3290 | } |
| 3291 | } |
| 3292 | |
| 3293 | // Not splitting. Enqueue dispatch entries for the event as is. |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3294 | enqueueDispatchEntryAndStartDispatchCycleLocked(currentTime, connection, eventEntry, |
| 3295 | inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3296 | } |
| 3297 | |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3298 | void InputDispatcher::enqueueDispatchEntryAndStartDispatchCycleLocked( |
| 3299 | nsecs_t currentTime, const std::shared_ptr<Connection>& connection, |
| 3300 | std::shared_ptr<const EventEntry> eventEntry, const InputTarget& inputTarget) { |
Prabir Pradhan | 2dac8b8 | 2023-09-06 01:11:51 +0000 | [diff] [blame] | 3301 | ATRACE_NAME_IF(ATRACE_ENABLED(), |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3302 | StringPrintf("enqueueDispatchEntryAndStartDispatchCycleLocked(inputChannel=%s, " |
| 3303 | "id=0x%" PRIx32 ")", |
Prabir Pradhan | 2dac8b8 | 2023-09-06 01:11:51 +0000 | [diff] [blame] | 3304 | connection->getInputChannelName().c_str(), eventEntry->id)); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3305 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 3306 | const bool wasEmpty = connection->outboundQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3307 | |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3308 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3309 | |
| 3310 | // If the outbound queue was previously empty, start the dispatch cycle going. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3311 | if (wasEmpty && !connection->outboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3312 | startDispatchCycleLocked(currentTime, connection); |
| 3313 | } |
| 3314 | } |
| 3315 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3316 | void InputDispatcher::enqueueDispatchEntryLocked(const std::shared_ptr<Connection>& connection, |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 3317 | std::shared_ptr<const EventEntry> eventEntry, |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3318 | const InputTarget& inputTarget) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3319 | // This is a new event. |
| 3320 | // Enqueue a new dispatch entry onto the outbound queue for this connection. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3321 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3322 | createDispatchEntry(inputTarget, eventEntry, inputTarget.flags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3323 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3324 | // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a |
| 3325 | // different EventEntry than what was passed in. |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3326 | eventEntry = dispatchEntry->eventEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3327 | // Apply target flags and update the connection's input state. |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3328 | switch (eventEntry->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3329 | case EventEntry::Type::KEY: { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3330 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(*eventEntry); |
| 3331 | if (!connection->inputState.trackKey(keyEntry, keyEntry.flags)) { |
Siarhei Vishniakou | c94dafe | 2023-05-26 10:24:19 -0700 | [diff] [blame] | 3332 | LOG(WARNING) << "channel " << connection->getInputChannelName() |
| 3333 | << "~ dropping inconsistent event: " << *dispatchEntry; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3334 | return; // skip the inconsistent event |
| 3335 | } |
| 3336 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3337 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3338 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3339 | case EventEntry::Type::MOTION: { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3340 | std::shared_ptr<const MotionEntry> resolvedMotion = |
| 3341 | std::static_pointer_cast<const MotionEntry>(eventEntry); |
| 3342 | { |
| 3343 | // Determine the resolved motion entry. |
| 3344 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); |
| 3345 | int32_t resolvedAction = motionEntry.action; |
| 3346 | int32_t resolvedFlags = motionEntry.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3347 | |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3348 | if (inputTarget.dispatchMode == InputTarget::DispatchMode::OUTSIDE) { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3349 | resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3350 | } else if (inputTarget.dispatchMode == InputTarget::DispatchMode::HOVER_EXIT) { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3351 | resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3352 | } else if (inputTarget.dispatchMode == InputTarget::DispatchMode::HOVER_ENTER) { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3353 | resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3354 | } else if (inputTarget.dispatchMode == InputTarget::DispatchMode::SLIPPERY_EXIT) { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3355 | resolvedAction = AMOTION_EVENT_ACTION_CANCEL; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3356 | } else if (inputTarget.dispatchMode == InputTarget::DispatchMode::SLIPPERY_ENTER) { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3357 | resolvedAction = AMOTION_EVENT_ACTION_DOWN; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3358 | } |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3359 | if (resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE && |
| 3360 | !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source, |
| 3361 | motionEntry.displayId)) { |
| 3362 | if (DEBUG_DISPATCH_CYCLE) { |
| 3363 | LOG(DEBUG) << "channel '" << connection->getInputChannelName().c_str() |
| 3364 | << "' ~ enqueueDispatchEntryLocked: filling in missing hover " |
| 3365 | "enter event"; |
| 3366 | } |
| 3367 | resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 3368 | } |
| 3369 | |
| 3370 | if (resolvedAction == AMOTION_EVENT_ACTION_CANCEL) { |
| 3371 | resolvedFlags |= AMOTION_EVENT_FLAG_CANCELED; |
| 3372 | } |
| 3373 | if (dispatchEntry->targetFlags.test(InputTarget::Flags::WINDOW_IS_OBSCURED)) { |
| 3374 | resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; |
| 3375 | } |
| 3376 | if (dispatchEntry->targetFlags.test( |
| 3377 | InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED)) { |
| 3378 | resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 3379 | } |
| 3380 | |
| 3381 | dispatchEntry->resolvedFlags = resolvedFlags; |
| 3382 | if (resolvedAction != motionEntry.action) { |
| 3383 | // Generate a new MotionEntry with a new eventId using the resolved action and |
| 3384 | // flags. |
| 3385 | resolvedMotion = |
| 3386 | std::make_shared<MotionEntry>(mIdGenerator.nextId(), |
| 3387 | motionEntry.injectionState, |
| 3388 | motionEntry.eventTime, |
| 3389 | motionEntry.deviceId, motionEntry.source, |
| 3390 | motionEntry.displayId, |
| 3391 | motionEntry.policyFlags, resolvedAction, |
| 3392 | motionEntry.actionButton, resolvedFlags, |
| 3393 | motionEntry.metaState, |
| 3394 | motionEntry.buttonState, |
| 3395 | motionEntry.classification, |
| 3396 | motionEntry.edgeFlags, |
| 3397 | motionEntry.xPrecision, |
| 3398 | motionEntry.yPrecision, |
| 3399 | motionEntry.xCursorPosition, |
| 3400 | motionEntry.yCursorPosition, |
| 3401 | motionEntry.downTime, |
| 3402 | motionEntry.pointerProperties, |
| 3403 | motionEntry.pointerCoords); |
| 3404 | if (ATRACE_ENABLED()) { |
| 3405 | std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32 |
| 3406 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3407 | motionEntry.id, resolvedMotion->id); |
| 3408 | ATRACE_NAME(message.c_str()); |
| 3409 | } |
| 3410 | |
| 3411 | // Set the resolved motion entry in the DispatchEntry. |
| 3412 | dispatchEntry->eventEntry = resolvedMotion; |
| 3413 | eventEntry = resolvedMotion; |
| 3414 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3415 | } |
| 3416 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3417 | // Check if we need to cancel any of the ongoing gestures. We don't support multiple |
| 3418 | // devices being active at the same time in the same window, so if a new device is |
| 3419 | // active, cancel the gesture from the old device. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3420 | std::unique_ptr<EventEntry> cancelEvent = |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3421 | connection->inputState.cancelConflictingInputStream(*resolvedMotion); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3422 | if (cancelEvent != nullptr) { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3423 | LOG(INFO) << "Canceling pointers for device " << resolvedMotion->deviceId << " in " |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3424 | << connection->getInputChannelName() << " with event " |
| 3425 | << cancelEvent->getDescription(); |
| 3426 | std::unique_ptr<DispatchEntry> cancelDispatchEntry = |
| 3427 | createDispatchEntry(inputTarget, std::move(cancelEvent), |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3428 | ftl::Flags<InputTarget::Flags>()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3429 | |
| 3430 | // Send these cancel events to the queue before sending the event from the new |
| 3431 | // device. |
| 3432 | connection->outboundQueue.emplace_back(std::move(cancelDispatchEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3433 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3434 | |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3435 | if (!connection->inputState.trackMotion(*resolvedMotion, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3436 | dispatchEntry->resolvedFlags)) { |
Siarhei Vishniakou | c94dafe | 2023-05-26 10:24:19 -0700 | [diff] [blame] | 3437 | LOG(WARNING) << "channel " << connection->getInputChannelName() |
| 3438 | << "~ dropping inconsistent event: " << *dispatchEntry; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3439 | return; // skip the inconsistent event |
| 3440 | } |
| 3441 | |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3442 | if ((resolvedMotion->flags & AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) && |
| 3443 | (resolvedMotion->policyFlags & POLICY_FLAG_TRUSTED)) { |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 3444 | // Skip reporting pointer down outside focus to the policy. |
| 3445 | break; |
| 3446 | } |
| 3447 | |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3448 | dispatchPointerDownOutsideFocus(resolvedMotion->source, resolvedMotion->action, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3449 | inputTarget.inputChannel->getConnectionToken()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3450 | |
| 3451 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3452 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3453 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3454 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3455 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3456 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3457 | break; |
| 3458 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3459 | case EventEntry::Type::SENSOR: { |
| 3460 | LOG_ALWAYS_FATAL("SENSOR events should not go to apps via input channel"); |
| 3461 | break; |
| 3462 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3463 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 3464 | case EventEntry::Type::DEVICE_RESET: { |
| 3465 | LOG_ALWAYS_FATAL("%s events should not go to apps", |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3466 | ftl::enum_string(eventEntry->type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3467 | break; |
| 3468 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3469 | } |
| 3470 | |
| 3471 | // Remember that we are waiting for this dispatch to complete. |
| 3472 | if (dispatchEntry->hasForegroundTarget()) { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3473 | incrementPendingForegroundDispatches(*eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3474 | } |
| 3475 | |
| 3476 | // Enqueue the dispatch entry. |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 3477 | connection->outboundQueue.emplace_back(std::move(dispatchEntry)); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3478 | traceOutboundQueueLength(*connection); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3479 | } |
| 3480 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3481 | /** |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3482 | * This function is for debugging and metrics collection. It has two roles. |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3483 | * |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3484 | * The first role is to log input interaction with windows, which helps determine what the user was |
| 3485 | * interacting with. For example, if user is touching launcher, we will see an input_interaction log |
| 3486 | * that user started interacting with launcher window, as well as any other window that received |
| 3487 | * that gesture, such as the wallpaper or other spy windows. A new input_interaction is only logged |
| 3488 | * when the set of tokens that received the event changes. It is not logged again as long as the |
| 3489 | * user is interacting with the same windows. |
| 3490 | * |
| 3491 | * The second role is to track input device activity for metrics collection. For each input event, |
| 3492 | * we report the set of UIDs that the input device interacted with to the policy. Unlike for the |
| 3493 | * input_interaction logs, the device interaction is reported even when the set of interaction |
| 3494 | * tokens do not change. |
| 3495 | * |
| 3496 | * For these purposes, we do not count ACTION_OUTSIDE, ACTION_UP and ACTION_CANCEL actions as |
| 3497 | * interaction. This includes up and cancel events for both keys and motions. |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3498 | */ |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3499 | void InputDispatcher::processInteractionsLocked(const EventEntry& entry, |
| 3500 | const std::vector<InputTarget>& targets) { |
| 3501 | int32_t deviceId; |
| 3502 | nsecs_t eventTime; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3503 | // Skip ACTION_UP events, and all events other than keys and motions |
| 3504 | if (entry.type == EventEntry::Type::KEY) { |
| 3505 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 3506 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
| 3507 | return; |
| 3508 | } |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3509 | deviceId = keyEntry.deviceId; |
| 3510 | eventTime = keyEntry.eventTime; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3511 | } else if (entry.type == EventEntry::Type::MOTION) { |
| 3512 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 3513 | if (motionEntry.action == AMOTION_EVENT_ACTION_UP || |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3514 | motionEntry.action == AMOTION_EVENT_ACTION_CANCEL || |
| 3515 | MotionEvent::getActionMasked(motionEntry.action) == AMOTION_EVENT_ACTION_POINTER_UP) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3516 | return; |
| 3517 | } |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3518 | deviceId = motionEntry.deviceId; |
| 3519 | eventTime = motionEntry.eventTime; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3520 | } else { |
| 3521 | return; // Not a key or a motion |
| 3522 | } |
| 3523 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 3524 | std::set<gui::Uid> interactionUids; |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 3525 | std::unordered_set<sp<IBinder>, StrongPointerHash<IBinder>> newConnectionTokens; |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3526 | std::vector<std::shared_ptr<Connection>> newConnections; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3527 | for (const InputTarget& target : targets) { |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 3528 | if (target.dispatchMode == InputTarget::DispatchMode::OUTSIDE) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3529 | continue; // Skip windows that receive ACTION_OUTSIDE |
| 3530 | } |
| 3531 | |
| 3532 | sp<IBinder> token = target.inputChannel->getConnectionToken(); |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3533 | std::shared_ptr<Connection> connection = getConnectionLocked(token); |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3534 | if (connection == nullptr) { |
| 3535 | continue; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3536 | } |
| 3537 | newConnectionTokens.insert(std::move(token)); |
| 3538 | newConnections.emplace_back(connection); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3539 | if (target.windowHandle) { |
| 3540 | interactionUids.emplace(target.windowHandle->getInfo()->ownerUid); |
| 3541 | } |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3542 | } |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 3543 | |
| 3544 | auto command = [this, deviceId, eventTime, uids = std::move(interactionUids)]() |
| 3545 | REQUIRES(mLock) { |
| 3546 | scoped_unlock unlock(mLock); |
| 3547 | mPolicy.notifyDeviceInteraction(deviceId, eventTime, uids); |
| 3548 | }; |
| 3549 | postCommandLocked(std::move(command)); |
| 3550 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3551 | if (newConnectionTokens == mInteractionConnectionTokens) { |
| 3552 | return; // no change |
| 3553 | } |
| 3554 | mInteractionConnectionTokens = newConnectionTokens; |
| 3555 | |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3556 | std::string targetList; |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3557 | for (const std::shared_ptr<Connection>& connection : newConnections) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3558 | targetList += connection->getWindowName() + ", "; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3559 | } |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3560 | std::string message = "Interaction with: " + targetList; |
| 3561 | if (targetList.empty()) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3562 | message += "<none>"; |
| 3563 | } |
| 3564 | android_log_event_list(LOGTAG_INPUT_INTERACTION) << message << LOG_ID_EVENTS; |
| 3565 | } |
| 3566 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3567 | void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3568 | const sp<IBinder>& token) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3569 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3570 | uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK; |
| 3571 | if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3572 | return; |
| 3573 | } |
| 3574 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 3575 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3576 | if (focusedToken == token) { |
| 3577 | // ignore since token is focused |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3578 | return; |
| 3579 | } |
| 3580 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3581 | auto command = [this, token]() REQUIRES(mLock) { |
| 3582 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 3583 | mPolicy.onPointerDownOutsideFocus(token); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3584 | }; |
| 3585 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3586 | } |
| 3587 | |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3588 | status_t InputDispatcher::publishMotionEvent(Connection& connection, |
| 3589 | DispatchEntry& dispatchEntry) const { |
| 3590 | const EventEntry& eventEntry = *(dispatchEntry.eventEntry); |
| 3591 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 3592 | |
| 3593 | PointerCoords scaledCoords[MAX_POINTERS]; |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 3594 | const PointerCoords* usingCoords = motionEntry.pointerCoords.data(); |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3595 | |
| 3596 | // Set the X and Y offset and X and Y scale depending on the input source. |
| 3597 | if ((motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) && |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3598 | !(dispatchEntry.targetFlags.test(InputTarget::Flags::ZERO_COORDS))) { |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3599 | float globalScaleFactor = dispatchEntry.globalScaleFactor; |
| 3600 | if (globalScaleFactor != 1.0f) { |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 3601 | for (uint32_t i = 0; i < motionEntry.getPointerCount(); i++) { |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3602 | scaledCoords[i] = motionEntry.pointerCoords[i]; |
| 3603 | // Don't apply window scale here since we don't want scale to affect raw |
| 3604 | // coordinates. The scale will be sent back to the client and applied |
| 3605 | // later when requesting relative coordinates. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 3606 | scaledCoords[i].scale(globalScaleFactor, /*windowXScale=*/1, /*windowYScale=*/1); |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3607 | } |
| 3608 | usingCoords = scaledCoords; |
| 3609 | } |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3610 | } else if (dispatchEntry.targetFlags.test(InputTarget::Flags::ZERO_COORDS)) { |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3611 | // We don't want the dispatch target to know the coordinates |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 3612 | for (uint32_t i = 0; i < motionEntry.getPointerCount(); i++) { |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3613 | scaledCoords[i].clear(); |
| 3614 | } |
| 3615 | usingCoords = scaledCoords; |
| 3616 | } |
| 3617 | |
| 3618 | std::array<uint8_t, 32> hmac = getSignature(motionEntry, dispatchEntry); |
| 3619 | |
| 3620 | // Publish the motion event. |
| 3621 | return connection.inputPublisher |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3622 | .publishMotionEvent(dispatchEntry.seq, motionEntry.id, motionEntry.deviceId, |
| 3623 | motionEntry.source, motionEntry.displayId, std::move(hmac), |
| 3624 | motionEntry.action, motionEntry.actionButton, |
| 3625 | dispatchEntry.resolvedFlags, motionEntry.edgeFlags, |
| 3626 | motionEntry.metaState, motionEntry.buttonState, |
| 3627 | motionEntry.classification, dispatchEntry.transform, |
| 3628 | motionEntry.xPrecision, motionEntry.yPrecision, |
| 3629 | motionEntry.xCursorPosition, motionEntry.yCursorPosition, |
| 3630 | dispatchEntry.rawTransform, motionEntry.downTime, |
| 3631 | motionEntry.eventTime, motionEntry.getPointerCount(), |
| 3632 | motionEntry.pointerProperties.data(), usingCoords); |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3633 | } |
| 3634 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3635 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3636 | const std::shared_ptr<Connection>& connection) { |
Prabir Pradhan | 2dac8b8 | 2023-09-06 01:11:51 +0000 | [diff] [blame] | 3637 | ATRACE_NAME_IF(ATRACE_ENABLED(), |
| 3638 | StringPrintf("startDispatchCycleLocked(inputChannel=%s)", |
| 3639 | connection->getInputChannelName().c_str())); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3640 | if (DEBUG_DISPATCH_CYCLE) { |
| 3641 | ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str()); |
| 3642 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3643 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3644 | while (connection->status == Connection::Status::NORMAL && !connection->outboundQueue.empty()) { |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 3645 | std::unique_ptr<DispatchEntry>& dispatchEntry = connection->outboundQueue.front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3646 | dispatchEntry->deliveryTime = currentTime; |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3647 | const std::chrono::nanoseconds timeout = getDispatchingTimeoutLocked(connection); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3648 | dispatchEntry->timeoutTime = currentTime + timeout.count(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3649 | |
| 3650 | // Publish the event. |
| 3651 | status_t status; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3652 | const EventEntry& eventEntry = *(dispatchEntry->eventEntry); |
| 3653 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3654 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3655 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 3656 | std::array<uint8_t, 32> hmac = getSignature(keyEntry, *dispatchEntry); |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 3657 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
Siarhei Vishniakou | 827d1ac | 2023-07-21 16:37:51 -0700 | [diff] [blame] | 3658 | LOG(INFO) << "Publishing " << *dispatchEntry << " to " |
| 3659 | << connection->getInputChannelName(); |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 3660 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3661 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3662 | // Publish the key event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3663 | status = connection->inputPublisher |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3664 | .publishKeyEvent(dispatchEntry->seq, keyEntry.id, |
| 3665 | keyEntry.deviceId, keyEntry.source, |
| 3666 | keyEntry.displayId, std::move(hmac), |
| 3667 | keyEntry.action, dispatchEntry->resolvedFlags, |
| 3668 | keyEntry.keyCode, keyEntry.scanCode, |
| 3669 | keyEntry.metaState, keyEntry.repeatCount, |
| 3670 | keyEntry.downTime, keyEntry.eventTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3671 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3672 | } |
| 3673 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3674 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 3675 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
Siarhei Vishniakou | 827d1ac | 2023-07-21 16:37:51 -0700 | [diff] [blame] | 3676 | LOG(INFO) << "Publishing " << *dispatchEntry << " to " |
| 3677 | << connection->getInputChannelName(); |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 3678 | } |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3679 | status = publishMotionEvent(*connection, *dispatchEntry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3680 | break; |
| 3681 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3682 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3683 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3684 | const FocusEntry& focusEntry = static_cast<const FocusEntry&>(eventEntry); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3685 | status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3686 | focusEntry.id, |
Antonio Kantek | 3cfec7b | 2021-11-05 18:26:17 -0700 | [diff] [blame] | 3687 | focusEntry.hasFocus); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3688 | break; |
| 3689 | } |
| 3690 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3691 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 3692 | const TouchModeEntry& touchModeEntry = |
| 3693 | static_cast<const TouchModeEntry&>(eventEntry); |
| 3694 | status = connection->inputPublisher |
| 3695 | .publishTouchModeEvent(dispatchEntry->seq, touchModeEntry.id, |
| 3696 | touchModeEntry.inTouchMode); |
| 3697 | |
| 3698 | break; |
| 3699 | } |
| 3700 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3701 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 3702 | const auto& captureEntry = |
| 3703 | static_cast<const PointerCaptureChangedEntry&>(eventEntry); |
| 3704 | status = connection->inputPublisher |
| 3705 | .publishCaptureEvent(dispatchEntry->seq, captureEntry.id, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 3706 | captureEntry.pointerCaptureRequest.enable); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3707 | break; |
| 3708 | } |
| 3709 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3710 | case EventEntry::Type::DRAG: { |
| 3711 | const DragEntry& dragEntry = static_cast<const DragEntry&>(eventEntry); |
| 3712 | status = connection->inputPublisher.publishDragEvent(dispatchEntry->seq, |
| 3713 | dragEntry.id, dragEntry.x, |
| 3714 | dragEntry.y, |
| 3715 | dragEntry.isExiting); |
| 3716 | break; |
| 3717 | } |
| 3718 | |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3719 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3720 | case EventEntry::Type::DEVICE_RESET: |
| 3721 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3722 | LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3723 | ftl::enum_string(eventEntry.type).c_str()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3724 | return; |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3725 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3726 | } |
| 3727 | |
| 3728 | // Check the result. |
| 3729 | if (status) { |
| 3730 | if (status == WOULD_BLOCK) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3731 | if (connection->waitQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3732 | ALOGE("channel '%s' ~ Could not publish event because the pipe is full. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3733 | "This is unexpected because the wait queue is empty, so the pipe " |
| 3734 | "should be empty and we shouldn't have any problems writing an " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3735 | "event to it, status=%s(%d)", |
| 3736 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3737 | status); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 3738 | abortBrokenDispatchCycleLocked(currentTime, connection, /*notify=*/true); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3739 | } else { |
| 3740 | // Pipe is full and we are waiting for the app to finish process some events |
| 3741 | // before sending more events to it. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3742 | if (DEBUG_DISPATCH_CYCLE) { |
| 3743 | ALOGD("channel '%s' ~ Could not publish event because the pipe is full, " |
| 3744 | "waiting for the application to catch up", |
| 3745 | connection->getInputChannelName().c_str()); |
| 3746 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3747 | } |
| 3748 | } else { |
| 3749 | ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3750 | "status=%s(%d)", |
| 3751 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3752 | status); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 3753 | abortBrokenDispatchCycleLocked(currentTime, connection, /*notify=*/true); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3754 | } |
| 3755 | return; |
| 3756 | } |
| 3757 | |
| 3758 | // Re-enqueue the event on the wait queue. |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 3759 | const nsecs_t timeoutTime = dispatchEntry->timeoutTime; |
| 3760 | connection->waitQueue.emplace_back(std::move(dispatchEntry)); |
| 3761 | connection->outboundQueue.erase(connection->outboundQueue.begin()); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3762 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3763 | if (connection->responsive) { |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 3764 | mAnrTracker.insert(timeoutTime, connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3765 | } |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3766 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3767 | } |
| 3768 | } |
| 3769 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3770 | std::array<uint8_t, 32> InputDispatcher::sign(const VerifiedInputEvent& event) const { |
| 3771 | size_t size; |
| 3772 | switch (event.type) { |
| 3773 | case VerifiedInputEvent::Type::KEY: { |
| 3774 | size = sizeof(VerifiedKeyEvent); |
| 3775 | break; |
| 3776 | } |
| 3777 | case VerifiedInputEvent::Type::MOTION: { |
| 3778 | size = sizeof(VerifiedMotionEvent); |
| 3779 | break; |
| 3780 | } |
| 3781 | } |
| 3782 | const uint8_t* start = reinterpret_cast<const uint8_t*>(&event); |
| 3783 | return mHmacKeyManager.sign(start, size); |
| 3784 | } |
| 3785 | |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3786 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3787 | const MotionEntry& motionEntry, const DispatchEntry& dispatchEntry) const { |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 3788 | const int32_t actionMasked = MotionEvent::getActionMasked(motionEntry.action); |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3789 | if (actionMasked != AMOTION_EVENT_ACTION_UP && actionMasked != AMOTION_EVENT_ACTION_DOWN) { |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3790 | // Only sign events up and down events as the purely move events |
| 3791 | // are tied to their up/down counterparts so signing would be redundant. |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3792 | return INVALID_HMAC; |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3793 | } |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3794 | |
| 3795 | VerifiedMotionEvent verifiedEvent = |
| 3796 | verifiedMotionEventFromMotionEntry(motionEntry, dispatchEntry.rawTransform); |
| 3797 | verifiedEvent.actionMasked = actionMasked; |
| 3798 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS; |
| 3799 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3800 | } |
| 3801 | |
| 3802 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3803 | const KeyEntry& keyEntry, const DispatchEntry& dispatchEntry) const { |
| 3804 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(keyEntry); |
| 3805 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_KEY_EVENT_FLAGS; |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3806 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3807 | } |
| 3808 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3809 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3810 | const std::shared_ptr<Connection>& connection, |
| 3811 | uint32_t seq, bool handled, nsecs_t consumeTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3812 | if (DEBUG_DISPATCH_CYCLE) { |
| 3813 | ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s", |
| 3814 | connection->getInputChannelName().c_str(), seq, toString(handled)); |
| 3815 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3816 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3817 | if (connection->status == Connection::Status::BROKEN || |
| 3818 | connection->status == Connection::Status::ZOMBIE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3819 | return; |
| 3820 | } |
| 3821 | |
| 3822 | // Notify other system components and prepare to start the next dispatch cycle. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3823 | auto command = [this, currentTime, connection, seq, handled, consumeTime]() REQUIRES(mLock) { |
| 3824 | doDispatchCycleFinishedCommand(currentTime, connection, seq, handled, consumeTime); |
| 3825 | }; |
| 3826 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3827 | } |
| 3828 | |
| 3829 | void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3830 | const std::shared_ptr<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3831 | bool notify) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3832 | if (DEBUG_DISPATCH_CYCLE) { |
Siarhei Vishniakou | 827d1ac | 2023-07-21 16:37:51 -0700 | [diff] [blame] | 3833 | LOG(INFO) << "channel '" << connection->getInputChannelName() << "'~ " << __func__ |
| 3834 | << " - notify=" << toString(notify); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3835 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3836 | |
| 3837 | // Clear the dispatch queues. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3838 | drainDispatchQueue(connection->outboundQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3839 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3840 | drainDispatchQueue(connection->waitQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3841 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3842 | |
| 3843 | // The connection appears to be unrecoverably broken. |
| 3844 | // Ignore already broken or zombie connections. |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3845 | if (connection->status == Connection::Status::NORMAL) { |
| 3846 | connection->status = Connection::Status::BROKEN; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3847 | |
| 3848 | if (notify) { |
| 3849 | // Notify other system components. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3850 | ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", |
| 3851 | connection->getInputChannelName().c_str()); |
| 3852 | |
| 3853 | auto command = [this, connection]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3854 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 3855 | mPolicy.notifyInputChannelBroken(connection->inputChannel->getConnectionToken()); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3856 | }; |
| 3857 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3858 | } |
| 3859 | } |
| 3860 | } |
| 3861 | |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 3862 | void InputDispatcher::drainDispatchQueue(std::deque<std::unique_ptr<DispatchEntry>>& queue) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3863 | while (!queue.empty()) { |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 3864 | releaseDispatchEntry(std::move(queue.front())); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3865 | queue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3866 | } |
| 3867 | } |
| 3868 | |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 3869 | void InputDispatcher::releaseDispatchEntry(std::unique_ptr<DispatchEntry> dispatchEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3870 | if (dispatchEntry->hasForegroundTarget()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3871 | decrementPendingForegroundDispatches(*(dispatchEntry->eventEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3872 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3873 | } |
| 3874 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3875 | int InputDispatcher::handleReceiveCallback(int events, sp<IBinder> connectionToken) { |
| 3876 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3877 | std::shared_ptr<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3878 | if (connection == nullptr) { |
| 3879 | ALOGW("Received looper callback for unknown input channel token %p. events=0x%x", |
| 3880 | connectionToken.get(), events); |
| 3881 | return 0; // remove the callback |
| 3882 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3883 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3884 | bool notify; |
| 3885 | if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { |
| 3886 | if (!(events & ALOOPER_EVENT_INPUT)) { |
| 3887 | ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. " |
| 3888 | "events=0x%x", |
| 3889 | connection->getInputChannelName().c_str(), events); |
| 3890 | return 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3891 | } |
| 3892 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3893 | nsecs_t currentTime = now(); |
| 3894 | bool gotOne = false; |
| 3895 | status_t status = OK; |
| 3896 | for (;;) { |
| 3897 | Result<InputPublisher::ConsumerResponse> result = |
| 3898 | connection->inputPublisher.receiveConsumerResponse(); |
| 3899 | if (!result.ok()) { |
| 3900 | status = result.error().code(); |
| 3901 | break; |
| 3902 | } |
| 3903 | |
| 3904 | if (std::holds_alternative<InputPublisher::Finished>(*result)) { |
| 3905 | const InputPublisher::Finished& finish = |
| 3906 | std::get<InputPublisher::Finished>(*result); |
| 3907 | finishDispatchCycleLocked(currentTime, connection, finish.seq, finish.handled, |
| 3908 | finish.consumeTime); |
| 3909 | } else if (std::holds_alternative<InputPublisher::Timeline>(*result)) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3910 | if (shouldReportMetricsForConnection(*connection)) { |
| 3911 | const InputPublisher::Timeline& timeline = |
| 3912 | std::get<InputPublisher::Timeline>(*result); |
| 3913 | mLatencyTracker |
| 3914 | .trackGraphicsLatency(timeline.inputEventId, |
| 3915 | connection->inputChannel->getConnectionToken(), |
| 3916 | std::move(timeline.graphicsTimeline)); |
| 3917 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3918 | } |
| 3919 | gotOne = true; |
| 3920 | } |
| 3921 | if (gotOne) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3922 | runCommandsLockedInterruptable(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3923 | if (status == WOULD_BLOCK) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3924 | return 1; |
| 3925 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3926 | } |
| 3927 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3928 | notify = status != DEAD_OBJECT || !connection->monitor; |
| 3929 | if (notify) { |
| 3930 | ALOGE("channel '%s' ~ Failed to receive finished signal. status=%s(%d)", |
| 3931 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3932 | status); |
| 3933 | } |
| 3934 | } else { |
| 3935 | // Monitor channels are never explicitly unregistered. |
| 3936 | // We do it automatically when the remote endpoint is closed so don't warn about them. |
| 3937 | const bool stillHaveWindowHandle = |
| 3938 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()) != nullptr; |
| 3939 | notify = !connection->monitor && stillHaveWindowHandle; |
| 3940 | if (notify) { |
| 3941 | ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred. events=0x%x", |
| 3942 | connection->getInputChannelName().c_str(), events); |
| 3943 | } |
| 3944 | } |
| 3945 | |
| 3946 | // Remove the channel. |
| 3947 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), notify); |
| 3948 | return 0; // remove the callback |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3949 | } |
| 3950 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3951 | void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3952 | const CancelationOptions& options) { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3953 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 3954 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3955 | } |
| 3956 | } |
| 3957 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3958 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3959 | const CancelationOptions& options) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 3960 | for (const auto& [_, monitors] : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3961 | for (const Monitor& monitor : monitors) { |
| 3962 | synthesizeCancelationEventsForInputChannelLocked(monitor.inputChannel, options); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3963 | } |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3964 | } |
| 3965 | } |
| 3966 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3967 | void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked( |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 3968 | const std::shared_ptr<InputChannel>& channel, const CancelationOptions& options) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3969 | std::shared_ptr<Connection> connection = getConnectionLocked(channel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3970 | if (connection == nullptr) { |
| 3971 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3972 | } |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3973 | |
| 3974 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3975 | } |
| 3976 | |
| 3977 | void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3978 | const std::shared_ptr<Connection>& connection, const CancelationOptions& options) { |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3979 | if (connection->status == Connection::Status::BROKEN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3980 | return; |
| 3981 | } |
| 3982 | |
| 3983 | nsecs_t currentTime = now(); |
| 3984 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3985 | std::vector<std::unique_ptr<EventEntry>> cancelationEvents = |
Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 3986 | connection->inputState.synthesizeCancelationEvents(currentTime, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3987 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3988 | if (cancelationEvents.empty()) { |
| 3989 | return; |
| 3990 | } |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 3991 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3992 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 3993 | 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] | 3994 | "with reality: %s, mode=%s.", |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3995 | connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason, |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3996 | ftl::enum_string(options.mode).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3997 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3998 | |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3999 | std::string reason = std::string("reason=").append(options.reason); |
| 4000 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 4001 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 4002 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 4003 | const bool wasEmpty = connection->outboundQueue.empty(); |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 4004 | // The target to use if we don't find a window associated with the channel. |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 4005 | const InputTarget fallbackTarget{.inputChannel = connection->inputChannel}; |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 4006 | const auto& token = connection->inputChannel->getConnectionToken(); |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 4007 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4008 | for (size_t i = 0; i < cancelationEvents.size(); i++) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4009 | std::unique_ptr<EventEntry> cancelationEventEntry = std::move(cancelationEvents[i]); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4010 | std::vector<InputTarget> targets{}; |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4011 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4012 | switch (cancelationEventEntry->type) { |
| 4013 | case EventEntry::Type::KEY: { |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4014 | const auto& keyEntry = static_cast<const KeyEntry&>(*cancelationEventEntry); |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 4015 | const std::optional<int32_t> targetDisplay = keyEntry.displayId != ADISPLAY_ID_NONE |
| 4016 | ? std::make_optional(keyEntry.displayId) |
| 4017 | : std::nullopt; |
| 4018 | if (const auto& window = getWindowHandleLocked(token, targetDisplay); window) { |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 4019 | addWindowTargetLocked(window, InputTarget::DispatchMode::AS_IS, |
| 4020 | /*targetFlags=*/{}, keyEntry.downTime, targets); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4021 | } else { |
| 4022 | targets.emplace_back(fallbackTarget); |
| 4023 | } |
| 4024 | logOutboundKeyDetails("cancel - ", keyEntry); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4025 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4026 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4027 | case EventEntry::Type::MOTION: { |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4028 | const auto& motionEntry = static_cast<const MotionEntry&>(*cancelationEventEntry); |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 4029 | const std::optional<int32_t> targetDisplay = |
| 4030 | motionEntry.displayId != ADISPLAY_ID_NONE |
| 4031 | ? std::make_optional(motionEntry.displayId) |
| 4032 | : std::nullopt; |
| 4033 | if (const auto& window = getWindowHandleLocked(token, targetDisplay); window) { |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4034 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4035 | for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.getPointerCount(); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4036 | pointerIndex++) { |
| 4037 | pointerIds.set(motionEntry.pointerProperties[pointerIndex].id); |
| 4038 | } |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 4039 | if (mDragState && mDragState->dragWindow->getToken() == token && |
| 4040 | pointerIds.test(mDragState->pointerId)) { |
| 4041 | LOG(INFO) << __func__ |
| 4042 | << ": Canceling drag and drop because the pointers for the drag " |
| 4043 | "window are being canceled."; |
| 4044 | sendDropWindowCommandLocked(nullptr, /*x=*/0, /*y=*/0); |
| 4045 | mDragState.reset(); |
| 4046 | } |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 4047 | addPointerWindowTargetLocked(window, InputTarget::DispatchMode::AS_IS, |
| 4048 | ftl::Flags<InputTarget::Flags>(), pointerIds, |
| 4049 | motionEntry.downTime, targets); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4050 | } else { |
| 4051 | targets.emplace_back(fallbackTarget); |
| 4052 | const auto it = mDisplayInfos.find(motionEntry.displayId); |
| 4053 | if (it != mDisplayInfos.end()) { |
| 4054 | targets.back().displayTransform = it->second.transform; |
| 4055 | targets.back().setDefaultPointerTransform(it->second.transform); |
| 4056 | } |
| 4057 | } |
| 4058 | logOutboundMotionDetails("cancel - ", motionEntry); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4059 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4060 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4061 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 4062 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4063 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 4064 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4065 | LOG_ALWAYS_FATAL("Canceling %s events is not supported", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 4066 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4067 | break; |
| 4068 | } |
| 4069 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4070 | case EventEntry::Type::DEVICE_RESET: |
| 4071 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4072 | 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] | 4073 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4074 | break; |
| 4075 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4076 | } |
| 4077 | |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4078 | if (targets.size() != 1) LOG(FATAL) << __func__ << ": InputTarget not created"; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 4079 | enqueueDispatchEntryLocked(connection, std::move(cancelationEventEntry), targets[0]); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4080 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 4081 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 4082 | // If the outbound queue was previously empty, start the dispatch cycle going. |
| 4083 | if (wasEmpty && !connection->outboundQueue.empty()) { |
| 4084 | startDispatchCycleLocked(currentTime, connection); |
| 4085 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4086 | } |
| 4087 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4088 | void InputDispatcher::synthesizePointerDownEventsForConnectionLocked( |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 4089 | const nsecs_t downTime, const std::shared_ptr<Connection>& connection, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4090 | ftl::Flags<InputTarget::Flags> targetFlags) { |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 4091 | if (connection->status == Connection::Status::BROKEN) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4092 | return; |
| 4093 | } |
| 4094 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4095 | std::vector<std::unique_ptr<EventEntry>> downEvents = |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4096 | connection->inputState.synthesizePointerDownEvents(downTime); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4097 | |
| 4098 | if (downEvents.empty()) { |
| 4099 | return; |
| 4100 | } |
| 4101 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4102 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4103 | ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.", |
| 4104 | connection->getInputChannelName().c_str(), downEvents.size()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4105 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4106 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4107 | sp<WindowInfoHandle> windowHandle = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4108 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4109 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 4110 | const bool wasEmpty = connection->outboundQueue.empty(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4111 | for (std::unique_ptr<EventEntry>& downEventEntry : downEvents) { |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 4112 | std::vector<InputTarget> targets{}; |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4113 | switch (downEventEntry->type) { |
| 4114 | case EventEntry::Type::MOTION: { |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 4115 | const auto& motionEntry = static_cast<const MotionEntry&>(*downEventEntry); |
| 4116 | if (windowHandle != nullptr) { |
| 4117 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4118 | for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.getPointerCount(); |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 4119 | pointerIndex++) { |
| 4120 | pointerIds.set(motionEntry.pointerProperties[pointerIndex].id); |
| 4121 | } |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 4122 | addPointerWindowTargetLocked(windowHandle, InputTarget::DispatchMode::AS_IS, |
| 4123 | targetFlags, pointerIds, motionEntry.downTime, |
| 4124 | targets); |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 4125 | } else { |
| 4126 | targets.emplace_back(InputTarget{.inputChannel = connection->inputChannel, |
| 4127 | .flags = targetFlags}); |
| 4128 | const auto it = mDisplayInfos.find(motionEntry.displayId); |
| 4129 | if (it != mDisplayInfos.end()) { |
| 4130 | targets.back().displayTransform = it->second.transform; |
| 4131 | targets.back().setDefaultPointerTransform(it->second.transform); |
| 4132 | } |
| 4133 | } |
| 4134 | logOutboundMotionDetails("down - ", motionEntry); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4135 | break; |
| 4136 | } |
| 4137 | |
| 4138 | case EventEntry::Type::KEY: |
| 4139 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 4140 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4141 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4142 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4143 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4144 | case EventEntry::Type::SENSOR: |
| 4145 | case EventEntry::Type::DRAG: { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4146 | 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] | 4147 | ftl::enum_string(downEventEntry->type).c_str()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4148 | break; |
| 4149 | } |
| 4150 | } |
| 4151 | |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 4152 | if (targets.size() != 1) LOG(FATAL) << __func__ << ": InputTarget not created"; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 4153 | enqueueDispatchEntryLocked(connection, std::move(downEventEntry), targets[0]); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4154 | } |
| 4155 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 4156 | // If the outbound queue was previously empty, start the dispatch cycle going. |
| 4157 | if (wasEmpty && !connection->outboundQueue.empty()) { |
| 4158 | startDispatchCycleLocked(downTime, connection); |
| 4159 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4160 | } |
| 4161 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4162 | void InputDispatcher::synthesizeCancelationEventsForWindowLocked( |
| 4163 | const sp<WindowInfoHandle>& windowHandle, const CancelationOptions& options) { |
| 4164 | if (windowHandle != nullptr) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 4165 | std::shared_ptr<Connection> wallpaperConnection = |
| 4166 | getConnectionLocked(windowHandle->getToken()); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4167 | if (wallpaperConnection != nullptr) { |
| 4168 | synthesizeCancelationEventsForConnectionLocked(wallpaperConnection, options); |
| 4169 | } |
| 4170 | } |
| 4171 | } |
| 4172 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4173 | std::unique_ptr<MotionEntry> InputDispatcher::splitMotionEvent( |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 4174 | const MotionEntry& originalMotionEntry, std::bitset<MAX_POINTER_ID + 1> pointerIds, |
| 4175 | nsecs_t splitDownTime) { |
| 4176 | ALOG_ASSERT(pointerIds.any()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4177 | |
| 4178 | uint32_t splitPointerIndexMap[MAX_POINTERS]; |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4179 | std::vector<PointerProperties> splitPointerProperties; |
| 4180 | std::vector<PointerCoords> splitPointerCoords; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4181 | |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4182 | uint32_t originalPointerCount = originalMotionEntry.getPointerCount(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4183 | uint32_t splitPointerCount = 0; |
| 4184 | |
| 4185 | for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4186 | originalPointerIndex++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4187 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 4188 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4189 | uint32_t pointerId = uint32_t(pointerProperties.id); |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 4190 | if (pointerIds.test(pointerId)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4191 | splitPointerIndexMap[splitPointerCount] = originalPointerIndex; |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4192 | splitPointerProperties.push_back(pointerProperties); |
| 4193 | splitPointerCoords.push_back(originalMotionEntry.pointerCoords[originalPointerIndex]); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4194 | splitPointerCount += 1; |
| 4195 | } |
| 4196 | } |
| 4197 | |
| 4198 | if (splitPointerCount != pointerIds.count()) { |
| 4199 | // This is bad. We are missing some of the pointers that we expected to deliver. |
| 4200 | // Most likely this indicates that we received an ACTION_MOVE events that has |
| 4201 | // different pointer ids than we expected based on the previous ACTION_DOWN |
| 4202 | // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers |
| 4203 | // in this way. |
| 4204 | ALOGW("Dropping split motion event because the pointer count is %d but " |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 4205 | "we expected there to be %zu pointers. This probably means we received " |
Siarhei Vishniakou | 16e4fa0 | 2023-02-16 17:48:56 -0800 | [diff] [blame] | 4206 | "a broken sequence of pointer ids from the input device: %s", |
| 4207 | splitPointerCount, pointerIds.count(), originalMotionEntry.getDescription().c_str()); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4208 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4209 | } |
| 4210 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 4211 | int32_t action = originalMotionEntry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4212 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4213 | if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN || |
| 4214 | maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
Siarhei Vishniakou | 5b9766d | 2023-07-18 14:06:29 -0700 | [diff] [blame] | 4215 | int32_t originalPointerIndex = MotionEvent::getActionIndex(action); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4216 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 4217 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4218 | uint32_t pointerId = uint32_t(pointerProperties.id); |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 4219 | if (pointerIds.test(pointerId)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4220 | if (pointerIds.count() == 1) { |
| 4221 | // The first/last pointer went down/up. |
| 4222 | action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4223 | ? AMOTION_EVENT_ACTION_DOWN |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4224 | : (originalMotionEntry.flags & AMOTION_EVENT_FLAG_CANCELED) != 0 |
| 4225 | ? AMOTION_EVENT_ACTION_CANCEL |
| 4226 | : AMOTION_EVENT_ACTION_UP; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4227 | } else { |
| 4228 | // A secondary pointer went down/up. |
| 4229 | uint32_t splitPointerIndex = 0; |
| 4230 | while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { |
| 4231 | splitPointerIndex += 1; |
| 4232 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4233 | action = maskedAction | |
| 4234 | (splitPointerIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4235 | } |
| 4236 | } else { |
| 4237 | // An unrelated pointer changed. |
| 4238 | action = AMOTION_EVENT_ACTION_MOVE; |
| 4239 | } |
| 4240 | } |
| 4241 | |
Siarhei Vishniakou | 59e302b | 2023-06-05 08:04:53 -0700 | [diff] [blame] | 4242 | if (action == AMOTION_EVENT_ACTION_DOWN && splitDownTime != originalMotionEntry.eventTime) { |
| 4243 | logDispatchStateLocked(); |
| 4244 | LOG_ALWAYS_FATAL("Split motion event has mismatching downTime and eventTime for " |
| 4245 | "ACTION_DOWN, motionEntry=%s, splitDownTime=%" PRId64, |
| 4246 | originalMotionEntry.getDescription().c_str(), splitDownTime); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4247 | } |
| 4248 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4249 | int32_t newId = mIdGenerator.nextId(); |
Prabir Pradhan | 2dac8b8 | 2023-09-06 01:11:51 +0000 | [diff] [blame] | 4250 | ATRACE_NAME_IF(ATRACE_ENABLED(), |
| 4251 | StringPrintf("Split MotionEvent(id=0x%" PRIx32 ") to MotionEvent(id=0x%" PRIx32 |
| 4252 | ").", |
| 4253 | originalMotionEntry.id, newId)); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4254 | std::unique_ptr<MotionEntry> splitMotionEntry = |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4255 | std::make_unique<MotionEntry>(newId, originalMotionEntry.injectionState, |
| 4256 | originalMotionEntry.eventTime, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4257 | originalMotionEntry.deviceId, originalMotionEntry.source, |
| 4258 | originalMotionEntry.displayId, |
| 4259 | originalMotionEntry.policyFlags, action, |
| 4260 | originalMotionEntry.actionButton, |
| 4261 | originalMotionEntry.flags, originalMotionEntry.metaState, |
| 4262 | originalMotionEntry.buttonState, |
| 4263 | originalMotionEntry.classification, |
| 4264 | originalMotionEntry.edgeFlags, |
| 4265 | originalMotionEntry.xPrecision, |
| 4266 | originalMotionEntry.yPrecision, |
| 4267 | originalMotionEntry.xCursorPosition, |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4268 | originalMotionEntry.yCursorPosition, splitDownTime, |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4269 | splitPointerProperties, splitPointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4270 | |
| 4271 | return splitMotionEntry; |
| 4272 | } |
| 4273 | |
Asmita Poddar | dd9a6cd | 2023-09-26 15:35:12 +0000 | [diff] [blame] | 4274 | void InputDispatcher::notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs& args) { |
| 4275 | std::scoped_lock _l(mLock); |
| 4276 | mLatencyTracker.setInputDevices(args.inputDeviceInfos); |
| 4277 | } |
| 4278 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4279 | void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4280 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4281 | ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args.eventTime); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4282 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4283 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4284 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4285 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4286 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4287 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4288 | std::unique_ptr<ConfigurationChangedEntry> newEntry = |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4289 | std::make_unique<ConfigurationChangedEntry>(args.id, args.eventTime); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4290 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4291 | } // release lock |
| 4292 | |
| 4293 | if (needWake) { |
| 4294 | mLooper->wake(); |
| 4295 | } |
| 4296 | } |
| 4297 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4298 | void InputDispatcher::notifyKey(const NotifyKeyArgs& args) { |
Prabir Pradhan | 96282b0 | 2023-02-24 22:36:17 +0000 | [diff] [blame] | 4299 | ALOGD_IF(debugInboundEventDetails(), |
| 4300 | "notifyKey - id=%" PRIx32 ", eventTime=%" PRId64 |
| 4301 | ", deviceId=%d, source=%s, displayId=%" PRId32 |
| 4302 | "policyFlags=0x%x, action=%s, flags=0x%x, keyCode=%s, scanCode=0x%x, metaState=0x%x, " |
| 4303 | "downTime=%" PRId64, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4304 | args.id, args.eventTime, args.deviceId, inputEventSourceToString(args.source).c_str(), |
| 4305 | args.displayId, args.policyFlags, KeyEvent::actionToString(args.action), args.flags, |
| 4306 | KeyEvent::getLabel(args.keyCode), args.scanCode, args.metaState, args.downTime); |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4307 | Result<void> keyCheck = validateKeyEvent(args.action); |
| 4308 | if (!keyCheck.ok()) { |
| 4309 | LOG(ERROR) << "invalid key event: " << keyCheck.error(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4310 | return; |
| 4311 | } |
| 4312 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4313 | uint32_t policyFlags = args.policyFlags; |
| 4314 | int32_t flags = args.flags; |
| 4315 | int32_t metaState = args.metaState; |
Siarhei Vishniakou | 622bd32 | 2018-10-29 18:02:27 -0700 | [diff] [blame] | 4316 | // InputDispatcher tracks and generates key repeats on behalf of |
| 4317 | // whatever notifies it, so repeatCount should always be set to 0 |
| 4318 | constexpr int32_t repeatCount = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4319 | if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { |
| 4320 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 4321 | flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
| 4322 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4323 | if (policyFlags & POLICY_FLAG_FUNCTION) { |
| 4324 | metaState |= AMETA_FUNCTION_ON; |
| 4325 | } |
| 4326 | |
| 4327 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 4328 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4329 | int32_t keyCode = args.keyCode; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4330 | KeyEvent event; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4331 | event.initialize(args.id, args.deviceId, args.source, args.displayId, INVALID_HMAC, args.action, |
| 4332 | flags, keyCode, args.scanCode, metaState, repeatCount, args.downTime, |
| 4333 | args.eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4334 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4335 | android::base::Timer t; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4336 | mPolicy.interceptKeyBeforeQueueing(event, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4337 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4338 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4339 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4340 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4341 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4342 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4343 | { // acquire lock |
| 4344 | mLock.lock(); |
| 4345 | |
| 4346 | if (shouldSendKeyToInputFilterLocked(args)) { |
| 4347 | mLock.unlock(); |
| 4348 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4349 | policyFlags |= POLICY_FLAG_FILTERED; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4350 | if (!mPolicy.filterInputEvent(event, policyFlags)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4351 | return; // event was consumed by the filter |
| 4352 | } |
| 4353 | |
| 4354 | mLock.lock(); |
| 4355 | } |
| 4356 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4357 | std::unique_ptr<KeyEntry> newEntry = |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4358 | std::make_unique<KeyEntry>(args.id, /*injectionState=*/nullptr, args.eventTime, |
| 4359 | args.deviceId, args.source, args.displayId, policyFlags, |
| 4360 | args.action, flags, keyCode, args.scanCode, metaState, |
| 4361 | repeatCount, args.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4362 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4363 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4364 | mLock.unlock(); |
| 4365 | } // release lock |
| 4366 | |
| 4367 | if (needWake) { |
| 4368 | mLooper->wake(); |
| 4369 | } |
| 4370 | } |
| 4371 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4372 | bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs& args) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4373 | return mInputFilterEnabled; |
| 4374 | } |
| 4375 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4376 | void InputDispatcher::notifyMotion(const NotifyMotionArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4377 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 96282b0 | 2023-02-24 22:36:17 +0000 | [diff] [blame] | 4378 | ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=%s, " |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4379 | "displayId=%" PRId32 ", policyFlags=0x%x, " |
Siarhei Vishniakou | 6ebd069 | 2022-10-20 15:05:45 -0700 | [diff] [blame] | 4380 | "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] | 4381 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, xCursorPosition=%f, " |
| 4382 | "yCursorPosition=%f, downTime=%" PRId64, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4383 | args.id, args.eventTime, args.deviceId, inputEventSourceToString(args.source).c_str(), |
| 4384 | args.displayId, args.policyFlags, MotionEvent::actionToString(args.action).c_str(), |
| 4385 | args.actionButton, args.flags, args.metaState, args.buttonState, args.edgeFlags, |
| 4386 | args.xPrecision, args.yPrecision, args.xCursorPosition, args.yCursorPosition, |
| 4387 | args.downTime); |
Siarhei Vishniakou | 3218fc0 | 2023-06-15 20:41:02 -0700 | [diff] [blame] | 4388 | for (uint32_t i = 0; i < args.getPointerCount(); i++) { |
Prabir Pradhan | 96282b0 | 2023-02-24 22:36:17 +0000 | [diff] [blame] | 4389 | ALOGD(" Pointer %d: id=%d, toolType=%s, x=%f, y=%f, pressure=%f, size=%f, " |
| 4390 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, orientation=%f", |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4391 | i, args.pointerProperties[i].id, |
| 4392 | ftl::enum_string(args.pointerProperties[i].toolType).c_str(), |
| 4393 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 4394 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 4395 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 4396 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 4397 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 4398 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 4399 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 4400 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 4401 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4402 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4403 | } |
Siarhei Vishniakou | 4ca9727 | 2023-03-01 11:31:35 -0800 | [diff] [blame] | 4404 | |
Siarhei Vishniakou | 3218fc0 | 2023-06-15 20:41:02 -0700 | [diff] [blame] | 4405 | Result<void> motionCheck = |
| 4406 | validateMotionEvent(args.action, args.actionButton, args.getPointerCount(), |
| 4407 | args.pointerProperties.data()); |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4408 | if (!motionCheck.ok()) { |
Siarhei Vishniakou | 23740b9 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4409 | LOG(FATAL) << "Invalid event: " << args.dump() << "; reason: " << motionCheck.error(); |
Siarhei Vishniakou | 4ca9727 | 2023-03-01 11:31:35 -0800 | [diff] [blame] | 4410 | return; |
| 4411 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4412 | |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 4413 | if (DEBUG_VERIFY_EVENTS) { |
| 4414 | auto [it, _] = |
| 4415 | mVerifiersByDisplay.try_emplace(args.displayId, |
| 4416 | StringPrintf("display %" PRId32, args.displayId)); |
| 4417 | Result<void> result = |
Siarhei Vishniakou | 2d151ac | 2023-09-19 13:30:24 -0700 | [diff] [blame] | 4418 | it->second.processMovement(args.deviceId, args.source, args.action, |
| 4419 | args.getPointerCount(), args.pointerProperties.data(), |
| 4420 | args.pointerCoords.data(), args.flags); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 4421 | if (!result.ok()) { |
| 4422 | LOG(FATAL) << "Bad stream: " << result.error() << " caused by " << args.dump(); |
| 4423 | } |
| 4424 | } |
| 4425 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4426 | uint32_t policyFlags = args.policyFlags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4427 | policyFlags |= POLICY_FLAG_TRUSTED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4428 | |
| 4429 | android::base::Timer t; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4430 | mPolicy.interceptMotionBeforeQueueing(args.displayId, args.eventTime, policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4431 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4432 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4433 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4434 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4435 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4436 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4437 | { // acquire lock |
| 4438 | mLock.lock(); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 4439 | if (!(policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
| 4440 | // Set the flag anyway if we already have an ongoing gesture. That would allow us to |
| 4441 | // complete the processing of the current stroke. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4442 | const auto touchStateIt = mTouchStatesByDisplay.find(args.displayId); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 4443 | if (touchStateIt != mTouchStatesByDisplay.end()) { |
| 4444 | const TouchState& touchState = touchStateIt->second; |
Linnan Li | 907ae73 | 2023-09-05 17:14:21 +0800 | [diff] [blame] | 4445 | if (touchState.hasTouchingPointers(args.deviceId) || |
| 4446 | touchState.hasHoveringPointers(args.deviceId)) { |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 4447 | policyFlags |= POLICY_FLAG_PASS_TO_USER; |
| 4448 | } |
| 4449 | } |
| 4450 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4451 | |
| 4452 | if (shouldSendMotionToInputFilterLocked(args)) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4453 | ui::Transform displayTransform; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4454 | if (const auto it = mDisplayInfos.find(args.displayId); it != mDisplayInfos.end()) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4455 | displayTransform = it->second.transform; |
| 4456 | } |
| 4457 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4458 | mLock.unlock(); |
| 4459 | |
| 4460 | MotionEvent event; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4461 | event.initialize(args.id, args.deviceId, args.source, args.displayId, INVALID_HMAC, |
| 4462 | args.action, args.actionButton, args.flags, args.edgeFlags, |
| 4463 | args.metaState, args.buttonState, args.classification, |
| 4464 | displayTransform, args.xPrecision, args.yPrecision, |
| 4465 | args.xCursorPosition, args.yCursorPosition, displayTransform, |
Siarhei Vishniakou | 3218fc0 | 2023-06-15 20:41:02 -0700 | [diff] [blame] | 4466 | args.downTime, args.eventTime, args.getPointerCount(), |
| 4467 | args.pointerProperties.data(), args.pointerCoords.data()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4468 | |
| 4469 | policyFlags |= POLICY_FLAG_FILTERED; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4470 | if (!mPolicy.filterInputEvent(event, policyFlags)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4471 | return; // event was consumed by the filter |
| 4472 | } |
| 4473 | |
| 4474 | mLock.lock(); |
| 4475 | } |
| 4476 | |
| 4477 | // Just enqueue a new motion event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4478 | std::unique_ptr<MotionEntry> newEntry = |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4479 | std::make_unique<MotionEntry>(args.id, /*injectionState=*/nullptr, args.eventTime, |
| 4480 | args.deviceId, args.source, args.displayId, |
| 4481 | policyFlags, args.action, args.actionButton, |
| 4482 | args.flags, args.metaState, args.buttonState, |
| 4483 | args.classification, args.edgeFlags, args.xPrecision, |
| 4484 | args.yPrecision, args.xCursorPosition, |
| 4485 | args.yCursorPosition, args.downTime, |
| 4486 | args.pointerProperties, args.pointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4487 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4488 | if (args.id != android::os::IInputConstants::INVALID_INPUT_EVENT_ID && |
| 4489 | IdGenerator::getSource(args.id) == IdGenerator::Source::INPUT_READER && |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 4490 | !mInputFilterEnabled) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4491 | const bool isDown = args.action == AMOTION_EVENT_ACTION_DOWN; |
Asmita Poddar | dd9a6cd | 2023-09-26 15:35:12 +0000 | [diff] [blame] | 4492 | std::set<InputDeviceUsageSource> sources = getUsageSourcesForMotionArgs(args); |
| 4493 | mLatencyTracker.trackListener(args.id, isDown, args.eventTime, args.readTime, |
| 4494 | args.deviceId, sources); |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 4495 | } |
| 4496 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4497 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4498 | mLock.unlock(); |
| 4499 | } // release lock |
| 4500 | |
| 4501 | if (needWake) { |
| 4502 | mLooper->wake(); |
| 4503 | } |
| 4504 | } |
| 4505 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4506 | void InputDispatcher::notifySensor(const NotifySensorArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4507 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4508 | ALOGD("notifySensor - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 4509 | " sensorType=%s", |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4510 | args.id, args.eventTime, args.deviceId, args.source, |
| 4511 | ftl::enum_string(args.sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4512 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4513 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4514 | bool needWake = false; |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4515 | { // acquire lock |
| 4516 | mLock.lock(); |
| 4517 | |
| 4518 | // Just enqueue a new sensor event. |
| 4519 | std::unique_ptr<SensorEntry> newEntry = |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4520 | std::make_unique<SensorEntry>(args.id, args.eventTime, args.deviceId, args.source, |
| 4521 | /* policyFlags=*/0, args.hwTimestamp, args.sensorType, |
| 4522 | args.accuracy, args.accuracyChanged, args.values); |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4523 | |
| 4524 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
| 4525 | mLock.unlock(); |
| 4526 | } // release lock |
| 4527 | |
| 4528 | if (needWake) { |
| 4529 | mLooper->wake(); |
| 4530 | } |
| 4531 | } |
| 4532 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4533 | void InputDispatcher::notifyVibratorState(const NotifyVibratorStateArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4534 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4535 | ALOGD("notifyVibratorState - eventTime=%" PRId64 ", device=%d, isOn=%d", args.eventTime, |
| 4536 | args.deviceId, args.isOn); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4537 | } |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4538 | mPolicy.notifyVibratorState(args.deviceId, args.isOn); |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 4539 | } |
| 4540 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4541 | bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs& args) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4542 | return mInputFilterEnabled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4543 | } |
| 4544 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4545 | void InputDispatcher::notifySwitch(const NotifySwitchArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4546 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4547 | ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, " |
| 4548 | "switchMask=0x%08x", |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4549 | args.eventTime, args.policyFlags, args.switchValues, args.switchMask); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4550 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4551 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4552 | uint32_t policyFlags = args.policyFlags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4553 | policyFlags |= POLICY_FLAG_TRUSTED; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4554 | mPolicy.notifySwitch(args.eventTime, args.switchValues, args.switchMask, policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4555 | } |
| 4556 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4557 | void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4558 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4559 | ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args.eventTime, |
| 4560 | args.deviceId); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4561 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4562 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4563 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4564 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4565 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4566 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4567 | std::unique_ptr<DeviceResetEntry> newEntry = |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4568 | std::make_unique<DeviceResetEntry>(args.id, args.eventTime, args.deviceId); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4569 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Siarhei Vishniakou | 1160ecd | 2023-06-28 15:57:47 -0700 | [diff] [blame] | 4570 | |
| 4571 | for (auto& [_, verifier] : mVerifiersByDisplay) { |
| 4572 | verifier.resetDevice(args.deviceId); |
| 4573 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4574 | } // release lock |
| 4575 | |
| 4576 | if (needWake) { |
| 4577 | mLooper->wake(); |
| 4578 | } |
| 4579 | } |
| 4580 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4581 | void InputDispatcher::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4582 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4583 | ALOGD("notifyPointerCaptureChanged - eventTime=%" PRId64 ", enabled=%s", args.eventTime, |
| 4584 | args.request.enable ? "true" : "false"); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4585 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4586 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4587 | bool needWake = false; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4588 | { // acquire lock |
| 4589 | std::scoped_lock _l(mLock); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4590 | auto entry = |
| 4591 | std::make_unique<PointerCaptureChangedEntry>(args.id, args.eventTime, args.request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4592 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 4593 | } // release lock |
| 4594 | |
| 4595 | if (needWake) { |
| 4596 | mLooper->wake(); |
| 4597 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4598 | } |
| 4599 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4600 | InputEventInjectionResult InputDispatcher::injectInputEvent(const InputEvent* event, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 4601 | std::optional<gui::Uid> targetUid, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4602 | InputEventInjectionSync syncMode, |
| 4603 | std::chrono::milliseconds timeout, |
| 4604 | uint32_t policyFlags) { |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4605 | Result<void> eventValidation = validateInputEvent(*event); |
| 4606 | if (!eventValidation.ok()) { |
| 4607 | LOG(INFO) << "Injection failed: invalid event: " << eventValidation.error(); |
| 4608 | return InputEventInjectionResult::FAILED; |
| 4609 | } |
| 4610 | |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4611 | if (debugInboundEventDetails()) { |
Siarhei Vishniakou | 827d1ac | 2023-07-21 16:37:51 -0700 | [diff] [blame] | 4612 | LOG(INFO) << __func__ << ": targetUid=" << toString(targetUid, &uidString) |
| 4613 | << ", syncMode=" << ftl::enum_string(syncMode) << ", timeout=" << timeout.count() |
| 4614 | << "ms, policyFlags=0x" << std::hex << policyFlags << std::dec |
| 4615 | << ", event=" << *event; |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4616 | } |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 4617 | 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] | 4618 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4619 | policyFlags |= POLICY_FLAG_INJECTED | POLICY_FLAG_TRUSTED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4620 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4621 | // 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] | 4622 | // that have gone through the InputFilter. If the event passed through the InputFilter, assign |
| 4623 | // the provided device id. If the InputFilter is accessibility, and it modifies or synthesizes |
| 4624 | // the injected event, it is responsible for setting POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY. |
| 4625 | // For those events, we will set FLAG_IS_ACCESSIBILITY_EVENT to allow apps to distinguish them |
| 4626 | // from events that originate from actual hardware. |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 4627 | DeviceId resolvedDeviceId = VIRTUAL_KEYBOARD_ID; |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4628 | if (policyFlags & POLICY_FLAG_FILTERED) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4629 | resolvedDeviceId = event->getDeviceId(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4630 | } |
| 4631 | |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4632 | const bool isAsync = syncMode == InputEventInjectionSync::NONE; |
| 4633 | auto injectionState = std::make_shared<InjectionState>(targetUid, isAsync); |
| 4634 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4635 | std::queue<std::unique_ptr<EventEntry>> injectedEntries; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4636 | switch (event->getType()) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4637 | case InputEventType::KEY: { |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4638 | const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event); |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4639 | const int32_t action = incomingKey.getAction(); |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4640 | int32_t flags = incomingKey.getFlags(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4641 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4642 | flags |= AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4643 | } |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4644 | int32_t keyCode = incomingKey.getKeyCode(); |
| 4645 | int32_t metaState = incomingKey.getMetaState(); |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4646 | KeyEvent keyEvent; |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4647 | keyEvent.initialize(incomingKey.getId(), resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4648 | incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode, |
| 4649 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), |
| 4650 | incomingKey.getDownTime(), incomingKey.getEventTime()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4651 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4652 | if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) { |
| 4653 | policyFlags |= POLICY_FLAG_VIRTUAL; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4654 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4655 | |
| 4656 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 4657 | android::base::Timer t; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4658 | mPolicy.interceptKeyBeforeQueueing(keyEvent, /*byref*/ policyFlags); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4659 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4660 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
| 4661 | std::to_string(t.duration().count()).c_str()); |
| 4662 | } |
| 4663 | } |
| 4664 | |
| 4665 | mLock.lock(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4666 | std::unique_ptr<KeyEntry> injectedEntry = |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4667 | std::make_unique<KeyEntry>(incomingKey.getId(), injectionState, |
| 4668 | incomingKey.getEventTime(), resolvedDeviceId, |
| 4669 | incomingKey.getSource(), incomingKey.getDisplayId(), |
| 4670 | policyFlags, action, flags, keyCode, |
| 4671 | incomingKey.getScanCode(), metaState, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4672 | incomingKey.getRepeatCount(), |
| 4673 | incomingKey.getDownTime()); |
| 4674 | injectedEntries.push(std::move(injectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4675 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4676 | } |
| 4677 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4678 | case InputEventType::MOTION: { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4679 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4680 | const bool isPointerEvent = |
| 4681 | isFromSource(event->getSource(), AINPUT_SOURCE_CLASS_POINTER); |
| 4682 | // If a pointer event has no displayId specified, inject it to the default display. |
Linnan Li | 43654e7 | 2024-03-28 11:07:13 +0000 | [diff] [blame^] | 4683 | const int32_t displayId = isPointerEvent && (event->getDisplayId() == ADISPLAY_ID_NONE) |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4684 | ? ADISPLAY_ID_DEFAULT |
| 4685 | : event->getDisplayId(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4686 | int32_t flags = motionEvent.getFlags(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4687 | |
| 4688 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4689 | nsecs_t eventTime = motionEvent.getEventTime(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4690 | android::base::Timer t; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 4691 | mPolicy.interceptMotionBeforeQueueing(displayId, eventTime, /*byref*/ policyFlags); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4692 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4693 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
| 4694 | std::to_string(t.duration().count()).c_str()); |
| 4695 | } |
| 4696 | } |
| 4697 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4698 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4699 | flags |= AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4700 | } |
| 4701 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4702 | mLock.lock(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4703 | const nsecs_t* sampleEventTimes = motionEvent.getSampleEventTimes(); |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4704 | const size_t pointerCount = motionEvent.getPointerCount(); |
| 4705 | const std::vector<PointerProperties> |
| 4706 | pointerProperties(motionEvent.getPointerProperties(), |
| 4707 | motionEvent.getPointerProperties() + pointerCount); |
| 4708 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4709 | const PointerCoords* samplePointerCoords = motionEvent.getSamplePointerCoords(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4710 | std::unique_ptr<MotionEntry> injectedEntry = |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4711 | std::make_unique<MotionEntry>(motionEvent.getId(), injectionState, |
| 4712 | *sampleEventTimes, resolvedDeviceId, |
| 4713 | motionEvent.getSource(), displayId, policyFlags, |
| 4714 | motionEvent.getAction(), |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4715 | motionEvent.getActionButton(), flags, |
| 4716 | motionEvent.getMetaState(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4717 | motionEvent.getButtonState(), |
| 4718 | motionEvent.getClassification(), |
| 4719 | motionEvent.getEdgeFlags(), |
| 4720 | motionEvent.getXPrecision(), |
| 4721 | motionEvent.getYPrecision(), |
| 4722 | motionEvent.getRawXCursorPosition(), |
| 4723 | motionEvent.getRawYCursorPosition(), |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4724 | motionEvent.getDownTime(), pointerProperties, |
| 4725 | std::vector<PointerCoords>(samplePointerCoords, |
| 4726 | samplePointerCoords + |
| 4727 | pointerCount)); |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4728 | transformMotionEntryForInjectionLocked(*injectedEntry, motionEvent.getTransform()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4729 | injectedEntries.push(std::move(injectedEntry)); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4730 | for (size_t i = motionEvent.getHistorySize(); i > 0; i--) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4731 | sampleEventTimes += 1; |
Siarhei Vishniakou | 6773db6 | 2023-04-21 11:30:20 -0700 | [diff] [blame] | 4732 | samplePointerCoords += motionEvent.getPointerCount(); |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4733 | std::unique_ptr<MotionEntry> nextInjectedEntry = std::make_unique< |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4734 | MotionEntry>(motionEvent.getId(), injectionState, *sampleEventTimes, |
| 4735 | resolvedDeviceId, motionEvent.getSource(), displayId, |
| 4736 | policyFlags, motionEvent.getAction(), |
| 4737 | motionEvent.getActionButton(), flags, |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4738 | motionEvent.getMetaState(), motionEvent.getButtonState(), |
| 4739 | motionEvent.getClassification(), motionEvent.getEdgeFlags(), |
| 4740 | motionEvent.getXPrecision(), motionEvent.getYPrecision(), |
| 4741 | motionEvent.getRawXCursorPosition(), |
| 4742 | motionEvent.getRawYCursorPosition(), motionEvent.getDownTime(), |
| 4743 | pointerProperties, |
| 4744 | std::vector<PointerCoords>(samplePointerCoords, |
| 4745 | samplePointerCoords + |
| 4746 | pointerCount)); |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4747 | transformMotionEntryForInjectionLocked(*nextInjectedEntry, |
| 4748 | motionEvent.getTransform()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4749 | injectedEntries.push(std::move(nextInjectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4750 | } |
| 4751 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4752 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4753 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4754 | default: |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4755 | LOG(WARNING) << "Cannot inject " << ftl::enum_string(event->getType()) << " events"; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4756 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4757 | } |
| 4758 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4759 | bool needWake = false; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4760 | while (!injectedEntries.empty()) { |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 4761 | if (DEBUG_INJECTION) { |
Siarhei Vishniakou | 827d1ac | 2023-07-21 16:37:51 -0700 | [diff] [blame] | 4762 | LOG(INFO) << "Injecting " << injectedEntries.front()->getDescription(); |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 4763 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4764 | needWake |= enqueueInboundEventLocked(std::move(injectedEntries.front())); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4765 | injectedEntries.pop(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4766 | } |
| 4767 | |
| 4768 | mLock.unlock(); |
| 4769 | |
| 4770 | if (needWake) { |
| 4771 | mLooper->wake(); |
| 4772 | } |
| 4773 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4774 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4775 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4776 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4777 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4778 | if (syncMode == InputEventInjectionSync::NONE) { |
| 4779 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4780 | } else { |
| 4781 | for (;;) { |
| 4782 | injectionResult = injectionState->injectionResult; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4783 | if (injectionResult != InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4784 | break; |
| 4785 | } |
| 4786 | |
| 4787 | nsecs_t remainingTimeout = endTime - now(); |
| 4788 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4789 | if (DEBUG_INJECTION) { |
| 4790 | ALOGD("injectInputEvent - Timed out waiting for injection result " |
| 4791 | "to become available."); |
| 4792 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4793 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4794 | break; |
| 4795 | } |
| 4796 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4797 | mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4798 | } |
| 4799 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4800 | if (injectionResult == InputEventInjectionResult::SUCCEEDED && |
| 4801 | syncMode == InputEventInjectionSync::WAIT_FOR_FINISHED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4802 | while (injectionState->pendingForegroundDispatches != 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4803 | if (DEBUG_INJECTION) { |
| 4804 | ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", |
| 4805 | injectionState->pendingForegroundDispatches); |
| 4806 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4807 | nsecs_t remainingTimeout = endTime - now(); |
| 4808 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4809 | if (DEBUG_INJECTION) { |
| 4810 | ALOGD("injectInputEvent - Timed out waiting for pending foreground " |
| 4811 | "dispatches to finish."); |
| 4812 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4813 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4814 | break; |
| 4815 | } |
| 4816 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4817 | mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4818 | } |
| 4819 | } |
| 4820 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4821 | } // release lock |
| 4822 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4823 | if (DEBUG_INJECTION) { |
Siarhei Vishniakou | 827d1ac | 2023-07-21 16:37:51 -0700 | [diff] [blame] | 4824 | LOG(INFO) << "injectInputEvent - Finished with result " |
| 4825 | << ftl::enum_string(injectionResult); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4826 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4827 | |
| 4828 | return injectionResult; |
| 4829 | } |
| 4830 | |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4831 | std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4832 | std::array<uint8_t, 32> calculatedHmac; |
| 4833 | std::unique_ptr<VerifiedInputEvent> result; |
| 4834 | switch (event.getType()) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4835 | case InputEventType::KEY: { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4836 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event); |
| 4837 | VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent); |
| 4838 | result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4839 | calculatedHmac = sign(verifiedKeyEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4840 | break; |
| 4841 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4842 | case InputEventType::MOTION: { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4843 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event); |
| 4844 | VerifiedMotionEvent verifiedMotionEvent = |
| 4845 | verifiedMotionEventFromMotionEvent(motionEvent); |
| 4846 | result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4847 | calculatedHmac = sign(verifiedMotionEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4848 | break; |
| 4849 | } |
| 4850 | default: { |
| 4851 | ALOGE("Cannot verify events of type %" PRId32, event.getType()); |
| 4852 | return nullptr; |
| 4853 | } |
| 4854 | } |
| 4855 | if (calculatedHmac == INVALID_HMAC) { |
| 4856 | return nullptr; |
| 4857 | } |
tyiu | 1573a67 | 2023-02-21 22:38:32 +0000 | [diff] [blame] | 4858 | if (0 != CRYPTO_memcmp(calculatedHmac.data(), event.getHmac().data(), calculatedHmac.size())) { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4859 | return nullptr; |
| 4860 | } |
| 4861 | return result; |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4862 | } |
| 4863 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 4864 | void InputDispatcher::setInjectionResult(const EventEntry& entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4865 | InputEventInjectionResult injectionResult) { |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4866 | if (!entry.injectionState) { |
| 4867 | // Not an injected event. |
| 4868 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4869 | } |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4870 | |
| 4871 | InjectionState& injectionState = *entry.injectionState; |
| 4872 | if (DEBUG_INJECTION) { |
| 4873 | LOG(INFO) << "Setting input event injection result to " |
| 4874 | << ftl::enum_string(injectionResult); |
| 4875 | } |
| 4876 | |
| 4877 | if (injectionState.injectionIsAsync && !(entry.policyFlags & POLICY_FLAG_FILTERED)) { |
| 4878 | // Log the outcome since the injector did not wait for the injection result. |
| 4879 | switch (injectionResult) { |
| 4880 | case InputEventInjectionResult::SUCCEEDED: |
| 4881 | ALOGV("Asynchronous input event injection succeeded."); |
| 4882 | break; |
| 4883 | case InputEventInjectionResult::TARGET_MISMATCH: |
| 4884 | ALOGV("Asynchronous input event injection target mismatch."); |
| 4885 | break; |
| 4886 | case InputEventInjectionResult::FAILED: |
| 4887 | ALOGW("Asynchronous input event injection failed."); |
| 4888 | break; |
| 4889 | case InputEventInjectionResult::TIMED_OUT: |
| 4890 | ALOGW("Asynchronous input event injection timed out."); |
| 4891 | break; |
| 4892 | case InputEventInjectionResult::PENDING: |
| 4893 | ALOGE("Setting result to 'PENDING' for asynchronous injection"); |
| 4894 | break; |
| 4895 | } |
| 4896 | } |
| 4897 | |
| 4898 | injectionState.injectionResult = injectionResult; |
| 4899 | mInjectionResultAvailable.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4900 | } |
| 4901 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4902 | void InputDispatcher::transformMotionEntryForInjectionLocked( |
| 4903 | MotionEntry& entry, const ui::Transform& injectedTransform) const { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4904 | // Input injection works in the logical display coordinate space, but the input pipeline works |
| 4905 | // display space, so we need to transform the injected events accordingly. |
| 4906 | const auto it = mDisplayInfos.find(entry.displayId); |
| 4907 | if (it == mDisplayInfos.end()) return; |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4908 | const auto& transformToDisplay = it->second.transform.inverse() * injectedTransform; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4909 | |
Prabir Pradhan | d9a2ebe | 2022-07-20 19:25:13 +0000 | [diff] [blame] | 4910 | if (entry.xCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION && |
| 4911 | entry.yCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) { |
| 4912 | const vec2 cursor = |
| 4913 | MotionEvent::calculateTransformedXY(entry.source, transformToDisplay, |
| 4914 | {entry.xCursorPosition, entry.yCursorPosition}); |
| 4915 | entry.xCursorPosition = cursor.x; |
| 4916 | entry.yCursorPosition = cursor.y; |
| 4917 | } |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 4918 | for (uint32_t i = 0; i < entry.getPointerCount(); i++) { |
Prabir Pradhan | 8e6ce22 | 2022-02-24 09:08:54 -0800 | [diff] [blame] | 4919 | entry.pointerCoords[i] = |
| 4920 | MotionEvent::calculateTransformedCoords(entry.source, transformToDisplay, |
| 4921 | entry.pointerCoords[i]); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4922 | } |
| 4923 | } |
| 4924 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 4925 | void InputDispatcher::incrementPendingForegroundDispatches(const EventEntry& entry) { |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4926 | if (entry.injectionState) { |
| 4927 | entry.injectionState->pendingForegroundDispatches += 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4928 | } |
| 4929 | } |
| 4930 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 4931 | void InputDispatcher::decrementPendingForegroundDispatches(const EventEntry& entry) { |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4932 | if (entry.injectionState) { |
| 4933 | entry.injectionState->pendingForegroundDispatches -= 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4934 | |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame] | 4935 | if (entry.injectionState->pendingForegroundDispatches == 0) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4936 | mInjectionSyncFinished.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4937 | } |
| 4938 | } |
| 4939 | } |
| 4940 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4941 | const std::vector<sp<WindowInfoHandle>>& InputDispatcher::getWindowHandlesLocked( |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4942 | int32_t displayId) const { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4943 | static const std::vector<sp<WindowInfoHandle>> EMPTY_WINDOW_HANDLES; |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4944 | auto it = mWindowHandlesByDisplay.find(displayId); |
| 4945 | return it != mWindowHandlesByDisplay.end() ? it->second : EMPTY_WINDOW_HANDLES; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4946 | } |
| 4947 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4948 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 4949 | const sp<IBinder>& windowHandleToken, std::optional<int32_t> displayId) const { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4950 | if (windowHandleToken == nullptr) { |
| 4951 | return nullptr; |
| 4952 | } |
| 4953 | |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 4954 | if (!displayId) { |
| 4955 | // Look through all displays. |
| 4956 | for (auto& it : mWindowHandlesByDisplay) { |
| 4957 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 4958 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 4959 | if (windowHandle->getToken() == windowHandleToken) { |
| 4960 | return windowHandle; |
| 4961 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4962 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4963 | } |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4964 | return nullptr; |
| 4965 | } |
| 4966 | |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 4967 | // Only look through the requested display. |
| 4968 | for (const sp<WindowInfoHandle>& windowHandle : getWindowHandlesLocked(*displayId)) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4969 | if (windowHandle->getToken() == windowHandleToken) { |
| 4970 | return windowHandle; |
| 4971 | } |
| 4972 | } |
| 4973 | return nullptr; |
| 4974 | } |
| 4975 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4976 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
| 4977 | const sp<WindowInfoHandle>& windowHandle) const { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4978 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4979 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 4980 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4981 | if (handle->getId() == windowHandle->getId() && |
| 4982 | handle->getToken() == windowHandle->getToken()) { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4983 | if (windowHandle->getInfo()->displayId != it.first) { |
| 4984 | ALOGE("Found window %s in display %" PRId32 |
| 4985 | ", but it should belong to display %" PRId32, |
| 4986 | windowHandle->getName().c_str(), it.first, |
| 4987 | windowHandle->getInfo()->displayId); |
| 4988 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4989 | return handle; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4990 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4991 | } |
| 4992 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4993 | return nullptr; |
| 4994 | } |
| 4995 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4996 | sp<WindowInfoHandle> InputDispatcher::getFocusedWindowHandleLocked(int displayId) const { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4997 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(displayId); |
| 4998 | return getWindowHandleLocked(focusedToken, displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4999 | } |
| 5000 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5001 | ui::Transform InputDispatcher::getTransformLocked(int32_t displayId) const { |
| 5002 | auto displayInfoIt = mDisplayInfos.find(displayId); |
| 5003 | return displayInfoIt != mDisplayInfos.end() ? displayInfoIt->second.transform |
| 5004 | : kIdentityTransform; |
| 5005 | } |
| 5006 | |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5007 | bool InputDispatcher::canWindowReceiveMotionLocked(const sp<WindowInfoHandle>& window, |
| 5008 | const MotionEntry& motionEntry) const { |
| 5009 | const WindowInfo& info = *window->getInfo(); |
| 5010 | |
| 5011 | // Skip spy window targets that are not valid for targeted injection. |
| 5012 | if (const auto err = verifyTargetedInjection(window, motionEntry); err) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5013 | return false; |
| 5014 | } |
| 5015 | |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5016 | if (info.inputConfig.test(WindowInfo::InputConfig::PAUSE_DISPATCHING)) { |
| 5017 | ALOGI("Not sending touch event to %s because it is paused", window->getName().c_str()); |
| 5018 | return false; |
| 5019 | } |
| 5020 | |
| 5021 | if (info.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) { |
| 5022 | ALOGW("Not sending touch gesture to %s because it has config NO_INPUT_CHANNEL", |
| 5023 | window->getName().c_str()); |
| 5024 | return false; |
| 5025 | } |
| 5026 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 5027 | std::shared_ptr<Connection> connection = getConnectionLocked(window->getToken()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5028 | if (connection == nullptr) { |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5029 | ALOGW("Not sending touch to %s because there's no corresponding connection", |
| 5030 | window->getName().c_str()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5031 | return false; |
| 5032 | } |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5033 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5034 | if (!connection->responsive) { |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5035 | 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] | 5036 | return false; |
| 5037 | } |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5038 | |
| 5039 | // Drop events that can't be trusted due to occlusion |
| 5040 | const auto [x, y] = resolveTouchedPosition(motionEntry); |
| 5041 | TouchOcclusionInfo occlusionInfo = computeTouchOcclusionInfoLocked(window, x, y); |
| 5042 | if (!isTouchTrustedLocked(occlusionInfo)) { |
| 5043 | if (DEBUG_TOUCH_OCCLUSION) { |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 5044 | ALOGD("Stack of obscuring windows during untrusted touch (%.1f, %.1f):", x, y); |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5045 | for (const auto& log : occlusionInfo.debugInfo) { |
| 5046 | ALOGD("%s", log.c_str()); |
| 5047 | } |
| 5048 | } |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 5049 | ALOGW("Dropping untrusted touch event due to %s/%s", occlusionInfo.obscuringPackage.c_str(), |
| 5050 | occlusionInfo.obscuringUid.toString().c_str()); |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 5051 | return false; |
| 5052 | } |
| 5053 | |
| 5054 | // Drop touch events if requested by input feature |
| 5055 | if (shouldDropInput(motionEntry, window)) { |
| 5056 | return false; |
| 5057 | } |
| 5058 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5059 | return true; |
| 5060 | } |
| 5061 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5062 | std::shared_ptr<InputChannel> InputDispatcher::getInputChannelLocked( |
| 5063 | const sp<IBinder>& token) const { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5064 | auto connectionIt = mConnectionsByToken.find(token); |
| 5065 | if (connectionIt == mConnectionsByToken.end()) { |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 5066 | return nullptr; |
| 5067 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5068 | return connectionIt->second->inputChannel; |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 5069 | } |
| 5070 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5071 | void InputDispatcher::updateWindowHandlesForDisplayLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5072 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
| 5073 | if (windowInfoHandles.empty()) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5074 | // Remove all handles on a display if there are no windows left. |
| 5075 | mWindowHandlesByDisplay.erase(displayId); |
| 5076 | return; |
| 5077 | } |
| 5078 | |
| 5079 | // Since we compare the pointer of input window handles across window updates, we need |
| 5080 | // 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] | 5081 | const std::vector<sp<WindowInfoHandle>>& oldHandles = getWindowHandlesLocked(displayId); |
| 5082 | std::unordered_map<int32_t /*id*/, sp<WindowInfoHandle>> oldHandlesById; |
| 5083 | for (const sp<WindowInfoHandle>& handle : oldHandles) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5084 | oldHandlesById[handle->getId()] = handle; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5085 | } |
| 5086 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5087 | std::vector<sp<WindowInfoHandle>> newHandles; |
| 5088 | for (const sp<WindowInfoHandle>& handle : windowInfoHandles) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5089 | const WindowInfo* info = handle->getInfo(); |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 5090 | if (getInputChannelLocked(handle->getToken()) == nullptr) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5091 | const bool noInputChannel = |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 5092 | info->inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5093 | const bool canReceiveInput = |
| 5094 | !info->inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE) || |
| 5095 | !info->inputConfig.test(WindowInfo::InputConfig::NOT_FOCUSABLE); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5096 | if (canReceiveInput && !noInputChannel) { |
John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 5097 | ALOGV("Window handle %s has no registered input channel", |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5098 | handle->getName().c_str()); |
Robert Carr | 2984b7a | 2020-04-13 17:06:45 -0700 | [diff] [blame] | 5099 | continue; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5100 | } |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5101 | } |
| 5102 | |
| 5103 | if (info->displayId != displayId) { |
| 5104 | ALOGE("Window %s updated by wrong display %d, should belong to display %d", |
| 5105 | handle->getName().c_str(), displayId, info->displayId); |
| 5106 | continue; |
| 5107 | } |
| 5108 | |
Robert Carr | edd1360 | 2020-04-13 17:24:34 -0700 | [diff] [blame] | 5109 | if ((oldHandlesById.find(handle->getId()) != oldHandlesById.end()) && |
| 5110 | (oldHandlesById.at(handle->getId())->getToken() == handle->getToken())) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5111 | const sp<WindowInfoHandle>& oldHandle = oldHandlesById.at(handle->getId()); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5112 | oldHandle->updateFrom(handle); |
| 5113 | newHandles.push_back(oldHandle); |
| 5114 | } else { |
| 5115 | newHandles.push_back(handle); |
| 5116 | } |
| 5117 | } |
| 5118 | |
| 5119 | // Insert or replace |
| 5120 | mWindowHandlesByDisplay[displayId] = newHandles; |
| 5121 | } |
| 5122 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5123 | /** |
| 5124 | * Called from InputManagerService, update window handle list by displayId that can receive input. |
| 5125 | * A window handle contains information about InputChannel, Touch Region, Types, Focused,... |
| 5126 | * If set an empty list, remove all handles from the specific display. |
| 5127 | * For focused handle, check if need to change and send a cancel event to previous one. |
| 5128 | * For removed handle, check if need to send a cancel event if already in touch. |
| 5129 | */ |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5130 | void InputDispatcher::setInputWindowsLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5131 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5132 | if (DEBUG_FOCUS) { |
| 5133 | std::string windowList; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5134 | for (const sp<WindowInfoHandle>& iwh : windowInfoHandles) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5135 | windowList += iwh->getName() + " "; |
| 5136 | } |
Siarhei Vishniakou | 366fb5b | 2023-12-06 11:23:41 -0800 | [diff] [blame] | 5137 | LOG(INFO) << "setInputWindows displayId=" << displayId << " " << windowList; |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5138 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5139 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 5140 | // Check preconditions for new input windows |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5141 | for (const sp<WindowInfoHandle>& window : windowInfoHandles) { |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 5142 | const WindowInfo& info = *window->getInfo(); |
| 5143 | |
| 5144 | // 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] | 5145 | const bool noInputWindow = info.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5146 | if (noInputWindow && window->getToken() != nullptr) { |
| 5147 | ALOGE("%s has feature NO_INPUT_WINDOW, but a non-null token. Clearing", |
| 5148 | window->getName().c_str()); |
| 5149 | window->releaseChannel(); |
| 5150 | } |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 5151 | |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 5152 | // Ensure all spy windows are trusted overlays |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5153 | LOG_ALWAYS_FATAL_IF(info.isSpy() && |
| 5154 | !info.inputConfig.test( |
| 5155 | WindowInfo::InputConfig::TRUSTED_OVERLAY), |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 5156 | "%s has feature SPY, but is not a trusted overlay.", |
| 5157 | window->getName().c_str()); |
| 5158 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 5159 | // Ensure all stylus interceptors are trusted overlays |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5160 | LOG_ALWAYS_FATAL_IF(info.interceptsStylus() && |
| 5161 | !info.inputConfig.test( |
| 5162 | WindowInfo::InputConfig::TRUSTED_OVERLAY), |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 5163 | "%s has feature INTERCEPTS_STYLUS, but is not a trusted overlay.", |
| 5164 | window->getName().c_str()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5165 | } |
| 5166 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5167 | // Copy old handles for release if they are no longer present. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5168 | const std::vector<sp<WindowInfoHandle>> oldWindowHandles = getWindowHandlesLocked(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5169 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5170 | updateWindowHandlesForDisplayLocked(windowInfoHandles, displayId); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 5171 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5172 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5173 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5174 | std::optional<FocusResolver::FocusChanges> changes = |
| 5175 | mFocusResolver.setInputWindows(displayId, windowHandles); |
| 5176 | if (changes) { |
| 5177 | onFocusChangedLocked(*changes); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5178 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5179 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5180 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 5181 | mTouchStatesByDisplay.find(displayId); |
| 5182 | if (stateIt != mTouchStatesByDisplay.end()) { |
| 5183 | TouchState& state = stateIt->second; |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5184 | for (size_t i = 0; i < state.windows.size();) { |
| 5185 | TouchedWindow& touchedWindow = state.windows[i]; |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 5186 | if (getWindowHandleLocked(touchedWindow.windowHandle) == nullptr) { |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 5187 | LOG(INFO) << "Touched window was removed: " << touchedWindow.windowHandle->getName() |
| 5188 | << " in display %" << displayId; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5189 | std::shared_ptr<InputChannel> touchedInputChannel = |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5190 | getInputChannelLocked(touchedWindow.windowHandle->getToken()); |
| 5191 | if (touchedInputChannel != nullptr) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 5192 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5193 | "touched window was removed"); |
| 5194 | synthesizeCancelationEventsForInputChannelLocked(touchedInputChannel, options); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5195 | // Since we are about to drop the touch, cancel the events for the wallpaper as |
| 5196 | // well. |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5197 | if (touchedWindow.targetFlags.test(InputTarget::Flags::FOREGROUND) && |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5198 | touchedWindow.windowHandle->getInfo()->inputConfig.test( |
| 5199 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER)) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5200 | sp<WindowInfoHandle> wallpaper = state.getWallpaperWindow(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5201 | synthesizeCancelationEventsForWindowLocked(wallpaper, options); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5202 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5203 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5204 | state.windows.erase(state.windows.begin() + i); |
| 5205 | } else { |
| 5206 | ++i; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5207 | } |
| 5208 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5209 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5210 | // 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] | 5211 | // could just clear the state here. |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 5212 | if (mDragState && mDragState->dragWindow->getInfo()->displayId == displayId && |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5213 | std::find(windowHandles.begin(), windowHandles.end(), mDragState->dragWindow) == |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5214 | windowHandles.end()) { |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 5215 | ALOGI("Drag window went away: %s", mDragState->dragWindow->getName().c_str()); |
| 5216 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5217 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5218 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5219 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 5220 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5221 | // Release information for windows that are no longer present. |
| 5222 | // This ensures that unused input channels are released promptly. |
| 5223 | // Otherwise, they might stick around until the window handle is destroyed |
| 5224 | // which might not happen until the next GC. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5225 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 5226 | if (getWindowHandleLocked(oldWindowHandle) == nullptr) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5227 | if (DEBUG_FOCUS) { |
| 5228 | ALOGD("Window went away: %s", oldWindowHandle->getName().c_str()); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 5229 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5230 | oldWindowHandle->releaseChannel(); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 5231 | } |
chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 5232 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5233 | } |
| 5234 | |
| 5235 | void InputDispatcher::setFocusedApplication( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5236 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5237 | if (DEBUG_FOCUS) { |
| 5238 | ALOGD("setFocusedApplication displayId=%" PRId32 " %s", displayId, |
| 5239 | inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>"); |
| 5240 | } |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 5241 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5242 | std::scoped_lock _l(mLock); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 5243 | setFocusedApplicationLocked(displayId, inputApplicationHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5244 | } // release lock |
| 5245 | |
| 5246 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5247 | mLooper->wake(); |
| 5248 | } |
| 5249 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 5250 | void InputDispatcher::setFocusedApplicationLocked( |
| 5251 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
| 5252 | std::shared_ptr<InputApplicationHandle> oldFocusedApplicationHandle = |
| 5253 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 5254 | |
| 5255 | if (sharedPointersEqual(oldFocusedApplicationHandle, inputApplicationHandle)) { |
| 5256 | return; // This application is already focused. No need to wake up or change anything. |
| 5257 | } |
| 5258 | |
| 5259 | // Set the new application handle. |
| 5260 | if (inputApplicationHandle != nullptr) { |
| 5261 | mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle; |
| 5262 | } else { |
| 5263 | mFocusedApplicationHandlesByDisplay.erase(displayId); |
| 5264 | } |
| 5265 | |
| 5266 | // No matter what the old focused application was, stop waiting on it because it is |
| 5267 | // no longer focused. |
| 5268 | resetNoFocusedWindowTimeoutLocked(); |
| 5269 | } |
| 5270 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5271 | /** |
| 5272 | * Sets the focused display, which is responsible for receiving focus-dispatched input events where |
| 5273 | * the display not specified. |
| 5274 | * |
| 5275 | * We track any unreleased events for each window. If a window loses the ability to receive the |
| 5276 | * released event, we will send a cancel event to it. So when the focused display is changed, we |
| 5277 | * cancel all the unreleased display-unspecified events for the focused window on the old focused |
| 5278 | * display. The display-specified events won't be affected. |
| 5279 | */ |
| 5280 | void InputDispatcher::setFocusedDisplay(int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5281 | if (DEBUG_FOCUS) { |
| 5282 | ALOGD("setFocusedDisplay displayId=%" PRId32, displayId); |
| 5283 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5284 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5285 | std::scoped_lock _l(mLock); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5286 | |
| 5287 | if (mFocusedDisplayId != displayId) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5288 | sp<IBinder> oldFocusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5289 | mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5290 | if (oldFocusedWindowToken != nullptr) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5291 | std::shared_ptr<InputChannel> inputChannel = |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5292 | getInputChannelLocked(oldFocusedWindowToken); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5293 | if (inputChannel != nullptr) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5294 | CancelationOptions |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 5295 | options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5296 | "The display which contains this window no longer has focus."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5297 | options.displayId = ADISPLAY_ID_NONE; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5298 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
| 5299 | } |
| 5300 | } |
| 5301 | mFocusedDisplayId = displayId; |
| 5302 | |
Chris Ye | 3c2d6f5 | 2020-08-09 10:39:48 -0700 | [diff] [blame] | 5303 | // Find new focused window and validate |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5304 | sp<IBinder> newFocusedWindowToken = mFocusResolver.getFocusedWindowToken(displayId); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5305 | sendFocusChangedCommandLocked(oldFocusedWindowToken, newFocusedWindowToken); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5306 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5307 | if (newFocusedWindowToken == nullptr) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5308 | ALOGW("Focused display #%" PRId32 " does not have a focused window.", displayId); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5309 | if (mFocusResolver.hasFocusedWindowTokens()) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5310 | ALOGE("But another display has a focused window\n%s", |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5311 | mFocusResolver.dumpFocusedWindows().c_str()); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5312 | } |
| 5313 | } |
| 5314 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5315 | } // release lock |
| 5316 | |
| 5317 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5318 | mLooper->wake(); |
| 5319 | } |
| 5320 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5321 | void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5322 | if (DEBUG_FOCUS) { |
| 5323 | ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); |
| 5324 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5325 | |
| 5326 | bool changed; |
| 5327 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5328 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5329 | |
| 5330 | if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { |
| 5331 | if (mDispatchFrozen && !frozen) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5332 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5333 | } |
| 5334 | |
| 5335 | if (mDispatchEnabled && !enabled) { |
| 5336 | resetAndDropEverythingLocked("dispatcher is being disabled"); |
| 5337 | } |
| 5338 | |
| 5339 | mDispatchEnabled = enabled; |
| 5340 | mDispatchFrozen = frozen; |
| 5341 | changed = true; |
| 5342 | } else { |
| 5343 | changed = false; |
| 5344 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5345 | } // release lock |
| 5346 | |
| 5347 | if (changed) { |
| 5348 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5349 | mLooper->wake(); |
| 5350 | } |
| 5351 | } |
| 5352 | |
| 5353 | void InputDispatcher::setInputFilterEnabled(bool enabled) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5354 | if (DEBUG_FOCUS) { |
| 5355 | ALOGD("setInputFilterEnabled: enabled=%d", enabled); |
| 5356 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5357 | |
| 5358 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5359 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5360 | |
| 5361 | if (mInputFilterEnabled == enabled) { |
| 5362 | return; |
| 5363 | } |
| 5364 | |
| 5365 | mInputFilterEnabled = enabled; |
| 5366 | resetAndDropEverythingLocked("input filter is being enabled or disabled"); |
| 5367 | } // release lock |
| 5368 | |
| 5369 | // Wake up poll loop since there might be work to do to drop everything. |
| 5370 | mLooper->wake(); |
| 5371 | } |
| 5372 | |
Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5373 | bool InputDispatcher::setInTouchMode(bool inTouchMode, gui::Pid pid, gui::Uid uid, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 5374 | bool hasPermission, int32_t displayId) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5375 | bool needWake = false; |
| 5376 | { |
| 5377 | std::scoped_lock lock(mLock); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5378 | ALOGD_IF(DEBUG_TOUCH_MODE, |
Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5379 | "Request to change touch mode to %s (calling pid=%s, uid=%s, " |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5380 | "hasPermission=%s, target displayId=%d, mTouchModePerDisplay[displayId]=%s)", |
Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5381 | toString(inTouchMode), pid.toString().c_str(), uid.toString().c_str(), |
| 5382 | toString(hasPermission), displayId, |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5383 | mTouchModePerDisplay.count(displayId) == 0 |
| 5384 | ? "not set" |
| 5385 | : std::to_string(mTouchModePerDisplay[displayId]).c_str()); |
| 5386 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5387 | auto touchModeIt = mTouchModePerDisplay.find(displayId); |
| 5388 | if (touchModeIt != mTouchModePerDisplay.end() && touchModeIt->second == inTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5389 | return false; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5390 | } |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5391 | if (!hasPermission) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 5392 | if (!focusedWindowIsOwnedByLocked(pid, uid) && |
| 5393 | !recentWindowsAreOwnedByLocked(pid, uid)) { |
Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5394 | 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] | 5395 | "window nor none of the previously interacted window", |
Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5396 | pid.toString().c_str(), uid.toString().c_str()); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5397 | return false; |
| 5398 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5399 | } |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5400 | mTouchModePerDisplay[displayId] = inTouchMode; |
| 5401 | auto entry = std::make_unique<TouchModeEntry>(mIdGenerator.nextId(), now(), inTouchMode, |
| 5402 | displayId); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5403 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 5404 | } // release lock |
| 5405 | |
| 5406 | if (needWake) { |
| 5407 | mLooper->wake(); |
| 5408 | } |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5409 | return true; |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 5410 | } |
| 5411 | |
Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5412 | bool InputDispatcher::focusedWindowIsOwnedByLocked(gui::Pid pid, gui::Uid uid) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 5413 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
| 5414 | if (focusedToken == nullptr) { |
| 5415 | return false; |
| 5416 | } |
| 5417 | sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(focusedToken); |
| 5418 | return isWindowOwnedBy(windowHandle, pid, uid); |
| 5419 | } |
| 5420 | |
Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5421 | bool InputDispatcher::recentWindowsAreOwnedByLocked(gui::Pid pid, gui::Uid uid) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 5422 | return std::find_if(mInteractionConnectionTokens.begin(), mInteractionConnectionTokens.end(), |
| 5423 | [&](const sp<IBinder>& connectionToken) REQUIRES(mLock) { |
| 5424 | const sp<WindowInfoHandle> windowHandle = |
| 5425 | getWindowHandleLocked(connectionToken); |
| 5426 | return isWindowOwnedBy(windowHandle, pid, uid); |
| 5427 | }) != mInteractionConnectionTokens.end(); |
| 5428 | } |
| 5429 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 5430 | void InputDispatcher::setMaximumObscuringOpacityForTouch(float opacity) { |
| 5431 | if (opacity < 0 || opacity > 1) { |
| 5432 | LOG_ALWAYS_FATAL("Maximum obscuring opacity for touch should be >= 0 and <= 1"); |
| 5433 | return; |
| 5434 | } |
| 5435 | |
| 5436 | std::scoped_lock lock(mLock); |
| 5437 | mMaximumObscuringOpacityForTouch = opacity; |
| 5438 | } |
| 5439 | |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5440 | std::tuple<TouchState*, TouchedWindow*, int32_t /*displayId*/> |
| 5441 | InputDispatcher::findTouchStateWindowAndDisplayLocked(const sp<IBinder>& token) { |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5442 | for (auto& [displayId, state] : mTouchStatesByDisplay) { |
| 5443 | for (TouchedWindow& w : state.windows) { |
| 5444 | if (w.windowHandle->getToken() == token) { |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5445 | return std::make_tuple(&state, &w, displayId); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5446 | } |
| 5447 | } |
| 5448 | } |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5449 | return std::make_tuple(nullptr, nullptr, ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5450 | } |
| 5451 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5452 | bool InputDispatcher::transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken, |
| 5453 | bool isDragDrop) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 5454 | if (fromToken == toToken) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5455 | if (DEBUG_FOCUS) { |
| 5456 | ALOGD("Trivial transfer to same window."); |
| 5457 | } |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 5458 | return true; |
| 5459 | } |
| 5460 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5461 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5462 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5463 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5464 | // Find the target touch state and touched window by fromToken. |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5465 | auto [state, touchedWindow, displayId] = findTouchStateWindowAndDisplayLocked(fromToken); |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5466 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5467 | if (state == nullptr || touchedWindow == nullptr) { |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5468 | ALOGD("Touch transfer failed because from window is not being touched."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5469 | return false; |
| 5470 | } |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5471 | std::set<int32_t> deviceIds = touchedWindow->getTouchingDeviceIds(); |
| 5472 | if (deviceIds.size() != 1) { |
Siarhei Vishniakou | 827d1ac | 2023-07-21 16:37:51 -0700 | [diff] [blame] | 5473 | LOG(INFO) << "Can't transfer touch. Currently touching devices: " << dumpSet(deviceIds) |
| 5474 | << " for window: " << touchedWindow->dump(); |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5475 | return false; |
| 5476 | } |
| 5477 | const int32_t deviceId = *deviceIds.begin(); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5478 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5479 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(toToken, displayId); |
| 5480 | if (toWindowHandle == nullptr) { |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5481 | ALOGW("Cannot transfer touch because to window not found."); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5482 | return false; |
| 5483 | } |
| 5484 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5485 | if (DEBUG_FOCUS) { |
| 5486 | ALOGD("transferTouchFocus: fromWindowHandle=%s, toWindowHandle=%s", |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5487 | touchedWindow->windowHandle->getName().c_str(), |
| 5488 | toWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5489 | } |
| 5490 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5491 | // Erase old window. |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5492 | ftl::Flags<InputTarget::Flags> oldTargetFlags = touchedWindow->targetFlags; |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5493 | std::bitset<MAX_POINTER_ID + 1> pointerIds = touchedWindow->getTouchingPointers(deviceId); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5494 | sp<WindowInfoHandle> fromWindowHandle = touchedWindow->windowHandle; |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5495 | state->removeWindowByToken(fromToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5496 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5497 | // Add new window. |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 5498 | nsecs_t downTimeInTarget = now(); |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5499 | ftl::Flags<InputTarget::Flags> newTargetFlags = |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 5500 | oldTargetFlags & (InputTarget::Flags::SPLIT); |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 5501 | if (canReceiveForegroundTouches(*toWindowHandle->getInfo())) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5502 | newTargetFlags |= InputTarget::Flags::FOREGROUND; |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 5503 | } |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 5504 | state->addOrUpdateWindow(toWindowHandle, InputTarget::DispatchMode::AS_IS, newTargetFlags, |
| 5505 | deviceId, pointerIds, downTimeInTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5506 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5507 | // Store the dragging window. |
| 5508 | if (isDragDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 5509 | if (pointerIds.count() != 1) { |
| 5510 | ALOGW("The drag and drop cannot be started when there is no pointer or more than 1" |
| 5511 | " pointer on the window."); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 5512 | return false; |
| 5513 | } |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 5514 | // Track the pointer id for drag window and generate the drag state. |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 5515 | const size_t id = firstMarkedBit(pointerIds); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 5516 | mDragState = std::make_unique<DragState>(toWindowHandle, id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5517 | } |
| 5518 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5519 | // Synthesize cancel for old window and down for new window. |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 5520 | std::shared_ptr<Connection> fromConnection = getConnectionLocked(fromToken); |
| 5521 | std::shared_ptr<Connection> toConnection = getConnectionLocked(toToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5522 | if (fromConnection != nullptr && toConnection != nullptr) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5523 | fromConnection->inputState.mergePointerStateTo(toConnection->inputState); |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5524 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
| 5525 | "transferring touch from this window to another window"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5526 | synthesizeCancelationEventsForConnectionLocked(fromConnection, options); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5527 | synthesizePointerDownEventsForConnectionLocked(downTimeInTarget, toConnection, |
| 5528 | newTargetFlags); |
| 5529 | |
| 5530 | // Check if the wallpaper window should deliver the corresponding event. |
| 5531 | transferWallpaperTouch(oldTargetFlags, newTargetFlags, fromWindowHandle, toWindowHandle, |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5532 | *state, deviceId, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5533 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5534 | } // release lock |
| 5535 | |
| 5536 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5537 | mLooper->wake(); |
| 5538 | return true; |
| 5539 | } |
| 5540 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5541 | /** |
| 5542 | * Get the touched foreground window on the given display. |
| 5543 | * Return null if there are no windows touched on that display, or if more than one foreground |
| 5544 | * window is being touched. |
| 5545 | */ |
| 5546 | sp<WindowInfoHandle> InputDispatcher::findTouchedForegroundWindowLocked(int32_t displayId) const { |
| 5547 | auto stateIt = mTouchStatesByDisplay.find(displayId); |
| 5548 | if (stateIt == mTouchStatesByDisplay.end()) { |
| 5549 | ALOGI("No touch state on display %" PRId32, displayId); |
| 5550 | return nullptr; |
| 5551 | } |
| 5552 | |
| 5553 | const TouchState& state = stateIt->second; |
| 5554 | sp<WindowInfoHandle> touchedForegroundWindow; |
| 5555 | // If multiple foreground windows are touched, return nullptr |
| 5556 | for (const TouchedWindow& window : state.windows) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5557 | if (window.targetFlags.test(InputTarget::Flags::FOREGROUND)) { |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5558 | if (touchedForegroundWindow != nullptr) { |
| 5559 | ALOGI("Two or more foreground windows: %s and %s", |
| 5560 | touchedForegroundWindow->getName().c_str(), |
| 5561 | window.windowHandle->getName().c_str()); |
| 5562 | return nullptr; |
| 5563 | } |
| 5564 | touchedForegroundWindow = window.windowHandle; |
| 5565 | } |
| 5566 | } |
| 5567 | return touchedForegroundWindow; |
| 5568 | } |
| 5569 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5570 | // Binder call |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5571 | bool InputDispatcher::transferTouch(const sp<IBinder>& destChannelToken, int32_t displayId) { |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5572 | sp<IBinder> fromToken; |
| 5573 | { // acquire lock |
| 5574 | std::scoped_lock _l(mLock); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5575 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(destChannelToken, displayId); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5576 | if (toWindowHandle == nullptr) { |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5577 | ALOGW("Could not find window associated with token=%p on display %" PRId32, |
| 5578 | destChannelToken.get(), displayId); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5579 | return false; |
| 5580 | } |
| 5581 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5582 | sp<WindowInfoHandle> from = findTouchedForegroundWindowLocked(displayId); |
| 5583 | if (from == nullptr) { |
| 5584 | ALOGE("Could not find a source window in %s for %p", __func__, destChannelToken.get()); |
| 5585 | return false; |
| 5586 | } |
| 5587 | |
| 5588 | fromToken = from->getToken(); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5589 | } // release lock |
| 5590 | |
| 5591 | return transferTouchFocus(fromToken, destChannelToken); |
| 5592 | } |
| 5593 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5594 | void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5595 | if (DEBUG_FOCUS) { |
| 5596 | ALOGD("Resetting and dropping all events (%s).", reason); |
| 5597 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5598 | |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 5599 | CancelationOptions options(CancelationOptions::Mode::CANCEL_ALL_EVENTS, reason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5600 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 5601 | |
| 5602 | resetKeyRepeatLocked(); |
| 5603 | releasePendingEventLocked(); |
| 5604 | drainInboundQueueLocked(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5605 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5606 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5607 | mAnrTracker.clear(); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5608 | mTouchStatesByDisplay.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5609 | } |
| 5610 | |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 5611 | void InputDispatcher::logDispatchStateLocked() const { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5612 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5613 | dumpDispatchStateLocked(dump); |
| 5614 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5615 | std::istringstream stream(dump); |
| 5616 | std::string line; |
| 5617 | |
| 5618 | while (std::getline(stream, line, '\n')) { |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 5619 | ALOGI("%s", line.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5620 | } |
| 5621 | } |
| 5622 | |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 5623 | std::string InputDispatcher::dumpPointerCaptureStateLocked() const { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5624 | std::string dump; |
| 5625 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5626 | dump += StringPrintf(INDENT "Pointer Capture Requested: %s\n", |
| 5627 | toString(mCurrentPointerCaptureRequest.enable)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5628 | |
| 5629 | std::string windowName = "None"; |
| 5630 | if (mWindowTokenWithPointerCapture) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5631 | const sp<WindowInfoHandle> captureWindowHandle = |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5632 | getWindowHandleLocked(mWindowTokenWithPointerCapture); |
| 5633 | windowName = captureWindowHandle ? captureWindowHandle->getName().c_str() |
| 5634 | : "token has capture without window"; |
| 5635 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5636 | 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] | 5637 | |
| 5638 | return dump; |
| 5639 | } |
| 5640 | |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 5641 | void InputDispatcher::dumpDispatchStateLocked(std::string& dump) const { |
Siarhei Vishniakou | 043a3ec | 2019-05-01 11:30:46 -0700 | [diff] [blame] | 5642 | dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled)); |
| 5643 | dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen)); |
| 5644 | dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5645 | dump += StringPrintf(INDENT "FocusedDisplayId: %" PRId32 "\n", mFocusedDisplayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5646 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5647 | if (!mFocusedApplicationHandlesByDisplay.empty()) { |
| 5648 | dump += StringPrintf(INDENT "FocusedApplications:\n"); |
| 5649 | for (auto& it : mFocusedApplicationHandlesByDisplay) { |
| 5650 | const int32_t displayId = it.first; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5651 | const std::shared_ptr<InputApplicationHandle>& applicationHandle = it.second; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5652 | const std::chrono::duration timeout = |
| 5653 | applicationHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5654 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5655 | ", name='%s', dispatchingTimeout=%" PRId64 "ms\n", |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5656 | displayId, applicationHandle->getName().c_str(), millis(timeout)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5657 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5658 | } else { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5659 | dump += StringPrintf(INDENT "FocusedApplications: <none>\n"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5660 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5661 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5662 | dump += mFocusResolver.dump(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5663 | dump += dumpPointerCaptureStateLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5664 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5665 | if (!mTouchStatesByDisplay.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5666 | dump += StringPrintf(INDENT "TouchStatesByDisplay:\n"); |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5667 | for (const auto& [displayId, state] : mTouchStatesByDisplay) { |
Siarhei Vishniakou | 6e1e987 | 2022-11-08 17:51:35 -0800 | [diff] [blame] | 5668 | std::string touchStateDump = addLinePrefix(state.dump(), INDENT2); |
| 5669 | dump += INDENT2 + std::to_string(displayId) + " : " + touchStateDump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5670 | } |
| 5671 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5672 | dump += INDENT "TouchStates: <no displays touched>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5673 | } |
| 5674 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5675 | if (mDragState) { |
| 5676 | dump += StringPrintf(INDENT "DragState:\n"); |
| 5677 | mDragState->dump(dump, INDENT2); |
| 5678 | } |
| 5679 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5680 | if (!mWindowHandlesByDisplay.empty()) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5681 | for (const auto& [displayId, windowHandles] : mWindowHandlesByDisplay) { |
| 5682 | dump += StringPrintf(INDENT "Display: %" PRId32 "\n", displayId); |
| 5683 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
| 5684 | const auto& displayInfo = it->second; |
| 5685 | dump += StringPrintf(INDENT2 "logicalSize=%dx%d\n", displayInfo.logicalWidth, |
| 5686 | displayInfo.logicalHeight); |
| 5687 | displayInfo.transform.dump(dump, "transform", INDENT4); |
| 5688 | } else { |
| 5689 | dump += INDENT2 "No DisplayInfo found!\n"; |
| 5690 | } |
| 5691 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5692 | if (!windowHandles.empty()) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5693 | dump += INDENT2 "Windows:\n"; |
| 5694 | for (size_t i = 0; i < windowHandles.size(); i++) { |
Siarhei Vishniakou | 366fb5b | 2023-12-06 11:23:41 -0800 | [diff] [blame] | 5695 | dump += StringPrintf(INDENT3 "%zu: %s", i, |
| 5696 | streamableToString(*windowHandles[i]).c_str()); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5697 | } |
| 5698 | } else { |
| 5699 | dump += INDENT2 "Windows: <none>\n"; |
| 5700 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5701 | } |
| 5702 | } else { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5703 | dump += INDENT "Displays: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5704 | } |
| 5705 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5706 | if (!mGlobalMonitorsByDisplay.empty()) { |
| 5707 | for (const auto& [displayId, monitors] : mGlobalMonitorsByDisplay) { |
| 5708 | dump += StringPrintf(INDENT "Global monitors on display %d:\n", displayId); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5709 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5710 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5711 | } else { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5712 | dump += INDENT "Global Monitors: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5713 | } |
| 5714 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5715 | const nsecs_t currentTime = now(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5716 | |
| 5717 | // Dump recently dispatched or dropped events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5718 | if (!mRecentQueue.empty()) { |
| 5719 | dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size()); |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 5720 | for (const std::shared_ptr<const EventEntry>& entry : mRecentQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5721 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5722 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5723 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5724 | } |
| 5725 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5726 | dump += INDENT "RecentQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5727 | } |
| 5728 | |
| 5729 | // Dump event currently being dispatched. |
| 5730 | if (mPendingEvent) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5731 | dump += INDENT "PendingEvent:\n"; |
| 5732 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5733 | dump += mPendingEvent->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5734 | dump += StringPrintf(", age=%" PRId64 "ms\n", |
| 5735 | ns2ms(currentTime - mPendingEvent->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5736 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5737 | dump += INDENT "PendingEvent: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5738 | } |
| 5739 | |
| 5740 | // Dump inbound events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5741 | if (!mInboundQueue.empty()) { |
| 5742 | dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size()); |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 5743 | for (const std::shared_ptr<const EventEntry>& entry : mInboundQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5744 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5745 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5746 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5747 | } |
| 5748 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5749 | dump += INDENT "InboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5750 | } |
| 5751 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5752 | if (!mCommandQueue.empty()) { |
| 5753 | dump += StringPrintf(INDENT "CommandQueue: size=%zu\n", mCommandQueue.size()); |
| 5754 | } else { |
| 5755 | dump += INDENT "CommandQueue: <empty>\n"; |
| 5756 | } |
| 5757 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5758 | if (!mConnectionsByToken.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5759 | dump += INDENT "Connections:\n"; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5760 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5761 | dump += StringPrintf(INDENT2 "%i: channelName='%s', windowName='%s', " |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5762 | "status=%s, monitor=%s, responsive=%s\n", |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5763 | connection->inputChannel->getFd().get(), |
| 5764 | connection->getInputChannelName().c_str(), |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5765 | connection->getWindowName().c_str(), |
| 5766 | ftl::enum_string(connection->status).c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5767 | toString(connection->monitor), toString(connection->responsive)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5768 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5769 | if (!connection->outboundQueue.empty()) { |
| 5770 | dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n", |
| 5771 | connection->outboundQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5772 | dump += dumpQueue(connection->outboundQueue, currentTime); |
| 5773 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5774 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5775 | dump += INDENT3 "OutboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5776 | } |
| 5777 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5778 | if (!connection->waitQueue.empty()) { |
| 5779 | dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n", |
| 5780 | connection->waitQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5781 | dump += dumpQueue(connection->waitQueue, currentTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5782 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5783 | dump += INDENT3 "WaitQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5784 | } |
Siarhei Vishniakou | 366fb5b | 2023-12-06 11:23:41 -0800 | [diff] [blame] | 5785 | std::string inputStateDump = streamableToString(connection->inputState); |
| 5786 | if (!inputStateDump.empty()) { |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 5787 | dump += INDENT3 "InputState: "; |
Siarhei Vishniakou | 366fb5b | 2023-12-06 11:23:41 -0800 | [diff] [blame] | 5788 | dump += inputStateDump + "\n"; |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 5789 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5790 | } |
| 5791 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5792 | dump += INDENT "Connections: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5793 | } |
| 5794 | |
Siarhei Vishniakou | 6520a58 | 2023-10-27 21:53:45 -0700 | [diff] [blame] | 5795 | dump += "input_flags::remove_app_switch_drops() = "; |
Siarhei Vishniakou | 17f1228 | 2023-11-02 13:40:29 -0700 | [diff] [blame] | 5796 | dump += toString(REMOVE_APP_SWITCH_DROPS); |
Siarhei Vishniakou | 6520a58 | 2023-10-27 21:53:45 -0700 | [diff] [blame] | 5797 | dump += "\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5798 | if (isAppSwitchPendingLocked()) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5799 | dump += StringPrintf(INDENT "AppSwitch: pending, due in %" PRId64 "ms\n", |
| 5800 | ns2ms(mAppSwitchDueTime - now())); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5801 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5802 | dump += INDENT "AppSwitch: not pending\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5803 | } |
| 5804 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5805 | if (!mTouchModePerDisplay.empty()) { |
| 5806 | dump += INDENT "TouchModePerDisplay:\n"; |
| 5807 | for (const auto& [displayId, touchMode] : mTouchModePerDisplay) { |
| 5808 | dump += StringPrintf(INDENT2 "Display: %" PRId32 " TouchMode: %s\n", displayId, |
| 5809 | std::to_string(touchMode).c_str()); |
| 5810 | } |
| 5811 | } else { |
| 5812 | dump += INDENT "TouchModePerDisplay: <none>\n"; |
| 5813 | } |
| 5814 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5815 | dump += INDENT "Configuration:\n"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5816 | dump += StringPrintf(INDENT2 "KeyRepeatDelay: %" PRId64 "ms\n", ns2ms(mConfig.keyRepeatDelay)); |
| 5817 | dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n", |
| 5818 | ns2ms(mConfig.keyRepeatTimeout)); |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 5819 | dump += mLatencyTracker.dump(INDENT2); |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 5820 | dump += mLatencyAggregator.dump(INDENT2); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5821 | } |
| 5822 | |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 5823 | void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) const { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5824 | const size_t numMonitors = monitors.size(); |
| 5825 | for (size_t i = 0; i < numMonitors; i++) { |
| 5826 | const Monitor& monitor = monitors[i]; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5827 | const std::shared_ptr<InputChannel>& channel = monitor.inputChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5828 | dump += StringPrintf(INDENT2 "%zu: '%s', ", i, channel->getName().c_str()); |
| 5829 | dump += "\n"; |
| 5830 | } |
| 5831 | } |
| 5832 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5833 | class LooperEventCallback : public LooperCallback { |
| 5834 | public: |
| 5835 | LooperEventCallback(std::function<int(int events)> callback) : mCallback(callback) {} |
| 5836 | int handleEvent(int /*fd*/, int events, void* /*data*/) override { return mCallback(events); } |
| 5837 | |
| 5838 | private: |
| 5839 | std::function<int(int events)> mCallback; |
| 5840 | }; |
| 5841 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5842 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputChannel(const std::string& name) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5843 | if (DEBUG_CHANNEL_CREATION) { |
| 5844 | ALOGD("channel '%s' ~ createInputChannel", name.c_str()); |
| 5845 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5846 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5847 | std::unique_ptr<InputChannel> serverChannel; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5848 | std::unique_ptr<InputChannel> clientChannel; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5849 | status_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5850 | |
| 5851 | if (result) { |
| 5852 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5853 | } |
| 5854 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5855 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5856 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5857 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Tomasz Wasilczyk | 3202460 | 2023-11-16 10:17:54 -0800 | [diff] [blame] | 5858 | auto&& fd = serverChannel->getFd(); |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 5859 | std::shared_ptr<Connection> connection = |
| 5860 | std::make_shared<Connection>(std::move(serverChannel), /*monitor=*/false, |
| 5861 | mIdGenerator); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5862 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5863 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5864 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5865 | } |
| 5866 | mConnectionsByToken.emplace(token, connection); |
| 5867 | |
| 5868 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5869 | this, std::placeholders::_1, token); |
| 5870 | |
Tomasz Wasilczyk | 3202460 | 2023-11-16 10:17:54 -0800 | [diff] [blame] | 5871 | mLooper->addFd(fd.get(), 0, ALOOPER_EVENT_INPUT, sp<LooperEventCallback>::make(callback), |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5872 | nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5873 | } // release lock |
| 5874 | |
| 5875 | // Wake the looper because some connections have changed. |
| 5876 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5877 | return clientChannel; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5878 | } |
| 5879 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5880 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputMonitor(int32_t displayId, |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5881 | const std::string& name, |
Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5882 | gui::Pid pid) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5883 | std::shared_ptr<InputChannel> serverChannel; |
| 5884 | std::unique_ptr<InputChannel> clientChannel; |
| 5885 | status_t result = openInputChannelPair(name, serverChannel, clientChannel); |
| 5886 | if (result) { |
| 5887 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5888 | } |
| 5889 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5890 | { // acquire lock |
| 5891 | std::scoped_lock _l(mLock); |
| 5892 | |
| 5893 | if (displayId < 0) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5894 | return base::Error(BAD_VALUE) << "Attempted to create input monitor with name " << name |
| 5895 | << " without a specified display."; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5896 | } |
| 5897 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 5898 | std::shared_ptr<Connection> connection = |
| 5899 | std::make_shared<Connection>(serverChannel, /*monitor=*/true, mIdGenerator); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5900 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Tomasz Wasilczyk | 3202460 | 2023-11-16 10:17:54 -0800 | [diff] [blame] | 5901 | auto&& fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5902 | |
| 5903 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5904 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5905 | } |
| 5906 | mConnectionsByToken.emplace(token, connection); |
| 5907 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5908 | this, std::placeholders::_1, token); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5909 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5910 | mGlobalMonitorsByDisplay[displayId].emplace_back(serverChannel, pid); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5911 | |
Tomasz Wasilczyk | 3202460 | 2023-11-16 10:17:54 -0800 | [diff] [blame] | 5912 | mLooper->addFd(fd.get(), 0, ALOOPER_EVENT_INPUT, sp<LooperEventCallback>::make(callback), |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5913 | nullptr); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5914 | } |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5915 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5916 | // Wake the looper because some connections have changed. |
| 5917 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5918 | return clientChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5919 | } |
| 5920 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5921 | status_t InputDispatcher::removeInputChannel(const sp<IBinder>& connectionToken) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5922 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5923 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5924 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5925 | status_t status = removeInputChannelLocked(connectionToken, /*notify=*/false); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5926 | if (status) { |
| 5927 | return status; |
| 5928 | } |
| 5929 | } // release lock |
| 5930 | |
| 5931 | // Wake the poll loop because removing the connection may have changed the current |
| 5932 | // synchronization state. |
| 5933 | mLooper->wake(); |
| 5934 | return OK; |
| 5935 | } |
| 5936 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5937 | status_t InputDispatcher::removeInputChannelLocked(const sp<IBinder>& connectionToken, |
| 5938 | bool notify) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 5939 | std::shared_ptr<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5940 | if (connection == nullptr) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5941 | // 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] | 5942 | return BAD_VALUE; |
| 5943 | } |
| 5944 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5945 | removeConnectionLocked(connection); |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 5946 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5947 | if (connection->monitor) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5948 | removeMonitorChannelLocked(connectionToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5949 | } |
| 5950 | |
Tomasz Wasilczyk | 3202460 | 2023-11-16 10:17:54 -0800 | [diff] [blame] | 5951 | mLooper->removeFd(connection->inputChannel->getFd().get()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5952 | |
| 5953 | nsecs_t currentTime = now(); |
| 5954 | abortBrokenDispatchCycleLocked(currentTime, connection, notify); |
| 5955 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5956 | connection->status = Connection::Status::ZOMBIE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5957 | return OK; |
| 5958 | } |
| 5959 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5960 | void InputDispatcher::removeMonitorChannelLocked(const sp<IBinder>& connectionToken) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5961 | for (auto it = mGlobalMonitorsByDisplay.begin(); it != mGlobalMonitorsByDisplay.end();) { |
| 5962 | auto& [displayId, monitors] = *it; |
| 5963 | std::erase_if(monitors, [connectionToken](const Monitor& monitor) { |
| 5964 | return monitor.inputChannel->getConnectionToken() == connectionToken; |
| 5965 | }); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5966 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5967 | if (monitors.empty()) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5968 | it = mGlobalMonitorsByDisplay.erase(it); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5969 | } else { |
| 5970 | ++it; |
| 5971 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5972 | } |
| 5973 | } |
| 5974 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5975 | status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5976 | std::scoped_lock _l(mLock); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 5977 | return pilferPointersLocked(token); |
| 5978 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5979 | |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 5980 | status_t InputDispatcher::pilferPointersLocked(const sp<IBinder>& token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5981 | const std::shared_ptr<InputChannel> requestingChannel = getInputChannelLocked(token); |
| 5982 | if (!requestingChannel) { |
Siarhei Vishniakou | 8384e0d | 2023-09-18 18:48:27 -0700 | [diff] [blame] | 5983 | LOG(WARNING) |
| 5984 | << "Attempted to pilfer pointers from an un-registered channel or invalid token"; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5985 | return BAD_VALUE; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5986 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5987 | |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5988 | auto [statePtr, windowPtr, displayId] = findTouchStateWindowAndDisplayLocked(token); |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5989 | if (statePtr == nullptr || windowPtr == nullptr) { |
Siarhei Vishniakou | 8384e0d | 2023-09-18 18:48:27 -0700 | [diff] [blame] | 5990 | LOG(WARNING) |
| 5991 | << "Attempted to pilfer points from a channel without any on-going pointer streams." |
| 5992 | " Ignoring."; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5993 | return BAD_VALUE; |
| 5994 | } |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5995 | std::set<int32_t> deviceIds = windowPtr->getTouchingDeviceIds(); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 5996 | if (deviceIds.empty()) { |
| 5997 | LOG(WARNING) << "Can't pilfer: no touching devices in window: " << windowPtr->dump(); |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 5998 | return BAD_VALUE; |
| 5999 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6000 | |
Siarhei Vishniakou | 8384e0d | 2023-09-18 18:48:27 -0700 | [diff] [blame] | 6001 | for (const DeviceId deviceId : deviceIds) { |
| 6002 | TouchState& state = *statePtr; |
| 6003 | TouchedWindow& window = *windowPtr; |
| 6004 | // Send cancel events to all the input channels we're stealing from. |
| 6005 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
| 6006 | "input channel stole pointer stream"); |
| 6007 | options.deviceId = deviceId; |
| 6008 | options.displayId = displayId; |
| 6009 | std::bitset<MAX_POINTER_ID + 1> pointerIds = window.getTouchingPointers(deviceId); |
| 6010 | options.pointerIds = pointerIds; |
| 6011 | std::string canceledWindows; |
| 6012 | for (const TouchedWindow& w : state.windows) { |
| 6013 | const std::shared_ptr<InputChannel> channel = |
| 6014 | getInputChannelLocked(w.windowHandle->getToken()); |
| 6015 | if (channel != nullptr && channel->getConnectionToken() != token) { |
| 6016 | synthesizeCancelationEventsForInputChannelLocked(channel, options); |
| 6017 | canceledWindows += canceledWindows.empty() ? "[" : ", "; |
| 6018 | canceledWindows += channel->getName(); |
| 6019 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6020 | } |
Siarhei Vishniakou | 8384e0d | 2023-09-18 18:48:27 -0700 | [diff] [blame] | 6021 | canceledWindows += canceledWindows.empty() ? "[]" : "]"; |
| 6022 | LOG(INFO) << "Channel " << requestingChannel->getName() |
| 6023 | << " is stealing input gesture for device " << deviceId << " from " |
| 6024 | << canceledWindows; |
| 6025 | |
| 6026 | // Prevent the gesture from being sent to any other windows. |
| 6027 | // This only blocks relevant pointers to be sent to other windows |
| 6028 | window.addPilferingPointers(deviceId, pointerIds); |
| 6029 | |
| 6030 | state.cancelPointersForWindowsExcept(deviceId, pointerIds, token); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6031 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6032 | return OK; |
| 6033 | } |
| 6034 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6035 | void InputDispatcher::requestPointerCapture(const sp<IBinder>& windowToken, bool enabled) { |
| 6036 | { // acquire lock |
| 6037 | std::scoped_lock _l(mLock); |
| 6038 | if (DEBUG_FOCUS) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 6039 | const sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(windowToken); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6040 | ALOGI("Request to %s Pointer Capture from: %s.", enabled ? "enable" : "disable", |
| 6041 | windowHandle != nullptr ? windowHandle->getName().c_str() |
| 6042 | : "token without window"); |
| 6043 | } |
| 6044 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6045 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6046 | if (focusedToken != windowToken) { |
| 6047 | ALOGW("Ignoring request to %s Pointer Capture: window does not have focus.", |
| 6048 | enabled ? "enable" : "disable"); |
| 6049 | return; |
| 6050 | } |
| 6051 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6052 | if (enabled == mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6053 | ALOGW("Ignoring request to %s Pointer Capture: " |
| 6054 | "window has %s requested pointer capture.", |
| 6055 | enabled ? "enable" : "disable", enabled ? "already" : "not"); |
| 6056 | return; |
| 6057 | } |
| 6058 | |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 6059 | if (enabled) { |
| 6060 | if (std::find(mIneligibleDisplaysForPointerCapture.begin(), |
| 6061 | mIneligibleDisplaysForPointerCapture.end(), |
| 6062 | mFocusedDisplayId) != mIneligibleDisplaysForPointerCapture.end()) { |
| 6063 | ALOGW("Ignoring request to enable Pointer Capture: display is not eligible"); |
| 6064 | return; |
| 6065 | } |
| 6066 | } |
| 6067 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6068 | setPointerCaptureLocked(enabled); |
| 6069 | } // release lock |
| 6070 | |
| 6071 | // Wake the thread to process command entries. |
| 6072 | mLooper->wake(); |
| 6073 | } |
| 6074 | |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 6075 | void InputDispatcher::setDisplayEligibilityForPointerCapture(int32_t displayId, bool isEligible) { |
| 6076 | { // acquire lock |
| 6077 | std::scoped_lock _l(mLock); |
| 6078 | std::erase(mIneligibleDisplaysForPointerCapture, displayId); |
| 6079 | if (!isEligible) { |
| 6080 | mIneligibleDisplaysForPointerCapture.push_back(displayId); |
| 6081 | } |
| 6082 | } // release lock |
| 6083 | } |
| 6084 | |
Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6085 | std::optional<gui::Pid> InputDispatcher::findMonitorPidByTokenLocked(const sp<IBinder>& token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6086 | for (const auto& [_, monitors] : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6087 | for (const Monitor& monitor : monitors) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 6088 | if (monitor.inputChannel->getConnectionToken() == token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6089 | return monitor.pid; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 6090 | } |
| 6091 | } |
| 6092 | } |
| 6093 | return std::nullopt; |
| 6094 | } |
| 6095 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6096 | std::shared_ptr<Connection> InputDispatcher::getConnectionLocked( |
| 6097 | const sp<IBinder>& inputConnectionToken) const { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 6098 | if (inputConnectionToken == nullptr) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 6099 | return nullptr; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 6100 | } |
| 6101 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 6102 | for (const auto& [token, connection] : mConnectionsByToken) { |
| 6103 | if (token == inputConnectionToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 6104 | return connection; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6105 | } |
| 6106 | } |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 6107 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 6108 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6109 | } |
| 6110 | |
Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 6111 | std::string InputDispatcher::getConnectionNameLocked(const sp<IBinder>& connectionToken) const { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6112 | std::shared_ptr<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 6113 | if (connection == nullptr) { |
| 6114 | return "<nullptr>"; |
| 6115 | } |
| 6116 | return connection->getInputChannelName(); |
| 6117 | } |
| 6118 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6119 | void InputDispatcher::removeConnectionLocked(const std::shared_ptr<Connection>& connection) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6120 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 6121 | mConnectionsByToken.erase(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6122 | } |
| 6123 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6124 | void InputDispatcher::doDispatchCycleFinishedCommand(nsecs_t finishTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6125 | const std::shared_ptr<Connection>& connection, |
| 6126 | uint32_t seq, bool handled, |
| 6127 | nsecs_t consumeTime) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6128 | // Handle post-event policy actions. |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 6129 | std::unique_ptr<const KeyEntry> fallbackKeyEntry; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6130 | |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6131 | { // Start critical section |
| 6132 | auto dispatchEntryIt = |
| 6133 | std::find_if(connection->waitQueue.begin(), connection->waitQueue.end(), |
| 6134 | [seq](auto& e) { return e->seq == seq; }); |
| 6135 | if (dispatchEntryIt == connection->waitQueue.end()) { |
| 6136 | return; |
| 6137 | } |
| 6138 | |
| 6139 | DispatchEntry& dispatchEntry = **dispatchEntryIt; |
| 6140 | |
| 6141 | const nsecs_t eventDuration = finishTime - dispatchEntry.deliveryTime; |
| 6142 | if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) { |
| 6143 | ALOGI("%s spent %" PRId64 "ms processing %s", connection->getWindowName().c_str(), |
| 6144 | ns2ms(eventDuration), dispatchEntry.eventEntry->getDescription().c_str()); |
| 6145 | } |
| 6146 | if (shouldReportFinishedEvent(dispatchEntry, *connection)) { |
| 6147 | mLatencyTracker.trackFinishedEvent(dispatchEntry.eventEntry->id, |
| 6148 | connection->inputChannel->getConnectionToken(), |
| 6149 | dispatchEntry.deliveryTime, consumeTime, finishTime); |
| 6150 | } |
| 6151 | |
| 6152 | if (dispatchEntry.eventEntry->type == EventEntry::Type::KEY) { |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6153 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(*(dispatchEntry.eventEntry)); |
| 6154 | fallbackKeyEntry = |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6155 | afterKeyEventLockedInterruptable(connection, dispatchEntry, keyEntry, handled); |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6156 | } |
| 6157 | } // End critical section: The -LockedInterruptable methods may have released the lock. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6158 | |
| 6159 | // Dequeue the event and start the next cycle. |
| 6160 | // Because the lock might have been released, it is possible that the |
| 6161 | // contents of the wait queue to have been drained, so we need to double-check |
| 6162 | // a few things. |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6163 | auto entryIt = std::find_if(connection->waitQueue.begin(), connection->waitQueue.end(), |
| 6164 | [seq](auto& e) { return e->seq == seq; }); |
| 6165 | if (entryIt != connection->waitQueue.end()) { |
| 6166 | std::unique_ptr<DispatchEntry> dispatchEntry = std::move(*entryIt); |
| 6167 | connection->waitQueue.erase(entryIt); |
| 6168 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6169 | const sp<IBinder>& connectionToken = connection->inputChannel->getConnectionToken(); |
| 6170 | mAnrTracker.erase(dispatchEntry->timeoutTime, connectionToken); |
| 6171 | if (!connection->responsive) { |
| 6172 | connection->responsive = isConnectionResponsive(*connection); |
| 6173 | if (connection->responsive) { |
| 6174 | // The connection was unresponsive, and now it's responsive. |
| 6175 | processConnectionResponsiveLocked(*connection); |
| 6176 | } |
| 6177 | } |
| 6178 | traceWaitQueueLength(*connection); |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6179 | if (fallbackKeyEntry && connection->status == Connection::Status::NORMAL) { |
| 6180 | const InputTarget target{.inputChannel = connection->inputChannel, |
| 6181 | .flags = dispatchEntry->targetFlags}; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 6182 | enqueueDispatchEntryLocked(connection, std::move(fallbackKeyEntry), target); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6183 | } |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6184 | releaseDispatchEntry(std::move(dispatchEntry)); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6185 | } |
| 6186 | |
| 6187 | // Start the next dispatch cycle for this connection. |
| 6188 | startDispatchCycleLocked(now(), connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6189 | } |
| 6190 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6191 | void InputDispatcher::sendFocusChangedCommandLocked(const sp<IBinder>& oldToken, |
| 6192 | const sp<IBinder>& newToken) { |
| 6193 | auto command = [this, oldToken, newToken]() REQUIRES(mLock) { |
| 6194 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6195 | mPolicy.notifyFocusChanged(oldToken, newToken); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6196 | }; |
| 6197 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6198 | } |
| 6199 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6200 | void InputDispatcher::sendDropWindowCommandLocked(const sp<IBinder>& token, float x, float y) { |
| 6201 | auto command = [this, token, x, y]() REQUIRES(mLock) { |
| 6202 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6203 | mPolicy.notifyDropWindow(token, x, y); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6204 | }; |
| 6205 | postCommandLocked(std::move(command)); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 6206 | } |
| 6207 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6208 | void InputDispatcher::onAnrLocked(const std::shared_ptr<Connection>& connection) { |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6209 | if (connection == nullptr) { |
| 6210 | LOG_ALWAYS_FATAL("Caller must check for nullness"); |
| 6211 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6212 | // Since we are allowing the policy to extend the timeout, maybe the waitQueue |
| 6213 | // is already healthy again. Don't raise ANR in this situation |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6214 | if (connection->waitQueue.empty()) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6215 | ALOGI("Not raising ANR because the connection %s has recovered", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6216 | connection->inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6217 | return; |
| 6218 | } |
| 6219 | /** |
| 6220 | * The "oldestEntry" is the entry that was first sent to the application. That entry, however, |
| 6221 | * may not be the one that caused the timeout to occur. One possibility is that window timeout |
| 6222 | * has changed. This could cause newer entries to time out before the already dispatched |
| 6223 | * entries. In that situation, the newest entries caused ANR. But in all likelihood, the app |
| 6224 | * processes the events linearly. So providing information about the oldest entry seems to be |
| 6225 | * most useful. |
| 6226 | */ |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6227 | DispatchEntry& oldestEntry = *connection->waitQueue.front(); |
| 6228 | const nsecs_t currentWait = now() - oldestEntry.deliveryTime; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6229 | std::string reason = |
| 6230 | android::base::StringPrintf("%s is not responding. Waited %" PRId64 "ms for %s", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6231 | connection->inputChannel->getName().c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6232 | ns2ms(currentWait), |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6233 | oldestEntry.eventEntry->getDescription().c_str()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6234 | sp<IBinder> connectionToken = connection->inputChannel->getConnectionToken(); |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 6235 | updateLastAnrStateLocked(getWindowHandleLocked(connectionToken), reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6236 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6237 | processConnectionUnresponsiveLocked(*connection, std::move(reason)); |
| 6238 | |
| 6239 | // Stop waking up for events on this connection, it is already unresponsive |
| 6240 | cancelEventsForAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6241 | } |
| 6242 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6243 | void InputDispatcher::onAnrLocked(std::shared_ptr<InputApplicationHandle> application) { |
| 6244 | std::string reason = |
| 6245 | StringPrintf("%s does not have a focused window", application->getName().c_str()); |
| 6246 | updateLastAnrStateLocked(*application, reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6247 | |
Yabin Cui | 8eb9c55 | 2023-06-08 18:05:07 +0000 | [diff] [blame] | 6248 | auto command = [this, app = std::move(application)]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6249 | scoped_unlock unlock(mLock); |
Yabin Cui | ced952f | 2023-06-09 21:12:51 +0000 | [diff] [blame] | 6250 | mPolicy.notifyNoFocusedWindowAnr(app); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6251 | }; |
| 6252 | postCommandLocked(std::move(command)); |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 6253 | } |
| 6254 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 6255 | void InputDispatcher::updateLastAnrStateLocked(const sp<WindowInfoHandle>& window, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6256 | const std::string& reason) { |
| 6257 | const std::string windowLabel = getApplicationWindowLabel(nullptr, window); |
| 6258 | updateLastAnrStateLocked(windowLabel, reason); |
| 6259 | } |
| 6260 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6261 | void InputDispatcher::updateLastAnrStateLocked(const InputApplicationHandle& application, |
| 6262 | const std::string& reason) { |
| 6263 | const std::string windowLabel = getApplicationWindowLabel(&application, nullptr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6264 | updateLastAnrStateLocked(windowLabel, reason); |
| 6265 | } |
| 6266 | |
| 6267 | void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel, |
| 6268 | const std::string& reason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6269 | // 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] | 6270 | time_t t = time(nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6271 | struct tm tm; |
| 6272 | localtime_r(&t, &tm); |
| 6273 | char timestr[64]; |
| 6274 | strftime(timestr, sizeof(timestr), "%F %T", &tm); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6275 | mLastAnrState.clear(); |
| 6276 | mLastAnrState += INDENT "ANR:\n"; |
| 6277 | mLastAnrState += StringPrintf(INDENT2 "Time: %s\n", timestr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6278 | mLastAnrState += StringPrintf(INDENT2 "Reason: %s\n", reason.c_str()); |
| 6279 | mLastAnrState += StringPrintf(INDENT2 "Window: %s\n", windowLabel.c_str()); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6280 | dumpDispatchStateLocked(mLastAnrState); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6281 | } |
| 6282 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6283 | void InputDispatcher::doInterceptKeyBeforeDispatchingCommand(const sp<IBinder>& focusedWindowToken, |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 6284 | const KeyEntry& entry) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6285 | const KeyEvent event = createKeyEvent(entry); |
| 6286 | nsecs_t delay = 0; |
| 6287 | { // release lock |
| 6288 | scoped_unlock unlock(mLock); |
| 6289 | android::base::Timer t; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6290 | delay = mPolicy.interceptKeyBeforeDispatching(focusedWindowToken, event, entry.policyFlags); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6291 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 6292 | ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms", |
| 6293 | std::to_string(t.duration().count()).c_str()); |
| 6294 | } |
| 6295 | } // acquire lock |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6296 | |
| 6297 | if (delay < 0) { |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 6298 | entry.interceptKeyResult = KeyEntry::InterceptKeyResult::SKIP; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6299 | } else if (delay == 0) { |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 6300 | entry.interceptKeyResult = KeyEntry::InterceptKeyResult::CONTINUE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6301 | } else { |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 6302 | entry.interceptKeyResult = KeyEntry::InterceptKeyResult::TRY_AGAIN_LATER; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6303 | entry.interceptKeyWakeupTime = now() + delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6304 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6305 | } |
| 6306 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6307 | void InputDispatcher::sendWindowUnresponsiveCommandLocked(const sp<IBinder>& token, |
Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6308 | std::optional<gui::Pid> pid, |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6309 | std::string reason) { |
Yabin Cui | 8eb9c55 | 2023-06-08 18:05:07 +0000 | [diff] [blame] | 6310 | auto command = [this, token, pid, r = std::move(reason)]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6311 | scoped_unlock unlock(mLock); |
Yabin Cui | ced952f | 2023-06-09 21:12:51 +0000 | [diff] [blame] | 6312 | mPolicy.notifyWindowUnresponsive(token, pid, r); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6313 | }; |
| 6314 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6315 | } |
| 6316 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6317 | void InputDispatcher::sendWindowResponsiveCommandLocked(const sp<IBinder>& token, |
Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6318 | std::optional<gui::Pid> pid) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6319 | auto command = [this, token, pid]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6320 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6321 | mPolicy.notifyWindowResponsive(token, pid); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6322 | }; |
| 6323 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6324 | } |
| 6325 | |
| 6326 | /** |
| 6327 | * Tell the policy that a connection has become unresponsive so that it can start ANR. |
| 6328 | * Check whether the connection of interest is a monitor or a window, and add the corresponding |
| 6329 | * command entry to the command queue. |
| 6330 | */ |
| 6331 | void InputDispatcher::processConnectionUnresponsiveLocked(const Connection& connection, |
| 6332 | std::string reason) { |
| 6333 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6334 | std::optional<gui::Pid> pid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6335 | if (connection.monitor) { |
| 6336 | ALOGW("Monitor %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 6337 | reason.c_str()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6338 | pid = findMonitorPidByTokenLocked(connectionToken); |
| 6339 | } else { |
| 6340 | // The connection is a window |
| 6341 | ALOGW("Window %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 6342 | reason.c_str()); |
| 6343 | const sp<WindowInfoHandle> handle = getWindowHandleLocked(connectionToken); |
| 6344 | if (handle != nullptr) { |
| 6345 | pid = handle->getInfo()->ownerPid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6346 | } |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6347 | } |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6348 | sendWindowUnresponsiveCommandLocked(connectionToken, pid, std::move(reason)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6349 | } |
| 6350 | |
| 6351 | /** |
| 6352 | * Tell the policy that a connection has become responsive so that it can stop ANR. |
| 6353 | */ |
| 6354 | void InputDispatcher::processConnectionResponsiveLocked(const Connection& connection) { |
| 6355 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6356 | std::optional<gui::Pid> pid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6357 | if (connection.monitor) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6358 | pid = findMonitorPidByTokenLocked(connectionToken); |
| 6359 | } else { |
| 6360 | // The connection is a window |
| 6361 | const sp<WindowInfoHandle> handle = getWindowHandleLocked(connectionToken); |
| 6362 | if (handle != nullptr) { |
| 6363 | pid = handle->getInfo()->ownerPid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6364 | } |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6365 | } |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6366 | sendWindowResponsiveCommandLocked(connectionToken, pid); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6367 | } |
| 6368 | |
Prabir Pradhan | 2404754 | 2023-11-02 17:14:59 +0000 | [diff] [blame] | 6369 | std::unique_ptr<const KeyEntry> InputDispatcher::afterKeyEventLockedInterruptable( |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6370 | const std::shared_ptr<Connection>& connection, DispatchEntry& dispatchEntry, |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6371 | const KeyEntry& keyEntry, bool handled) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6372 | if (keyEntry.flags & AKEY_EVENT_FLAG_FALLBACK) { |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6373 | if (!handled) { |
| 6374 | // Report the key as unhandled, since the fallback was not handled. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6375 | mReporter->reportUnhandledKey(keyEntry.id); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6376 | } |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6377 | return {}; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6378 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6379 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6380 | // Get the fallback key state. |
| 6381 | // Clear it out after dispatching the UP. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6382 | int32_t originalKeyCode = keyEntry.keyCode; |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6383 | std::optional<int32_t> fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6384 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6385 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6386 | } |
| 6387 | |
Prabir Pradhan | 8c90d78 | 2023-09-15 21:16:44 +0000 | [diff] [blame] | 6388 | if (handled || !dispatchEntry.hasForegroundTarget()) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6389 | // If the application handles the original key for which we previously |
| 6390 | // generated a fallback or if the window is not a foreground window, |
| 6391 | // then cancel the associated fallback key, if any. |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6392 | if (fallbackKeyCode) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6393 | // Dispatch the unhandled key to the policy with the cancel flag. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6394 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6395 | ALOGD("Unhandled key event: Asking policy to cancel fallback action. " |
| 6396 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6397 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, |
| 6398 | keyEntry.policyFlags); |
| 6399 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6400 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6401 | event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6402 | |
| 6403 | mLock.unlock(); |
| 6404 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6405 | if (const auto unhandledKeyFallback = |
| 6406 | mPolicy.dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), |
| 6407 | event, keyEntry.policyFlags); |
| 6408 | unhandledKeyFallback) { |
| 6409 | event = *unhandledKeyFallback; |
| 6410 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6411 | |
| 6412 | mLock.lock(); |
| 6413 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6414 | // Cancel the fallback key. |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6415 | if (*fallbackKeyCode != AKEYCODE_UNKNOWN) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 6416 | CancelationOptions options(CancelationOptions::Mode::CANCEL_FALLBACK_EVENTS, |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6417 | "application handled the original non-fallback key " |
| 6418 | "or is no longer a foreground target, " |
| 6419 | "canceling previously dispatched fallback key"); |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6420 | options.keyCode = *fallbackKeyCode; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6421 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6422 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6423 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6424 | } |
| 6425 | } else { |
| 6426 | // If the application did not handle a non-fallback key, first check |
| 6427 | // that we are in a good state to perform unhandled key event processing |
| 6428 | // Then ask the policy what to do with it. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6429 | bool initialDown = keyEntry.action == AKEY_EVENT_ACTION_DOWN && keyEntry.repeatCount == 0; |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6430 | if (!fallbackKeyCode && !initialDown) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6431 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6432 | ALOGD("Unhandled key event: Skipping unhandled key event processing " |
| 6433 | "since this is not an initial down. " |
| 6434 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6435 | originalKeyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 6436 | } |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6437 | return {}; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6438 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6439 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6440 | // Dispatch the unhandled key to the policy. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6441 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6442 | ALOGD("Unhandled key event: Asking policy to perform fallback action. " |
| 6443 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6444 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 6445 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6446 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6447 | |
| 6448 | mLock.unlock(); |
| 6449 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6450 | bool fallback = false; |
| 6451 | if (auto fb = mPolicy.dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), |
| 6452 | event, keyEntry.policyFlags); |
| 6453 | fb) { |
| 6454 | fallback = true; |
| 6455 | event = *fb; |
| 6456 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6457 | |
| 6458 | mLock.lock(); |
| 6459 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 6460 | if (connection->status != Connection::Status::NORMAL) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6461 | connection->inputState.removeFallbackKey(originalKeyCode); |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6462 | return {}; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6463 | } |
| 6464 | |
| 6465 | // Latch the fallback keycode for this key on an initial down. |
| 6466 | // The fallback keycode cannot change at any other point in the lifecycle. |
| 6467 | if (initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6468 | if (fallback) { |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6469 | *fallbackKeyCode = event.getKeyCode(); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6470 | } else { |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6471 | *fallbackKeyCode = AKEYCODE_UNKNOWN; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6472 | } |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6473 | connection->inputState.setFallbackKey(originalKeyCode, *fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6474 | } |
| 6475 | |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6476 | ALOG_ASSERT(fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6477 | |
| 6478 | // Cancel the fallback key if the policy decides not to send it anymore. |
| 6479 | // We will continue to dispatch the key to the policy but we will no |
| 6480 | // longer dispatch a fallback key to the application. |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6481 | if (*fallbackKeyCode != AKEYCODE_UNKNOWN && |
| 6482 | (!fallback || *fallbackKeyCode != event.getKeyCode())) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6483 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6484 | if (fallback) { |
| 6485 | ALOGD("Unhandled key event: Policy requested to send key %d" |
| 6486 | "as a fallback for %d, but on the DOWN it had requested " |
| 6487 | "to send %d instead. Fallback canceled.", |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6488 | event.getKeyCode(), originalKeyCode, *fallbackKeyCode); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6489 | } else { |
| 6490 | ALOGD("Unhandled key event: Policy did not request fallback for %d, " |
| 6491 | "but on the DOWN it had requested to send %d. " |
| 6492 | "Fallback canceled.", |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6493 | originalKeyCode, *fallbackKeyCode); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6494 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6495 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6496 | |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 6497 | CancelationOptions options(CancelationOptions::Mode::CANCEL_FALLBACK_EVENTS, |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6498 | "canceling fallback, policy no longer desires it"); |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6499 | options.keyCode = *fallbackKeyCode; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6500 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
| 6501 | |
| 6502 | fallback = false; |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6503 | *fallbackKeyCode = AKEYCODE_UNKNOWN; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6504 | if (keyEntry.action != AKEY_EVENT_ACTION_UP) { |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6505 | connection->inputState.setFallbackKey(originalKeyCode, *fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6506 | } |
| 6507 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6508 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6509 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6510 | { |
| 6511 | std::string msg; |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6512 | const std::map<int32_t, int32_t>& fallbackKeys = |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6513 | connection->inputState.getFallbackKeys(); |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6514 | for (const auto& [key, value] : fallbackKeys) { |
| 6515 | msg += StringPrintf(", %d->%d", key, value); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6516 | } |
| 6517 | ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.", |
| 6518 | fallbackKeys.size(), msg.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6519 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6520 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6521 | |
| 6522 | if (fallback) { |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6523 | // Return the fallback key that we want dispatched to the channel. |
| 6524 | std::unique_ptr<KeyEntry> newEntry = |
| 6525 | std::make_unique<KeyEntry>(mIdGenerator.nextId(), keyEntry.injectionState, |
| 6526 | event.getEventTime(), event.getDeviceId(), |
| 6527 | event.getSource(), event.getDisplayId(), |
| 6528 | keyEntry.policyFlags, keyEntry.action, |
| 6529 | event.getFlags() | AKEY_EVENT_FLAG_FALLBACK, |
| 6530 | *fallbackKeyCode, event.getScanCode(), |
| 6531 | event.getMetaState(), event.getRepeatCount(), |
| 6532 | event.getDownTime()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6533 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6534 | ALOGD("Unhandled key event: Dispatching fallback key. " |
| 6535 | "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x", |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6536 | originalKeyCode, *fallbackKeyCode, keyEntry.metaState); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6537 | } |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6538 | return newEntry; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6539 | } else { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6540 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6541 | ALOGD("Unhandled key event: No fallback key."); |
| 6542 | } |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6543 | |
| 6544 | // Report the key as unhandled, since there is no fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6545 | mReporter->reportUnhandledKey(keyEntry.id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6546 | } |
| 6547 | } |
Prabir Pradhan | b9dd164 | 2023-11-02 18:05:36 +0000 | [diff] [blame] | 6548 | return {}; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6549 | } |
| 6550 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6551 | void InputDispatcher::traceInboundQueueLengthLocked() { |
| 6552 | if (ATRACE_ENABLED()) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 6553 | ATRACE_INT("iq", mInboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6554 | } |
| 6555 | } |
| 6556 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6557 | void InputDispatcher::traceOutboundQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6558 | if (ATRACE_ENABLED()) { |
| 6559 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6560 | snprintf(counterName, sizeof(counterName), "oq:%s", connection.getWindowName().c_str()); |
| 6561 | ATRACE_INT(counterName, connection.outboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6562 | } |
| 6563 | } |
| 6564 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6565 | void InputDispatcher::traceWaitQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6566 | if (ATRACE_ENABLED()) { |
| 6567 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6568 | snprintf(counterName, sizeof(counterName), "wq:%s", connection.getWindowName().c_str()); |
| 6569 | ATRACE_INT(counterName, connection.waitQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6570 | } |
| 6571 | } |
| 6572 | |
Siarhei Vishniakou | 5e20f27 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 6573 | void InputDispatcher::dump(std::string& dump) const { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6574 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6575 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6576 | dump += "Input Dispatcher State:\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6577 | dumpDispatchStateLocked(dump); |
| 6578 | |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6579 | if (!mLastAnrState.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6580 | dump += "\nInput Dispatcher State at time of last ANR:\n"; |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6581 | dump += mLastAnrState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6582 | } |
| 6583 | } |
| 6584 | |
| 6585 | void InputDispatcher::monitor() { |
| 6586 | // 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] | 6587 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6588 | mLooper->wake(); |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6589 | mDispatcherIsAlive.wait(_l); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6590 | } |
| 6591 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6592 | /** |
| 6593 | * Wake up the dispatcher and wait until it processes all events and commands. |
| 6594 | * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so |
| 6595 | * this method can be safely called from any thread, as long as you've ensured that |
| 6596 | * the work you are interested in completing has already been queued. |
| 6597 | */ |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 6598 | bool InputDispatcher::waitForIdle() const { |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6599 | /** |
| 6600 | * Timeout should represent the longest possible time that a device might spend processing |
| 6601 | * events and commands. |
| 6602 | */ |
| 6603 | constexpr std::chrono::duration TIMEOUT = 100ms; |
| 6604 | std::unique_lock lock(mLock); |
| 6605 | mLooper->wake(); |
| 6606 | std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT); |
| 6607 | return result == std::cv_status::no_timeout; |
| 6608 | } |
| 6609 | |
Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 6610 | /** |
| 6611 | * Sets focus to the window identified by the token. This must be called |
| 6612 | * after updating any input window handles. |
| 6613 | * |
| 6614 | * Params: |
| 6615 | * request.token - input channel token used to identify the window that should gain focus. |
| 6616 | * request.focusedToken - the token that the caller expects currently to be focused. If the |
| 6617 | * specified token does not match the currently focused window, this request will be dropped. |
| 6618 | * If the specified focused token matches the currently focused window, the call will succeed. |
| 6619 | * Set this to "null" if this call should succeed no matter what the currently focused token is. |
| 6620 | * request.timestamp - SYSTEM_TIME_MONOTONIC timestamp in nanos set by the client (wm) |
| 6621 | * when requesting the focus change. This determines which request gets |
| 6622 | * precedence if there is a focus change request from another source such as pointer down. |
| 6623 | */ |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6624 | void InputDispatcher::setFocusedWindow(const FocusRequest& request) { |
| 6625 | { // acquire lock |
| 6626 | std::scoped_lock _l(mLock); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6627 | std::optional<FocusResolver::FocusChanges> changes = |
| 6628 | mFocusResolver.setFocusedWindow(request, getWindowHandlesLocked(request.displayId)); |
| 6629 | if (changes) { |
| 6630 | onFocusChangedLocked(*changes); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6631 | } |
| 6632 | } // release lock |
| 6633 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6634 | mLooper->wake(); |
| 6635 | } |
| 6636 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6637 | void InputDispatcher::onFocusChangedLocked(const FocusResolver::FocusChanges& changes) { |
| 6638 | if (changes.oldFocus) { |
| 6639 | std::shared_ptr<InputChannel> focusedInputChannel = getInputChannelLocked(changes.oldFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6640 | if (focusedInputChannel) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 6641 | CancelationOptions options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6642 | "focus left window"); |
| 6643 | synthesizeCancelationEventsForInputChannelLocked(focusedInputChannel, options); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6644 | enqueueFocusEventLocked(changes.oldFocus, /*hasFocus=*/false, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6645 | } |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6646 | } |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6647 | if (changes.newFocus) { |
Siarhei Vishniakou | c033dfb | 2023-10-03 10:45:16 -0700 | [diff] [blame] | 6648 | resetNoFocusedWindowTimeoutLocked(); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6649 | enqueueFocusEventLocked(changes.newFocus, /*hasFocus=*/true, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6650 | } |
| 6651 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6652 | // If a window has pointer capture, then it must have focus. We need to ensure that this |
| 6653 | // contract is upheld when pointer capture is being disabled due to a loss of window focus. |
| 6654 | // If the window loses focus before it loses pointer capture, then the window can be in a state |
| 6655 | // where it has pointer capture but not focus, violating the contract. Therefore we must |
| 6656 | // dispatch the pointer capture event before the focus event. Since focus events are added to |
| 6657 | // the front of the queue (above), we add the pointer capture event to the front of the queue |
| 6658 | // after the focus events are added. This ensures the pointer capture event ends up at the |
| 6659 | // front. |
| 6660 | disablePointerCaptureForcedLocked(); |
| 6661 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6662 | if (mFocusedDisplayId == changes.displayId) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6663 | sendFocusChangedCommandLocked(changes.oldFocus, changes.newFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6664 | } |
| 6665 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6666 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6667 | void InputDispatcher::disablePointerCaptureForcedLocked() { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6668 | if (!mCurrentPointerCaptureRequest.enable && !mWindowTokenWithPointerCapture) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6669 | return; |
| 6670 | } |
| 6671 | |
| 6672 | ALOGD_IF(DEBUG_FOCUS, "Disabling Pointer Capture because the window lost focus."); |
| 6673 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6674 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6675 | setPointerCaptureLocked(false); |
| 6676 | } |
| 6677 | |
| 6678 | if (!mWindowTokenWithPointerCapture) { |
| 6679 | // No need to send capture changes because no window has capture. |
| 6680 | return; |
| 6681 | } |
| 6682 | |
| 6683 | if (mPendingEvent != nullptr) { |
| 6684 | // Move the pending event to the front of the queue. This will give the chance |
| 6685 | // for the pending event to be dropped if it is a captured event. |
| 6686 | mInboundQueue.push_front(mPendingEvent); |
| 6687 | mPendingEvent = nullptr; |
| 6688 | } |
| 6689 | |
| 6690 | auto entry = std::make_unique<PointerCaptureChangedEntry>(mIdGenerator.nextId(), now(), |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6691 | mCurrentPointerCaptureRequest); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6692 | mInboundQueue.push_front(std::move(entry)); |
| 6693 | } |
| 6694 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6695 | void InputDispatcher::setPointerCaptureLocked(bool enable) { |
| 6696 | mCurrentPointerCaptureRequest.enable = enable; |
| 6697 | mCurrentPointerCaptureRequest.seq++; |
| 6698 | auto command = [this, request = mCurrentPointerCaptureRequest]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6699 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 6700 | mPolicy.setPointerCapture(request); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6701 | }; |
| 6702 | postCommandLocked(std::move(command)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6703 | } |
| 6704 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6705 | void InputDispatcher::displayRemoved(int32_t displayId) { |
| 6706 | { // acquire lock |
| 6707 | std::scoped_lock _l(mLock); |
| 6708 | // Set an empty list to remove all handles from the specific display. |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 6709 | setInputWindowsLocked(/*windowInfoHandles=*/{}, displayId); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6710 | setFocusedApplicationLocked(displayId, nullptr); |
| 6711 | // Call focus resolver to clean up stale requests. This must be called after input windows |
| 6712 | // have been removed for the removed display. |
| 6713 | mFocusResolver.displayRemoved(displayId); |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 6714 | // Reset pointer capture eligibility, regardless of previous state. |
| 6715 | std::erase(mIneligibleDisplaysForPointerCapture, displayId); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 6716 | // Remove the associated touch mode state. |
| 6717 | mTouchModePerDisplay.erase(displayId); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 6718 | mVerifiersByDisplay.erase(displayId); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6719 | } // release lock |
| 6720 | |
| 6721 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6722 | mLooper->wake(); |
| 6723 | } |
| 6724 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 6725 | void InputDispatcher::onWindowInfosChanged(const gui::WindowInfosUpdate& update) { |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6726 | // The listener sends the windows as a flattened array. Separate the windows by display for |
| 6727 | // more convenient parsing. |
| 6728 | std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>> handlesPerDisplay; |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 6729 | for (const auto& info : update.windowInfos) { |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6730 | handlesPerDisplay.emplace(info.displayId, std::vector<sp<WindowInfoHandle>>()); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6731 | handlesPerDisplay[info.displayId].push_back(sp<WindowInfoHandle>::make(info)); |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6732 | } |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6733 | |
| 6734 | { // acquire lock |
| 6735 | std::scoped_lock _l(mLock); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 6736 | |
| 6737 | // Ensure that we have an entry created for all existing displays so that if a displayId has |
| 6738 | // no windows, we can tell that the windows were removed from the display. |
| 6739 | for (const auto& [displayId, _] : mWindowHandlesByDisplay) { |
| 6740 | handlesPerDisplay[displayId]; |
| 6741 | } |
| 6742 | |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6743 | mDisplayInfos.clear(); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 6744 | for (const auto& displayInfo : update.displayInfos) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6745 | mDisplayInfos.emplace(displayInfo.displayId, displayInfo); |
| 6746 | } |
| 6747 | |
| 6748 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 6749 | setInputWindowsLocked(handles, displayId); |
| 6750 | } |
Patrick Williams | 9464b2c | 2023-05-23 11:22:04 -0500 | [diff] [blame] | 6751 | |
| 6752 | if (update.vsyncId < mWindowInfosVsyncId) { |
| 6753 | ALOGE("Received out of order window infos update. Last update vsync id: %" PRId64 |
| 6754 | ", current update vsync id: %" PRId64, |
| 6755 | mWindowInfosVsyncId, update.vsyncId); |
| 6756 | } |
| 6757 | mWindowInfosVsyncId = update.vsyncId; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6758 | } |
| 6759 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6760 | mLooper->wake(); |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6761 | } |
| 6762 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6763 | bool InputDispatcher::shouldDropInput( |
| 6764 | const EventEntry& entry, const sp<android::gui::WindowInfoHandle>& windowHandle) const { |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6765 | if (windowHandle->getInfo()->inputConfig.test(WindowInfo::InputConfig::DROP_INPUT) || |
| 6766 | (windowHandle->getInfo()->inputConfig.test( |
| 6767 | WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED) && |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6768 | isWindowObscuredLocked(windowHandle))) { |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6769 | ALOGW("Dropping %s event targeting %s as requested by the input configuration {%s} on " |
| 6770 | "display %" PRId32 ".", |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6771 | ftl::enum_string(entry.type).c_str(), windowHandle->getName().c_str(), |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6772 | windowHandle->getInfo()->inputConfig.string().c_str(), |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6773 | windowHandle->getInfo()->displayId); |
| 6774 | return true; |
| 6775 | } |
| 6776 | return false; |
| 6777 | } |
| 6778 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 6779 | void InputDispatcher::DispatcherWindowListener::onWindowInfosChanged( |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 6780 | const gui::WindowInfosUpdate& update) { |
| 6781 | mDispatcher.onWindowInfosChanged(update); |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 6782 | } |
| 6783 | |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6784 | void InputDispatcher::cancelCurrentTouch() { |
| 6785 | { |
| 6786 | std::scoped_lock _l(mLock); |
| 6787 | ALOGD("Canceling all ongoing pointer gestures on all displays."); |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 6788 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6789 | "cancel current touch"); |
| 6790 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 6791 | |
| 6792 | mTouchStatesByDisplay.clear(); |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6793 | } |
| 6794 | // Wake up poll loop since there might be work to do. |
| 6795 | mLooper->wake(); |
| 6796 | } |
| 6797 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6798 | void InputDispatcher::setMonitorDispatchingTimeoutForTest(std::chrono::nanoseconds timeout) { |
| 6799 | std::scoped_lock _l(mLock); |
| 6800 | mMonitorDispatchingTimeout = timeout; |
| 6801 | } |
| 6802 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6803 | void InputDispatcher::slipWallpaperTouch(ftl::Flags<InputTarget::Flags> targetFlags, |
| 6804 | const sp<WindowInfoHandle>& oldWindowHandle, |
| 6805 | const sp<WindowInfoHandle>& newWindowHandle, |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 6806 | TouchState& state, int32_t deviceId, int32_t pointerId, |
Siarhei Vishniakou | bf88052 | 2023-05-01 11:03:22 -0700 | [diff] [blame] | 6807 | std::vector<InputTarget>& targets) const { |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 6808 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
| 6809 | pointerIds.set(pointerId); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6810 | const bool oldHasWallpaper = oldWindowHandle->getInfo()->inputConfig.test( |
| 6811 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER); |
| 6812 | const bool newHasWallpaper = targetFlags.test(InputTarget::Flags::FOREGROUND) && |
| 6813 | newWindowHandle->getInfo()->inputConfig.test( |
| 6814 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER); |
| 6815 | const sp<WindowInfoHandle> oldWallpaper = |
| 6816 | oldHasWallpaper ? state.getWallpaperWindow() : nullptr; |
| 6817 | const sp<WindowInfoHandle> newWallpaper = |
| 6818 | newHasWallpaper ? findWallpaperWindowBelow(newWindowHandle) : nullptr; |
| 6819 | if (oldWallpaper == newWallpaper) { |
| 6820 | return; |
| 6821 | } |
| 6822 | |
| 6823 | if (oldWallpaper != nullptr) { |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 6824 | const TouchedWindow& oldTouchedWindow = state.getTouchedWindow(oldWallpaper); |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 6825 | addPointerWindowTargetLocked(oldWallpaper, InputTarget::DispatchMode::SLIPPERY_EXIT, |
| 6826 | oldTouchedWindow.targetFlags, pointerIds, |
| 6827 | oldTouchedWindow.getDownTimeInTarget(deviceId), targets); |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 6828 | state.removeTouchingPointerFromWindow(deviceId, pointerId, oldWallpaper); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6829 | } |
| 6830 | |
| 6831 | if (newWallpaper != nullptr) { |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 6832 | state.addOrUpdateWindow(newWallpaper, InputTarget::DispatchMode::SLIPPERY_ENTER, |
| 6833 | InputTarget::Flags::WINDOW_IS_OBSCURED | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6834 | InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED, |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 6835 | deviceId, pointerIds); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6836 | } |
| 6837 | } |
| 6838 | |
| 6839 | void InputDispatcher::transferWallpaperTouch(ftl::Flags<InputTarget::Flags> oldTargetFlags, |
| 6840 | ftl::Flags<InputTarget::Flags> newTargetFlags, |
| 6841 | const sp<WindowInfoHandle> fromWindowHandle, |
| 6842 | const sp<WindowInfoHandle> toWindowHandle, |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 6843 | TouchState& state, int32_t deviceId, |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 6844 | std::bitset<MAX_POINTER_ID + 1> pointerIds) { |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6845 | const bool oldHasWallpaper = oldTargetFlags.test(InputTarget::Flags::FOREGROUND) && |
| 6846 | fromWindowHandle->getInfo()->inputConfig.test( |
| 6847 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER); |
| 6848 | const bool newHasWallpaper = newTargetFlags.test(InputTarget::Flags::FOREGROUND) && |
| 6849 | toWindowHandle->getInfo()->inputConfig.test( |
| 6850 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER); |
| 6851 | |
| 6852 | const sp<WindowInfoHandle> oldWallpaper = |
| 6853 | oldHasWallpaper ? state.getWallpaperWindow() : nullptr; |
| 6854 | const sp<WindowInfoHandle> newWallpaper = |
| 6855 | newHasWallpaper ? findWallpaperWindowBelow(toWindowHandle) : nullptr; |
| 6856 | if (oldWallpaper == newWallpaper) { |
| 6857 | return; |
| 6858 | } |
| 6859 | |
| 6860 | if (oldWallpaper != nullptr) { |
| 6861 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
| 6862 | "transferring touch focus to another window"); |
| 6863 | state.removeWindowByToken(oldWallpaper->getToken()); |
| 6864 | synthesizeCancelationEventsForWindowLocked(oldWallpaper, options); |
| 6865 | } |
| 6866 | |
| 6867 | if (newWallpaper != nullptr) { |
| 6868 | nsecs_t downTimeInTarget = now(); |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 6869 | ftl::Flags<InputTarget::Flags> wallpaperFlags = oldTargetFlags & InputTarget::Flags::SPLIT; |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6870 | wallpaperFlags |= InputTarget::Flags::WINDOW_IS_OBSCURED | |
| 6871 | InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 6872 | state.addOrUpdateWindow(newWallpaper, InputTarget::DispatchMode::AS_IS, wallpaperFlags, |
| 6873 | deviceId, pointerIds, downTimeInTarget); |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6874 | std::shared_ptr<Connection> wallpaperConnection = |
| 6875 | getConnectionLocked(newWallpaper->getToken()); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6876 | if (wallpaperConnection != nullptr) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6877 | std::shared_ptr<Connection> toConnection = |
| 6878 | getConnectionLocked(toWindowHandle->getToken()); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6879 | toConnection->inputState.mergePointerStateTo(wallpaperConnection->inputState); |
| 6880 | synthesizePointerDownEventsForConnectionLocked(downTimeInTarget, wallpaperConnection, |
| 6881 | wallpaperFlags); |
| 6882 | } |
| 6883 | } |
| 6884 | } |
| 6885 | |
| 6886 | sp<WindowInfoHandle> InputDispatcher::findWallpaperWindowBelow( |
| 6887 | const sp<WindowInfoHandle>& windowHandle) const { |
| 6888 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
| 6889 | getWindowHandlesLocked(windowHandle->getInfo()->displayId); |
| 6890 | bool foundWindow = false; |
| 6891 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
| 6892 | if (!foundWindow && otherHandle != windowHandle) { |
| 6893 | continue; |
| 6894 | } |
| 6895 | if (windowHandle == otherHandle) { |
| 6896 | foundWindow = true; |
| 6897 | continue; |
| 6898 | } |
| 6899 | |
| 6900 | if (otherHandle->getInfo()->inputConfig.test(WindowInfo::InputConfig::IS_WALLPAPER)) { |
| 6901 | return otherHandle; |
| 6902 | } |
| 6903 | } |
| 6904 | return nullptr; |
| 6905 | } |
| 6906 | |
Siarhei Vishniakou | fa2a049 | 2023-11-14 13:13:18 -0800 | [diff] [blame] | 6907 | void InputDispatcher::setKeyRepeatConfiguration(std::chrono::nanoseconds timeout, |
| 6908 | std::chrono::nanoseconds delay) { |
Nergi Rahardi | 730cf3c | 2023-04-13 12:41:17 +0900 | [diff] [blame] | 6909 | std::scoped_lock _l(mLock); |
| 6910 | |
Siarhei Vishniakou | fa2a049 | 2023-11-14 13:13:18 -0800 | [diff] [blame] | 6911 | mConfig.keyRepeatTimeout = timeout.count(); |
| 6912 | mConfig.keyRepeatDelay = delay.count(); |
Nergi Rahardi | 730cf3c | 2023-04-13 12:41:17 +0900 | [diff] [blame] | 6913 | } |
| 6914 | |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 6915 | bool InputDispatcher::isPointerInWindow(const sp<android::IBinder>& token, int32_t displayId, |
| 6916 | DeviceId deviceId, int32_t pointerId) { |
| 6917 | std::scoped_lock _l(mLock); |
| 6918 | auto touchStateIt = mTouchStatesByDisplay.find(displayId); |
| 6919 | if (touchStateIt == mTouchStatesByDisplay.end()) { |
| 6920 | return false; |
| 6921 | } |
| 6922 | for (const TouchedWindow& window : touchStateIt->second.windows) { |
| 6923 | if (window.windowHandle->getToken() == token && |
| 6924 | (window.hasTouchingPointer(deviceId, pointerId) || |
| 6925 | window.hasHoveringPointer(deviceId, pointerId))) { |
| 6926 | return true; |
| 6927 | } |
| 6928 | } |
| 6929 | return false; |
| 6930 | } |
| 6931 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 6932 | } // namespace android::inputdispatcher |