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> |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 28 | #include <ftl/enum.h> |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 29 | #if defined(__ANDROID__) |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 30 | #include <gui/SurfaceComposerClient.h> |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 31 | #endif |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 32 | #include <input/InputDevice.h> |
Siarhei Vishniakou | 6e1e987 | 2022-11-08 17:51:35 -0800 | [diff] [blame] | 33 | #include <input/PrintTools.h> |
tyiu | 1573a67 | 2023-02-21 22:38:32 +0000 | [diff] [blame] | 34 | #include <openssl/mem.h> |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 35 | #include <powermanager/PowerManager.h> |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 36 | #include <unistd.h> |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 37 | #include <utils/Trace.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 38 | |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 39 | #include <cerrno> |
| 40 | #include <cinttypes> |
| 41 | #include <climits> |
| 42 | #include <cstddef> |
| 43 | #include <ctime> |
| 44 | #include <queue> |
| 45 | #include <sstream> |
| 46 | |
| 47 | #include "Connection.h" |
Arthur Hung | 1a1007b | 2022-05-11 07:15:01 +0000 | [diff] [blame] | 48 | #include "DebugConfig.h" |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 49 | #include "InputDispatcher.h" |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 50 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 51 | #define INDENT " " |
| 52 | #define INDENT2 " " |
| 53 | #define INDENT3 " " |
| 54 | #define INDENT4 " " |
| 55 | |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 56 | using namespace android::ftl::flag_operators; |
Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 57 | using android::base::HwTimeoutMultiplier; |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 58 | using android::base::Result; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 59 | using android::base::StringPrintf; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 60 | using android::gui::DisplayInfo; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 61 | using android::gui::FocusRequest; |
| 62 | using android::gui::TouchOcclusionMode; |
| 63 | using android::gui::WindowInfo; |
| 64 | using android::gui::WindowInfoHandle; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 65 | using android::os::InputEventInjectionResult; |
| 66 | using android::os::InputEventInjectionSync; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 67 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 68 | namespace android::inputdispatcher { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 69 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 70 | namespace { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 71 | // Temporarily releases a held mutex for the lifetime of the instance. |
| 72 | // Named to match std::scoped_lock |
| 73 | class scoped_unlock { |
| 74 | public: |
| 75 | explicit scoped_unlock(std::mutex& mutex) : mMutex(mutex) { mMutex.unlock(); } |
| 76 | ~scoped_unlock() { mMutex.lock(); } |
| 77 | |
| 78 | private: |
| 79 | std::mutex& mMutex; |
| 80 | }; |
| 81 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 82 | // Default input dispatching timeout if there is no focused application or paused window |
| 83 | // from which to determine an appropriate dispatching timeout. |
Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 84 | const std::chrono::duration DEFAULT_INPUT_DISPATCHING_TIMEOUT = std::chrono::milliseconds( |
| 85 | android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS * |
| 86 | HwTimeoutMultiplier()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 87 | |
| 88 | // Amount of time to allow for all pending events to be processed when an app switch |
| 89 | // key is on the way. This is used to preempt input dispatch and drop input events |
| 90 | // 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] | 91 | constexpr nsecs_t APP_SWITCH_TIMEOUT = 500 * 1000000LL; // 0.5sec |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 92 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 93 | const std::chrono::duration STALE_EVENT_TIMEOUT = std::chrono::seconds(10) * HwTimeoutMultiplier(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 94 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 95 | // 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] | 96 | constexpr nsecs_t SLOW_EVENT_PROCESSING_WARNING_TIMEOUT = 2000 * 1000000LL; // 2sec |
| 97 | |
| 98 | // Log a warning when an interception call takes longer than this to process. |
| 99 | constexpr std::chrono::milliseconds SLOW_INTERCEPTION_THRESHOLD = 50ms; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 100 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 101 | // Additional key latency in case a connection is still processing some motion events. |
| 102 | // This will help with the case when a user touched a button that opens a new window, |
| 103 | // and gives us the chance to dispatch the key to this new window. |
| 104 | constexpr std::chrono::nanoseconds KEY_WAITING_FOR_EVENTS_TIMEOUT = 500ms; |
| 105 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 106 | // Number of recent events to keep for debugging purposes. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 107 | constexpr size_t RECENT_QUEUE_MAX_SIZE = 10; |
| 108 | |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 109 | // Event log tags. See EventLogTags.logtags for reference. |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 110 | constexpr int LOGTAG_INPUT_INTERACTION = 62000; |
| 111 | constexpr int LOGTAG_INPUT_FOCUS = 62001; |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 112 | constexpr int LOGTAG_INPUT_CANCEL = 62003; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 113 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 114 | const ui::Transform kIdentityTransform; |
| 115 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 116 | inline nsecs_t now() { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 117 | return systemTime(SYSTEM_TIME_MONOTONIC); |
| 118 | } |
| 119 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 120 | inline const std::string binderToString(const sp<IBinder>& binder) { |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 121 | if (binder == nullptr) { |
| 122 | return "<null>"; |
| 123 | } |
| 124 | return StringPrintf("%p", binder.get()); |
| 125 | } |
| 126 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 127 | inline int32_t getMotionEventActionPointerIndex(int32_t action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 128 | return (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> |
| 129 | AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 132 | bool isValidKeyAction(int32_t action) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 133 | switch (action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 134 | case AKEY_EVENT_ACTION_DOWN: |
| 135 | case AKEY_EVENT_ACTION_UP: |
| 136 | return true; |
| 137 | default: |
| 138 | return false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 142 | bool validateKeyEvent(int32_t action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 143 | if (!isValidKeyAction(action)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 144 | ALOGE("Key event has invalid action code 0x%x", action); |
| 145 | return false; |
| 146 | } |
| 147 | return true; |
| 148 | } |
| 149 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 150 | bool isValidMotionAction(int32_t action, int32_t actionButton, int32_t pointerCount) { |
Siarhei Vishniakou | 2f61bdc | 2022-12-02 08:55:51 -0800 | [diff] [blame] | 151 | switch (MotionEvent::getActionMasked(action)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 152 | case AMOTION_EVENT_ACTION_DOWN: |
| 153 | case AMOTION_EVENT_ACTION_UP: |
Siarhei Vishniakou | 2f61bdc | 2022-12-02 08:55:51 -0800 | [diff] [blame] | 154 | return pointerCount == 1; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 155 | case AMOTION_EVENT_ACTION_MOVE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 156 | case AMOTION_EVENT_ACTION_HOVER_ENTER: |
| 157 | case AMOTION_EVENT_ACTION_HOVER_MOVE: |
| 158 | case AMOTION_EVENT_ACTION_HOVER_EXIT: |
Siarhei Vishniakou | 2f61bdc | 2022-12-02 08:55:51 -0800 | [diff] [blame] | 159 | return pointerCount >= 1; |
| 160 | case AMOTION_EVENT_ACTION_CANCEL: |
| 161 | case AMOTION_EVENT_ACTION_OUTSIDE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 162 | case AMOTION_EVENT_ACTION_SCROLL: |
| 163 | return true; |
| 164 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 165 | case AMOTION_EVENT_ACTION_POINTER_UP: { |
Siarhei Vishniakou | 2f61bdc | 2022-12-02 08:55:51 -0800 | [diff] [blame] | 166 | const int32_t index = MotionEvent::getActionIndex(action); |
| 167 | return index >= 0 && index < pointerCount && pointerCount > 1; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 168 | } |
| 169 | case AMOTION_EVENT_ACTION_BUTTON_PRESS: |
| 170 | case AMOTION_EVENT_ACTION_BUTTON_RELEASE: |
| 171 | return actionButton != 0; |
| 172 | default: |
| 173 | return false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 177 | int64_t millis(std::chrono::nanoseconds t) { |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 178 | return std::chrono::duration_cast<std::chrono::milliseconds>(t).count(); |
| 179 | } |
| 180 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 181 | bool validateMotionEvent(int32_t action, int32_t actionButton, size_t pointerCount, |
| 182 | const PointerProperties* pointerProperties) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 183 | if (!isValidMotionAction(action, actionButton, pointerCount)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 184 | ALOGE("Motion event has invalid action code 0x%x", action); |
| 185 | return false; |
| 186 | } |
| 187 | if (pointerCount < 1 || pointerCount > MAX_POINTERS) { |
Siarhei Vishniakou | 0174738 | 2022-01-20 13:23:27 -0800 | [diff] [blame] | 188 | ALOGE("Motion event has invalid pointer count %zu; value must be between 1 and %zu.", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 189 | pointerCount, MAX_POINTERS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 190 | return false; |
| 191 | } |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 192 | std::bitset<MAX_POINTER_ID + 1> pointerIdBits; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 193 | for (size_t i = 0; i < pointerCount; i++) { |
| 194 | int32_t id = pointerProperties[i].id; |
| 195 | if (id < 0 || id > MAX_POINTER_ID) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 196 | ALOGE("Motion event has invalid pointer id %d; value must be between 0 and %d", id, |
| 197 | MAX_POINTER_ID); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 198 | return false; |
| 199 | } |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 200 | if (pointerIdBits.test(id)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 201 | ALOGE("Motion event has duplicate pointer id %d", id); |
| 202 | return false; |
| 203 | } |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 204 | pointerIdBits.set(id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 205 | } |
| 206 | return true; |
| 207 | } |
| 208 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 209 | std::string dumpRegion(const Region& region) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 210 | if (region.isEmpty()) { |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 211 | return "<empty>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 212 | } |
| 213 | |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 214 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 215 | bool first = true; |
| 216 | Region::const_iterator cur = region.begin(); |
| 217 | Region::const_iterator const tail = region.end(); |
| 218 | while (cur != tail) { |
| 219 | if (first) { |
| 220 | first = false; |
| 221 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 222 | dump += "|"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 223 | } |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 224 | 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] | 225 | cur++; |
| 226 | } |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 227 | return dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 228 | } |
| 229 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 230 | std::string dumpQueue(const std::deque<DispatchEntry*>& queue, nsecs_t currentTime) { |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 231 | constexpr size_t maxEntries = 50; // max events to print |
| 232 | constexpr size_t skipBegin = maxEntries / 2; |
| 233 | const size_t skipEnd = queue.size() - maxEntries / 2; |
| 234 | // skip from maxEntries / 2 ... size() - maxEntries/2 |
| 235 | // only print from 0 .. skipBegin and then from skipEnd .. size() |
| 236 | |
| 237 | std::string dump; |
| 238 | for (size_t i = 0; i < queue.size(); i++) { |
| 239 | const DispatchEntry& entry = *queue[i]; |
| 240 | if (i >= skipBegin && i < skipEnd) { |
| 241 | dump += StringPrintf(INDENT4 "<skipped %zu entries>\n", skipEnd - skipBegin); |
| 242 | i = skipEnd - 1; // it will be incremented to "skipEnd" by 'continue' |
| 243 | continue; |
| 244 | } |
| 245 | dump.append(INDENT4); |
| 246 | dump += entry.eventEntry->getDescription(); |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 247 | dump += StringPrintf(", seq=%" PRIu32 ", targetFlags=%s, resolvedAction=%d, age=%" PRId64 |
| 248 | "ms", |
| 249 | entry.seq, entry.targetFlags.string().c_str(), entry.resolvedAction, |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 250 | ns2ms(currentTime - entry.eventEntry->eventTime)); |
| 251 | if (entry.deliveryTime != 0) { |
| 252 | // This entry was delivered, so add information on how long we've been waiting |
| 253 | dump += StringPrintf(", wait=%" PRId64 "ms", ns2ms(currentTime - entry.deliveryTime)); |
| 254 | } |
| 255 | dump.append("\n"); |
| 256 | } |
| 257 | return dump; |
| 258 | } |
| 259 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 260 | /** |
| 261 | * Find the entry in std::unordered_map by key, and return it. |
| 262 | * If the entry is not found, return a default constructed entry. |
| 263 | * |
| 264 | * Useful when the entries are vectors, since an empty vector will be returned |
| 265 | * if the entry is not found. |
| 266 | * Also useful when the entries are sp<>. If an entry is not found, nullptr is returned. |
| 267 | */ |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 268 | template <typename K, typename V> |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 269 | V getValueByKey(const std::unordered_map<K, V>& map, K key) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 270 | auto it = map.find(key); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 271 | return it != map.end() ? it->second : V{}; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 272 | } |
| 273 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 274 | bool haveSameToken(const sp<WindowInfoHandle>& first, const sp<WindowInfoHandle>& second) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 275 | if (first == second) { |
| 276 | return true; |
| 277 | } |
| 278 | |
| 279 | if (first == nullptr || second == nullptr) { |
| 280 | return false; |
| 281 | } |
| 282 | |
| 283 | return first->getToken() == second->getToken(); |
| 284 | } |
| 285 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 286 | bool haveSameApplicationToken(const WindowInfo* first, const WindowInfo* second) { |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 287 | if (first == nullptr || second == nullptr) { |
| 288 | return false; |
| 289 | } |
| 290 | return first->applicationInfo.token != nullptr && |
| 291 | first->applicationInfo.token == second->applicationInfo.token; |
| 292 | } |
| 293 | |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 294 | template <typename T> |
| 295 | size_t firstMarkedBit(T set) { |
| 296 | // TODO: replace with std::countr_zero from <bit> when that's available |
| 297 | LOG_ALWAYS_FATAL_IF(set.none()); |
| 298 | size_t i = 0; |
| 299 | while (!set.test(i)) { |
| 300 | i++; |
| 301 | } |
| 302 | return i; |
| 303 | } |
| 304 | |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 305 | std::unique_ptr<DispatchEntry> createDispatchEntry( |
| 306 | const InputTarget& inputTarget, std::shared_ptr<EventEntry> eventEntry, |
| 307 | ftl::Flags<InputTarget::Flags> inputTargetFlags) { |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 308 | if (inputTarget.useDefaultPointerTransform()) { |
| 309 | const ui::Transform& transform = inputTarget.getDefaultPointerTransform(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 310 | return std::make_unique<DispatchEntry>(eventEntry, inputTargetFlags, transform, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 311 | inputTarget.displayTransform, |
| 312 | inputTarget.globalScaleFactor); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | ALOG_ASSERT(eventEntry->type == EventEntry::Type::MOTION); |
| 316 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); |
| 317 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 318 | std::vector<PointerCoords> pointerCoords; |
| 319 | pointerCoords.resize(motionEntry.pointerCount); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 320 | |
| 321 | // Use the first pointer information to normalize all other pointers. This could be any pointer |
| 322 | // 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] | 323 | // uses the transform for the normalized pointer. |
| 324 | const ui::Transform& firstPointerTransform = |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 325 | inputTarget.pointerTransforms[firstMarkedBit(inputTarget.pointerIds)]; |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 326 | ui::Transform inverseFirstTransform = firstPointerTransform.inverse(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 327 | |
| 328 | // Iterate through all pointers in the event to normalize against the first. |
| 329 | for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.pointerCount; pointerIndex++) { |
| 330 | const PointerProperties& pointerProperties = motionEntry.pointerProperties[pointerIndex]; |
| 331 | uint32_t pointerId = uint32_t(pointerProperties.id); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 332 | const ui::Transform& currTransform = inputTarget.pointerTransforms[pointerId]; |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 333 | |
| 334 | pointerCoords[pointerIndex].copyFrom(motionEntry.pointerCoords[pointerIndex]); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 335 | // First, apply the current pointer's transform to update the coordinates into |
| 336 | // window space. |
| 337 | pointerCoords[pointerIndex].transform(currTransform); |
| 338 | // Next, apply the inverse transform of the normalized coordinates so the |
| 339 | // current coordinates are transformed into the normalized coordinate space. |
| 340 | pointerCoords[pointerIndex].transform(inverseFirstTransform); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 343 | std::unique_ptr<MotionEntry> combinedMotionEntry = |
| 344 | std::make_unique<MotionEntry>(motionEntry.id, motionEntry.eventTime, |
| 345 | motionEntry.deviceId, motionEntry.source, |
| 346 | motionEntry.displayId, motionEntry.policyFlags, |
| 347 | motionEntry.action, motionEntry.actionButton, |
| 348 | motionEntry.flags, motionEntry.metaState, |
| 349 | motionEntry.buttonState, motionEntry.classification, |
| 350 | motionEntry.edgeFlags, motionEntry.xPrecision, |
| 351 | motionEntry.yPrecision, motionEntry.xCursorPosition, |
| 352 | motionEntry.yCursorPosition, motionEntry.downTime, |
| 353 | motionEntry.pointerCount, motionEntry.pointerProperties, |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 354 | pointerCoords.data()); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 355 | |
| 356 | if (motionEntry.injectionState) { |
| 357 | combinedMotionEntry->injectionState = motionEntry.injectionState; |
| 358 | combinedMotionEntry->injectionState->refCount += 1; |
| 359 | } |
| 360 | |
| 361 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 362 | std::make_unique<DispatchEntry>(std::move(combinedMotionEntry), inputTargetFlags, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 363 | firstPointerTransform, inputTarget.displayTransform, |
| 364 | inputTarget.globalScaleFactor); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 365 | return dispatchEntry; |
| 366 | } |
| 367 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 368 | status_t openInputChannelPair(const std::string& name, std::shared_ptr<InputChannel>& serverChannel, |
| 369 | std::unique_ptr<InputChannel>& clientChannel) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 370 | std::unique_ptr<InputChannel> uniqueServerChannel; |
| 371 | status_t result = InputChannel::openInputChannelPair(name, uniqueServerChannel, clientChannel); |
| 372 | |
| 373 | serverChannel = std::move(uniqueServerChannel); |
| 374 | return result; |
| 375 | } |
| 376 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 377 | template <typename T> |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 378 | 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] | 379 | if (lhs == nullptr && rhs == nullptr) { |
| 380 | return true; |
| 381 | } |
| 382 | if (lhs == nullptr || rhs == nullptr) { |
| 383 | return false; |
| 384 | } |
| 385 | return *lhs == *rhs; |
| 386 | } |
| 387 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 388 | KeyEvent createKeyEvent(const KeyEntry& entry) { |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 389 | KeyEvent event; |
| 390 | event.initialize(entry.id, entry.deviceId, entry.source, entry.displayId, INVALID_HMAC, |
| 391 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, |
| 392 | entry.repeatCount, entry.downTime, entry.eventTime); |
| 393 | return event; |
| 394 | } |
| 395 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 396 | bool shouldReportMetricsForConnection(const Connection& connection) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 397 | // Do not keep track of gesture monitors. They receive every event and would disproportionately |
| 398 | // affect the statistics. |
| 399 | if (connection.monitor) { |
| 400 | return false; |
| 401 | } |
| 402 | // If the connection is experiencing ANR, let's skip it. We have separate ANR metrics |
| 403 | if (!connection.responsive) { |
| 404 | return false; |
| 405 | } |
| 406 | return true; |
| 407 | } |
| 408 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 409 | bool shouldReportFinishedEvent(const DispatchEntry& dispatchEntry, const Connection& connection) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 410 | const EventEntry& eventEntry = *dispatchEntry.eventEntry; |
| 411 | const int32_t& inputEventId = eventEntry.id; |
| 412 | if (inputEventId != dispatchEntry.resolvedEventId) { |
| 413 | // Event was transmuted |
| 414 | return false; |
| 415 | } |
| 416 | if (inputEventId == android::os::IInputConstants::INVALID_INPUT_EVENT_ID) { |
| 417 | return false; |
| 418 | } |
| 419 | // Only track latency for events that originated from hardware |
| 420 | if (eventEntry.isSynthesized()) { |
| 421 | return false; |
| 422 | } |
| 423 | const EventEntry::Type& inputEventEntryType = eventEntry.type; |
| 424 | if (inputEventEntryType == EventEntry::Type::KEY) { |
| 425 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 426 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
| 427 | return false; |
| 428 | } |
| 429 | } else if (inputEventEntryType == EventEntry::Type::MOTION) { |
| 430 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 431 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL || |
| 432 | motionEntry.action == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 433 | return false; |
| 434 | } |
| 435 | } else { |
| 436 | // Not a key or a motion |
| 437 | return false; |
| 438 | } |
| 439 | if (!shouldReportMetricsForConnection(connection)) { |
| 440 | return false; |
| 441 | } |
| 442 | return true; |
| 443 | } |
| 444 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 445 | /** |
| 446 | * Connection is responsive if it has no events in the waitQueue that are older than the |
| 447 | * current time. |
| 448 | */ |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 449 | bool isConnectionResponsive(const Connection& connection) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 450 | const nsecs_t currentTime = now(); |
| 451 | for (const DispatchEntry* entry : connection.waitQueue) { |
| 452 | if (entry->timeoutTime < currentTime) { |
| 453 | return false; |
| 454 | } |
| 455 | } |
| 456 | return true; |
| 457 | } |
| 458 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 459 | // Returns true if the event type passed as argument represents a user activity. |
| 460 | bool isUserActivityEvent(const EventEntry& eventEntry) { |
| 461 | switch (eventEntry.type) { |
| 462 | case EventEntry::Type::FOCUS: |
| 463 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 464 | case EventEntry::Type::DRAG: |
| 465 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
| 466 | case EventEntry::Type::SENSOR: |
| 467 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 468 | return false; |
| 469 | case EventEntry::Type::DEVICE_RESET: |
| 470 | case EventEntry::Type::KEY: |
| 471 | case EventEntry::Type::MOTION: |
| 472 | return true; |
| 473 | } |
| 474 | } |
| 475 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 476 | // 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] | 477 | bool windowAcceptsTouchAt(const WindowInfo& windowInfo, int32_t displayId, float x, float y, |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 478 | bool isStylus, const ui::Transform& displayTransform) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 479 | const auto inputConfig = windowInfo.inputConfig; |
| 480 | if (windowInfo.displayId != displayId || |
| 481 | inputConfig.test(WindowInfo::InputConfig::NOT_VISIBLE)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 482 | return false; |
| 483 | } |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 484 | const bool windowCanInterceptTouch = isStylus && windowInfo.interceptsStylus(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 485 | if (inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE) && !windowCanInterceptTouch) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 486 | return false; |
| 487 | } |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 488 | |
| 489 | // Window Manager works in the logical display coordinate space. When it specifies bounds for a |
| 490 | // window as (l, t, r, b), the range of x in [l, r) and y in [t, b) are considered to be inside |
| 491 | // the window. Points on the right and bottom edges should not be inside the window, so we need |
| 492 | // to be careful about performing a hit test when the display is rotated, since the "right" and |
| 493 | // "bottom" of the window will be different in the display (un-rotated) space compared to in the |
| 494 | // logical display in which WM determined the bounds. Perform the hit test in the logical |
| 495 | // display space to ensure these edges are considered correctly in all orientations. |
| 496 | const auto touchableRegion = displayTransform.transform(windowInfo.touchableRegion); |
| 497 | const auto p = displayTransform.transform(x, y); |
| 498 | if (!touchableRegion.contains(std::floor(p.x), std::floor(p.y))) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 499 | return false; |
| 500 | } |
| 501 | return true; |
| 502 | } |
| 503 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 504 | bool isPointerFromStylus(const MotionEntry& entry, int32_t pointerIndex) { |
| 505 | return isFromSource(entry.source, AINPUT_SOURCE_STYLUS) && |
Prabir Pradhan | e562696 | 2022-10-27 20:30:53 +0000 | [diff] [blame] | 506 | isStylusToolType(entry.pointerProperties[pointerIndex].toolType); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 507 | } |
| 508 | |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 509 | // Determines if the given window can be targeted as InputTarget::Flags::FOREGROUND. |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 510 | // Foreground events are only sent to "foreground targetable" windows, but not all gestures sent to |
| 511 | // such window are necessarily targeted with the flag. For example, an event with ACTION_OUTSIDE can |
| 512 | // 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] | 513 | // InputTarget::Flags::FOREGROUND. |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 514 | bool canReceiveForegroundTouches(const WindowInfo& info) { |
| 515 | // A non-touchable window can still receive touch events (e.g. in the case of |
| 516 | // STYLUS_INTERCEPTOR), so prevent such windows from receiving foreground events for touches. |
| 517 | return !info.inputConfig.test(gui::WindowInfo::InputConfig::NOT_TOUCHABLE) && !info.isSpy(); |
| 518 | } |
| 519 | |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 520 | bool isWindowOwnedBy(const sp<WindowInfoHandle>& windowHandle, int32_t pid, int32_t uid) { |
| 521 | if (windowHandle == nullptr) { |
| 522 | return false; |
| 523 | } |
| 524 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 525 | if (pid == windowInfo->ownerPid && uid == windowInfo->ownerUid) { |
| 526 | return true; |
| 527 | } |
| 528 | return false; |
| 529 | } |
| 530 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 531 | // Checks targeted injection using the window's owner's uid. |
| 532 | // Returns an empty string if an entry can be sent to the given window, or an error message if the |
| 533 | // entry is a targeted injection whose uid target doesn't match the window owner. |
| 534 | std::optional<std::string> verifyTargetedInjection(const sp<WindowInfoHandle>& window, |
| 535 | const EventEntry& entry) { |
| 536 | if (entry.injectionState == nullptr || !entry.injectionState->targetUid) { |
| 537 | // The event was not injected, or the injected event does not target a window. |
| 538 | return {}; |
| 539 | } |
| 540 | const int32_t uid = *entry.injectionState->targetUid; |
| 541 | if (window == nullptr) { |
| 542 | return StringPrintf("No valid window target for injection into uid %d.", uid); |
| 543 | } |
| 544 | if (entry.injectionState->targetUid != window->getInfo()->ownerUid) { |
| 545 | return StringPrintf("Injected event targeted at uid %d would be dispatched to window '%s' " |
| 546 | "owned by uid %d.", |
| 547 | uid, window->getName().c_str(), window->getInfo()->ownerUid); |
| 548 | } |
| 549 | return {}; |
| 550 | } |
| 551 | |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 552 | std::pair<float, float> resolveTouchedPosition(const MotionEntry& entry) { |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 553 | const bool isFromMouse = isFromSource(entry.source, AINPUT_SOURCE_MOUSE); |
| 554 | // Always dispatch mouse events to cursor position. |
| 555 | if (isFromMouse) { |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 556 | return {entry.xCursorPosition, entry.yCursorPosition}; |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | const int32_t pointerIndex = getMotionEventActionPointerIndex(entry.action); |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 560 | return {entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 561 | entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_Y)}; |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 562 | } |
| 563 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 564 | std::optional<nsecs_t> getDownTime(const EventEntry& eventEntry) { |
| 565 | if (eventEntry.type == EventEntry::Type::KEY) { |
| 566 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 567 | return keyEntry.downTime; |
| 568 | } else if (eventEntry.type == EventEntry::Type::MOTION) { |
| 569 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 570 | return motionEntry.downTime; |
| 571 | } |
| 572 | return std::nullopt; |
| 573 | } |
| 574 | |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 575 | /** |
| 576 | * Compare the old touch state to the new touch state, and generate the corresponding touched |
| 577 | * windows (== input targets). |
| 578 | * If a window had the hovering pointer, but now it doesn't, produce HOVER_EXIT for that window. |
| 579 | * If the pointer just entered the new window, produce HOVER_ENTER. |
| 580 | * For pointers remaining in the window, produce HOVER_MOVE. |
| 581 | */ |
| 582 | std::vector<TouchedWindow> getHoveringWindowsLocked(const TouchState* oldState, |
| 583 | const TouchState& newTouchState, |
| 584 | const MotionEntry& entry) { |
| 585 | std::vector<TouchedWindow> out; |
| 586 | const int32_t maskedAction = MotionEvent::getActionMasked(entry.action); |
| 587 | if (maskedAction != AMOTION_EVENT_ACTION_HOVER_ENTER && |
| 588 | maskedAction != AMOTION_EVENT_ACTION_HOVER_MOVE && |
| 589 | maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 590 | // Not a hover event - don't need to do anything |
| 591 | return out; |
| 592 | } |
| 593 | |
| 594 | // We should consider all hovering pointers here. But for now, just use the first one |
| 595 | const int32_t pointerId = entry.pointerProperties[0].id; |
| 596 | |
| 597 | std::set<sp<WindowInfoHandle>> oldWindows; |
| 598 | if (oldState != nullptr) { |
| 599 | oldWindows = oldState->getWindowsWithHoveringPointer(entry.deviceId, pointerId); |
| 600 | } |
| 601 | |
| 602 | std::set<sp<WindowInfoHandle>> newWindows = |
| 603 | newTouchState.getWindowsWithHoveringPointer(entry.deviceId, pointerId); |
| 604 | |
| 605 | // If the pointer is no longer in the new window set, send HOVER_EXIT. |
| 606 | for (const sp<WindowInfoHandle>& oldWindow : oldWindows) { |
| 607 | if (newWindows.find(oldWindow) == newWindows.end()) { |
| 608 | TouchedWindow touchedWindow; |
| 609 | touchedWindow.windowHandle = oldWindow; |
| 610 | touchedWindow.targetFlags = InputTarget::Flags::DISPATCH_AS_HOVER_EXIT; |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 611 | touchedWindow.pointerIds.set(pointerId); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 612 | out.push_back(touchedWindow); |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | for (const sp<WindowInfoHandle>& newWindow : newWindows) { |
| 617 | TouchedWindow touchedWindow; |
| 618 | touchedWindow.windowHandle = newWindow; |
| 619 | if (oldWindows.find(newWindow) == oldWindows.end()) { |
| 620 | // Any windows that have this pointer now, and didn't have it before, should get |
| 621 | // HOVER_ENTER |
| 622 | touchedWindow.targetFlags = InputTarget::Flags::DISPATCH_AS_HOVER_ENTER; |
| 623 | } else { |
| 624 | // This pointer was already sent to the window. Use ACTION_HOVER_MOVE. |
Siarhei Vishniakou | c2eb850 | 2023-04-11 18:33:36 -0700 | [diff] [blame] | 625 | if (CC_UNLIKELY(maskedAction != AMOTION_EVENT_ACTION_HOVER_MOVE)) { |
| 626 | LOG(FATAL) << "Expected ACTION_HOVER_MOVE instead of " << entry.getDescription(); |
| 627 | } |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 628 | touchedWindow.targetFlags = InputTarget::Flags::DISPATCH_AS_IS; |
| 629 | } |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 630 | touchedWindow.pointerIds.set(pointerId); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 631 | if (canReceiveForegroundTouches(*newWindow->getInfo())) { |
| 632 | touchedWindow.targetFlags |= InputTarget::Flags::FOREGROUND; |
| 633 | } |
| 634 | out.push_back(touchedWindow); |
| 635 | } |
| 636 | return out; |
| 637 | } |
| 638 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 639 | template <typename T> |
| 640 | std::vector<T>& operator+=(std::vector<T>& left, const std::vector<T>& right) { |
| 641 | left.insert(left.end(), right.begin(), right.end()); |
| 642 | return left; |
| 643 | } |
| 644 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 645 | } // namespace |
| 646 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 647 | // --- InputDispatcher --- |
| 648 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame^] | 649 | InputDispatcher::InputDispatcher(InputDispatcherPolicyInterface& policy) |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 650 | : InputDispatcher(policy, STALE_EVENT_TIMEOUT) {} |
| 651 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame^] | 652 | InputDispatcher::InputDispatcher(InputDispatcherPolicyInterface& policy, |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 653 | std::chrono::nanoseconds staleEventTimeout) |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 654 | : mPolicy(policy), |
| 655 | mPendingEvent(nullptr), |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 656 | mLastDropReason(DropReason::NOT_DROPPED), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 657 | mIdGenerator(IdGenerator::Source::INPUT_DISPATCHER), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 658 | mAppSwitchSawKeyDown(false), |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 659 | mAppSwitchDueTime(LLONG_MAX), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 660 | mNextUnblockedEvent(nullptr), |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 661 | mMonitorDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 662 | mDispatchEnabled(false), |
| 663 | mDispatchFrozen(false), |
| 664 | mInputFilterEnabled(false), |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 665 | mMaximumObscuringOpacityForTouch(1.0f), |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 666 | mFocusedDisplayId(ADISPLAY_ID_DEFAULT), |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 667 | mWindowTokenWithPointerCapture(nullptr), |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 668 | mStaleEventTimeout(staleEventTimeout), |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 669 | mLatencyAggregator(), |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 670 | mLatencyTracker(&mLatencyAggregator) { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 671 | mLooper = sp<Looper>::make(false); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 672 | mReporter = createInputReporter(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 673 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 674 | mWindowInfoListener = sp<DispatcherWindowListener>::make(*this); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 675 | #if defined(__ANDROID__) |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 676 | SurfaceComposerClient::getDefault()->addWindowInfosListener(mWindowInfoListener); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 677 | #endif |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 678 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | InputDispatcher::~InputDispatcher() { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 682 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 683 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 684 | resetKeyRepeatLocked(); |
| 685 | releasePendingEventLocked(); |
| 686 | drainInboundQueueLocked(); |
| 687 | mCommandQueue.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 688 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 689 | while (!mConnectionsByToken.empty()) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 690 | std::shared_ptr<Connection> connection = mConnectionsByToken.begin()->second; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 691 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), /*notify=*/false); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 692 | } |
| 693 | } |
| 694 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 695 | status_t InputDispatcher::start() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 696 | if (mThread) { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 697 | return ALREADY_EXISTS; |
| 698 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 699 | mThread = std::make_unique<InputThread>( |
| 700 | "InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); }); |
| 701 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | status_t InputDispatcher::stop() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 705 | if (mThread && mThread->isCallingThread()) { |
| 706 | ALOGE("InputDispatcher cannot be stopped from its own thread!"); |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 707 | return INVALID_OPERATION; |
| 708 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 709 | mThread.reset(); |
| 710 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 711 | } |
| 712 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 713 | void InputDispatcher::dispatchOnce() { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 714 | nsecs_t nextWakeupTime = LLONG_MAX; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 715 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 716 | std::scoped_lock _l(mLock); |
| 717 | mDispatcherIsAlive.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 718 | |
| 719 | // Run a dispatch loop if there are no pending commands. |
| 720 | // The dispatch loop might enqueue commands to run afterwards. |
| 721 | if (!haveCommandsLocked()) { |
| 722 | dispatchOnceInnerLocked(&nextWakeupTime); |
| 723 | } |
| 724 | |
| 725 | // Run all pending commands if there are any. |
| 726 | // 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] | 727 | if (runCommandsLockedInterruptable()) { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 728 | nextWakeupTime = LLONG_MIN; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 729 | } |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 730 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 731 | // If we are still waiting for ack on some events, |
| 732 | // we might have to wake up earlier to check if an app is anr'ing. |
| 733 | const nsecs_t nextAnrCheck = processAnrsLocked(); |
| 734 | nextWakeupTime = std::min(nextWakeupTime, nextAnrCheck); |
| 735 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 736 | // We are about to enter an infinitely long sleep, because we have no commands or |
| 737 | // pending or queued events |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 738 | if (nextWakeupTime == LLONG_MAX) { |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 739 | mDispatcherEnteredIdle.notify_all(); |
| 740 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 741 | } // release lock |
| 742 | |
| 743 | // Wait for callback or timeout or wake. (make sure we round up, not down) |
| 744 | nsecs_t currentTime = now(); |
| 745 | int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime); |
| 746 | mLooper->pollOnce(timeoutMillis); |
| 747 | } |
| 748 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 749 | /** |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 750 | * Raise ANR if there is no focused window. |
| 751 | * Before the ANR is raised, do a final state check: |
| 752 | * 1. The currently focused application must be the same one we are waiting for. |
| 753 | * 2. Ensure we still don't have a focused window. |
| 754 | */ |
| 755 | void InputDispatcher::processNoFocusedWindowAnrLocked() { |
| 756 | // Check if the application that we are waiting for is still focused. |
| 757 | std::shared_ptr<InputApplicationHandle> focusedApplication = |
| 758 | getValueByKey(mFocusedApplicationHandlesByDisplay, mAwaitedApplicationDisplayId); |
| 759 | if (focusedApplication == nullptr || |
| 760 | focusedApplication->getApplicationToken() != |
| 761 | mAwaitedFocusedApplication->getApplicationToken()) { |
| 762 | // Unexpected because we should have reset the ANR timer when focused application changed |
| 763 | ALOGE("Waited for a focused window, but focused application has already changed to %s", |
| 764 | focusedApplication->getName().c_str()); |
| 765 | return; // The focused application has changed. |
| 766 | } |
| 767 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 768 | const sp<WindowInfoHandle>& focusedWindowHandle = |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 769 | getFocusedWindowHandleLocked(mAwaitedApplicationDisplayId); |
| 770 | if (focusedWindowHandle != nullptr) { |
| 771 | return; // We now have a focused window. No need for ANR. |
| 772 | } |
| 773 | onAnrLocked(mAwaitedFocusedApplication); |
| 774 | } |
| 775 | |
| 776 | /** |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 777 | * Check if any of the connections' wait queues have events that are too old. |
| 778 | * If we waited for events to be ack'ed for more than the window timeout, raise an ANR. |
| 779 | * Return the time at which we should wake up next. |
| 780 | */ |
| 781 | nsecs_t InputDispatcher::processAnrsLocked() { |
| 782 | const nsecs_t currentTime = now(); |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 783 | nsecs_t nextAnrCheck = LLONG_MAX; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 784 | // Check if we are waiting for a focused window to appear. Raise ANR if waited too long |
| 785 | if (mNoFocusedWindowTimeoutTime.has_value() && mAwaitedFocusedApplication != nullptr) { |
| 786 | if (currentTime >= *mNoFocusedWindowTimeoutTime) { |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 787 | processNoFocusedWindowAnrLocked(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 788 | mAwaitedFocusedApplication.reset(); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 789 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 790 | return LLONG_MIN; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 791 | } else { |
Siarhei Vishniakou | 38a6d27 | 2020-10-20 20:29:33 -0500 | [diff] [blame] | 792 | // Keep waiting. We will drop the event when mNoFocusedWindowTimeoutTime comes. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 793 | nextAnrCheck = *mNoFocusedWindowTimeoutTime; |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | // Check if any connection ANRs are due |
| 798 | nextAnrCheck = std::min(nextAnrCheck, mAnrTracker.firstTimeout()); |
| 799 | if (currentTime < nextAnrCheck) { // most likely scenario |
| 800 | return nextAnrCheck; // everything is normal. Let's check again at nextAnrCheck |
| 801 | } |
| 802 | |
| 803 | // If we reached here, we have an unresponsive connection. |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 804 | std::shared_ptr<Connection> connection = getConnectionLocked(mAnrTracker.firstToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 805 | if (connection == nullptr) { |
| 806 | ALOGE("Could not find connection for entry %" PRId64, mAnrTracker.firstTimeout()); |
| 807 | return nextAnrCheck; |
| 808 | } |
| 809 | connection->responsive = false; |
| 810 | // Stop waking up for this unresponsive connection |
| 811 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 812 | onAnrLocked(connection); |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 813 | return LLONG_MIN; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 814 | } |
| 815 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 816 | std::chrono::nanoseconds InputDispatcher::getDispatchingTimeoutLocked( |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 817 | const std::shared_ptr<Connection>& connection) { |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 818 | if (connection->monitor) { |
| 819 | return mMonitorDispatchingTimeout; |
| 820 | } |
| 821 | const sp<WindowInfoHandle> window = |
| 822 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 823 | if (window != nullptr) { |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 824 | return window->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 825 | } |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 826 | return DEFAULT_INPUT_DISPATCHING_TIMEOUT; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 827 | } |
| 828 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 829 | void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) { |
| 830 | nsecs_t currentTime = now(); |
| 831 | |
Jeff Brown | dc5992e | 2014-04-11 01:27:26 -0700 | [diff] [blame] | 832 | // Reset the key repeat timer whenever normal dispatch is suspended while the |
| 833 | // device is in a non-interactive state. This is to ensure that we abort a key |
| 834 | // repeat if the device is just coming out of sleep. |
| 835 | if (!mDispatchEnabled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 836 | resetKeyRepeatLocked(); |
| 837 | } |
| 838 | |
| 839 | // If dispatching is frozen, do not process timeouts or try to deliver any new events. |
| 840 | if (mDispatchFrozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 841 | if (DEBUG_FOCUS) { |
| 842 | ALOGD("Dispatch frozen. Waiting some more."); |
| 843 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 844 | return; |
| 845 | } |
| 846 | |
| 847 | // Optimize latency of app switches. |
| 848 | // Essentially we start a short timeout when an app switch key (HOME / ENDCALL) has |
| 849 | // been pressed. When it expires, we preempt dispatch and drop all other pending events. |
| 850 | bool isAppSwitchDue = mAppSwitchDueTime <= currentTime; |
| 851 | if (mAppSwitchDueTime < *nextWakeupTime) { |
| 852 | *nextWakeupTime = mAppSwitchDueTime; |
| 853 | } |
| 854 | |
| 855 | // Ready to start a new event. |
| 856 | // If we don't already have a pending event, go grab one. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 857 | if (!mPendingEvent) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 858 | if (mInboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 859 | if (isAppSwitchDue) { |
| 860 | // The inbound queue is empty so the app switch key we were waiting |
| 861 | // for will never arrive. Stop waiting for it. |
| 862 | resetPendingAppSwitchLocked(false); |
| 863 | isAppSwitchDue = false; |
| 864 | } |
| 865 | |
| 866 | // Synthesize a key repeat if appropriate. |
| 867 | if (mKeyRepeatState.lastKeyEntry) { |
| 868 | if (currentTime >= mKeyRepeatState.nextRepeatTime) { |
| 869 | mPendingEvent = synthesizeKeyRepeatLocked(currentTime); |
| 870 | } else { |
| 871 | if (mKeyRepeatState.nextRepeatTime < *nextWakeupTime) { |
| 872 | *nextWakeupTime = mKeyRepeatState.nextRepeatTime; |
| 873 | } |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | // Nothing to do if there is no pending event. |
| 878 | if (!mPendingEvent) { |
| 879 | return; |
| 880 | } |
| 881 | } else { |
| 882 | // Inbound queue has at least one entry. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 883 | mPendingEvent = mInboundQueue.front(); |
| 884 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 885 | traceInboundQueueLengthLocked(); |
| 886 | } |
| 887 | |
| 888 | // Poke user activity for this event. |
| 889 | if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 890 | pokeUserActivityLocked(*mPendingEvent); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 891 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | // Now we have an event to dispatch. |
| 895 | // 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] | 896 | ALOG_ASSERT(mPendingEvent != nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 897 | bool done = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 898 | DropReason dropReason = DropReason::NOT_DROPPED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 899 | if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 900 | dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 901 | } else if (!mDispatchEnabled) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 902 | dropReason = DropReason::DISABLED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | if (mNextUnblockedEvent == mPendingEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 906 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | switch (mPendingEvent->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 910 | case EventEntry::Type::CONFIGURATION_CHANGED: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 911 | const ConfigurationChangedEntry& typedEntry = |
| 912 | static_cast<const ConfigurationChangedEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 913 | done = dispatchConfigurationChangedLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 914 | dropReason = DropReason::NOT_DROPPED; // configuration changes are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 915 | break; |
| 916 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 917 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 918 | case EventEntry::Type::DEVICE_RESET: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 919 | const DeviceResetEntry& typedEntry = |
| 920 | static_cast<const DeviceResetEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 921 | done = dispatchDeviceResetLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 922 | dropReason = DropReason::NOT_DROPPED; // device resets are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 923 | break; |
| 924 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 925 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 926 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 927 | std::shared_ptr<FocusEntry> typedEntry = |
| 928 | std::static_pointer_cast<FocusEntry>(mPendingEvent); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 929 | dispatchFocusLocked(currentTime, typedEntry); |
| 930 | done = true; |
| 931 | dropReason = DropReason::NOT_DROPPED; // focus events are never dropped |
| 932 | break; |
| 933 | } |
| 934 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 935 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 936 | const auto typedEntry = std::static_pointer_cast<TouchModeEntry>(mPendingEvent); |
| 937 | dispatchTouchModeChangeLocked(currentTime, typedEntry); |
| 938 | done = true; |
| 939 | dropReason = DropReason::NOT_DROPPED; // touch mode events are never dropped |
| 940 | break; |
| 941 | } |
| 942 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 943 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 944 | const auto typedEntry = |
| 945 | std::static_pointer_cast<PointerCaptureChangedEntry>(mPendingEvent); |
| 946 | dispatchPointerCaptureChangedLocked(currentTime, typedEntry, dropReason); |
| 947 | done = true; |
| 948 | break; |
| 949 | } |
| 950 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 951 | case EventEntry::Type::DRAG: { |
| 952 | std::shared_ptr<DragEntry> typedEntry = |
| 953 | std::static_pointer_cast<DragEntry>(mPendingEvent); |
| 954 | dispatchDragLocked(currentTime, typedEntry); |
| 955 | done = true; |
| 956 | break; |
| 957 | } |
| 958 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 959 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 960 | std::shared_ptr<KeyEntry> keyEntry = std::static_pointer_cast<KeyEntry>(mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 961 | if (isAppSwitchDue) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 962 | if (isAppSwitchKeyEvent(*keyEntry)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 963 | resetPendingAppSwitchLocked(true); |
| 964 | isAppSwitchDue = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 965 | } else if (dropReason == DropReason::NOT_DROPPED) { |
| 966 | dropReason = DropReason::APP_SWITCH; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 967 | } |
| 968 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 969 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *keyEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 970 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 971 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 972 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 973 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 974 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 975 | done = dispatchKeyLocked(currentTime, keyEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 976 | break; |
| 977 | } |
| 978 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 979 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 980 | std::shared_ptr<MotionEntry> motionEntry = |
| 981 | std::static_pointer_cast<MotionEntry>(mPendingEvent); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 982 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 983 | dropReason = DropReason::APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 984 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 985 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *motionEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 986 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 987 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 988 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 989 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 990 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 991 | done = dispatchMotionLocked(currentTime, motionEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 992 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 993 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 994 | |
| 995 | case EventEntry::Type::SENSOR: { |
| 996 | std::shared_ptr<SensorEntry> sensorEntry = |
| 997 | std::static_pointer_cast<SensorEntry>(mPendingEvent); |
| 998 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 999 | dropReason = DropReason::APP_SWITCH; |
| 1000 | } |
| 1001 | // Sensor timestamps use SYSTEM_TIME_BOOTTIME time base, so we can't use |
| 1002 | // 'currentTime' here, get SYSTEM_TIME_BOOTTIME instead. |
| 1003 | nsecs_t bootTime = systemTime(SYSTEM_TIME_BOOTTIME); |
| 1004 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(bootTime, *sensorEntry)) { |
| 1005 | dropReason = DropReason::STALE; |
| 1006 | } |
| 1007 | dispatchSensorLocked(currentTime, sensorEntry, &dropReason, nextWakeupTime); |
| 1008 | done = true; |
| 1009 | break; |
| 1010 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1011 | } |
| 1012 | |
| 1013 | if (done) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1014 | if (dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1015 | dropInboundEventLocked(*mPendingEvent, dropReason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1016 | } |
Michael Wright | 3a98172 | 2015-06-10 15:26:13 +0100 | [diff] [blame] | 1017 | mLastDropReason = dropReason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1018 | |
| 1019 | releasePendingEventLocked(); |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 1020 | *nextWakeupTime = LLONG_MIN; // force next poll to wake up immediately |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1021 | } |
| 1022 | } |
| 1023 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 1024 | bool InputDispatcher::isStaleEvent(nsecs_t currentTime, const EventEntry& entry) { |
| 1025 | return std::chrono::nanoseconds(currentTime - entry.eventTime) >= mStaleEventTimeout; |
| 1026 | } |
| 1027 | |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1028 | /** |
| 1029 | * Return true if the events preceding this incoming motion event should be dropped |
| 1030 | * Return false otherwise (the default behaviour) |
| 1031 | */ |
| 1032 | bool InputDispatcher::shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1033 | const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN && |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 1034 | isFromSource(motionEntry.source, AINPUT_SOURCE_CLASS_POINTER); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1035 | |
| 1036 | // Optimize case where the current application is unresponsive and the user |
| 1037 | // decides to touch a window in a different application. |
| 1038 | // If the application takes too long to catch up then we drop all events preceding |
| 1039 | // the touch into the other window. |
| 1040 | if (isPointerDownEvent && mAwaitedFocusedApplication != nullptr) { |
Siarhei Vishniakou | 9306c38 | 2022-09-30 15:30:31 -0700 | [diff] [blame] | 1041 | const int32_t displayId = motionEntry.displayId; |
| 1042 | const auto [x, y] = resolveTouchedPosition(motionEntry); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1043 | const bool isStylus = isPointerFromStylus(motionEntry, /*pointerIndex=*/0); |
Siarhei Vishniakou | 9306c38 | 2022-09-30 15:30:31 -0700 | [diff] [blame] | 1044 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1045 | auto [touchedWindowHandle, _] = findTouchedWindowAtLocked(displayId, x, y, isStylus); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1046 | if (touchedWindowHandle != nullptr && |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1047 | touchedWindowHandle->getApplicationToken() != |
| 1048 | mAwaitedFocusedApplication->getApplicationToken()) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1049 | // User touched a different application than the one we are waiting on. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1050 | ALOGI("Pruning input queue because user touched a different application while waiting " |
| 1051 | "for %s", |
| 1052 | mAwaitedFocusedApplication->getName().c_str()); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1053 | return true; |
| 1054 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1055 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1056 | // Alternatively, maybe there's a spy window that could handle this event. |
| 1057 | const std::vector<sp<WindowInfoHandle>> touchedSpies = |
| 1058 | findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus); |
| 1059 | for (const auto& windowHandle : touchedSpies) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 1060 | const std::shared_ptr<Connection> connection = |
| 1061 | getConnectionLocked(windowHandle->getToken()); |
Siarhei Vishniakou | 34ed4d4 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1062 | if (connection != nullptr && connection->responsive) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1063 | // This spy window could take more input. Drop all events preceding this |
| 1064 | // 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] | 1065 | 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] | 1066 | "responsive spy window that may handle the event.", |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1067 | mAwaitedFocusedApplication->getName().c_str()); |
| 1068 | return true; |
| 1069 | } |
| 1070 | } |
| 1071 | } |
| 1072 | |
| 1073 | // Prevent getting stuck: if we have a pending key event, and some motion events that have not |
| 1074 | // yet been processed by some connections, the dispatcher will wait for these motion |
| 1075 | // events to be processed before dispatching the key event. This is because these motion events |
| 1076 | // may cause a new window to be launched, which the user might expect to receive focus. |
| 1077 | // To prevent waiting forever for such events, just send the key to the currently focused window |
| 1078 | if (isPointerDownEvent && mKeyIsWaitingForEventsTimeout) { |
| 1079 | ALOGD("Received a new pointer down event, stop waiting for events to process and " |
| 1080 | "just send the pending key event to the focused window."); |
| 1081 | mKeyIsWaitingForEventsTimeout = now(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1082 | } |
| 1083 | return false; |
| 1084 | } |
| 1085 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1086 | bool InputDispatcher::enqueueInboundEventLocked(std::unique_ptr<EventEntry> newEntry) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1087 | bool needWake = mInboundQueue.empty(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1088 | mInboundQueue.push_back(std::move(newEntry)); |
| 1089 | EventEntry& entry = *(mInboundQueue.back()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1090 | traceInboundQueueLengthLocked(); |
| 1091 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1092 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1093 | case EventEntry::Type::KEY: { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1094 | LOG_ALWAYS_FATAL_IF((entry.policyFlags & POLICY_FLAG_TRUSTED) == 0, |
| 1095 | "Unexpected untrusted event."); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1096 | // Optimize app switch latency. |
| 1097 | // If the application takes too long to catch up then we drop all events preceding |
| 1098 | // the app switch key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1099 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1100 | if (isAppSwitchKeyEvent(keyEntry)) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1101 | if (keyEntry.action == AKEY_EVENT_ACTION_DOWN) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1102 | mAppSwitchSawKeyDown = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1103 | } else if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1104 | if (mAppSwitchSawKeyDown) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1105 | if (DEBUG_APP_SWITCH) { |
| 1106 | ALOGD("App switch is pending!"); |
| 1107 | } |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1108 | mAppSwitchDueTime = keyEntry.eventTime + APP_SWITCH_TIMEOUT; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1109 | mAppSwitchSawKeyDown = false; |
| 1110 | needWake = true; |
| 1111 | } |
| 1112 | } |
| 1113 | } |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 1114 | |
| 1115 | // If a new up event comes in, and the pending event with same key code has been asked |
| 1116 | // to try again later because of the policy. We have to reset the intercept key wake up |
| 1117 | // time for it may have been handled in the policy and could be dropped. |
| 1118 | if (keyEntry.action == AKEY_EVENT_ACTION_UP && mPendingEvent && |
| 1119 | mPendingEvent->type == EventEntry::Type::KEY) { |
| 1120 | KeyEntry& pendingKey = static_cast<KeyEntry&>(*mPendingEvent); |
| 1121 | if (pendingKey.keyCode == keyEntry.keyCode && |
| 1122 | pendingKey.interceptKeyResult == |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1123 | KeyEntry::InterceptKeyResult::TRY_AGAIN_LATER) { |
| 1124 | pendingKey.interceptKeyResult = KeyEntry::InterceptKeyResult::UNKNOWN; |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 1125 | pendingKey.interceptKeyWakeupTime = 0; |
| 1126 | needWake = true; |
| 1127 | } |
| 1128 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1129 | break; |
| 1130 | } |
| 1131 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1132 | case EventEntry::Type::MOTION: { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1133 | LOG_ALWAYS_FATAL_IF((entry.policyFlags & POLICY_FLAG_TRUSTED) == 0, |
| 1134 | "Unexpected untrusted event."); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1135 | if (shouldPruneInboundQueueLocked(static_cast<MotionEntry&>(entry))) { |
| 1136 | mNextUnblockedEvent = mInboundQueue.back(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1137 | needWake = true; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1138 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1139 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1140 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1141 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1142 | LOG_ALWAYS_FATAL("Focus events should be inserted using enqueueFocusEventLocked"); |
| 1143 | break; |
| 1144 | } |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1145 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1146 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1147 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1148 | case EventEntry::Type::SENSOR: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1149 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1150 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1151 | // nothing to do |
| 1152 | break; |
| 1153 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1154 | } |
| 1155 | |
| 1156 | return needWake; |
| 1157 | } |
| 1158 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1159 | void InputDispatcher::addRecentEventLocked(std::shared_ptr<EventEntry> entry) { |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1160 | // Do not store sensor event in recent queue to avoid flooding the queue. |
| 1161 | if (entry->type != EventEntry::Type::SENSOR) { |
| 1162 | mRecentQueue.push_back(entry); |
| 1163 | } |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1164 | if (mRecentQueue.size() > RECENT_QUEUE_MAX_SIZE) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1165 | mRecentQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1166 | } |
| 1167 | } |
| 1168 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1169 | std::pair<sp<WindowInfoHandle>, std::vector<InputTarget>> |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 1170 | InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, float x, float y, bool isStylus, |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1171 | bool ignoreDragWindow) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1172 | // Traverse windows from front to back to find touched window. |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1173 | std::vector<InputTarget> outsideTargets; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1174 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1175 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 1176 | if (ignoreDragWindow && haveSameToken(windowHandle, mDragState->dragWindow)) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1177 | continue; |
| 1178 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1179 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1180 | const WindowInfo& info = *windowHandle->getInfo(); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 1181 | if (!info.isSpy() && |
| 1182 | windowAcceptsTouchAt(info, displayId, x, y, isStylus, getTransformLocked(displayId))) { |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1183 | return {windowHandle, outsideTargets}; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1184 | } |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1185 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1186 | if (info.inputConfig.test(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH)) { |
| 1187 | addWindowTargetLocked(windowHandle, InputTarget::Flags::DISPATCH_AS_OUTSIDE, |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 1188 | /*pointerIds=*/{}, /*firstDownTimeInTarget=*/std::nullopt, |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1189 | outsideTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1190 | } |
| 1191 | } |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1192 | return {nullptr, {}}; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1193 | } |
| 1194 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1195 | std::vector<sp<WindowInfoHandle>> InputDispatcher::findTouchedSpyWindowsAtLocked( |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 1196 | int32_t displayId, float x, float y, bool isStylus) const { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1197 | // Traverse windows from front to back and gather the touched spy windows. |
| 1198 | std::vector<sp<WindowInfoHandle>> spyWindows; |
| 1199 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
| 1200 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 1201 | const WindowInfo& info = *windowHandle->getInfo(); |
| 1202 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 1203 | if (!windowAcceptsTouchAt(info, displayId, x, y, isStylus, getTransformLocked(displayId))) { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1204 | continue; |
| 1205 | } |
| 1206 | if (!info.isSpy()) { |
| 1207 | // The first touched non-spy window was found, so return the spy windows touched so far. |
| 1208 | return spyWindows; |
| 1209 | } |
| 1210 | spyWindows.push_back(windowHandle); |
| 1211 | } |
| 1212 | return spyWindows; |
| 1213 | } |
| 1214 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1215 | void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1216 | const char* reason; |
| 1217 | switch (dropReason) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1218 | case DropReason::POLICY: |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 1219 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1220 | ALOGD("Dropped event because policy consumed it."); |
| 1221 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1222 | reason = "inbound event was dropped because the policy consumed it"; |
| 1223 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1224 | case DropReason::DISABLED: |
| 1225 | if (mLastDropReason != DropReason::DISABLED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1226 | ALOGI("Dropped event because input dispatch is disabled."); |
| 1227 | } |
| 1228 | reason = "inbound event was dropped because input dispatch is disabled"; |
| 1229 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1230 | case DropReason::APP_SWITCH: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1231 | ALOGI("Dropped event because of pending overdue app switch."); |
| 1232 | reason = "inbound event was dropped because of pending overdue app switch"; |
| 1233 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1234 | case DropReason::BLOCKED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1235 | ALOGI("Dropped event because the current application is not responding and the user " |
| 1236 | "has started interacting with a different application."); |
| 1237 | reason = "inbound event was dropped because the current application is not responding " |
| 1238 | "and the user has started interacting with a different application"; |
| 1239 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1240 | case DropReason::STALE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1241 | ALOGI("Dropped event because it is stale."); |
| 1242 | reason = "inbound event was dropped because it is stale"; |
| 1243 | break; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1244 | case DropReason::NO_POINTER_CAPTURE: |
| 1245 | ALOGI("Dropped event because there is no window with Pointer Capture."); |
| 1246 | reason = "inbound event was dropped because there is no window with Pointer Capture"; |
| 1247 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1248 | case DropReason::NOT_DROPPED: { |
| 1249 | LOG_ALWAYS_FATAL("Should not be dropping a NOT_DROPPED event"); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1250 | return; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1251 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1252 | } |
| 1253 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1254 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1255 | case EventEntry::Type::KEY: { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1256 | CancelationOptions options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, reason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1257 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1258 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1259 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1260 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1261 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1262 | if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1263 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, reason); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1264 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1265 | } else { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1266 | CancelationOptions options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, |
| 1267 | reason); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1268 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1269 | } |
| 1270 | break; |
| 1271 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1272 | case EventEntry::Type::SENSOR: { |
| 1273 | break; |
| 1274 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1275 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1276 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1277 | break; |
| 1278 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1279 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1280 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1281 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 1282 | case EventEntry::Type::DEVICE_RESET: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1283 | 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] | 1284 | break; |
| 1285 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1286 | } |
| 1287 | } |
| 1288 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1289 | static bool isAppSwitchKeyCode(int32_t keyCode) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1290 | return keyCode == AKEYCODE_HOME || keyCode == AKEYCODE_ENDCALL || |
| 1291 | keyCode == AKEYCODE_APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1292 | } |
| 1293 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1294 | bool InputDispatcher::isAppSwitchKeyEvent(const KeyEntry& keyEntry) { |
| 1295 | return !(keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) && isAppSwitchKeyCode(keyEntry.keyCode) && |
| 1296 | (keyEntry.policyFlags & POLICY_FLAG_TRUSTED) && |
| 1297 | (keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1298 | } |
| 1299 | |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 1300 | bool InputDispatcher::isAppSwitchPendingLocked() const { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 1301 | return mAppSwitchDueTime != LLONG_MAX; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1302 | } |
| 1303 | |
| 1304 | void InputDispatcher::resetPendingAppSwitchLocked(bool handled) { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 1305 | mAppSwitchDueTime = LLONG_MAX; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1306 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1307 | if (DEBUG_APP_SWITCH) { |
| 1308 | if (handled) { |
| 1309 | ALOGD("App switch has arrived."); |
| 1310 | } else { |
| 1311 | ALOGD("App switch was abandoned."); |
| 1312 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1313 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1314 | } |
| 1315 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1316 | bool InputDispatcher::haveCommandsLocked() const { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1317 | return !mCommandQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1318 | } |
| 1319 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1320 | bool InputDispatcher::runCommandsLockedInterruptable() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1321 | if (mCommandQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1322 | return false; |
| 1323 | } |
| 1324 | |
| 1325 | do { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1326 | auto command = std::move(mCommandQueue.front()); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1327 | mCommandQueue.pop_front(); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1328 | // Commands are run with the lock held, but may release and re-acquire the lock from within. |
| 1329 | command(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1330 | } while (!mCommandQueue.empty()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1331 | return true; |
| 1332 | } |
| 1333 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1334 | void InputDispatcher::postCommandLocked(Command&& command) { |
| 1335 | mCommandQueue.push_back(command); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1336 | } |
| 1337 | |
| 1338 | void InputDispatcher::drainInboundQueueLocked() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1339 | while (!mInboundQueue.empty()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1340 | std::shared_ptr<EventEntry> entry = mInboundQueue.front(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1341 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1342 | releaseInboundEventLocked(entry); |
| 1343 | } |
| 1344 | traceInboundQueueLengthLocked(); |
| 1345 | } |
| 1346 | |
| 1347 | void InputDispatcher::releasePendingEventLocked() { |
| 1348 | if (mPendingEvent) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1349 | releaseInboundEventLocked(mPendingEvent); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1350 | mPendingEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1351 | } |
| 1352 | } |
| 1353 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1354 | void InputDispatcher::releaseInboundEventLocked(std::shared_ptr<EventEntry> entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1355 | InjectionState* injectionState = entry->injectionState; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1356 | if (injectionState && injectionState->injectionResult == InputEventInjectionResult::PENDING) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1357 | if (DEBUG_DISPATCH_CYCLE) { |
| 1358 | ALOGD("Injected inbound event was dropped."); |
| 1359 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1360 | setInjectionResult(*entry, InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1361 | } |
| 1362 | if (entry == mNextUnblockedEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1363 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1364 | } |
| 1365 | addRecentEventLocked(entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1366 | } |
| 1367 | |
| 1368 | void InputDispatcher::resetKeyRepeatLocked() { |
| 1369 | if (mKeyRepeatState.lastKeyEntry) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1370 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1371 | } |
| 1372 | } |
| 1373 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1374 | std::shared_ptr<KeyEntry> InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) { |
| 1375 | std::shared_ptr<KeyEntry> entry = mKeyRepeatState.lastKeyEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1376 | |
Michael Wright | 2e73295 | 2014-09-24 13:26:59 -0700 | [diff] [blame] | 1377 | uint32_t policyFlags = entry->policyFlags & |
| 1378 | (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1379 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1380 | std::shared_ptr<KeyEntry> newEntry = |
| 1381 | std::make_unique<KeyEntry>(mIdGenerator.nextId(), currentTime, entry->deviceId, |
| 1382 | entry->source, entry->displayId, policyFlags, entry->action, |
| 1383 | entry->flags, entry->keyCode, entry->scanCode, |
| 1384 | entry->metaState, entry->repeatCount + 1, entry->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1385 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1386 | newEntry->syntheticRepeat = true; |
| 1387 | mKeyRepeatState.lastKeyEntry = newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1388 | mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1389 | return newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1390 | } |
| 1391 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1392 | bool InputDispatcher::dispatchConfigurationChangedLocked(nsecs_t currentTime, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1393 | const ConfigurationChangedEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1394 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1395 | ALOGD("dispatchConfigurationChanged - eventTime=%" PRId64, entry.eventTime); |
| 1396 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1397 | |
| 1398 | // Reset key repeating in case a keyboard device was added or removed or something. |
| 1399 | resetKeyRepeatLocked(); |
| 1400 | |
| 1401 | // 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] | 1402 | auto command = [this, eventTime = entry.eventTime]() REQUIRES(mLock) { |
| 1403 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame^] | 1404 | mPolicy.notifyConfigurationChanged(eventTime); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1405 | }; |
| 1406 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1407 | return true; |
| 1408 | } |
| 1409 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1410 | bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime, |
| 1411 | const DeviceResetEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1412 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1413 | ALOGD("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry.eventTime, |
| 1414 | entry.deviceId); |
| 1415 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1416 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 1417 | // Reset key repeating in case a keyboard device was disabled or enabled. |
| 1418 | if (mKeyRepeatState.lastKeyEntry && mKeyRepeatState.lastKeyEntry->deviceId == entry.deviceId) { |
| 1419 | resetKeyRepeatLocked(); |
| 1420 | } |
| 1421 | |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1422 | CancelationOptions options(CancelationOptions::Mode::CANCEL_ALL_EVENTS, "device was reset"); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1423 | options.deviceId = entry.deviceId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1424 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1425 | return true; |
| 1426 | } |
| 1427 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1428 | void InputDispatcher::enqueueFocusEventLocked(const sp<IBinder>& windowToken, bool hasFocus, |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1429 | const std::string& reason) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1430 | if (mPendingEvent != nullptr) { |
| 1431 | // Move the pending event to the front of the queue. This will give the chance |
| 1432 | // for the pending event to get dispatched to the newly focused window |
| 1433 | mInboundQueue.push_front(mPendingEvent); |
| 1434 | mPendingEvent = nullptr; |
| 1435 | } |
| 1436 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1437 | std::unique_ptr<FocusEntry> focusEntry = |
| 1438 | std::make_unique<FocusEntry>(mIdGenerator.nextId(), now(), windowToken, hasFocus, |
| 1439 | reason); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1440 | |
| 1441 | // This event should go to the front of the queue, but behind all other focus events |
| 1442 | // Find the last focus event, and insert right after it |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1443 | std::deque<std::shared_ptr<EventEntry>>::reverse_iterator it = |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1444 | std::find_if(mInboundQueue.rbegin(), mInboundQueue.rend(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1445 | [](const std::shared_ptr<EventEntry>& event) { |
| 1446 | return event->type == EventEntry::Type::FOCUS; |
| 1447 | }); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1448 | |
| 1449 | // 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] | 1450 | mInboundQueue.insert(it.base(), std::move(focusEntry)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1451 | } |
| 1452 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1453 | void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime, std::shared_ptr<FocusEntry> entry) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 1454 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1455 | if (channel == nullptr) { |
| 1456 | return; // Window has gone away |
| 1457 | } |
| 1458 | InputTarget target; |
| 1459 | target.inputChannel = channel; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 1460 | target.flags = InputTarget::Flags::DISPATCH_AS_IS; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1461 | entry->dispatchInProgress = true; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1462 | std::string message = std::string("Focus ") + (entry->hasFocus ? "entering " : "leaving ") + |
| 1463 | channel->getName(); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 1464 | std::string reason = std::string("reason=").append(entry->reason); |
| 1465 | android_log_event_list(LOGTAG_INPUT_FOCUS) << message << reason << LOG_ID_EVENTS; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1466 | dispatchEventLocked(currentTime, entry, {target}); |
| 1467 | } |
| 1468 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1469 | void InputDispatcher::dispatchPointerCaptureChangedLocked( |
| 1470 | nsecs_t currentTime, const std::shared_ptr<PointerCaptureChangedEntry>& entry, |
| 1471 | DropReason& dropReason) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1472 | dropReason = DropReason::NOT_DROPPED; |
| 1473 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1474 | const bool haveWindowWithPointerCapture = mWindowTokenWithPointerCapture != nullptr; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1475 | sp<IBinder> token; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1476 | |
| 1477 | if (entry->pointerCaptureRequest.enable) { |
| 1478 | // Enable Pointer Capture. |
| 1479 | if (haveWindowWithPointerCapture && |
| 1480 | (entry->pointerCaptureRequest == mCurrentPointerCaptureRequest)) { |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 1481 | // This can happen if pointer capture is disabled and re-enabled before we notify the |
| 1482 | // app of the state change, so there is no need to notify the app. |
| 1483 | ALOGI("Skipping dispatch of Pointer Capture being enabled: no state change."); |
| 1484 | return; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1485 | } |
| 1486 | if (!mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1487 | // This can happen if a window requests capture and immediately releases capture. |
| 1488 | ALOGW("No window requested Pointer Capture."); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1489 | dropReason = DropReason::NO_POINTER_CAPTURE; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1490 | return; |
| 1491 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1492 | if (entry->pointerCaptureRequest.seq != mCurrentPointerCaptureRequest.seq) { |
| 1493 | ALOGI("Skipping dispatch of Pointer Capture being enabled: sequence number mismatch."); |
| 1494 | return; |
| 1495 | } |
| 1496 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1497 | token = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1498 | LOG_ALWAYS_FATAL_IF(!token, "Cannot find focused window for Pointer Capture."); |
| 1499 | mWindowTokenWithPointerCapture = token; |
| 1500 | } else { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1501 | // Disable Pointer Capture. |
| 1502 | // We do not check if the sequence number matches for requests to disable Pointer Capture |
| 1503 | // for two reasons: |
| 1504 | // 1. Pointer Capture can be disabled by a focus change, which means we can get two entries |
| 1505 | // to disable capture with the same sequence number: one generated by |
| 1506 | // disablePointerCaptureForcedLocked() and another as an acknowledgement of Pointer |
| 1507 | // Capture being disabled in InputReader. |
| 1508 | // 2. We respect any request to disable Pointer Capture generated by InputReader, since the |
| 1509 | // actual Pointer Capture state that affects events being generated by input devices is |
| 1510 | // in InputReader. |
| 1511 | if (!haveWindowWithPointerCapture) { |
| 1512 | // Pointer capture was already forcefully disabled because of focus change. |
| 1513 | dropReason = DropReason::NOT_DROPPED; |
| 1514 | return; |
| 1515 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1516 | token = mWindowTokenWithPointerCapture; |
| 1517 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1518 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1519 | setPointerCaptureLocked(false); |
| 1520 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1521 | } |
| 1522 | |
| 1523 | auto channel = getInputChannelLocked(token); |
| 1524 | if (channel == nullptr) { |
| 1525 | // Window has gone away, clean up Pointer Capture state. |
| 1526 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1527 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1528 | setPointerCaptureLocked(false); |
| 1529 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1530 | return; |
| 1531 | } |
| 1532 | InputTarget target; |
| 1533 | target.inputChannel = channel; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 1534 | target.flags = InputTarget::Flags::DISPATCH_AS_IS; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1535 | entry->dispatchInProgress = true; |
| 1536 | dispatchEventLocked(currentTime, entry, {target}); |
| 1537 | |
| 1538 | dropReason = DropReason::NOT_DROPPED; |
| 1539 | } |
| 1540 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1541 | void InputDispatcher::dispatchTouchModeChangeLocked(nsecs_t currentTime, |
| 1542 | const std::shared_ptr<TouchModeEntry>& entry) { |
| 1543 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 1544 | getWindowHandlesLocked(entry->displayId); |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1545 | if (windowHandles.empty()) { |
| 1546 | return; |
| 1547 | } |
| 1548 | const std::vector<InputTarget> inputTargets = |
| 1549 | getInputTargetsFromWindowHandlesLocked(windowHandles); |
| 1550 | if (inputTargets.empty()) { |
| 1551 | return; |
| 1552 | } |
| 1553 | entry->dispatchInProgress = true; |
| 1554 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1555 | } |
| 1556 | |
| 1557 | std::vector<InputTarget> InputDispatcher::getInputTargetsFromWindowHandlesLocked( |
| 1558 | const std::vector<sp<WindowInfoHandle>>& windowHandles) const { |
| 1559 | std::vector<InputTarget> inputTargets; |
| 1560 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1561 | const sp<IBinder>& token = handle->getToken(); |
| 1562 | if (token == nullptr) { |
| 1563 | continue; |
| 1564 | } |
| 1565 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(token); |
| 1566 | if (channel == nullptr) { |
| 1567 | continue; // Window has gone away |
| 1568 | } |
| 1569 | InputTarget target; |
| 1570 | target.inputChannel = channel; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 1571 | target.flags = InputTarget::Flags::DISPATCH_AS_IS; |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1572 | inputTargets.push_back(target); |
| 1573 | } |
| 1574 | return inputTargets; |
| 1575 | } |
| 1576 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1577 | bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, std::shared_ptr<KeyEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1578 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1579 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1580 | if (!entry->dispatchInProgress) { |
| 1581 | if (entry->repeatCount == 0 && entry->action == AKEY_EVENT_ACTION_DOWN && |
| 1582 | (entry->policyFlags & POLICY_FLAG_TRUSTED) && |
| 1583 | (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { |
| 1584 | if (mKeyRepeatState.lastKeyEntry && |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1585 | mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode && |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1586 | // We have seen two identical key downs in a row which indicates that the device |
| 1587 | // driver is automatically generating key repeats itself. We take note of the |
| 1588 | // repeat here, but we disable our own next key repeat timer since it is clear that |
| 1589 | // we will not need to synthesize key repeats ourselves. |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1590 | mKeyRepeatState.lastKeyEntry->deviceId == entry->deviceId) { |
| 1591 | // Make sure we don't get key down from a different device. If a different |
| 1592 | // device Id has same key pressed down, the new device Id will replace the |
| 1593 | // current one to hold the key repeat with repeat count reset. |
| 1594 | // In the future when got a KEY_UP on the device id, drop it and do not |
| 1595 | // stop the key repeat on current device. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1596 | entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; |
| 1597 | resetKeyRepeatLocked(); |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 1598 | mKeyRepeatState.nextRepeatTime = LLONG_MAX; // don't generate repeats ourselves |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1599 | } else { |
| 1600 | // Not a repeat. Save key down state in case we do see a repeat later. |
| 1601 | resetKeyRepeatLocked(); |
| 1602 | mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; |
| 1603 | } |
| 1604 | mKeyRepeatState.lastKeyEntry = entry; |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1605 | } else if (entry->action == AKEY_EVENT_ACTION_UP && mKeyRepeatState.lastKeyEntry && |
| 1606 | mKeyRepeatState.lastKeyEntry->deviceId != entry->deviceId) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1607 | // 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] | 1608 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1609 | ALOGD("deviceId=%d got KEY_UP as stale", entry->deviceId); |
| 1610 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1611 | } else if (!entry->syntheticRepeat) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1612 | resetKeyRepeatLocked(); |
| 1613 | } |
| 1614 | |
| 1615 | if (entry->repeatCount == 1) { |
| 1616 | entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; |
| 1617 | } else { |
| 1618 | entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; |
| 1619 | } |
| 1620 | |
| 1621 | entry->dispatchInProgress = true; |
| 1622 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1623 | logOutboundKeyDetails("dispatchKey - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1624 | } |
| 1625 | |
| 1626 | // 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] | 1627 | if (entry->interceptKeyResult == KeyEntry::InterceptKeyResult::TRY_AGAIN_LATER) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1628 | if (currentTime < entry->interceptKeyWakeupTime) { |
| 1629 | if (entry->interceptKeyWakeupTime < *nextWakeupTime) { |
| 1630 | *nextWakeupTime = entry->interceptKeyWakeupTime; |
| 1631 | } |
| 1632 | return false; // wait until next wakeup |
| 1633 | } |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1634 | entry->interceptKeyResult = KeyEntry::InterceptKeyResult::UNKNOWN; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1635 | entry->interceptKeyWakeupTime = 0; |
| 1636 | } |
| 1637 | |
| 1638 | // Give the policy a chance to intercept the key. |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1639 | if (entry->interceptKeyResult == KeyEntry::InterceptKeyResult::UNKNOWN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1640 | if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1641 | sp<IBinder> focusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1642 | mFocusResolver.getFocusedWindowToken(getTargetDisplayId(*entry)); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1643 | |
| 1644 | auto command = [this, focusedWindowToken, entry]() REQUIRES(mLock) { |
| 1645 | doInterceptKeyBeforeDispatchingCommand(focusedWindowToken, *entry); |
| 1646 | }; |
| 1647 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1648 | return false; // wait for the command to run |
| 1649 | } else { |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1650 | entry->interceptKeyResult = KeyEntry::InterceptKeyResult::CONTINUE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1651 | } |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1652 | } else if (entry->interceptKeyResult == KeyEntry::InterceptKeyResult::SKIP) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1653 | if (*dropReason == DropReason::NOT_DROPPED) { |
| 1654 | *dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1655 | } |
| 1656 | } |
| 1657 | |
| 1658 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1659 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1660 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1661 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1662 | : InputEventInjectionResult::FAILED); |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1663 | mReporter->reportDroppedKey(entry->id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1664 | return true; |
| 1665 | } |
| 1666 | |
| 1667 | // Identify targets. |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1668 | InputEventInjectionResult injectionResult; |
| 1669 | sp<WindowInfoHandle> focusedWindow = |
| 1670 | findFocusedWindowTargetLocked(currentTime, *entry, nextWakeupTime, |
| 1671 | /*byref*/ injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1672 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1673 | return false; |
| 1674 | } |
| 1675 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1676 | setInjectionResult(*entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1677 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1678 | return true; |
| 1679 | } |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1680 | LOG_ALWAYS_FATAL_IF(focusedWindow == nullptr); |
| 1681 | |
| 1682 | std::vector<InputTarget> inputTargets; |
| 1683 | addWindowTargetLocked(focusedWindow, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 1684 | InputTarget::Flags::FOREGROUND | InputTarget::Flags::DISPATCH_AS_IS, |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 1685 | /*pointerIds=*/{}, getDownTime(*entry), inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1686 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1687 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1688 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1689 | |
| 1690 | // Dispatch the key. |
| 1691 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1692 | return true; |
| 1693 | } |
| 1694 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1695 | void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1696 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1697 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 ", " |
| 1698 | "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, " |
| 1699 | "metaState=0x%x, repeatCount=%d, downTime=%" PRId64, |
| 1700 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, |
| 1701 | entry.policyFlags, entry.action, entry.flags, entry.keyCode, entry.scanCode, |
| 1702 | entry.metaState, entry.repeatCount, entry.downTime); |
| 1703 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1704 | } |
| 1705 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1706 | void InputDispatcher::dispatchSensorLocked(nsecs_t currentTime, |
| 1707 | const std::shared_ptr<SensorEntry>& entry, |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1708 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1709 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1710 | ALOGD("notifySensorEvent eventTime=%" PRId64 ", hwTimestamp=%" PRId64 ", deviceId=%d, " |
| 1711 | "source=0x%x, sensorType=%s", |
| 1712 | entry->eventTime, entry->hwTimestamp, entry->deviceId, entry->source, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1713 | ftl::enum_string(entry->sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1714 | } |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1715 | auto command = [this, entry]() REQUIRES(mLock) { |
| 1716 | scoped_unlock unlock(mLock); |
| 1717 | |
| 1718 | if (entry->accuracyChanged) { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame^] | 1719 | mPolicy.notifySensorAccuracy(entry->deviceId, entry->sensorType, entry->accuracy); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1720 | } |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame^] | 1721 | mPolicy.notifySensorEvent(entry->deviceId, entry->sensorType, entry->accuracy, |
| 1722 | entry->hwTimestamp, entry->values); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1723 | }; |
| 1724 | postCommandLocked(std::move(command)); |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1725 | } |
| 1726 | |
| 1727 | bool InputDispatcher::flushSensor(int deviceId, InputDeviceSensorType sensorType) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1728 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1729 | ALOGD("flushSensor deviceId=%d, sensorType=%s", deviceId, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1730 | ftl::enum_string(sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1731 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1732 | { // acquire lock |
| 1733 | std::scoped_lock _l(mLock); |
| 1734 | |
| 1735 | for (auto it = mInboundQueue.begin(); it != mInboundQueue.end(); it++) { |
| 1736 | std::shared_ptr<EventEntry> entry = *it; |
| 1737 | if (entry->type == EventEntry::Type::SENSOR) { |
| 1738 | it = mInboundQueue.erase(it); |
| 1739 | releaseInboundEventLocked(entry); |
| 1740 | } |
| 1741 | } |
| 1742 | } |
| 1743 | return true; |
| 1744 | } |
| 1745 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1746 | bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, std::shared_ptr<MotionEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1747 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1748 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1749 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1750 | if (!entry->dispatchInProgress) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1751 | entry->dispatchInProgress = true; |
| 1752 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1753 | logOutboundMotionDetails("dispatchMotion - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1754 | } |
| 1755 | |
| 1756 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1757 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1758 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1759 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1760 | : InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1761 | return true; |
| 1762 | } |
| 1763 | |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 1764 | const bool isPointerEvent = isFromSource(entry->source, AINPUT_SOURCE_CLASS_POINTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1765 | |
| 1766 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1767 | std::vector<InputTarget> inputTargets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1768 | |
| 1769 | bool conflictingPointerActions = false; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1770 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1771 | if (isPointerEvent) { |
| 1772 | // Pointer event. (eg. touchscreen) |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 1773 | |
| 1774 | if (mDragState && |
| 1775 | (entry->action & AMOTION_EVENT_ACTION_MASK) == AMOTION_EVENT_ACTION_POINTER_DOWN) { |
| 1776 | // If drag and drop ongoing and pointer down occur: pilfer drag window pointers |
| 1777 | pilferPointersLocked(mDragState->dragWindow->getToken()); |
| 1778 | } |
| 1779 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1780 | inputTargets = |
Siarhei Vishniakou | 4fe5739 | 2022-10-25 13:44:30 -0700 | [diff] [blame] | 1781 | findTouchedWindowTargetsLocked(currentTime, *entry, &conflictingPointerActions, |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1782 | /*byref*/ injectionResult); |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 1783 | LOG_ALWAYS_FATAL_IF(injectionResult != InputEventInjectionResult::SUCCEEDED && |
| 1784 | !inputTargets.empty()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1785 | } else { |
| 1786 | // Non touch event. (eg. trackball) |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1787 | sp<WindowInfoHandle> focusedWindow = |
| 1788 | findFocusedWindowTargetLocked(currentTime, *entry, nextWakeupTime, injectionResult); |
| 1789 | if (injectionResult == InputEventInjectionResult::SUCCEEDED) { |
| 1790 | LOG_ALWAYS_FATAL_IF(focusedWindow == nullptr); |
| 1791 | addWindowTargetLocked(focusedWindow, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 1792 | InputTarget::Flags::FOREGROUND | |
| 1793 | InputTarget::Flags::DISPATCH_AS_IS, |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 1794 | /*pointerIds=*/{}, getDownTime(*entry), inputTargets); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1795 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1796 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1797 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1798 | return false; |
| 1799 | } |
| 1800 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1801 | setInjectionResult(*entry, injectionResult); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1802 | if (injectionResult == InputEventInjectionResult::TARGET_MISMATCH) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1803 | return true; |
| 1804 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1805 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1806 | CancelationOptions::Mode mode( |
| 1807 | isPointerEvent ? CancelationOptions::Mode::CANCEL_POINTER_EVENTS |
| 1808 | : CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS); |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1809 | CancelationOptions options(mode, "input event injection failed"); |
| 1810 | synthesizeCancelationEventsForMonitorsLocked(options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1811 | return true; |
| 1812 | } |
| 1813 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1814 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1815 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1816 | |
| 1817 | // Dispatch the motion. |
| 1818 | if (conflictingPointerActions) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1819 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1820 | "conflicting pointer actions"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1821 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1822 | } |
| 1823 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1824 | return true; |
| 1825 | } |
| 1826 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1827 | void InputDispatcher::enqueueDragEventLocked(const sp<WindowInfoHandle>& windowHandle, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 1828 | bool isExiting, const int32_t rawX, |
| 1829 | const int32_t rawY) { |
| 1830 | const vec2 xy = windowHandle->getInfo()->transform.transform(vec2(rawX, rawY)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1831 | std::unique_ptr<DragEntry> dragEntry = |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 1832 | std::make_unique<DragEntry>(mIdGenerator.nextId(), now(), windowHandle->getToken(), |
| 1833 | isExiting, xy.x, xy.y); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1834 | |
| 1835 | enqueueInboundEventLocked(std::move(dragEntry)); |
| 1836 | } |
| 1837 | |
| 1838 | void InputDispatcher::dispatchDragLocked(nsecs_t currentTime, std::shared_ptr<DragEntry> entry) { |
| 1839 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
| 1840 | if (channel == nullptr) { |
| 1841 | return; // Window has gone away |
| 1842 | } |
| 1843 | InputTarget target; |
| 1844 | target.inputChannel = channel; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 1845 | target.flags = InputTarget::Flags::DISPATCH_AS_IS; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1846 | entry->dispatchInProgress = true; |
| 1847 | dispatchEventLocked(currentTime, entry, {target}); |
| 1848 | } |
| 1849 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1850 | void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1851 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 1852 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=%s, displayId=%" PRId32 |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1853 | ", policyFlags=0x%x, " |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1854 | "action=%s, actionButton=0x%x, flags=0x%x, " |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1855 | "metaState=0x%x, buttonState=0x%x," |
| 1856 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64, |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 1857 | prefix, entry.eventTime, entry.deviceId, |
| 1858 | inputEventSourceToString(entry.source).c_str(), entry.displayId, entry.policyFlags, |
| 1859 | MotionEvent::actionToString(entry.action).c_str(), entry.actionButton, entry.flags, |
| 1860 | entry.metaState, entry.buttonState, entry.edgeFlags, entry.xPrecision, |
| 1861 | entry.yPrecision, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1862 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1863 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1864 | ALOGD(" Pointer %d: id=%d, toolType=%s, " |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1865 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 1866 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 1867 | "orientation=%f", |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1868 | i, entry.pointerProperties[i].id, |
| 1869 | ftl::enum_string(entry.pointerProperties[i].toolType).c_str(), |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1870 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 1871 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 1872 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 1873 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 1874 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 1875 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 1876 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 1877 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 1878 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| 1879 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1880 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1881 | } |
| 1882 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1883 | void InputDispatcher::dispatchEventLocked(nsecs_t currentTime, |
| 1884 | std::shared_ptr<EventEntry> eventEntry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1885 | const std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1886 | ATRACE_CALL(); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1887 | if (DEBUG_DISPATCH_CYCLE) { |
| 1888 | ALOGD("dispatchEventToCurrentInputTargets"); |
| 1889 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1890 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1891 | updateInteractionTokensLocked(*eventEntry, inputTargets); |
| 1892 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1893 | ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true |
| 1894 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1895 | pokeUserActivityLocked(*eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1896 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1897 | for (const InputTarget& inputTarget : inputTargets) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 1898 | std::shared_ptr<Connection> connection = |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 1899 | getConnectionLocked(inputTarget.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 1900 | if (connection != nullptr) { |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 1901 | prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1902 | } else { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1903 | if (DEBUG_FOCUS) { |
| 1904 | ALOGD("Dropping event delivery to target with channel '%s' because it " |
| 1905 | "is no longer registered with the input dispatcher.", |
| 1906 | inputTarget.inputChannel->getName().c_str()); |
| 1907 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1908 | } |
| 1909 | } |
| 1910 | } |
| 1911 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 1912 | void InputDispatcher::cancelEventsForAnrLocked(const std::shared_ptr<Connection>& connection) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1913 | // We will not be breaking any connections here, even if the policy wants us to abort dispatch. |
| 1914 | // If the policy decides to close the app, we will get a channel removal event via |
| 1915 | // unregisterInputChannel, and will clean up the connection that way. We are already not |
| 1916 | // sending new pointers to the connection when it blocked, but focused events will continue to |
| 1917 | // pile up. |
| 1918 | ALOGW("Canceling events for %s because it is unresponsive", |
| 1919 | connection->inputChannel->getName().c_str()); |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 1920 | if (connection->status == Connection::Status::NORMAL) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1921 | CancelationOptions options(CancelationOptions::Mode::CANCEL_ALL_EVENTS, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1922 | "application not responding"); |
| 1923 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1924 | } |
| 1925 | } |
| 1926 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1927 | void InputDispatcher::resetNoFocusedWindowTimeoutLocked() { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1928 | if (DEBUG_FOCUS) { |
| 1929 | ALOGD("Resetting ANR timeouts."); |
| 1930 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1931 | |
| 1932 | // Reset input target wait timeout. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1933 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1934 | mAwaitedFocusedApplication.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1935 | } |
| 1936 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1937 | /** |
| 1938 | * Get the display id that the given event should go to. If this event specifies a valid display id, |
| 1939 | * then it should be dispatched to that display. Otherwise, the event goes to the focused display. |
| 1940 | * Focused display is the display that the user most recently interacted with. |
| 1941 | */ |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1942 | int32_t InputDispatcher::getTargetDisplayId(const EventEntry& entry) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1943 | int32_t displayId; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1944 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1945 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1946 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 1947 | displayId = keyEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1948 | break; |
| 1949 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1950 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1951 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1952 | displayId = motionEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1953 | break; |
| 1954 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1955 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1956 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1957 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1958 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1959 | case EventEntry::Type::DEVICE_RESET: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1960 | case EventEntry::Type::SENSOR: |
| 1961 | case EventEntry::Type::DRAG: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1962 | 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] | 1963 | return ADISPLAY_ID_NONE; |
| 1964 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1965 | } |
| 1966 | return displayId == ADISPLAY_ID_NONE ? mFocusedDisplayId : displayId; |
| 1967 | } |
| 1968 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1969 | bool InputDispatcher::shouldWaitToSendKeyLocked(nsecs_t currentTime, |
| 1970 | const char* focusedWindowName) { |
| 1971 | if (mAnrTracker.empty()) { |
| 1972 | // already processed all events that we waited for |
| 1973 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1974 | return false; |
| 1975 | } |
| 1976 | |
| 1977 | if (!mKeyIsWaitingForEventsTimeout.has_value()) { |
| 1978 | // Start the timer |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 1979 | // 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] | 1980 | mKeyIsWaitingForEventsTimeout = currentTime + |
| 1981 | std::chrono::duration_cast<std::chrono::nanoseconds>(KEY_WAITING_FOR_EVENTS_TIMEOUT) |
| 1982 | .count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1983 | return true; |
| 1984 | } |
| 1985 | |
| 1986 | // We still have pending events, and already started the timer |
| 1987 | if (currentTime < *mKeyIsWaitingForEventsTimeout) { |
| 1988 | return true; // Still waiting |
| 1989 | } |
| 1990 | |
| 1991 | // Waited too long, and some connection still hasn't processed all motions |
| 1992 | // Just send the key to the focused window |
| 1993 | ALOGW("Dispatching key to %s even though there are other unprocessed events", |
| 1994 | focusedWindowName); |
| 1995 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1996 | return false; |
| 1997 | } |
| 1998 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1999 | sp<WindowInfoHandle> InputDispatcher::findFocusedWindowTargetLocked( |
| 2000 | nsecs_t currentTime, const EventEntry& entry, nsecs_t* nextWakeupTime, |
| 2001 | InputEventInjectionResult& outInjectionResult) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2002 | std::string reason; |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2003 | outInjectionResult = InputEventInjectionResult::FAILED; // Default result |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2004 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2005 | int32_t displayId = getTargetDisplayId(entry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2006 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2007 | std::shared_ptr<InputApplicationHandle> focusedApplicationHandle = |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2008 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 2009 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2010 | // If there is no currently focused window and no focused application |
| 2011 | // then drop the event. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2012 | if (focusedWindowHandle == nullptr && focusedApplicationHandle == nullptr) { |
| 2013 | ALOGI("Dropping %s event because there is no focused window or focused application in " |
| 2014 | "display %" PRId32 ".", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2015 | ftl::enum_string(entry.type).c_str(), displayId); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2016 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2017 | } |
| 2018 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2019 | // Drop key events if requested by input feature |
| 2020 | if (focusedWindowHandle != nullptr && shouldDropInput(entry, focusedWindowHandle)) { |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2021 | return nullptr; |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2022 | } |
| 2023 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2024 | // Compatibility behavior: raise ANR if there is a focused application, but no focused window. |
| 2025 | // Only start counting when we have a focused event to dispatch. The ANR is canceled if we |
| 2026 | // start interacting with another application via touch (app switch). This code can be removed |
| 2027 | // if the "no focused window ANR" is moved to the policy. Input doesn't know whether |
| 2028 | // an app is expected to have a focused window. |
| 2029 | if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) { |
| 2030 | if (!mNoFocusedWindowTimeoutTime.has_value()) { |
| 2031 | // 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] | 2032 | std::chrono::nanoseconds timeout = focusedApplicationHandle->getDispatchingTimeout( |
| 2033 | DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
| 2034 | mNoFocusedWindowTimeoutTime = currentTime + timeout.count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2035 | mAwaitedFocusedApplication = focusedApplicationHandle; |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 2036 | mAwaitedApplicationDisplayId = displayId; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2037 | ALOGW("Waiting because no window has focus but %s may eventually add a " |
| 2038 | "window when it finishes starting up. Will wait for %" PRId64 "ms", |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 2039 | mAwaitedFocusedApplication->getName().c_str(), millis(timeout)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2040 | *nextWakeupTime = *mNoFocusedWindowTimeoutTime; |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2041 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 2042 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2043 | } else if (currentTime > *mNoFocusedWindowTimeoutTime) { |
| 2044 | // Already raised ANR. Drop the event |
| 2045 | ALOGE("Dropping %s event because there is no focused window", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2046 | ftl::enum_string(entry.type).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2047 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2048 | } else { |
| 2049 | // Still waiting for the focused window |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2050 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 2051 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2052 | } |
| 2053 | } |
| 2054 | |
| 2055 | // we have a valid, non-null focused window |
| 2056 | resetNoFocusedWindowTimeoutLocked(); |
| 2057 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2058 | // Verify targeted injection. |
| 2059 | if (const auto err = verifyTargetedInjection(focusedWindowHandle, entry); err) { |
| 2060 | ALOGW("Dropping injected event: %s", (*err).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2061 | outInjectionResult = InputEventInjectionResult::TARGET_MISMATCH; |
| 2062 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2063 | } |
| 2064 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2065 | if (focusedWindowHandle->getInfo()->inputConfig.test( |
| 2066 | WindowInfo::InputConfig::PAUSE_DISPATCHING)) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2067 | ALOGI("Waiting because %s is paused", focusedWindowHandle->getName().c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2068 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 2069 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2070 | } |
| 2071 | |
| 2072 | // If the event is a key event, then we must wait for all previous events to |
| 2073 | // complete before delivering it because previous events may have the |
| 2074 | // side-effect of transferring focus to a different window and we want to |
| 2075 | // ensure that the following keys are sent to the new window. |
| 2076 | // |
| 2077 | // Suppose the user touches a button in a window then immediately presses "A". |
| 2078 | // If the button causes a pop-up window to appear then we want to ensure that |
| 2079 | // the "A" key is delivered to the new pop-up window. This is because users |
| 2080 | // often anticipate pending UI changes when typing on a keyboard. |
| 2081 | // To obtain this behavior, we must serialize key events with respect to all |
| 2082 | // prior input events. |
| 2083 | if (entry.type == EventEntry::Type::KEY) { |
| 2084 | if (shouldWaitToSendKeyLocked(currentTime, focusedWindowHandle->getName().c_str())) { |
| 2085 | *nextWakeupTime = *mKeyIsWaitingForEventsTimeout; |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2086 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 2087 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2088 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2089 | } |
| 2090 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2091 | outInjectionResult = InputEventInjectionResult::SUCCEEDED; |
| 2092 | return focusedWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2093 | } |
| 2094 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2095 | /** |
| 2096 | * Given a list of monitors, remove the ones we cannot find a connection for, and the ones |
| 2097 | * that are currently unresponsive. |
| 2098 | */ |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2099 | std::vector<Monitor> InputDispatcher::selectResponsiveMonitorsLocked( |
| 2100 | const std::vector<Monitor>& monitors) const { |
| 2101 | std::vector<Monitor> responsiveMonitors; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2102 | std::copy_if(monitors.begin(), monitors.end(), std::back_inserter(responsiveMonitors), |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2103 | [this](const Monitor& monitor) REQUIRES(mLock) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 2104 | std::shared_ptr<Connection> connection = |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2105 | getConnectionLocked(monitor.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2106 | if (connection == nullptr) { |
| 2107 | ALOGE("Could not find connection for monitor %s", |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2108 | monitor.inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2109 | return false; |
| 2110 | } |
| 2111 | if (!connection->responsive) { |
| 2112 | ALOGW("Unresponsive monitor %s will not get the new gesture", |
| 2113 | connection->inputChannel->getName().c_str()); |
| 2114 | return false; |
| 2115 | } |
| 2116 | return true; |
| 2117 | }); |
| 2118 | return responsiveMonitors; |
| 2119 | } |
| 2120 | |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 2121 | /** |
| 2122 | * In general, touch should be always split between windows. Some exceptions: |
| 2123 | * 1. Don't split touch is if we have an active pointer down, and a new pointer is going down that's |
| 2124 | * from the same device, *and* the window that's receiving the current pointer does not support |
| 2125 | * split touch. |
| 2126 | * 2. Don't split mouse events |
| 2127 | */ |
| 2128 | bool InputDispatcher::shouldSplitTouch(const TouchState& touchState, |
| 2129 | const MotionEntry& entry) const { |
| 2130 | if (isFromSource(entry.source, AINPUT_SOURCE_MOUSE)) { |
| 2131 | // We should never split mouse events |
| 2132 | return false; |
| 2133 | } |
| 2134 | for (const TouchedWindow& touchedWindow : touchState.windows) { |
| 2135 | if (touchedWindow.windowHandle->getInfo()->isSpy()) { |
| 2136 | // Spy windows should not affect whether or not touch is split. |
| 2137 | continue; |
| 2138 | } |
| 2139 | if (touchedWindow.windowHandle->getInfo()->supportsSplitTouch()) { |
| 2140 | continue; |
| 2141 | } |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2142 | if (touchedWindow.windowHandle->getInfo()->inputConfig.test( |
| 2143 | gui::WindowInfo::InputConfig::IS_WALLPAPER)) { |
| 2144 | // Wallpaper window should not affect whether or not touch is split |
| 2145 | continue; |
| 2146 | } |
| 2147 | |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 2148 | // Eventually, touchedWindow will contain the deviceId of each pointer that's currently |
| 2149 | // being sent there. For now, use deviceId from touch state. |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 2150 | if (entry.deviceId == touchState.deviceId && touchedWindow.pointerIds.any()) { |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 2151 | return false; |
| 2152 | } |
| 2153 | } |
| 2154 | return true; |
| 2155 | } |
| 2156 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2157 | std::vector<InputTarget> InputDispatcher::findTouchedWindowTargetsLocked( |
Siarhei Vishniakou | 4fe5739 | 2022-10-25 13:44:30 -0700 | [diff] [blame] | 2158 | nsecs_t currentTime, const MotionEntry& entry, bool* outConflictingPointerActions, |
| 2159 | InputEventInjectionResult& outInjectionResult) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2160 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2161 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2162 | std::vector<InputTarget> targets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2163 | // For security reasons, we defer updating the touch state until we are sure that |
| 2164 | // event injection will be allowed. |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2165 | const int32_t displayId = entry.displayId; |
| 2166 | const int32_t action = entry.action; |
| 2167 | const int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2168 | |
| 2169 | // 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] | 2170 | outInjectionResult = InputEventInjectionResult::PENDING; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2171 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2172 | // Copy current touch state into tempTouchState. |
| 2173 | // This state will be used to update mTouchStatesByDisplay at the end of this function. |
| 2174 | // 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] | 2175 | const TouchState* oldState = nullptr; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2176 | TouchState tempTouchState; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2177 | if (const auto it = mTouchStatesByDisplay.find(displayId); it != mTouchStatesByDisplay.end()) { |
| 2178 | oldState = &(it->second); |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 2179 | tempTouchState = *oldState; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2180 | } |
| 2181 | |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 2182 | bool isSplit = shouldSplitTouch(tempTouchState, entry); |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 2183 | const bool switchedDevice = (oldState != nullptr) && |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2184 | (oldState->deviceId != entry.deviceId || oldState->source != entry.source); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2185 | |
| 2186 | const bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE || |
| 2187 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2188 | maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2189 | // A DOWN could be generated from POINTER_DOWN if the initial pointers did not land into any |
| 2190 | // touchable windows. |
| 2191 | const bool wasDown = oldState != nullptr && oldState->isDown(); |
| 2192 | const bool isDown = (maskedAction == AMOTION_EVENT_ACTION_DOWN) || |
| 2193 | (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN && !wasDown); |
| 2194 | const bool newGesture = isDown || maskedAction == AMOTION_EVENT_ACTION_SCROLL || isHoverAction; |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 2195 | const bool isFromMouse = isFromSource(entry.source, AINPUT_SOURCE_MOUSE); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2196 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2197 | // If pointers are already down, let's finish the current gesture and ignore the new events |
| 2198 | // from another device. However, if the new event is a down event, let's cancel the current |
| 2199 | // touch and let the new one take over. |
| 2200 | if (switchedDevice && wasDown && !isDown) { |
| 2201 | LOG(INFO) << "Dropping event because a pointer for device " << oldState->deviceId |
| 2202 | << " is already down in display " << displayId << ": " << entry.getDescription(); |
| 2203 | // TODO(b/211379801): test multiple simultaneous input streams. |
| 2204 | outInjectionResult = InputEventInjectionResult::FAILED; |
| 2205 | return {}; // wrong device |
| 2206 | } |
| 2207 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2208 | if (newGesture) { |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2209 | // If a new gesture is starting, clear the touch state completely. |
| 2210 | tempTouchState.reset(); |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2211 | tempTouchState.deviceId = entry.deviceId; |
| 2212 | tempTouchState.source = entry.source; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2213 | isSplit = false; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2214 | } else if (switchedDevice && maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2215 | ALOGI("Dropping move event because a pointer for a different device is already active " |
| 2216 | "in display %" PRId32, |
| 2217 | displayId); |
Siarhei Vishniakou | 16e4fa0 | 2023-02-16 17:48:56 -0800 | [diff] [blame] | 2218 | // TODO(b/211379801): test multiple simultaneous input streams. |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2219 | outInjectionResult = InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2220 | return {}; // wrong device |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2221 | } |
| 2222 | |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2223 | if (isHoverAction) { |
| 2224 | // For hover actions, we will treat 'tempTouchState' as a new state, so let's erase |
| 2225 | // all of the existing hovering pointers and recompute. |
| 2226 | tempTouchState.clearHoveringPointers(); |
| 2227 | } |
| 2228 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2229 | if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { |
| 2230 | /* 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] | 2231 | const auto [x, y] = resolveTouchedPosition(entry); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2232 | const int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2233 | // Outside targets should be added upon first dispatched DOWN event. That means, this should |
| 2234 | // 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] | 2235 | const bool isStylus = isPointerFromStylus(entry, pointerIndex); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2236 | auto [newTouchedWindowHandle, outsideTargets] = |
| 2237 | findTouchedWindowAtLocked(displayId, x, y, isStylus); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2238 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2239 | if (isDown) { |
| 2240 | targets += outsideTargets; |
| 2241 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2242 | // Handle the case where we did not find a window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2243 | if (newTouchedWindowHandle == nullptr) { |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 2244 | 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] | 2245 | // 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] | 2246 | newTouchedWindowHandle = tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2247 | } |
| 2248 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2249 | // Verify targeted injection. |
| 2250 | if (const auto err = verifyTargetedInjection(newTouchedWindowHandle, entry); err) { |
| 2251 | ALOGW("Dropping injected touch event: %s", (*err).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2252 | outInjectionResult = os::InputEventInjectionResult::TARGET_MISMATCH; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2253 | newTouchedWindowHandle = nullptr; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2254 | return {}; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2255 | } |
| 2256 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2257 | // Figure out whether splitting will be allowed for this window. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2258 | if (newTouchedWindowHandle != nullptr) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2259 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
| 2260 | // New window supports splitting, but we should never split mouse events. |
| 2261 | isSplit = !isFromMouse; |
| 2262 | } else if (isSplit) { |
| 2263 | // New window does not support splitting but we have already split events. |
| 2264 | // Ignore the new window. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2265 | newTouchedWindowHandle = nullptr; |
| 2266 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2267 | } else { |
| 2268 | // 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] | 2269 | // be delivered to a new window which supports split touch. Pointers from a mouse device |
| 2270 | // should never be split. |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 2271 | isSplit = !isFromMouse; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2272 | } |
| 2273 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2274 | std::vector<sp<WindowInfoHandle>> newTouchedWindows = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2275 | findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2276 | if (newTouchedWindowHandle != nullptr) { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2277 | // Process the foreground window first so that it is the first to receive the event. |
| 2278 | newTouchedWindows.insert(newTouchedWindows.begin(), newTouchedWindowHandle); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2279 | } |
| 2280 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2281 | if (newTouchedWindows.empty()) { |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 2282 | ALOGI("Dropping event because there is no touchable window at (%.1f, %.1f) on display " |
| 2283 | "%d.", |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2284 | x, y, displayId); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2285 | outInjectionResult = InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2286 | return {}; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2287 | } |
| 2288 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2289 | for (const sp<WindowInfoHandle>& windowHandle : newTouchedWindows) { |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 2290 | if (!canWindowReceiveMotionLocked(windowHandle, entry)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2291 | continue; |
| 2292 | } |
| 2293 | |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2294 | if (isHoverAction) { |
| 2295 | const int32_t pointerId = entry.pointerProperties[0].id; |
| 2296 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 2297 | // Pointer left. Remove it |
| 2298 | tempTouchState.removeHoveringPointer(entry.deviceId, pointerId); |
| 2299 | } else { |
| 2300 | // The "windowHandle" is the target of this hovering pointer. |
| 2301 | tempTouchState.addHoveringPointerToWindow(windowHandle, entry.deviceId, |
| 2302 | pointerId); |
| 2303 | } |
| 2304 | } |
| 2305 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2306 | // Set target flags. |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2307 | ftl::Flags<InputTarget::Flags> targetFlags = InputTarget::Flags::DISPATCH_AS_IS; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2308 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2309 | if (canReceiveForegroundTouches(*windowHandle->getInfo())) { |
| 2310 | // 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] | 2311 | targetFlags |= InputTarget::Flags::FOREGROUND; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2312 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2313 | |
| 2314 | if (isSplit) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2315 | targetFlags |= InputTarget::Flags::SPLIT; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2316 | } |
| 2317 | if (isWindowObscuredAtPointLocked(windowHandle, x, y)) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2318 | targetFlags |= InputTarget::Flags::WINDOW_IS_OBSCURED; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2319 | } else if (isWindowObscuredLocked(windowHandle)) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2320 | targetFlags |= InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2321 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2322 | |
| 2323 | // Update the temporary touch state. |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 2324 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2325 | if (!isHoverAction) { |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 2326 | pointerIds.set(entry.pointerProperties[pointerIndex].id); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2327 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2328 | |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2329 | const bool isDownOrPointerDown = maskedAction == AMOTION_EVENT_ACTION_DOWN || |
| 2330 | maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN; |
| 2331 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2332 | // TODO(b/211379801): Currently, even if pointerIds are empty (hover case), we would |
| 2333 | // still add a window to the touch state. We should avoid doing that, but some of the |
| 2334 | // later checks ("at least one foreground window") rely on this in order to dispatch |
| 2335 | // the event properly, so that needs to be updated, possibly by looking at InputTargets. |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2336 | tempTouchState.addOrUpdateWindow(windowHandle, targetFlags, pointerIds, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2337 | isDownOrPointerDown |
| 2338 | ? std::make_optional(entry.eventTime) |
| 2339 | : std::nullopt); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2340 | |
| 2341 | // If this is the pointer going down and the touched window has a wallpaper |
| 2342 | // then also add the touched wallpaper windows so they are locked in for the duration |
| 2343 | // of the touch gesture. |
| 2344 | // We do not collect wallpapers during HOVER_MOVE or SCROLL because the wallpaper |
| 2345 | // engine only supports touch events. We would need to add a mechanism similar |
| 2346 | // to View.onGenericMotionEvent to enable wallpapers to handle these events. |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2347 | if (isDownOrPointerDown) { |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2348 | if (targetFlags.test(InputTarget::Flags::FOREGROUND) && |
| 2349 | windowHandle->getInfo()->inputConfig.test( |
| 2350 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER)) { |
| 2351 | sp<WindowInfoHandle> wallpaper = findWallpaperWindowBelow(windowHandle); |
| 2352 | if (wallpaper != nullptr) { |
| 2353 | ftl::Flags<InputTarget::Flags> wallpaperFlags = |
| 2354 | InputTarget::Flags::WINDOW_IS_OBSCURED | |
| 2355 | InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED | |
| 2356 | InputTarget::Flags::DISPATCH_AS_IS; |
| 2357 | if (isSplit) { |
| 2358 | wallpaperFlags |= InputTarget::Flags::SPLIT; |
| 2359 | } |
| 2360 | tempTouchState.addOrUpdateWindow(wallpaper, wallpaperFlags, pointerIds, |
| 2361 | entry.eventTime); |
| 2362 | } |
| 2363 | } |
| 2364 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2365 | } |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 2366 | |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 2367 | // If a window is already pilfering some pointers, give it this new pointer as well and |
| 2368 | // make it pilfering. This will prevent other non-spy windows from getting this pointer, |
| 2369 | // which is a specific behaviour that we want. |
| 2370 | const int32_t pointerId = entry.pointerProperties[pointerIndex].id; |
| 2371 | for (TouchedWindow& touchedWindow : tempTouchState.windows) { |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 2372 | if (touchedWindow.pointerIds.test(pointerId) && |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 2373 | touchedWindow.pilferedPointerIds.count() > 0) { |
| 2374 | // This window is already pilfering some pointers, and this new pointer is also |
| 2375 | // going to it. Therefore, take over this pointer and don't give it to anyone |
| 2376 | // else. |
| 2377 | touchedWindow.pilferedPointerIds.set(pointerId); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 2378 | } |
| 2379 | } |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 2380 | |
| 2381 | // Restrict all pilfered pointers to the pilfering windows. |
| 2382 | tempTouchState.cancelPointersForNonPilferingWindows(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2383 | } else { |
| 2384 | /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ |
| 2385 | |
| 2386 | // If the pointer is not currently down, then ignore the event. |
Siarhei Vishniakou | 3ad385b | 2022-11-04 10:09:53 -0700 | [diff] [blame] | 2387 | if (!tempTouchState.isDown()) { |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2388 | LOG(INFO) << "Dropping event because the pointer is not down or we previously " |
| 2389 | "dropped the pointer down event in display " |
| 2390 | << displayId << ": " << entry.getDescription(); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2391 | outInjectionResult = InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2392 | return {}; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2393 | } |
| 2394 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2395 | addDragEventLocked(entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2396 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2397 | // Check whether touches should slip outside of the current foreground window. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2398 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.pointerCount == 1 && |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2399 | tempTouchState.isSlippery()) { |
Siarhei Vishniakou | 9306c38 | 2022-09-30 15:30:31 -0700 | [diff] [blame] | 2400 | const auto [x, y] = resolveTouchedPosition(entry); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2401 | const bool isStylus = isPointerFromStylus(entry, /*pointerIndex=*/0); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2402 | sp<WindowInfoHandle> oldTouchedWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2403 | tempTouchState.getFirstForegroundWindowHandle(); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2404 | auto [newTouchedWindowHandle, _] = findTouchedWindowAtLocked(displayId, x, y, isStylus); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2405 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2406 | // Verify targeted injection. |
| 2407 | if (const auto err = verifyTargetedInjection(newTouchedWindowHandle, entry); err) { |
| 2408 | ALOGW("Dropping injected event: %s", (*err).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2409 | outInjectionResult = os::InputEventInjectionResult::TARGET_MISMATCH; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2410 | return {}; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2411 | } |
| 2412 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2413 | // Drop touch events if requested by input feature |
| 2414 | if (newTouchedWindowHandle != nullptr && |
| 2415 | shouldDropInput(entry, newTouchedWindowHandle)) { |
| 2416 | newTouchedWindowHandle = nullptr; |
| 2417 | } |
| 2418 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2419 | if (oldTouchedWindowHandle != newTouchedWindowHandle && |
| 2420 | oldTouchedWindowHandle != nullptr && newTouchedWindowHandle != nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2421 | if (DEBUG_FOCUS) { |
| 2422 | ALOGD("Touch is slipping out of window %s into window %s in display %" PRId32, |
| 2423 | oldTouchedWindowHandle->getName().c_str(), |
| 2424 | newTouchedWindowHandle->getName().c_str(), displayId); |
| 2425 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2426 | // Make a slippery exit from the old window. |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 2427 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2428 | const int32_t pointerId = entry.pointerProperties[0].id; |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 2429 | pointerIds.set(pointerId); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2430 | |
| 2431 | const TouchedWindow& touchedWindow = |
| 2432 | tempTouchState.getTouchedWindow(oldTouchedWindowHandle); |
| 2433 | addWindowTargetLocked(oldTouchedWindowHandle, |
| 2434 | InputTarget::Flags::DISPATCH_AS_SLIPPERY_EXIT, pointerIds, |
| 2435 | touchedWindow.firstDownTimeInTarget, targets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2436 | |
| 2437 | // Make a slippery entrance into the new window. |
| 2438 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
Prabir Pradhan | 713bb3e | 2021-12-20 02:07:40 -0800 | [diff] [blame] | 2439 | isSplit = !isFromMouse; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2440 | } |
| 2441 | |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2442 | ftl::Flags<InputTarget::Flags> targetFlags = |
| 2443 | InputTarget::Flags::DISPATCH_AS_SLIPPERY_ENTER; |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2444 | if (canReceiveForegroundTouches(*newTouchedWindowHandle->getInfo())) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2445 | targetFlags |= InputTarget::Flags::FOREGROUND; |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2446 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2447 | if (isSplit) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2448 | targetFlags |= InputTarget::Flags::SPLIT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2449 | } |
| 2450 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2451 | targetFlags |= InputTarget::Flags::WINDOW_IS_OBSCURED; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2452 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2453 | targetFlags |= InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2454 | } |
| 2455 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2456 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds, |
| 2457 | entry.eventTime); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2458 | |
| 2459 | // Check if the wallpaper window should deliver the corresponding event. |
| 2460 | slipWallpaperTouch(targetFlags, oldTouchedWindowHandle, newTouchedWindowHandle, |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2461 | tempTouchState, pointerId, targets); |
| 2462 | tempTouchState.removeTouchedPointerFromWindow(pointerId, oldTouchedWindowHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2463 | } |
| 2464 | } |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 2465 | |
| 2466 | // Update the pointerIds for non-splittable when it received pointer down. |
| 2467 | if (!isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN) { |
| 2468 | // If no split, we suppose all touched windows should receive pointer down. |
| 2469 | const int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
| 2470 | for (size_t i = 0; i < tempTouchState.windows.size(); i++) { |
| 2471 | TouchedWindow& touchedWindow = tempTouchState.windows[i]; |
| 2472 | // Ignore drag window for it should just track one pointer. |
| 2473 | if (mDragState && mDragState->dragWindow == touchedWindow.windowHandle) { |
| 2474 | continue; |
| 2475 | } |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 2476 | touchedWindow.pointerIds.set(entry.pointerProperties[pointerIndex].id); |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 2477 | } |
| 2478 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2479 | } |
| 2480 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2481 | // Update dispatching for hover enter and exit. |
Sam Dubey | f886dec | 2023-01-27 13:28:19 +0000 | [diff] [blame] | 2482 | { |
| 2483 | std::vector<TouchedWindow> hoveringWindows = |
| 2484 | getHoveringWindowsLocked(oldState, tempTouchState, entry); |
| 2485 | for (const TouchedWindow& touchedWindow : hoveringWindows) { |
| 2486 | addWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags, |
| 2487 | touchedWindow.pointerIds, touchedWindow.firstDownTimeInTarget, |
| 2488 | targets); |
| 2489 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2490 | } |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2491 | // Ensure that we have at least one foreground window or at least one window that cannot be a |
| 2492 | // foreground target. If we only have windows that are not receiving foreground touches (e.g. we |
| 2493 | // only have windows getting ACTION_OUTSIDE), then drop the event, because there is no window |
| 2494 | // that is actually receiving the entire gesture. |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2495 | if (std::none_of(tempTouchState.windows.begin(), tempTouchState.windows.end(), |
| 2496 | [](const TouchedWindow& touchedWindow) { |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2497 | return !canReceiveForegroundTouches( |
| 2498 | *touchedWindow.windowHandle->getInfo()) || |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2499 | touchedWindow.targetFlags.test(InputTarget::Flags::FOREGROUND); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2500 | })) { |
Siarhei Vishniakou | 1fb1891 | 2022-03-08 10:31:39 -0800 | [diff] [blame] | 2501 | ALOGI("Dropping event because there is no touched window on display %d to receive it: %s", |
| 2502 | displayId, entry.getDescription().c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2503 | outInjectionResult = InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2504 | return {}; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2505 | } |
| 2506 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2507 | // Ensure that all touched windows are valid for injection. |
| 2508 | if (entry.injectionState != nullptr) { |
| 2509 | std::string errs; |
| 2510 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2511 | if (touchedWindow.targetFlags.test(InputTarget::Flags::DISPATCH_AS_OUTSIDE)) { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2512 | // Allow ACTION_OUTSIDE events generated by targeted injection to be |
| 2513 | // dispatched to any uid, since the coords will be zeroed out later. |
| 2514 | continue; |
| 2515 | } |
| 2516 | const auto err = verifyTargetedInjection(touchedWindow.windowHandle, entry); |
| 2517 | if (err) errs += "\n - " + *err; |
| 2518 | } |
| 2519 | if (!errs.empty()) { |
| 2520 | ALOGW("Dropping targeted injection: At least one touched window is not owned by uid " |
| 2521 | "%d:%s", |
| 2522 | *entry.injectionState->targetUid, errs.c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2523 | outInjectionResult = InputEventInjectionResult::TARGET_MISMATCH; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2524 | return {}; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2525 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2526 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2527 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2528 | // Check whether windows listening for outside touches are owned by the same UID. If the owner |
| 2529 | // 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] | 2530 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2531 | sp<WindowInfoHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2532 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2533 | if (foregroundWindowHandle) { |
| 2534 | const int32_t foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2535 | for (InputTarget& target : targets) { |
| 2536 | if (target.flags.test(InputTarget::Flags::DISPATCH_AS_OUTSIDE)) { |
| 2537 | sp<WindowInfoHandle> targetWindow = |
| 2538 | getWindowHandleLocked(target.inputChannel->getConnectionToken()); |
| 2539 | if (targetWindow->getInfo()->ownerUid != foregroundWindowUid) { |
| 2540 | target.flags |= InputTarget::Flags::ZERO_COORDS; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2541 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2542 | } |
| 2543 | } |
| 2544 | } |
| 2545 | } |
| 2546 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2547 | // Success! Output targets from the touch state. |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2548 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 2549 | if (touchedWindow.pointerIds.none() && !touchedWindow.hasHoveringPointers(entry.deviceId)) { |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2550 | // Windows with hovering pointers are getting persisted inside TouchState. |
| 2551 | // Do not send this event to those windows. |
| 2552 | continue; |
| 2553 | } |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2554 | addWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags, |
| 2555 | touchedWindow.pointerIds, touchedWindow.firstDownTimeInTarget, |
| 2556 | targets); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2557 | } |
Sam Dubey | 39d37cf | 2022-12-07 18:05:35 +0000 | [diff] [blame] | 2558 | |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2559 | outInjectionResult = InputEventInjectionResult::SUCCEEDED; |
Sam Dubey | f886dec | 2023-01-27 13:28:19 +0000 | [diff] [blame] | 2560 | // Drop the outside or hover touch windows since we will not care about them |
| 2561 | // in the next iteration. |
| 2562 | tempTouchState.filterNonAsIsTouchWindows(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2563 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2564 | // Update final pieces of touch state if the injector had permission. |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2565 | if (switchedDevice) { |
| 2566 | if (DEBUG_FOCUS) { |
| 2567 | ALOGD("Conflicting pointer actions: Switched to a different device."); |
| 2568 | } |
| 2569 | *outConflictingPointerActions = true; |
| 2570 | } |
| 2571 | |
| 2572 | if (isHoverAction) { |
| 2573 | // Started hovering, therefore no longer down. |
Siarhei Vishniakou | 3ad385b | 2022-11-04 10:09:53 -0700 | [diff] [blame] | 2574 | if (oldState && oldState->isDown()) { |
Siarhei Vishniakou | 6e1e987 | 2022-11-08 17:51:35 -0800 | [diff] [blame] | 2575 | ALOGD_IF(DEBUG_FOCUS, |
| 2576 | "Conflicting pointer actions: Hover received while pointer was down."); |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2577 | *outConflictingPointerActions = true; |
| 2578 | } |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2579 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2580 | maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) { |
| 2581 | tempTouchState.deviceId = entry.deviceId; |
| 2582 | tempTouchState.source = entry.source; |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2583 | } |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2584 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP) { |
| 2585 | // Pointer went up. |
| 2586 | tempTouchState.removeTouchedPointer(entry.pointerProperties[0].id); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2587 | } else if (maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2588 | // All pointers up or canceled. |
| 2589 | tempTouchState.reset(); |
| 2590 | } else if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 2591 | // First pointer went down. |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2592 | if (oldState && (oldState->isDown() || oldState->hasHoveringPointers())) { |
| 2593 | ALOGD("Conflicting pointer actions: Down received while already down or hovering."); |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2594 | *outConflictingPointerActions = true; |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2595 | } |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2596 | } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 2597 | // One pointer went up. |
| 2598 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
| 2599 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2600 | |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2601 | for (size_t i = 0; i < tempTouchState.windows.size();) { |
| 2602 | TouchedWindow& touchedWindow = tempTouchState.windows[i]; |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 2603 | touchedWindow.pointerIds.reset(pointerId); |
| 2604 | if (touchedWindow.pointerIds.none()) { |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2605 | tempTouchState.windows.erase(tempTouchState.windows.begin() + i); |
| 2606 | continue; |
| 2607 | } |
| 2608 | i += 1; |
| 2609 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2610 | } |
| 2611 | |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2612 | // Save changes unless the action was scroll in which case the temporary touch |
| 2613 | // state was only valid for this one action. |
| 2614 | if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) { |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 2615 | if (displayId >= 0) { |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2616 | tempTouchState.clearWindowsWithoutPointers(); |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2617 | mTouchStatesByDisplay[displayId] = tempTouchState; |
| 2618 | } else { |
| 2619 | mTouchStatesByDisplay.erase(displayId); |
| 2620 | } |
| 2621 | } |
| 2622 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 2623 | if (tempTouchState.windows.empty()) { |
| 2624 | mTouchStatesByDisplay.erase(displayId); |
| 2625 | } |
| 2626 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2627 | return targets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2628 | } |
| 2629 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2630 | void InputDispatcher::finishDragAndDrop(int32_t displayId, float x, float y) { |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2631 | // Prevent stylus interceptor windows from affecting drag and drop behavior for now, until we |
| 2632 | // have an explicit reason to support it. |
| 2633 | constexpr bool isStylus = false; |
| 2634 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2635 | auto [dropWindow, _] = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2636 | findTouchedWindowAtLocked(displayId, x, y, isStylus, /*ignoreDragWindow=*/true); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2637 | if (dropWindow) { |
| 2638 | vec2 local = dropWindow->getInfo()->transform.transform(x, y); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2639 | sendDropWindowCommandLocked(dropWindow->getToken(), local.x, local.y); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 2640 | } else { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2641 | ALOGW("No window found when drop."); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2642 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2643 | } |
| 2644 | mDragState.reset(); |
| 2645 | } |
| 2646 | |
| 2647 | void InputDispatcher::addDragEventLocked(const MotionEntry& entry) { |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 2648 | if (!mDragState || mDragState->dragWindow->getInfo()->displayId != entry.displayId) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2649 | return; |
| 2650 | } |
| 2651 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2652 | if (!mDragState->isStartDrag) { |
| 2653 | mDragState->isStartDrag = true; |
| 2654 | mDragState->isStylusButtonDownAtStart = |
| 2655 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2656 | } |
| 2657 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2658 | // Find the pointer index by id. |
| 2659 | int32_t pointerIndex = 0; |
| 2660 | for (; static_cast<uint32_t>(pointerIndex) < entry.pointerCount; pointerIndex++) { |
| 2661 | const PointerProperties& pointerProperties = entry.pointerProperties[pointerIndex]; |
| 2662 | if (pointerProperties.id == mDragState->pointerId) { |
| 2663 | break; |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2664 | } |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2665 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2666 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2667 | if (uint32_t(pointerIndex) == entry.pointerCount) { |
| 2668 | LOG_ALWAYS_FATAL("Should find a valid pointer index by id %d", mDragState->pointerId); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2669 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2670 | mDragState.reset(); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2671 | return; |
| 2672 | } |
| 2673 | |
| 2674 | const int32_t maskedAction = entry.action & AMOTION_EVENT_ACTION_MASK; |
| 2675 | const int32_t x = entry.pointerCoords[pointerIndex].getX(); |
| 2676 | const int32_t y = entry.pointerCoords[pointerIndex].getY(); |
| 2677 | |
| 2678 | switch (maskedAction) { |
| 2679 | case AMOTION_EVENT_ACTION_MOVE: { |
| 2680 | // Handle the special case : stylus button no longer pressed. |
| 2681 | bool isStylusButtonDown = |
| 2682 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2683 | if (mDragState->isStylusButtonDownAtStart && !isStylusButtonDown) { |
| 2684 | finishDragAndDrop(entry.displayId, x, y); |
| 2685 | return; |
| 2686 | } |
| 2687 | |
| 2688 | // Prevent stylus interceptor windows from affecting drag and drop behavior for now, |
| 2689 | // until we have an explicit reason to support it. |
| 2690 | constexpr bool isStylus = false; |
| 2691 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2692 | auto [hoverWindowHandle, _] = findTouchedWindowAtLocked(entry.displayId, x, y, isStylus, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2693 | /*ignoreDragWindow=*/true); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2694 | // enqueue drag exit if needed. |
| 2695 | if (hoverWindowHandle != mDragState->dragHoverWindowHandle && |
| 2696 | !haveSameToken(hoverWindowHandle, mDragState->dragHoverWindowHandle)) { |
| 2697 | if (mDragState->dragHoverWindowHandle != nullptr) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2698 | enqueueDragEventLocked(mDragState->dragHoverWindowHandle, /*isExiting=*/true, x, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2699 | y); |
| 2700 | } |
| 2701 | mDragState->dragHoverWindowHandle = hoverWindowHandle; |
| 2702 | } |
| 2703 | // enqueue drag location if needed. |
| 2704 | if (hoverWindowHandle != nullptr) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2705 | enqueueDragEventLocked(hoverWindowHandle, /*isExiting=*/false, x, y); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2706 | } |
| 2707 | break; |
| 2708 | } |
| 2709 | |
| 2710 | case AMOTION_EVENT_ACTION_POINTER_UP: |
| 2711 | if (getMotionEventActionPointerIndex(entry.action) != pointerIndex) { |
| 2712 | break; |
| 2713 | } |
| 2714 | // The drag pointer is up. |
| 2715 | [[fallthrough]]; |
| 2716 | case AMOTION_EVENT_ACTION_UP: |
| 2717 | finishDragAndDrop(entry.displayId, x, y); |
| 2718 | break; |
| 2719 | case AMOTION_EVENT_ACTION_CANCEL: { |
| 2720 | ALOGD("Receiving cancel when drag and drop."); |
| 2721 | sendDropWindowCommandLocked(nullptr, 0, 0); |
| 2722 | mDragState.reset(); |
| 2723 | break; |
| 2724 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2725 | } |
| 2726 | } |
| 2727 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2728 | void InputDispatcher::addWindowTargetLocked(const sp<WindowInfoHandle>& windowHandle, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2729 | ftl::Flags<InputTarget::Flags> targetFlags, |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 2730 | std::bitset<MAX_POINTER_ID + 1> pointerIds, |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2731 | std::optional<nsecs_t> firstDownTimeInTarget, |
Siarhei Vishniakou | f75cddb | 2022-10-25 10:42:16 -0700 | [diff] [blame] | 2732 | std::vector<InputTarget>& inputTargets) const { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2733 | std::vector<InputTarget>::iterator it = |
| 2734 | std::find_if(inputTargets.begin(), inputTargets.end(), |
| 2735 | [&windowHandle](const InputTarget& inputTarget) { |
| 2736 | return inputTarget.inputChannel->getConnectionToken() == |
| 2737 | windowHandle->getToken(); |
| 2738 | }); |
Chavi Weingarten | 97b8eec | 2020-01-09 18:09:08 +0000 | [diff] [blame] | 2739 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2740 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2741 | |
| 2742 | if (it == inputTargets.end()) { |
| 2743 | InputTarget inputTarget; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 2744 | std::shared_ptr<InputChannel> inputChannel = |
| 2745 | getInputChannelLocked(windowHandle->getToken()); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2746 | if (inputChannel == nullptr) { |
| 2747 | ALOGW("Window %s already unregistered input channel", windowHandle->getName().c_str()); |
| 2748 | return; |
| 2749 | } |
| 2750 | inputTarget.inputChannel = inputChannel; |
| 2751 | inputTarget.flags = targetFlags; |
| 2752 | inputTarget.globalScaleFactor = windowInfo->globalScaleFactor; |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2753 | inputTarget.firstDownTimeInTarget = firstDownTimeInTarget; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2754 | const auto& displayInfoIt = mDisplayInfos.find(windowInfo->displayId); |
| 2755 | if (displayInfoIt != mDisplayInfos.end()) { |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 2756 | inputTarget.displayTransform = displayInfoIt->second.transform; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2757 | } else { |
Siarhei Vishniakou | a06bb55 | 2023-02-07 09:38:56 -0800 | [diff] [blame] | 2758 | // DisplayInfo not found for this window on display windowInfo->displayId. |
| 2759 | // TODO(b/198444055): Make this an error message after 'setInputWindows' API is removed. |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2760 | } |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2761 | inputTargets.push_back(inputTarget); |
| 2762 | it = inputTargets.end() - 1; |
| 2763 | } |
| 2764 | |
| 2765 | ALOG_ASSERT(it->flags == targetFlags); |
| 2766 | ALOG_ASSERT(it->globalScaleFactor == windowInfo->globalScaleFactor); |
| 2767 | |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2768 | it->addPointers(pointerIds, windowInfo->transform); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2769 | } |
| 2770 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2771 | void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2772 | int32_t displayId) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2773 | auto monitorsIt = mGlobalMonitorsByDisplay.find(displayId); |
| 2774 | if (monitorsIt == mGlobalMonitorsByDisplay.end()) return; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2775 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2776 | for (const Monitor& monitor : selectResponsiveMonitorsLocked(monitorsIt->second)) { |
| 2777 | InputTarget target; |
| 2778 | target.inputChannel = monitor.inputChannel; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2779 | target.flags = InputTarget::Flags::DISPATCH_AS_IS; |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2780 | // target.firstDownTimeInTarget is not set for global monitors. It is only required in split |
| 2781 | // touch and global monitoring works as intended even without setting firstDownTimeInTarget |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2782 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
| 2783 | target.displayTransform = it->second.transform; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2784 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2785 | target.setDefaultPointerTransform(target.displayTransform); |
| 2786 | inputTargets.push_back(target); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2787 | } |
| 2788 | } |
| 2789 | |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2790 | /** |
| 2791 | * Indicate whether one window handle should be considered as obscuring |
| 2792 | * another window handle. We only check a few preconditions. Actually |
| 2793 | * checking the bounds is left to the caller. |
| 2794 | */ |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2795 | static bool canBeObscuredBy(const sp<WindowInfoHandle>& windowHandle, |
| 2796 | const sp<WindowInfoHandle>& otherHandle) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2797 | // Compare by token so cloned layers aren't counted |
| 2798 | if (haveSameToken(windowHandle, otherHandle)) { |
| 2799 | return false; |
| 2800 | } |
| 2801 | auto info = windowHandle->getInfo(); |
| 2802 | auto otherInfo = otherHandle->getInfo(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2803 | if (otherInfo->inputConfig.test(WindowInfo::InputConfig::NOT_VISIBLE)) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2804 | return false; |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2805 | } else if (otherInfo->alpha == 0 && |
| 2806 | otherInfo->inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE)) { |
Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame] | 2807 | // Those act as if they were invisible, so we don't need to flag them. |
| 2808 | // We do want to potentially flag touchable windows even if they have 0 |
| 2809 | // opacity, since they can consume touches and alter the effects of the |
| 2810 | // user interaction (eg. apps that rely on |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2811 | // Flags::WINDOW_IS_PARTIALLY_OBSCURED should still be told about those |
Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame] | 2812 | // windows), hence we also check for FLAG_NOT_TOUCHABLE. |
| 2813 | return false; |
Bernardo Rufino | 8007daf | 2020-09-22 09:40:01 +0000 | [diff] [blame] | 2814 | } else if (info->ownerUid == otherInfo->ownerUid) { |
| 2815 | // If ownerUid is the same we don't generate occlusion events as there |
| 2816 | // is no security boundary within an uid. |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2817 | return false; |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2818 | } else if (otherInfo->inputConfig.test(gui::WindowInfo::InputConfig::TRUSTED_OVERLAY)) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2819 | return false; |
| 2820 | } else if (otherInfo->displayId != info->displayId) { |
| 2821 | return false; |
| 2822 | } |
| 2823 | return true; |
| 2824 | } |
| 2825 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2826 | /** |
| 2827 | * Returns touch occlusion information in the form of TouchOcclusionInfo. To check if the touch is |
| 2828 | * untrusted, one should check: |
| 2829 | * |
| 2830 | * 1. If result.hasBlockingOcclusion is true. |
| 2831 | * If it's, it means the touch should be blocked due to a window with occlusion mode of |
| 2832 | * BLOCK_UNTRUSTED. |
| 2833 | * |
| 2834 | * 2. If result.obscuringOpacity > mMaximumObscuringOpacityForTouch. |
| 2835 | * If it is (and 1 is false), then the touch should be blocked because a stack of windows |
| 2836 | * (possibly only one) with occlusion mode of USE_OPACITY from one UID resulted in a composed |
| 2837 | * obscuring opacity above the threshold. Note that if there was no window of occlusion mode |
| 2838 | * USE_OPACITY, result.obscuringOpacity would've been 0 and since |
| 2839 | * mMaximumObscuringOpacityForTouch >= 0, the condition above would never be true. |
| 2840 | * |
| 2841 | * If neither of those is true, then it means the touch can be allowed. |
| 2842 | */ |
| 2843 | InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2844 | const sp<WindowInfoHandle>& windowHandle, int32_t x, int32_t y) const { |
| 2845 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2846 | int32_t displayId = windowInfo->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2847 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2848 | TouchOcclusionInfo info; |
| 2849 | info.hasBlockingOcclusion = false; |
| 2850 | info.obscuringOpacity = 0; |
| 2851 | info.obscuringUid = -1; |
| 2852 | std::map<int32_t, float> opacityByUid; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2853 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2854 | if (windowHandle == otherHandle) { |
| 2855 | break; // All future windows are below us. Exit early. |
| 2856 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2857 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 2858 | if (canBeObscuredBy(windowHandle, otherHandle) && otherInfo->frameContainsPoint(x, y) && |
| 2859 | !haveSameApplicationToken(windowInfo, otherInfo)) { |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2860 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2861 | info.debugInfo.push_back( |
| 2862 | dumpWindowForTouchOcclusion(otherInfo, /* isTouchedWindow */ false)); |
| 2863 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2864 | // canBeObscuredBy() has returned true above, which means this window is untrusted, so |
| 2865 | // we perform the checks below to see if the touch can be propagated or not based on the |
| 2866 | // window's touch occlusion mode |
| 2867 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::BLOCK_UNTRUSTED) { |
| 2868 | info.hasBlockingOcclusion = true; |
| 2869 | info.obscuringUid = otherInfo->ownerUid; |
| 2870 | info.obscuringPackage = otherInfo->packageName; |
| 2871 | break; |
| 2872 | } |
| 2873 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::USE_OPACITY) { |
| 2874 | uint32_t uid = otherInfo->ownerUid; |
| 2875 | float opacity = |
| 2876 | (opacityByUid.find(uid) == opacityByUid.end()) ? 0 : opacityByUid[uid]; |
| 2877 | // Given windows A and B: |
| 2878 | // opacity(A, B) = 1 - [1 - opacity(A)] * [1 - opacity(B)] |
| 2879 | opacity = 1 - (1 - opacity) * (1 - otherInfo->alpha); |
| 2880 | opacityByUid[uid] = opacity; |
| 2881 | if (opacity > info.obscuringOpacity) { |
| 2882 | info.obscuringOpacity = opacity; |
| 2883 | info.obscuringUid = uid; |
| 2884 | info.obscuringPackage = otherInfo->packageName; |
| 2885 | } |
| 2886 | } |
| 2887 | } |
| 2888 | } |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2889 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2890 | info.debugInfo.push_back( |
| 2891 | dumpWindowForTouchOcclusion(windowInfo, /* isTouchedWindow */ true)); |
| 2892 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2893 | return info; |
| 2894 | } |
| 2895 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2896 | std::string InputDispatcher::dumpWindowForTouchOcclusion(const WindowInfo* info, |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2897 | bool isTouchedWindow) const { |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 2898 | return StringPrintf(INDENT2 "* %spackage=%s/%" PRId32 ", id=%" PRId32 ", mode=%s, alpha=%.2f, " |
| 2899 | "frame=[%" PRId32 ",%" PRId32 "][%" PRId32 ",%" PRId32 |
| 2900 | "], touchableRegion=%s, window={%s}, inputConfig={%s}, " |
| 2901 | "hasToken=%s, applicationInfo.name=%s, applicationInfo.token=%s\n", |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2902 | isTouchedWindow ? "[TOUCHED] " : "", info->packageName.c_str(), |
| 2903 | info->ownerUid, info->id, toString(info->touchOcclusionMode).c_str(), |
| 2904 | info->alpha, info->frameLeft, info->frameTop, info->frameRight, |
| 2905 | info->frameBottom, dumpRegion(info->touchableRegion).c_str(), |
| 2906 | info->name.c_str(), info->inputConfig.string().c_str(), |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 2907 | toString(info->token != nullptr), info->applicationInfo.name.c_str(), |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 2908 | binderToString(info->applicationInfo.token).c_str()); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2909 | } |
| 2910 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2911 | bool InputDispatcher::isTouchTrustedLocked(const TouchOcclusionInfo& occlusionInfo) const { |
| 2912 | if (occlusionInfo.hasBlockingOcclusion) { |
| 2913 | ALOGW("Untrusted touch due to occlusion by %s/%d", occlusionInfo.obscuringPackage.c_str(), |
| 2914 | occlusionInfo.obscuringUid); |
| 2915 | return false; |
| 2916 | } |
| 2917 | if (occlusionInfo.obscuringOpacity > mMaximumObscuringOpacityForTouch) { |
| 2918 | ALOGW("Untrusted touch due to occlusion by %s/%d (obscuring opacity = " |
| 2919 | "%.2f, maximum allowed = %.2f)", |
| 2920 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid, |
| 2921 | occlusionInfo.obscuringOpacity, mMaximumObscuringOpacityForTouch); |
| 2922 | return false; |
| 2923 | } |
| 2924 | return true; |
| 2925 | } |
| 2926 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2927 | bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2928 | int32_t x, int32_t y) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2929 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2930 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 2931 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2932 | if (windowHandle == otherHandle) { |
| 2933 | break; // All future windows are below us. Exit early. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2934 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2935 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2936 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2937 | otherInfo->frameContainsPoint(x, y)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2938 | return true; |
| 2939 | } |
| 2940 | } |
| 2941 | return false; |
| 2942 | } |
| 2943 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2944 | bool InputDispatcher::isWindowObscuredLocked(const sp<WindowInfoHandle>& windowHandle) const { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2945 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2946 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 2947 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 2948 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2949 | if (windowHandle == otherHandle) { |
| 2950 | break; // All future windows are below us. Exit early. |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2951 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2952 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2953 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2954 | otherInfo->overlaps(windowInfo)) { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2955 | return true; |
| 2956 | } |
| 2957 | } |
| 2958 | return false; |
| 2959 | } |
| 2960 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2961 | std::string InputDispatcher::getApplicationWindowLabel( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2962 | const InputApplicationHandle* applicationHandle, const sp<WindowInfoHandle>& windowHandle) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2963 | if (applicationHandle != nullptr) { |
| 2964 | if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2965 | return applicationHandle->getName() + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2966 | } else { |
| 2967 | return applicationHandle->getName(); |
| 2968 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2969 | } else if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2970 | return windowHandle->getInfo()->applicationInfo.name + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2971 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2972 | return "<unknown application or window>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2973 | } |
| 2974 | } |
| 2975 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2976 | void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 2977 | if (!isUserActivityEvent(eventEntry)) { |
| 2978 | // 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] | 2979 | return; |
| 2980 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2981 | int32_t displayId = getTargetDisplayId(eventEntry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2982 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2983 | if (focusedWindowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2984 | const WindowInfo* info = focusedWindowHandle->getInfo(); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 2985 | if (info->inputConfig.test(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2986 | if (DEBUG_DISPATCH_CYCLE) { |
| 2987 | ALOGD("Not poking user activity: disabled by window '%s'.", info->name.c_str()); |
| 2988 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2989 | return; |
| 2990 | } |
| 2991 | } |
| 2992 | |
| 2993 | int32_t eventType = USER_ACTIVITY_EVENT_OTHER; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2994 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2995 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2996 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 2997 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2998 | return; |
| 2999 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3000 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3001 | if (MotionEvent::isTouchEvent(motionEntry.source, motionEntry.action)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3002 | eventType = USER_ACTIVITY_EVENT_TOUCH; |
| 3003 | } |
| 3004 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3005 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3006 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3007 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 3008 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3009 | return; |
| 3010 | } |
| 3011 | eventType = USER_ACTIVITY_EVENT_BUTTON; |
| 3012 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3013 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3014 | default: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3015 | LOG_ALWAYS_FATAL("%s events are not user activity", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3016 | ftl::enum_string(eventEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3017 | break; |
| 3018 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3019 | } |
| 3020 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3021 | auto command = [this, eventTime = eventEntry.eventTime, eventType, displayId]() |
| 3022 | REQUIRES(mLock) { |
| 3023 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame^] | 3024 | mPolicy.pokeUserActivity(eventTime, eventType, displayId); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3025 | }; |
| 3026 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3027 | } |
| 3028 | |
| 3029 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3030 | const std::shared_ptr<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3031 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3032 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3033 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3034 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3035 | StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3036 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3037 | ATRACE_NAME(message.c_str()); |
| 3038 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3039 | if (DEBUG_DISPATCH_CYCLE) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3040 | ALOGD("channel '%s' ~ prepareDispatchCycle - flags=%s, " |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 3041 | "globalScaleFactor=%f, pointerIds=%s %s", |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3042 | connection->getInputChannelName().c_str(), inputTarget.flags.string().c_str(), |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 3043 | inputTarget.globalScaleFactor, bitsetToString(inputTarget.pointerIds).c_str(), |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3044 | inputTarget.getPointerInfoString().c_str()); |
| 3045 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3046 | |
| 3047 | // Skip this event if the connection status is not normal. |
| 3048 | // 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] | 3049 | if (connection->status != Connection::Status::NORMAL) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3050 | if (DEBUG_DISPATCH_CYCLE) { |
| 3051 | ALOGD("channel '%s' ~ Dropping event because the channel status is %s", |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3052 | connection->getInputChannelName().c_str(), |
| 3053 | ftl::enum_string(connection->status).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3054 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3055 | return; |
| 3056 | } |
| 3057 | |
| 3058 | // Split a motion event if needed. |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3059 | if (inputTarget.flags.test(InputTarget::Flags::SPLIT)) { |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3060 | LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3061 | "Entry type %s should not have Flags::SPLIT", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3062 | ftl::enum_string(eventEntry->type).c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3063 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3064 | const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry); |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3065 | if (inputTarget.pointerIds.count() != originalMotionEntry.pointerCount) { |
Siarhei Vishniakou | 16e4fa0 | 2023-02-16 17:48:56 -0800 | [diff] [blame] | 3066 | if (!inputTarget.firstDownTimeInTarget.has_value()) { |
| 3067 | logDispatchStateLocked(); |
| 3068 | LOG(FATAL) << "Splitting motion events requires a down time to be set for the " |
| 3069 | "target on connection " |
| 3070 | << connection->getInputChannelName() << " for " |
| 3071 | << originalMotionEntry.getDescription(); |
| 3072 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3073 | std::unique_ptr<MotionEntry> splitMotionEntry = |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3074 | splitMotionEvent(originalMotionEntry, inputTarget.pointerIds, |
| 3075 | inputTarget.firstDownTimeInTarget.value()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3076 | if (!splitMotionEntry) { |
| 3077 | return; // split event was dropped |
| 3078 | } |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3079 | if (splitMotionEntry->action == AMOTION_EVENT_ACTION_CANCEL) { |
| 3080 | std::string reason = std::string("reason=pointer cancel on split window"); |
| 3081 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 3082 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 3083 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3084 | if (DEBUG_FOCUS) { |
| 3085 | ALOGD("channel '%s' ~ Split motion event.", |
| 3086 | connection->getInputChannelName().c_str()); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3087 | logOutboundMotionDetails(" ", *splitMotionEntry); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3088 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3089 | enqueueDispatchEntriesLocked(currentTime, connection, std::move(splitMotionEntry), |
| 3090 | inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3091 | return; |
| 3092 | } |
| 3093 | } |
| 3094 | |
| 3095 | // Not splitting. Enqueue dispatch entries for the event as is. |
| 3096 | enqueueDispatchEntriesLocked(currentTime, connection, eventEntry, inputTarget); |
| 3097 | } |
| 3098 | |
| 3099 | void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3100 | const std::shared_ptr<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3101 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3102 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3103 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3104 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3105 | StringPrintf("enqueueDispatchEntriesLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3106 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3107 | ATRACE_NAME(message.c_str()); |
| 3108 | } |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3109 | LOG_ALWAYS_FATAL_IF(!inputTarget.flags.any(InputTarget::DISPATCH_MASK), |
| 3110 | "No dispatch flags are set for %s", eventEntry->getDescription().c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3111 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 3112 | const bool wasEmpty = connection->outboundQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3113 | |
| 3114 | // Enqueue dispatch entries for the requested modes. |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3115 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3116 | InputTarget::Flags::DISPATCH_AS_HOVER_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3117 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3118 | InputTarget::Flags::DISPATCH_AS_OUTSIDE); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3119 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3120 | InputTarget::Flags::DISPATCH_AS_HOVER_ENTER); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3121 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3122 | InputTarget::Flags::DISPATCH_AS_IS); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3123 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3124 | InputTarget::Flags::DISPATCH_AS_SLIPPERY_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3125 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3126 | InputTarget::Flags::DISPATCH_AS_SLIPPERY_ENTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3127 | |
| 3128 | // If the outbound queue was previously empty, start the dispatch cycle going. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3129 | if (wasEmpty && !connection->outboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3130 | startDispatchCycleLocked(currentTime, connection); |
| 3131 | } |
| 3132 | } |
| 3133 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3134 | void InputDispatcher::enqueueDispatchEntryLocked(const std::shared_ptr<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3135 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3136 | const InputTarget& inputTarget, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3137 | ftl::Flags<InputTarget::Flags> dispatchMode) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3138 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3139 | std::string message = StringPrintf("enqueueDispatchEntry(inputChannel=%s, dispatchMode=%s)", |
| 3140 | connection->getInputChannelName().c_str(), |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3141 | dispatchMode.string().c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3142 | ATRACE_NAME(message.c_str()); |
| 3143 | } |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3144 | ftl::Flags<InputTarget::Flags> inputTargetFlags = inputTarget.flags; |
| 3145 | if (!inputTargetFlags.any(dispatchMode)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3146 | return; |
| 3147 | } |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3148 | |
| 3149 | inputTargetFlags.clear(InputTarget::DISPATCH_MASK); |
| 3150 | inputTargetFlags |= dispatchMode; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3151 | |
| 3152 | // This is a new event. |
| 3153 | // Enqueue a new dispatch entry onto the outbound queue for this connection. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3154 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3155 | createDispatchEntry(inputTarget, eventEntry, inputTargetFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3156 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3157 | // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a |
| 3158 | // different EventEntry than what was passed in. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3159 | EventEntry& newEntry = *(dispatchEntry->eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3160 | // Apply target flags and update the connection's input state. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3161 | switch (newEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3162 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3163 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3164 | dispatchEntry->resolvedEventId = keyEntry.id; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3165 | dispatchEntry->resolvedAction = keyEntry.action; |
| 3166 | dispatchEntry->resolvedFlags = keyEntry.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3167 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3168 | if (!connection->inputState.trackKey(keyEntry, dispatchEntry->resolvedAction, |
| 3169 | dispatchEntry->resolvedFlags)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3170 | if (DEBUG_DISPATCH_CYCLE) { |
| 3171 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent key " |
| 3172 | "event", |
| 3173 | connection->getInputChannelName().c_str()); |
| 3174 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3175 | return; // skip the inconsistent event |
| 3176 | } |
| 3177 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3178 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3179 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3180 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3181 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3182 | // Assign a default value to dispatchEntry that will never be generated by InputReader, |
| 3183 | // and assign a InputDispatcher value if it doesn't change in the if-else chain below. |
| 3184 | constexpr int32_t DEFAULT_RESOLVED_EVENT_ID = |
| 3185 | static_cast<int32_t>(IdGenerator::Source::OTHER); |
| 3186 | dispatchEntry->resolvedEventId = DEFAULT_RESOLVED_EVENT_ID; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3187 | if (dispatchMode.test(InputTarget::Flags::DISPATCH_AS_OUTSIDE)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3188 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3189 | } else if (dispatchMode.test(InputTarget::Flags::DISPATCH_AS_HOVER_EXIT)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3190 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3191 | } else if (dispatchMode.test(InputTarget::Flags::DISPATCH_AS_HOVER_ENTER)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3192 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3193 | } else if (dispatchMode.test(InputTarget::Flags::DISPATCH_AS_SLIPPERY_EXIT)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3194 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_CANCEL; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3195 | } else if (dispatchMode.test(InputTarget::Flags::DISPATCH_AS_SLIPPERY_ENTER)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3196 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_DOWN; |
| 3197 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3198 | dispatchEntry->resolvedAction = motionEntry.action; |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3199 | dispatchEntry->resolvedEventId = motionEntry.id; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3200 | } |
| 3201 | if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE && |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3202 | !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source, |
| 3203 | motionEntry.displayId)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3204 | if (DEBUG_DISPATCH_CYCLE) { |
| 3205 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: filling in missing hover " |
| 3206 | "enter event", |
| 3207 | connection->getInputChannelName().c_str()); |
| 3208 | } |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3209 | // We keep the 'resolvedEventId' here equal to the original 'motionEntry.id' because |
| 3210 | // this is a one-to-one event conversion. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3211 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 3212 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3213 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3214 | dispatchEntry->resolvedFlags = motionEntry.flags; |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 3215 | if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_CANCEL) { |
| 3216 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_CANCELED; |
| 3217 | } |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3218 | if (dispatchEntry->targetFlags.test(InputTarget::Flags::WINDOW_IS_OBSCURED)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3219 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; |
| 3220 | } |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3221 | if (dispatchEntry->targetFlags.test(InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3222 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 3223 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3224 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3225 | if (!connection->inputState.trackMotion(motionEntry, dispatchEntry->resolvedAction, |
| 3226 | dispatchEntry->resolvedFlags)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3227 | if (DEBUG_DISPATCH_CYCLE) { |
| 3228 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent motion " |
| 3229 | "event", |
| 3230 | connection->getInputChannelName().c_str()); |
| 3231 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3232 | return; // skip the inconsistent event |
| 3233 | } |
| 3234 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3235 | dispatchEntry->resolvedEventId = |
| 3236 | dispatchEntry->resolvedEventId == DEFAULT_RESOLVED_EVENT_ID |
| 3237 | ? mIdGenerator.nextId() |
| 3238 | : motionEntry.id; |
| 3239 | if (ATRACE_ENABLED() && dispatchEntry->resolvedEventId != motionEntry.id) { |
| 3240 | std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32 |
| 3241 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3242 | motionEntry.id, dispatchEntry->resolvedEventId); |
| 3243 | ATRACE_NAME(message.c_str()); |
| 3244 | } |
| 3245 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 3246 | if ((motionEntry.flags & AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) && |
| 3247 | (motionEntry.policyFlags & POLICY_FLAG_TRUSTED)) { |
| 3248 | // Skip reporting pointer down outside focus to the policy. |
| 3249 | break; |
| 3250 | } |
| 3251 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3252 | dispatchPointerDownOutsideFocus(motionEntry.source, dispatchEntry->resolvedAction, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3253 | inputTarget.inputChannel->getConnectionToken()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3254 | |
| 3255 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3256 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3257 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3258 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3259 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3260 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3261 | break; |
| 3262 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3263 | case EventEntry::Type::SENSOR: { |
| 3264 | LOG_ALWAYS_FATAL("SENSOR events should not go to apps via input channel"); |
| 3265 | break; |
| 3266 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3267 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 3268 | case EventEntry::Type::DEVICE_RESET: { |
| 3269 | LOG_ALWAYS_FATAL("%s events should not go to apps", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3270 | ftl::enum_string(newEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3271 | break; |
| 3272 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3273 | } |
| 3274 | |
| 3275 | // Remember that we are waiting for this dispatch to complete. |
| 3276 | if (dispatchEntry->hasForegroundTarget()) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3277 | incrementPendingForegroundDispatches(newEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3278 | } |
| 3279 | |
| 3280 | // Enqueue the dispatch entry. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3281 | connection->outboundQueue.push_back(dispatchEntry.release()); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3282 | traceOutboundQueueLength(*connection); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3283 | } |
| 3284 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3285 | /** |
| 3286 | * This function is purely for debugging. It helps us understand where the user interaction |
| 3287 | * was taking place. For example, if user is touching launcher, we will see a log that user |
| 3288 | * started interacting with launcher. In that example, the event would go to the wallpaper as well. |
| 3289 | * We will see both launcher and wallpaper in that list. |
| 3290 | * Once the interaction with a particular set of connections starts, no new logs will be printed |
| 3291 | * until the set of interacted connections changes. |
| 3292 | * |
| 3293 | * The following items are skipped, to reduce the logspam: |
| 3294 | * ACTION_OUTSIDE: any windows that are receiving ACTION_OUTSIDE are not logged |
| 3295 | * ACTION_UP: any windows that receive ACTION_UP are not logged (for both keys and motions). |
| 3296 | * This includes situations like the soft BACK button key. When the user releases (lifts up the |
| 3297 | * finger) the back button, then navigation bar will inject KEYCODE_BACK with ACTION_UP. |
| 3298 | * Both of those ACTION_UP events would not be logged |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3299 | */ |
| 3300 | void InputDispatcher::updateInteractionTokensLocked(const EventEntry& entry, |
| 3301 | const std::vector<InputTarget>& targets) { |
| 3302 | // Skip ACTION_UP events, and all events other than keys and motions |
| 3303 | if (entry.type == EventEntry::Type::KEY) { |
| 3304 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 3305 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
| 3306 | return; |
| 3307 | } |
| 3308 | } else if (entry.type == EventEntry::Type::MOTION) { |
| 3309 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 3310 | if (motionEntry.action == AMOTION_EVENT_ACTION_UP || |
| 3311 | motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
| 3312 | return; |
| 3313 | } |
| 3314 | } else { |
| 3315 | return; // Not a key or a motion |
| 3316 | } |
| 3317 | |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 3318 | std::unordered_set<sp<IBinder>, StrongPointerHash<IBinder>> newConnectionTokens; |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3319 | std::vector<std::shared_ptr<Connection>> newConnections; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3320 | for (const InputTarget& target : targets) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3321 | if (target.flags.test(InputTarget::Flags::DISPATCH_AS_OUTSIDE)) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3322 | continue; // Skip windows that receive ACTION_OUTSIDE |
| 3323 | } |
| 3324 | |
| 3325 | sp<IBinder> token = target.inputChannel->getConnectionToken(); |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3326 | std::shared_ptr<Connection> connection = getConnectionLocked(token); |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3327 | if (connection == nullptr) { |
| 3328 | continue; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3329 | } |
| 3330 | newConnectionTokens.insert(std::move(token)); |
| 3331 | newConnections.emplace_back(connection); |
| 3332 | } |
| 3333 | if (newConnectionTokens == mInteractionConnectionTokens) { |
| 3334 | return; // no change |
| 3335 | } |
| 3336 | mInteractionConnectionTokens = newConnectionTokens; |
| 3337 | |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3338 | std::string targetList; |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3339 | for (const std::shared_ptr<Connection>& connection : newConnections) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3340 | targetList += connection->getWindowName() + ", "; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3341 | } |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3342 | std::string message = "Interaction with: " + targetList; |
| 3343 | if (targetList.empty()) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3344 | message += "<none>"; |
| 3345 | } |
| 3346 | android_log_event_list(LOGTAG_INPUT_INTERACTION) << message << LOG_ID_EVENTS; |
| 3347 | } |
| 3348 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3349 | void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3350 | const sp<IBinder>& token) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3351 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3352 | uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK; |
| 3353 | if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3354 | return; |
| 3355 | } |
| 3356 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 3357 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3358 | if (focusedToken == token) { |
| 3359 | // ignore since token is focused |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3360 | return; |
| 3361 | } |
| 3362 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3363 | auto command = [this, token]() REQUIRES(mLock) { |
| 3364 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame^] | 3365 | mPolicy.onPointerDownOutsideFocus(token); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3366 | }; |
| 3367 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3368 | } |
| 3369 | |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3370 | status_t InputDispatcher::publishMotionEvent(Connection& connection, |
| 3371 | DispatchEntry& dispatchEntry) const { |
| 3372 | const EventEntry& eventEntry = *(dispatchEntry.eventEntry); |
| 3373 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 3374 | |
| 3375 | PointerCoords scaledCoords[MAX_POINTERS]; |
| 3376 | const PointerCoords* usingCoords = motionEntry.pointerCoords; |
| 3377 | |
| 3378 | // Set the X and Y offset and X and Y scale depending on the input source. |
| 3379 | if ((motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) && |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3380 | !(dispatchEntry.targetFlags.test(InputTarget::Flags::ZERO_COORDS))) { |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3381 | float globalScaleFactor = dispatchEntry.globalScaleFactor; |
| 3382 | if (globalScaleFactor != 1.0f) { |
| 3383 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
| 3384 | scaledCoords[i] = motionEntry.pointerCoords[i]; |
| 3385 | // Don't apply window scale here since we don't want scale to affect raw |
| 3386 | // coordinates. The scale will be sent back to the client and applied |
| 3387 | // later when requesting relative coordinates. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 3388 | scaledCoords[i].scale(globalScaleFactor, /*windowXScale=*/1, /*windowYScale=*/1); |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3389 | } |
| 3390 | usingCoords = scaledCoords; |
| 3391 | } |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3392 | } else if (dispatchEntry.targetFlags.test(InputTarget::Flags::ZERO_COORDS)) { |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3393 | // We don't want the dispatch target to know the coordinates |
| 3394 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
| 3395 | scaledCoords[i].clear(); |
| 3396 | } |
| 3397 | usingCoords = scaledCoords; |
| 3398 | } |
| 3399 | |
| 3400 | std::array<uint8_t, 32> hmac = getSignature(motionEntry, dispatchEntry); |
| 3401 | |
| 3402 | // Publish the motion event. |
| 3403 | return connection.inputPublisher |
| 3404 | .publishMotionEvent(dispatchEntry.seq, dispatchEntry.resolvedEventId, |
| 3405 | motionEntry.deviceId, motionEntry.source, motionEntry.displayId, |
| 3406 | std::move(hmac), dispatchEntry.resolvedAction, |
| 3407 | motionEntry.actionButton, dispatchEntry.resolvedFlags, |
| 3408 | motionEntry.edgeFlags, motionEntry.metaState, |
| 3409 | motionEntry.buttonState, motionEntry.classification, |
| 3410 | dispatchEntry.transform, motionEntry.xPrecision, |
| 3411 | motionEntry.yPrecision, motionEntry.xCursorPosition, |
| 3412 | motionEntry.yCursorPosition, dispatchEntry.rawTransform, |
| 3413 | motionEntry.downTime, motionEntry.eventTime, |
| 3414 | motionEntry.pointerCount, motionEntry.pointerProperties, |
| 3415 | usingCoords); |
| 3416 | } |
| 3417 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3418 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3419 | const std::shared_ptr<Connection>& connection) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3420 | if (ATRACE_ENABLED()) { |
| 3421 | std::string message = StringPrintf("startDispatchCycleLocked(inputChannel=%s)", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3422 | connection->getInputChannelName().c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3423 | ATRACE_NAME(message.c_str()); |
| 3424 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3425 | if (DEBUG_DISPATCH_CYCLE) { |
| 3426 | ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str()); |
| 3427 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3428 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3429 | while (connection->status == Connection::Status::NORMAL && !connection->outboundQueue.empty()) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3430 | DispatchEntry* dispatchEntry = connection->outboundQueue.front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3431 | dispatchEntry->deliveryTime = currentTime; |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3432 | const std::chrono::nanoseconds timeout = getDispatchingTimeoutLocked(connection); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3433 | dispatchEntry->timeoutTime = currentTime + timeout.count(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3434 | |
| 3435 | // Publish the event. |
| 3436 | status_t status; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3437 | const EventEntry& eventEntry = *(dispatchEntry->eventEntry); |
| 3438 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3439 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3440 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 3441 | std::array<uint8_t, 32> hmac = getSignature(keyEntry, *dispatchEntry); |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 3442 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 3443 | LOG(DEBUG) << "Publishing " << *dispatchEntry << " to " |
| 3444 | << connection->getInputChannelName(); |
| 3445 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3446 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3447 | // Publish the key event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3448 | status = connection->inputPublisher |
| 3449 | .publishKeyEvent(dispatchEntry->seq, |
| 3450 | dispatchEntry->resolvedEventId, keyEntry.deviceId, |
| 3451 | keyEntry.source, keyEntry.displayId, |
| 3452 | std::move(hmac), dispatchEntry->resolvedAction, |
| 3453 | dispatchEntry->resolvedFlags, keyEntry.keyCode, |
| 3454 | keyEntry.scanCode, keyEntry.metaState, |
| 3455 | keyEntry.repeatCount, keyEntry.downTime, |
| 3456 | keyEntry.eventTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3457 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3458 | } |
| 3459 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3460 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 3461 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 3462 | LOG(DEBUG) << "Publishing " << *dispatchEntry << " to " |
| 3463 | << connection->getInputChannelName(); |
| 3464 | } |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3465 | status = publishMotionEvent(*connection, *dispatchEntry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3466 | break; |
| 3467 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3468 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3469 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3470 | const FocusEntry& focusEntry = static_cast<const FocusEntry&>(eventEntry); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3471 | status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3472 | focusEntry.id, |
Antonio Kantek | 3cfec7b | 2021-11-05 18:26:17 -0700 | [diff] [blame] | 3473 | focusEntry.hasFocus); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3474 | break; |
| 3475 | } |
| 3476 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3477 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 3478 | const TouchModeEntry& touchModeEntry = |
| 3479 | static_cast<const TouchModeEntry&>(eventEntry); |
| 3480 | status = connection->inputPublisher |
| 3481 | .publishTouchModeEvent(dispatchEntry->seq, touchModeEntry.id, |
| 3482 | touchModeEntry.inTouchMode); |
| 3483 | |
| 3484 | break; |
| 3485 | } |
| 3486 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3487 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 3488 | const auto& captureEntry = |
| 3489 | static_cast<const PointerCaptureChangedEntry&>(eventEntry); |
| 3490 | status = connection->inputPublisher |
| 3491 | .publishCaptureEvent(dispatchEntry->seq, captureEntry.id, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 3492 | captureEntry.pointerCaptureRequest.enable); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3493 | break; |
| 3494 | } |
| 3495 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3496 | case EventEntry::Type::DRAG: { |
| 3497 | const DragEntry& dragEntry = static_cast<const DragEntry&>(eventEntry); |
| 3498 | status = connection->inputPublisher.publishDragEvent(dispatchEntry->seq, |
| 3499 | dragEntry.id, dragEntry.x, |
| 3500 | dragEntry.y, |
| 3501 | dragEntry.isExiting); |
| 3502 | break; |
| 3503 | } |
| 3504 | |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3505 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3506 | case EventEntry::Type::DEVICE_RESET: |
| 3507 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3508 | LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3509 | ftl::enum_string(eventEntry.type).c_str()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3510 | return; |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3511 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3512 | } |
| 3513 | |
| 3514 | // Check the result. |
| 3515 | if (status) { |
| 3516 | if (status == WOULD_BLOCK) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3517 | if (connection->waitQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3518 | ALOGE("channel '%s' ~ Could not publish event because the pipe is full. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3519 | "This is unexpected because the wait queue is empty, so the pipe " |
| 3520 | "should be empty and we shouldn't have any problems writing an " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3521 | "event to it, status=%s(%d)", |
| 3522 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3523 | status); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 3524 | abortBrokenDispatchCycleLocked(currentTime, connection, /*notify=*/true); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3525 | } else { |
| 3526 | // Pipe is full and we are waiting for the app to finish process some events |
| 3527 | // before sending more events to it. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3528 | if (DEBUG_DISPATCH_CYCLE) { |
| 3529 | ALOGD("channel '%s' ~ Could not publish event because the pipe is full, " |
| 3530 | "waiting for the application to catch up", |
| 3531 | connection->getInputChannelName().c_str()); |
| 3532 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3533 | } |
| 3534 | } else { |
| 3535 | ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3536 | "status=%s(%d)", |
| 3537 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3538 | status); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 3539 | abortBrokenDispatchCycleLocked(currentTime, connection, /*notify=*/true); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3540 | } |
| 3541 | return; |
| 3542 | } |
| 3543 | |
| 3544 | // Re-enqueue the event on the wait queue. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3545 | connection->outboundQueue.erase(std::remove(connection->outboundQueue.begin(), |
| 3546 | connection->outboundQueue.end(), |
| 3547 | dispatchEntry)); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3548 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3549 | connection->waitQueue.push_back(dispatchEntry); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3550 | if (connection->responsive) { |
| 3551 | mAnrTracker.insert(dispatchEntry->timeoutTime, |
| 3552 | connection->inputChannel->getConnectionToken()); |
| 3553 | } |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3554 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3555 | } |
| 3556 | } |
| 3557 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3558 | std::array<uint8_t, 32> InputDispatcher::sign(const VerifiedInputEvent& event) const { |
| 3559 | size_t size; |
| 3560 | switch (event.type) { |
| 3561 | case VerifiedInputEvent::Type::KEY: { |
| 3562 | size = sizeof(VerifiedKeyEvent); |
| 3563 | break; |
| 3564 | } |
| 3565 | case VerifiedInputEvent::Type::MOTION: { |
| 3566 | size = sizeof(VerifiedMotionEvent); |
| 3567 | break; |
| 3568 | } |
| 3569 | } |
| 3570 | const uint8_t* start = reinterpret_cast<const uint8_t*>(&event); |
| 3571 | return mHmacKeyManager.sign(start, size); |
| 3572 | } |
| 3573 | |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3574 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3575 | const MotionEntry& motionEntry, const DispatchEntry& dispatchEntry) const { |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3576 | const int32_t actionMasked = dispatchEntry.resolvedAction & AMOTION_EVENT_ACTION_MASK; |
| 3577 | if (actionMasked != AMOTION_EVENT_ACTION_UP && actionMasked != AMOTION_EVENT_ACTION_DOWN) { |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3578 | // Only sign events up and down events as the purely move events |
| 3579 | // are tied to their up/down counterparts so signing would be redundant. |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3580 | return INVALID_HMAC; |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3581 | } |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3582 | |
| 3583 | VerifiedMotionEvent verifiedEvent = |
| 3584 | verifiedMotionEventFromMotionEntry(motionEntry, dispatchEntry.rawTransform); |
| 3585 | verifiedEvent.actionMasked = actionMasked; |
| 3586 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS; |
| 3587 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3588 | } |
| 3589 | |
| 3590 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3591 | const KeyEntry& keyEntry, const DispatchEntry& dispatchEntry) const { |
| 3592 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(keyEntry); |
| 3593 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_KEY_EVENT_FLAGS; |
| 3594 | verifiedEvent.action = dispatchEntry.resolvedAction; |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3595 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3596 | } |
| 3597 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3598 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3599 | const std::shared_ptr<Connection>& connection, |
| 3600 | uint32_t seq, bool handled, nsecs_t consumeTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3601 | if (DEBUG_DISPATCH_CYCLE) { |
| 3602 | ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s", |
| 3603 | connection->getInputChannelName().c_str(), seq, toString(handled)); |
| 3604 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3605 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3606 | if (connection->status == Connection::Status::BROKEN || |
| 3607 | connection->status == Connection::Status::ZOMBIE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3608 | return; |
| 3609 | } |
| 3610 | |
| 3611 | // Notify other system components and prepare to start the next dispatch cycle. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3612 | auto command = [this, currentTime, connection, seq, handled, consumeTime]() REQUIRES(mLock) { |
| 3613 | doDispatchCycleFinishedCommand(currentTime, connection, seq, handled, consumeTime); |
| 3614 | }; |
| 3615 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3616 | } |
| 3617 | |
| 3618 | void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3619 | const std::shared_ptr<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3620 | bool notify) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3621 | if (DEBUG_DISPATCH_CYCLE) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 3622 | LOG(DEBUG) << "channel '" << connection->getInputChannelName() << "'~ " << __func__ |
| 3623 | << " - notify=" << toString(notify); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3624 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3625 | |
| 3626 | // Clear the dispatch queues. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3627 | drainDispatchQueue(connection->outboundQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3628 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3629 | drainDispatchQueue(connection->waitQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3630 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3631 | |
| 3632 | // The connection appears to be unrecoverably broken. |
| 3633 | // Ignore already broken or zombie connections. |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3634 | if (connection->status == Connection::Status::NORMAL) { |
| 3635 | connection->status = Connection::Status::BROKEN; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3636 | |
| 3637 | if (notify) { |
| 3638 | // Notify other system components. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3639 | ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", |
| 3640 | connection->getInputChannelName().c_str()); |
| 3641 | |
| 3642 | auto command = [this, connection]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3643 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame^] | 3644 | mPolicy.notifyInputChannelBroken(connection->inputChannel->getConnectionToken()); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3645 | }; |
| 3646 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3647 | } |
| 3648 | } |
| 3649 | } |
| 3650 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3651 | void InputDispatcher::drainDispatchQueue(std::deque<DispatchEntry*>& queue) { |
| 3652 | while (!queue.empty()) { |
| 3653 | DispatchEntry* dispatchEntry = queue.front(); |
| 3654 | queue.pop_front(); |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3655 | releaseDispatchEntry(dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3656 | } |
| 3657 | } |
| 3658 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3659 | void InputDispatcher::releaseDispatchEntry(DispatchEntry* dispatchEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3660 | if (dispatchEntry->hasForegroundTarget()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3661 | decrementPendingForegroundDispatches(*(dispatchEntry->eventEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3662 | } |
| 3663 | delete dispatchEntry; |
| 3664 | } |
| 3665 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3666 | int InputDispatcher::handleReceiveCallback(int events, sp<IBinder> connectionToken) { |
| 3667 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3668 | std::shared_ptr<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3669 | if (connection == nullptr) { |
| 3670 | ALOGW("Received looper callback for unknown input channel token %p. events=0x%x", |
| 3671 | connectionToken.get(), events); |
| 3672 | return 0; // remove the callback |
| 3673 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3674 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3675 | bool notify; |
| 3676 | if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { |
| 3677 | if (!(events & ALOOPER_EVENT_INPUT)) { |
| 3678 | ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. " |
| 3679 | "events=0x%x", |
| 3680 | connection->getInputChannelName().c_str(), events); |
| 3681 | return 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3682 | } |
| 3683 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3684 | nsecs_t currentTime = now(); |
| 3685 | bool gotOne = false; |
| 3686 | status_t status = OK; |
| 3687 | for (;;) { |
| 3688 | Result<InputPublisher::ConsumerResponse> result = |
| 3689 | connection->inputPublisher.receiveConsumerResponse(); |
| 3690 | if (!result.ok()) { |
| 3691 | status = result.error().code(); |
| 3692 | break; |
| 3693 | } |
| 3694 | |
| 3695 | if (std::holds_alternative<InputPublisher::Finished>(*result)) { |
| 3696 | const InputPublisher::Finished& finish = |
| 3697 | std::get<InputPublisher::Finished>(*result); |
| 3698 | finishDispatchCycleLocked(currentTime, connection, finish.seq, finish.handled, |
| 3699 | finish.consumeTime); |
| 3700 | } else if (std::holds_alternative<InputPublisher::Timeline>(*result)) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3701 | if (shouldReportMetricsForConnection(*connection)) { |
| 3702 | const InputPublisher::Timeline& timeline = |
| 3703 | std::get<InputPublisher::Timeline>(*result); |
| 3704 | mLatencyTracker |
| 3705 | .trackGraphicsLatency(timeline.inputEventId, |
| 3706 | connection->inputChannel->getConnectionToken(), |
| 3707 | std::move(timeline.graphicsTimeline)); |
| 3708 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3709 | } |
| 3710 | gotOne = true; |
| 3711 | } |
| 3712 | if (gotOne) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3713 | runCommandsLockedInterruptable(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3714 | if (status == WOULD_BLOCK) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3715 | return 1; |
| 3716 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3717 | } |
| 3718 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3719 | notify = status != DEAD_OBJECT || !connection->monitor; |
| 3720 | if (notify) { |
| 3721 | ALOGE("channel '%s' ~ Failed to receive finished signal. status=%s(%d)", |
| 3722 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3723 | status); |
| 3724 | } |
| 3725 | } else { |
| 3726 | // Monitor channels are never explicitly unregistered. |
| 3727 | // We do it automatically when the remote endpoint is closed so don't warn about them. |
| 3728 | const bool stillHaveWindowHandle = |
| 3729 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()) != nullptr; |
| 3730 | notify = !connection->monitor && stillHaveWindowHandle; |
| 3731 | if (notify) { |
| 3732 | ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred. events=0x%x", |
| 3733 | connection->getInputChannelName().c_str(), events); |
| 3734 | } |
| 3735 | } |
| 3736 | |
| 3737 | // Remove the channel. |
| 3738 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), notify); |
| 3739 | return 0; // remove the callback |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3740 | } |
| 3741 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3742 | void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3743 | const CancelationOptions& options) { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3744 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 3745 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3746 | } |
| 3747 | } |
| 3748 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3749 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3750 | const CancelationOptions& options) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 3751 | for (const auto& [_, monitors] : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3752 | for (const Monitor& monitor : monitors) { |
| 3753 | synthesizeCancelationEventsForInputChannelLocked(monitor.inputChannel, options); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3754 | } |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3755 | } |
| 3756 | } |
| 3757 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3758 | void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked( |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 3759 | const std::shared_ptr<InputChannel>& channel, const CancelationOptions& options) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3760 | std::shared_ptr<Connection> connection = getConnectionLocked(channel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3761 | if (connection == nullptr) { |
| 3762 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3763 | } |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3764 | |
| 3765 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3766 | } |
| 3767 | |
| 3768 | void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3769 | const std::shared_ptr<Connection>& connection, const CancelationOptions& options) { |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3770 | if (connection->status == Connection::Status::BROKEN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3771 | return; |
| 3772 | } |
| 3773 | |
| 3774 | nsecs_t currentTime = now(); |
| 3775 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3776 | std::vector<std::unique_ptr<EventEntry>> cancelationEvents = |
Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 3777 | connection->inputState.synthesizeCancelationEvents(currentTime, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3778 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3779 | if (cancelationEvents.empty()) { |
| 3780 | return; |
| 3781 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3782 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 3783 | 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] | 3784 | "with reality: %s, mode=%s.", |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3785 | connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason, |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3786 | ftl::enum_string(options.mode).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3787 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3788 | |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3789 | std::string reason = std::string("reason=").append(options.reason); |
| 3790 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 3791 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 3792 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3793 | InputTarget target; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3794 | sp<WindowInfoHandle> windowHandle = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3795 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3796 | if (windowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3797 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3798 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3799 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3800 | } |
| 3801 | target.inputChannel = connection->inputChannel; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3802 | target.flags = InputTarget::Flags::DISPATCH_AS_IS; |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3803 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 3804 | const bool wasEmpty = connection->outboundQueue.empty(); |
| 3805 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3806 | for (size_t i = 0; i < cancelationEvents.size(); i++) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3807 | std::unique_ptr<EventEntry> cancelationEventEntry = std::move(cancelationEvents[i]); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3808 | switch (cancelationEventEntry->type) { |
| 3809 | case EventEntry::Type::KEY: { |
| 3810 | logOutboundKeyDetails("cancel - ", |
| 3811 | static_cast<const KeyEntry&>(*cancelationEventEntry)); |
| 3812 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3813 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3814 | case EventEntry::Type::MOTION: { |
| 3815 | logOutboundMotionDetails("cancel - ", |
| 3816 | static_cast<const MotionEntry&>(*cancelationEventEntry)); |
| 3817 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3818 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3819 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3820 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3821 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3822 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3823 | LOG_ALWAYS_FATAL("Canceling %s events is not supported", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3824 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3825 | break; |
| 3826 | } |
| 3827 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3828 | case EventEntry::Type::DEVICE_RESET: |
| 3829 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3830 | 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] | 3831 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3832 | break; |
| 3833 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3834 | } |
| 3835 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3836 | enqueueDispatchEntryLocked(connection, std::move(cancelationEventEntry), target, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3837 | InputTarget::Flags::DISPATCH_AS_IS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3838 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3839 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 3840 | // If the outbound queue was previously empty, start the dispatch cycle going. |
| 3841 | if (wasEmpty && !connection->outboundQueue.empty()) { |
| 3842 | startDispatchCycleLocked(currentTime, connection); |
| 3843 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3844 | } |
| 3845 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3846 | void InputDispatcher::synthesizePointerDownEventsForConnectionLocked( |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3847 | const nsecs_t downTime, const std::shared_ptr<Connection>& connection, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 3848 | ftl::Flags<InputTarget::Flags> targetFlags) { |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3849 | if (connection->status == Connection::Status::BROKEN) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3850 | return; |
| 3851 | } |
| 3852 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3853 | std::vector<std::unique_ptr<EventEntry>> downEvents = |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3854 | connection->inputState.synthesizePointerDownEvents(downTime); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3855 | |
| 3856 | if (downEvents.empty()) { |
| 3857 | return; |
| 3858 | } |
| 3859 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3860 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3861 | ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.", |
| 3862 | connection->getInputChannelName().c_str(), downEvents.size()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3863 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3864 | |
| 3865 | InputTarget target; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3866 | sp<WindowInfoHandle> windowHandle = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3867 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3868 | if (windowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3869 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3870 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3871 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3872 | } |
| 3873 | target.inputChannel = connection->inputChannel; |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 3874 | target.flags = targetFlags; |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3875 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 3876 | const bool wasEmpty = connection->outboundQueue.empty(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3877 | for (std::unique_ptr<EventEntry>& downEventEntry : downEvents) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3878 | switch (downEventEntry->type) { |
| 3879 | case EventEntry::Type::MOTION: { |
| 3880 | logOutboundMotionDetails("down - ", |
| 3881 | static_cast<const MotionEntry&>(*downEventEntry)); |
| 3882 | break; |
| 3883 | } |
| 3884 | |
| 3885 | case EventEntry::Type::KEY: |
| 3886 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3887 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3888 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3889 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3890 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3891 | case EventEntry::Type::SENSOR: |
| 3892 | case EventEntry::Type::DRAG: { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3893 | 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] | 3894 | ftl::enum_string(downEventEntry->type).c_str()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3895 | break; |
| 3896 | } |
| 3897 | } |
| 3898 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3899 | enqueueDispatchEntryLocked(connection, std::move(downEventEntry), target, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3900 | InputTarget::Flags::DISPATCH_AS_IS); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3901 | } |
| 3902 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 3903 | // If the outbound queue was previously empty, start the dispatch cycle going. |
| 3904 | if (wasEmpty && !connection->outboundQueue.empty()) { |
| 3905 | startDispatchCycleLocked(downTime, connection); |
| 3906 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3907 | } |
| 3908 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 3909 | void InputDispatcher::synthesizeCancelationEventsForWindowLocked( |
| 3910 | const sp<WindowInfoHandle>& windowHandle, const CancelationOptions& options) { |
| 3911 | if (windowHandle != nullptr) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 3912 | std::shared_ptr<Connection> wallpaperConnection = |
| 3913 | getConnectionLocked(windowHandle->getToken()); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 3914 | if (wallpaperConnection != nullptr) { |
| 3915 | synthesizeCancelationEventsForConnectionLocked(wallpaperConnection, options); |
| 3916 | } |
| 3917 | } |
| 3918 | } |
| 3919 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3920 | std::unique_ptr<MotionEntry> InputDispatcher::splitMotionEvent( |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 3921 | const MotionEntry& originalMotionEntry, std::bitset<MAX_POINTER_ID + 1> pointerIds, |
| 3922 | nsecs_t splitDownTime) { |
| 3923 | ALOG_ASSERT(pointerIds.any()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3924 | |
| 3925 | uint32_t splitPointerIndexMap[MAX_POINTERS]; |
| 3926 | PointerProperties splitPointerProperties[MAX_POINTERS]; |
| 3927 | PointerCoords splitPointerCoords[MAX_POINTERS]; |
| 3928 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3929 | uint32_t originalPointerCount = originalMotionEntry.pointerCount; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3930 | uint32_t splitPointerCount = 0; |
| 3931 | |
| 3932 | for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3933 | originalPointerIndex++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3934 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3935 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3936 | uint32_t pointerId = uint32_t(pointerProperties.id); |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 3937 | if (pointerIds.test(pointerId)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3938 | splitPointerIndexMap[splitPointerCount] = originalPointerIndex; |
| 3939 | splitPointerProperties[splitPointerCount].copyFrom(pointerProperties); |
| 3940 | splitPointerCoords[splitPointerCount].copyFrom( |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3941 | originalMotionEntry.pointerCoords[originalPointerIndex]); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3942 | splitPointerCount += 1; |
| 3943 | } |
| 3944 | } |
| 3945 | |
| 3946 | if (splitPointerCount != pointerIds.count()) { |
| 3947 | // This is bad. We are missing some of the pointers that we expected to deliver. |
| 3948 | // Most likely this indicates that we received an ACTION_MOVE events that has |
| 3949 | // different pointer ids than we expected based on the previous ACTION_DOWN |
| 3950 | // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers |
| 3951 | // in this way. |
| 3952 | ALOGW("Dropping split motion event because the pointer count is %d but " |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 3953 | "we expected there to be %zu pointers. This probably means we received " |
Siarhei Vishniakou | 16e4fa0 | 2023-02-16 17:48:56 -0800 | [diff] [blame] | 3954 | "a broken sequence of pointer ids from the input device: %s", |
| 3955 | splitPointerCount, pointerIds.count(), originalMotionEntry.getDescription().c_str()); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3956 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3957 | } |
| 3958 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3959 | int32_t action = originalMotionEntry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3960 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3961 | if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN || |
| 3962 | maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3963 | int32_t originalPointerIndex = getMotionEventActionPointerIndex(action); |
| 3964 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3965 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3966 | uint32_t pointerId = uint32_t(pointerProperties.id); |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 3967 | if (pointerIds.test(pointerId)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3968 | if (pointerIds.count() == 1) { |
| 3969 | // The first/last pointer went down/up. |
| 3970 | action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3971 | ? AMOTION_EVENT_ACTION_DOWN |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3972 | : (originalMotionEntry.flags & AMOTION_EVENT_FLAG_CANCELED) != 0 |
| 3973 | ? AMOTION_EVENT_ACTION_CANCEL |
| 3974 | : AMOTION_EVENT_ACTION_UP; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3975 | } else { |
| 3976 | // A secondary pointer went down/up. |
| 3977 | uint32_t splitPointerIndex = 0; |
| 3978 | while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { |
| 3979 | splitPointerIndex += 1; |
| 3980 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3981 | action = maskedAction | |
| 3982 | (splitPointerIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3983 | } |
| 3984 | } else { |
| 3985 | // An unrelated pointer changed. |
| 3986 | action = AMOTION_EVENT_ACTION_MOVE; |
| 3987 | } |
| 3988 | } |
| 3989 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3990 | if (action == AMOTION_EVENT_ACTION_DOWN) { |
| 3991 | LOG_ALWAYS_FATAL_IF(splitDownTime != originalMotionEntry.eventTime, |
| 3992 | "Split motion event has mismatching downTime and eventTime for " |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3993 | "ACTION_DOWN, motionEntry=%s, splitDownTime=%" PRId64, |
| 3994 | originalMotionEntry.getDescription().c_str(), splitDownTime); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3995 | } |
| 3996 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3997 | int32_t newId = mIdGenerator.nextId(); |
| 3998 | if (ATRACE_ENABLED()) { |
| 3999 | std::string message = StringPrintf("Split MotionEvent(id=0x%" PRIx32 |
| 4000 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 4001 | originalMotionEntry.id, newId); |
| 4002 | ATRACE_NAME(message.c_str()); |
| 4003 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4004 | std::unique_ptr<MotionEntry> splitMotionEntry = |
| 4005 | std::make_unique<MotionEntry>(newId, originalMotionEntry.eventTime, |
| 4006 | originalMotionEntry.deviceId, originalMotionEntry.source, |
| 4007 | originalMotionEntry.displayId, |
| 4008 | originalMotionEntry.policyFlags, action, |
| 4009 | originalMotionEntry.actionButton, |
| 4010 | originalMotionEntry.flags, originalMotionEntry.metaState, |
| 4011 | originalMotionEntry.buttonState, |
| 4012 | originalMotionEntry.classification, |
| 4013 | originalMotionEntry.edgeFlags, |
| 4014 | originalMotionEntry.xPrecision, |
| 4015 | originalMotionEntry.yPrecision, |
| 4016 | originalMotionEntry.xCursorPosition, |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4017 | originalMotionEntry.yCursorPosition, splitDownTime, |
| 4018 | splitPointerCount, splitPointerProperties, |
| 4019 | splitPointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4020 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 4021 | if (originalMotionEntry.injectionState) { |
| 4022 | splitMotionEntry->injectionState = originalMotionEntry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4023 | splitMotionEntry->injectionState->refCount += 1; |
| 4024 | } |
| 4025 | |
| 4026 | return splitMotionEntry; |
| 4027 | } |
| 4028 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4029 | void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4030 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4031 | ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args.eventTime); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4032 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4033 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4034 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4035 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4036 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4037 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4038 | std::unique_ptr<ConfigurationChangedEntry> newEntry = |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4039 | std::make_unique<ConfigurationChangedEntry>(args.id, args.eventTime); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4040 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4041 | } // release lock |
| 4042 | |
| 4043 | if (needWake) { |
| 4044 | mLooper->wake(); |
| 4045 | } |
| 4046 | } |
| 4047 | |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 4048 | /** |
| 4049 | * If one of the meta shortcuts is detected, process them here: |
Vaibhav Devmurari | 34cd5b0 | 2023-02-23 14:51:18 +0000 | [diff] [blame] | 4050 | * Meta + Backspace; Meta + Grave; Meta + Left arrow -> generate BACK |
| 4051 | * Most System shortcuts are handled in PhoneWindowManager.java except 'Back' shortcuts. Unlike |
| 4052 | * Back, other shortcuts DO NOT need to be sent to applications and are fully handled by the system. |
| 4053 | * But for Back key and Back shortcuts, we need to send KEYCODE_BACK to applications which can |
| 4054 | * potentially handle the back key presses. |
| 4055 | * Note: We don't send any Meta based KeyEvents to applications, so we need to convert to a KeyEvent |
| 4056 | * where meta modifier is off before sending. Currently only use case is 'Back'. |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 4057 | */ |
| 4058 | void InputDispatcher::accelerateMetaShortcuts(const int32_t deviceId, const int32_t action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4059 | int32_t& keyCode, int32_t& metaState) { |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 4060 | if (metaState & AMETA_META_ON && action == AKEY_EVENT_ACTION_DOWN) { |
| 4061 | int32_t newKeyCode = AKEYCODE_UNKNOWN; |
Vaibhav Devmurari | 34cd5b0 | 2023-02-23 14:51:18 +0000 | [diff] [blame] | 4062 | if (keyCode == AKEYCODE_DEL || keyCode == AKEYCODE_GRAVE || keyCode == AKEYCODE_DPAD_LEFT) { |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 4063 | newKeyCode = AKEYCODE_BACK; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 4064 | } |
| 4065 | if (newKeyCode != AKEYCODE_UNKNOWN) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4066 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 4067 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4068 | mReplacedKeys[replacement] = newKeyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 4069 | keyCode = newKeyCode; |
| 4070 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 4071 | } |
| 4072 | } else if (action == AKEY_EVENT_ACTION_UP) { |
| 4073 | // In order to maintain a consistent stream of up and down events, check to see if the key |
| 4074 | // going up is one we've replaced in a down event and haven't yet replaced in an up event, |
| 4075 | // even if the modifier was released between the down and the up events. |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4076 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 4077 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4078 | auto replacementIt = mReplacedKeys.find(replacement); |
| 4079 | if (replacementIt != mReplacedKeys.end()) { |
| 4080 | keyCode = replacementIt->second; |
| 4081 | mReplacedKeys.erase(replacementIt); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 4082 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 4083 | } |
| 4084 | } |
| 4085 | } |
| 4086 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4087 | void InputDispatcher::notifyKey(const NotifyKeyArgs& args) { |
Prabir Pradhan | 96282b0 | 2023-02-24 22:36:17 +0000 | [diff] [blame] | 4088 | ALOGD_IF(debugInboundEventDetails(), |
| 4089 | "notifyKey - id=%" PRIx32 ", eventTime=%" PRId64 |
| 4090 | ", deviceId=%d, source=%s, displayId=%" PRId32 |
| 4091 | "policyFlags=0x%x, action=%s, flags=0x%x, keyCode=%s, scanCode=0x%x, metaState=0x%x, " |
| 4092 | "downTime=%" PRId64, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4093 | args.id, args.eventTime, args.deviceId, inputEventSourceToString(args.source).c_str(), |
| 4094 | args.displayId, args.policyFlags, KeyEvent::actionToString(args.action), args.flags, |
| 4095 | KeyEvent::getLabel(args.keyCode), args.scanCode, args.metaState, args.downTime); |
| 4096 | if (!validateKeyEvent(args.action)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4097 | return; |
| 4098 | } |
| 4099 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4100 | uint32_t policyFlags = args.policyFlags; |
| 4101 | int32_t flags = args.flags; |
| 4102 | int32_t metaState = args.metaState; |
Siarhei Vishniakou | 622bd32 | 2018-10-29 18:02:27 -0700 | [diff] [blame] | 4103 | // InputDispatcher tracks and generates key repeats on behalf of |
| 4104 | // whatever notifies it, so repeatCount should always be set to 0 |
| 4105 | constexpr int32_t repeatCount = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4106 | if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { |
| 4107 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 4108 | flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
| 4109 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4110 | if (policyFlags & POLICY_FLAG_FUNCTION) { |
| 4111 | metaState |= AMETA_FUNCTION_ON; |
| 4112 | } |
| 4113 | |
| 4114 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 4115 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4116 | int32_t keyCode = args.keyCode; |
| 4117 | accelerateMetaShortcuts(args.deviceId, args.action, keyCode, metaState); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 4118 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4119 | KeyEvent event; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4120 | event.initialize(args.id, args.deviceId, args.source, args.displayId, INVALID_HMAC, args.action, |
| 4121 | flags, keyCode, args.scanCode, metaState, repeatCount, args.downTime, |
| 4122 | args.eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4123 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4124 | android::base::Timer t; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame^] | 4125 | mPolicy.interceptKeyBeforeQueueing(event, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4126 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4127 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4128 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4129 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4130 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4131 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4132 | { // acquire lock |
| 4133 | mLock.lock(); |
| 4134 | |
| 4135 | if (shouldSendKeyToInputFilterLocked(args)) { |
| 4136 | mLock.unlock(); |
| 4137 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4138 | policyFlags |= POLICY_FLAG_FILTERED; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame^] | 4139 | if (!mPolicy.filterInputEvent(event, policyFlags)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4140 | return; // event was consumed by the filter |
| 4141 | } |
| 4142 | |
| 4143 | mLock.lock(); |
| 4144 | } |
| 4145 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4146 | std::unique_ptr<KeyEntry> newEntry = |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4147 | std::make_unique<KeyEntry>(args.id, args.eventTime, args.deviceId, args.source, |
| 4148 | args.displayId, policyFlags, args.action, flags, keyCode, |
| 4149 | args.scanCode, metaState, repeatCount, args.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4150 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4151 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4152 | mLock.unlock(); |
| 4153 | } // release lock |
| 4154 | |
| 4155 | if (needWake) { |
| 4156 | mLooper->wake(); |
| 4157 | } |
| 4158 | } |
| 4159 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4160 | bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs& args) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4161 | return mInputFilterEnabled; |
| 4162 | } |
| 4163 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4164 | void InputDispatcher::notifyMotion(const NotifyMotionArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4165 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 96282b0 | 2023-02-24 22:36:17 +0000 | [diff] [blame] | 4166 | ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=%s, " |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4167 | "displayId=%" PRId32 ", policyFlags=0x%x, " |
Siarhei Vishniakou | 6ebd069 | 2022-10-20 15:05:45 -0700 | [diff] [blame] | 4168 | "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] | 4169 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, xCursorPosition=%f, " |
| 4170 | "yCursorPosition=%f, downTime=%" PRId64, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4171 | args.id, args.eventTime, args.deviceId, inputEventSourceToString(args.source).c_str(), |
| 4172 | args.displayId, args.policyFlags, MotionEvent::actionToString(args.action).c_str(), |
| 4173 | args.actionButton, args.flags, args.metaState, args.buttonState, args.edgeFlags, |
| 4174 | args.xPrecision, args.yPrecision, args.xCursorPosition, args.yCursorPosition, |
| 4175 | args.downTime); |
| 4176 | for (uint32_t i = 0; i < args.pointerCount; i++) { |
Prabir Pradhan | 96282b0 | 2023-02-24 22:36:17 +0000 | [diff] [blame] | 4177 | ALOGD(" Pointer %d: id=%d, toolType=%s, x=%f, y=%f, pressure=%f, size=%f, " |
| 4178 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, orientation=%f", |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4179 | i, args.pointerProperties[i].id, |
| 4180 | ftl::enum_string(args.pointerProperties[i].toolType).c_str(), |
| 4181 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 4182 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 4183 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 4184 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 4185 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 4186 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 4187 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 4188 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 4189 | args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4190 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4191 | } |
Siarhei Vishniakou | 4ca9727 | 2023-03-01 11:31:35 -0800 | [diff] [blame] | 4192 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4193 | if (!validateMotionEvent(args.action, args.actionButton, args.pointerCount, |
| 4194 | args.pointerProperties)) { |
| 4195 | LOG(ERROR) << "Invalid event: " << args.dump(); |
Siarhei Vishniakou | 4ca9727 | 2023-03-01 11:31:35 -0800 | [diff] [blame] | 4196 | return; |
| 4197 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4198 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4199 | uint32_t policyFlags = args.policyFlags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4200 | policyFlags |= POLICY_FLAG_TRUSTED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4201 | |
| 4202 | android::base::Timer t; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame^] | 4203 | mPolicy.interceptMotionBeforeQueueing(args.displayId, args.eventTime, policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4204 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4205 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4206 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4207 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4208 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4209 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4210 | { // acquire lock |
| 4211 | mLock.lock(); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 4212 | if (!(policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
| 4213 | // Set the flag anyway if we already have an ongoing gesture. That would allow us to |
| 4214 | // complete the processing of the current stroke. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4215 | const auto touchStateIt = mTouchStatesByDisplay.find(args.displayId); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 4216 | if (touchStateIt != mTouchStatesByDisplay.end()) { |
| 4217 | const TouchState& touchState = touchStateIt->second; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4218 | if (touchState.deviceId == args.deviceId && touchState.isDown()) { |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 4219 | policyFlags |= POLICY_FLAG_PASS_TO_USER; |
| 4220 | } |
| 4221 | } |
| 4222 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4223 | |
| 4224 | if (shouldSendMotionToInputFilterLocked(args)) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4225 | ui::Transform displayTransform; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4226 | if (const auto it = mDisplayInfos.find(args.displayId); it != mDisplayInfos.end()) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4227 | displayTransform = it->second.transform; |
| 4228 | } |
| 4229 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4230 | mLock.unlock(); |
| 4231 | |
| 4232 | MotionEvent event; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4233 | event.initialize(args.id, args.deviceId, args.source, args.displayId, INVALID_HMAC, |
| 4234 | args.action, args.actionButton, args.flags, args.edgeFlags, |
| 4235 | args.metaState, args.buttonState, args.classification, |
| 4236 | displayTransform, args.xPrecision, args.yPrecision, |
| 4237 | args.xCursorPosition, args.yCursorPosition, displayTransform, |
| 4238 | args.downTime, args.eventTime, args.pointerCount, |
| 4239 | args.pointerProperties, args.pointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4240 | |
| 4241 | policyFlags |= POLICY_FLAG_FILTERED; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame^] | 4242 | if (!mPolicy.filterInputEvent(event, policyFlags)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4243 | return; // event was consumed by the filter |
| 4244 | } |
| 4245 | |
| 4246 | mLock.lock(); |
| 4247 | } |
| 4248 | |
| 4249 | // Just enqueue a new motion event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4250 | std::unique_ptr<MotionEntry> newEntry = |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4251 | std::make_unique<MotionEntry>(args.id, args.eventTime, args.deviceId, args.source, |
| 4252 | args.displayId, policyFlags, args.action, |
| 4253 | args.actionButton, args.flags, args.metaState, |
| 4254 | args.buttonState, args.classification, args.edgeFlags, |
| 4255 | args.xPrecision, args.yPrecision, |
| 4256 | args.xCursorPosition, args.yCursorPosition, |
| 4257 | args.downTime, args.pointerCount, |
| 4258 | args.pointerProperties, args.pointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4259 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4260 | if (args.id != android::os::IInputConstants::INVALID_INPUT_EVENT_ID && |
| 4261 | IdGenerator::getSource(args.id) == IdGenerator::Source::INPUT_READER && |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 4262 | !mInputFilterEnabled) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4263 | const bool isDown = args.action == AMOTION_EVENT_ACTION_DOWN; |
| 4264 | mLatencyTracker.trackListener(args.id, isDown, args.eventTime, args.readTime); |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 4265 | } |
| 4266 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4267 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4268 | mLock.unlock(); |
| 4269 | } // release lock |
| 4270 | |
| 4271 | if (needWake) { |
| 4272 | mLooper->wake(); |
| 4273 | } |
| 4274 | } |
| 4275 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4276 | void InputDispatcher::notifySensor(const NotifySensorArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4277 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4278 | ALOGD("notifySensor - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 4279 | " sensorType=%s", |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4280 | args.id, args.eventTime, args.deviceId, args.source, |
| 4281 | ftl::enum_string(args.sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4282 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4283 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4284 | bool needWake = false; |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4285 | { // acquire lock |
| 4286 | mLock.lock(); |
| 4287 | |
| 4288 | // Just enqueue a new sensor event. |
| 4289 | std::unique_ptr<SensorEntry> newEntry = |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4290 | std::make_unique<SensorEntry>(args.id, args.eventTime, args.deviceId, args.source, |
| 4291 | /* policyFlags=*/0, args.hwTimestamp, args.sensorType, |
| 4292 | args.accuracy, args.accuracyChanged, args.values); |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4293 | |
| 4294 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
| 4295 | mLock.unlock(); |
| 4296 | } // release lock |
| 4297 | |
| 4298 | if (needWake) { |
| 4299 | mLooper->wake(); |
| 4300 | } |
| 4301 | } |
| 4302 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4303 | void InputDispatcher::notifyVibratorState(const NotifyVibratorStateArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4304 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4305 | ALOGD("notifyVibratorState - eventTime=%" PRId64 ", device=%d, isOn=%d", args.eventTime, |
| 4306 | args.deviceId, args.isOn); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4307 | } |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame^] | 4308 | mPolicy.notifyVibratorState(args.deviceId, args.isOn); |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 4309 | } |
| 4310 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4311 | bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs& args) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4312 | return mInputFilterEnabled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4313 | } |
| 4314 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4315 | void InputDispatcher::notifySwitch(const NotifySwitchArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4316 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4317 | ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, " |
| 4318 | "switchMask=0x%08x", |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4319 | args.eventTime, args.policyFlags, args.switchValues, args.switchMask); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4320 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4321 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4322 | uint32_t policyFlags = args.policyFlags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4323 | policyFlags |= POLICY_FLAG_TRUSTED; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame^] | 4324 | mPolicy.notifySwitch(args.eventTime, args.switchValues, args.switchMask, policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4325 | } |
| 4326 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4327 | void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4328 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4329 | ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args.eventTime, |
| 4330 | args.deviceId); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4331 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4332 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4333 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4334 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4335 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4336 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4337 | std::unique_ptr<DeviceResetEntry> newEntry = |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4338 | std::make_unique<DeviceResetEntry>(args.id, args.eventTime, args.deviceId); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4339 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4340 | } // release lock |
| 4341 | |
| 4342 | if (needWake) { |
| 4343 | mLooper->wake(); |
| 4344 | } |
| 4345 | } |
| 4346 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4347 | void InputDispatcher::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs& args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4348 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4349 | ALOGD("notifyPointerCaptureChanged - eventTime=%" PRId64 ", enabled=%s", args.eventTime, |
| 4350 | args.request.enable ? "true" : "false"); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4351 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4352 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4353 | bool needWake = false; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4354 | { // acquire lock |
| 4355 | std::scoped_lock _l(mLock); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4356 | auto entry = |
| 4357 | std::make_unique<PointerCaptureChangedEntry>(args.id, args.eventTime, args.request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4358 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 4359 | } // release lock |
| 4360 | |
| 4361 | if (needWake) { |
| 4362 | mLooper->wake(); |
| 4363 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4364 | } |
| 4365 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4366 | InputEventInjectionResult InputDispatcher::injectInputEvent(const InputEvent* event, |
| 4367 | std::optional<int32_t> targetUid, |
| 4368 | InputEventInjectionSync syncMode, |
| 4369 | std::chrono::milliseconds timeout, |
| 4370 | uint32_t policyFlags) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame] | 4371 | if (debugInboundEventDetails()) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4372 | LOG(DEBUG) << __func__ << ": targetUid=" << toString(targetUid) |
| 4373 | << ", syncMode=" << ftl::enum_string(syncMode) << ", timeout=" << timeout.count() |
| 4374 | << "ms, policyFlags=0x" << std::hex << policyFlags << std::dec |
| 4375 | << ", event=" << *event; |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4376 | } |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 4377 | 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] | 4378 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4379 | policyFlags |= POLICY_FLAG_INJECTED | POLICY_FLAG_TRUSTED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4380 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4381 | // 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] | 4382 | // that have gone through the InputFilter. If the event passed through the InputFilter, assign |
| 4383 | // the provided device id. If the InputFilter is accessibility, and it modifies or synthesizes |
| 4384 | // the injected event, it is responsible for setting POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY. |
| 4385 | // For those events, we will set FLAG_IS_ACCESSIBILITY_EVENT to allow apps to distinguish them |
| 4386 | // from events that originate from actual hardware. |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4387 | int32_t resolvedDeviceId = VIRTUAL_KEYBOARD_ID; |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4388 | if (policyFlags & POLICY_FLAG_FILTERED) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4389 | resolvedDeviceId = event->getDeviceId(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4390 | } |
| 4391 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4392 | std::queue<std::unique_ptr<EventEntry>> injectedEntries; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4393 | switch (event->getType()) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4394 | case InputEventType::KEY: { |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4395 | const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event); |
| 4396 | int32_t action = incomingKey.getAction(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4397 | if (!validateKeyEvent(action)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4398 | return InputEventInjectionResult::FAILED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4399 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4400 | |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4401 | int32_t flags = incomingKey.getFlags(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4402 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4403 | flags |= AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4404 | } |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4405 | int32_t keyCode = incomingKey.getKeyCode(); |
| 4406 | int32_t metaState = incomingKey.getMetaState(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4407 | accelerateMetaShortcuts(resolvedDeviceId, action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4408 | /*byref*/ keyCode, /*byref*/ metaState); |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4409 | KeyEvent keyEvent; |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4410 | keyEvent.initialize(incomingKey.getId(), resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4411 | incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode, |
| 4412 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), |
| 4413 | incomingKey.getDownTime(), incomingKey.getEventTime()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4414 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4415 | if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) { |
| 4416 | policyFlags |= POLICY_FLAG_VIRTUAL; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4417 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4418 | |
| 4419 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 4420 | android::base::Timer t; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame^] | 4421 | mPolicy.interceptKeyBeforeQueueing(keyEvent, /*byref*/ policyFlags); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4422 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4423 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
| 4424 | std::to_string(t.duration().count()).c_str()); |
| 4425 | } |
| 4426 | } |
| 4427 | |
| 4428 | mLock.lock(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4429 | std::unique_ptr<KeyEntry> injectedEntry = |
| 4430 | std::make_unique<KeyEntry>(incomingKey.getId(), incomingKey.getEventTime(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4431 | resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4432 | incomingKey.getDisplayId(), policyFlags, action, |
| 4433 | flags, keyCode, incomingKey.getScanCode(), metaState, |
| 4434 | incomingKey.getRepeatCount(), |
| 4435 | incomingKey.getDownTime()); |
| 4436 | injectedEntries.push(std::move(injectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4437 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4438 | } |
| 4439 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4440 | case InputEventType::MOTION: { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4441 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4442 | const int32_t action = motionEvent.getAction(); |
| 4443 | const bool isPointerEvent = |
| 4444 | isFromSource(event->getSource(), AINPUT_SOURCE_CLASS_POINTER); |
| 4445 | // If a pointer event has no displayId specified, inject it to the default display. |
| 4446 | const uint32_t displayId = isPointerEvent && (event->getDisplayId() == ADISPLAY_ID_NONE) |
| 4447 | ? ADISPLAY_ID_DEFAULT |
| 4448 | : event->getDisplayId(); |
| 4449 | const size_t pointerCount = motionEvent.getPointerCount(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4450 | const PointerProperties* pointerProperties = motionEvent.getPointerProperties(); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4451 | const int32_t actionButton = motionEvent.getActionButton(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4452 | int32_t flags = motionEvent.getFlags(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4453 | if (!validateMotionEvent(action, actionButton, pointerCount, pointerProperties)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4454 | return InputEventInjectionResult::FAILED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4455 | } |
| 4456 | |
| 4457 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4458 | nsecs_t eventTime = motionEvent.getEventTime(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4459 | android::base::Timer t; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame^] | 4460 | mPolicy.interceptMotionBeforeQueueing(displayId, eventTime, /*byref*/ policyFlags); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4461 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4462 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
| 4463 | std::to_string(t.duration().count()).c_str()); |
| 4464 | } |
| 4465 | } |
| 4466 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4467 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4468 | flags |= AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4469 | } |
| 4470 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4471 | mLock.lock(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4472 | const nsecs_t* sampleEventTimes = motionEvent.getSampleEventTimes(); |
| 4473 | const PointerCoords* samplePointerCoords = motionEvent.getSamplePointerCoords(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4474 | std::unique_ptr<MotionEntry> injectedEntry = |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4475 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, |
| 4476 | resolvedDeviceId, motionEvent.getSource(), |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4477 | displayId, policyFlags, action, actionButton, |
| 4478 | flags, motionEvent.getMetaState(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4479 | motionEvent.getButtonState(), |
| 4480 | motionEvent.getClassification(), |
| 4481 | motionEvent.getEdgeFlags(), |
| 4482 | motionEvent.getXPrecision(), |
| 4483 | motionEvent.getYPrecision(), |
| 4484 | motionEvent.getRawXCursorPosition(), |
| 4485 | motionEvent.getRawYCursorPosition(), |
| 4486 | motionEvent.getDownTime(), uint32_t(pointerCount), |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4487 | pointerProperties, samplePointerCoords); |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4488 | transformMotionEntryForInjectionLocked(*injectedEntry, motionEvent.getTransform()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4489 | injectedEntries.push(std::move(injectedEntry)); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4490 | for (size_t i = motionEvent.getHistorySize(); i > 0; i--) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4491 | sampleEventTimes += 1; |
| 4492 | samplePointerCoords += pointerCount; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4493 | std::unique_ptr<MotionEntry> nextInjectedEntry = |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4494 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, |
| 4495 | resolvedDeviceId, motionEvent.getSource(), |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4496 | displayId, policyFlags, action, actionButton, |
| 4497 | flags, motionEvent.getMetaState(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4498 | motionEvent.getButtonState(), |
| 4499 | motionEvent.getClassification(), |
| 4500 | motionEvent.getEdgeFlags(), |
| 4501 | motionEvent.getXPrecision(), |
| 4502 | motionEvent.getYPrecision(), |
| 4503 | motionEvent.getRawXCursorPosition(), |
| 4504 | motionEvent.getRawYCursorPosition(), |
| 4505 | motionEvent.getDownTime(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4506 | uint32_t(pointerCount), pointerProperties, |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4507 | samplePointerCoords); |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4508 | transformMotionEntryForInjectionLocked(*nextInjectedEntry, |
| 4509 | motionEvent.getTransform()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4510 | injectedEntries.push(std::move(nextInjectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4511 | } |
| 4512 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4513 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4514 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4515 | default: |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4516 | LOG(WARNING) << "Cannot inject " << ftl::enum_string(event->getType()) << " events"; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4517 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4518 | } |
| 4519 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4520 | InjectionState* injectionState = new InjectionState(targetUid); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4521 | if (syncMode == InputEventInjectionSync::NONE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4522 | injectionState->injectionIsAsync = true; |
| 4523 | } |
| 4524 | |
| 4525 | injectionState->refCount += 1; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4526 | injectedEntries.back()->injectionState = injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4527 | |
| 4528 | bool needWake = false; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4529 | while (!injectedEntries.empty()) { |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 4530 | if (DEBUG_INJECTION) { |
| 4531 | LOG(DEBUG) << "Injecting " << injectedEntries.front()->getDescription(); |
| 4532 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4533 | needWake |= enqueueInboundEventLocked(std::move(injectedEntries.front())); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4534 | injectedEntries.pop(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4535 | } |
| 4536 | |
| 4537 | mLock.unlock(); |
| 4538 | |
| 4539 | if (needWake) { |
| 4540 | mLooper->wake(); |
| 4541 | } |
| 4542 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4543 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4544 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4545 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4546 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4547 | if (syncMode == InputEventInjectionSync::NONE) { |
| 4548 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4549 | } else { |
| 4550 | for (;;) { |
| 4551 | injectionResult = injectionState->injectionResult; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4552 | if (injectionResult != InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4553 | break; |
| 4554 | } |
| 4555 | |
| 4556 | nsecs_t remainingTimeout = endTime - now(); |
| 4557 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4558 | if (DEBUG_INJECTION) { |
| 4559 | ALOGD("injectInputEvent - Timed out waiting for injection result " |
| 4560 | "to become available."); |
| 4561 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4562 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4563 | break; |
| 4564 | } |
| 4565 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4566 | mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4567 | } |
| 4568 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4569 | if (injectionResult == InputEventInjectionResult::SUCCEEDED && |
| 4570 | syncMode == InputEventInjectionSync::WAIT_FOR_FINISHED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4571 | while (injectionState->pendingForegroundDispatches != 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4572 | if (DEBUG_INJECTION) { |
| 4573 | ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", |
| 4574 | injectionState->pendingForegroundDispatches); |
| 4575 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4576 | nsecs_t remainingTimeout = endTime - now(); |
| 4577 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4578 | if (DEBUG_INJECTION) { |
| 4579 | ALOGD("injectInputEvent - Timed out waiting for pending foreground " |
| 4580 | "dispatches to finish."); |
| 4581 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4582 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4583 | break; |
| 4584 | } |
| 4585 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4586 | mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4587 | } |
| 4588 | } |
| 4589 | } |
| 4590 | |
| 4591 | injectionState->release(); |
| 4592 | } // release lock |
| 4593 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4594 | if (DEBUG_INJECTION) { |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 4595 | LOG(DEBUG) << "injectInputEvent - Finished with result " |
| 4596 | << ftl::enum_string(injectionResult); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4597 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4598 | |
| 4599 | return injectionResult; |
| 4600 | } |
| 4601 | |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4602 | std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4603 | std::array<uint8_t, 32> calculatedHmac; |
| 4604 | std::unique_ptr<VerifiedInputEvent> result; |
| 4605 | switch (event.getType()) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4606 | case InputEventType::KEY: { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4607 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event); |
| 4608 | VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent); |
| 4609 | result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4610 | calculatedHmac = sign(verifiedKeyEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4611 | break; |
| 4612 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4613 | case InputEventType::MOTION: { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4614 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event); |
| 4615 | VerifiedMotionEvent verifiedMotionEvent = |
| 4616 | verifiedMotionEventFromMotionEvent(motionEvent); |
| 4617 | result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4618 | calculatedHmac = sign(verifiedMotionEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4619 | break; |
| 4620 | } |
| 4621 | default: { |
| 4622 | ALOGE("Cannot verify events of type %" PRId32, event.getType()); |
| 4623 | return nullptr; |
| 4624 | } |
| 4625 | } |
| 4626 | if (calculatedHmac == INVALID_HMAC) { |
| 4627 | return nullptr; |
| 4628 | } |
tyiu | 1573a67 | 2023-02-21 22:38:32 +0000 | [diff] [blame] | 4629 | if (0 != CRYPTO_memcmp(calculatedHmac.data(), event.getHmac().data(), calculatedHmac.size())) { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4630 | return nullptr; |
| 4631 | } |
| 4632 | return result; |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4633 | } |
| 4634 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4635 | void InputDispatcher::setInjectionResult(EventEntry& entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4636 | InputEventInjectionResult injectionResult) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4637 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4638 | if (injectionState) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4639 | if (DEBUG_INJECTION) { |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 4640 | LOG(DEBUG) << "Setting input event injection result to " |
| 4641 | << ftl::enum_string(injectionResult); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4642 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4643 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4644 | if (injectionState->injectionIsAsync && !(entry.policyFlags & POLICY_FLAG_FILTERED)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4645 | // Log the outcome since the injector did not wait for the injection result. |
| 4646 | switch (injectionResult) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4647 | case InputEventInjectionResult::SUCCEEDED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4648 | ALOGV("Asynchronous input event injection succeeded."); |
| 4649 | break; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4650 | case InputEventInjectionResult::TARGET_MISMATCH: |
| 4651 | ALOGV("Asynchronous input event injection target mismatch."); |
| 4652 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4653 | case InputEventInjectionResult::FAILED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4654 | ALOGW("Asynchronous input event injection failed."); |
| 4655 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4656 | case InputEventInjectionResult::TIMED_OUT: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4657 | ALOGW("Asynchronous input event injection timed out."); |
| 4658 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4659 | case InputEventInjectionResult::PENDING: |
| 4660 | ALOGE("Setting result to 'PENDING' for asynchronous injection"); |
| 4661 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4662 | } |
| 4663 | } |
| 4664 | |
| 4665 | injectionState->injectionResult = injectionResult; |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4666 | mInjectionResultAvailable.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4667 | } |
| 4668 | } |
| 4669 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4670 | void InputDispatcher::transformMotionEntryForInjectionLocked( |
| 4671 | MotionEntry& entry, const ui::Transform& injectedTransform) const { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4672 | // Input injection works in the logical display coordinate space, but the input pipeline works |
| 4673 | // display space, so we need to transform the injected events accordingly. |
| 4674 | const auto it = mDisplayInfos.find(entry.displayId); |
| 4675 | if (it == mDisplayInfos.end()) return; |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4676 | const auto& transformToDisplay = it->second.transform.inverse() * injectedTransform; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4677 | |
Prabir Pradhan | d9a2ebe | 2022-07-20 19:25:13 +0000 | [diff] [blame] | 4678 | if (entry.xCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION && |
| 4679 | entry.yCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) { |
| 4680 | const vec2 cursor = |
| 4681 | MotionEvent::calculateTransformedXY(entry.source, transformToDisplay, |
| 4682 | {entry.xCursorPosition, entry.yCursorPosition}); |
| 4683 | entry.xCursorPosition = cursor.x; |
| 4684 | entry.yCursorPosition = cursor.y; |
| 4685 | } |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4686 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
Prabir Pradhan | 8e6ce22 | 2022-02-24 09:08:54 -0800 | [diff] [blame] | 4687 | entry.pointerCoords[i] = |
| 4688 | MotionEvent::calculateTransformedCoords(entry.source, transformToDisplay, |
| 4689 | entry.pointerCoords[i]); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4690 | } |
| 4691 | } |
| 4692 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4693 | void InputDispatcher::incrementPendingForegroundDispatches(EventEntry& entry) { |
| 4694 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4695 | if (injectionState) { |
| 4696 | injectionState->pendingForegroundDispatches += 1; |
| 4697 | } |
| 4698 | } |
| 4699 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4700 | void InputDispatcher::decrementPendingForegroundDispatches(EventEntry& entry) { |
| 4701 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4702 | if (injectionState) { |
| 4703 | injectionState->pendingForegroundDispatches -= 1; |
| 4704 | |
| 4705 | if (injectionState->pendingForegroundDispatches == 0) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4706 | mInjectionSyncFinished.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4707 | } |
| 4708 | } |
| 4709 | } |
| 4710 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4711 | const std::vector<sp<WindowInfoHandle>>& InputDispatcher::getWindowHandlesLocked( |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4712 | int32_t displayId) const { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4713 | static const std::vector<sp<WindowInfoHandle>> EMPTY_WINDOW_HANDLES; |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4714 | auto it = mWindowHandlesByDisplay.find(displayId); |
| 4715 | return it != mWindowHandlesByDisplay.end() ? it->second : EMPTY_WINDOW_HANDLES; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4716 | } |
| 4717 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4718 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4719 | const sp<IBinder>& windowHandleToken) const { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4720 | if (windowHandleToken == nullptr) { |
| 4721 | return nullptr; |
| 4722 | } |
| 4723 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4724 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4725 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 4726 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4727 | if (windowHandle->getToken() == windowHandleToken) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4728 | return windowHandle; |
| 4729 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4730 | } |
| 4731 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4732 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4733 | } |
| 4734 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4735 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked(const sp<IBinder>& windowHandleToken, |
| 4736 | int displayId) const { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4737 | if (windowHandleToken == nullptr) { |
| 4738 | return nullptr; |
| 4739 | } |
| 4740 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4741 | for (const sp<WindowInfoHandle>& windowHandle : getWindowHandlesLocked(displayId)) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4742 | if (windowHandle->getToken() == windowHandleToken) { |
| 4743 | return windowHandle; |
| 4744 | } |
| 4745 | } |
| 4746 | return nullptr; |
| 4747 | } |
| 4748 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4749 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
| 4750 | const sp<WindowInfoHandle>& windowHandle) const { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4751 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4752 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 4753 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4754 | if (handle->getId() == windowHandle->getId() && |
| 4755 | handle->getToken() == windowHandle->getToken()) { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4756 | if (windowHandle->getInfo()->displayId != it.first) { |
| 4757 | ALOGE("Found window %s in display %" PRId32 |
| 4758 | ", but it should belong to display %" PRId32, |
| 4759 | windowHandle->getName().c_str(), it.first, |
| 4760 | windowHandle->getInfo()->displayId); |
| 4761 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4762 | return handle; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4763 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4764 | } |
| 4765 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4766 | return nullptr; |
| 4767 | } |
| 4768 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4769 | sp<WindowInfoHandle> InputDispatcher::getFocusedWindowHandleLocked(int displayId) const { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4770 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(displayId); |
| 4771 | return getWindowHandleLocked(focusedToken, displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4772 | } |
| 4773 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4774 | ui::Transform InputDispatcher::getTransformLocked(int32_t displayId) const { |
| 4775 | auto displayInfoIt = mDisplayInfos.find(displayId); |
| 4776 | return displayInfoIt != mDisplayInfos.end() ? displayInfoIt->second.transform |
| 4777 | : kIdentityTransform; |
| 4778 | } |
| 4779 | |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 4780 | bool InputDispatcher::canWindowReceiveMotionLocked(const sp<WindowInfoHandle>& window, |
| 4781 | const MotionEntry& motionEntry) const { |
| 4782 | const WindowInfo& info = *window->getInfo(); |
| 4783 | |
| 4784 | // Skip spy window targets that are not valid for targeted injection. |
| 4785 | if (const auto err = verifyTargetedInjection(window, motionEntry); err) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4786 | return false; |
| 4787 | } |
| 4788 | |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 4789 | if (info.inputConfig.test(WindowInfo::InputConfig::PAUSE_DISPATCHING)) { |
| 4790 | ALOGI("Not sending touch event to %s because it is paused", window->getName().c_str()); |
| 4791 | return false; |
| 4792 | } |
| 4793 | |
| 4794 | if (info.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) { |
| 4795 | ALOGW("Not sending touch gesture to %s because it has config NO_INPUT_CHANNEL", |
| 4796 | window->getName().c_str()); |
| 4797 | return false; |
| 4798 | } |
| 4799 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 4800 | std::shared_ptr<Connection> connection = getConnectionLocked(window->getToken()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4801 | if (connection == nullptr) { |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 4802 | ALOGW("Not sending touch to %s because there's no corresponding connection", |
| 4803 | window->getName().c_str()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4804 | return false; |
| 4805 | } |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 4806 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4807 | if (!connection->responsive) { |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 4808 | 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] | 4809 | return false; |
| 4810 | } |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 4811 | |
| 4812 | // Drop events that can't be trusted due to occlusion |
| 4813 | const auto [x, y] = resolveTouchedPosition(motionEntry); |
| 4814 | TouchOcclusionInfo occlusionInfo = computeTouchOcclusionInfoLocked(window, x, y); |
| 4815 | if (!isTouchTrustedLocked(occlusionInfo)) { |
| 4816 | if (DEBUG_TOUCH_OCCLUSION) { |
Prabir Pradhan | 82e081e | 2022-12-06 09:50:09 +0000 | [diff] [blame] | 4817 | ALOGD("Stack of obscuring windows during untrusted touch (%.1f, %.1f):", x, y); |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 4818 | for (const auto& log : occlusionInfo.debugInfo) { |
| 4819 | ALOGD("%s", log.c_str()); |
| 4820 | } |
| 4821 | } |
| 4822 | ALOGW("Dropping untrusted touch event due to %s/%d", occlusionInfo.obscuringPackage.c_str(), |
| 4823 | occlusionInfo.obscuringUid); |
| 4824 | return false; |
| 4825 | } |
| 4826 | |
| 4827 | // Drop touch events if requested by input feature |
| 4828 | if (shouldDropInput(motionEntry, window)) { |
| 4829 | return false; |
| 4830 | } |
| 4831 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4832 | return true; |
| 4833 | } |
| 4834 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4835 | std::shared_ptr<InputChannel> InputDispatcher::getInputChannelLocked( |
| 4836 | const sp<IBinder>& token) const { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4837 | auto connectionIt = mConnectionsByToken.find(token); |
| 4838 | if (connectionIt == mConnectionsByToken.end()) { |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4839 | return nullptr; |
| 4840 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4841 | return connectionIt->second->inputChannel; |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4842 | } |
| 4843 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4844 | void InputDispatcher::updateWindowHandlesForDisplayLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4845 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
| 4846 | if (windowInfoHandles.empty()) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4847 | // Remove all handles on a display if there are no windows left. |
| 4848 | mWindowHandlesByDisplay.erase(displayId); |
| 4849 | return; |
| 4850 | } |
| 4851 | |
| 4852 | // Since we compare the pointer of input window handles across window updates, we need |
| 4853 | // 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] | 4854 | const std::vector<sp<WindowInfoHandle>>& oldHandles = getWindowHandlesLocked(displayId); |
| 4855 | std::unordered_map<int32_t /*id*/, sp<WindowInfoHandle>> oldHandlesById; |
| 4856 | for (const sp<WindowInfoHandle>& handle : oldHandles) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4857 | oldHandlesById[handle->getId()] = handle; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4858 | } |
| 4859 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4860 | std::vector<sp<WindowInfoHandle>> newHandles; |
| 4861 | for (const sp<WindowInfoHandle>& handle : windowInfoHandles) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4862 | const WindowInfo* info = handle->getInfo(); |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 4863 | if (getInputChannelLocked(handle->getToken()) == nullptr) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4864 | const bool noInputChannel = |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 4865 | info->inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4866 | const bool canReceiveInput = |
| 4867 | !info->inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE) || |
| 4868 | !info->inputConfig.test(WindowInfo::InputConfig::NOT_FOCUSABLE); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4869 | if (canReceiveInput && !noInputChannel) { |
John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 4870 | ALOGV("Window handle %s has no registered input channel", |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4871 | handle->getName().c_str()); |
Robert Carr | 2984b7a | 2020-04-13 17:06:45 -0700 | [diff] [blame] | 4872 | continue; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4873 | } |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4874 | } |
| 4875 | |
| 4876 | if (info->displayId != displayId) { |
| 4877 | ALOGE("Window %s updated by wrong display %d, should belong to display %d", |
| 4878 | handle->getName().c_str(), displayId, info->displayId); |
| 4879 | continue; |
| 4880 | } |
| 4881 | |
Robert Carr | edd1360 | 2020-04-13 17:24:34 -0700 | [diff] [blame] | 4882 | if ((oldHandlesById.find(handle->getId()) != oldHandlesById.end()) && |
| 4883 | (oldHandlesById.at(handle->getId())->getToken() == handle->getToken())) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4884 | const sp<WindowInfoHandle>& oldHandle = oldHandlesById.at(handle->getId()); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4885 | oldHandle->updateFrom(handle); |
| 4886 | newHandles.push_back(oldHandle); |
| 4887 | } else { |
| 4888 | newHandles.push_back(handle); |
| 4889 | } |
| 4890 | } |
| 4891 | |
| 4892 | // Insert or replace |
| 4893 | mWindowHandlesByDisplay[displayId] = newHandles; |
| 4894 | } |
| 4895 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4896 | void InputDispatcher::setInputWindows( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4897 | const std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>>& handlesPerDisplay) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 4898 | // TODO(b/198444055): Remove setInputWindows from InputDispatcher. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4899 | { // acquire lock |
| 4900 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 4901 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 4902 | setInputWindowsLocked(handles, displayId); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4903 | } |
| 4904 | } |
| 4905 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4906 | mLooper->wake(); |
| 4907 | } |
| 4908 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4909 | /** |
| 4910 | * Called from InputManagerService, update window handle list by displayId that can receive input. |
| 4911 | * A window handle contains information about InputChannel, Touch Region, Types, Focused,... |
| 4912 | * If set an empty list, remove all handles from the specific display. |
| 4913 | * For focused handle, check if need to change and send a cancel event to previous one. |
| 4914 | * For removed handle, check if need to send a cancel event if already in touch. |
| 4915 | */ |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4916 | void InputDispatcher::setInputWindowsLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4917 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4918 | if (DEBUG_FOCUS) { |
| 4919 | std::string windowList; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4920 | for (const sp<WindowInfoHandle>& iwh : windowInfoHandles) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4921 | windowList += iwh->getName() + " "; |
| 4922 | } |
| 4923 | ALOGD("setInputWindows displayId=%" PRId32 " %s", displayId, windowList.c_str()); |
| 4924 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4925 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4926 | // Check preconditions for new input windows |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4927 | for (const sp<WindowInfoHandle>& window : windowInfoHandles) { |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4928 | const WindowInfo& info = *window->getInfo(); |
| 4929 | |
| 4930 | // 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] | 4931 | const bool noInputWindow = info.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4932 | if (noInputWindow && window->getToken() != nullptr) { |
| 4933 | ALOGE("%s has feature NO_INPUT_WINDOW, but a non-null token. Clearing", |
| 4934 | window->getName().c_str()); |
| 4935 | window->releaseChannel(); |
| 4936 | } |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4937 | |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 4938 | // Ensure all spy windows are trusted overlays |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4939 | LOG_ALWAYS_FATAL_IF(info.isSpy() && |
| 4940 | !info.inputConfig.test( |
| 4941 | WindowInfo::InputConfig::TRUSTED_OVERLAY), |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 4942 | "%s has feature SPY, but is not a trusted overlay.", |
| 4943 | window->getName().c_str()); |
| 4944 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4945 | // Ensure all stylus interceptors are trusted overlays |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4946 | LOG_ALWAYS_FATAL_IF(info.interceptsStylus() && |
| 4947 | !info.inputConfig.test( |
| 4948 | WindowInfo::InputConfig::TRUSTED_OVERLAY), |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4949 | "%s has feature INTERCEPTS_STYLUS, but is not a trusted overlay.", |
| 4950 | window->getName().c_str()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4951 | } |
| 4952 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4953 | // Copy old handles for release if they are no longer present. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4954 | const std::vector<sp<WindowInfoHandle>> oldWindowHandles = getWindowHandlesLocked(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4955 | |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4956 | // Save the old windows' orientation by ID before it gets updated. |
| 4957 | std::unordered_map<int32_t, uint32_t> oldWindowOrientations; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4958 | for (const sp<WindowInfoHandle>& handle : oldWindowHandles) { |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4959 | oldWindowOrientations.emplace(handle->getId(), |
| 4960 | handle->getInfo()->transform.getOrientation()); |
| 4961 | } |
| 4962 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4963 | updateWindowHandlesForDisplayLocked(windowInfoHandles, displayId); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4964 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4965 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4966 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4967 | std::optional<FocusResolver::FocusChanges> changes = |
| 4968 | mFocusResolver.setInputWindows(displayId, windowHandles); |
| 4969 | if (changes) { |
| 4970 | onFocusChangedLocked(*changes); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4971 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4972 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4973 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 4974 | mTouchStatesByDisplay.find(displayId); |
| 4975 | if (stateIt != mTouchStatesByDisplay.end()) { |
| 4976 | TouchState& state = stateIt->second; |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4977 | for (size_t i = 0; i < state.windows.size();) { |
| 4978 | TouchedWindow& touchedWindow = state.windows[i]; |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4979 | if (getWindowHandleLocked(touchedWindow.windowHandle) == nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4980 | if (DEBUG_FOCUS) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4981 | ALOGD("Touched window was removed: %s in display %" PRId32, |
| 4982 | touchedWindow.windowHandle->getName().c_str(), displayId); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4983 | } |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4984 | std::shared_ptr<InputChannel> touchedInputChannel = |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4985 | getInputChannelLocked(touchedWindow.windowHandle->getToken()); |
| 4986 | if (touchedInputChannel != nullptr) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 4987 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4988 | "touched window was removed"); |
| 4989 | synthesizeCancelationEventsForInputChannelLocked(touchedInputChannel, options); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4990 | // Since we are about to drop the touch, cancel the events for the wallpaper as |
| 4991 | // well. |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 4992 | if (touchedWindow.targetFlags.test(InputTarget::Flags::FOREGROUND) && |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4993 | touchedWindow.windowHandle->getInfo()->inputConfig.test( |
| 4994 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER)) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4995 | sp<WindowInfoHandle> wallpaper = state.getWallpaperWindow(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4996 | synthesizeCancelationEventsForWindowLocked(wallpaper, options); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4997 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4998 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4999 | state.windows.erase(state.windows.begin() + i); |
| 5000 | } else { |
| 5001 | ++i; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5002 | } |
| 5003 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5004 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5005 | // 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] | 5006 | // could just clear the state here. |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 5007 | if (mDragState && mDragState->dragWindow->getInfo()->displayId == displayId && |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5008 | std::find(windowHandles.begin(), windowHandles.end(), mDragState->dragWindow) == |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5009 | windowHandles.end()) { |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 5010 | ALOGI("Drag window went away: %s", mDragState->dragWindow->getName().c_str()); |
| 5011 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5012 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5013 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5014 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 5015 | |
Prabir Pradhan | 8b89c2f | 2021-07-29 16:30:14 +0000 | [diff] [blame] | 5016 | // Determine if the orientation of any of the input windows have changed, and cancel all |
| 5017 | // pointer events if necessary. |
| 5018 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
| 5019 | const sp<WindowInfoHandle> newWindowHandle = getWindowHandleLocked(oldWindowHandle); |
| 5020 | if (newWindowHandle != nullptr && |
| 5021 | newWindowHandle->getInfo()->transform.getOrientation() != |
| 5022 | oldWindowOrientations[oldWindowHandle->getId()]) { |
| 5023 | std::shared_ptr<InputChannel> inputChannel = |
| 5024 | getInputChannelLocked(newWindowHandle->getToken()); |
| 5025 | if (inputChannel != nullptr) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 5026 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
Prabir Pradhan | 8b89c2f | 2021-07-29 16:30:14 +0000 | [diff] [blame] | 5027 | "touched window's orientation changed"); |
| 5028 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 5029 | } |
| 5030 | } |
| 5031 | } |
| 5032 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5033 | // Release information for windows that are no longer present. |
| 5034 | // This ensures that unused input channels are released promptly. |
| 5035 | // Otherwise, they might stick around until the window handle is destroyed |
| 5036 | // which might not happen until the next GC. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5037 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 5038 | if (getWindowHandleLocked(oldWindowHandle) == nullptr) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5039 | if (DEBUG_FOCUS) { |
| 5040 | ALOGD("Window went away: %s", oldWindowHandle->getName().c_str()); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 5041 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5042 | oldWindowHandle->releaseChannel(); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 5043 | } |
chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 5044 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5045 | } |
| 5046 | |
| 5047 | void InputDispatcher::setFocusedApplication( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5048 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5049 | if (DEBUG_FOCUS) { |
| 5050 | ALOGD("setFocusedApplication displayId=%" PRId32 " %s", displayId, |
| 5051 | inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>"); |
| 5052 | } |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 5053 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5054 | std::scoped_lock _l(mLock); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 5055 | setFocusedApplicationLocked(displayId, inputApplicationHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5056 | } // release lock |
| 5057 | |
| 5058 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5059 | mLooper->wake(); |
| 5060 | } |
| 5061 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 5062 | void InputDispatcher::setFocusedApplicationLocked( |
| 5063 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
| 5064 | std::shared_ptr<InputApplicationHandle> oldFocusedApplicationHandle = |
| 5065 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 5066 | |
| 5067 | if (sharedPointersEqual(oldFocusedApplicationHandle, inputApplicationHandle)) { |
| 5068 | return; // This application is already focused. No need to wake up or change anything. |
| 5069 | } |
| 5070 | |
| 5071 | // Set the new application handle. |
| 5072 | if (inputApplicationHandle != nullptr) { |
| 5073 | mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle; |
| 5074 | } else { |
| 5075 | mFocusedApplicationHandlesByDisplay.erase(displayId); |
| 5076 | } |
| 5077 | |
| 5078 | // No matter what the old focused application was, stop waiting on it because it is |
| 5079 | // no longer focused. |
| 5080 | resetNoFocusedWindowTimeoutLocked(); |
| 5081 | } |
| 5082 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5083 | /** |
| 5084 | * Sets the focused display, which is responsible for receiving focus-dispatched input events where |
| 5085 | * the display not specified. |
| 5086 | * |
| 5087 | * We track any unreleased events for each window. If a window loses the ability to receive the |
| 5088 | * released event, we will send a cancel event to it. So when the focused display is changed, we |
| 5089 | * cancel all the unreleased display-unspecified events for the focused window on the old focused |
| 5090 | * display. The display-specified events won't be affected. |
| 5091 | */ |
| 5092 | void InputDispatcher::setFocusedDisplay(int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5093 | if (DEBUG_FOCUS) { |
| 5094 | ALOGD("setFocusedDisplay displayId=%" PRId32, displayId); |
| 5095 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5096 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5097 | std::scoped_lock _l(mLock); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5098 | |
| 5099 | if (mFocusedDisplayId != displayId) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5100 | sp<IBinder> oldFocusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5101 | mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5102 | if (oldFocusedWindowToken != nullptr) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5103 | std::shared_ptr<InputChannel> inputChannel = |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5104 | getInputChannelLocked(oldFocusedWindowToken); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5105 | if (inputChannel != nullptr) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5106 | CancelationOptions |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 5107 | options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5108 | "The display which contains this window no longer has focus."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5109 | options.displayId = ADISPLAY_ID_NONE; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5110 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
| 5111 | } |
| 5112 | } |
| 5113 | mFocusedDisplayId = displayId; |
| 5114 | |
Chris Ye | 3c2d6f5 | 2020-08-09 10:39:48 -0700 | [diff] [blame] | 5115 | // Find new focused window and validate |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5116 | sp<IBinder> newFocusedWindowToken = mFocusResolver.getFocusedWindowToken(displayId); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5117 | sendFocusChangedCommandLocked(oldFocusedWindowToken, newFocusedWindowToken); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5118 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5119 | if (newFocusedWindowToken == nullptr) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5120 | ALOGW("Focused display #%" PRId32 " does not have a focused window.", displayId); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5121 | if (mFocusResolver.hasFocusedWindowTokens()) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5122 | ALOGE("But another display has a focused window\n%s", |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5123 | mFocusResolver.dumpFocusedWindows().c_str()); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5124 | } |
| 5125 | } |
| 5126 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5127 | } // release lock |
| 5128 | |
| 5129 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5130 | mLooper->wake(); |
| 5131 | } |
| 5132 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5133 | void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5134 | if (DEBUG_FOCUS) { |
| 5135 | ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); |
| 5136 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5137 | |
| 5138 | bool changed; |
| 5139 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5140 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5141 | |
| 5142 | if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { |
| 5143 | if (mDispatchFrozen && !frozen) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5144 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5145 | } |
| 5146 | |
| 5147 | if (mDispatchEnabled && !enabled) { |
| 5148 | resetAndDropEverythingLocked("dispatcher is being disabled"); |
| 5149 | } |
| 5150 | |
| 5151 | mDispatchEnabled = enabled; |
| 5152 | mDispatchFrozen = frozen; |
| 5153 | changed = true; |
| 5154 | } else { |
| 5155 | changed = false; |
| 5156 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5157 | } // release lock |
| 5158 | |
| 5159 | if (changed) { |
| 5160 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5161 | mLooper->wake(); |
| 5162 | } |
| 5163 | } |
| 5164 | |
| 5165 | void InputDispatcher::setInputFilterEnabled(bool enabled) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5166 | if (DEBUG_FOCUS) { |
| 5167 | ALOGD("setInputFilterEnabled: enabled=%d", enabled); |
| 5168 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5169 | |
| 5170 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5171 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5172 | |
| 5173 | if (mInputFilterEnabled == enabled) { |
| 5174 | return; |
| 5175 | } |
| 5176 | |
| 5177 | mInputFilterEnabled = enabled; |
| 5178 | resetAndDropEverythingLocked("input filter is being enabled or disabled"); |
| 5179 | } // release lock |
| 5180 | |
| 5181 | // Wake up poll loop since there might be work to do to drop everything. |
| 5182 | mLooper->wake(); |
| 5183 | } |
| 5184 | |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 5185 | bool InputDispatcher::setInTouchMode(bool inTouchMode, int32_t pid, int32_t uid, bool hasPermission, |
| 5186 | int32_t displayId) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5187 | bool needWake = false; |
| 5188 | { |
| 5189 | std::scoped_lock lock(mLock); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5190 | ALOGD_IF(DEBUG_TOUCH_MODE, |
| 5191 | "Request to change touch mode to %s (calling pid=%d, uid=%d, " |
| 5192 | "hasPermission=%s, target displayId=%d, mTouchModePerDisplay[displayId]=%s)", |
| 5193 | toString(inTouchMode), pid, uid, toString(hasPermission), displayId, |
| 5194 | mTouchModePerDisplay.count(displayId) == 0 |
| 5195 | ? "not set" |
| 5196 | : std::to_string(mTouchModePerDisplay[displayId]).c_str()); |
| 5197 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5198 | auto touchModeIt = mTouchModePerDisplay.find(displayId); |
| 5199 | if (touchModeIt != mTouchModePerDisplay.end() && touchModeIt->second == inTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5200 | return false; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5201 | } |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5202 | if (!hasPermission) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 5203 | if (!focusedWindowIsOwnedByLocked(pid, uid) && |
| 5204 | !recentWindowsAreOwnedByLocked(pid, uid)) { |
| 5205 | ALOGD("Touch mode switch rejected, caller (pid=%d, uid=%d) doesn't own the focused " |
| 5206 | "window nor none of the previously interacted window", |
| 5207 | pid, uid); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5208 | return false; |
| 5209 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5210 | } |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5211 | mTouchModePerDisplay[displayId] = inTouchMode; |
| 5212 | auto entry = std::make_unique<TouchModeEntry>(mIdGenerator.nextId(), now(), inTouchMode, |
| 5213 | displayId); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5214 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 5215 | } // release lock |
| 5216 | |
| 5217 | if (needWake) { |
| 5218 | mLooper->wake(); |
| 5219 | } |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5220 | return true; |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 5221 | } |
| 5222 | |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 5223 | bool InputDispatcher::focusedWindowIsOwnedByLocked(int32_t pid, int32_t uid) { |
| 5224 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
| 5225 | if (focusedToken == nullptr) { |
| 5226 | return false; |
| 5227 | } |
| 5228 | sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(focusedToken); |
| 5229 | return isWindowOwnedBy(windowHandle, pid, uid); |
| 5230 | } |
| 5231 | |
| 5232 | bool InputDispatcher::recentWindowsAreOwnedByLocked(int32_t pid, int32_t uid) { |
| 5233 | return std::find_if(mInteractionConnectionTokens.begin(), mInteractionConnectionTokens.end(), |
| 5234 | [&](const sp<IBinder>& connectionToken) REQUIRES(mLock) { |
| 5235 | const sp<WindowInfoHandle> windowHandle = |
| 5236 | getWindowHandleLocked(connectionToken); |
| 5237 | return isWindowOwnedBy(windowHandle, pid, uid); |
| 5238 | }) != mInteractionConnectionTokens.end(); |
| 5239 | } |
| 5240 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 5241 | void InputDispatcher::setMaximumObscuringOpacityForTouch(float opacity) { |
| 5242 | if (opacity < 0 || opacity > 1) { |
| 5243 | LOG_ALWAYS_FATAL("Maximum obscuring opacity for touch should be >= 0 and <= 1"); |
| 5244 | return; |
| 5245 | } |
| 5246 | |
| 5247 | std::scoped_lock lock(mLock); |
| 5248 | mMaximumObscuringOpacityForTouch = opacity; |
| 5249 | } |
| 5250 | |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5251 | std::tuple<TouchState*, TouchedWindow*, int32_t /*displayId*/> |
| 5252 | InputDispatcher::findTouchStateWindowAndDisplayLocked(const sp<IBinder>& token) { |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5253 | for (auto& [displayId, state] : mTouchStatesByDisplay) { |
| 5254 | for (TouchedWindow& w : state.windows) { |
| 5255 | if (w.windowHandle->getToken() == token) { |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5256 | return std::make_tuple(&state, &w, displayId); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5257 | } |
| 5258 | } |
| 5259 | } |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5260 | return std::make_tuple(nullptr, nullptr, ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5261 | } |
| 5262 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5263 | bool InputDispatcher::transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken, |
| 5264 | bool isDragDrop) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 5265 | if (fromToken == toToken) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5266 | if (DEBUG_FOCUS) { |
| 5267 | ALOGD("Trivial transfer to same window."); |
| 5268 | } |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 5269 | return true; |
| 5270 | } |
| 5271 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5272 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5273 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5274 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5275 | // Find the target touch state and touched window by fromToken. |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5276 | auto [state, touchedWindow, displayId] = findTouchStateWindowAndDisplayLocked(fromToken); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5277 | if (state == nullptr || touchedWindow == nullptr) { |
| 5278 | ALOGD("Focus transfer failed because from window is not being touched."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5279 | return false; |
| 5280 | } |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5281 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5282 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(toToken, displayId); |
| 5283 | if (toWindowHandle == nullptr) { |
| 5284 | ALOGW("Cannot transfer focus because to window not found."); |
| 5285 | return false; |
| 5286 | } |
| 5287 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5288 | if (DEBUG_FOCUS) { |
| 5289 | ALOGD("transferTouchFocus: fromWindowHandle=%s, toWindowHandle=%s", |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5290 | touchedWindow->windowHandle->getName().c_str(), |
| 5291 | toWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5292 | } |
| 5293 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5294 | // Erase old window. |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5295 | ftl::Flags<InputTarget::Flags> oldTargetFlags = touchedWindow->targetFlags; |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 5296 | std::bitset<MAX_POINTER_ID + 1> pointerIds = touchedWindow->pointerIds; |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5297 | sp<WindowInfoHandle> fromWindowHandle = touchedWindow->windowHandle; |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5298 | state->removeWindowByToken(fromToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5299 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5300 | // Add new window. |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 5301 | nsecs_t downTimeInTarget = now(); |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5302 | ftl::Flags<InputTarget::Flags> newTargetFlags = |
| 5303 | oldTargetFlags & (InputTarget::Flags::SPLIT | InputTarget::Flags::DISPATCH_AS_IS); |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 5304 | if (canReceiveForegroundTouches(*toWindowHandle->getInfo())) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5305 | newTargetFlags |= InputTarget::Flags::FOREGROUND; |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 5306 | } |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 5307 | state->addOrUpdateWindow(toWindowHandle, newTargetFlags, pointerIds, downTimeInTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5308 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5309 | // Store the dragging window. |
| 5310 | if (isDragDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 5311 | if (pointerIds.count() != 1) { |
| 5312 | ALOGW("The drag and drop cannot be started when there is no pointer or more than 1" |
| 5313 | " pointer on the window."); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 5314 | return false; |
| 5315 | } |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 5316 | // Track the pointer id for drag window and generate the drag state. |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 5317 | const size_t id = firstMarkedBit(pointerIds); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 5318 | mDragState = std::make_unique<DragState>(toWindowHandle, id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5319 | } |
| 5320 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5321 | // Synthesize cancel for old window and down for new window. |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 5322 | std::shared_ptr<Connection> fromConnection = getConnectionLocked(fromToken); |
| 5323 | std::shared_ptr<Connection> toConnection = getConnectionLocked(toToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5324 | if (fromConnection != nullptr && toConnection != nullptr) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5325 | fromConnection->inputState.mergePointerStateTo(toConnection->inputState); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5326 | CancelationOptions |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 5327 | options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5328 | "transferring touch focus from this window to another window"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5329 | synthesizeCancelationEventsForConnectionLocked(fromConnection, options); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5330 | synthesizePointerDownEventsForConnectionLocked(downTimeInTarget, toConnection, |
| 5331 | newTargetFlags); |
| 5332 | |
| 5333 | // Check if the wallpaper window should deliver the corresponding event. |
| 5334 | transferWallpaperTouch(oldTargetFlags, newTargetFlags, fromWindowHandle, toWindowHandle, |
| 5335 | *state, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5336 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5337 | } // release lock |
| 5338 | |
| 5339 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5340 | mLooper->wake(); |
| 5341 | return true; |
| 5342 | } |
| 5343 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5344 | /** |
| 5345 | * Get the touched foreground window on the given display. |
| 5346 | * Return null if there are no windows touched on that display, or if more than one foreground |
| 5347 | * window is being touched. |
| 5348 | */ |
| 5349 | sp<WindowInfoHandle> InputDispatcher::findTouchedForegroundWindowLocked(int32_t displayId) const { |
| 5350 | auto stateIt = mTouchStatesByDisplay.find(displayId); |
| 5351 | if (stateIt == mTouchStatesByDisplay.end()) { |
| 5352 | ALOGI("No touch state on display %" PRId32, displayId); |
| 5353 | return nullptr; |
| 5354 | } |
| 5355 | |
| 5356 | const TouchState& state = stateIt->second; |
| 5357 | sp<WindowInfoHandle> touchedForegroundWindow; |
| 5358 | // If multiple foreground windows are touched, return nullptr |
| 5359 | for (const TouchedWindow& window : state.windows) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5360 | if (window.targetFlags.test(InputTarget::Flags::FOREGROUND)) { |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5361 | if (touchedForegroundWindow != nullptr) { |
| 5362 | ALOGI("Two or more foreground windows: %s and %s", |
| 5363 | touchedForegroundWindow->getName().c_str(), |
| 5364 | window.windowHandle->getName().c_str()); |
| 5365 | return nullptr; |
| 5366 | } |
| 5367 | touchedForegroundWindow = window.windowHandle; |
| 5368 | } |
| 5369 | } |
| 5370 | return touchedForegroundWindow; |
| 5371 | } |
| 5372 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5373 | // Binder call |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5374 | bool InputDispatcher::transferTouch(const sp<IBinder>& destChannelToken, int32_t displayId) { |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5375 | sp<IBinder> fromToken; |
| 5376 | { // acquire lock |
| 5377 | std::scoped_lock _l(mLock); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5378 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(destChannelToken, displayId); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5379 | if (toWindowHandle == nullptr) { |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5380 | ALOGW("Could not find window associated with token=%p on display %" PRId32, |
| 5381 | destChannelToken.get(), displayId); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5382 | return false; |
| 5383 | } |
| 5384 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5385 | sp<WindowInfoHandle> from = findTouchedForegroundWindowLocked(displayId); |
| 5386 | if (from == nullptr) { |
| 5387 | ALOGE("Could not find a source window in %s for %p", __func__, destChannelToken.get()); |
| 5388 | return false; |
| 5389 | } |
| 5390 | |
| 5391 | fromToken = from->getToken(); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5392 | } // release lock |
| 5393 | |
| 5394 | return transferTouchFocus(fromToken, destChannelToken); |
| 5395 | } |
| 5396 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5397 | void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5398 | if (DEBUG_FOCUS) { |
| 5399 | ALOGD("Resetting and dropping all events (%s).", reason); |
| 5400 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5401 | |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 5402 | CancelationOptions options(CancelationOptions::Mode::CANCEL_ALL_EVENTS, reason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5403 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 5404 | |
| 5405 | resetKeyRepeatLocked(); |
| 5406 | releasePendingEventLocked(); |
| 5407 | drainInboundQueueLocked(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5408 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5409 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5410 | mAnrTracker.clear(); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5411 | mTouchStatesByDisplay.clear(); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5412 | mReplacedKeys.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5413 | } |
| 5414 | |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 5415 | void InputDispatcher::logDispatchStateLocked() const { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5416 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5417 | dumpDispatchStateLocked(dump); |
| 5418 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5419 | std::istringstream stream(dump); |
| 5420 | std::string line; |
| 5421 | |
| 5422 | while (std::getline(stream, line, '\n')) { |
| 5423 | ALOGD("%s", line.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5424 | } |
| 5425 | } |
| 5426 | |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 5427 | std::string InputDispatcher::dumpPointerCaptureStateLocked() const { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5428 | std::string dump; |
| 5429 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5430 | dump += StringPrintf(INDENT "Pointer Capture Requested: %s\n", |
| 5431 | toString(mCurrentPointerCaptureRequest.enable)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5432 | |
| 5433 | std::string windowName = "None"; |
| 5434 | if (mWindowTokenWithPointerCapture) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5435 | const sp<WindowInfoHandle> captureWindowHandle = |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5436 | getWindowHandleLocked(mWindowTokenWithPointerCapture); |
| 5437 | windowName = captureWindowHandle ? captureWindowHandle->getName().c_str() |
| 5438 | : "token has capture without window"; |
| 5439 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5440 | 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] | 5441 | |
| 5442 | return dump; |
| 5443 | } |
| 5444 | |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 5445 | void InputDispatcher::dumpDispatchStateLocked(std::string& dump) const { |
Siarhei Vishniakou | 043a3ec | 2019-05-01 11:30:46 -0700 | [diff] [blame] | 5446 | dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled)); |
| 5447 | dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen)); |
| 5448 | dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5449 | dump += StringPrintf(INDENT "FocusedDisplayId: %" PRId32 "\n", mFocusedDisplayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5450 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5451 | if (!mFocusedApplicationHandlesByDisplay.empty()) { |
| 5452 | dump += StringPrintf(INDENT "FocusedApplications:\n"); |
| 5453 | for (auto& it : mFocusedApplicationHandlesByDisplay) { |
| 5454 | const int32_t displayId = it.first; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5455 | const std::shared_ptr<InputApplicationHandle>& applicationHandle = it.second; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5456 | const std::chrono::duration timeout = |
| 5457 | applicationHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5458 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5459 | ", name='%s', dispatchingTimeout=%" PRId64 "ms\n", |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5460 | displayId, applicationHandle->getName().c_str(), millis(timeout)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5461 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5462 | } else { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5463 | dump += StringPrintf(INDENT "FocusedApplications: <none>\n"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5464 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5465 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5466 | dump += mFocusResolver.dump(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5467 | dump += dumpPointerCaptureStateLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5468 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5469 | if (!mTouchStatesByDisplay.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5470 | dump += StringPrintf(INDENT "TouchStatesByDisplay:\n"); |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5471 | for (const auto& [displayId, state] : mTouchStatesByDisplay) { |
Siarhei Vishniakou | 6e1e987 | 2022-11-08 17:51:35 -0800 | [diff] [blame] | 5472 | std::string touchStateDump = addLinePrefix(state.dump(), INDENT2); |
| 5473 | dump += INDENT2 + std::to_string(displayId) + " : " + touchStateDump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5474 | } |
| 5475 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5476 | dump += INDENT "TouchStates: <no displays touched>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5477 | } |
| 5478 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5479 | if (mDragState) { |
| 5480 | dump += StringPrintf(INDENT "DragState:\n"); |
| 5481 | mDragState->dump(dump, INDENT2); |
| 5482 | } |
| 5483 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5484 | if (!mWindowHandlesByDisplay.empty()) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5485 | for (const auto& [displayId, windowHandles] : mWindowHandlesByDisplay) { |
| 5486 | dump += StringPrintf(INDENT "Display: %" PRId32 "\n", displayId); |
| 5487 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
| 5488 | const auto& displayInfo = it->second; |
| 5489 | dump += StringPrintf(INDENT2 "logicalSize=%dx%d\n", displayInfo.logicalWidth, |
| 5490 | displayInfo.logicalHeight); |
| 5491 | displayInfo.transform.dump(dump, "transform", INDENT4); |
| 5492 | } else { |
| 5493 | dump += INDENT2 "No DisplayInfo found!\n"; |
| 5494 | } |
| 5495 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5496 | if (!windowHandles.empty()) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5497 | dump += INDENT2 "Windows:\n"; |
| 5498 | for (size_t i = 0; i < windowHandles.size(); i++) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5499 | const sp<WindowInfoHandle>& windowHandle = windowHandles[i]; |
| 5500 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5501 | |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5502 | dump += StringPrintf(INDENT3 "%zu: name='%s', id=%" PRId32 ", displayId=%d, " |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5503 | "inputConfig=%s, alpha=%.2f, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5504 | "frame=[%d,%d][%d,%d], globalScale=%f, " |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5505 | "applicationInfo.name=%s, " |
| 5506 | "applicationInfo.token=%s, " |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 5507 | "touchableRegion=", |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5508 | i, windowInfo->name.c_str(), windowInfo->id, |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5509 | windowInfo->displayId, |
| 5510 | windowInfo->inputConfig.string().c_str(), |
| 5511 | windowInfo->alpha, windowInfo->frameLeft, |
| 5512 | windowInfo->frameTop, windowInfo->frameRight, |
| 5513 | windowInfo->frameBottom, windowInfo->globalScaleFactor, |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5514 | windowInfo->applicationInfo.name.c_str(), |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 5515 | binderToString(windowInfo->applicationInfo.token).c_str()); |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 5516 | dump += dumpRegion(windowInfo->touchableRegion); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5517 | dump += StringPrintf(", ownerPid=%d, ownerUid=%d, dispatchingTimeout=%" PRId64 |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5518 | "ms, hasToken=%s, " |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5519 | "touchOcclusionMode=%s\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5520 | windowInfo->ownerPid, windowInfo->ownerUid, |
Bernardo Rufino | c2f1fad | 2020-11-04 17:30:57 +0000 | [diff] [blame] | 5521 | millis(windowInfo->dispatchingTimeout), |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 5522 | binderToString(windowInfo->token).c_str(), |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5523 | toString(windowInfo->touchOcclusionMode).c_str()); |
chaviw | 85b4420 | 2020-07-24 11:46:21 -0700 | [diff] [blame] | 5524 | windowInfo->transform.dump(dump, "transform", INDENT4); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5525 | } |
| 5526 | } else { |
| 5527 | dump += INDENT2 "Windows: <none>\n"; |
| 5528 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5529 | } |
| 5530 | } else { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5531 | dump += INDENT "Displays: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5532 | } |
| 5533 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5534 | if (!mGlobalMonitorsByDisplay.empty()) { |
| 5535 | for (const auto& [displayId, monitors] : mGlobalMonitorsByDisplay) { |
| 5536 | dump += StringPrintf(INDENT "Global monitors on display %d:\n", displayId); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5537 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5538 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5539 | } else { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5540 | dump += INDENT "Global Monitors: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5541 | } |
| 5542 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5543 | const nsecs_t currentTime = now(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5544 | |
| 5545 | // Dump recently dispatched or dropped events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5546 | if (!mRecentQueue.empty()) { |
| 5547 | dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size()); |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 5548 | for (const std::shared_ptr<EventEntry>& entry : mRecentQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5549 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5550 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5551 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5552 | } |
| 5553 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5554 | dump += INDENT "RecentQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5555 | } |
| 5556 | |
| 5557 | // Dump event currently being dispatched. |
| 5558 | if (mPendingEvent) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5559 | dump += INDENT "PendingEvent:\n"; |
| 5560 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5561 | dump += mPendingEvent->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5562 | dump += StringPrintf(", age=%" PRId64 "ms\n", |
| 5563 | ns2ms(currentTime - mPendingEvent->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5564 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5565 | dump += INDENT "PendingEvent: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5566 | } |
| 5567 | |
| 5568 | // Dump inbound events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5569 | if (!mInboundQueue.empty()) { |
| 5570 | dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size()); |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 5571 | for (const std::shared_ptr<EventEntry>& entry : mInboundQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5572 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5573 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5574 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5575 | } |
| 5576 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5577 | dump += INDENT "InboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5578 | } |
| 5579 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5580 | if (!mReplacedKeys.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5581 | dump += INDENT "ReplacedKeys:\n"; |
Michael Wright | 3cec446 | 2022-11-24 22:05:46 +0000 | [diff] [blame] | 5582 | for (const auto& [replacement, newKeyCode] : mReplacedKeys) { |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5583 | dump += StringPrintf(INDENT2 "originalKeyCode=%d, deviceId=%d -> newKeyCode=%d\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5584 | replacement.keyCode, replacement.deviceId, newKeyCode); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5585 | } |
| 5586 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5587 | dump += INDENT "ReplacedKeys: <empty>\n"; |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5588 | } |
| 5589 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5590 | if (!mCommandQueue.empty()) { |
| 5591 | dump += StringPrintf(INDENT "CommandQueue: size=%zu\n", mCommandQueue.size()); |
| 5592 | } else { |
| 5593 | dump += INDENT "CommandQueue: <empty>\n"; |
| 5594 | } |
| 5595 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5596 | if (!mConnectionsByToken.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5597 | dump += INDENT "Connections:\n"; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5598 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5599 | dump += StringPrintf(INDENT2 "%i: channelName='%s', windowName='%s', " |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5600 | "status=%s, monitor=%s, responsive=%s\n", |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5601 | connection->inputChannel->getFd().get(), |
| 5602 | connection->getInputChannelName().c_str(), |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5603 | connection->getWindowName().c_str(), |
| 5604 | ftl::enum_string(connection->status).c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5605 | toString(connection->monitor), toString(connection->responsive)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5606 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5607 | if (!connection->outboundQueue.empty()) { |
| 5608 | dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n", |
| 5609 | connection->outboundQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5610 | dump += dumpQueue(connection->outboundQueue, currentTime); |
| 5611 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5612 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5613 | dump += INDENT3 "OutboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5614 | } |
| 5615 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5616 | if (!connection->waitQueue.empty()) { |
| 5617 | dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n", |
| 5618 | connection->waitQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5619 | dump += dumpQueue(connection->waitQueue, currentTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5620 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5621 | dump += INDENT3 "WaitQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5622 | } |
| 5623 | } |
| 5624 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5625 | dump += INDENT "Connections: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5626 | } |
| 5627 | |
| 5628 | if (isAppSwitchPendingLocked()) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5629 | dump += StringPrintf(INDENT "AppSwitch: pending, due in %" PRId64 "ms\n", |
| 5630 | ns2ms(mAppSwitchDueTime - now())); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5631 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5632 | dump += INDENT "AppSwitch: not pending\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5633 | } |
| 5634 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5635 | if (!mTouchModePerDisplay.empty()) { |
| 5636 | dump += INDENT "TouchModePerDisplay:\n"; |
| 5637 | for (const auto& [displayId, touchMode] : mTouchModePerDisplay) { |
| 5638 | dump += StringPrintf(INDENT2 "Display: %" PRId32 " TouchMode: %s\n", displayId, |
| 5639 | std::to_string(touchMode).c_str()); |
| 5640 | } |
| 5641 | } else { |
| 5642 | dump += INDENT "TouchModePerDisplay: <none>\n"; |
| 5643 | } |
| 5644 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5645 | dump += INDENT "Configuration:\n"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5646 | dump += StringPrintf(INDENT2 "KeyRepeatDelay: %" PRId64 "ms\n", ns2ms(mConfig.keyRepeatDelay)); |
| 5647 | dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n", |
| 5648 | ns2ms(mConfig.keyRepeatTimeout)); |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 5649 | dump += mLatencyTracker.dump(INDENT2); |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 5650 | dump += mLatencyAggregator.dump(INDENT2); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5651 | } |
| 5652 | |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 5653 | void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) const { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5654 | const size_t numMonitors = monitors.size(); |
| 5655 | for (size_t i = 0; i < numMonitors; i++) { |
| 5656 | const Monitor& monitor = monitors[i]; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5657 | const std::shared_ptr<InputChannel>& channel = monitor.inputChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5658 | dump += StringPrintf(INDENT2 "%zu: '%s', ", i, channel->getName().c_str()); |
| 5659 | dump += "\n"; |
| 5660 | } |
| 5661 | } |
| 5662 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5663 | class LooperEventCallback : public LooperCallback { |
| 5664 | public: |
| 5665 | LooperEventCallback(std::function<int(int events)> callback) : mCallback(callback) {} |
| 5666 | int handleEvent(int /*fd*/, int events, void* /*data*/) override { return mCallback(events); } |
| 5667 | |
| 5668 | private: |
| 5669 | std::function<int(int events)> mCallback; |
| 5670 | }; |
| 5671 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5672 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputChannel(const std::string& name) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5673 | if (DEBUG_CHANNEL_CREATION) { |
| 5674 | ALOGD("channel '%s' ~ createInputChannel", name.c_str()); |
| 5675 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5676 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5677 | std::unique_ptr<InputChannel> serverChannel; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5678 | std::unique_ptr<InputChannel> clientChannel; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5679 | status_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5680 | |
| 5681 | if (result) { |
| 5682 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5683 | } |
| 5684 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5685 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5686 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5687 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5688 | int fd = serverChannel->getFd(); |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 5689 | std::shared_ptr<Connection> connection = |
| 5690 | std::make_shared<Connection>(std::move(serverChannel), /*monitor=*/false, |
| 5691 | mIdGenerator); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5692 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5693 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5694 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5695 | } |
| 5696 | mConnectionsByToken.emplace(token, connection); |
| 5697 | |
| 5698 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5699 | this, std::placeholders::_1, token); |
| 5700 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5701 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, sp<LooperEventCallback>::make(callback), |
| 5702 | nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5703 | } // release lock |
| 5704 | |
| 5705 | // Wake the looper because some connections have changed. |
| 5706 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5707 | return clientChannel; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5708 | } |
| 5709 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5710 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputMonitor(int32_t displayId, |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5711 | const std::string& name, |
| 5712 | int32_t pid) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5713 | std::shared_ptr<InputChannel> serverChannel; |
| 5714 | std::unique_ptr<InputChannel> clientChannel; |
| 5715 | status_t result = openInputChannelPair(name, serverChannel, clientChannel); |
| 5716 | if (result) { |
| 5717 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5718 | } |
| 5719 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5720 | { // acquire lock |
| 5721 | std::scoped_lock _l(mLock); |
| 5722 | |
| 5723 | if (displayId < 0) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5724 | return base::Error(BAD_VALUE) << "Attempted to create input monitor with name " << name |
| 5725 | << " without a specified display."; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5726 | } |
| 5727 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 5728 | std::shared_ptr<Connection> connection = |
| 5729 | std::make_shared<Connection>(serverChannel, /*monitor=*/true, mIdGenerator); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5730 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5731 | const int fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5732 | |
| 5733 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5734 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5735 | } |
| 5736 | mConnectionsByToken.emplace(token, connection); |
| 5737 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5738 | this, std::placeholders::_1, token); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5739 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5740 | mGlobalMonitorsByDisplay[displayId].emplace_back(serverChannel, pid); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5741 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5742 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, sp<LooperEventCallback>::make(callback), |
| 5743 | nullptr); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5744 | } |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5745 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5746 | // Wake the looper because some connections have changed. |
| 5747 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5748 | return clientChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5749 | } |
| 5750 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5751 | status_t InputDispatcher::removeInputChannel(const sp<IBinder>& connectionToken) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5752 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5753 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5754 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5755 | status_t status = removeInputChannelLocked(connectionToken, /*notify=*/false); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5756 | if (status) { |
| 5757 | return status; |
| 5758 | } |
| 5759 | } // release lock |
| 5760 | |
| 5761 | // Wake the poll loop because removing the connection may have changed the current |
| 5762 | // synchronization state. |
| 5763 | mLooper->wake(); |
| 5764 | return OK; |
| 5765 | } |
| 5766 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5767 | status_t InputDispatcher::removeInputChannelLocked(const sp<IBinder>& connectionToken, |
| 5768 | bool notify) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 5769 | std::shared_ptr<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5770 | if (connection == nullptr) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5771 | // 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] | 5772 | return BAD_VALUE; |
| 5773 | } |
| 5774 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5775 | removeConnectionLocked(connection); |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 5776 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5777 | if (connection->monitor) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5778 | removeMonitorChannelLocked(connectionToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5779 | } |
| 5780 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5781 | mLooper->removeFd(connection->inputChannel->getFd()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5782 | |
| 5783 | nsecs_t currentTime = now(); |
| 5784 | abortBrokenDispatchCycleLocked(currentTime, connection, notify); |
| 5785 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5786 | connection->status = Connection::Status::ZOMBIE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5787 | return OK; |
| 5788 | } |
| 5789 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5790 | void InputDispatcher::removeMonitorChannelLocked(const sp<IBinder>& connectionToken) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5791 | for (auto it = mGlobalMonitorsByDisplay.begin(); it != mGlobalMonitorsByDisplay.end();) { |
| 5792 | auto& [displayId, monitors] = *it; |
| 5793 | std::erase_if(monitors, [connectionToken](const Monitor& monitor) { |
| 5794 | return monitor.inputChannel->getConnectionToken() == connectionToken; |
| 5795 | }); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5796 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5797 | if (monitors.empty()) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5798 | it = mGlobalMonitorsByDisplay.erase(it); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5799 | } else { |
| 5800 | ++it; |
| 5801 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5802 | } |
| 5803 | } |
| 5804 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5805 | status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5806 | std::scoped_lock _l(mLock); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 5807 | return pilferPointersLocked(token); |
| 5808 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5809 | |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 5810 | status_t InputDispatcher::pilferPointersLocked(const sp<IBinder>& token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5811 | const std::shared_ptr<InputChannel> requestingChannel = getInputChannelLocked(token); |
| 5812 | if (!requestingChannel) { |
| 5813 | ALOGW("Attempted to pilfer pointers from an un-registered channel or invalid token"); |
| 5814 | return BAD_VALUE; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5815 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5816 | |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5817 | auto [statePtr, windowPtr, displayId] = findTouchStateWindowAndDisplayLocked(token); |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 5818 | if (statePtr == nullptr || windowPtr == nullptr || windowPtr->pointerIds.none()) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5819 | ALOGW("Attempted to pilfer points from a channel without any on-going pointer streams." |
| 5820 | " Ignoring."); |
| 5821 | return BAD_VALUE; |
| 5822 | } |
| 5823 | |
| 5824 | TouchState& state = *statePtr; |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 5825 | TouchedWindow& window = *windowPtr; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5826 | // Send cancel events to all the input channels we're stealing from. |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 5827 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5828 | "input channel stole pointer stream"); |
| 5829 | options.deviceId = state.deviceId; |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5830 | options.displayId = displayId; |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 5831 | options.pointerIds = window.pointerIds; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5832 | std::string canceledWindows; |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 5833 | for (const TouchedWindow& w : state.windows) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5834 | const std::shared_ptr<InputChannel> channel = |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 5835 | getInputChannelLocked(w.windowHandle->getToken()); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5836 | if (channel != nullptr && channel->getConnectionToken() != token) { |
| 5837 | synthesizeCancelationEventsForInputChannelLocked(channel, options); |
| 5838 | canceledWindows += canceledWindows.empty() ? "[" : ", "; |
| 5839 | canceledWindows += channel->getName(); |
| 5840 | } |
| 5841 | } |
| 5842 | canceledWindows += canceledWindows.empty() ? "[]" : "]"; |
| 5843 | ALOGI("Channel %s is stealing touch from %s", requestingChannel->getName().c_str(), |
| 5844 | canceledWindows.c_str()); |
| 5845 | |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 5846 | // Prevent the gesture from being sent to any other windows. |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 5847 | // This only blocks relevant pointers to be sent to other windows |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 5848 | window.pilferedPointerIds |= window.pointerIds; |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 5849 | |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 5850 | state.cancelPointersForWindowsExcept(window.pointerIds, token); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5851 | return OK; |
| 5852 | } |
| 5853 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5854 | void InputDispatcher::requestPointerCapture(const sp<IBinder>& windowToken, bool enabled) { |
| 5855 | { // acquire lock |
| 5856 | std::scoped_lock _l(mLock); |
| 5857 | if (DEBUG_FOCUS) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5858 | const sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(windowToken); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5859 | ALOGI("Request to %s Pointer Capture from: %s.", enabled ? "enable" : "disable", |
| 5860 | windowHandle != nullptr ? windowHandle->getName().c_str() |
| 5861 | : "token without window"); |
| 5862 | } |
| 5863 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5864 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5865 | if (focusedToken != windowToken) { |
| 5866 | ALOGW("Ignoring request to %s Pointer Capture: window does not have focus.", |
| 5867 | enabled ? "enable" : "disable"); |
| 5868 | return; |
| 5869 | } |
| 5870 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5871 | if (enabled == mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5872 | ALOGW("Ignoring request to %s Pointer Capture: " |
| 5873 | "window has %s requested pointer capture.", |
| 5874 | enabled ? "enable" : "disable", enabled ? "already" : "not"); |
| 5875 | return; |
| 5876 | } |
| 5877 | |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 5878 | if (enabled) { |
| 5879 | if (std::find(mIneligibleDisplaysForPointerCapture.begin(), |
| 5880 | mIneligibleDisplaysForPointerCapture.end(), |
| 5881 | mFocusedDisplayId) != mIneligibleDisplaysForPointerCapture.end()) { |
| 5882 | ALOGW("Ignoring request to enable Pointer Capture: display is not eligible"); |
| 5883 | return; |
| 5884 | } |
| 5885 | } |
| 5886 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5887 | setPointerCaptureLocked(enabled); |
| 5888 | } // release lock |
| 5889 | |
| 5890 | // Wake the thread to process command entries. |
| 5891 | mLooper->wake(); |
| 5892 | } |
| 5893 | |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 5894 | void InputDispatcher::setDisplayEligibilityForPointerCapture(int32_t displayId, bool isEligible) { |
| 5895 | { // acquire lock |
| 5896 | std::scoped_lock _l(mLock); |
| 5897 | std::erase(mIneligibleDisplaysForPointerCapture, displayId); |
| 5898 | if (!isEligible) { |
| 5899 | mIneligibleDisplaysForPointerCapture.push_back(displayId); |
| 5900 | } |
| 5901 | } // release lock |
| 5902 | } |
| 5903 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5904 | std::optional<int32_t> InputDispatcher::findMonitorPidByTokenLocked(const sp<IBinder>& token) { |
| 5905 | for (const auto& [_, monitors] : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5906 | for (const Monitor& monitor : monitors) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5907 | if (monitor.inputChannel->getConnectionToken() == token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5908 | return monitor.pid; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5909 | } |
| 5910 | } |
| 5911 | } |
| 5912 | return std::nullopt; |
| 5913 | } |
| 5914 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 5915 | std::shared_ptr<Connection> InputDispatcher::getConnectionLocked( |
| 5916 | const sp<IBinder>& inputConnectionToken) const { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 5917 | if (inputConnectionToken == nullptr) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5918 | return nullptr; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 5919 | } |
| 5920 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5921 | for (const auto& [token, connection] : mConnectionsByToken) { |
| 5922 | if (token == inputConnectionToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5923 | return connection; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5924 | } |
| 5925 | } |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 5926 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5927 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5928 | } |
| 5929 | |
Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 5930 | std::string InputDispatcher::getConnectionNameLocked(const sp<IBinder>& connectionToken) const { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 5931 | std::shared_ptr<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 5932 | if (connection == nullptr) { |
| 5933 | return "<nullptr>"; |
| 5934 | } |
| 5935 | return connection->getInputChannelName(); |
| 5936 | } |
| 5937 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 5938 | void InputDispatcher::removeConnectionLocked(const std::shared_ptr<Connection>& connection) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5939 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5940 | mConnectionsByToken.erase(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5941 | } |
| 5942 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5943 | void InputDispatcher::doDispatchCycleFinishedCommand(nsecs_t finishTime, |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 5944 | const std::shared_ptr<Connection>& connection, |
| 5945 | uint32_t seq, bool handled, |
| 5946 | nsecs_t consumeTime) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5947 | // Handle post-event policy actions. |
| 5948 | std::deque<DispatchEntry*>::iterator dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 5949 | if (dispatchEntryIt == connection->waitQueue.end()) { |
| 5950 | return; |
| 5951 | } |
| 5952 | DispatchEntry* dispatchEntry = *dispatchEntryIt; |
| 5953 | const nsecs_t eventDuration = finishTime - dispatchEntry->deliveryTime; |
| 5954 | if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) { |
| 5955 | ALOGI("%s spent %" PRId64 "ms processing %s", connection->getWindowName().c_str(), |
| 5956 | ns2ms(eventDuration), dispatchEntry->eventEntry->getDescription().c_str()); |
| 5957 | } |
| 5958 | if (shouldReportFinishedEvent(*dispatchEntry, *connection)) { |
| 5959 | mLatencyTracker.trackFinishedEvent(dispatchEntry->eventEntry->id, |
| 5960 | connection->inputChannel->getConnectionToken(), |
| 5961 | dispatchEntry->deliveryTime, consumeTime, finishTime); |
| 5962 | } |
| 5963 | |
| 5964 | bool restartEvent; |
| 5965 | if (dispatchEntry->eventEntry->type == EventEntry::Type::KEY) { |
| 5966 | KeyEntry& keyEntry = static_cast<KeyEntry&>(*(dispatchEntry->eventEntry)); |
| 5967 | restartEvent = |
| 5968 | afterKeyEventLockedInterruptable(connection, dispatchEntry, keyEntry, handled); |
| 5969 | } else if (dispatchEntry->eventEntry->type == EventEntry::Type::MOTION) { |
| 5970 | MotionEntry& motionEntry = static_cast<MotionEntry&>(*(dispatchEntry->eventEntry)); |
| 5971 | restartEvent = afterMotionEventLockedInterruptable(connection, dispatchEntry, motionEntry, |
| 5972 | handled); |
| 5973 | } else { |
| 5974 | restartEvent = false; |
| 5975 | } |
| 5976 | |
| 5977 | // Dequeue the event and start the next cycle. |
| 5978 | // Because the lock might have been released, it is possible that the |
| 5979 | // contents of the wait queue to have been drained, so we need to double-check |
| 5980 | // a few things. |
| 5981 | dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 5982 | if (dispatchEntryIt != connection->waitQueue.end()) { |
| 5983 | dispatchEntry = *dispatchEntryIt; |
| 5984 | connection->waitQueue.erase(dispatchEntryIt); |
| 5985 | const sp<IBinder>& connectionToken = connection->inputChannel->getConnectionToken(); |
| 5986 | mAnrTracker.erase(dispatchEntry->timeoutTime, connectionToken); |
| 5987 | if (!connection->responsive) { |
| 5988 | connection->responsive = isConnectionResponsive(*connection); |
| 5989 | if (connection->responsive) { |
| 5990 | // The connection was unresponsive, and now it's responsive. |
| 5991 | processConnectionResponsiveLocked(*connection); |
| 5992 | } |
| 5993 | } |
| 5994 | traceWaitQueueLength(*connection); |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5995 | if (restartEvent && connection->status == Connection::Status::NORMAL) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5996 | connection->outboundQueue.push_front(dispatchEntry); |
| 5997 | traceOutboundQueueLength(*connection); |
| 5998 | } else { |
| 5999 | releaseDispatchEntry(dispatchEntry); |
| 6000 | } |
| 6001 | } |
| 6002 | |
| 6003 | // Start the next dispatch cycle for this connection. |
| 6004 | startDispatchCycleLocked(now(), connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6005 | } |
| 6006 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6007 | void InputDispatcher::sendFocusChangedCommandLocked(const sp<IBinder>& oldToken, |
| 6008 | const sp<IBinder>& newToken) { |
| 6009 | auto command = [this, oldToken, newToken]() REQUIRES(mLock) { |
| 6010 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame^] | 6011 | mPolicy.notifyFocusChanged(oldToken, newToken); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6012 | }; |
| 6013 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6014 | } |
| 6015 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6016 | void InputDispatcher::sendDropWindowCommandLocked(const sp<IBinder>& token, float x, float y) { |
| 6017 | auto command = [this, token, x, y]() REQUIRES(mLock) { |
| 6018 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame^] | 6019 | mPolicy.notifyDropWindow(token, x, y); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6020 | }; |
| 6021 | postCommandLocked(std::move(command)); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 6022 | } |
| 6023 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6024 | void InputDispatcher::onAnrLocked(const std::shared_ptr<Connection>& connection) { |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6025 | if (connection == nullptr) { |
| 6026 | LOG_ALWAYS_FATAL("Caller must check for nullness"); |
| 6027 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6028 | // Since we are allowing the policy to extend the timeout, maybe the waitQueue |
| 6029 | // is already healthy again. Don't raise ANR in this situation |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6030 | if (connection->waitQueue.empty()) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6031 | ALOGI("Not raising ANR because the connection %s has recovered", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6032 | connection->inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6033 | return; |
| 6034 | } |
| 6035 | /** |
| 6036 | * The "oldestEntry" is the entry that was first sent to the application. That entry, however, |
| 6037 | * may not be the one that caused the timeout to occur. One possibility is that window timeout |
| 6038 | * has changed. This could cause newer entries to time out before the already dispatched |
| 6039 | * entries. In that situation, the newest entries caused ANR. But in all likelihood, the app |
| 6040 | * processes the events linearly. So providing information about the oldest entry seems to be |
| 6041 | * most useful. |
| 6042 | */ |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6043 | DispatchEntry* oldestEntry = *connection->waitQueue.begin(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6044 | const nsecs_t currentWait = now() - oldestEntry->deliveryTime; |
| 6045 | std::string reason = |
| 6046 | android::base::StringPrintf("%s is not responding. Waited %" PRId64 "ms for %s", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6047 | connection->inputChannel->getName().c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6048 | ns2ms(currentWait), |
| 6049 | oldestEntry->eventEntry->getDescription().c_str()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6050 | sp<IBinder> connectionToken = connection->inputChannel->getConnectionToken(); |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 6051 | updateLastAnrStateLocked(getWindowHandleLocked(connectionToken), reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6052 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6053 | processConnectionUnresponsiveLocked(*connection, std::move(reason)); |
| 6054 | |
| 6055 | // Stop waking up for events on this connection, it is already unresponsive |
| 6056 | cancelEventsForAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6057 | } |
| 6058 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6059 | void InputDispatcher::onAnrLocked(std::shared_ptr<InputApplicationHandle> application) { |
| 6060 | std::string reason = |
| 6061 | StringPrintf("%s does not have a focused window", application->getName().c_str()); |
| 6062 | updateLastAnrStateLocked(*application, reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6063 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6064 | auto command = [this, application = std::move(application)]() REQUIRES(mLock) { |
| 6065 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame^] | 6066 | mPolicy.notifyNoFocusedWindowAnr(application); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6067 | }; |
| 6068 | postCommandLocked(std::move(command)); |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 6069 | } |
| 6070 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 6071 | void InputDispatcher::updateLastAnrStateLocked(const sp<WindowInfoHandle>& window, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6072 | const std::string& reason) { |
| 6073 | const std::string windowLabel = getApplicationWindowLabel(nullptr, window); |
| 6074 | updateLastAnrStateLocked(windowLabel, reason); |
| 6075 | } |
| 6076 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6077 | void InputDispatcher::updateLastAnrStateLocked(const InputApplicationHandle& application, |
| 6078 | const std::string& reason) { |
| 6079 | const std::string windowLabel = getApplicationWindowLabel(&application, nullptr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6080 | updateLastAnrStateLocked(windowLabel, reason); |
| 6081 | } |
| 6082 | |
| 6083 | void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel, |
| 6084 | const std::string& reason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6085 | // 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] | 6086 | time_t t = time(nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6087 | struct tm tm; |
| 6088 | localtime_r(&t, &tm); |
| 6089 | char timestr[64]; |
| 6090 | strftime(timestr, sizeof(timestr), "%F %T", &tm); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6091 | mLastAnrState.clear(); |
| 6092 | mLastAnrState += INDENT "ANR:\n"; |
| 6093 | mLastAnrState += StringPrintf(INDENT2 "Time: %s\n", timestr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6094 | mLastAnrState += StringPrintf(INDENT2 "Reason: %s\n", reason.c_str()); |
| 6095 | mLastAnrState += StringPrintf(INDENT2 "Window: %s\n", windowLabel.c_str()); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6096 | dumpDispatchStateLocked(mLastAnrState); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6097 | } |
| 6098 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6099 | void InputDispatcher::doInterceptKeyBeforeDispatchingCommand(const sp<IBinder>& focusedWindowToken, |
| 6100 | KeyEntry& entry) { |
| 6101 | const KeyEvent event = createKeyEvent(entry); |
| 6102 | nsecs_t delay = 0; |
| 6103 | { // release lock |
| 6104 | scoped_unlock unlock(mLock); |
| 6105 | android::base::Timer t; |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame^] | 6106 | delay = mPolicy.interceptKeyBeforeDispatching(focusedWindowToken, event, entry.policyFlags); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6107 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 6108 | ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms", |
| 6109 | std::to_string(t.duration().count()).c_str()); |
| 6110 | } |
| 6111 | } // acquire lock |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6112 | |
| 6113 | if (delay < 0) { |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 6114 | entry.interceptKeyResult = KeyEntry::InterceptKeyResult::SKIP; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6115 | } else if (delay == 0) { |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 6116 | entry.interceptKeyResult = KeyEntry::InterceptKeyResult::CONTINUE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6117 | } else { |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 6118 | entry.interceptKeyResult = KeyEntry::InterceptKeyResult::TRY_AGAIN_LATER; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6119 | entry.interceptKeyWakeupTime = now() + delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6120 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6121 | } |
| 6122 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6123 | void InputDispatcher::sendWindowUnresponsiveCommandLocked(const sp<IBinder>& token, |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6124 | std::optional<int32_t> pid, |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6125 | std::string reason) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6126 | auto command = [this, token, pid, reason = std::move(reason)]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6127 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame^] | 6128 | mPolicy.notifyWindowUnresponsive(token, pid, reason); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6129 | }; |
| 6130 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6131 | } |
| 6132 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6133 | void InputDispatcher::sendWindowResponsiveCommandLocked(const sp<IBinder>& token, |
| 6134 | std::optional<int32_t> pid) { |
| 6135 | auto command = [this, token, pid]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6136 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame^] | 6137 | mPolicy.notifyWindowResponsive(token, pid); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6138 | }; |
| 6139 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6140 | } |
| 6141 | |
| 6142 | /** |
| 6143 | * Tell the policy that a connection has become unresponsive so that it can start ANR. |
| 6144 | * Check whether the connection of interest is a monitor or a window, and add the corresponding |
| 6145 | * command entry to the command queue. |
| 6146 | */ |
| 6147 | void InputDispatcher::processConnectionUnresponsiveLocked(const Connection& connection, |
| 6148 | std::string reason) { |
| 6149 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6150 | std::optional<int32_t> pid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6151 | if (connection.monitor) { |
| 6152 | ALOGW("Monitor %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 6153 | reason.c_str()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6154 | pid = findMonitorPidByTokenLocked(connectionToken); |
| 6155 | } else { |
| 6156 | // The connection is a window |
| 6157 | ALOGW("Window %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 6158 | reason.c_str()); |
| 6159 | const sp<WindowInfoHandle> handle = getWindowHandleLocked(connectionToken); |
| 6160 | if (handle != nullptr) { |
| 6161 | pid = handle->getInfo()->ownerPid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6162 | } |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6163 | } |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6164 | sendWindowUnresponsiveCommandLocked(connectionToken, pid, std::move(reason)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6165 | } |
| 6166 | |
| 6167 | /** |
| 6168 | * Tell the policy that a connection has become responsive so that it can stop ANR. |
| 6169 | */ |
| 6170 | void InputDispatcher::processConnectionResponsiveLocked(const Connection& connection) { |
| 6171 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6172 | std::optional<int32_t> pid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6173 | if (connection.monitor) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6174 | pid = findMonitorPidByTokenLocked(connectionToken); |
| 6175 | } else { |
| 6176 | // The connection is a window |
| 6177 | const sp<WindowInfoHandle> handle = getWindowHandleLocked(connectionToken); |
| 6178 | if (handle != nullptr) { |
| 6179 | pid = handle->getInfo()->ownerPid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6180 | } |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6181 | } |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6182 | sendWindowResponsiveCommandLocked(connectionToken, pid); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6183 | } |
| 6184 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6185 | bool InputDispatcher::afterKeyEventLockedInterruptable( |
| 6186 | const std::shared_ptr<Connection>& connection, DispatchEntry* dispatchEntry, |
| 6187 | KeyEntry& keyEntry, bool handled) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6188 | if (keyEntry.flags & AKEY_EVENT_FLAG_FALLBACK) { |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6189 | if (!handled) { |
| 6190 | // Report the key as unhandled, since the fallback was not handled. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6191 | mReporter->reportUnhandledKey(keyEntry.id); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6192 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6193 | return false; |
| 6194 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6195 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6196 | // Get the fallback key state. |
| 6197 | // Clear it out after dispatching the UP. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6198 | int32_t originalKeyCode = keyEntry.keyCode; |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6199 | std::optional<int32_t> fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6200 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6201 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6202 | } |
| 6203 | |
| 6204 | if (handled || !dispatchEntry->hasForegroundTarget()) { |
| 6205 | // If the application handles the original key for which we previously |
| 6206 | // generated a fallback or if the window is not a foreground window, |
| 6207 | // then cancel the associated fallback key, if any. |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6208 | if (fallbackKeyCode) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6209 | // Dispatch the unhandled key to the policy with the cancel flag. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6210 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6211 | ALOGD("Unhandled key event: Asking policy to cancel fallback action. " |
| 6212 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6213 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, |
| 6214 | keyEntry.policyFlags); |
| 6215 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6216 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6217 | event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6218 | |
| 6219 | mLock.unlock(); |
| 6220 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame^] | 6221 | if (const auto unhandledKeyFallback = |
| 6222 | mPolicy.dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), |
| 6223 | event, keyEntry.policyFlags); |
| 6224 | unhandledKeyFallback) { |
| 6225 | event = *unhandledKeyFallback; |
| 6226 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6227 | |
| 6228 | mLock.lock(); |
| 6229 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6230 | // Cancel the fallback key. |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6231 | if (*fallbackKeyCode != AKEYCODE_UNKNOWN) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 6232 | CancelationOptions options(CancelationOptions::Mode::CANCEL_FALLBACK_EVENTS, |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6233 | "application handled the original non-fallback key " |
| 6234 | "or is no longer a foreground target, " |
| 6235 | "canceling previously dispatched fallback key"); |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6236 | options.keyCode = *fallbackKeyCode; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6237 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6238 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6239 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6240 | } |
| 6241 | } else { |
| 6242 | // If the application did not handle a non-fallback key, first check |
| 6243 | // that we are in a good state to perform unhandled key event processing |
| 6244 | // Then ask the policy what to do with it. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6245 | bool initialDown = keyEntry.action == AKEY_EVENT_ACTION_DOWN && keyEntry.repeatCount == 0; |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6246 | if (!fallbackKeyCode && !initialDown) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6247 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6248 | ALOGD("Unhandled key event: Skipping unhandled key event processing " |
| 6249 | "since this is not an initial down. " |
| 6250 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6251 | originalKeyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 6252 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6253 | return false; |
| 6254 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6255 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6256 | // Dispatch the unhandled key to the policy. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6257 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6258 | ALOGD("Unhandled key event: Asking policy to perform fallback action. " |
| 6259 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6260 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 6261 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6262 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6263 | |
| 6264 | mLock.unlock(); |
| 6265 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame^] | 6266 | bool fallback = false; |
| 6267 | if (auto fb = mPolicy.dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), |
| 6268 | event, keyEntry.policyFlags); |
| 6269 | fb) { |
| 6270 | fallback = true; |
| 6271 | event = *fb; |
| 6272 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6273 | |
| 6274 | mLock.lock(); |
| 6275 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 6276 | if (connection->status != Connection::Status::NORMAL) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6277 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6278 | return false; |
| 6279 | } |
| 6280 | |
| 6281 | // Latch the fallback keycode for this key on an initial down. |
| 6282 | // The fallback keycode cannot change at any other point in the lifecycle. |
| 6283 | if (initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6284 | if (fallback) { |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6285 | *fallbackKeyCode = event.getKeyCode(); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6286 | } else { |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6287 | *fallbackKeyCode = AKEYCODE_UNKNOWN; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6288 | } |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6289 | connection->inputState.setFallbackKey(originalKeyCode, *fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6290 | } |
| 6291 | |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6292 | ALOG_ASSERT(fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6293 | |
| 6294 | // Cancel the fallback key if the policy decides not to send it anymore. |
| 6295 | // We will continue to dispatch the key to the policy but we will no |
| 6296 | // longer dispatch a fallback key to the application. |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6297 | if (*fallbackKeyCode != AKEYCODE_UNKNOWN && |
| 6298 | (!fallback || *fallbackKeyCode != event.getKeyCode())) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6299 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6300 | if (fallback) { |
| 6301 | ALOGD("Unhandled key event: Policy requested to send key %d" |
| 6302 | "as a fallback for %d, but on the DOWN it had requested " |
| 6303 | "to send %d instead. Fallback canceled.", |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6304 | event.getKeyCode(), originalKeyCode, *fallbackKeyCode); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6305 | } else { |
| 6306 | ALOGD("Unhandled key event: Policy did not request fallback for %d, " |
| 6307 | "but on the DOWN it had requested to send %d. " |
| 6308 | "Fallback canceled.", |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6309 | originalKeyCode, *fallbackKeyCode); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6310 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6311 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6312 | |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 6313 | CancelationOptions options(CancelationOptions::Mode::CANCEL_FALLBACK_EVENTS, |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6314 | "canceling fallback, policy no longer desires it"); |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6315 | options.keyCode = *fallbackKeyCode; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6316 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
| 6317 | |
| 6318 | fallback = false; |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6319 | *fallbackKeyCode = AKEYCODE_UNKNOWN; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6320 | if (keyEntry.action != AKEY_EVENT_ACTION_UP) { |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6321 | connection->inputState.setFallbackKey(originalKeyCode, *fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6322 | } |
| 6323 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6324 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6325 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6326 | { |
| 6327 | std::string msg; |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6328 | const std::map<int32_t, int32_t>& fallbackKeys = |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6329 | connection->inputState.getFallbackKeys(); |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6330 | for (const auto& [key, value] : fallbackKeys) { |
| 6331 | msg += StringPrintf(", %d->%d", key, value); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6332 | } |
| 6333 | ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.", |
| 6334 | fallbackKeys.size(), msg.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6335 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6336 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6337 | |
| 6338 | if (fallback) { |
| 6339 | // Restart the dispatch cycle using the fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6340 | keyEntry.eventTime = event.getEventTime(); |
| 6341 | keyEntry.deviceId = event.getDeviceId(); |
| 6342 | keyEntry.source = event.getSource(); |
| 6343 | keyEntry.displayId = event.getDisplayId(); |
| 6344 | keyEntry.flags = event.getFlags() | AKEY_EVENT_FLAG_FALLBACK; |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6345 | keyEntry.keyCode = *fallbackKeyCode; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6346 | keyEntry.scanCode = event.getScanCode(); |
| 6347 | keyEntry.metaState = event.getMetaState(); |
| 6348 | keyEntry.repeatCount = event.getRepeatCount(); |
| 6349 | keyEntry.downTime = event.getDownTime(); |
| 6350 | keyEntry.syntheticRepeat = false; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6351 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6352 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6353 | ALOGD("Unhandled key event: Dispatching fallback key. " |
| 6354 | "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x", |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 6355 | originalKeyCode, *fallbackKeyCode, keyEntry.metaState); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6356 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6357 | return true; // restart the event |
| 6358 | } else { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6359 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6360 | ALOGD("Unhandled key event: No fallback key."); |
| 6361 | } |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6362 | |
| 6363 | // Report the key as unhandled, since there is no fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6364 | mReporter->reportUnhandledKey(keyEntry.id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6365 | } |
| 6366 | } |
| 6367 | return false; |
| 6368 | } |
| 6369 | |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6370 | bool InputDispatcher::afterMotionEventLockedInterruptable( |
| 6371 | const std::shared_ptr<Connection>& connection, DispatchEntry* dispatchEntry, |
| 6372 | MotionEntry& motionEntry, bool handled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6373 | return false; |
| 6374 | } |
| 6375 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6376 | void InputDispatcher::traceInboundQueueLengthLocked() { |
| 6377 | if (ATRACE_ENABLED()) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 6378 | ATRACE_INT("iq", mInboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6379 | } |
| 6380 | } |
| 6381 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6382 | void InputDispatcher::traceOutboundQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6383 | if (ATRACE_ENABLED()) { |
| 6384 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6385 | snprintf(counterName, sizeof(counterName), "oq:%s", connection.getWindowName().c_str()); |
| 6386 | ATRACE_INT(counterName, connection.outboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6387 | } |
| 6388 | } |
| 6389 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6390 | void InputDispatcher::traceWaitQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6391 | if (ATRACE_ENABLED()) { |
| 6392 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6393 | snprintf(counterName, sizeof(counterName), "wq:%s", connection.getWindowName().c_str()); |
| 6394 | ATRACE_INT(counterName, connection.waitQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6395 | } |
| 6396 | } |
| 6397 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6398 | void InputDispatcher::dump(std::string& dump) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6399 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6400 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6401 | dump += "Input Dispatcher State:\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6402 | dumpDispatchStateLocked(dump); |
| 6403 | |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6404 | if (!mLastAnrState.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6405 | dump += "\nInput Dispatcher State at time of last ANR:\n"; |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6406 | dump += mLastAnrState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6407 | } |
| 6408 | } |
| 6409 | |
| 6410 | void InputDispatcher::monitor() { |
| 6411 | // 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] | 6412 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6413 | mLooper->wake(); |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6414 | mDispatcherIsAlive.wait(_l); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6415 | } |
| 6416 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6417 | /** |
| 6418 | * Wake up the dispatcher and wait until it processes all events and commands. |
| 6419 | * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so |
| 6420 | * this method can be safely called from any thread, as long as you've ensured that |
| 6421 | * the work you are interested in completing has already been queued. |
| 6422 | */ |
| 6423 | bool InputDispatcher::waitForIdle() { |
| 6424 | /** |
| 6425 | * Timeout should represent the longest possible time that a device might spend processing |
| 6426 | * events and commands. |
| 6427 | */ |
| 6428 | constexpr std::chrono::duration TIMEOUT = 100ms; |
| 6429 | std::unique_lock lock(mLock); |
| 6430 | mLooper->wake(); |
| 6431 | std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT); |
| 6432 | return result == std::cv_status::no_timeout; |
| 6433 | } |
| 6434 | |
Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 6435 | /** |
| 6436 | * Sets focus to the window identified by the token. This must be called |
| 6437 | * after updating any input window handles. |
| 6438 | * |
| 6439 | * Params: |
| 6440 | * request.token - input channel token used to identify the window that should gain focus. |
| 6441 | * request.focusedToken - the token that the caller expects currently to be focused. If the |
| 6442 | * specified token does not match the currently focused window, this request will be dropped. |
| 6443 | * If the specified focused token matches the currently focused window, the call will succeed. |
| 6444 | * Set this to "null" if this call should succeed no matter what the currently focused token is. |
| 6445 | * request.timestamp - SYSTEM_TIME_MONOTONIC timestamp in nanos set by the client (wm) |
| 6446 | * when requesting the focus change. This determines which request gets |
| 6447 | * precedence if there is a focus change request from another source such as pointer down. |
| 6448 | */ |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6449 | void InputDispatcher::setFocusedWindow(const FocusRequest& request) { |
| 6450 | { // acquire lock |
| 6451 | std::scoped_lock _l(mLock); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6452 | std::optional<FocusResolver::FocusChanges> changes = |
| 6453 | mFocusResolver.setFocusedWindow(request, getWindowHandlesLocked(request.displayId)); |
| 6454 | if (changes) { |
| 6455 | onFocusChangedLocked(*changes); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6456 | } |
| 6457 | } // release lock |
| 6458 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6459 | mLooper->wake(); |
| 6460 | } |
| 6461 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6462 | void InputDispatcher::onFocusChangedLocked(const FocusResolver::FocusChanges& changes) { |
| 6463 | if (changes.oldFocus) { |
| 6464 | std::shared_ptr<InputChannel> focusedInputChannel = getInputChannelLocked(changes.oldFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6465 | if (focusedInputChannel) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 6466 | CancelationOptions options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6467 | "focus left window"); |
| 6468 | synthesizeCancelationEventsForInputChannelLocked(focusedInputChannel, options); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6469 | enqueueFocusEventLocked(changes.oldFocus, /*hasFocus=*/false, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6470 | } |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6471 | } |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6472 | if (changes.newFocus) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6473 | enqueueFocusEventLocked(changes.newFocus, /*hasFocus=*/true, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6474 | } |
| 6475 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6476 | // If a window has pointer capture, then it must have focus. We need to ensure that this |
| 6477 | // contract is upheld when pointer capture is being disabled due to a loss of window focus. |
| 6478 | // If the window loses focus before it loses pointer capture, then the window can be in a state |
| 6479 | // where it has pointer capture but not focus, violating the contract. Therefore we must |
| 6480 | // dispatch the pointer capture event before the focus event. Since focus events are added to |
| 6481 | // the front of the queue (above), we add the pointer capture event to the front of the queue |
| 6482 | // after the focus events are added. This ensures the pointer capture event ends up at the |
| 6483 | // front. |
| 6484 | disablePointerCaptureForcedLocked(); |
| 6485 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6486 | if (mFocusedDisplayId == changes.displayId) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6487 | sendFocusChangedCommandLocked(changes.oldFocus, changes.newFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6488 | } |
| 6489 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6490 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6491 | void InputDispatcher::disablePointerCaptureForcedLocked() { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6492 | if (!mCurrentPointerCaptureRequest.enable && !mWindowTokenWithPointerCapture) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6493 | return; |
| 6494 | } |
| 6495 | |
| 6496 | ALOGD_IF(DEBUG_FOCUS, "Disabling Pointer Capture because the window lost focus."); |
| 6497 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6498 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6499 | setPointerCaptureLocked(false); |
| 6500 | } |
| 6501 | |
| 6502 | if (!mWindowTokenWithPointerCapture) { |
| 6503 | // No need to send capture changes because no window has capture. |
| 6504 | return; |
| 6505 | } |
| 6506 | |
| 6507 | if (mPendingEvent != nullptr) { |
| 6508 | // Move the pending event to the front of the queue. This will give the chance |
| 6509 | // for the pending event to be dropped if it is a captured event. |
| 6510 | mInboundQueue.push_front(mPendingEvent); |
| 6511 | mPendingEvent = nullptr; |
| 6512 | } |
| 6513 | |
| 6514 | auto entry = std::make_unique<PointerCaptureChangedEntry>(mIdGenerator.nextId(), now(), |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6515 | mCurrentPointerCaptureRequest); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6516 | mInboundQueue.push_front(std::move(entry)); |
| 6517 | } |
| 6518 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6519 | void InputDispatcher::setPointerCaptureLocked(bool enable) { |
| 6520 | mCurrentPointerCaptureRequest.enable = enable; |
| 6521 | mCurrentPointerCaptureRequest.seq++; |
| 6522 | auto command = [this, request = mCurrentPointerCaptureRequest]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6523 | scoped_unlock unlock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame^] | 6524 | mPolicy.setPointerCapture(request); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6525 | }; |
| 6526 | postCommandLocked(std::move(command)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6527 | } |
| 6528 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6529 | void InputDispatcher::displayRemoved(int32_t displayId) { |
| 6530 | { // acquire lock |
| 6531 | std::scoped_lock _l(mLock); |
| 6532 | // Set an empty list to remove all handles from the specific display. |
| 6533 | setInputWindowsLocked(/* window handles */ {}, displayId); |
| 6534 | setFocusedApplicationLocked(displayId, nullptr); |
| 6535 | // Call focus resolver to clean up stale requests. This must be called after input windows |
| 6536 | // have been removed for the removed display. |
| 6537 | mFocusResolver.displayRemoved(displayId); |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 6538 | // Reset pointer capture eligibility, regardless of previous state. |
| 6539 | std::erase(mIneligibleDisplaysForPointerCapture, displayId); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 6540 | // Remove the associated touch mode state. |
| 6541 | mTouchModePerDisplay.erase(displayId); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6542 | } // release lock |
| 6543 | |
| 6544 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6545 | mLooper->wake(); |
| 6546 | } |
| 6547 | |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6548 | void InputDispatcher::onWindowInfosChanged(const std::vector<WindowInfo>& windowInfos, |
| 6549 | const std::vector<DisplayInfo>& displayInfos) { |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6550 | // The listener sends the windows as a flattened array. Separate the windows by display for |
| 6551 | // more convenient parsing. |
| 6552 | std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>> handlesPerDisplay; |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6553 | for (const auto& info : windowInfos) { |
| 6554 | handlesPerDisplay.emplace(info.displayId, std::vector<sp<WindowInfoHandle>>()); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6555 | handlesPerDisplay[info.displayId].push_back(sp<WindowInfoHandle>::make(info)); |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6556 | } |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6557 | |
| 6558 | { // acquire lock |
| 6559 | std::scoped_lock _l(mLock); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 6560 | |
| 6561 | // Ensure that we have an entry created for all existing displays so that if a displayId has |
| 6562 | // no windows, we can tell that the windows were removed from the display. |
| 6563 | for (const auto& [displayId, _] : mWindowHandlesByDisplay) { |
| 6564 | handlesPerDisplay[displayId]; |
| 6565 | } |
| 6566 | |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6567 | mDisplayInfos.clear(); |
| 6568 | for (const auto& displayInfo : displayInfos) { |
| 6569 | mDisplayInfos.emplace(displayInfo.displayId, displayInfo); |
| 6570 | } |
| 6571 | |
| 6572 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 6573 | setInputWindowsLocked(handles, displayId); |
| 6574 | } |
| 6575 | } |
| 6576 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6577 | mLooper->wake(); |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6578 | } |
| 6579 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6580 | bool InputDispatcher::shouldDropInput( |
| 6581 | const EventEntry& entry, const sp<android::gui::WindowInfoHandle>& windowHandle) const { |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6582 | if (windowHandle->getInfo()->inputConfig.test(WindowInfo::InputConfig::DROP_INPUT) || |
| 6583 | (windowHandle->getInfo()->inputConfig.test( |
| 6584 | WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED) && |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6585 | isWindowObscuredLocked(windowHandle))) { |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6586 | ALOGW("Dropping %s event targeting %s as requested by the input configuration {%s} on " |
| 6587 | "display %" PRId32 ".", |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6588 | ftl::enum_string(entry.type).c_str(), windowHandle->getName().c_str(), |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6589 | windowHandle->getInfo()->inputConfig.string().c_str(), |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6590 | windowHandle->getInfo()->displayId); |
| 6591 | return true; |
| 6592 | } |
| 6593 | return false; |
| 6594 | } |
| 6595 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 6596 | void InputDispatcher::DispatcherWindowListener::onWindowInfosChanged( |
| 6597 | const std::vector<gui::WindowInfo>& windowInfos, |
| 6598 | const std::vector<DisplayInfo>& displayInfos) { |
| 6599 | mDispatcher.onWindowInfosChanged(windowInfos, displayInfos); |
| 6600 | } |
| 6601 | |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6602 | void InputDispatcher::cancelCurrentTouch() { |
| 6603 | { |
| 6604 | std::scoped_lock _l(mLock); |
| 6605 | ALOGD("Canceling all ongoing pointer gestures on all displays."); |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 6606 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6607 | "cancel current touch"); |
| 6608 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 6609 | |
| 6610 | mTouchStatesByDisplay.clear(); |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6611 | } |
| 6612 | // Wake up poll loop since there might be work to do. |
| 6613 | mLooper->wake(); |
| 6614 | } |
| 6615 | |
Prabir Pradhan | 87112a7 | 2023-04-20 19:13:39 +0000 | [diff] [blame] | 6616 | void InputDispatcher::requestRefreshConfiguration() { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame^] | 6617 | InputDispatcherConfiguration config = mPolicy.getDispatcherConfiguration(); |
Prabir Pradhan | 87112a7 | 2023-04-20 19:13:39 +0000 | [diff] [blame] | 6618 | |
| 6619 | std::scoped_lock _l(mLock); |
| 6620 | mConfig = config; |
| 6621 | } |
| 6622 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6623 | void InputDispatcher::setMonitorDispatchingTimeoutForTest(std::chrono::nanoseconds timeout) { |
| 6624 | std::scoped_lock _l(mLock); |
| 6625 | mMonitorDispatchingTimeout = timeout; |
| 6626 | } |
| 6627 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6628 | void InputDispatcher::slipWallpaperTouch(ftl::Flags<InputTarget::Flags> targetFlags, |
| 6629 | const sp<WindowInfoHandle>& oldWindowHandle, |
| 6630 | const sp<WindowInfoHandle>& newWindowHandle, |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 6631 | TouchState& state, int32_t pointerId, |
| 6632 | std::vector<InputTarget>& targets) { |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 6633 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
| 6634 | pointerIds.set(pointerId); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6635 | const bool oldHasWallpaper = oldWindowHandle->getInfo()->inputConfig.test( |
| 6636 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER); |
| 6637 | const bool newHasWallpaper = targetFlags.test(InputTarget::Flags::FOREGROUND) && |
| 6638 | newWindowHandle->getInfo()->inputConfig.test( |
| 6639 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER); |
| 6640 | const sp<WindowInfoHandle> oldWallpaper = |
| 6641 | oldHasWallpaper ? state.getWallpaperWindow() : nullptr; |
| 6642 | const sp<WindowInfoHandle> newWallpaper = |
| 6643 | newHasWallpaper ? findWallpaperWindowBelow(newWindowHandle) : nullptr; |
| 6644 | if (oldWallpaper == newWallpaper) { |
| 6645 | return; |
| 6646 | } |
| 6647 | |
| 6648 | if (oldWallpaper != nullptr) { |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 6649 | const TouchedWindow& oldTouchedWindow = state.getTouchedWindow(oldWallpaper); |
| 6650 | addWindowTargetLocked(oldWallpaper, |
| 6651 | oldTouchedWindow.targetFlags | |
| 6652 | InputTarget::Flags::DISPATCH_AS_SLIPPERY_EXIT, |
| 6653 | pointerIds, oldTouchedWindow.firstDownTimeInTarget, targets); |
| 6654 | state.removeTouchedPointerFromWindow(pointerId, oldWallpaper); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6655 | } |
| 6656 | |
| 6657 | if (newWallpaper != nullptr) { |
| 6658 | state.addOrUpdateWindow(newWallpaper, |
| 6659 | InputTarget::Flags::DISPATCH_AS_SLIPPERY_ENTER | |
| 6660 | InputTarget::Flags::WINDOW_IS_OBSCURED | |
| 6661 | InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED, |
| 6662 | pointerIds); |
| 6663 | } |
| 6664 | } |
| 6665 | |
| 6666 | void InputDispatcher::transferWallpaperTouch(ftl::Flags<InputTarget::Flags> oldTargetFlags, |
| 6667 | ftl::Flags<InputTarget::Flags> newTargetFlags, |
| 6668 | const sp<WindowInfoHandle> fromWindowHandle, |
| 6669 | const sp<WindowInfoHandle> toWindowHandle, |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 6670 | TouchState& state, |
| 6671 | std::bitset<MAX_POINTER_ID + 1> pointerIds) { |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6672 | const bool oldHasWallpaper = oldTargetFlags.test(InputTarget::Flags::FOREGROUND) && |
| 6673 | fromWindowHandle->getInfo()->inputConfig.test( |
| 6674 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER); |
| 6675 | const bool newHasWallpaper = newTargetFlags.test(InputTarget::Flags::FOREGROUND) && |
| 6676 | toWindowHandle->getInfo()->inputConfig.test( |
| 6677 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER); |
| 6678 | |
| 6679 | const sp<WindowInfoHandle> oldWallpaper = |
| 6680 | oldHasWallpaper ? state.getWallpaperWindow() : nullptr; |
| 6681 | const sp<WindowInfoHandle> newWallpaper = |
| 6682 | newHasWallpaper ? findWallpaperWindowBelow(toWindowHandle) : nullptr; |
| 6683 | if (oldWallpaper == newWallpaper) { |
| 6684 | return; |
| 6685 | } |
| 6686 | |
| 6687 | if (oldWallpaper != nullptr) { |
| 6688 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
| 6689 | "transferring touch focus to another window"); |
| 6690 | state.removeWindowByToken(oldWallpaper->getToken()); |
| 6691 | synthesizeCancelationEventsForWindowLocked(oldWallpaper, options); |
| 6692 | } |
| 6693 | |
| 6694 | if (newWallpaper != nullptr) { |
| 6695 | nsecs_t downTimeInTarget = now(); |
| 6696 | ftl::Flags<InputTarget::Flags> wallpaperFlags = |
| 6697 | oldTargetFlags & (InputTarget::Flags::SPLIT | InputTarget::Flags::DISPATCH_AS_IS); |
| 6698 | wallpaperFlags |= InputTarget::Flags::WINDOW_IS_OBSCURED | |
| 6699 | InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED; |
| 6700 | state.addOrUpdateWindow(newWallpaper, wallpaperFlags, pointerIds, downTimeInTarget); |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6701 | std::shared_ptr<Connection> wallpaperConnection = |
| 6702 | getConnectionLocked(newWallpaper->getToken()); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6703 | if (wallpaperConnection != nullptr) { |
Siarhei Vishniakou | 1069fa8 | 2023-04-19 12:14:39 -0700 | [diff] [blame] | 6704 | std::shared_ptr<Connection> toConnection = |
| 6705 | getConnectionLocked(toWindowHandle->getToken()); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6706 | toConnection->inputState.mergePointerStateTo(wallpaperConnection->inputState); |
| 6707 | synthesizePointerDownEventsForConnectionLocked(downTimeInTarget, wallpaperConnection, |
| 6708 | wallpaperFlags); |
| 6709 | } |
| 6710 | } |
| 6711 | } |
| 6712 | |
| 6713 | sp<WindowInfoHandle> InputDispatcher::findWallpaperWindowBelow( |
| 6714 | const sp<WindowInfoHandle>& windowHandle) const { |
| 6715 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
| 6716 | getWindowHandlesLocked(windowHandle->getInfo()->displayId); |
| 6717 | bool foundWindow = false; |
| 6718 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
| 6719 | if (!foundWindow && otherHandle != windowHandle) { |
| 6720 | continue; |
| 6721 | } |
| 6722 | if (windowHandle == otherHandle) { |
| 6723 | foundWindow = true; |
| 6724 | continue; |
| 6725 | } |
| 6726 | |
| 6727 | if (otherHandle->getInfo()->inputConfig.test(WindowInfo::InputConfig::IS_WALLPAPER)) { |
| 6728 | return otherHandle; |
| 6729 | } |
| 6730 | } |
| 6731 | return nullptr; |
| 6732 | } |
| 6733 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 6734 | } // namespace android::inputdispatcher |