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> |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 34 | #include <powermanager/PowerManager.h> |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 35 | #include <unistd.h> |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 36 | #include <utils/Trace.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 37 | |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 38 | #include <cerrno> |
| 39 | #include <cinttypes> |
| 40 | #include <climits> |
| 41 | #include <cstddef> |
| 42 | #include <ctime> |
| 43 | #include <queue> |
| 44 | #include <sstream> |
| 45 | |
| 46 | #include "Connection.h" |
Arthur Hung | 1a1007b | 2022-05-11 07:15:01 +0000 | [diff] [blame] | 47 | #include "DebugConfig.h" |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 48 | #include "InputDispatcher.h" |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 49 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 50 | #define INDENT " " |
| 51 | #define INDENT2 " " |
| 52 | #define INDENT3 " " |
| 53 | #define INDENT4 " " |
| 54 | |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 55 | using namespace android::ftl::flag_operators; |
Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 56 | using android::base::HwTimeoutMultiplier; |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 57 | using android::base::Result; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 58 | using android::base::StringPrintf; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 59 | using android::gui::DisplayInfo; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 60 | using android::gui::FocusRequest; |
| 61 | using android::gui::TouchOcclusionMode; |
| 62 | using android::gui::WindowInfo; |
| 63 | using android::gui::WindowInfoHandle; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 64 | using android::os::InputEventInjectionResult; |
| 65 | using android::os::InputEventInjectionSync; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 66 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 67 | namespace android::inputdispatcher { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 68 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 69 | namespace { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 70 | // Temporarily releases a held mutex for the lifetime of the instance. |
| 71 | // Named to match std::scoped_lock |
| 72 | class scoped_unlock { |
| 73 | public: |
| 74 | explicit scoped_unlock(std::mutex& mutex) : mMutex(mutex) { mMutex.unlock(); } |
| 75 | ~scoped_unlock() { mMutex.lock(); } |
| 76 | |
| 77 | private: |
| 78 | std::mutex& mMutex; |
| 79 | }; |
| 80 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 81 | // Default input dispatching timeout if there is no focused application or paused window |
| 82 | // from which to determine an appropriate dispatching timeout. |
Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 83 | const std::chrono::duration DEFAULT_INPUT_DISPATCHING_TIMEOUT = std::chrono::milliseconds( |
| 84 | android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS * |
| 85 | HwTimeoutMultiplier()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 86 | |
| 87 | // Amount of time to allow for all pending events to be processed when an app switch |
| 88 | // key is on the way. This is used to preempt input dispatch and drop input events |
| 89 | // 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] | 90 | constexpr nsecs_t APP_SWITCH_TIMEOUT = 500 * 1000000LL; // 0.5sec |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 91 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 92 | const std::chrono::duration STALE_EVENT_TIMEOUT = std::chrono::seconds(10) * HwTimeoutMultiplier(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 93 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 94 | // 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] | 95 | constexpr nsecs_t SLOW_EVENT_PROCESSING_WARNING_TIMEOUT = 2000 * 1000000LL; // 2sec |
| 96 | |
| 97 | // Log a warning when an interception call takes longer than this to process. |
| 98 | constexpr std::chrono::milliseconds SLOW_INTERCEPTION_THRESHOLD = 50ms; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 99 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 100 | // Additional key latency in case a connection is still processing some motion events. |
| 101 | // This will help with the case when a user touched a button that opens a new window, |
| 102 | // and gives us the chance to dispatch the key to this new window. |
| 103 | constexpr std::chrono::nanoseconds KEY_WAITING_FOR_EVENTS_TIMEOUT = 500ms; |
| 104 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 105 | // Number of recent events to keep for debugging purposes. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 106 | constexpr size_t RECENT_QUEUE_MAX_SIZE = 10; |
| 107 | |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 108 | // Event log tags. See EventLogTags.logtags for reference. |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 109 | constexpr int LOGTAG_INPUT_INTERACTION = 62000; |
| 110 | constexpr int LOGTAG_INPUT_FOCUS = 62001; |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 111 | constexpr int LOGTAG_INPUT_CANCEL = 62003; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 112 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 113 | inline nsecs_t now() { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 114 | return systemTime(SYSTEM_TIME_MONOTONIC); |
| 115 | } |
| 116 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 117 | inline const char* toString(bool value) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 118 | return value ? "true" : "false"; |
| 119 | } |
| 120 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 121 | inline const std::string toString(const sp<IBinder>& binder) { |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 122 | if (binder == nullptr) { |
| 123 | return "<null>"; |
| 124 | } |
| 125 | return StringPrintf("%p", binder.get()); |
| 126 | } |
| 127 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 128 | inline int32_t getMotionEventActionPointerIndex(int32_t action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 129 | return (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> |
| 130 | AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 133 | bool isValidKeyAction(int32_t action) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 134 | switch (action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 135 | case AKEY_EVENT_ACTION_DOWN: |
| 136 | case AKEY_EVENT_ACTION_UP: |
| 137 | return true; |
| 138 | default: |
| 139 | return false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 143 | bool validateKeyEvent(int32_t action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 144 | if (!isValidKeyAction(action)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 145 | ALOGE("Key event has invalid action code 0x%x", action); |
| 146 | return false; |
| 147 | } |
| 148 | return true; |
| 149 | } |
| 150 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 151 | bool isValidMotionAction(int32_t action, int32_t actionButton, int32_t pointerCount) { |
Siarhei Vishniakou | 2f61bdc | 2022-12-02 08:55:51 -0800 | [diff] [blame] | 152 | switch (MotionEvent::getActionMasked(action)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 153 | case AMOTION_EVENT_ACTION_DOWN: |
| 154 | case AMOTION_EVENT_ACTION_UP: |
Siarhei Vishniakou | 2f61bdc | 2022-12-02 08:55:51 -0800 | [diff] [blame] | 155 | return pointerCount == 1; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 156 | case AMOTION_EVENT_ACTION_MOVE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 157 | case AMOTION_EVENT_ACTION_HOVER_ENTER: |
| 158 | case AMOTION_EVENT_ACTION_HOVER_MOVE: |
| 159 | case AMOTION_EVENT_ACTION_HOVER_EXIT: |
Siarhei Vishniakou | 2f61bdc | 2022-12-02 08:55:51 -0800 | [diff] [blame] | 160 | return pointerCount >= 1; |
| 161 | case AMOTION_EVENT_ACTION_CANCEL: |
| 162 | case AMOTION_EVENT_ACTION_OUTSIDE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 163 | case AMOTION_EVENT_ACTION_SCROLL: |
| 164 | return true; |
| 165 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 166 | case AMOTION_EVENT_ACTION_POINTER_UP: { |
Siarhei Vishniakou | 2f61bdc | 2022-12-02 08:55:51 -0800 | [diff] [blame] | 167 | const int32_t index = MotionEvent::getActionIndex(action); |
| 168 | return index >= 0 && index < pointerCount && pointerCount > 1; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 169 | } |
| 170 | case AMOTION_EVENT_ACTION_BUTTON_PRESS: |
| 171 | case AMOTION_EVENT_ACTION_BUTTON_RELEASE: |
| 172 | return actionButton != 0; |
| 173 | default: |
| 174 | return false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 178 | int64_t millis(std::chrono::nanoseconds t) { |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 179 | return std::chrono::duration_cast<std::chrono::milliseconds>(t).count(); |
| 180 | } |
| 181 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 182 | bool validateMotionEvent(int32_t action, int32_t actionButton, size_t pointerCount, |
| 183 | const PointerProperties* pointerProperties) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 184 | if (!isValidMotionAction(action, actionButton, pointerCount)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 185 | ALOGE("Motion event has invalid action code 0x%x", action); |
| 186 | return false; |
| 187 | } |
| 188 | if (pointerCount < 1 || pointerCount > MAX_POINTERS) { |
Siarhei Vishniakou | 0174738 | 2022-01-20 13:23:27 -0800 | [diff] [blame] | 189 | 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] | 190 | pointerCount, MAX_POINTERS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 191 | return false; |
| 192 | } |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 193 | std::bitset<MAX_POINTER_ID + 1> pointerIdBits; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 194 | for (size_t i = 0; i < pointerCount; i++) { |
| 195 | int32_t id = pointerProperties[i].id; |
| 196 | if (id < 0 || id > MAX_POINTER_ID) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 197 | ALOGE("Motion event has invalid pointer id %d; value must be between 0 and %d", id, |
| 198 | MAX_POINTER_ID); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 199 | return false; |
| 200 | } |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 201 | if (pointerIdBits.test(id)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 202 | ALOGE("Motion event has duplicate pointer id %d", id); |
| 203 | return false; |
| 204 | } |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 205 | pointerIdBits.set(id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 206 | } |
| 207 | return true; |
| 208 | } |
| 209 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 210 | std::string dumpRegion(const Region& region) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 211 | if (region.isEmpty()) { |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 212 | return "<empty>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 213 | } |
| 214 | |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 215 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 216 | bool first = true; |
| 217 | Region::const_iterator cur = region.begin(); |
| 218 | Region::const_iterator const tail = region.end(); |
| 219 | while (cur != tail) { |
| 220 | if (first) { |
| 221 | first = false; |
| 222 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 223 | dump += "|"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 224 | } |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 225 | 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] | 226 | cur++; |
| 227 | } |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 228 | return dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 229 | } |
| 230 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 231 | std::string dumpQueue(const std::deque<DispatchEntry*>& queue, nsecs_t currentTime) { |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 232 | constexpr size_t maxEntries = 50; // max events to print |
| 233 | constexpr size_t skipBegin = maxEntries / 2; |
| 234 | const size_t skipEnd = queue.size() - maxEntries / 2; |
| 235 | // skip from maxEntries / 2 ... size() - maxEntries/2 |
| 236 | // only print from 0 .. skipBegin and then from skipEnd .. size() |
| 237 | |
| 238 | std::string dump; |
| 239 | for (size_t i = 0; i < queue.size(); i++) { |
| 240 | const DispatchEntry& entry = *queue[i]; |
| 241 | if (i >= skipBegin && i < skipEnd) { |
| 242 | dump += StringPrintf(INDENT4 "<skipped %zu entries>\n", skipEnd - skipBegin); |
| 243 | i = skipEnd - 1; // it will be incremented to "skipEnd" by 'continue' |
| 244 | continue; |
| 245 | } |
| 246 | dump.append(INDENT4); |
| 247 | dump += entry.eventEntry->getDescription(); |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 248 | dump += StringPrintf(", seq=%" PRIu32 ", targetFlags=%s, resolvedAction=%d, age=%" PRId64 |
| 249 | "ms", |
| 250 | entry.seq, entry.targetFlags.string().c_str(), entry.resolvedAction, |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 251 | ns2ms(currentTime - entry.eventEntry->eventTime)); |
| 252 | if (entry.deliveryTime != 0) { |
| 253 | // This entry was delivered, so add information on how long we've been waiting |
| 254 | dump += StringPrintf(", wait=%" PRId64 "ms", ns2ms(currentTime - entry.deliveryTime)); |
| 255 | } |
| 256 | dump.append("\n"); |
| 257 | } |
| 258 | return dump; |
| 259 | } |
| 260 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 261 | /** |
| 262 | * Find the entry in std::unordered_map by key, and return it. |
| 263 | * If the entry is not found, return a default constructed entry. |
| 264 | * |
| 265 | * Useful when the entries are vectors, since an empty vector will be returned |
| 266 | * if the entry is not found. |
| 267 | * Also useful when the entries are sp<>. If an entry is not found, nullptr is returned. |
| 268 | */ |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 269 | template <typename K, typename V> |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 270 | V getValueByKey(const std::unordered_map<K, V>& map, K key) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 271 | auto it = map.find(key); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 272 | return it != map.end() ? it->second : V{}; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 273 | } |
| 274 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 275 | bool haveSameToken(const sp<WindowInfoHandle>& first, const sp<WindowInfoHandle>& second) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 276 | if (first == second) { |
| 277 | return true; |
| 278 | } |
| 279 | |
| 280 | if (first == nullptr || second == nullptr) { |
| 281 | return false; |
| 282 | } |
| 283 | |
| 284 | return first->getToken() == second->getToken(); |
| 285 | } |
| 286 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 287 | bool haveSameApplicationToken(const WindowInfo* first, const WindowInfo* second) { |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 288 | if (first == nullptr || second == nullptr) { |
| 289 | return false; |
| 290 | } |
| 291 | return first->applicationInfo.token != nullptr && |
| 292 | first->applicationInfo.token == second->applicationInfo.token; |
| 293 | } |
| 294 | |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 295 | template <typename T> |
| 296 | size_t firstMarkedBit(T set) { |
| 297 | // TODO: replace with std::countr_zero from <bit> when that's available |
| 298 | LOG_ALWAYS_FATAL_IF(set.none()); |
| 299 | size_t i = 0; |
| 300 | while (!set.test(i)) { |
| 301 | i++; |
| 302 | } |
| 303 | return i; |
| 304 | } |
| 305 | |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 306 | std::unique_ptr<DispatchEntry> createDispatchEntry( |
| 307 | const InputTarget& inputTarget, std::shared_ptr<EventEntry> eventEntry, |
| 308 | ftl::Flags<InputTarget::Flags> inputTargetFlags) { |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 309 | if (inputTarget.useDefaultPointerTransform()) { |
| 310 | const ui::Transform& transform = inputTarget.getDefaultPointerTransform(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 311 | return std::make_unique<DispatchEntry>(eventEntry, inputTargetFlags, transform, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 312 | inputTarget.displayTransform, |
| 313 | inputTarget.globalScaleFactor); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | ALOG_ASSERT(eventEntry->type == EventEntry::Type::MOTION); |
| 317 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); |
| 318 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 319 | std::vector<PointerCoords> pointerCoords; |
| 320 | pointerCoords.resize(motionEntry.pointerCount); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 321 | |
| 322 | // Use the first pointer information to normalize all other pointers. This could be any pointer |
| 323 | // 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] | 324 | // uses the transform for the normalized pointer. |
| 325 | const ui::Transform& firstPointerTransform = |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 326 | inputTarget.pointerTransforms[firstMarkedBit(inputTarget.pointerIds)]; |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 327 | ui::Transform inverseFirstTransform = firstPointerTransform.inverse(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 328 | |
| 329 | // Iterate through all pointers in the event to normalize against the first. |
| 330 | for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.pointerCount; pointerIndex++) { |
| 331 | const PointerProperties& pointerProperties = motionEntry.pointerProperties[pointerIndex]; |
| 332 | uint32_t pointerId = uint32_t(pointerProperties.id); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 333 | const ui::Transform& currTransform = inputTarget.pointerTransforms[pointerId]; |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 334 | |
| 335 | pointerCoords[pointerIndex].copyFrom(motionEntry.pointerCoords[pointerIndex]); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 336 | // First, apply the current pointer's transform to update the coordinates into |
| 337 | // window space. |
| 338 | pointerCoords[pointerIndex].transform(currTransform); |
| 339 | // Next, apply the inverse transform of the normalized coordinates so the |
| 340 | // current coordinates are transformed into the normalized coordinate space. |
| 341 | pointerCoords[pointerIndex].transform(inverseFirstTransform); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 344 | std::unique_ptr<MotionEntry> combinedMotionEntry = |
| 345 | std::make_unique<MotionEntry>(motionEntry.id, motionEntry.eventTime, |
| 346 | motionEntry.deviceId, motionEntry.source, |
| 347 | motionEntry.displayId, motionEntry.policyFlags, |
| 348 | motionEntry.action, motionEntry.actionButton, |
| 349 | motionEntry.flags, motionEntry.metaState, |
| 350 | motionEntry.buttonState, motionEntry.classification, |
| 351 | motionEntry.edgeFlags, motionEntry.xPrecision, |
| 352 | motionEntry.yPrecision, motionEntry.xCursorPosition, |
| 353 | motionEntry.yCursorPosition, motionEntry.downTime, |
| 354 | motionEntry.pointerCount, motionEntry.pointerProperties, |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 355 | pointerCoords.data()); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 356 | |
| 357 | if (motionEntry.injectionState) { |
| 358 | combinedMotionEntry->injectionState = motionEntry.injectionState; |
| 359 | combinedMotionEntry->injectionState->refCount += 1; |
| 360 | } |
| 361 | |
| 362 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 363 | std::make_unique<DispatchEntry>(std::move(combinedMotionEntry), inputTargetFlags, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 364 | firstPointerTransform, inputTarget.displayTransform, |
| 365 | inputTarget.globalScaleFactor); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 366 | return dispatchEntry; |
| 367 | } |
| 368 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 369 | status_t openInputChannelPair(const std::string& name, std::shared_ptr<InputChannel>& serverChannel, |
| 370 | std::unique_ptr<InputChannel>& clientChannel) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 371 | std::unique_ptr<InputChannel> uniqueServerChannel; |
| 372 | status_t result = InputChannel::openInputChannelPair(name, uniqueServerChannel, clientChannel); |
| 373 | |
| 374 | serverChannel = std::move(uniqueServerChannel); |
| 375 | return result; |
| 376 | } |
| 377 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 378 | template <typename T> |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 379 | 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] | 380 | if (lhs == nullptr && rhs == nullptr) { |
| 381 | return true; |
| 382 | } |
| 383 | if (lhs == nullptr || rhs == nullptr) { |
| 384 | return false; |
| 385 | } |
| 386 | return *lhs == *rhs; |
| 387 | } |
| 388 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 389 | KeyEvent createKeyEvent(const KeyEntry& entry) { |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 390 | KeyEvent event; |
| 391 | event.initialize(entry.id, entry.deviceId, entry.source, entry.displayId, INVALID_HMAC, |
| 392 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, |
| 393 | entry.repeatCount, entry.downTime, entry.eventTime); |
| 394 | return event; |
| 395 | } |
| 396 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 397 | bool shouldReportMetricsForConnection(const Connection& connection) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 398 | // Do not keep track of gesture monitors. They receive every event and would disproportionately |
| 399 | // affect the statistics. |
| 400 | if (connection.monitor) { |
| 401 | return false; |
| 402 | } |
| 403 | // If the connection is experiencing ANR, let's skip it. We have separate ANR metrics |
| 404 | if (!connection.responsive) { |
| 405 | return false; |
| 406 | } |
| 407 | return true; |
| 408 | } |
| 409 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 410 | bool shouldReportFinishedEvent(const DispatchEntry& dispatchEntry, const Connection& connection) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 411 | const EventEntry& eventEntry = *dispatchEntry.eventEntry; |
| 412 | const int32_t& inputEventId = eventEntry.id; |
| 413 | if (inputEventId != dispatchEntry.resolvedEventId) { |
| 414 | // Event was transmuted |
| 415 | return false; |
| 416 | } |
| 417 | if (inputEventId == android::os::IInputConstants::INVALID_INPUT_EVENT_ID) { |
| 418 | return false; |
| 419 | } |
| 420 | // Only track latency for events that originated from hardware |
| 421 | if (eventEntry.isSynthesized()) { |
| 422 | return false; |
| 423 | } |
| 424 | const EventEntry::Type& inputEventEntryType = eventEntry.type; |
| 425 | if (inputEventEntryType == EventEntry::Type::KEY) { |
| 426 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 427 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
| 428 | return false; |
| 429 | } |
| 430 | } else if (inputEventEntryType == EventEntry::Type::MOTION) { |
| 431 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 432 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL || |
| 433 | motionEntry.action == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 434 | return false; |
| 435 | } |
| 436 | } else { |
| 437 | // Not a key or a motion |
| 438 | return false; |
| 439 | } |
| 440 | if (!shouldReportMetricsForConnection(connection)) { |
| 441 | return false; |
| 442 | } |
| 443 | return true; |
| 444 | } |
| 445 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 446 | /** |
| 447 | * Connection is responsive if it has no events in the waitQueue that are older than the |
| 448 | * current time. |
| 449 | */ |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 450 | bool isConnectionResponsive(const Connection& connection) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 451 | const nsecs_t currentTime = now(); |
| 452 | for (const DispatchEntry* entry : connection.waitQueue) { |
| 453 | if (entry->timeoutTime < currentTime) { |
| 454 | return false; |
| 455 | } |
| 456 | } |
| 457 | return true; |
| 458 | } |
| 459 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 460 | // Returns true if the event type passed as argument represents a user activity. |
| 461 | bool isUserActivityEvent(const EventEntry& eventEntry) { |
| 462 | switch (eventEntry.type) { |
| 463 | case EventEntry::Type::FOCUS: |
| 464 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 465 | case EventEntry::Type::DRAG: |
| 466 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
| 467 | case EventEntry::Type::SENSOR: |
| 468 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 469 | return false; |
| 470 | case EventEntry::Type::DEVICE_RESET: |
| 471 | case EventEntry::Type::KEY: |
| 472 | case EventEntry::Type::MOTION: |
| 473 | return true; |
| 474 | } |
| 475 | } |
| 476 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 477 | // Returns true if the given window can accept pointer events at the given display location. |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 478 | bool windowAcceptsTouchAt(const WindowInfo& windowInfo, int32_t displayId, int32_t x, int32_t y, |
| 479 | bool isStylus) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 480 | const auto inputConfig = windowInfo.inputConfig; |
| 481 | if (windowInfo.displayId != displayId || |
| 482 | inputConfig.test(WindowInfo::InputConfig::NOT_VISIBLE)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 483 | return false; |
| 484 | } |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 485 | const bool windowCanInterceptTouch = isStylus && windowInfo.interceptsStylus(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 486 | if (inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE) && !windowCanInterceptTouch) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 487 | return false; |
| 488 | } |
Prabir Pradhan | 0634904 | 2022-02-04 09:19:17 -0800 | [diff] [blame] | 489 | if (!windowInfo.touchableRegionContainsPoint(x, y)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 490 | return false; |
| 491 | } |
| 492 | return true; |
| 493 | } |
| 494 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 495 | bool isPointerFromStylus(const MotionEntry& entry, int32_t pointerIndex) { |
| 496 | return isFromSource(entry.source, AINPUT_SOURCE_STYLUS) && |
Prabir Pradhan | e562696 | 2022-10-27 20:30:53 +0000 | [diff] [blame] | 497 | isStylusToolType(entry.pointerProperties[pointerIndex].toolType); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 498 | } |
| 499 | |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 500 | // Determines if the given window can be targeted as InputTarget::Flags::FOREGROUND. |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 501 | // Foreground events are only sent to "foreground targetable" windows, but not all gestures sent to |
| 502 | // such window are necessarily targeted with the flag. For example, an event with ACTION_OUTSIDE can |
| 503 | // 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] | 504 | // InputTarget::Flags::FOREGROUND. |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 505 | bool canReceiveForegroundTouches(const WindowInfo& info) { |
| 506 | // A non-touchable window can still receive touch events (e.g. in the case of |
| 507 | // STYLUS_INTERCEPTOR), so prevent such windows from receiving foreground events for touches. |
| 508 | return !info.inputConfig.test(gui::WindowInfo::InputConfig::NOT_TOUCHABLE) && !info.isSpy(); |
| 509 | } |
| 510 | |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 511 | bool isWindowOwnedBy(const sp<WindowInfoHandle>& windowHandle, int32_t pid, int32_t uid) { |
| 512 | if (windowHandle == nullptr) { |
| 513 | return false; |
| 514 | } |
| 515 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 516 | if (pid == windowInfo->ownerPid && uid == windowInfo->ownerUid) { |
| 517 | return true; |
| 518 | } |
| 519 | return false; |
| 520 | } |
| 521 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 522 | // Checks targeted injection using the window's owner's uid. |
| 523 | // Returns an empty string if an entry can be sent to the given window, or an error message if the |
| 524 | // entry is a targeted injection whose uid target doesn't match the window owner. |
| 525 | std::optional<std::string> verifyTargetedInjection(const sp<WindowInfoHandle>& window, |
| 526 | const EventEntry& entry) { |
| 527 | if (entry.injectionState == nullptr || !entry.injectionState->targetUid) { |
| 528 | // The event was not injected, or the injected event does not target a window. |
| 529 | return {}; |
| 530 | } |
| 531 | const int32_t uid = *entry.injectionState->targetUid; |
| 532 | if (window == nullptr) { |
| 533 | return StringPrintf("No valid window target for injection into uid %d.", uid); |
| 534 | } |
| 535 | if (entry.injectionState->targetUid != window->getInfo()->ownerUid) { |
| 536 | return StringPrintf("Injected event targeted at uid %d would be dispatched to window '%s' " |
| 537 | "owned by uid %d.", |
| 538 | uid, window->getName().c_str(), window->getInfo()->ownerUid); |
| 539 | } |
| 540 | return {}; |
| 541 | } |
| 542 | |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 543 | Point resolveTouchedPosition(const MotionEntry& entry) { |
| 544 | const bool isFromMouse = isFromSource(entry.source, AINPUT_SOURCE_MOUSE); |
| 545 | // Always dispatch mouse events to cursor position. |
| 546 | if (isFromMouse) { |
| 547 | return Point(static_cast<int32_t>(entry.xCursorPosition), |
| 548 | static_cast<int32_t>(entry.yCursorPosition)); |
| 549 | } |
| 550 | |
| 551 | const int32_t pointerIndex = getMotionEventActionPointerIndex(entry.action); |
| 552 | return Point(static_cast<int32_t>( |
| 553 | entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_X)), |
| 554 | static_cast<int32_t>( |
| 555 | entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_Y))); |
| 556 | } |
| 557 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 558 | std::optional<nsecs_t> getDownTime(const EventEntry& eventEntry) { |
| 559 | if (eventEntry.type == EventEntry::Type::KEY) { |
| 560 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 561 | return keyEntry.downTime; |
| 562 | } else if (eventEntry.type == EventEntry::Type::MOTION) { |
| 563 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 564 | return motionEntry.downTime; |
| 565 | } |
| 566 | return std::nullopt; |
| 567 | } |
| 568 | |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 569 | /** |
| 570 | * Compare the old touch state to the new touch state, and generate the corresponding touched |
| 571 | * windows (== input targets). |
| 572 | * If a window had the hovering pointer, but now it doesn't, produce HOVER_EXIT for that window. |
| 573 | * If the pointer just entered the new window, produce HOVER_ENTER. |
| 574 | * For pointers remaining in the window, produce HOVER_MOVE. |
| 575 | */ |
| 576 | std::vector<TouchedWindow> getHoveringWindowsLocked(const TouchState* oldState, |
| 577 | const TouchState& newTouchState, |
| 578 | const MotionEntry& entry) { |
| 579 | std::vector<TouchedWindow> out; |
| 580 | const int32_t maskedAction = MotionEvent::getActionMasked(entry.action); |
| 581 | if (maskedAction != AMOTION_EVENT_ACTION_HOVER_ENTER && |
| 582 | maskedAction != AMOTION_EVENT_ACTION_HOVER_MOVE && |
| 583 | maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 584 | // Not a hover event - don't need to do anything |
| 585 | return out; |
| 586 | } |
| 587 | |
| 588 | // We should consider all hovering pointers here. But for now, just use the first one |
| 589 | const int32_t pointerId = entry.pointerProperties[0].id; |
| 590 | |
| 591 | std::set<sp<WindowInfoHandle>> oldWindows; |
| 592 | if (oldState != nullptr) { |
| 593 | oldWindows = oldState->getWindowsWithHoveringPointer(entry.deviceId, pointerId); |
| 594 | } |
| 595 | |
| 596 | std::set<sp<WindowInfoHandle>> newWindows = |
| 597 | newTouchState.getWindowsWithHoveringPointer(entry.deviceId, pointerId); |
| 598 | |
| 599 | // If the pointer is no longer in the new window set, send HOVER_EXIT. |
| 600 | for (const sp<WindowInfoHandle>& oldWindow : oldWindows) { |
| 601 | if (newWindows.find(oldWindow) == newWindows.end()) { |
| 602 | TouchedWindow touchedWindow; |
| 603 | touchedWindow.windowHandle = oldWindow; |
| 604 | touchedWindow.targetFlags = InputTarget::Flags::DISPATCH_AS_HOVER_EXIT; |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 605 | touchedWindow.pointerIds.set(pointerId); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 606 | out.push_back(touchedWindow); |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | for (const sp<WindowInfoHandle>& newWindow : newWindows) { |
| 611 | TouchedWindow touchedWindow; |
| 612 | touchedWindow.windowHandle = newWindow; |
| 613 | if (oldWindows.find(newWindow) == oldWindows.end()) { |
| 614 | // Any windows that have this pointer now, and didn't have it before, should get |
| 615 | // HOVER_ENTER |
| 616 | touchedWindow.targetFlags = InputTarget::Flags::DISPATCH_AS_HOVER_ENTER; |
| 617 | } else { |
| 618 | // This pointer was already sent to the window. Use ACTION_HOVER_MOVE. |
| 619 | LOG_ALWAYS_FATAL_IF(maskedAction != AMOTION_EVENT_ACTION_HOVER_MOVE); |
| 620 | touchedWindow.targetFlags = InputTarget::Flags::DISPATCH_AS_IS; |
| 621 | } |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 622 | touchedWindow.pointerIds.set(pointerId); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 623 | if (canReceiveForegroundTouches(*newWindow->getInfo())) { |
| 624 | touchedWindow.targetFlags |= InputTarget::Flags::FOREGROUND; |
| 625 | } |
| 626 | out.push_back(touchedWindow); |
| 627 | } |
| 628 | return out; |
| 629 | } |
| 630 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 631 | template <typename T> |
| 632 | std::vector<T>& operator+=(std::vector<T>& left, const std::vector<T>& right) { |
| 633 | left.insert(left.end(), right.begin(), right.end()); |
| 634 | return left; |
| 635 | } |
| 636 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 637 | } // namespace |
| 638 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 639 | // --- InputDispatcher --- |
| 640 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 641 | InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy) |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 642 | : InputDispatcher(policy, STALE_EVENT_TIMEOUT) {} |
| 643 | |
| 644 | InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy, |
| 645 | std::chrono::nanoseconds staleEventTimeout) |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 646 | : mPolicy(policy), |
| 647 | mPendingEvent(nullptr), |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 648 | mLastDropReason(DropReason::NOT_DROPPED), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 649 | mIdGenerator(IdGenerator::Source::INPUT_DISPATCHER), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 650 | mAppSwitchSawKeyDown(false), |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 651 | mAppSwitchDueTime(LLONG_MAX), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 652 | mNextUnblockedEvent(nullptr), |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 653 | mMonitorDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 654 | mDispatchEnabled(false), |
| 655 | mDispatchFrozen(false), |
| 656 | mInputFilterEnabled(false), |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 657 | mMaximumObscuringOpacityForTouch(1.0f), |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 658 | mFocusedDisplayId(ADISPLAY_ID_DEFAULT), |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 659 | mWindowTokenWithPointerCapture(nullptr), |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 660 | mStaleEventTimeout(staleEventTimeout), |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 661 | mLatencyAggregator(), |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 662 | mLatencyTracker(&mLatencyAggregator) { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 663 | mLooper = sp<Looper>::make(false); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 664 | mReporter = createInputReporter(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 665 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 666 | mWindowInfoListener = sp<DispatcherWindowListener>::make(*this); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 667 | #if defined(__ANDROID__) |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 668 | SurfaceComposerClient::getDefault()->addWindowInfosListener(mWindowInfoListener); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 669 | #endif |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 670 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 671 | policy->getDispatcherConfiguration(&mConfig); |
| 672 | } |
| 673 | |
| 674 | InputDispatcher::~InputDispatcher() { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 675 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 676 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 677 | resetKeyRepeatLocked(); |
| 678 | releasePendingEventLocked(); |
| 679 | drainInboundQueueLocked(); |
| 680 | mCommandQueue.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 681 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 682 | while (!mConnectionsByToken.empty()) { |
| 683 | sp<Connection> connection = mConnectionsByToken.begin()->second; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 684 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), /*notify=*/false); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 685 | } |
| 686 | } |
| 687 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 688 | status_t InputDispatcher::start() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 689 | if (mThread) { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 690 | return ALREADY_EXISTS; |
| 691 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 692 | mThread = std::make_unique<InputThread>( |
| 693 | "InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); }); |
| 694 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | status_t InputDispatcher::stop() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 698 | if (mThread && mThread->isCallingThread()) { |
| 699 | ALOGE("InputDispatcher cannot be stopped from its own thread!"); |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 700 | return INVALID_OPERATION; |
| 701 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 702 | mThread.reset(); |
| 703 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 704 | } |
| 705 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 706 | void InputDispatcher::dispatchOnce() { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 707 | nsecs_t nextWakeupTime = LLONG_MAX; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 708 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 709 | std::scoped_lock _l(mLock); |
| 710 | mDispatcherIsAlive.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 711 | |
| 712 | // Run a dispatch loop if there are no pending commands. |
| 713 | // The dispatch loop might enqueue commands to run afterwards. |
| 714 | if (!haveCommandsLocked()) { |
| 715 | dispatchOnceInnerLocked(&nextWakeupTime); |
| 716 | } |
| 717 | |
| 718 | // Run all pending commands if there are any. |
| 719 | // 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] | 720 | if (runCommandsLockedInterruptable()) { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 721 | nextWakeupTime = LLONG_MIN; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 722 | } |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 723 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 724 | // If we are still waiting for ack on some events, |
| 725 | // we might have to wake up earlier to check if an app is anr'ing. |
| 726 | const nsecs_t nextAnrCheck = processAnrsLocked(); |
| 727 | nextWakeupTime = std::min(nextWakeupTime, nextAnrCheck); |
| 728 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 729 | // We are about to enter an infinitely long sleep, because we have no commands or |
| 730 | // pending or queued events |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 731 | if (nextWakeupTime == LLONG_MAX) { |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 732 | mDispatcherEnteredIdle.notify_all(); |
| 733 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 734 | } // release lock |
| 735 | |
| 736 | // Wait for callback or timeout or wake. (make sure we round up, not down) |
| 737 | nsecs_t currentTime = now(); |
| 738 | int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime); |
| 739 | mLooper->pollOnce(timeoutMillis); |
| 740 | } |
| 741 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 742 | /** |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 743 | * Raise ANR if there is no focused window. |
| 744 | * Before the ANR is raised, do a final state check: |
| 745 | * 1. The currently focused application must be the same one we are waiting for. |
| 746 | * 2. Ensure we still don't have a focused window. |
| 747 | */ |
| 748 | void InputDispatcher::processNoFocusedWindowAnrLocked() { |
| 749 | // Check if the application that we are waiting for is still focused. |
| 750 | std::shared_ptr<InputApplicationHandle> focusedApplication = |
| 751 | getValueByKey(mFocusedApplicationHandlesByDisplay, mAwaitedApplicationDisplayId); |
| 752 | if (focusedApplication == nullptr || |
| 753 | focusedApplication->getApplicationToken() != |
| 754 | mAwaitedFocusedApplication->getApplicationToken()) { |
| 755 | // Unexpected because we should have reset the ANR timer when focused application changed |
| 756 | ALOGE("Waited for a focused window, but focused application has already changed to %s", |
| 757 | focusedApplication->getName().c_str()); |
| 758 | return; // The focused application has changed. |
| 759 | } |
| 760 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 761 | const sp<WindowInfoHandle>& focusedWindowHandle = |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 762 | getFocusedWindowHandleLocked(mAwaitedApplicationDisplayId); |
| 763 | if (focusedWindowHandle != nullptr) { |
| 764 | return; // We now have a focused window. No need for ANR. |
| 765 | } |
| 766 | onAnrLocked(mAwaitedFocusedApplication); |
| 767 | } |
| 768 | |
| 769 | /** |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 770 | * Check if any of the connections' wait queues have events that are too old. |
| 771 | * If we waited for events to be ack'ed for more than the window timeout, raise an ANR. |
| 772 | * Return the time at which we should wake up next. |
| 773 | */ |
| 774 | nsecs_t InputDispatcher::processAnrsLocked() { |
| 775 | const nsecs_t currentTime = now(); |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 776 | nsecs_t nextAnrCheck = LLONG_MAX; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 777 | // Check if we are waiting for a focused window to appear. Raise ANR if waited too long |
| 778 | if (mNoFocusedWindowTimeoutTime.has_value() && mAwaitedFocusedApplication != nullptr) { |
| 779 | if (currentTime >= *mNoFocusedWindowTimeoutTime) { |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 780 | processNoFocusedWindowAnrLocked(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 781 | mAwaitedFocusedApplication.reset(); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 782 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 783 | return LLONG_MIN; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 784 | } else { |
Siarhei Vishniakou | 38a6d27 | 2020-10-20 20:29:33 -0500 | [diff] [blame] | 785 | // Keep waiting. We will drop the event when mNoFocusedWindowTimeoutTime comes. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 786 | nextAnrCheck = *mNoFocusedWindowTimeoutTime; |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | // Check if any connection ANRs are due |
| 791 | nextAnrCheck = std::min(nextAnrCheck, mAnrTracker.firstTimeout()); |
| 792 | if (currentTime < nextAnrCheck) { // most likely scenario |
| 793 | return nextAnrCheck; // everything is normal. Let's check again at nextAnrCheck |
| 794 | } |
| 795 | |
| 796 | // If we reached here, we have an unresponsive connection. |
| 797 | sp<Connection> connection = getConnectionLocked(mAnrTracker.firstToken()); |
| 798 | if (connection == nullptr) { |
| 799 | ALOGE("Could not find connection for entry %" PRId64, mAnrTracker.firstTimeout()); |
| 800 | return nextAnrCheck; |
| 801 | } |
| 802 | connection->responsive = false; |
| 803 | // Stop waking up for this unresponsive connection |
| 804 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 805 | onAnrLocked(connection); |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 806 | return LLONG_MIN; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 807 | } |
| 808 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 809 | std::chrono::nanoseconds InputDispatcher::getDispatchingTimeoutLocked( |
| 810 | const sp<Connection>& connection) { |
| 811 | if (connection->monitor) { |
| 812 | return mMonitorDispatchingTimeout; |
| 813 | } |
| 814 | const sp<WindowInfoHandle> window = |
| 815 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 816 | if (window != nullptr) { |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 817 | return window->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 818 | } |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 819 | return DEFAULT_INPUT_DISPATCHING_TIMEOUT; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 820 | } |
| 821 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 822 | void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) { |
| 823 | nsecs_t currentTime = now(); |
| 824 | |
Jeff Brown | dc5992e | 2014-04-11 01:27:26 -0700 | [diff] [blame] | 825 | // Reset the key repeat timer whenever normal dispatch is suspended while the |
| 826 | // device is in a non-interactive state. This is to ensure that we abort a key |
| 827 | // repeat if the device is just coming out of sleep. |
| 828 | if (!mDispatchEnabled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 829 | resetKeyRepeatLocked(); |
| 830 | } |
| 831 | |
| 832 | // If dispatching is frozen, do not process timeouts or try to deliver any new events. |
| 833 | if (mDispatchFrozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 834 | if (DEBUG_FOCUS) { |
| 835 | ALOGD("Dispatch frozen. Waiting some more."); |
| 836 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 837 | return; |
| 838 | } |
| 839 | |
| 840 | // Optimize latency of app switches. |
| 841 | // Essentially we start a short timeout when an app switch key (HOME / ENDCALL) has |
| 842 | // been pressed. When it expires, we preempt dispatch and drop all other pending events. |
| 843 | bool isAppSwitchDue = mAppSwitchDueTime <= currentTime; |
| 844 | if (mAppSwitchDueTime < *nextWakeupTime) { |
| 845 | *nextWakeupTime = mAppSwitchDueTime; |
| 846 | } |
| 847 | |
| 848 | // Ready to start a new event. |
| 849 | // If we don't already have a pending event, go grab one. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 850 | if (!mPendingEvent) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 851 | if (mInboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 852 | if (isAppSwitchDue) { |
| 853 | // The inbound queue is empty so the app switch key we were waiting |
| 854 | // for will never arrive. Stop waiting for it. |
| 855 | resetPendingAppSwitchLocked(false); |
| 856 | isAppSwitchDue = false; |
| 857 | } |
| 858 | |
| 859 | // Synthesize a key repeat if appropriate. |
| 860 | if (mKeyRepeatState.lastKeyEntry) { |
| 861 | if (currentTime >= mKeyRepeatState.nextRepeatTime) { |
| 862 | mPendingEvent = synthesizeKeyRepeatLocked(currentTime); |
| 863 | } else { |
| 864 | if (mKeyRepeatState.nextRepeatTime < *nextWakeupTime) { |
| 865 | *nextWakeupTime = mKeyRepeatState.nextRepeatTime; |
| 866 | } |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | // Nothing to do if there is no pending event. |
| 871 | if (!mPendingEvent) { |
| 872 | return; |
| 873 | } |
| 874 | } else { |
| 875 | // Inbound queue has at least one entry. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 876 | mPendingEvent = mInboundQueue.front(); |
| 877 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 878 | traceInboundQueueLengthLocked(); |
| 879 | } |
| 880 | |
| 881 | // Poke user activity for this event. |
| 882 | if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 883 | pokeUserActivityLocked(*mPendingEvent); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 884 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | // Now we have an event to dispatch. |
| 888 | // 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] | 889 | ALOG_ASSERT(mPendingEvent != nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 890 | bool done = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 891 | DropReason dropReason = DropReason::NOT_DROPPED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 892 | if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 893 | dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 894 | } else if (!mDispatchEnabled) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 895 | dropReason = DropReason::DISABLED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | if (mNextUnblockedEvent == mPendingEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 899 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | switch (mPendingEvent->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 903 | case EventEntry::Type::CONFIGURATION_CHANGED: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 904 | const ConfigurationChangedEntry& typedEntry = |
| 905 | static_cast<const ConfigurationChangedEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 906 | done = dispatchConfigurationChangedLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 907 | dropReason = DropReason::NOT_DROPPED; // configuration changes are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 908 | break; |
| 909 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 910 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 911 | case EventEntry::Type::DEVICE_RESET: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 912 | const DeviceResetEntry& typedEntry = |
| 913 | static_cast<const DeviceResetEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 914 | done = dispatchDeviceResetLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 915 | dropReason = DropReason::NOT_DROPPED; // device resets are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 916 | break; |
| 917 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 918 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 919 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 920 | std::shared_ptr<FocusEntry> typedEntry = |
| 921 | std::static_pointer_cast<FocusEntry>(mPendingEvent); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 922 | dispatchFocusLocked(currentTime, typedEntry); |
| 923 | done = true; |
| 924 | dropReason = DropReason::NOT_DROPPED; // focus events are never dropped |
| 925 | break; |
| 926 | } |
| 927 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 928 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 929 | const auto typedEntry = std::static_pointer_cast<TouchModeEntry>(mPendingEvent); |
| 930 | dispatchTouchModeChangeLocked(currentTime, typedEntry); |
| 931 | done = true; |
| 932 | dropReason = DropReason::NOT_DROPPED; // touch mode events are never dropped |
| 933 | break; |
| 934 | } |
| 935 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 936 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 937 | const auto typedEntry = |
| 938 | std::static_pointer_cast<PointerCaptureChangedEntry>(mPendingEvent); |
| 939 | dispatchPointerCaptureChangedLocked(currentTime, typedEntry, dropReason); |
| 940 | done = true; |
| 941 | break; |
| 942 | } |
| 943 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 944 | case EventEntry::Type::DRAG: { |
| 945 | std::shared_ptr<DragEntry> typedEntry = |
| 946 | std::static_pointer_cast<DragEntry>(mPendingEvent); |
| 947 | dispatchDragLocked(currentTime, typedEntry); |
| 948 | done = true; |
| 949 | break; |
| 950 | } |
| 951 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 952 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 953 | std::shared_ptr<KeyEntry> keyEntry = std::static_pointer_cast<KeyEntry>(mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 954 | if (isAppSwitchDue) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 955 | if (isAppSwitchKeyEvent(*keyEntry)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 956 | resetPendingAppSwitchLocked(true); |
| 957 | isAppSwitchDue = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 958 | } else if (dropReason == DropReason::NOT_DROPPED) { |
| 959 | dropReason = DropReason::APP_SWITCH; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 960 | } |
| 961 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 962 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *keyEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 963 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 964 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 965 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 966 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 967 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 968 | done = dispatchKeyLocked(currentTime, keyEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 969 | break; |
| 970 | } |
| 971 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 972 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 973 | std::shared_ptr<MotionEntry> motionEntry = |
| 974 | std::static_pointer_cast<MotionEntry>(mPendingEvent); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 975 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 976 | dropReason = DropReason::APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 977 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 978 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *motionEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 979 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 980 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 981 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 982 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 983 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 984 | done = dispatchMotionLocked(currentTime, motionEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 985 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 986 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 987 | |
| 988 | case EventEntry::Type::SENSOR: { |
| 989 | std::shared_ptr<SensorEntry> sensorEntry = |
| 990 | std::static_pointer_cast<SensorEntry>(mPendingEvent); |
| 991 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 992 | dropReason = DropReason::APP_SWITCH; |
| 993 | } |
| 994 | // Sensor timestamps use SYSTEM_TIME_BOOTTIME time base, so we can't use |
| 995 | // 'currentTime' here, get SYSTEM_TIME_BOOTTIME instead. |
| 996 | nsecs_t bootTime = systemTime(SYSTEM_TIME_BOOTTIME); |
| 997 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(bootTime, *sensorEntry)) { |
| 998 | dropReason = DropReason::STALE; |
| 999 | } |
| 1000 | dispatchSensorLocked(currentTime, sensorEntry, &dropReason, nextWakeupTime); |
| 1001 | done = true; |
| 1002 | break; |
| 1003 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1004 | } |
| 1005 | |
| 1006 | if (done) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1007 | if (dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1008 | dropInboundEventLocked(*mPendingEvent, dropReason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1009 | } |
Michael Wright | 3a98172 | 2015-06-10 15:26:13 +0100 | [diff] [blame] | 1010 | mLastDropReason = dropReason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1011 | |
| 1012 | releasePendingEventLocked(); |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 1013 | *nextWakeupTime = LLONG_MIN; // force next poll to wake up immediately |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1014 | } |
| 1015 | } |
| 1016 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 1017 | bool InputDispatcher::isStaleEvent(nsecs_t currentTime, const EventEntry& entry) { |
| 1018 | return std::chrono::nanoseconds(currentTime - entry.eventTime) >= mStaleEventTimeout; |
| 1019 | } |
| 1020 | |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1021 | /** |
| 1022 | * Return true if the events preceding this incoming motion event should be dropped |
| 1023 | * Return false otherwise (the default behaviour) |
| 1024 | */ |
| 1025 | bool InputDispatcher::shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1026 | const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN && |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 1027 | isFromSource(motionEntry.source, AINPUT_SOURCE_CLASS_POINTER); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1028 | |
| 1029 | // Optimize case where the current application is unresponsive and the user |
| 1030 | // decides to touch a window in a different application. |
| 1031 | // If the application takes too long to catch up then we drop all events preceding |
| 1032 | // the touch into the other window. |
| 1033 | if (isPointerDownEvent && mAwaitedFocusedApplication != nullptr) { |
Siarhei Vishniakou | 9306c38 | 2022-09-30 15:30:31 -0700 | [diff] [blame] | 1034 | const int32_t displayId = motionEntry.displayId; |
| 1035 | const auto [x, y] = resolveTouchedPosition(motionEntry); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1036 | const bool isStylus = isPointerFromStylus(motionEntry, /*pointerIndex=*/0); |
Siarhei Vishniakou | 9306c38 | 2022-09-30 15:30:31 -0700 | [diff] [blame] | 1037 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1038 | auto [touchedWindowHandle, _] = findTouchedWindowAtLocked(displayId, x, y, isStylus); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1039 | if (touchedWindowHandle != nullptr && |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1040 | touchedWindowHandle->getApplicationToken() != |
| 1041 | mAwaitedFocusedApplication->getApplicationToken()) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1042 | // User touched a different application than the one we are waiting on. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1043 | ALOGI("Pruning input queue because user touched a different application while waiting " |
| 1044 | "for %s", |
| 1045 | mAwaitedFocusedApplication->getName().c_str()); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1046 | return true; |
| 1047 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1048 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1049 | // Alternatively, maybe there's a spy window that could handle this event. |
| 1050 | const std::vector<sp<WindowInfoHandle>> touchedSpies = |
| 1051 | findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus); |
| 1052 | for (const auto& windowHandle : touchedSpies) { |
| 1053 | const sp<Connection> connection = getConnectionLocked(windowHandle->getToken()); |
Siarhei Vishniakou | 34ed4d4 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1054 | if (connection != nullptr && connection->responsive) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1055 | // This spy window could take more input. Drop all events preceding this |
| 1056 | // 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] | 1057 | 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] | 1058 | "responsive spy window that may handle the event.", |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1059 | mAwaitedFocusedApplication->getName().c_str()); |
| 1060 | return true; |
| 1061 | } |
| 1062 | } |
| 1063 | } |
| 1064 | |
| 1065 | // Prevent getting stuck: if we have a pending key event, and some motion events that have not |
| 1066 | // yet been processed by some connections, the dispatcher will wait for these motion |
| 1067 | // events to be processed before dispatching the key event. This is because these motion events |
| 1068 | // may cause a new window to be launched, which the user might expect to receive focus. |
| 1069 | // To prevent waiting forever for such events, just send the key to the currently focused window |
| 1070 | if (isPointerDownEvent && mKeyIsWaitingForEventsTimeout) { |
| 1071 | ALOGD("Received a new pointer down event, stop waiting for events to process and " |
| 1072 | "just send the pending key event to the focused window."); |
| 1073 | mKeyIsWaitingForEventsTimeout = now(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1074 | } |
| 1075 | return false; |
| 1076 | } |
| 1077 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1078 | bool InputDispatcher::enqueueInboundEventLocked(std::unique_ptr<EventEntry> newEntry) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1079 | bool needWake = mInboundQueue.empty(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1080 | mInboundQueue.push_back(std::move(newEntry)); |
| 1081 | EventEntry& entry = *(mInboundQueue.back()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1082 | traceInboundQueueLengthLocked(); |
| 1083 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1084 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1085 | case EventEntry::Type::KEY: { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1086 | LOG_ALWAYS_FATAL_IF((entry.policyFlags & POLICY_FLAG_TRUSTED) == 0, |
| 1087 | "Unexpected untrusted event."); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1088 | // Optimize app switch latency. |
| 1089 | // If the application takes too long to catch up then we drop all events preceding |
| 1090 | // the app switch key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1091 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1092 | if (isAppSwitchKeyEvent(keyEntry)) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1093 | if (keyEntry.action == AKEY_EVENT_ACTION_DOWN) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1094 | mAppSwitchSawKeyDown = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1095 | } else if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1096 | if (mAppSwitchSawKeyDown) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1097 | if (DEBUG_APP_SWITCH) { |
| 1098 | ALOGD("App switch is pending!"); |
| 1099 | } |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1100 | mAppSwitchDueTime = keyEntry.eventTime + APP_SWITCH_TIMEOUT; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1101 | mAppSwitchSawKeyDown = false; |
| 1102 | needWake = true; |
| 1103 | } |
| 1104 | } |
| 1105 | } |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 1106 | |
| 1107 | // If a new up event comes in, and the pending event with same key code has been asked |
| 1108 | // to try again later because of the policy. We have to reset the intercept key wake up |
| 1109 | // time for it may have been handled in the policy and could be dropped. |
| 1110 | if (keyEntry.action == AKEY_EVENT_ACTION_UP && mPendingEvent && |
| 1111 | mPendingEvent->type == EventEntry::Type::KEY) { |
| 1112 | KeyEntry& pendingKey = static_cast<KeyEntry&>(*mPendingEvent); |
| 1113 | if (pendingKey.keyCode == keyEntry.keyCode && |
| 1114 | pendingKey.interceptKeyResult == |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1115 | KeyEntry::InterceptKeyResult::TRY_AGAIN_LATER) { |
| 1116 | pendingKey.interceptKeyResult = KeyEntry::InterceptKeyResult::UNKNOWN; |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 1117 | pendingKey.interceptKeyWakeupTime = 0; |
| 1118 | needWake = true; |
| 1119 | } |
| 1120 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1121 | break; |
| 1122 | } |
| 1123 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1124 | case EventEntry::Type::MOTION: { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1125 | LOG_ALWAYS_FATAL_IF((entry.policyFlags & POLICY_FLAG_TRUSTED) == 0, |
| 1126 | "Unexpected untrusted event."); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1127 | if (shouldPruneInboundQueueLocked(static_cast<MotionEntry&>(entry))) { |
| 1128 | mNextUnblockedEvent = mInboundQueue.back(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1129 | needWake = true; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1130 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1131 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1132 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1133 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1134 | LOG_ALWAYS_FATAL("Focus events should be inserted using enqueueFocusEventLocked"); |
| 1135 | break; |
| 1136 | } |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1137 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1138 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1139 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1140 | case EventEntry::Type::SENSOR: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1141 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1142 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1143 | // nothing to do |
| 1144 | break; |
| 1145 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1146 | } |
| 1147 | |
| 1148 | return needWake; |
| 1149 | } |
| 1150 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1151 | void InputDispatcher::addRecentEventLocked(std::shared_ptr<EventEntry> entry) { |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1152 | // Do not store sensor event in recent queue to avoid flooding the queue. |
| 1153 | if (entry->type != EventEntry::Type::SENSOR) { |
| 1154 | mRecentQueue.push_back(entry); |
| 1155 | } |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1156 | if (mRecentQueue.size() > RECENT_QUEUE_MAX_SIZE) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1157 | mRecentQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1158 | } |
| 1159 | } |
| 1160 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1161 | std::pair<sp<WindowInfoHandle>, std::vector<InputTarget>> |
| 1162 | InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, int32_t x, int32_t y, bool isStylus, |
| 1163 | bool ignoreDragWindow) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1164 | // Traverse windows from front to back to find touched window. |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1165 | std::vector<InputTarget> outsideTargets; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1166 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1167 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 1168 | if (ignoreDragWindow && haveSameToken(windowHandle, mDragState->dragWindow)) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1169 | continue; |
| 1170 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1171 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1172 | const WindowInfo& info = *windowHandle->getInfo(); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1173 | if (!info.isSpy() && windowAcceptsTouchAt(info, displayId, x, y, isStylus)) { |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1174 | return {windowHandle, outsideTargets}; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1175 | } |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1176 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1177 | if (info.inputConfig.test(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH)) { |
| 1178 | addWindowTargetLocked(windowHandle, InputTarget::Flags::DISPATCH_AS_OUTSIDE, |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 1179 | /*pointerIds=*/{}, /*firstDownTimeInTarget=*/std::nullopt, |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1180 | outsideTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1181 | } |
| 1182 | } |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1183 | return {nullptr, {}}; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1184 | } |
| 1185 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1186 | std::vector<sp<WindowInfoHandle>> InputDispatcher::findTouchedSpyWindowsAtLocked( |
| 1187 | int32_t displayId, int32_t x, int32_t y, bool isStylus) const { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1188 | // Traverse windows from front to back and gather the touched spy windows. |
| 1189 | std::vector<sp<WindowInfoHandle>> spyWindows; |
| 1190 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
| 1191 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 1192 | const WindowInfo& info = *windowHandle->getInfo(); |
| 1193 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1194 | if (!windowAcceptsTouchAt(info, displayId, x, y, isStylus)) { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1195 | continue; |
| 1196 | } |
| 1197 | if (!info.isSpy()) { |
| 1198 | // The first touched non-spy window was found, so return the spy windows touched so far. |
| 1199 | return spyWindows; |
| 1200 | } |
| 1201 | spyWindows.push_back(windowHandle); |
| 1202 | } |
| 1203 | return spyWindows; |
| 1204 | } |
| 1205 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1206 | void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1207 | const char* reason; |
| 1208 | switch (dropReason) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1209 | case DropReason::POLICY: |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame^] | 1210 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1211 | ALOGD("Dropped event because policy consumed it."); |
| 1212 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1213 | reason = "inbound event was dropped because the policy consumed it"; |
| 1214 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1215 | case DropReason::DISABLED: |
| 1216 | if (mLastDropReason != DropReason::DISABLED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1217 | ALOGI("Dropped event because input dispatch is disabled."); |
| 1218 | } |
| 1219 | reason = "inbound event was dropped because input dispatch is disabled"; |
| 1220 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1221 | case DropReason::APP_SWITCH: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1222 | ALOGI("Dropped event because of pending overdue app switch."); |
| 1223 | reason = "inbound event was dropped because of pending overdue app switch"; |
| 1224 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1225 | case DropReason::BLOCKED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1226 | ALOGI("Dropped event because the current application is not responding and the user " |
| 1227 | "has started interacting with a different application."); |
| 1228 | reason = "inbound event was dropped because the current application is not responding " |
| 1229 | "and the user has started interacting with a different application"; |
| 1230 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1231 | case DropReason::STALE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1232 | ALOGI("Dropped event because it is stale."); |
| 1233 | reason = "inbound event was dropped because it is stale"; |
| 1234 | break; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1235 | case DropReason::NO_POINTER_CAPTURE: |
| 1236 | ALOGI("Dropped event because there is no window with Pointer Capture."); |
| 1237 | reason = "inbound event was dropped because there is no window with Pointer Capture"; |
| 1238 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1239 | case DropReason::NOT_DROPPED: { |
| 1240 | LOG_ALWAYS_FATAL("Should not be dropping a NOT_DROPPED event"); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1241 | return; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1242 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1243 | } |
| 1244 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1245 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1246 | case EventEntry::Type::KEY: { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1247 | CancelationOptions options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, reason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1248 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1249 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1250 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1251 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1252 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1253 | if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1254 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, reason); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1255 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1256 | } else { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1257 | CancelationOptions options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, |
| 1258 | reason); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1259 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1260 | } |
| 1261 | break; |
| 1262 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1263 | case EventEntry::Type::SENSOR: { |
| 1264 | break; |
| 1265 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1266 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1267 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1268 | break; |
| 1269 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1270 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1271 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1272 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 1273 | case EventEntry::Type::DEVICE_RESET: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1274 | 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] | 1275 | break; |
| 1276 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1277 | } |
| 1278 | } |
| 1279 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1280 | static bool isAppSwitchKeyCode(int32_t keyCode) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1281 | return keyCode == AKEYCODE_HOME || keyCode == AKEYCODE_ENDCALL || |
| 1282 | keyCode == AKEYCODE_APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1283 | } |
| 1284 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1285 | bool InputDispatcher::isAppSwitchKeyEvent(const KeyEntry& keyEntry) { |
| 1286 | return !(keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) && isAppSwitchKeyCode(keyEntry.keyCode) && |
| 1287 | (keyEntry.policyFlags & POLICY_FLAG_TRUSTED) && |
| 1288 | (keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1289 | } |
| 1290 | |
| 1291 | bool InputDispatcher::isAppSwitchPendingLocked() { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 1292 | return mAppSwitchDueTime != LLONG_MAX; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | void InputDispatcher::resetPendingAppSwitchLocked(bool handled) { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 1296 | mAppSwitchDueTime = LLONG_MAX; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1297 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1298 | if (DEBUG_APP_SWITCH) { |
| 1299 | if (handled) { |
| 1300 | ALOGD("App switch has arrived."); |
| 1301 | } else { |
| 1302 | ALOGD("App switch was abandoned."); |
| 1303 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1304 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1305 | } |
| 1306 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1307 | bool InputDispatcher::haveCommandsLocked() const { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1308 | return !mCommandQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1309 | } |
| 1310 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1311 | bool InputDispatcher::runCommandsLockedInterruptable() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1312 | if (mCommandQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1313 | return false; |
| 1314 | } |
| 1315 | |
| 1316 | do { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1317 | auto command = std::move(mCommandQueue.front()); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1318 | mCommandQueue.pop_front(); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1319 | // Commands are run with the lock held, but may release and re-acquire the lock from within. |
| 1320 | command(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1321 | } while (!mCommandQueue.empty()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1322 | return true; |
| 1323 | } |
| 1324 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1325 | void InputDispatcher::postCommandLocked(Command&& command) { |
| 1326 | mCommandQueue.push_back(command); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1327 | } |
| 1328 | |
| 1329 | void InputDispatcher::drainInboundQueueLocked() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1330 | while (!mInboundQueue.empty()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1331 | std::shared_ptr<EventEntry> entry = mInboundQueue.front(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1332 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1333 | releaseInboundEventLocked(entry); |
| 1334 | } |
| 1335 | traceInboundQueueLengthLocked(); |
| 1336 | } |
| 1337 | |
| 1338 | void InputDispatcher::releasePendingEventLocked() { |
| 1339 | if (mPendingEvent) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1340 | releaseInboundEventLocked(mPendingEvent); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1341 | mPendingEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1342 | } |
| 1343 | } |
| 1344 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1345 | void InputDispatcher::releaseInboundEventLocked(std::shared_ptr<EventEntry> entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1346 | InjectionState* injectionState = entry->injectionState; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1347 | if (injectionState && injectionState->injectionResult == InputEventInjectionResult::PENDING) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1348 | if (DEBUG_DISPATCH_CYCLE) { |
| 1349 | ALOGD("Injected inbound event was dropped."); |
| 1350 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1351 | setInjectionResult(*entry, InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1352 | } |
| 1353 | if (entry == mNextUnblockedEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1354 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1355 | } |
| 1356 | addRecentEventLocked(entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1357 | } |
| 1358 | |
| 1359 | void InputDispatcher::resetKeyRepeatLocked() { |
| 1360 | if (mKeyRepeatState.lastKeyEntry) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1361 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1362 | } |
| 1363 | } |
| 1364 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1365 | std::shared_ptr<KeyEntry> InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) { |
| 1366 | std::shared_ptr<KeyEntry> entry = mKeyRepeatState.lastKeyEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1367 | |
Michael Wright | 2e73295 | 2014-09-24 13:26:59 -0700 | [diff] [blame] | 1368 | uint32_t policyFlags = entry->policyFlags & |
| 1369 | (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1370 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1371 | std::shared_ptr<KeyEntry> newEntry = |
| 1372 | std::make_unique<KeyEntry>(mIdGenerator.nextId(), currentTime, entry->deviceId, |
| 1373 | entry->source, entry->displayId, policyFlags, entry->action, |
| 1374 | entry->flags, entry->keyCode, entry->scanCode, |
| 1375 | entry->metaState, entry->repeatCount + 1, entry->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1376 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1377 | newEntry->syntheticRepeat = true; |
| 1378 | mKeyRepeatState.lastKeyEntry = newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1379 | mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1380 | return newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1381 | } |
| 1382 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1383 | bool InputDispatcher::dispatchConfigurationChangedLocked(nsecs_t currentTime, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1384 | const ConfigurationChangedEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1385 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1386 | ALOGD("dispatchConfigurationChanged - eventTime=%" PRId64, entry.eventTime); |
| 1387 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1388 | |
| 1389 | // Reset key repeating in case a keyboard device was added or removed or something. |
| 1390 | resetKeyRepeatLocked(); |
| 1391 | |
| 1392 | // 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] | 1393 | auto command = [this, eventTime = entry.eventTime]() REQUIRES(mLock) { |
| 1394 | scoped_unlock unlock(mLock); |
| 1395 | mPolicy->notifyConfigurationChanged(eventTime); |
| 1396 | }; |
| 1397 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1398 | return true; |
| 1399 | } |
| 1400 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1401 | bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime, |
| 1402 | const DeviceResetEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1403 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1404 | ALOGD("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry.eventTime, |
| 1405 | entry.deviceId); |
| 1406 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1407 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 1408 | // Reset key repeating in case a keyboard device was disabled or enabled. |
| 1409 | if (mKeyRepeatState.lastKeyEntry && mKeyRepeatState.lastKeyEntry->deviceId == entry.deviceId) { |
| 1410 | resetKeyRepeatLocked(); |
| 1411 | } |
| 1412 | |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1413 | CancelationOptions options(CancelationOptions::Mode::CANCEL_ALL_EVENTS, "device was reset"); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1414 | options.deviceId = entry.deviceId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1415 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1416 | return true; |
| 1417 | } |
| 1418 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1419 | void InputDispatcher::enqueueFocusEventLocked(const sp<IBinder>& windowToken, bool hasFocus, |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1420 | const std::string& reason) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1421 | if (mPendingEvent != nullptr) { |
| 1422 | // Move the pending event to the front of the queue. This will give the chance |
| 1423 | // for the pending event to get dispatched to the newly focused window |
| 1424 | mInboundQueue.push_front(mPendingEvent); |
| 1425 | mPendingEvent = nullptr; |
| 1426 | } |
| 1427 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1428 | std::unique_ptr<FocusEntry> focusEntry = |
| 1429 | std::make_unique<FocusEntry>(mIdGenerator.nextId(), now(), windowToken, hasFocus, |
| 1430 | reason); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1431 | |
| 1432 | // This event should go to the front of the queue, but behind all other focus events |
| 1433 | // Find the last focus event, and insert right after it |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1434 | std::deque<std::shared_ptr<EventEntry>>::reverse_iterator it = |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1435 | std::find_if(mInboundQueue.rbegin(), mInboundQueue.rend(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1436 | [](const std::shared_ptr<EventEntry>& event) { |
| 1437 | return event->type == EventEntry::Type::FOCUS; |
| 1438 | }); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1439 | |
| 1440 | // 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] | 1441 | mInboundQueue.insert(it.base(), std::move(focusEntry)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1442 | } |
| 1443 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1444 | void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime, std::shared_ptr<FocusEntry> entry) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 1445 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1446 | if (channel == nullptr) { |
| 1447 | return; // Window has gone away |
| 1448 | } |
| 1449 | InputTarget target; |
| 1450 | target.inputChannel = channel; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 1451 | target.flags = InputTarget::Flags::DISPATCH_AS_IS; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1452 | entry->dispatchInProgress = true; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1453 | std::string message = std::string("Focus ") + (entry->hasFocus ? "entering " : "leaving ") + |
| 1454 | channel->getName(); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 1455 | std::string reason = std::string("reason=").append(entry->reason); |
| 1456 | android_log_event_list(LOGTAG_INPUT_FOCUS) << message << reason << LOG_ID_EVENTS; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1457 | dispatchEventLocked(currentTime, entry, {target}); |
| 1458 | } |
| 1459 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1460 | void InputDispatcher::dispatchPointerCaptureChangedLocked( |
| 1461 | nsecs_t currentTime, const std::shared_ptr<PointerCaptureChangedEntry>& entry, |
| 1462 | DropReason& dropReason) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1463 | dropReason = DropReason::NOT_DROPPED; |
| 1464 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1465 | const bool haveWindowWithPointerCapture = mWindowTokenWithPointerCapture != nullptr; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1466 | sp<IBinder> token; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1467 | |
| 1468 | if (entry->pointerCaptureRequest.enable) { |
| 1469 | // Enable Pointer Capture. |
| 1470 | if (haveWindowWithPointerCapture && |
| 1471 | (entry->pointerCaptureRequest == mCurrentPointerCaptureRequest)) { |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 1472 | // This can happen if pointer capture is disabled and re-enabled before we notify the |
| 1473 | // app of the state change, so there is no need to notify the app. |
| 1474 | ALOGI("Skipping dispatch of Pointer Capture being enabled: no state change."); |
| 1475 | return; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1476 | } |
| 1477 | if (!mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1478 | // This can happen if a window requests capture and immediately releases capture. |
| 1479 | ALOGW("No window requested Pointer Capture."); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1480 | dropReason = DropReason::NO_POINTER_CAPTURE; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1481 | return; |
| 1482 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1483 | if (entry->pointerCaptureRequest.seq != mCurrentPointerCaptureRequest.seq) { |
| 1484 | ALOGI("Skipping dispatch of Pointer Capture being enabled: sequence number mismatch."); |
| 1485 | return; |
| 1486 | } |
| 1487 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1488 | token = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1489 | LOG_ALWAYS_FATAL_IF(!token, "Cannot find focused window for Pointer Capture."); |
| 1490 | mWindowTokenWithPointerCapture = token; |
| 1491 | } else { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1492 | // Disable Pointer Capture. |
| 1493 | // We do not check if the sequence number matches for requests to disable Pointer Capture |
| 1494 | // for two reasons: |
| 1495 | // 1. Pointer Capture can be disabled by a focus change, which means we can get two entries |
| 1496 | // to disable capture with the same sequence number: one generated by |
| 1497 | // disablePointerCaptureForcedLocked() and another as an acknowledgement of Pointer |
| 1498 | // Capture being disabled in InputReader. |
| 1499 | // 2. We respect any request to disable Pointer Capture generated by InputReader, since the |
| 1500 | // actual Pointer Capture state that affects events being generated by input devices is |
| 1501 | // in InputReader. |
| 1502 | if (!haveWindowWithPointerCapture) { |
| 1503 | // Pointer capture was already forcefully disabled because of focus change. |
| 1504 | dropReason = DropReason::NOT_DROPPED; |
| 1505 | return; |
| 1506 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1507 | token = mWindowTokenWithPointerCapture; |
| 1508 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1509 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1510 | setPointerCaptureLocked(false); |
| 1511 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1512 | } |
| 1513 | |
| 1514 | auto channel = getInputChannelLocked(token); |
| 1515 | if (channel == nullptr) { |
| 1516 | // Window has gone away, clean up Pointer Capture state. |
| 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 | return; |
| 1522 | } |
| 1523 | InputTarget target; |
| 1524 | target.inputChannel = channel; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 1525 | target.flags = InputTarget::Flags::DISPATCH_AS_IS; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1526 | entry->dispatchInProgress = true; |
| 1527 | dispatchEventLocked(currentTime, entry, {target}); |
| 1528 | |
| 1529 | dropReason = DropReason::NOT_DROPPED; |
| 1530 | } |
| 1531 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1532 | void InputDispatcher::dispatchTouchModeChangeLocked(nsecs_t currentTime, |
| 1533 | const std::shared_ptr<TouchModeEntry>& entry) { |
| 1534 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 1535 | getWindowHandlesLocked(entry->displayId); |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1536 | if (windowHandles.empty()) { |
| 1537 | return; |
| 1538 | } |
| 1539 | const std::vector<InputTarget> inputTargets = |
| 1540 | getInputTargetsFromWindowHandlesLocked(windowHandles); |
| 1541 | if (inputTargets.empty()) { |
| 1542 | return; |
| 1543 | } |
| 1544 | entry->dispatchInProgress = true; |
| 1545 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1546 | } |
| 1547 | |
| 1548 | std::vector<InputTarget> InputDispatcher::getInputTargetsFromWindowHandlesLocked( |
| 1549 | const std::vector<sp<WindowInfoHandle>>& windowHandles) const { |
| 1550 | std::vector<InputTarget> inputTargets; |
| 1551 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1552 | const sp<IBinder>& token = handle->getToken(); |
| 1553 | if (token == nullptr) { |
| 1554 | continue; |
| 1555 | } |
| 1556 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(token); |
| 1557 | if (channel == nullptr) { |
| 1558 | continue; // Window has gone away |
| 1559 | } |
| 1560 | InputTarget target; |
| 1561 | target.inputChannel = channel; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 1562 | target.flags = InputTarget::Flags::DISPATCH_AS_IS; |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1563 | inputTargets.push_back(target); |
| 1564 | } |
| 1565 | return inputTargets; |
| 1566 | } |
| 1567 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1568 | bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, std::shared_ptr<KeyEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1569 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1570 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1571 | if (!entry->dispatchInProgress) { |
| 1572 | if (entry->repeatCount == 0 && entry->action == AKEY_EVENT_ACTION_DOWN && |
| 1573 | (entry->policyFlags & POLICY_FLAG_TRUSTED) && |
| 1574 | (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { |
| 1575 | if (mKeyRepeatState.lastKeyEntry && |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1576 | mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode && |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1577 | // We have seen two identical key downs in a row which indicates that the device |
| 1578 | // driver is automatically generating key repeats itself. We take note of the |
| 1579 | // repeat here, but we disable our own next key repeat timer since it is clear that |
| 1580 | // we will not need to synthesize key repeats ourselves. |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1581 | mKeyRepeatState.lastKeyEntry->deviceId == entry->deviceId) { |
| 1582 | // Make sure we don't get key down from a different device. If a different |
| 1583 | // device Id has same key pressed down, the new device Id will replace the |
| 1584 | // current one to hold the key repeat with repeat count reset. |
| 1585 | // In the future when got a KEY_UP on the device id, drop it and do not |
| 1586 | // stop the key repeat on current device. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1587 | entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; |
| 1588 | resetKeyRepeatLocked(); |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 1589 | mKeyRepeatState.nextRepeatTime = LLONG_MAX; // don't generate repeats ourselves |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1590 | } else { |
| 1591 | // Not a repeat. Save key down state in case we do see a repeat later. |
| 1592 | resetKeyRepeatLocked(); |
| 1593 | mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; |
| 1594 | } |
| 1595 | mKeyRepeatState.lastKeyEntry = entry; |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1596 | } else if (entry->action == AKEY_EVENT_ACTION_UP && mKeyRepeatState.lastKeyEntry && |
| 1597 | mKeyRepeatState.lastKeyEntry->deviceId != entry->deviceId) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1598 | // 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^] | 1599 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1600 | ALOGD("deviceId=%d got KEY_UP as stale", entry->deviceId); |
| 1601 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1602 | } else if (!entry->syntheticRepeat) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1603 | resetKeyRepeatLocked(); |
| 1604 | } |
| 1605 | |
| 1606 | if (entry->repeatCount == 1) { |
| 1607 | entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; |
| 1608 | } else { |
| 1609 | entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; |
| 1610 | } |
| 1611 | |
| 1612 | entry->dispatchInProgress = true; |
| 1613 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1614 | logOutboundKeyDetails("dispatchKey - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1615 | } |
| 1616 | |
| 1617 | // 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] | 1618 | if (entry->interceptKeyResult == KeyEntry::InterceptKeyResult::TRY_AGAIN_LATER) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1619 | if (currentTime < entry->interceptKeyWakeupTime) { |
| 1620 | if (entry->interceptKeyWakeupTime < *nextWakeupTime) { |
| 1621 | *nextWakeupTime = entry->interceptKeyWakeupTime; |
| 1622 | } |
| 1623 | return false; // wait until next wakeup |
| 1624 | } |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1625 | entry->interceptKeyResult = KeyEntry::InterceptKeyResult::UNKNOWN; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1626 | entry->interceptKeyWakeupTime = 0; |
| 1627 | } |
| 1628 | |
| 1629 | // Give the policy a chance to intercept the key. |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1630 | if (entry->interceptKeyResult == KeyEntry::InterceptKeyResult::UNKNOWN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1631 | if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1632 | sp<IBinder> focusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1633 | mFocusResolver.getFocusedWindowToken(getTargetDisplayId(*entry)); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1634 | |
| 1635 | auto command = [this, focusedWindowToken, entry]() REQUIRES(mLock) { |
| 1636 | doInterceptKeyBeforeDispatchingCommand(focusedWindowToken, *entry); |
| 1637 | }; |
| 1638 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1639 | return false; // wait for the command to run |
| 1640 | } else { |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1641 | entry->interceptKeyResult = KeyEntry::InterceptKeyResult::CONTINUE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1642 | } |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 1643 | } else if (entry->interceptKeyResult == KeyEntry::InterceptKeyResult::SKIP) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1644 | if (*dropReason == DropReason::NOT_DROPPED) { |
| 1645 | *dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1646 | } |
| 1647 | } |
| 1648 | |
| 1649 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1650 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1651 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1652 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1653 | : InputEventInjectionResult::FAILED); |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1654 | mReporter->reportDroppedKey(entry->id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1655 | return true; |
| 1656 | } |
| 1657 | |
| 1658 | // Identify targets. |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1659 | InputEventInjectionResult injectionResult; |
| 1660 | sp<WindowInfoHandle> focusedWindow = |
| 1661 | findFocusedWindowTargetLocked(currentTime, *entry, nextWakeupTime, |
| 1662 | /*byref*/ injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1663 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1664 | return false; |
| 1665 | } |
| 1666 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1667 | setInjectionResult(*entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1668 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1669 | return true; |
| 1670 | } |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1671 | LOG_ALWAYS_FATAL_IF(focusedWindow == nullptr); |
| 1672 | |
| 1673 | std::vector<InputTarget> inputTargets; |
| 1674 | addWindowTargetLocked(focusedWindow, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 1675 | InputTarget::Flags::FOREGROUND | InputTarget::Flags::DISPATCH_AS_IS, |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 1676 | /*pointerIds=*/{}, getDownTime(*entry), inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1677 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1678 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1679 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1680 | |
| 1681 | // Dispatch the key. |
| 1682 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1683 | return true; |
| 1684 | } |
| 1685 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1686 | void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1687 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1688 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 ", " |
| 1689 | "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, " |
| 1690 | "metaState=0x%x, repeatCount=%d, downTime=%" PRId64, |
| 1691 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, |
| 1692 | entry.policyFlags, entry.action, entry.flags, entry.keyCode, entry.scanCode, |
| 1693 | entry.metaState, entry.repeatCount, entry.downTime); |
| 1694 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1695 | } |
| 1696 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1697 | void InputDispatcher::dispatchSensorLocked(nsecs_t currentTime, |
| 1698 | const std::shared_ptr<SensorEntry>& entry, |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1699 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1700 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1701 | ALOGD("notifySensorEvent eventTime=%" PRId64 ", hwTimestamp=%" PRId64 ", deviceId=%d, " |
| 1702 | "source=0x%x, sensorType=%s", |
| 1703 | entry->eventTime, entry->hwTimestamp, entry->deviceId, entry->source, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1704 | ftl::enum_string(entry->sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1705 | } |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1706 | auto command = [this, entry]() REQUIRES(mLock) { |
| 1707 | scoped_unlock unlock(mLock); |
| 1708 | |
| 1709 | if (entry->accuracyChanged) { |
| 1710 | mPolicy->notifySensorAccuracy(entry->deviceId, entry->sensorType, entry->accuracy); |
| 1711 | } |
| 1712 | mPolicy->notifySensorEvent(entry->deviceId, entry->sensorType, entry->accuracy, |
| 1713 | entry->hwTimestamp, entry->values); |
| 1714 | }; |
| 1715 | postCommandLocked(std::move(command)); |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1716 | } |
| 1717 | |
| 1718 | bool InputDispatcher::flushSensor(int deviceId, InputDeviceSensorType sensorType) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1719 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1720 | ALOGD("flushSensor deviceId=%d, sensorType=%s", deviceId, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1721 | ftl::enum_string(sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1722 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1723 | { // acquire lock |
| 1724 | std::scoped_lock _l(mLock); |
| 1725 | |
| 1726 | for (auto it = mInboundQueue.begin(); it != mInboundQueue.end(); it++) { |
| 1727 | std::shared_ptr<EventEntry> entry = *it; |
| 1728 | if (entry->type == EventEntry::Type::SENSOR) { |
| 1729 | it = mInboundQueue.erase(it); |
| 1730 | releaseInboundEventLocked(entry); |
| 1731 | } |
| 1732 | } |
| 1733 | } |
| 1734 | return true; |
| 1735 | } |
| 1736 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1737 | bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, std::shared_ptr<MotionEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1738 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1739 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1740 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1741 | if (!entry->dispatchInProgress) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1742 | entry->dispatchInProgress = true; |
| 1743 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1744 | logOutboundMotionDetails("dispatchMotion - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1745 | } |
| 1746 | |
| 1747 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1748 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1749 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1750 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1751 | : InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1752 | return true; |
| 1753 | } |
| 1754 | |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 1755 | const bool isPointerEvent = isFromSource(entry->source, AINPUT_SOURCE_CLASS_POINTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1756 | |
| 1757 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1758 | std::vector<InputTarget> inputTargets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1759 | |
| 1760 | bool conflictingPointerActions = false; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1761 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1762 | if (isPointerEvent) { |
| 1763 | // Pointer event. (eg. touchscreen) |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 1764 | |
| 1765 | if (mDragState && |
| 1766 | (entry->action & AMOTION_EVENT_ACTION_MASK) == AMOTION_EVENT_ACTION_POINTER_DOWN) { |
| 1767 | // If drag and drop ongoing and pointer down occur: pilfer drag window pointers |
| 1768 | pilferPointersLocked(mDragState->dragWindow->getToken()); |
| 1769 | } |
| 1770 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 1771 | inputTargets = |
Siarhei Vishniakou | 4fe5739 | 2022-10-25 13:44:30 -0700 | [diff] [blame] | 1772 | findTouchedWindowTargetsLocked(currentTime, *entry, &conflictingPointerActions, |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1773 | /*byref*/ injectionResult); |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 1774 | LOG_ALWAYS_FATAL_IF(injectionResult != InputEventInjectionResult::SUCCEEDED && |
| 1775 | !inputTargets.empty()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1776 | } else { |
| 1777 | // Non touch event. (eg. trackball) |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1778 | sp<WindowInfoHandle> focusedWindow = |
| 1779 | findFocusedWindowTargetLocked(currentTime, *entry, nextWakeupTime, injectionResult); |
| 1780 | if (injectionResult == InputEventInjectionResult::SUCCEEDED) { |
| 1781 | LOG_ALWAYS_FATAL_IF(focusedWindow == nullptr); |
| 1782 | addWindowTargetLocked(focusedWindow, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 1783 | InputTarget::Flags::FOREGROUND | |
| 1784 | InputTarget::Flags::DISPATCH_AS_IS, |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 1785 | /*pointerIds=*/{}, getDownTime(*entry), inputTargets); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1786 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1787 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1788 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1789 | return false; |
| 1790 | } |
| 1791 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1792 | setInjectionResult(*entry, injectionResult); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1793 | if (injectionResult == InputEventInjectionResult::TARGET_MISMATCH) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1794 | return true; |
| 1795 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1796 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1797 | CancelationOptions::Mode mode( |
| 1798 | isPointerEvent ? CancelationOptions::Mode::CANCEL_POINTER_EVENTS |
| 1799 | : CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS); |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1800 | CancelationOptions options(mode, "input event injection failed"); |
| 1801 | synthesizeCancelationEventsForMonitorsLocked(options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1802 | return true; |
| 1803 | } |
| 1804 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1805 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1806 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1807 | |
| 1808 | // Dispatch the motion. |
| 1809 | if (conflictingPointerActions) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1810 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1811 | "conflicting pointer actions"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1812 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1813 | } |
| 1814 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1815 | return true; |
| 1816 | } |
| 1817 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1818 | void InputDispatcher::enqueueDragEventLocked(const sp<WindowInfoHandle>& windowHandle, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 1819 | bool isExiting, const int32_t rawX, |
| 1820 | const int32_t rawY) { |
| 1821 | const vec2 xy = windowHandle->getInfo()->transform.transform(vec2(rawX, rawY)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1822 | std::unique_ptr<DragEntry> dragEntry = |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 1823 | std::make_unique<DragEntry>(mIdGenerator.nextId(), now(), windowHandle->getToken(), |
| 1824 | isExiting, xy.x, xy.y); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1825 | |
| 1826 | enqueueInboundEventLocked(std::move(dragEntry)); |
| 1827 | } |
| 1828 | |
| 1829 | void InputDispatcher::dispatchDragLocked(nsecs_t currentTime, std::shared_ptr<DragEntry> entry) { |
| 1830 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
| 1831 | if (channel == nullptr) { |
| 1832 | return; // Window has gone away |
| 1833 | } |
| 1834 | InputTarget target; |
| 1835 | target.inputChannel = channel; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 1836 | target.flags = InputTarget::Flags::DISPATCH_AS_IS; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1837 | entry->dispatchInProgress = true; |
| 1838 | dispatchEventLocked(currentTime, entry, {target}); |
| 1839 | } |
| 1840 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1841 | void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1842 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 1843 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=%s, displayId=%" PRId32 |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1844 | ", policyFlags=0x%x, " |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1845 | "action=%s, actionButton=0x%x, flags=0x%x, " |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1846 | "metaState=0x%x, buttonState=0x%x," |
| 1847 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64, |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 1848 | prefix, entry.eventTime, entry.deviceId, |
| 1849 | inputEventSourceToString(entry.source).c_str(), entry.displayId, entry.policyFlags, |
| 1850 | MotionEvent::actionToString(entry.action).c_str(), entry.actionButton, entry.flags, |
| 1851 | entry.metaState, entry.buttonState, entry.edgeFlags, entry.xPrecision, |
| 1852 | entry.yPrecision, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1853 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1854 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
| 1855 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
| 1856 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 1857 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 1858 | "orientation=%f", |
| 1859 | i, entry.pointerProperties[i].id, entry.pointerProperties[i].toolType, |
| 1860 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 1861 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 1862 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 1863 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 1864 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 1865 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 1866 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 1867 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 1868 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| 1869 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1870 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1871 | } |
| 1872 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1873 | void InputDispatcher::dispatchEventLocked(nsecs_t currentTime, |
| 1874 | std::shared_ptr<EventEntry> eventEntry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1875 | const std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1876 | ATRACE_CALL(); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1877 | if (DEBUG_DISPATCH_CYCLE) { |
| 1878 | ALOGD("dispatchEventToCurrentInputTargets"); |
| 1879 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1880 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1881 | updateInteractionTokensLocked(*eventEntry, inputTargets); |
| 1882 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1883 | ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true |
| 1884 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1885 | pokeUserActivityLocked(*eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1886 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1887 | for (const InputTarget& inputTarget : inputTargets) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 1888 | sp<Connection> connection = |
| 1889 | getConnectionLocked(inputTarget.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 1890 | if (connection != nullptr) { |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 1891 | prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1892 | } else { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1893 | if (DEBUG_FOCUS) { |
| 1894 | ALOGD("Dropping event delivery to target with channel '%s' because it " |
| 1895 | "is no longer registered with the input dispatcher.", |
| 1896 | inputTarget.inputChannel->getName().c_str()); |
| 1897 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1898 | } |
| 1899 | } |
| 1900 | } |
| 1901 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1902 | void InputDispatcher::cancelEventsForAnrLocked(const sp<Connection>& connection) { |
| 1903 | // We will not be breaking any connections here, even if the policy wants us to abort dispatch. |
| 1904 | // If the policy decides to close the app, we will get a channel removal event via |
| 1905 | // unregisterInputChannel, and will clean up the connection that way. We are already not |
| 1906 | // sending new pointers to the connection when it blocked, but focused events will continue to |
| 1907 | // pile up. |
| 1908 | ALOGW("Canceling events for %s because it is unresponsive", |
| 1909 | connection->inputChannel->getName().c_str()); |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 1910 | if (connection->status == Connection::Status::NORMAL) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 1911 | CancelationOptions options(CancelationOptions::Mode::CANCEL_ALL_EVENTS, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1912 | "application not responding"); |
| 1913 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1914 | } |
| 1915 | } |
| 1916 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1917 | void InputDispatcher::resetNoFocusedWindowTimeoutLocked() { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1918 | if (DEBUG_FOCUS) { |
| 1919 | ALOGD("Resetting ANR timeouts."); |
| 1920 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1921 | |
| 1922 | // Reset input target wait timeout. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1923 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1924 | mAwaitedFocusedApplication.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1925 | } |
| 1926 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1927 | /** |
| 1928 | * Get the display id that the given event should go to. If this event specifies a valid display id, |
| 1929 | * then it should be dispatched to that display. Otherwise, the event goes to the focused display. |
| 1930 | * Focused display is the display that the user most recently interacted with. |
| 1931 | */ |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1932 | int32_t InputDispatcher::getTargetDisplayId(const EventEntry& entry) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1933 | int32_t displayId; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1934 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1935 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1936 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 1937 | displayId = keyEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1938 | break; |
| 1939 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1940 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1941 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1942 | displayId = motionEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1943 | break; |
| 1944 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1945 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1946 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1947 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1948 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1949 | case EventEntry::Type::DEVICE_RESET: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1950 | case EventEntry::Type::SENSOR: |
| 1951 | case EventEntry::Type::DRAG: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1952 | 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] | 1953 | return ADISPLAY_ID_NONE; |
| 1954 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1955 | } |
| 1956 | return displayId == ADISPLAY_ID_NONE ? mFocusedDisplayId : displayId; |
| 1957 | } |
| 1958 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1959 | bool InputDispatcher::shouldWaitToSendKeyLocked(nsecs_t currentTime, |
| 1960 | const char* focusedWindowName) { |
| 1961 | if (mAnrTracker.empty()) { |
| 1962 | // already processed all events that we waited for |
| 1963 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1964 | return false; |
| 1965 | } |
| 1966 | |
| 1967 | if (!mKeyIsWaitingForEventsTimeout.has_value()) { |
| 1968 | // Start the timer |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 1969 | // 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] | 1970 | mKeyIsWaitingForEventsTimeout = currentTime + |
| 1971 | std::chrono::duration_cast<std::chrono::nanoseconds>(KEY_WAITING_FOR_EVENTS_TIMEOUT) |
| 1972 | .count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1973 | return true; |
| 1974 | } |
| 1975 | |
| 1976 | // We still have pending events, and already started the timer |
| 1977 | if (currentTime < *mKeyIsWaitingForEventsTimeout) { |
| 1978 | return true; // Still waiting |
| 1979 | } |
| 1980 | |
| 1981 | // Waited too long, and some connection still hasn't processed all motions |
| 1982 | // Just send the key to the focused window |
| 1983 | ALOGW("Dispatching key to %s even though there are other unprocessed events", |
| 1984 | focusedWindowName); |
| 1985 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1986 | return false; |
| 1987 | } |
| 1988 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1989 | sp<WindowInfoHandle> InputDispatcher::findFocusedWindowTargetLocked( |
| 1990 | nsecs_t currentTime, const EventEntry& entry, nsecs_t* nextWakeupTime, |
| 1991 | InputEventInjectionResult& outInjectionResult) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1992 | std::string reason; |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1993 | outInjectionResult = InputEventInjectionResult::FAILED; // Default result |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1994 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1995 | int32_t displayId = getTargetDisplayId(entry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1996 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1997 | std::shared_ptr<InputApplicationHandle> focusedApplicationHandle = |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1998 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 1999 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2000 | // If there is no currently focused window and no focused application |
| 2001 | // then drop the event. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2002 | if (focusedWindowHandle == nullptr && focusedApplicationHandle == nullptr) { |
| 2003 | ALOGI("Dropping %s event because there is no focused window or focused application in " |
| 2004 | "display %" PRId32 ".", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2005 | ftl::enum_string(entry.type).c_str(), displayId); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2006 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2007 | } |
| 2008 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2009 | // Drop key events if requested by input feature |
| 2010 | if (focusedWindowHandle != nullptr && shouldDropInput(entry, focusedWindowHandle)) { |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2011 | return nullptr; |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2012 | } |
| 2013 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2014 | // Compatibility behavior: raise ANR if there is a focused application, but no focused window. |
| 2015 | // Only start counting when we have a focused event to dispatch. The ANR is canceled if we |
| 2016 | // start interacting with another application via touch (app switch). This code can be removed |
| 2017 | // if the "no focused window ANR" is moved to the policy. Input doesn't know whether |
| 2018 | // an app is expected to have a focused window. |
| 2019 | if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) { |
| 2020 | if (!mNoFocusedWindowTimeoutTime.has_value()) { |
| 2021 | // 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] | 2022 | std::chrono::nanoseconds timeout = focusedApplicationHandle->getDispatchingTimeout( |
| 2023 | DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
| 2024 | mNoFocusedWindowTimeoutTime = currentTime + timeout.count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2025 | mAwaitedFocusedApplication = focusedApplicationHandle; |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 2026 | mAwaitedApplicationDisplayId = displayId; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2027 | ALOGW("Waiting because no window has focus but %s may eventually add a " |
| 2028 | "window when it finishes starting up. Will wait for %" PRId64 "ms", |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 2029 | mAwaitedFocusedApplication->getName().c_str(), millis(timeout)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2030 | *nextWakeupTime = *mNoFocusedWindowTimeoutTime; |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2031 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 2032 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2033 | } else if (currentTime > *mNoFocusedWindowTimeoutTime) { |
| 2034 | // Already raised ANR. Drop the event |
| 2035 | ALOGE("Dropping %s event because there is no focused window", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2036 | ftl::enum_string(entry.type).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2037 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2038 | } else { |
| 2039 | // Still waiting for the focused window |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2040 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 2041 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2042 | } |
| 2043 | } |
| 2044 | |
| 2045 | // we have a valid, non-null focused window |
| 2046 | resetNoFocusedWindowTimeoutLocked(); |
| 2047 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2048 | // Verify targeted injection. |
| 2049 | if (const auto err = verifyTargetedInjection(focusedWindowHandle, entry); err) { |
| 2050 | ALOGW("Dropping injected event: %s", (*err).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2051 | outInjectionResult = InputEventInjectionResult::TARGET_MISMATCH; |
| 2052 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2053 | } |
| 2054 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2055 | if (focusedWindowHandle->getInfo()->inputConfig.test( |
| 2056 | WindowInfo::InputConfig::PAUSE_DISPATCHING)) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2057 | ALOGI("Waiting because %s is paused", focusedWindowHandle->getName().c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2058 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 2059 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2060 | } |
| 2061 | |
| 2062 | // If the event is a key event, then we must wait for all previous events to |
| 2063 | // complete before delivering it because previous events may have the |
| 2064 | // side-effect of transferring focus to a different window and we want to |
| 2065 | // ensure that the following keys are sent to the new window. |
| 2066 | // |
| 2067 | // Suppose the user touches a button in a window then immediately presses "A". |
| 2068 | // If the button causes a pop-up window to appear then we want to ensure that |
| 2069 | // the "A" key is delivered to the new pop-up window. This is because users |
| 2070 | // often anticipate pending UI changes when typing on a keyboard. |
| 2071 | // To obtain this behavior, we must serialize key events with respect to all |
| 2072 | // prior input events. |
| 2073 | if (entry.type == EventEntry::Type::KEY) { |
| 2074 | if (shouldWaitToSendKeyLocked(currentTime, focusedWindowHandle->getName().c_str())) { |
| 2075 | *nextWakeupTime = *mKeyIsWaitingForEventsTimeout; |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2076 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 2077 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2078 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2079 | } |
| 2080 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2081 | outInjectionResult = InputEventInjectionResult::SUCCEEDED; |
| 2082 | return focusedWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2083 | } |
| 2084 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2085 | /** |
| 2086 | * Given a list of monitors, remove the ones we cannot find a connection for, and the ones |
| 2087 | * that are currently unresponsive. |
| 2088 | */ |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2089 | std::vector<Monitor> InputDispatcher::selectResponsiveMonitorsLocked( |
| 2090 | const std::vector<Monitor>& monitors) const { |
| 2091 | std::vector<Monitor> responsiveMonitors; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2092 | std::copy_if(monitors.begin(), monitors.end(), std::back_inserter(responsiveMonitors), |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2093 | [this](const Monitor& monitor) REQUIRES(mLock) { |
| 2094 | sp<Connection> connection = |
| 2095 | getConnectionLocked(monitor.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2096 | if (connection == nullptr) { |
| 2097 | ALOGE("Could not find connection for monitor %s", |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2098 | monitor.inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2099 | return false; |
| 2100 | } |
| 2101 | if (!connection->responsive) { |
| 2102 | ALOGW("Unresponsive monitor %s will not get the new gesture", |
| 2103 | connection->inputChannel->getName().c_str()); |
| 2104 | return false; |
| 2105 | } |
| 2106 | return true; |
| 2107 | }); |
| 2108 | return responsiveMonitors; |
| 2109 | } |
| 2110 | |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 2111 | /** |
| 2112 | * In general, touch should be always split between windows. Some exceptions: |
| 2113 | * 1. Don't split touch is if we have an active pointer down, and a new pointer is going down that's |
| 2114 | * from the same device, *and* the window that's receiving the current pointer does not support |
| 2115 | * split touch. |
| 2116 | * 2. Don't split mouse events |
| 2117 | */ |
| 2118 | bool InputDispatcher::shouldSplitTouch(const TouchState& touchState, |
| 2119 | const MotionEntry& entry) const { |
| 2120 | if (isFromSource(entry.source, AINPUT_SOURCE_MOUSE)) { |
| 2121 | // We should never split mouse events |
| 2122 | return false; |
| 2123 | } |
| 2124 | for (const TouchedWindow& touchedWindow : touchState.windows) { |
| 2125 | if (touchedWindow.windowHandle->getInfo()->isSpy()) { |
| 2126 | // Spy windows should not affect whether or not touch is split. |
| 2127 | continue; |
| 2128 | } |
| 2129 | if (touchedWindow.windowHandle->getInfo()->supportsSplitTouch()) { |
| 2130 | continue; |
| 2131 | } |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2132 | if (touchedWindow.windowHandle->getInfo()->inputConfig.test( |
| 2133 | gui::WindowInfo::InputConfig::IS_WALLPAPER)) { |
| 2134 | // Wallpaper window should not affect whether or not touch is split |
| 2135 | continue; |
| 2136 | } |
| 2137 | |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 2138 | // Eventually, touchedWindow will contain the deviceId of each pointer that's currently |
| 2139 | // being sent there. For now, use deviceId from touch state. |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 2140 | if (entry.deviceId == touchState.deviceId && touchedWindow.pointerIds.any()) { |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 2141 | return false; |
| 2142 | } |
| 2143 | } |
| 2144 | return true; |
| 2145 | } |
| 2146 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2147 | std::vector<InputTarget> InputDispatcher::findTouchedWindowTargetsLocked( |
Siarhei Vishniakou | 4fe5739 | 2022-10-25 13:44:30 -0700 | [diff] [blame] | 2148 | nsecs_t currentTime, const MotionEntry& entry, bool* outConflictingPointerActions, |
| 2149 | InputEventInjectionResult& outInjectionResult) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2150 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2151 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2152 | std::vector<InputTarget> targets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2153 | // For security reasons, we defer updating the touch state until we are sure that |
| 2154 | // event injection will be allowed. |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2155 | const int32_t displayId = entry.displayId; |
| 2156 | const int32_t action = entry.action; |
| 2157 | const int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2158 | |
| 2159 | // 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] | 2160 | outInjectionResult = InputEventInjectionResult::PENDING; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2161 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2162 | // Copy current touch state into tempTouchState. |
| 2163 | // This state will be used to update mTouchStatesByDisplay at the end of this function. |
| 2164 | // 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] | 2165 | const TouchState* oldState = nullptr; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2166 | TouchState tempTouchState; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2167 | if (const auto it = mTouchStatesByDisplay.find(displayId); it != mTouchStatesByDisplay.end()) { |
| 2168 | oldState = &(it->second); |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 2169 | tempTouchState = *oldState; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2170 | } |
| 2171 | |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 2172 | bool isSplit = shouldSplitTouch(tempTouchState, entry); |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 2173 | const bool switchedDevice = (oldState != nullptr) && |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2174 | (oldState->deviceId != entry.deviceId || oldState->source != entry.source); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2175 | |
| 2176 | const bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE || |
| 2177 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2178 | maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2179 | // A DOWN could be generated from POINTER_DOWN if the initial pointers did not land into any |
| 2180 | // touchable windows. |
| 2181 | const bool wasDown = oldState != nullptr && oldState->isDown(); |
| 2182 | const bool isDown = (maskedAction == AMOTION_EVENT_ACTION_DOWN) || |
| 2183 | (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN && !wasDown); |
| 2184 | const bool newGesture = isDown || maskedAction == AMOTION_EVENT_ACTION_SCROLL || isHoverAction; |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 2185 | const bool isFromMouse = isFromSource(entry.source, AINPUT_SOURCE_MOUSE); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2186 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2187 | // If pointers are already down, let's finish the current gesture and ignore the new events |
| 2188 | // from another device. However, if the new event is a down event, let's cancel the current |
| 2189 | // touch and let the new one take over. |
| 2190 | if (switchedDevice && wasDown && !isDown) { |
| 2191 | LOG(INFO) << "Dropping event because a pointer for device " << oldState->deviceId |
| 2192 | << " is already down in display " << displayId << ": " << entry.getDescription(); |
| 2193 | // TODO(b/211379801): test multiple simultaneous input streams. |
| 2194 | outInjectionResult = InputEventInjectionResult::FAILED; |
| 2195 | return {}; // wrong device |
| 2196 | } |
| 2197 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2198 | if (newGesture) { |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2199 | // If a new gesture is starting, clear the touch state completely. |
| 2200 | tempTouchState.reset(); |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2201 | tempTouchState.deviceId = entry.deviceId; |
| 2202 | tempTouchState.source = entry.source; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2203 | isSplit = false; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2204 | } else if (switchedDevice && maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2205 | ALOGI("Dropping move event because a pointer for a different device is already active " |
| 2206 | "in display %" PRId32, |
| 2207 | displayId); |
Siarhei Vishniakou | 16e4fa0 | 2023-02-16 17:48:56 -0800 | [diff] [blame] | 2208 | // TODO(b/211379801): test multiple simultaneous input streams. |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2209 | outInjectionResult = InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2210 | return {}; // wrong device |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2211 | } |
| 2212 | |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2213 | if (isHoverAction) { |
| 2214 | // For hover actions, we will treat 'tempTouchState' as a new state, so let's erase |
| 2215 | // all of the existing hovering pointers and recompute. |
| 2216 | tempTouchState.clearHoveringPointers(); |
| 2217 | } |
| 2218 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2219 | if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { |
| 2220 | /* 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] | 2221 | const auto [x, y] = resolveTouchedPosition(entry); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2222 | const int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2223 | // Outside targets should be added upon first dispatched DOWN event. That means, this should |
| 2224 | // 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] | 2225 | const bool isStylus = isPointerFromStylus(entry, pointerIndex); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2226 | auto [newTouchedWindowHandle, outsideTargets] = |
| 2227 | findTouchedWindowAtLocked(displayId, x, y, isStylus); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2228 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2229 | if (isDown) { |
| 2230 | targets += outsideTargets; |
| 2231 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2232 | // Handle the case where we did not find a window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2233 | if (newTouchedWindowHandle == nullptr) { |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 2234 | ALOGD("No new touched window at (%" PRId32 ", %" PRId32 ") in display %" PRId32, x, y, |
| 2235 | displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2236 | // 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] | 2237 | newTouchedWindowHandle = tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2238 | } |
| 2239 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2240 | // Verify targeted injection. |
| 2241 | if (const auto err = verifyTargetedInjection(newTouchedWindowHandle, entry); err) { |
| 2242 | ALOGW("Dropping injected touch event: %s", (*err).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2243 | outInjectionResult = os::InputEventInjectionResult::TARGET_MISMATCH; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2244 | newTouchedWindowHandle = nullptr; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2245 | return {}; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2246 | } |
| 2247 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2248 | // Figure out whether splitting will be allowed for this window. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2249 | if (newTouchedWindowHandle != nullptr) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2250 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
| 2251 | // New window supports splitting, but we should never split mouse events. |
| 2252 | isSplit = !isFromMouse; |
| 2253 | } else if (isSplit) { |
| 2254 | // New window does not support splitting but we have already split events. |
| 2255 | // Ignore the new window. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2256 | newTouchedWindowHandle = nullptr; |
| 2257 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2258 | } else { |
| 2259 | // 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] | 2260 | // be delivered to a new window which supports split touch. Pointers from a mouse device |
| 2261 | // should never be split. |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 2262 | isSplit = !isFromMouse; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2263 | } |
| 2264 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2265 | std::vector<sp<WindowInfoHandle>> newTouchedWindows = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2266 | findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2267 | if (newTouchedWindowHandle != nullptr) { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2268 | // Process the foreground window first so that it is the first to receive the event. |
| 2269 | newTouchedWindows.insert(newTouchedWindows.begin(), newTouchedWindowHandle); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2270 | } |
| 2271 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2272 | if (newTouchedWindows.empty()) { |
| 2273 | ALOGI("Dropping event because there is no touchable window at (%d, %d) on display %d.", |
| 2274 | x, y, displayId); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2275 | outInjectionResult = InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2276 | return {}; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2277 | } |
| 2278 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2279 | for (const sp<WindowInfoHandle>& windowHandle : newTouchedWindows) { |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 2280 | if (!canWindowReceiveMotionLocked(windowHandle, entry)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2281 | continue; |
| 2282 | } |
| 2283 | |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2284 | if (isHoverAction) { |
| 2285 | const int32_t pointerId = entry.pointerProperties[0].id; |
| 2286 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 2287 | // Pointer left. Remove it |
| 2288 | tempTouchState.removeHoveringPointer(entry.deviceId, pointerId); |
| 2289 | } else { |
| 2290 | // The "windowHandle" is the target of this hovering pointer. |
| 2291 | tempTouchState.addHoveringPointerToWindow(windowHandle, entry.deviceId, |
| 2292 | pointerId); |
| 2293 | } |
| 2294 | } |
| 2295 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2296 | // Set target flags. |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2297 | ftl::Flags<InputTarget::Flags> targetFlags = InputTarget::Flags::DISPATCH_AS_IS; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2298 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2299 | if (canReceiveForegroundTouches(*windowHandle->getInfo())) { |
| 2300 | // 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] | 2301 | targetFlags |= InputTarget::Flags::FOREGROUND; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2302 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2303 | |
| 2304 | if (isSplit) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2305 | targetFlags |= InputTarget::Flags::SPLIT; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2306 | } |
| 2307 | if (isWindowObscuredAtPointLocked(windowHandle, x, y)) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2308 | targetFlags |= InputTarget::Flags::WINDOW_IS_OBSCURED; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2309 | } else if (isWindowObscuredLocked(windowHandle)) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2310 | targetFlags |= InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2311 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2312 | |
| 2313 | // Update the temporary touch state. |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 2314 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2315 | if (!isHoverAction) { |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 2316 | pointerIds.set(entry.pointerProperties[pointerIndex].id); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2317 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2318 | |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2319 | const bool isDownOrPointerDown = maskedAction == AMOTION_EVENT_ACTION_DOWN || |
| 2320 | maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN; |
| 2321 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2322 | // TODO(b/211379801): Currently, even if pointerIds are empty (hover case), we would |
| 2323 | // still add a window to the touch state. We should avoid doing that, but some of the |
| 2324 | // later checks ("at least one foreground window") rely on this in order to dispatch |
| 2325 | // 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] | 2326 | tempTouchState.addOrUpdateWindow(windowHandle, targetFlags, pointerIds, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2327 | isDownOrPointerDown |
| 2328 | ? std::make_optional(entry.eventTime) |
| 2329 | : std::nullopt); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2330 | |
| 2331 | // If this is the pointer going down and the touched window has a wallpaper |
| 2332 | // then also add the touched wallpaper windows so they are locked in for the duration |
| 2333 | // of the touch gesture. |
| 2334 | // We do not collect wallpapers during HOVER_MOVE or SCROLL because the wallpaper |
| 2335 | // engine only supports touch events. We would need to add a mechanism similar |
| 2336 | // to View.onGenericMotionEvent to enable wallpapers to handle these events. |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2337 | if (isDownOrPointerDown) { |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2338 | if (targetFlags.test(InputTarget::Flags::FOREGROUND) && |
| 2339 | windowHandle->getInfo()->inputConfig.test( |
| 2340 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER)) { |
| 2341 | sp<WindowInfoHandle> wallpaper = findWallpaperWindowBelow(windowHandle); |
| 2342 | if (wallpaper != nullptr) { |
| 2343 | ftl::Flags<InputTarget::Flags> wallpaperFlags = |
| 2344 | InputTarget::Flags::WINDOW_IS_OBSCURED | |
| 2345 | InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED | |
| 2346 | InputTarget::Flags::DISPATCH_AS_IS; |
| 2347 | if (isSplit) { |
| 2348 | wallpaperFlags |= InputTarget::Flags::SPLIT; |
| 2349 | } |
| 2350 | tempTouchState.addOrUpdateWindow(wallpaper, wallpaperFlags, pointerIds, |
| 2351 | entry.eventTime); |
| 2352 | } |
| 2353 | } |
| 2354 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2355 | } |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 2356 | |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 2357 | // If a window is already pilfering some pointers, give it this new pointer as well and |
| 2358 | // make it pilfering. This will prevent other non-spy windows from getting this pointer, |
| 2359 | // which is a specific behaviour that we want. |
| 2360 | const int32_t pointerId = entry.pointerProperties[pointerIndex].id; |
| 2361 | for (TouchedWindow& touchedWindow : tempTouchState.windows) { |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 2362 | if (touchedWindow.pointerIds.test(pointerId) && |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 2363 | touchedWindow.pilferedPointerIds.count() > 0) { |
| 2364 | // This window is already pilfering some pointers, and this new pointer is also |
| 2365 | // going to it. Therefore, take over this pointer and don't give it to anyone |
| 2366 | // else. |
| 2367 | touchedWindow.pilferedPointerIds.set(pointerId); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 2368 | } |
| 2369 | } |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 2370 | |
| 2371 | // Restrict all pilfered pointers to the pilfering windows. |
| 2372 | tempTouchState.cancelPointersForNonPilferingWindows(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2373 | } else { |
| 2374 | /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ |
| 2375 | |
| 2376 | // If the pointer is not currently down, then ignore the event. |
Siarhei Vishniakou | 3ad385b | 2022-11-04 10:09:53 -0700 | [diff] [blame] | 2377 | if (!tempTouchState.isDown()) { |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2378 | LOG(INFO) << "Dropping event because the pointer is not down or we previously " |
| 2379 | "dropped the pointer down event in display " |
| 2380 | << displayId << ": " << entry.getDescription(); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2381 | outInjectionResult = InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2382 | return {}; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2383 | } |
| 2384 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2385 | addDragEventLocked(entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2386 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2387 | // Check whether touches should slip outside of the current foreground window. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2388 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.pointerCount == 1 && |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2389 | tempTouchState.isSlippery()) { |
Siarhei Vishniakou | 9306c38 | 2022-09-30 15:30:31 -0700 | [diff] [blame] | 2390 | const auto [x, y] = resolveTouchedPosition(entry); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2391 | const bool isStylus = isPointerFromStylus(entry, /*pointerIndex=*/0); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2392 | sp<WindowInfoHandle> oldTouchedWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2393 | tempTouchState.getFirstForegroundWindowHandle(); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2394 | auto [newTouchedWindowHandle, _] = findTouchedWindowAtLocked(displayId, x, y, isStylus); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2395 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2396 | // Verify targeted injection. |
| 2397 | if (const auto err = verifyTargetedInjection(newTouchedWindowHandle, entry); err) { |
| 2398 | ALOGW("Dropping injected event: %s", (*err).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2399 | outInjectionResult = os::InputEventInjectionResult::TARGET_MISMATCH; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2400 | return {}; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2401 | } |
| 2402 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2403 | // Drop touch events if requested by input feature |
| 2404 | if (newTouchedWindowHandle != nullptr && |
| 2405 | shouldDropInput(entry, newTouchedWindowHandle)) { |
| 2406 | newTouchedWindowHandle = nullptr; |
| 2407 | } |
| 2408 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2409 | if (oldTouchedWindowHandle != newTouchedWindowHandle && |
| 2410 | oldTouchedWindowHandle != nullptr && newTouchedWindowHandle != nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2411 | if (DEBUG_FOCUS) { |
| 2412 | ALOGD("Touch is slipping out of window %s into window %s in display %" PRId32, |
| 2413 | oldTouchedWindowHandle->getName().c_str(), |
| 2414 | newTouchedWindowHandle->getName().c_str(), displayId); |
| 2415 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2416 | // Make a slippery exit from the old window. |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 2417 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2418 | const int32_t pointerId = entry.pointerProperties[0].id; |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 2419 | pointerIds.set(pointerId); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2420 | |
| 2421 | const TouchedWindow& touchedWindow = |
| 2422 | tempTouchState.getTouchedWindow(oldTouchedWindowHandle); |
| 2423 | addWindowTargetLocked(oldTouchedWindowHandle, |
| 2424 | InputTarget::Flags::DISPATCH_AS_SLIPPERY_EXIT, pointerIds, |
| 2425 | touchedWindow.firstDownTimeInTarget, targets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2426 | |
| 2427 | // Make a slippery entrance into the new window. |
| 2428 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
Prabir Pradhan | 713bb3e | 2021-12-20 02:07:40 -0800 | [diff] [blame] | 2429 | isSplit = !isFromMouse; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2430 | } |
| 2431 | |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2432 | ftl::Flags<InputTarget::Flags> targetFlags = |
| 2433 | InputTarget::Flags::DISPATCH_AS_SLIPPERY_ENTER; |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2434 | if (canReceiveForegroundTouches(*newTouchedWindowHandle->getInfo())) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2435 | targetFlags |= InputTarget::Flags::FOREGROUND; |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2436 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2437 | if (isSplit) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2438 | targetFlags |= InputTarget::Flags::SPLIT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2439 | } |
| 2440 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2441 | targetFlags |= InputTarget::Flags::WINDOW_IS_OBSCURED; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2442 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2443 | targetFlags |= InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2444 | } |
| 2445 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2446 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds, |
| 2447 | entry.eventTime); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2448 | |
| 2449 | // Check if the wallpaper window should deliver the corresponding event. |
| 2450 | slipWallpaperTouch(targetFlags, oldTouchedWindowHandle, newTouchedWindowHandle, |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2451 | tempTouchState, pointerId, targets); |
| 2452 | tempTouchState.removeTouchedPointerFromWindow(pointerId, oldTouchedWindowHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2453 | } |
| 2454 | } |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 2455 | |
| 2456 | // Update the pointerIds for non-splittable when it received pointer down. |
| 2457 | if (!isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN) { |
| 2458 | // If no split, we suppose all touched windows should receive pointer down. |
| 2459 | const int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
| 2460 | for (size_t i = 0; i < tempTouchState.windows.size(); i++) { |
| 2461 | TouchedWindow& touchedWindow = tempTouchState.windows[i]; |
| 2462 | // Ignore drag window for it should just track one pointer. |
| 2463 | if (mDragState && mDragState->dragWindow == touchedWindow.windowHandle) { |
| 2464 | continue; |
| 2465 | } |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 2466 | touchedWindow.pointerIds.set(entry.pointerProperties[pointerIndex].id); |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 2467 | } |
| 2468 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2469 | } |
| 2470 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2471 | // Update dispatching for hover enter and exit. |
Sam Dubey | f886dec | 2023-01-27 13:28:19 +0000 | [diff] [blame] | 2472 | { |
| 2473 | std::vector<TouchedWindow> hoveringWindows = |
| 2474 | getHoveringWindowsLocked(oldState, tempTouchState, entry); |
| 2475 | for (const TouchedWindow& touchedWindow : hoveringWindows) { |
| 2476 | addWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags, |
| 2477 | touchedWindow.pointerIds, touchedWindow.firstDownTimeInTarget, |
| 2478 | targets); |
| 2479 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2480 | } |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2481 | // Ensure that we have at least one foreground window or at least one window that cannot be a |
| 2482 | // foreground target. If we only have windows that are not receiving foreground touches (e.g. we |
| 2483 | // only have windows getting ACTION_OUTSIDE), then drop the event, because there is no window |
| 2484 | // that is actually receiving the entire gesture. |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2485 | if (std::none_of(tempTouchState.windows.begin(), tempTouchState.windows.end(), |
| 2486 | [](const TouchedWindow& touchedWindow) { |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2487 | return !canReceiveForegroundTouches( |
| 2488 | *touchedWindow.windowHandle->getInfo()) || |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2489 | touchedWindow.targetFlags.test(InputTarget::Flags::FOREGROUND); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2490 | })) { |
Siarhei Vishniakou | 1fb1891 | 2022-03-08 10:31:39 -0800 | [diff] [blame] | 2491 | ALOGI("Dropping event because there is no touched window on display %d to receive it: %s", |
| 2492 | displayId, entry.getDescription().c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2493 | outInjectionResult = InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2494 | return {}; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2495 | } |
| 2496 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2497 | // Ensure that all touched windows are valid for injection. |
| 2498 | if (entry.injectionState != nullptr) { |
| 2499 | std::string errs; |
| 2500 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2501 | if (touchedWindow.targetFlags.test(InputTarget::Flags::DISPATCH_AS_OUTSIDE)) { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2502 | // Allow ACTION_OUTSIDE events generated by targeted injection to be |
| 2503 | // dispatched to any uid, since the coords will be zeroed out later. |
| 2504 | continue; |
| 2505 | } |
| 2506 | const auto err = verifyTargetedInjection(touchedWindow.windowHandle, entry); |
| 2507 | if (err) errs += "\n - " + *err; |
| 2508 | } |
| 2509 | if (!errs.empty()) { |
| 2510 | ALOGW("Dropping targeted injection: At least one touched window is not owned by uid " |
| 2511 | "%d:%s", |
| 2512 | *entry.injectionState->targetUid, errs.c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2513 | outInjectionResult = InputEventInjectionResult::TARGET_MISMATCH; |
Siarhei Vishniakou | 8619eb3 | 2022-12-01 21:30:59 -0800 | [diff] [blame] | 2514 | return {}; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2515 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2516 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2517 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2518 | // Check whether windows listening for outside touches are owned by the same UID. If the owner |
| 2519 | // 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] | 2520 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2521 | sp<WindowInfoHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2522 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2523 | if (foregroundWindowHandle) { |
| 2524 | const int32_t foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2525 | for (InputTarget& target : targets) { |
| 2526 | if (target.flags.test(InputTarget::Flags::DISPATCH_AS_OUTSIDE)) { |
| 2527 | sp<WindowInfoHandle> targetWindow = |
| 2528 | getWindowHandleLocked(target.inputChannel->getConnectionToken()); |
| 2529 | if (targetWindow->getInfo()->ownerUid != foregroundWindowUid) { |
| 2530 | target.flags |= InputTarget::Flags::ZERO_COORDS; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2531 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2532 | } |
| 2533 | } |
| 2534 | } |
| 2535 | } |
| 2536 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2537 | // Success! Output targets from the touch state. |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2538 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 2539 | if (touchedWindow.pointerIds.none() && !touchedWindow.hasHoveringPointers(entry.deviceId)) { |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2540 | // Windows with hovering pointers are getting persisted inside TouchState. |
| 2541 | // Do not send this event to those windows. |
| 2542 | continue; |
| 2543 | } |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2544 | addWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags, |
| 2545 | touchedWindow.pointerIds, touchedWindow.firstDownTimeInTarget, |
| 2546 | targets); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2547 | } |
Sam Dubey | 39d37cf | 2022-12-07 18:05:35 +0000 | [diff] [blame] | 2548 | |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2549 | outInjectionResult = InputEventInjectionResult::SUCCEEDED; |
Sam Dubey | f886dec | 2023-01-27 13:28:19 +0000 | [diff] [blame] | 2550 | // Drop the outside or hover touch windows since we will not care about them |
| 2551 | // in the next iteration. |
| 2552 | tempTouchState.filterNonAsIsTouchWindows(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2553 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2554 | // Update final pieces of touch state if the injector had permission. |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2555 | if (switchedDevice) { |
| 2556 | if (DEBUG_FOCUS) { |
| 2557 | ALOGD("Conflicting pointer actions: Switched to a different device."); |
| 2558 | } |
| 2559 | *outConflictingPointerActions = true; |
| 2560 | } |
| 2561 | |
| 2562 | if (isHoverAction) { |
| 2563 | // Started hovering, therefore no longer down. |
Siarhei Vishniakou | 3ad385b | 2022-11-04 10:09:53 -0700 | [diff] [blame] | 2564 | if (oldState && oldState->isDown()) { |
Siarhei Vishniakou | 6e1e987 | 2022-11-08 17:51:35 -0800 | [diff] [blame] | 2565 | ALOGD_IF(DEBUG_FOCUS, |
| 2566 | "Conflicting pointer actions: Hover received while pointer was down."); |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2567 | *outConflictingPointerActions = true; |
| 2568 | } |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2569 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2570 | maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) { |
| 2571 | tempTouchState.deviceId = entry.deviceId; |
| 2572 | tempTouchState.source = entry.source; |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2573 | } |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2574 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP) { |
| 2575 | // Pointer went up. |
| 2576 | tempTouchState.removeTouchedPointer(entry.pointerProperties[0].id); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2577 | } else if (maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2578 | // All pointers up or canceled. |
| 2579 | tempTouchState.reset(); |
| 2580 | } else if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 2581 | // First pointer went down. |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2582 | if (oldState && (oldState->isDown() || oldState->hasHoveringPointers())) { |
| 2583 | ALOGD("Conflicting pointer actions: Down received while already down or hovering."); |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2584 | *outConflictingPointerActions = true; |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2585 | } |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2586 | } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 2587 | // One pointer went up. |
| 2588 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
| 2589 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2590 | |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2591 | for (size_t i = 0; i < tempTouchState.windows.size();) { |
| 2592 | TouchedWindow& touchedWindow = tempTouchState.windows[i]; |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 2593 | touchedWindow.pointerIds.reset(pointerId); |
| 2594 | if (touchedWindow.pointerIds.none()) { |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2595 | tempTouchState.windows.erase(tempTouchState.windows.begin() + i); |
| 2596 | continue; |
| 2597 | } |
| 2598 | i += 1; |
| 2599 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2600 | } |
| 2601 | |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2602 | // Save changes unless the action was scroll in which case the temporary touch |
| 2603 | // state was only valid for this one action. |
| 2604 | if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) { |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 2605 | if (displayId >= 0) { |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2606 | tempTouchState.clearWindowsWithoutPointers(); |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2607 | mTouchStatesByDisplay[displayId] = tempTouchState; |
| 2608 | } else { |
| 2609 | mTouchStatesByDisplay.erase(displayId); |
| 2610 | } |
| 2611 | } |
| 2612 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 2613 | if (tempTouchState.windows.empty()) { |
| 2614 | mTouchStatesByDisplay.erase(displayId); |
| 2615 | } |
| 2616 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2617 | return targets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2618 | } |
| 2619 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2620 | void InputDispatcher::finishDragAndDrop(int32_t displayId, float x, float y) { |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2621 | // Prevent stylus interceptor windows from affecting drag and drop behavior for now, until we |
| 2622 | // have an explicit reason to support it. |
| 2623 | constexpr bool isStylus = false; |
| 2624 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2625 | auto [dropWindow, _] = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2626 | findTouchedWindowAtLocked(displayId, x, y, isStylus, /*ignoreDragWindow=*/true); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2627 | if (dropWindow) { |
| 2628 | vec2 local = dropWindow->getInfo()->transform.transform(x, y); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2629 | sendDropWindowCommandLocked(dropWindow->getToken(), local.x, local.y); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 2630 | } else { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2631 | ALOGW("No window found when drop."); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2632 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2633 | } |
| 2634 | mDragState.reset(); |
| 2635 | } |
| 2636 | |
| 2637 | void InputDispatcher::addDragEventLocked(const MotionEntry& entry) { |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 2638 | if (!mDragState || mDragState->dragWindow->getInfo()->displayId != entry.displayId) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2639 | return; |
| 2640 | } |
| 2641 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2642 | if (!mDragState->isStartDrag) { |
| 2643 | mDragState->isStartDrag = true; |
| 2644 | mDragState->isStylusButtonDownAtStart = |
| 2645 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2646 | } |
| 2647 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2648 | // Find the pointer index by id. |
| 2649 | int32_t pointerIndex = 0; |
| 2650 | for (; static_cast<uint32_t>(pointerIndex) < entry.pointerCount; pointerIndex++) { |
| 2651 | const PointerProperties& pointerProperties = entry.pointerProperties[pointerIndex]; |
| 2652 | if (pointerProperties.id == mDragState->pointerId) { |
| 2653 | break; |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2654 | } |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2655 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2656 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2657 | if (uint32_t(pointerIndex) == entry.pointerCount) { |
| 2658 | 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] | 2659 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2660 | mDragState.reset(); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2661 | return; |
| 2662 | } |
| 2663 | |
| 2664 | const int32_t maskedAction = entry.action & AMOTION_EVENT_ACTION_MASK; |
| 2665 | const int32_t x = entry.pointerCoords[pointerIndex].getX(); |
| 2666 | const int32_t y = entry.pointerCoords[pointerIndex].getY(); |
| 2667 | |
| 2668 | switch (maskedAction) { |
| 2669 | case AMOTION_EVENT_ACTION_MOVE: { |
| 2670 | // Handle the special case : stylus button no longer pressed. |
| 2671 | bool isStylusButtonDown = |
| 2672 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2673 | if (mDragState->isStylusButtonDownAtStart && !isStylusButtonDown) { |
| 2674 | finishDragAndDrop(entry.displayId, x, y); |
| 2675 | return; |
| 2676 | } |
| 2677 | |
| 2678 | // Prevent stylus interceptor windows from affecting drag and drop behavior for now, |
| 2679 | // until we have an explicit reason to support it. |
| 2680 | constexpr bool isStylus = false; |
| 2681 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 2682 | auto [hoverWindowHandle, _] = findTouchedWindowAtLocked(entry.displayId, x, y, isStylus, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2683 | /*ignoreDragWindow=*/true); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2684 | // enqueue drag exit if needed. |
| 2685 | if (hoverWindowHandle != mDragState->dragHoverWindowHandle && |
| 2686 | !haveSameToken(hoverWindowHandle, mDragState->dragHoverWindowHandle)) { |
| 2687 | if (mDragState->dragHoverWindowHandle != nullptr) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2688 | enqueueDragEventLocked(mDragState->dragHoverWindowHandle, /*isExiting=*/true, x, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2689 | y); |
| 2690 | } |
| 2691 | mDragState->dragHoverWindowHandle = hoverWindowHandle; |
| 2692 | } |
| 2693 | // enqueue drag location if needed. |
| 2694 | if (hoverWindowHandle != nullptr) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2695 | enqueueDragEventLocked(hoverWindowHandle, /*isExiting=*/false, x, y); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2696 | } |
| 2697 | break; |
| 2698 | } |
| 2699 | |
| 2700 | case AMOTION_EVENT_ACTION_POINTER_UP: |
| 2701 | if (getMotionEventActionPointerIndex(entry.action) != pointerIndex) { |
| 2702 | break; |
| 2703 | } |
| 2704 | // The drag pointer is up. |
| 2705 | [[fallthrough]]; |
| 2706 | case AMOTION_EVENT_ACTION_UP: |
| 2707 | finishDragAndDrop(entry.displayId, x, y); |
| 2708 | break; |
| 2709 | case AMOTION_EVENT_ACTION_CANCEL: { |
| 2710 | ALOGD("Receiving cancel when drag and drop."); |
| 2711 | sendDropWindowCommandLocked(nullptr, 0, 0); |
| 2712 | mDragState.reset(); |
| 2713 | break; |
| 2714 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2715 | } |
| 2716 | } |
| 2717 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2718 | void InputDispatcher::addWindowTargetLocked(const sp<WindowInfoHandle>& windowHandle, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2719 | ftl::Flags<InputTarget::Flags> targetFlags, |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 2720 | std::bitset<MAX_POINTER_ID + 1> pointerIds, |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2721 | std::optional<nsecs_t> firstDownTimeInTarget, |
Siarhei Vishniakou | f75cddb | 2022-10-25 10:42:16 -0700 | [diff] [blame] | 2722 | std::vector<InputTarget>& inputTargets) const { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2723 | std::vector<InputTarget>::iterator it = |
| 2724 | std::find_if(inputTargets.begin(), inputTargets.end(), |
| 2725 | [&windowHandle](const InputTarget& inputTarget) { |
| 2726 | return inputTarget.inputChannel->getConnectionToken() == |
| 2727 | windowHandle->getToken(); |
| 2728 | }); |
Chavi Weingarten | 97b8eec | 2020-01-09 18:09:08 +0000 | [diff] [blame] | 2729 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2730 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2731 | |
| 2732 | if (it == inputTargets.end()) { |
| 2733 | InputTarget inputTarget; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 2734 | std::shared_ptr<InputChannel> inputChannel = |
| 2735 | getInputChannelLocked(windowHandle->getToken()); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2736 | if (inputChannel == nullptr) { |
| 2737 | ALOGW("Window %s already unregistered input channel", windowHandle->getName().c_str()); |
| 2738 | return; |
| 2739 | } |
| 2740 | inputTarget.inputChannel = inputChannel; |
| 2741 | inputTarget.flags = targetFlags; |
| 2742 | inputTarget.globalScaleFactor = windowInfo->globalScaleFactor; |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2743 | inputTarget.firstDownTimeInTarget = firstDownTimeInTarget; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2744 | const auto& displayInfoIt = mDisplayInfos.find(windowInfo->displayId); |
| 2745 | if (displayInfoIt != mDisplayInfos.end()) { |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 2746 | inputTarget.displayTransform = displayInfoIt->second.transform; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2747 | } else { |
Siarhei Vishniakou | a06bb55 | 2023-02-07 09:38:56 -0800 | [diff] [blame] | 2748 | // DisplayInfo not found for this window on display windowInfo->displayId. |
| 2749 | // 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] | 2750 | } |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2751 | inputTargets.push_back(inputTarget); |
| 2752 | it = inputTargets.end() - 1; |
| 2753 | } |
| 2754 | |
| 2755 | ALOG_ASSERT(it->flags == targetFlags); |
| 2756 | ALOG_ASSERT(it->globalScaleFactor == windowInfo->globalScaleFactor); |
| 2757 | |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2758 | it->addPointers(pointerIds, windowInfo->transform); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2759 | } |
| 2760 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2761 | void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2762 | int32_t displayId) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2763 | auto monitorsIt = mGlobalMonitorsByDisplay.find(displayId); |
| 2764 | if (monitorsIt == mGlobalMonitorsByDisplay.end()) return; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2765 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2766 | for (const Monitor& monitor : selectResponsiveMonitorsLocked(monitorsIt->second)) { |
| 2767 | InputTarget target; |
| 2768 | target.inputChannel = monitor.inputChannel; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2769 | target.flags = InputTarget::Flags::DISPATCH_AS_IS; |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2770 | // target.firstDownTimeInTarget is not set for global monitors. It is only required in split |
| 2771 | // touch and global monitoring works as intended even without setting firstDownTimeInTarget |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2772 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
| 2773 | target.displayTransform = it->second.transform; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2774 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2775 | target.setDefaultPointerTransform(target.displayTransform); |
| 2776 | inputTargets.push_back(target); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2777 | } |
| 2778 | } |
| 2779 | |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2780 | /** |
| 2781 | * Indicate whether one window handle should be considered as obscuring |
| 2782 | * another window handle. We only check a few preconditions. Actually |
| 2783 | * checking the bounds is left to the caller. |
| 2784 | */ |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2785 | static bool canBeObscuredBy(const sp<WindowInfoHandle>& windowHandle, |
| 2786 | const sp<WindowInfoHandle>& otherHandle) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2787 | // Compare by token so cloned layers aren't counted |
| 2788 | if (haveSameToken(windowHandle, otherHandle)) { |
| 2789 | return false; |
| 2790 | } |
| 2791 | auto info = windowHandle->getInfo(); |
| 2792 | auto otherInfo = otherHandle->getInfo(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2793 | if (otherInfo->inputConfig.test(WindowInfo::InputConfig::NOT_VISIBLE)) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2794 | return false; |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2795 | } else if (otherInfo->alpha == 0 && |
| 2796 | otherInfo->inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE)) { |
Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame] | 2797 | // Those act as if they were invisible, so we don't need to flag them. |
| 2798 | // We do want to potentially flag touchable windows even if they have 0 |
| 2799 | // opacity, since they can consume touches and alter the effects of the |
| 2800 | // user interaction (eg. apps that rely on |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 2801 | // Flags::WINDOW_IS_PARTIALLY_OBSCURED should still be told about those |
Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame] | 2802 | // windows), hence we also check for FLAG_NOT_TOUCHABLE. |
| 2803 | return false; |
Bernardo Rufino | 8007daf | 2020-09-22 09:40:01 +0000 | [diff] [blame] | 2804 | } else if (info->ownerUid == otherInfo->ownerUid) { |
| 2805 | // If ownerUid is the same we don't generate occlusion events as there |
| 2806 | // is no security boundary within an uid. |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2807 | return false; |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2808 | } else if (otherInfo->inputConfig.test(gui::WindowInfo::InputConfig::TRUSTED_OVERLAY)) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2809 | return false; |
| 2810 | } else if (otherInfo->displayId != info->displayId) { |
| 2811 | return false; |
| 2812 | } |
| 2813 | return true; |
| 2814 | } |
| 2815 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2816 | /** |
| 2817 | * Returns touch occlusion information in the form of TouchOcclusionInfo. To check if the touch is |
| 2818 | * untrusted, one should check: |
| 2819 | * |
| 2820 | * 1. If result.hasBlockingOcclusion is true. |
| 2821 | * If it's, it means the touch should be blocked due to a window with occlusion mode of |
| 2822 | * BLOCK_UNTRUSTED. |
| 2823 | * |
| 2824 | * 2. If result.obscuringOpacity > mMaximumObscuringOpacityForTouch. |
| 2825 | * If it is (and 1 is false), then the touch should be blocked because a stack of windows |
| 2826 | * (possibly only one) with occlusion mode of USE_OPACITY from one UID resulted in a composed |
| 2827 | * obscuring opacity above the threshold. Note that if there was no window of occlusion mode |
| 2828 | * USE_OPACITY, result.obscuringOpacity would've been 0 and since |
| 2829 | * mMaximumObscuringOpacityForTouch >= 0, the condition above would never be true. |
| 2830 | * |
| 2831 | * If neither of those is true, then it means the touch can be allowed. |
| 2832 | */ |
| 2833 | InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2834 | const sp<WindowInfoHandle>& windowHandle, int32_t x, int32_t y) const { |
| 2835 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2836 | int32_t displayId = windowInfo->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2837 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2838 | TouchOcclusionInfo info; |
| 2839 | info.hasBlockingOcclusion = false; |
| 2840 | info.obscuringOpacity = 0; |
| 2841 | info.obscuringUid = -1; |
| 2842 | std::map<int32_t, float> opacityByUid; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2843 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2844 | if (windowHandle == otherHandle) { |
| 2845 | break; // All future windows are below us. Exit early. |
| 2846 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2847 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 2848 | if (canBeObscuredBy(windowHandle, otherHandle) && otherInfo->frameContainsPoint(x, y) && |
| 2849 | !haveSameApplicationToken(windowInfo, otherInfo)) { |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2850 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2851 | info.debugInfo.push_back( |
| 2852 | dumpWindowForTouchOcclusion(otherInfo, /* isTouchedWindow */ false)); |
| 2853 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2854 | // canBeObscuredBy() has returned true above, which means this window is untrusted, so |
| 2855 | // we perform the checks below to see if the touch can be propagated or not based on the |
| 2856 | // window's touch occlusion mode |
| 2857 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::BLOCK_UNTRUSTED) { |
| 2858 | info.hasBlockingOcclusion = true; |
| 2859 | info.obscuringUid = otherInfo->ownerUid; |
| 2860 | info.obscuringPackage = otherInfo->packageName; |
| 2861 | break; |
| 2862 | } |
| 2863 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::USE_OPACITY) { |
| 2864 | uint32_t uid = otherInfo->ownerUid; |
| 2865 | float opacity = |
| 2866 | (opacityByUid.find(uid) == opacityByUid.end()) ? 0 : opacityByUid[uid]; |
| 2867 | // Given windows A and B: |
| 2868 | // opacity(A, B) = 1 - [1 - opacity(A)] * [1 - opacity(B)] |
| 2869 | opacity = 1 - (1 - opacity) * (1 - otherInfo->alpha); |
| 2870 | opacityByUid[uid] = opacity; |
| 2871 | if (opacity > info.obscuringOpacity) { |
| 2872 | info.obscuringOpacity = opacity; |
| 2873 | info.obscuringUid = uid; |
| 2874 | info.obscuringPackage = otherInfo->packageName; |
| 2875 | } |
| 2876 | } |
| 2877 | } |
| 2878 | } |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2879 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2880 | info.debugInfo.push_back( |
| 2881 | dumpWindowForTouchOcclusion(windowInfo, /* isTouchedWindow */ true)); |
| 2882 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2883 | return info; |
| 2884 | } |
| 2885 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2886 | std::string InputDispatcher::dumpWindowForTouchOcclusion(const WindowInfo* info, |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2887 | bool isTouchedWindow) const { |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 2888 | return StringPrintf(INDENT2 "* %spackage=%s/%" PRId32 ", id=%" PRId32 ", mode=%s, alpha=%.2f, " |
| 2889 | "frame=[%" PRId32 ",%" PRId32 "][%" PRId32 ",%" PRId32 |
| 2890 | "], touchableRegion=%s, window={%s}, inputConfig={%s}, " |
| 2891 | "hasToken=%s, applicationInfo.name=%s, applicationInfo.token=%s\n", |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2892 | isTouchedWindow ? "[TOUCHED] " : "", info->packageName.c_str(), |
| 2893 | info->ownerUid, info->id, toString(info->touchOcclusionMode).c_str(), |
| 2894 | info->alpha, info->frameLeft, info->frameTop, info->frameRight, |
| 2895 | info->frameBottom, dumpRegion(info->touchableRegion).c_str(), |
| 2896 | info->name.c_str(), info->inputConfig.string().c_str(), |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 2897 | toString(info->token != nullptr), info->applicationInfo.name.c_str(), |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 2898 | toString(info->applicationInfo.token).c_str()); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2899 | } |
| 2900 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2901 | bool InputDispatcher::isTouchTrustedLocked(const TouchOcclusionInfo& occlusionInfo) const { |
| 2902 | if (occlusionInfo.hasBlockingOcclusion) { |
| 2903 | ALOGW("Untrusted touch due to occlusion by %s/%d", occlusionInfo.obscuringPackage.c_str(), |
| 2904 | occlusionInfo.obscuringUid); |
| 2905 | return false; |
| 2906 | } |
| 2907 | if (occlusionInfo.obscuringOpacity > mMaximumObscuringOpacityForTouch) { |
| 2908 | ALOGW("Untrusted touch due to occlusion by %s/%d (obscuring opacity = " |
| 2909 | "%.2f, maximum allowed = %.2f)", |
| 2910 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid, |
| 2911 | occlusionInfo.obscuringOpacity, mMaximumObscuringOpacityForTouch); |
| 2912 | return false; |
| 2913 | } |
| 2914 | return true; |
| 2915 | } |
| 2916 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2917 | bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2918 | int32_t x, int32_t y) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2919 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2920 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 2921 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2922 | if (windowHandle == otherHandle) { |
| 2923 | break; // All future windows are below us. Exit early. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2924 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2925 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2926 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2927 | otherInfo->frameContainsPoint(x, y)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2928 | return true; |
| 2929 | } |
| 2930 | } |
| 2931 | return false; |
| 2932 | } |
| 2933 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2934 | bool InputDispatcher::isWindowObscuredLocked(const sp<WindowInfoHandle>& windowHandle) const { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2935 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2936 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 2937 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 2938 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2939 | if (windowHandle == otherHandle) { |
| 2940 | break; // All future windows are below us. Exit early. |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2941 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2942 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2943 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2944 | otherInfo->overlaps(windowInfo)) { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2945 | return true; |
| 2946 | } |
| 2947 | } |
| 2948 | return false; |
| 2949 | } |
| 2950 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2951 | std::string InputDispatcher::getApplicationWindowLabel( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2952 | const InputApplicationHandle* applicationHandle, const sp<WindowInfoHandle>& windowHandle) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2953 | if (applicationHandle != nullptr) { |
| 2954 | if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2955 | return applicationHandle->getName() + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2956 | } else { |
| 2957 | return applicationHandle->getName(); |
| 2958 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2959 | } else if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2960 | return windowHandle->getInfo()->applicationInfo.name + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2961 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2962 | return "<unknown application or window>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2963 | } |
| 2964 | } |
| 2965 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2966 | void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 2967 | if (!isUserActivityEvent(eventEntry)) { |
| 2968 | // 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] | 2969 | return; |
| 2970 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2971 | int32_t displayId = getTargetDisplayId(eventEntry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2972 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2973 | if (focusedWindowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2974 | const WindowInfo* info = focusedWindowHandle->getInfo(); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 2975 | if (info->inputConfig.test(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2976 | if (DEBUG_DISPATCH_CYCLE) { |
| 2977 | ALOGD("Not poking user activity: disabled by window '%s'.", info->name.c_str()); |
| 2978 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2979 | return; |
| 2980 | } |
| 2981 | } |
| 2982 | |
| 2983 | int32_t eventType = USER_ACTIVITY_EVENT_OTHER; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2984 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2985 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2986 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 2987 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2988 | return; |
| 2989 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2990 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2991 | if (MotionEvent::isTouchEvent(motionEntry.source, motionEntry.action)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2992 | eventType = USER_ACTIVITY_EVENT_TOUCH; |
| 2993 | } |
| 2994 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2995 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2996 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2997 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 2998 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2999 | return; |
| 3000 | } |
| 3001 | eventType = USER_ACTIVITY_EVENT_BUTTON; |
| 3002 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3003 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3004 | default: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3005 | LOG_ALWAYS_FATAL("%s events are not user activity", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3006 | ftl::enum_string(eventEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3007 | break; |
| 3008 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3009 | } |
| 3010 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3011 | auto command = [this, eventTime = eventEntry.eventTime, eventType, displayId]() |
| 3012 | REQUIRES(mLock) { |
| 3013 | scoped_unlock unlock(mLock); |
| 3014 | mPolicy->pokeUserActivity(eventTime, eventType, displayId); |
| 3015 | }; |
| 3016 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3017 | } |
| 3018 | |
| 3019 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3020 | const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3021 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3022 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3023 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3024 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3025 | StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3026 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3027 | ATRACE_NAME(message.c_str()); |
| 3028 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3029 | if (DEBUG_DISPATCH_CYCLE) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3030 | ALOGD("channel '%s' ~ prepareDispatchCycle - flags=%s, " |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 3031 | "globalScaleFactor=%f, pointerIds=%s %s", |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3032 | connection->getInputChannelName().c_str(), inputTarget.flags.string().c_str(), |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 3033 | inputTarget.globalScaleFactor, bitsetToString(inputTarget.pointerIds).c_str(), |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3034 | inputTarget.getPointerInfoString().c_str()); |
| 3035 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3036 | |
| 3037 | // Skip this event if the connection status is not normal. |
| 3038 | // 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] | 3039 | if (connection->status != Connection::Status::NORMAL) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3040 | if (DEBUG_DISPATCH_CYCLE) { |
| 3041 | ALOGD("channel '%s' ~ Dropping event because the channel status is %s", |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3042 | connection->getInputChannelName().c_str(), |
| 3043 | ftl::enum_string(connection->status).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3044 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3045 | return; |
| 3046 | } |
| 3047 | |
| 3048 | // Split a motion event if needed. |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3049 | if (inputTarget.flags.test(InputTarget::Flags::SPLIT)) { |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3050 | LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3051 | "Entry type %s should not have Flags::SPLIT", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3052 | ftl::enum_string(eventEntry->type).c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3053 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3054 | const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry); |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3055 | if (inputTarget.pointerIds.count() != originalMotionEntry.pointerCount) { |
Siarhei Vishniakou | 16e4fa0 | 2023-02-16 17:48:56 -0800 | [diff] [blame] | 3056 | if (!inputTarget.firstDownTimeInTarget.has_value()) { |
| 3057 | logDispatchStateLocked(); |
| 3058 | LOG(FATAL) << "Splitting motion events requires a down time to be set for the " |
| 3059 | "target on connection " |
| 3060 | << connection->getInputChannelName() << " for " |
| 3061 | << originalMotionEntry.getDescription(); |
| 3062 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3063 | std::unique_ptr<MotionEntry> splitMotionEntry = |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3064 | splitMotionEvent(originalMotionEntry, inputTarget.pointerIds, |
| 3065 | inputTarget.firstDownTimeInTarget.value()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3066 | if (!splitMotionEntry) { |
| 3067 | return; // split event was dropped |
| 3068 | } |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3069 | if (splitMotionEntry->action == AMOTION_EVENT_ACTION_CANCEL) { |
| 3070 | std::string reason = std::string("reason=pointer cancel on split window"); |
| 3071 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 3072 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 3073 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3074 | if (DEBUG_FOCUS) { |
| 3075 | ALOGD("channel '%s' ~ Split motion event.", |
| 3076 | connection->getInputChannelName().c_str()); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3077 | logOutboundMotionDetails(" ", *splitMotionEntry); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 3078 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3079 | enqueueDispatchEntriesLocked(currentTime, connection, std::move(splitMotionEntry), |
| 3080 | inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3081 | return; |
| 3082 | } |
| 3083 | } |
| 3084 | |
| 3085 | // Not splitting. Enqueue dispatch entries for the event as is. |
| 3086 | enqueueDispatchEntriesLocked(currentTime, connection, eventEntry, inputTarget); |
| 3087 | } |
| 3088 | |
| 3089 | void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3090 | const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3091 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3092 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3093 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3094 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3095 | StringPrintf("enqueueDispatchEntriesLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3096 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3097 | ATRACE_NAME(message.c_str()); |
| 3098 | } |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3099 | LOG_ALWAYS_FATAL_IF(!inputTarget.flags.any(InputTarget::DISPATCH_MASK), |
| 3100 | "No dispatch flags are set for %s", eventEntry->getDescription().c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3101 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 3102 | const bool wasEmpty = connection->outboundQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3103 | |
| 3104 | // Enqueue dispatch entries for the requested modes. |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3105 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3106 | InputTarget::Flags::DISPATCH_AS_HOVER_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3107 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3108 | InputTarget::Flags::DISPATCH_AS_OUTSIDE); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3109 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3110 | InputTarget::Flags::DISPATCH_AS_HOVER_ENTER); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3111 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3112 | InputTarget::Flags::DISPATCH_AS_IS); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3113 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3114 | InputTarget::Flags::DISPATCH_AS_SLIPPERY_EXIT); |
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_SLIPPERY_ENTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3117 | |
| 3118 | // If the outbound queue was previously empty, start the dispatch cycle going. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3119 | if (wasEmpty && !connection->outboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3120 | startDispatchCycleLocked(currentTime, connection); |
| 3121 | } |
| 3122 | } |
| 3123 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3124 | void InputDispatcher::enqueueDispatchEntryLocked(const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3125 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3126 | const InputTarget& inputTarget, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3127 | ftl::Flags<InputTarget::Flags> dispatchMode) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3128 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3129 | std::string message = StringPrintf("enqueueDispatchEntry(inputChannel=%s, dispatchMode=%s)", |
| 3130 | connection->getInputChannelName().c_str(), |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3131 | dispatchMode.string().c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3132 | ATRACE_NAME(message.c_str()); |
| 3133 | } |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3134 | ftl::Flags<InputTarget::Flags> inputTargetFlags = inputTarget.flags; |
| 3135 | if (!inputTargetFlags.any(dispatchMode)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3136 | return; |
| 3137 | } |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3138 | |
| 3139 | inputTargetFlags.clear(InputTarget::DISPATCH_MASK); |
| 3140 | inputTargetFlags |= dispatchMode; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3141 | |
| 3142 | // This is a new event. |
| 3143 | // Enqueue a new dispatch entry onto the outbound queue for this connection. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3144 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3145 | createDispatchEntry(inputTarget, eventEntry, inputTargetFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3146 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3147 | // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a |
| 3148 | // different EventEntry than what was passed in. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3149 | EventEntry& newEntry = *(dispatchEntry->eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3150 | // Apply target flags and update the connection's input state. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3151 | switch (newEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3152 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3153 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3154 | dispatchEntry->resolvedEventId = keyEntry.id; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3155 | dispatchEntry->resolvedAction = keyEntry.action; |
| 3156 | dispatchEntry->resolvedFlags = keyEntry.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3157 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3158 | if (!connection->inputState.trackKey(keyEntry, dispatchEntry->resolvedAction, |
| 3159 | dispatchEntry->resolvedFlags)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3160 | if (DEBUG_DISPATCH_CYCLE) { |
| 3161 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent key " |
| 3162 | "event", |
| 3163 | connection->getInputChannelName().c_str()); |
| 3164 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3165 | return; // skip the inconsistent event |
| 3166 | } |
| 3167 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3168 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3169 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3170 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3171 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3172 | // Assign a default value to dispatchEntry that will never be generated by InputReader, |
| 3173 | // and assign a InputDispatcher value if it doesn't change in the if-else chain below. |
| 3174 | constexpr int32_t DEFAULT_RESOLVED_EVENT_ID = |
| 3175 | static_cast<int32_t>(IdGenerator::Source::OTHER); |
| 3176 | dispatchEntry->resolvedEventId = DEFAULT_RESOLVED_EVENT_ID; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3177 | if (dispatchMode.test(InputTarget::Flags::DISPATCH_AS_OUTSIDE)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3178 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3179 | } else if (dispatchMode.test(InputTarget::Flags::DISPATCH_AS_HOVER_EXIT)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3180 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3181 | } else if (dispatchMode.test(InputTarget::Flags::DISPATCH_AS_HOVER_ENTER)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3182 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3183 | } else if (dispatchMode.test(InputTarget::Flags::DISPATCH_AS_SLIPPERY_EXIT)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3184 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_CANCEL; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3185 | } else if (dispatchMode.test(InputTarget::Flags::DISPATCH_AS_SLIPPERY_ENTER)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3186 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_DOWN; |
| 3187 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3188 | dispatchEntry->resolvedAction = motionEntry.action; |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3189 | dispatchEntry->resolvedEventId = motionEntry.id; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3190 | } |
| 3191 | if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE && |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3192 | !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source, |
| 3193 | motionEntry.displayId)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3194 | if (DEBUG_DISPATCH_CYCLE) { |
| 3195 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: filling in missing hover " |
| 3196 | "enter event", |
| 3197 | connection->getInputChannelName().c_str()); |
| 3198 | } |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3199 | // We keep the 'resolvedEventId' here equal to the original 'motionEntry.id' because |
| 3200 | // this is a one-to-one event conversion. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3201 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 3202 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3203 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3204 | dispatchEntry->resolvedFlags = motionEntry.flags; |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 3205 | if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_CANCEL) { |
| 3206 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_CANCELED; |
| 3207 | } |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3208 | if (dispatchEntry->targetFlags.test(InputTarget::Flags::WINDOW_IS_OBSCURED)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3209 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; |
| 3210 | } |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3211 | if (dispatchEntry->targetFlags.test(InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3212 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 3213 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3214 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3215 | if (!connection->inputState.trackMotion(motionEntry, dispatchEntry->resolvedAction, |
| 3216 | dispatchEntry->resolvedFlags)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3217 | if (DEBUG_DISPATCH_CYCLE) { |
| 3218 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent motion " |
| 3219 | "event", |
| 3220 | connection->getInputChannelName().c_str()); |
| 3221 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3222 | return; // skip the inconsistent event |
| 3223 | } |
| 3224 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3225 | dispatchEntry->resolvedEventId = |
| 3226 | dispatchEntry->resolvedEventId == DEFAULT_RESOLVED_EVENT_ID |
| 3227 | ? mIdGenerator.nextId() |
| 3228 | : motionEntry.id; |
| 3229 | if (ATRACE_ENABLED() && dispatchEntry->resolvedEventId != motionEntry.id) { |
| 3230 | std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32 |
| 3231 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3232 | motionEntry.id, dispatchEntry->resolvedEventId); |
| 3233 | ATRACE_NAME(message.c_str()); |
| 3234 | } |
| 3235 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 3236 | if ((motionEntry.flags & AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) && |
| 3237 | (motionEntry.policyFlags & POLICY_FLAG_TRUSTED)) { |
| 3238 | // Skip reporting pointer down outside focus to the policy. |
| 3239 | break; |
| 3240 | } |
| 3241 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3242 | dispatchPointerDownOutsideFocus(motionEntry.source, dispatchEntry->resolvedAction, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3243 | inputTarget.inputChannel->getConnectionToken()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3244 | |
| 3245 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3246 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3247 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3248 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3249 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3250 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3251 | break; |
| 3252 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3253 | case EventEntry::Type::SENSOR: { |
| 3254 | LOG_ALWAYS_FATAL("SENSOR events should not go to apps via input channel"); |
| 3255 | break; |
| 3256 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3257 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 3258 | case EventEntry::Type::DEVICE_RESET: { |
| 3259 | LOG_ALWAYS_FATAL("%s events should not go to apps", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3260 | ftl::enum_string(newEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3261 | break; |
| 3262 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3263 | } |
| 3264 | |
| 3265 | // Remember that we are waiting for this dispatch to complete. |
| 3266 | if (dispatchEntry->hasForegroundTarget()) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3267 | incrementPendingForegroundDispatches(newEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3268 | } |
| 3269 | |
| 3270 | // Enqueue the dispatch entry. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3271 | connection->outboundQueue.push_back(dispatchEntry.release()); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3272 | traceOutboundQueueLength(*connection); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3273 | } |
| 3274 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3275 | /** |
| 3276 | * This function is purely for debugging. It helps us understand where the user interaction |
| 3277 | * was taking place. For example, if user is touching launcher, we will see a log that user |
| 3278 | * started interacting with launcher. In that example, the event would go to the wallpaper as well. |
| 3279 | * We will see both launcher and wallpaper in that list. |
| 3280 | * Once the interaction with a particular set of connections starts, no new logs will be printed |
| 3281 | * until the set of interacted connections changes. |
| 3282 | * |
| 3283 | * The following items are skipped, to reduce the logspam: |
| 3284 | * ACTION_OUTSIDE: any windows that are receiving ACTION_OUTSIDE are not logged |
| 3285 | * ACTION_UP: any windows that receive ACTION_UP are not logged (for both keys and motions). |
| 3286 | * This includes situations like the soft BACK button key. When the user releases (lifts up the |
| 3287 | * finger) the back button, then navigation bar will inject KEYCODE_BACK with ACTION_UP. |
| 3288 | * Both of those ACTION_UP events would not be logged |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3289 | */ |
| 3290 | void InputDispatcher::updateInteractionTokensLocked(const EventEntry& entry, |
| 3291 | const std::vector<InputTarget>& targets) { |
| 3292 | // Skip ACTION_UP events, and all events other than keys and motions |
| 3293 | if (entry.type == EventEntry::Type::KEY) { |
| 3294 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 3295 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
| 3296 | return; |
| 3297 | } |
| 3298 | } else if (entry.type == EventEntry::Type::MOTION) { |
| 3299 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 3300 | if (motionEntry.action == AMOTION_EVENT_ACTION_UP || |
| 3301 | motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
| 3302 | return; |
| 3303 | } |
| 3304 | } else { |
| 3305 | return; // Not a key or a motion |
| 3306 | } |
| 3307 | |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 3308 | std::unordered_set<sp<IBinder>, StrongPointerHash<IBinder>> newConnectionTokens; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3309 | std::vector<sp<Connection>> newConnections; |
| 3310 | for (const InputTarget& target : targets) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3311 | if (target.flags.test(InputTarget::Flags::DISPATCH_AS_OUTSIDE)) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3312 | continue; // Skip windows that receive ACTION_OUTSIDE |
| 3313 | } |
| 3314 | |
| 3315 | sp<IBinder> token = target.inputChannel->getConnectionToken(); |
| 3316 | sp<Connection> connection = getConnectionLocked(token); |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3317 | if (connection == nullptr) { |
| 3318 | continue; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3319 | } |
| 3320 | newConnectionTokens.insert(std::move(token)); |
| 3321 | newConnections.emplace_back(connection); |
| 3322 | } |
| 3323 | if (newConnectionTokens == mInteractionConnectionTokens) { |
| 3324 | return; // no change |
| 3325 | } |
| 3326 | mInteractionConnectionTokens = newConnectionTokens; |
| 3327 | |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3328 | std::string targetList; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3329 | for (const sp<Connection>& connection : newConnections) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3330 | targetList += connection->getWindowName() + ", "; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3331 | } |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3332 | std::string message = "Interaction with: " + targetList; |
| 3333 | if (targetList.empty()) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3334 | message += "<none>"; |
| 3335 | } |
| 3336 | android_log_event_list(LOGTAG_INPUT_INTERACTION) << message << LOG_ID_EVENTS; |
| 3337 | } |
| 3338 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3339 | void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3340 | const sp<IBinder>& token) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3341 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3342 | uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK; |
| 3343 | if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3344 | return; |
| 3345 | } |
| 3346 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 3347 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3348 | if (focusedToken == token) { |
| 3349 | // ignore since token is focused |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3350 | return; |
| 3351 | } |
| 3352 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3353 | auto command = [this, token]() REQUIRES(mLock) { |
| 3354 | scoped_unlock unlock(mLock); |
| 3355 | mPolicy->onPointerDownOutsideFocus(token); |
| 3356 | }; |
| 3357 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3358 | } |
| 3359 | |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3360 | status_t InputDispatcher::publishMotionEvent(Connection& connection, |
| 3361 | DispatchEntry& dispatchEntry) const { |
| 3362 | const EventEntry& eventEntry = *(dispatchEntry.eventEntry); |
| 3363 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 3364 | |
| 3365 | PointerCoords scaledCoords[MAX_POINTERS]; |
| 3366 | const PointerCoords* usingCoords = motionEntry.pointerCoords; |
| 3367 | |
| 3368 | // Set the X and Y offset and X and Y scale depending on the input source. |
| 3369 | if ((motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) && |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3370 | !(dispatchEntry.targetFlags.test(InputTarget::Flags::ZERO_COORDS))) { |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3371 | float globalScaleFactor = dispatchEntry.globalScaleFactor; |
| 3372 | if (globalScaleFactor != 1.0f) { |
| 3373 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
| 3374 | scaledCoords[i] = motionEntry.pointerCoords[i]; |
| 3375 | // Don't apply window scale here since we don't want scale to affect raw |
| 3376 | // coordinates. The scale will be sent back to the client and applied |
| 3377 | // later when requesting relative coordinates. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 3378 | scaledCoords[i].scale(globalScaleFactor, /*windowXScale=*/1, /*windowYScale=*/1); |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3379 | } |
| 3380 | usingCoords = scaledCoords; |
| 3381 | } |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3382 | } else if (dispatchEntry.targetFlags.test(InputTarget::Flags::ZERO_COORDS)) { |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3383 | // We don't want the dispatch target to know the coordinates |
| 3384 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
| 3385 | scaledCoords[i].clear(); |
| 3386 | } |
| 3387 | usingCoords = scaledCoords; |
| 3388 | } |
| 3389 | |
| 3390 | std::array<uint8_t, 32> hmac = getSignature(motionEntry, dispatchEntry); |
| 3391 | |
| 3392 | // Publish the motion event. |
| 3393 | return connection.inputPublisher |
| 3394 | .publishMotionEvent(dispatchEntry.seq, dispatchEntry.resolvedEventId, |
| 3395 | motionEntry.deviceId, motionEntry.source, motionEntry.displayId, |
| 3396 | std::move(hmac), dispatchEntry.resolvedAction, |
| 3397 | motionEntry.actionButton, dispatchEntry.resolvedFlags, |
| 3398 | motionEntry.edgeFlags, motionEntry.metaState, |
| 3399 | motionEntry.buttonState, motionEntry.classification, |
| 3400 | dispatchEntry.transform, motionEntry.xPrecision, |
| 3401 | motionEntry.yPrecision, motionEntry.xCursorPosition, |
| 3402 | motionEntry.yCursorPosition, dispatchEntry.rawTransform, |
| 3403 | motionEntry.downTime, motionEntry.eventTime, |
| 3404 | motionEntry.pointerCount, motionEntry.pointerProperties, |
| 3405 | usingCoords); |
| 3406 | } |
| 3407 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3408 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3409 | const sp<Connection>& connection) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3410 | if (ATRACE_ENABLED()) { |
| 3411 | std::string message = StringPrintf("startDispatchCycleLocked(inputChannel=%s)", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3412 | connection->getInputChannelName().c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3413 | ATRACE_NAME(message.c_str()); |
| 3414 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3415 | if (DEBUG_DISPATCH_CYCLE) { |
| 3416 | ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str()); |
| 3417 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3418 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3419 | while (connection->status == Connection::Status::NORMAL && !connection->outboundQueue.empty()) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3420 | DispatchEntry* dispatchEntry = connection->outboundQueue.front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3421 | dispatchEntry->deliveryTime = currentTime; |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3422 | const std::chrono::nanoseconds timeout = getDispatchingTimeoutLocked(connection); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3423 | dispatchEntry->timeoutTime = currentTime + timeout.count(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3424 | |
| 3425 | // Publish the event. |
| 3426 | status_t status; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3427 | const EventEntry& eventEntry = *(dispatchEntry->eventEntry); |
| 3428 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3429 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3430 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 3431 | std::array<uint8_t, 32> hmac = getSignature(keyEntry, *dispatchEntry); |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 3432 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 3433 | LOG(DEBUG) << "Publishing " << *dispatchEntry << " to " |
| 3434 | << connection->getInputChannelName(); |
| 3435 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3436 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3437 | // Publish the key event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3438 | status = connection->inputPublisher |
| 3439 | .publishKeyEvent(dispatchEntry->seq, |
| 3440 | dispatchEntry->resolvedEventId, keyEntry.deviceId, |
| 3441 | keyEntry.source, keyEntry.displayId, |
| 3442 | std::move(hmac), dispatchEntry->resolvedAction, |
| 3443 | dispatchEntry->resolvedFlags, keyEntry.keyCode, |
| 3444 | keyEntry.scanCode, keyEntry.metaState, |
| 3445 | keyEntry.repeatCount, keyEntry.downTime, |
| 3446 | keyEntry.eventTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3447 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3448 | } |
| 3449 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3450 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 3451 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 3452 | LOG(DEBUG) << "Publishing " << *dispatchEntry << " to " |
| 3453 | << connection->getInputChannelName(); |
| 3454 | } |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3455 | status = publishMotionEvent(*connection, *dispatchEntry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3456 | break; |
| 3457 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3458 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3459 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3460 | const FocusEntry& focusEntry = static_cast<const FocusEntry&>(eventEntry); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3461 | status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3462 | focusEntry.id, |
Antonio Kantek | 3cfec7b | 2021-11-05 18:26:17 -0700 | [diff] [blame] | 3463 | focusEntry.hasFocus); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3464 | break; |
| 3465 | } |
| 3466 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3467 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 3468 | const TouchModeEntry& touchModeEntry = |
| 3469 | static_cast<const TouchModeEntry&>(eventEntry); |
| 3470 | status = connection->inputPublisher |
| 3471 | .publishTouchModeEvent(dispatchEntry->seq, touchModeEntry.id, |
| 3472 | touchModeEntry.inTouchMode); |
| 3473 | |
| 3474 | break; |
| 3475 | } |
| 3476 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3477 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 3478 | const auto& captureEntry = |
| 3479 | static_cast<const PointerCaptureChangedEntry&>(eventEntry); |
| 3480 | status = connection->inputPublisher |
| 3481 | .publishCaptureEvent(dispatchEntry->seq, captureEntry.id, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 3482 | captureEntry.pointerCaptureRequest.enable); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3483 | break; |
| 3484 | } |
| 3485 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3486 | case EventEntry::Type::DRAG: { |
| 3487 | const DragEntry& dragEntry = static_cast<const DragEntry&>(eventEntry); |
| 3488 | status = connection->inputPublisher.publishDragEvent(dispatchEntry->seq, |
| 3489 | dragEntry.id, dragEntry.x, |
| 3490 | dragEntry.y, |
| 3491 | dragEntry.isExiting); |
| 3492 | break; |
| 3493 | } |
| 3494 | |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3495 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3496 | case EventEntry::Type::DEVICE_RESET: |
| 3497 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3498 | LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3499 | ftl::enum_string(eventEntry.type).c_str()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3500 | return; |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3501 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3502 | } |
| 3503 | |
| 3504 | // Check the result. |
| 3505 | if (status) { |
| 3506 | if (status == WOULD_BLOCK) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3507 | if (connection->waitQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3508 | ALOGE("channel '%s' ~ Could not publish event because the pipe is full. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3509 | "This is unexpected because the wait queue is empty, so the pipe " |
| 3510 | "should be empty and we shouldn't have any problems writing an " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3511 | "event to it, status=%s(%d)", |
| 3512 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3513 | status); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 3514 | abortBrokenDispatchCycleLocked(currentTime, connection, /*notify=*/true); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3515 | } else { |
| 3516 | // Pipe is full and we are waiting for the app to finish process some events |
| 3517 | // before sending more events to it. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3518 | if (DEBUG_DISPATCH_CYCLE) { |
| 3519 | ALOGD("channel '%s' ~ Could not publish event because the pipe is full, " |
| 3520 | "waiting for the application to catch up", |
| 3521 | connection->getInputChannelName().c_str()); |
| 3522 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3523 | } |
| 3524 | } else { |
| 3525 | ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3526 | "status=%s(%d)", |
| 3527 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3528 | status); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 3529 | abortBrokenDispatchCycleLocked(currentTime, connection, /*notify=*/true); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3530 | } |
| 3531 | return; |
| 3532 | } |
| 3533 | |
| 3534 | // Re-enqueue the event on the wait queue. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3535 | connection->outboundQueue.erase(std::remove(connection->outboundQueue.begin(), |
| 3536 | connection->outboundQueue.end(), |
| 3537 | dispatchEntry)); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3538 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3539 | connection->waitQueue.push_back(dispatchEntry); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3540 | if (connection->responsive) { |
| 3541 | mAnrTracker.insert(dispatchEntry->timeoutTime, |
| 3542 | connection->inputChannel->getConnectionToken()); |
| 3543 | } |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3544 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3545 | } |
| 3546 | } |
| 3547 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3548 | std::array<uint8_t, 32> InputDispatcher::sign(const VerifiedInputEvent& event) const { |
| 3549 | size_t size; |
| 3550 | switch (event.type) { |
| 3551 | case VerifiedInputEvent::Type::KEY: { |
| 3552 | size = sizeof(VerifiedKeyEvent); |
| 3553 | break; |
| 3554 | } |
| 3555 | case VerifiedInputEvent::Type::MOTION: { |
| 3556 | size = sizeof(VerifiedMotionEvent); |
| 3557 | break; |
| 3558 | } |
| 3559 | } |
| 3560 | const uint8_t* start = reinterpret_cast<const uint8_t*>(&event); |
| 3561 | return mHmacKeyManager.sign(start, size); |
| 3562 | } |
| 3563 | |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3564 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3565 | const MotionEntry& motionEntry, const DispatchEntry& dispatchEntry) const { |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3566 | const int32_t actionMasked = dispatchEntry.resolvedAction & AMOTION_EVENT_ACTION_MASK; |
| 3567 | if (actionMasked != AMOTION_EVENT_ACTION_UP && actionMasked != AMOTION_EVENT_ACTION_DOWN) { |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3568 | // Only sign events up and down events as the purely move events |
| 3569 | // are tied to their up/down counterparts so signing would be redundant. |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3570 | return INVALID_HMAC; |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3571 | } |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3572 | |
| 3573 | VerifiedMotionEvent verifiedEvent = |
| 3574 | verifiedMotionEventFromMotionEntry(motionEntry, dispatchEntry.rawTransform); |
| 3575 | verifiedEvent.actionMasked = actionMasked; |
| 3576 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS; |
| 3577 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3578 | } |
| 3579 | |
| 3580 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3581 | const KeyEntry& keyEntry, const DispatchEntry& dispatchEntry) const { |
| 3582 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(keyEntry); |
| 3583 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_KEY_EVENT_FLAGS; |
| 3584 | verifiedEvent.action = dispatchEntry.resolvedAction; |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3585 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3586 | } |
| 3587 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3588 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3589 | const sp<Connection>& connection, uint32_t seq, |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 3590 | bool handled, nsecs_t consumeTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3591 | if (DEBUG_DISPATCH_CYCLE) { |
| 3592 | ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s", |
| 3593 | connection->getInputChannelName().c_str(), seq, toString(handled)); |
| 3594 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3595 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3596 | if (connection->status == Connection::Status::BROKEN || |
| 3597 | connection->status == Connection::Status::ZOMBIE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3598 | return; |
| 3599 | } |
| 3600 | |
| 3601 | // Notify other system components and prepare to start the next dispatch cycle. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3602 | auto command = [this, currentTime, connection, seq, handled, consumeTime]() REQUIRES(mLock) { |
| 3603 | doDispatchCycleFinishedCommand(currentTime, connection, seq, handled, consumeTime); |
| 3604 | }; |
| 3605 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3606 | } |
| 3607 | |
| 3608 | void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3609 | const sp<Connection>& connection, |
| 3610 | bool notify) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3611 | if (DEBUG_DISPATCH_CYCLE) { |
| 3612 | ALOGD("channel '%s' ~ abortBrokenDispatchCycle - notify=%s", |
| 3613 | connection->getInputChannelName().c_str(), toString(notify)); |
| 3614 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3615 | |
| 3616 | // Clear the dispatch queues. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3617 | drainDispatchQueue(connection->outboundQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3618 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3619 | drainDispatchQueue(connection->waitQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3620 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3621 | |
| 3622 | // The connection appears to be unrecoverably broken. |
| 3623 | // Ignore already broken or zombie connections. |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3624 | if (connection->status == Connection::Status::NORMAL) { |
| 3625 | connection->status = Connection::Status::BROKEN; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3626 | |
| 3627 | if (notify) { |
| 3628 | // Notify other system components. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3629 | ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", |
| 3630 | connection->getInputChannelName().c_str()); |
| 3631 | |
| 3632 | auto command = [this, connection]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3633 | scoped_unlock unlock(mLock); |
| 3634 | mPolicy->notifyInputChannelBroken(connection->inputChannel->getConnectionToken()); |
| 3635 | }; |
| 3636 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3637 | } |
| 3638 | } |
| 3639 | } |
| 3640 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3641 | void InputDispatcher::drainDispatchQueue(std::deque<DispatchEntry*>& queue) { |
| 3642 | while (!queue.empty()) { |
| 3643 | DispatchEntry* dispatchEntry = queue.front(); |
| 3644 | queue.pop_front(); |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3645 | releaseDispatchEntry(dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3646 | } |
| 3647 | } |
| 3648 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3649 | void InputDispatcher::releaseDispatchEntry(DispatchEntry* dispatchEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3650 | if (dispatchEntry->hasForegroundTarget()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3651 | decrementPendingForegroundDispatches(*(dispatchEntry->eventEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3652 | } |
| 3653 | delete dispatchEntry; |
| 3654 | } |
| 3655 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3656 | int InputDispatcher::handleReceiveCallback(int events, sp<IBinder> connectionToken) { |
| 3657 | std::scoped_lock _l(mLock); |
| 3658 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 3659 | if (connection == nullptr) { |
| 3660 | ALOGW("Received looper callback for unknown input channel token %p. events=0x%x", |
| 3661 | connectionToken.get(), events); |
| 3662 | return 0; // remove the callback |
| 3663 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3664 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3665 | bool notify; |
| 3666 | if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { |
| 3667 | if (!(events & ALOOPER_EVENT_INPUT)) { |
| 3668 | ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. " |
| 3669 | "events=0x%x", |
| 3670 | connection->getInputChannelName().c_str(), events); |
| 3671 | return 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3672 | } |
| 3673 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3674 | nsecs_t currentTime = now(); |
| 3675 | bool gotOne = false; |
| 3676 | status_t status = OK; |
| 3677 | for (;;) { |
| 3678 | Result<InputPublisher::ConsumerResponse> result = |
| 3679 | connection->inputPublisher.receiveConsumerResponse(); |
| 3680 | if (!result.ok()) { |
| 3681 | status = result.error().code(); |
| 3682 | break; |
| 3683 | } |
| 3684 | |
| 3685 | if (std::holds_alternative<InputPublisher::Finished>(*result)) { |
| 3686 | const InputPublisher::Finished& finish = |
| 3687 | std::get<InputPublisher::Finished>(*result); |
| 3688 | finishDispatchCycleLocked(currentTime, connection, finish.seq, finish.handled, |
| 3689 | finish.consumeTime); |
| 3690 | } else if (std::holds_alternative<InputPublisher::Timeline>(*result)) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3691 | if (shouldReportMetricsForConnection(*connection)) { |
| 3692 | const InputPublisher::Timeline& timeline = |
| 3693 | std::get<InputPublisher::Timeline>(*result); |
| 3694 | mLatencyTracker |
| 3695 | .trackGraphicsLatency(timeline.inputEventId, |
| 3696 | connection->inputChannel->getConnectionToken(), |
| 3697 | std::move(timeline.graphicsTimeline)); |
| 3698 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3699 | } |
| 3700 | gotOne = true; |
| 3701 | } |
| 3702 | if (gotOne) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3703 | runCommandsLockedInterruptable(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3704 | if (status == WOULD_BLOCK) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3705 | return 1; |
| 3706 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3707 | } |
| 3708 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3709 | notify = status != DEAD_OBJECT || !connection->monitor; |
| 3710 | if (notify) { |
| 3711 | ALOGE("channel '%s' ~ Failed to receive finished signal. status=%s(%d)", |
| 3712 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3713 | status); |
| 3714 | } |
| 3715 | } else { |
| 3716 | // Monitor channels are never explicitly unregistered. |
| 3717 | // We do it automatically when the remote endpoint is closed so don't warn about them. |
| 3718 | const bool stillHaveWindowHandle = |
| 3719 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()) != nullptr; |
| 3720 | notify = !connection->monitor && stillHaveWindowHandle; |
| 3721 | if (notify) { |
| 3722 | ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred. events=0x%x", |
| 3723 | connection->getInputChannelName().c_str(), events); |
| 3724 | } |
| 3725 | } |
| 3726 | |
| 3727 | // Remove the channel. |
| 3728 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), notify); |
| 3729 | return 0; // remove the callback |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3730 | } |
| 3731 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3732 | void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3733 | const CancelationOptions& options) { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3734 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 3735 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3736 | } |
| 3737 | } |
| 3738 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3739 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3740 | const CancelationOptions& options) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 3741 | for (const auto& [_, monitors] : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3742 | for (const Monitor& monitor : monitors) { |
| 3743 | synthesizeCancelationEventsForInputChannelLocked(monitor.inputChannel, options); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3744 | } |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3745 | } |
| 3746 | } |
| 3747 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3748 | void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked( |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 3749 | const std::shared_ptr<InputChannel>& channel, const CancelationOptions& options) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 3750 | sp<Connection> connection = getConnectionLocked(channel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3751 | if (connection == nullptr) { |
| 3752 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3753 | } |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3754 | |
| 3755 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3756 | } |
| 3757 | |
| 3758 | void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( |
| 3759 | const sp<Connection>& connection, const CancelationOptions& options) { |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3760 | if (connection->status == Connection::Status::BROKEN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3761 | return; |
| 3762 | } |
| 3763 | |
| 3764 | nsecs_t currentTime = now(); |
| 3765 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3766 | std::vector<std::unique_ptr<EventEntry>> cancelationEvents = |
Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 3767 | connection->inputState.synthesizeCancelationEvents(currentTime, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3768 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3769 | if (cancelationEvents.empty()) { |
| 3770 | return; |
| 3771 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3772 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 3773 | 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] | 3774 | "with reality: %s, mode=%s.", |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3775 | connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason, |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3776 | ftl::enum_string(options.mode).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3777 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3778 | |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3779 | std::string reason = std::string("reason=").append(options.reason); |
| 3780 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 3781 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 3782 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3783 | InputTarget target; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3784 | sp<WindowInfoHandle> windowHandle = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3785 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3786 | if (windowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3787 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3788 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3789 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3790 | } |
| 3791 | target.inputChannel = connection->inputChannel; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3792 | target.flags = InputTarget::Flags::DISPATCH_AS_IS; |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3793 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 3794 | const bool wasEmpty = connection->outboundQueue.empty(); |
| 3795 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3796 | for (size_t i = 0; i < cancelationEvents.size(); i++) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3797 | std::unique_ptr<EventEntry> cancelationEventEntry = std::move(cancelationEvents[i]); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3798 | switch (cancelationEventEntry->type) { |
| 3799 | case EventEntry::Type::KEY: { |
| 3800 | logOutboundKeyDetails("cancel - ", |
| 3801 | static_cast<const KeyEntry&>(*cancelationEventEntry)); |
| 3802 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3803 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3804 | case EventEntry::Type::MOTION: { |
| 3805 | logOutboundMotionDetails("cancel - ", |
| 3806 | static_cast<const MotionEntry&>(*cancelationEventEntry)); |
| 3807 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3808 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3809 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3810 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3811 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3812 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3813 | LOG_ALWAYS_FATAL("Canceling %s events is not supported", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3814 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3815 | break; |
| 3816 | } |
| 3817 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3818 | case EventEntry::Type::DEVICE_RESET: |
| 3819 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3820 | 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] | 3821 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3822 | break; |
| 3823 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3824 | } |
| 3825 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3826 | enqueueDispatchEntryLocked(connection, std::move(cancelationEventEntry), target, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3827 | InputTarget::Flags::DISPATCH_AS_IS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3828 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3829 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 3830 | // If the outbound queue was previously empty, start the dispatch cycle going. |
| 3831 | if (wasEmpty && !connection->outboundQueue.empty()) { |
| 3832 | startDispatchCycleLocked(currentTime, connection); |
| 3833 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3834 | } |
| 3835 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3836 | void InputDispatcher::synthesizePointerDownEventsForConnectionLocked( |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 3837 | const nsecs_t downTime, const sp<Connection>& connection, |
| 3838 | ftl::Flags<InputTarget::Flags> targetFlags) { |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3839 | if (connection->status == Connection::Status::BROKEN) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3840 | return; |
| 3841 | } |
| 3842 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3843 | std::vector<std::unique_ptr<EventEntry>> downEvents = |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3844 | connection->inputState.synthesizePointerDownEvents(downTime); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3845 | |
| 3846 | if (downEvents.empty()) { |
| 3847 | return; |
| 3848 | } |
| 3849 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3850 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3851 | ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.", |
| 3852 | connection->getInputChannelName().c_str(), downEvents.size()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3853 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3854 | |
| 3855 | InputTarget target; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3856 | sp<WindowInfoHandle> windowHandle = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3857 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3858 | if (windowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3859 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3860 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3861 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3862 | } |
| 3863 | target.inputChannel = connection->inputChannel; |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 3864 | target.flags = targetFlags; |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3865 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 3866 | const bool wasEmpty = connection->outboundQueue.empty(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3867 | for (std::unique_ptr<EventEntry>& downEventEntry : downEvents) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3868 | switch (downEventEntry->type) { |
| 3869 | case EventEntry::Type::MOTION: { |
| 3870 | logOutboundMotionDetails("down - ", |
| 3871 | static_cast<const MotionEntry&>(*downEventEntry)); |
| 3872 | break; |
| 3873 | } |
| 3874 | |
| 3875 | case EventEntry::Type::KEY: |
| 3876 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3877 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3878 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3879 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3880 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3881 | case EventEntry::Type::SENSOR: |
| 3882 | case EventEntry::Type::DRAG: { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3883 | 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] | 3884 | ftl::enum_string(downEventEntry->type).c_str()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3885 | break; |
| 3886 | } |
| 3887 | } |
| 3888 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3889 | enqueueDispatchEntryLocked(connection, std::move(downEventEntry), target, |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 3890 | InputTarget::Flags::DISPATCH_AS_IS); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3891 | } |
| 3892 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 3893 | // If the outbound queue was previously empty, start the dispatch cycle going. |
| 3894 | if (wasEmpty && !connection->outboundQueue.empty()) { |
| 3895 | startDispatchCycleLocked(downTime, connection); |
| 3896 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3897 | } |
| 3898 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 3899 | void InputDispatcher::synthesizeCancelationEventsForWindowLocked( |
| 3900 | const sp<WindowInfoHandle>& windowHandle, const CancelationOptions& options) { |
| 3901 | if (windowHandle != nullptr) { |
| 3902 | sp<Connection> wallpaperConnection = getConnectionLocked(windowHandle->getToken()); |
| 3903 | if (wallpaperConnection != nullptr) { |
| 3904 | synthesizeCancelationEventsForConnectionLocked(wallpaperConnection, options); |
| 3905 | } |
| 3906 | } |
| 3907 | } |
| 3908 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3909 | std::unique_ptr<MotionEntry> InputDispatcher::splitMotionEvent( |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 3910 | const MotionEntry& originalMotionEntry, std::bitset<MAX_POINTER_ID + 1> pointerIds, |
| 3911 | nsecs_t splitDownTime) { |
| 3912 | ALOG_ASSERT(pointerIds.any()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3913 | |
| 3914 | uint32_t splitPointerIndexMap[MAX_POINTERS]; |
| 3915 | PointerProperties splitPointerProperties[MAX_POINTERS]; |
| 3916 | PointerCoords splitPointerCoords[MAX_POINTERS]; |
| 3917 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3918 | uint32_t originalPointerCount = originalMotionEntry.pointerCount; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3919 | uint32_t splitPointerCount = 0; |
| 3920 | |
| 3921 | for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3922 | originalPointerIndex++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3923 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3924 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3925 | uint32_t pointerId = uint32_t(pointerProperties.id); |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 3926 | if (pointerIds.test(pointerId)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3927 | splitPointerIndexMap[splitPointerCount] = originalPointerIndex; |
| 3928 | splitPointerProperties[splitPointerCount].copyFrom(pointerProperties); |
| 3929 | splitPointerCoords[splitPointerCount].copyFrom( |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3930 | originalMotionEntry.pointerCoords[originalPointerIndex]); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3931 | splitPointerCount += 1; |
| 3932 | } |
| 3933 | } |
| 3934 | |
| 3935 | if (splitPointerCount != pointerIds.count()) { |
| 3936 | // This is bad. We are missing some of the pointers that we expected to deliver. |
| 3937 | // Most likely this indicates that we received an ACTION_MOVE events that has |
| 3938 | // different pointer ids than we expected based on the previous ACTION_DOWN |
| 3939 | // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers |
| 3940 | // in this way. |
| 3941 | ALOGW("Dropping split motion event because the pointer count is %d but " |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 3942 | "we expected there to be %zu pointers. This probably means we received " |
Siarhei Vishniakou | 16e4fa0 | 2023-02-16 17:48:56 -0800 | [diff] [blame] | 3943 | "a broken sequence of pointer ids from the input device: %s", |
| 3944 | splitPointerCount, pointerIds.count(), originalMotionEntry.getDescription().c_str()); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3945 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3946 | } |
| 3947 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3948 | int32_t action = originalMotionEntry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3949 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3950 | if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN || |
| 3951 | maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3952 | int32_t originalPointerIndex = getMotionEventActionPointerIndex(action); |
| 3953 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3954 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3955 | uint32_t pointerId = uint32_t(pointerProperties.id); |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 3956 | if (pointerIds.test(pointerId)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3957 | if (pointerIds.count() == 1) { |
| 3958 | // The first/last pointer went down/up. |
| 3959 | action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3960 | ? AMOTION_EVENT_ACTION_DOWN |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3961 | : (originalMotionEntry.flags & AMOTION_EVENT_FLAG_CANCELED) != 0 |
| 3962 | ? AMOTION_EVENT_ACTION_CANCEL |
| 3963 | : AMOTION_EVENT_ACTION_UP; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3964 | } else { |
| 3965 | // A secondary pointer went down/up. |
| 3966 | uint32_t splitPointerIndex = 0; |
| 3967 | while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { |
| 3968 | splitPointerIndex += 1; |
| 3969 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3970 | action = maskedAction | |
| 3971 | (splitPointerIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3972 | } |
| 3973 | } else { |
| 3974 | // An unrelated pointer changed. |
| 3975 | action = AMOTION_EVENT_ACTION_MOVE; |
| 3976 | } |
| 3977 | } |
| 3978 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3979 | if (action == AMOTION_EVENT_ACTION_DOWN) { |
| 3980 | LOG_ALWAYS_FATAL_IF(splitDownTime != originalMotionEntry.eventTime, |
| 3981 | "Split motion event has mismatching downTime and eventTime for " |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3982 | "ACTION_DOWN, motionEntry=%s, splitDownTime=%" PRId64, |
| 3983 | originalMotionEntry.getDescription().c_str(), splitDownTime); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3984 | } |
| 3985 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3986 | int32_t newId = mIdGenerator.nextId(); |
| 3987 | if (ATRACE_ENABLED()) { |
| 3988 | std::string message = StringPrintf("Split MotionEvent(id=0x%" PRIx32 |
| 3989 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3990 | originalMotionEntry.id, newId); |
| 3991 | ATRACE_NAME(message.c_str()); |
| 3992 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3993 | std::unique_ptr<MotionEntry> splitMotionEntry = |
| 3994 | std::make_unique<MotionEntry>(newId, originalMotionEntry.eventTime, |
| 3995 | originalMotionEntry.deviceId, originalMotionEntry.source, |
| 3996 | originalMotionEntry.displayId, |
| 3997 | originalMotionEntry.policyFlags, action, |
| 3998 | originalMotionEntry.actionButton, |
| 3999 | originalMotionEntry.flags, originalMotionEntry.metaState, |
| 4000 | originalMotionEntry.buttonState, |
| 4001 | originalMotionEntry.classification, |
| 4002 | originalMotionEntry.edgeFlags, |
| 4003 | originalMotionEntry.xPrecision, |
| 4004 | originalMotionEntry.yPrecision, |
| 4005 | originalMotionEntry.xCursorPosition, |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4006 | originalMotionEntry.yCursorPosition, splitDownTime, |
| 4007 | splitPointerCount, splitPointerProperties, |
| 4008 | splitPointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4009 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 4010 | if (originalMotionEntry.injectionState) { |
| 4011 | splitMotionEntry->injectionState = originalMotionEntry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4012 | splitMotionEntry->injectionState->refCount += 1; |
| 4013 | } |
| 4014 | |
| 4015 | return splitMotionEntry; |
| 4016 | } |
| 4017 | |
| 4018 | void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame^] | 4019 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4020 | ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args->eventTime); |
| 4021 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4022 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4023 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4024 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4025 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4026 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4027 | std::unique_ptr<ConfigurationChangedEntry> newEntry = |
| 4028 | std::make_unique<ConfigurationChangedEntry>(args->id, args->eventTime); |
| 4029 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4030 | } // release lock |
| 4031 | |
| 4032 | if (needWake) { |
| 4033 | mLooper->wake(); |
| 4034 | } |
| 4035 | } |
| 4036 | |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 4037 | /** |
| 4038 | * If one of the meta shortcuts is detected, process them here: |
| 4039 | * Meta + Backspace -> generate BACK |
| 4040 | * Meta + Enter -> generate HOME |
| 4041 | * This will potentially overwrite keyCode and metaState. |
| 4042 | */ |
| 4043 | void InputDispatcher::accelerateMetaShortcuts(const int32_t deviceId, const int32_t action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4044 | int32_t& keyCode, int32_t& metaState) { |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 4045 | if (metaState & AMETA_META_ON && action == AKEY_EVENT_ACTION_DOWN) { |
| 4046 | int32_t newKeyCode = AKEYCODE_UNKNOWN; |
| 4047 | if (keyCode == AKEYCODE_DEL) { |
| 4048 | newKeyCode = AKEYCODE_BACK; |
| 4049 | } else if (keyCode == AKEYCODE_ENTER) { |
| 4050 | newKeyCode = AKEYCODE_HOME; |
| 4051 | } |
| 4052 | if (newKeyCode != AKEYCODE_UNKNOWN) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4053 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 4054 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4055 | mReplacedKeys[replacement] = newKeyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 4056 | keyCode = newKeyCode; |
| 4057 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 4058 | } |
| 4059 | } else if (action == AKEY_EVENT_ACTION_UP) { |
| 4060 | // In order to maintain a consistent stream of up and down events, check to see if the key |
| 4061 | // going up is one we've replaced in a down event and haven't yet replaced in an up event, |
| 4062 | // 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] | 4063 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 4064 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4065 | auto replacementIt = mReplacedKeys.find(replacement); |
| 4066 | if (replacementIt != mReplacedKeys.end()) { |
| 4067 | keyCode = replacementIt->second; |
| 4068 | mReplacedKeys.erase(replacementIt); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 4069 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 4070 | } |
| 4071 | } |
| 4072 | } |
| 4073 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4074 | void InputDispatcher::notifyKey(const NotifyKeyArgs* args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame^] | 4075 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4076 | ALOGD("notifyKey - eventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
| 4077 | "policyFlags=0x%x, action=0x%x, " |
| 4078 | "flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, downTime=%" PRId64, |
| 4079 | args->eventTime, args->deviceId, args->source, args->displayId, args->policyFlags, |
| 4080 | args->action, args->flags, args->keyCode, args->scanCode, args->metaState, |
| 4081 | args->downTime); |
| 4082 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4083 | if (!validateKeyEvent(args->action)) { |
| 4084 | return; |
| 4085 | } |
| 4086 | |
| 4087 | uint32_t policyFlags = args->policyFlags; |
| 4088 | int32_t flags = args->flags; |
| 4089 | int32_t metaState = args->metaState; |
Siarhei Vishniakou | 622bd32 | 2018-10-29 18:02:27 -0700 | [diff] [blame] | 4090 | // InputDispatcher tracks and generates key repeats on behalf of |
| 4091 | // whatever notifies it, so repeatCount should always be set to 0 |
| 4092 | constexpr int32_t repeatCount = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4093 | if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { |
| 4094 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 4095 | flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
| 4096 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4097 | if (policyFlags & POLICY_FLAG_FUNCTION) { |
| 4098 | metaState |= AMETA_FUNCTION_ON; |
| 4099 | } |
| 4100 | |
| 4101 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 4102 | |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 4103 | int32_t keyCode = args->keyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 4104 | accelerateMetaShortcuts(args->deviceId, args->action, keyCode, metaState); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 4105 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4106 | KeyEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4107 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 4108 | args->action, flags, keyCode, args->scanCode, metaState, repeatCount, |
| 4109 | args->downTime, args->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4110 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4111 | android::base::Timer t; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4112 | mPolicy->interceptKeyBeforeQueueing(&event, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4113 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4114 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4115 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4116 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4117 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4118 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4119 | { // acquire lock |
| 4120 | mLock.lock(); |
| 4121 | |
| 4122 | if (shouldSendKeyToInputFilterLocked(args)) { |
| 4123 | mLock.unlock(); |
| 4124 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4125 | policyFlags |= POLICY_FLAG_FILTERED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4126 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 4127 | return; // event was consumed by the filter |
| 4128 | } |
| 4129 | |
| 4130 | mLock.lock(); |
| 4131 | } |
| 4132 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4133 | std::unique_ptr<KeyEntry> newEntry = |
| 4134 | std::make_unique<KeyEntry>(args->id, args->eventTime, args->deviceId, args->source, |
| 4135 | args->displayId, policyFlags, args->action, flags, |
| 4136 | keyCode, args->scanCode, metaState, repeatCount, |
| 4137 | args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4138 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4139 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4140 | mLock.unlock(); |
| 4141 | } // release lock |
| 4142 | |
| 4143 | if (needWake) { |
| 4144 | mLooper->wake(); |
| 4145 | } |
| 4146 | } |
| 4147 | |
| 4148 | bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) { |
| 4149 | return mInputFilterEnabled; |
| 4150 | } |
| 4151 | |
| 4152 | void InputDispatcher::notifyMotion(const NotifyMotionArgs* args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame^] | 4153 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4154 | ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 4155 | "displayId=%" PRId32 ", policyFlags=0x%x, " |
Siarhei Vishniakou | 6ebd069 | 2022-10-20 15:05:45 -0700 | [diff] [blame] | 4156 | "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] | 4157 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, xCursorPosition=%f, " |
| 4158 | "yCursorPosition=%f, downTime=%" PRId64, |
| 4159 | args->id, args->eventTime, args->deviceId, args->source, args->displayId, |
Siarhei Vishniakou | 6ebd069 | 2022-10-20 15:05:45 -0700 | [diff] [blame] | 4160 | args->policyFlags, MotionEvent::actionToString(args->action).c_str(), |
| 4161 | args->actionButton, args->flags, args->metaState, args->buttonState, args->edgeFlags, |
| 4162 | args->xPrecision, args->yPrecision, args->xCursorPosition, args->yCursorPosition, |
| 4163 | args->downTime); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4164 | for (uint32_t i = 0; i < args->pointerCount; i++) { |
| 4165 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
| 4166 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 4167 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 4168 | "orientation=%f", |
| 4169 | i, args->pointerProperties[i].id, args->pointerProperties[i].toolType, |
| 4170 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 4171 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 4172 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 4173 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 4174 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 4175 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 4176 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 4177 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 4178 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| 4179 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4180 | } |
Siarhei Vishniakou | 2f61bdc | 2022-12-02 08:55:51 -0800 | [diff] [blame] | 4181 | LOG_ALWAYS_FATAL_IF(!validateMotionEvent(args->action, args->actionButton, args->pointerCount, |
| 4182 | args->pointerProperties), |
| 4183 | "Invalid event: %s", args->dump().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4184 | |
| 4185 | uint32_t policyFlags = args->policyFlags; |
| 4186 | policyFlags |= POLICY_FLAG_TRUSTED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4187 | |
| 4188 | android::base::Timer t; |
Charles Chen | 3611f1f | 2019-01-29 17:26:18 +0800 | [diff] [blame] | 4189 | mPolicy->interceptMotionBeforeQueueing(args->displayId, args->eventTime, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4190 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4191 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4192 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4193 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4194 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4195 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4196 | { // acquire lock |
| 4197 | mLock.lock(); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 4198 | if (!(policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
| 4199 | // Set the flag anyway if we already have an ongoing gesture. That would allow us to |
| 4200 | // complete the processing of the current stroke. |
| 4201 | const auto touchStateIt = mTouchStatesByDisplay.find(args->displayId); |
| 4202 | if (touchStateIt != mTouchStatesByDisplay.end()) { |
| 4203 | const TouchState& touchState = touchStateIt->second; |
| 4204 | if (touchState.deviceId == args->deviceId && touchState.isDown()) { |
| 4205 | policyFlags |= POLICY_FLAG_PASS_TO_USER; |
| 4206 | } |
| 4207 | } |
| 4208 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4209 | |
| 4210 | if (shouldSendMotionToInputFilterLocked(args)) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4211 | ui::Transform displayTransform; |
| 4212 | if (const auto it = mDisplayInfos.find(args->displayId); it != mDisplayInfos.end()) { |
| 4213 | displayTransform = it->second.transform; |
| 4214 | } |
| 4215 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4216 | mLock.unlock(); |
| 4217 | |
| 4218 | MotionEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4219 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
| 4220 | args->action, args->actionButton, args->flags, args->edgeFlags, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 4221 | args->metaState, args->buttonState, args->classification, |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4222 | displayTransform, args->xPrecision, args->yPrecision, |
| 4223 | args->xCursorPosition, args->yCursorPosition, displayTransform, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 4224 | args->downTime, args->eventTime, args->pointerCount, |
| 4225 | args->pointerProperties, args->pointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4226 | |
| 4227 | policyFlags |= POLICY_FLAG_FILTERED; |
| 4228 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 4229 | return; // event was consumed by the filter |
| 4230 | } |
| 4231 | |
| 4232 | mLock.lock(); |
| 4233 | } |
| 4234 | |
| 4235 | // Just enqueue a new motion event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4236 | std::unique_ptr<MotionEntry> newEntry = |
| 4237 | std::make_unique<MotionEntry>(args->id, args->eventTime, args->deviceId, |
| 4238 | args->source, args->displayId, policyFlags, |
| 4239 | args->action, args->actionButton, args->flags, |
| 4240 | args->metaState, args->buttonState, |
| 4241 | args->classification, args->edgeFlags, |
| 4242 | args->xPrecision, args->yPrecision, |
| 4243 | args->xCursorPosition, args->yCursorPosition, |
| 4244 | args->downTime, args->pointerCount, |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4245 | args->pointerProperties, args->pointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4246 | |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 4247 | if (args->id != android::os::IInputConstants::INVALID_INPUT_EVENT_ID && |
| 4248 | IdGenerator::getSource(args->id) == IdGenerator::Source::INPUT_READER && |
| 4249 | !mInputFilterEnabled) { |
| 4250 | const bool isDown = args->action == AMOTION_EVENT_ACTION_DOWN; |
| 4251 | mLatencyTracker.trackListener(args->id, isDown, args->eventTime, args->readTime); |
| 4252 | } |
| 4253 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4254 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4255 | mLock.unlock(); |
| 4256 | } // release lock |
| 4257 | |
| 4258 | if (needWake) { |
| 4259 | mLooper->wake(); |
| 4260 | } |
| 4261 | } |
| 4262 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4263 | void InputDispatcher::notifySensor(const NotifySensorArgs* args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame^] | 4264 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4265 | ALOGD("notifySensor - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 4266 | " sensorType=%s", |
| 4267 | args->id, args->eventTime, args->deviceId, args->source, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 4268 | ftl::enum_string(args->sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4269 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4270 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4271 | bool needWake = false; |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4272 | { // acquire lock |
| 4273 | mLock.lock(); |
| 4274 | |
| 4275 | // Just enqueue a new sensor event. |
| 4276 | std::unique_ptr<SensorEntry> newEntry = |
| 4277 | std::make_unique<SensorEntry>(args->id, args->eventTime, args->deviceId, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4278 | args->source, /* policyFlags=*/0, args->hwTimestamp, |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4279 | args->sensorType, args->accuracy, |
| 4280 | args->accuracyChanged, args->values); |
| 4281 | |
| 4282 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
| 4283 | mLock.unlock(); |
| 4284 | } // release lock |
| 4285 | |
| 4286 | if (needWake) { |
| 4287 | mLooper->wake(); |
| 4288 | } |
| 4289 | } |
| 4290 | |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 4291 | void InputDispatcher::notifyVibratorState(const NotifyVibratorStateArgs* args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame^] | 4292 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4293 | ALOGD("notifyVibratorState - eventTime=%" PRId64 ", device=%d, isOn=%d", args->eventTime, |
| 4294 | args->deviceId, args->isOn); |
| 4295 | } |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 4296 | mPolicy->notifyVibratorState(args->deviceId, args->isOn); |
| 4297 | } |
| 4298 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4299 | bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4300 | return mInputFilterEnabled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4301 | } |
| 4302 | |
| 4303 | void InputDispatcher::notifySwitch(const NotifySwitchArgs* args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame^] | 4304 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4305 | ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, " |
| 4306 | "switchMask=0x%08x", |
| 4307 | args->eventTime, args->policyFlags, args->switchValues, args->switchMask); |
| 4308 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4309 | |
| 4310 | uint32_t policyFlags = args->policyFlags; |
| 4311 | policyFlags |= POLICY_FLAG_TRUSTED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4312 | mPolicy->notifySwitch(args->eventTime, args->switchValues, args->switchMask, policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4313 | } |
| 4314 | |
| 4315 | void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs* 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("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args->eventTime, |
| 4318 | args->deviceId); |
| 4319 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4320 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4321 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4322 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4323 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4324 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4325 | std::unique_ptr<DeviceResetEntry> newEntry = |
| 4326 | std::make_unique<DeviceResetEntry>(args->id, args->eventTime, args->deviceId); |
| 4327 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4328 | } // release lock |
| 4329 | |
| 4330 | if (needWake) { |
| 4331 | mLooper->wake(); |
| 4332 | } |
| 4333 | } |
| 4334 | |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4335 | void InputDispatcher::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs* args) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame^] | 4336 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4337 | ALOGD("notifyPointerCaptureChanged - eventTime=%" PRId64 ", enabled=%s", args->eventTime, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4338 | args->request.enable ? "true" : "false"); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4339 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4340 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4341 | bool needWake = false; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4342 | { // acquire lock |
| 4343 | std::scoped_lock _l(mLock); |
| 4344 | auto entry = std::make_unique<PointerCaptureChangedEntry>(args->id, args->eventTime, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4345 | args->request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4346 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 4347 | } // release lock |
| 4348 | |
| 4349 | if (needWake) { |
| 4350 | mLooper->wake(); |
| 4351 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4352 | } |
| 4353 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4354 | InputEventInjectionResult InputDispatcher::injectInputEvent(const InputEvent* event, |
| 4355 | std::optional<int32_t> targetUid, |
| 4356 | InputEventInjectionSync syncMode, |
| 4357 | std::chrono::milliseconds timeout, |
| 4358 | uint32_t policyFlags) { |
Prabir Pradhan | 6561380 | 2023-02-22 23:36:58 +0000 | [diff] [blame^] | 4359 | if (debugInboundEventDetails()) { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4360 | ALOGD("injectInputEvent - eventType=%d, targetUid=%s, syncMode=%d, timeout=%lld, " |
| 4361 | "policyFlags=0x%08x", |
| 4362 | event->getType(), targetUid ? std::to_string(*targetUid).c_str() : "none", syncMode, |
| 4363 | timeout.count(), policyFlags); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4364 | } |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 4365 | 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] | 4366 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4367 | policyFlags |= POLICY_FLAG_INJECTED | POLICY_FLAG_TRUSTED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4368 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4369 | // 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] | 4370 | // that have gone through the InputFilter. If the event passed through the InputFilter, assign |
| 4371 | // the provided device id. If the InputFilter is accessibility, and it modifies or synthesizes |
| 4372 | // the injected event, it is responsible for setting POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY. |
| 4373 | // For those events, we will set FLAG_IS_ACCESSIBILITY_EVENT to allow apps to distinguish them |
| 4374 | // from events that originate from actual hardware. |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4375 | int32_t resolvedDeviceId = VIRTUAL_KEYBOARD_ID; |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4376 | if (policyFlags & POLICY_FLAG_FILTERED) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4377 | resolvedDeviceId = event->getDeviceId(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4378 | } |
| 4379 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4380 | std::queue<std::unique_ptr<EventEntry>> injectedEntries; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4381 | switch (event->getType()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4382 | case AINPUT_EVENT_TYPE_KEY: { |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4383 | const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event); |
| 4384 | int32_t action = incomingKey.getAction(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4385 | if (!validateKeyEvent(action)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4386 | return InputEventInjectionResult::FAILED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4387 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4388 | |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4389 | int32_t flags = incomingKey.getFlags(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4390 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4391 | flags |= AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4392 | } |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4393 | int32_t keyCode = incomingKey.getKeyCode(); |
| 4394 | int32_t metaState = incomingKey.getMetaState(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4395 | accelerateMetaShortcuts(resolvedDeviceId, action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4396 | /*byref*/ keyCode, /*byref*/ metaState); |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4397 | KeyEvent keyEvent; |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4398 | keyEvent.initialize(incomingKey.getId(), resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4399 | incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode, |
| 4400 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), |
| 4401 | incomingKey.getDownTime(), incomingKey.getEventTime()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4402 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4403 | if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) { |
| 4404 | policyFlags |= POLICY_FLAG_VIRTUAL; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4405 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4406 | |
| 4407 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 4408 | android::base::Timer t; |
| 4409 | mPolicy->interceptKeyBeforeQueueing(&keyEvent, /*byref*/ policyFlags); |
| 4410 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4411 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
| 4412 | std::to_string(t.duration().count()).c_str()); |
| 4413 | } |
| 4414 | } |
| 4415 | |
| 4416 | mLock.lock(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4417 | std::unique_ptr<KeyEntry> injectedEntry = |
| 4418 | std::make_unique<KeyEntry>(incomingKey.getId(), incomingKey.getEventTime(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4419 | resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4420 | incomingKey.getDisplayId(), policyFlags, action, |
| 4421 | flags, keyCode, incomingKey.getScanCode(), metaState, |
| 4422 | incomingKey.getRepeatCount(), |
| 4423 | incomingKey.getDownTime()); |
| 4424 | injectedEntries.push(std::move(injectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4425 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4426 | } |
| 4427 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4428 | case AINPUT_EVENT_TYPE_MOTION: { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4429 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4430 | const int32_t action = motionEvent.getAction(); |
| 4431 | const bool isPointerEvent = |
| 4432 | isFromSource(event->getSource(), AINPUT_SOURCE_CLASS_POINTER); |
| 4433 | // If a pointer event has no displayId specified, inject it to the default display. |
| 4434 | const uint32_t displayId = isPointerEvent && (event->getDisplayId() == ADISPLAY_ID_NONE) |
| 4435 | ? ADISPLAY_ID_DEFAULT |
| 4436 | : event->getDisplayId(); |
| 4437 | const size_t pointerCount = motionEvent.getPointerCount(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4438 | const PointerProperties* pointerProperties = motionEvent.getPointerProperties(); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4439 | const int32_t actionButton = motionEvent.getActionButton(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4440 | int32_t flags = motionEvent.getFlags(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4441 | if (!validateMotionEvent(action, actionButton, pointerCount, pointerProperties)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4442 | return InputEventInjectionResult::FAILED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4443 | } |
| 4444 | |
| 4445 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4446 | nsecs_t eventTime = motionEvent.getEventTime(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4447 | android::base::Timer t; |
| 4448 | mPolicy->interceptMotionBeforeQueueing(displayId, eventTime, /*byref*/ policyFlags); |
| 4449 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4450 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
| 4451 | std::to_string(t.duration().count()).c_str()); |
| 4452 | } |
| 4453 | } |
| 4454 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4455 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4456 | flags |= AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4457 | } |
| 4458 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4459 | mLock.lock(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4460 | const nsecs_t* sampleEventTimes = motionEvent.getSampleEventTimes(); |
| 4461 | const PointerCoords* samplePointerCoords = motionEvent.getSamplePointerCoords(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4462 | std::unique_ptr<MotionEntry> injectedEntry = |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4463 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, |
| 4464 | resolvedDeviceId, motionEvent.getSource(), |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4465 | displayId, policyFlags, action, actionButton, |
| 4466 | flags, motionEvent.getMetaState(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4467 | motionEvent.getButtonState(), |
| 4468 | motionEvent.getClassification(), |
| 4469 | motionEvent.getEdgeFlags(), |
| 4470 | motionEvent.getXPrecision(), |
| 4471 | motionEvent.getYPrecision(), |
| 4472 | motionEvent.getRawXCursorPosition(), |
| 4473 | motionEvent.getRawYCursorPosition(), |
| 4474 | motionEvent.getDownTime(), uint32_t(pointerCount), |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4475 | pointerProperties, samplePointerCoords); |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4476 | transformMotionEntryForInjectionLocked(*injectedEntry, motionEvent.getTransform()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4477 | injectedEntries.push(std::move(injectedEntry)); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4478 | for (size_t i = motionEvent.getHistorySize(); i > 0; i--) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4479 | sampleEventTimes += 1; |
| 4480 | samplePointerCoords += pointerCount; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4481 | std::unique_ptr<MotionEntry> nextInjectedEntry = |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4482 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, |
| 4483 | resolvedDeviceId, motionEvent.getSource(), |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4484 | displayId, policyFlags, action, actionButton, |
| 4485 | flags, motionEvent.getMetaState(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4486 | motionEvent.getButtonState(), |
| 4487 | motionEvent.getClassification(), |
| 4488 | motionEvent.getEdgeFlags(), |
| 4489 | motionEvent.getXPrecision(), |
| 4490 | motionEvent.getYPrecision(), |
| 4491 | motionEvent.getRawXCursorPosition(), |
| 4492 | motionEvent.getRawYCursorPosition(), |
| 4493 | motionEvent.getDownTime(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4494 | uint32_t(pointerCount), pointerProperties, |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4495 | samplePointerCoords); |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4496 | transformMotionEntryForInjectionLocked(*nextInjectedEntry, |
| 4497 | motionEvent.getTransform()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4498 | injectedEntries.push(std::move(nextInjectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4499 | } |
| 4500 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4501 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4502 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4503 | default: |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 4504 | ALOGW("Cannot inject %s events", inputEventTypeToString(event->getType())); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4505 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4506 | } |
| 4507 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4508 | InjectionState* injectionState = new InjectionState(targetUid); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4509 | if (syncMode == InputEventInjectionSync::NONE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4510 | injectionState->injectionIsAsync = true; |
| 4511 | } |
| 4512 | |
| 4513 | injectionState->refCount += 1; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4514 | injectedEntries.back()->injectionState = injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4515 | |
| 4516 | bool needWake = false; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4517 | while (!injectedEntries.empty()) { |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 4518 | if (DEBUG_INJECTION) { |
| 4519 | LOG(DEBUG) << "Injecting " << injectedEntries.front()->getDescription(); |
| 4520 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4521 | needWake |= enqueueInboundEventLocked(std::move(injectedEntries.front())); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4522 | injectedEntries.pop(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4523 | } |
| 4524 | |
| 4525 | mLock.unlock(); |
| 4526 | |
| 4527 | if (needWake) { |
| 4528 | mLooper->wake(); |
| 4529 | } |
| 4530 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4531 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4532 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4533 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4534 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4535 | if (syncMode == InputEventInjectionSync::NONE) { |
| 4536 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4537 | } else { |
| 4538 | for (;;) { |
| 4539 | injectionResult = injectionState->injectionResult; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4540 | if (injectionResult != InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4541 | break; |
| 4542 | } |
| 4543 | |
| 4544 | nsecs_t remainingTimeout = endTime - now(); |
| 4545 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4546 | if (DEBUG_INJECTION) { |
| 4547 | ALOGD("injectInputEvent - Timed out waiting for injection result " |
| 4548 | "to become available."); |
| 4549 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4550 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4551 | break; |
| 4552 | } |
| 4553 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4554 | mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4555 | } |
| 4556 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4557 | if (injectionResult == InputEventInjectionResult::SUCCEEDED && |
| 4558 | syncMode == InputEventInjectionSync::WAIT_FOR_FINISHED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4559 | while (injectionState->pendingForegroundDispatches != 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4560 | if (DEBUG_INJECTION) { |
| 4561 | ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", |
| 4562 | injectionState->pendingForegroundDispatches); |
| 4563 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4564 | nsecs_t remainingTimeout = endTime - now(); |
| 4565 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4566 | if (DEBUG_INJECTION) { |
| 4567 | ALOGD("injectInputEvent - Timed out waiting for pending foreground " |
| 4568 | "dispatches to finish."); |
| 4569 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4570 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4571 | break; |
| 4572 | } |
| 4573 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4574 | mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4575 | } |
| 4576 | } |
| 4577 | } |
| 4578 | |
| 4579 | injectionState->release(); |
| 4580 | } // release lock |
| 4581 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4582 | if (DEBUG_INJECTION) { |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 4583 | LOG(DEBUG) << "injectInputEvent - Finished with result " |
| 4584 | << ftl::enum_string(injectionResult); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4585 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4586 | |
| 4587 | return injectionResult; |
| 4588 | } |
| 4589 | |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4590 | std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4591 | std::array<uint8_t, 32> calculatedHmac; |
| 4592 | std::unique_ptr<VerifiedInputEvent> result; |
| 4593 | switch (event.getType()) { |
| 4594 | case AINPUT_EVENT_TYPE_KEY: { |
| 4595 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event); |
| 4596 | VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent); |
| 4597 | result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4598 | calculatedHmac = sign(verifiedKeyEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4599 | break; |
| 4600 | } |
| 4601 | case AINPUT_EVENT_TYPE_MOTION: { |
| 4602 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event); |
| 4603 | VerifiedMotionEvent verifiedMotionEvent = |
| 4604 | verifiedMotionEventFromMotionEvent(motionEvent); |
| 4605 | result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4606 | calculatedHmac = sign(verifiedMotionEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4607 | break; |
| 4608 | } |
| 4609 | default: { |
| 4610 | ALOGE("Cannot verify events of type %" PRId32, event.getType()); |
| 4611 | return nullptr; |
| 4612 | } |
| 4613 | } |
| 4614 | if (calculatedHmac == INVALID_HMAC) { |
| 4615 | return nullptr; |
| 4616 | } |
| 4617 | if (calculatedHmac != event.getHmac()) { |
| 4618 | return nullptr; |
| 4619 | } |
| 4620 | return result; |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4621 | } |
| 4622 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4623 | void InputDispatcher::setInjectionResult(EventEntry& entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4624 | InputEventInjectionResult injectionResult) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4625 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4626 | if (injectionState) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4627 | if (DEBUG_INJECTION) { |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 4628 | LOG(DEBUG) << "Setting input event injection result to " |
| 4629 | << ftl::enum_string(injectionResult); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4630 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4631 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4632 | if (injectionState->injectionIsAsync && !(entry.policyFlags & POLICY_FLAG_FILTERED)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4633 | // Log the outcome since the injector did not wait for the injection result. |
| 4634 | switch (injectionResult) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4635 | case InputEventInjectionResult::SUCCEEDED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4636 | ALOGV("Asynchronous input event injection succeeded."); |
| 4637 | break; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4638 | case InputEventInjectionResult::TARGET_MISMATCH: |
| 4639 | ALOGV("Asynchronous input event injection target mismatch."); |
| 4640 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4641 | case InputEventInjectionResult::FAILED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4642 | ALOGW("Asynchronous input event injection failed."); |
| 4643 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4644 | case InputEventInjectionResult::TIMED_OUT: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4645 | ALOGW("Asynchronous input event injection timed out."); |
| 4646 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4647 | case InputEventInjectionResult::PENDING: |
| 4648 | ALOGE("Setting result to 'PENDING' for asynchronous injection"); |
| 4649 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4650 | } |
| 4651 | } |
| 4652 | |
| 4653 | injectionState->injectionResult = injectionResult; |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4654 | mInjectionResultAvailable.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4655 | } |
| 4656 | } |
| 4657 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4658 | void InputDispatcher::transformMotionEntryForInjectionLocked( |
| 4659 | MotionEntry& entry, const ui::Transform& injectedTransform) const { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4660 | // Input injection works in the logical display coordinate space, but the input pipeline works |
| 4661 | // display space, so we need to transform the injected events accordingly. |
| 4662 | const auto it = mDisplayInfos.find(entry.displayId); |
| 4663 | if (it == mDisplayInfos.end()) return; |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4664 | const auto& transformToDisplay = it->second.transform.inverse() * injectedTransform; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4665 | |
Prabir Pradhan | d9a2ebe | 2022-07-20 19:25:13 +0000 | [diff] [blame] | 4666 | if (entry.xCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION && |
| 4667 | entry.yCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) { |
| 4668 | const vec2 cursor = |
| 4669 | MotionEvent::calculateTransformedXY(entry.source, transformToDisplay, |
| 4670 | {entry.xCursorPosition, entry.yCursorPosition}); |
| 4671 | entry.xCursorPosition = cursor.x; |
| 4672 | entry.yCursorPosition = cursor.y; |
| 4673 | } |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4674 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
Prabir Pradhan | 8e6ce22 | 2022-02-24 09:08:54 -0800 | [diff] [blame] | 4675 | entry.pointerCoords[i] = |
| 4676 | MotionEvent::calculateTransformedCoords(entry.source, transformToDisplay, |
| 4677 | entry.pointerCoords[i]); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4678 | } |
| 4679 | } |
| 4680 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4681 | void InputDispatcher::incrementPendingForegroundDispatches(EventEntry& entry) { |
| 4682 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4683 | if (injectionState) { |
| 4684 | injectionState->pendingForegroundDispatches += 1; |
| 4685 | } |
| 4686 | } |
| 4687 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4688 | void InputDispatcher::decrementPendingForegroundDispatches(EventEntry& entry) { |
| 4689 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4690 | if (injectionState) { |
| 4691 | injectionState->pendingForegroundDispatches -= 1; |
| 4692 | |
| 4693 | if (injectionState->pendingForegroundDispatches == 0) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4694 | mInjectionSyncFinished.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4695 | } |
| 4696 | } |
| 4697 | } |
| 4698 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4699 | const std::vector<sp<WindowInfoHandle>>& InputDispatcher::getWindowHandlesLocked( |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4700 | int32_t displayId) const { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4701 | static const std::vector<sp<WindowInfoHandle>> EMPTY_WINDOW_HANDLES; |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4702 | auto it = mWindowHandlesByDisplay.find(displayId); |
| 4703 | return it != mWindowHandlesByDisplay.end() ? it->second : EMPTY_WINDOW_HANDLES; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4704 | } |
| 4705 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4706 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4707 | const sp<IBinder>& windowHandleToken) const { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4708 | if (windowHandleToken == nullptr) { |
| 4709 | return nullptr; |
| 4710 | } |
| 4711 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4712 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4713 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 4714 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4715 | if (windowHandle->getToken() == windowHandleToken) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4716 | return windowHandle; |
| 4717 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4718 | } |
| 4719 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4720 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4721 | } |
| 4722 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4723 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked(const sp<IBinder>& windowHandleToken, |
| 4724 | int displayId) const { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4725 | if (windowHandleToken == nullptr) { |
| 4726 | return nullptr; |
| 4727 | } |
| 4728 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4729 | for (const sp<WindowInfoHandle>& windowHandle : getWindowHandlesLocked(displayId)) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4730 | if (windowHandle->getToken() == windowHandleToken) { |
| 4731 | return windowHandle; |
| 4732 | } |
| 4733 | } |
| 4734 | return nullptr; |
| 4735 | } |
| 4736 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4737 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
| 4738 | const sp<WindowInfoHandle>& windowHandle) const { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4739 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4740 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 4741 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4742 | if (handle->getId() == windowHandle->getId() && |
| 4743 | handle->getToken() == windowHandle->getToken()) { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4744 | if (windowHandle->getInfo()->displayId != it.first) { |
| 4745 | ALOGE("Found window %s in display %" PRId32 |
| 4746 | ", but it should belong to display %" PRId32, |
| 4747 | windowHandle->getName().c_str(), it.first, |
| 4748 | windowHandle->getInfo()->displayId); |
| 4749 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4750 | return handle; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4751 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4752 | } |
| 4753 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4754 | return nullptr; |
| 4755 | } |
| 4756 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4757 | sp<WindowInfoHandle> InputDispatcher::getFocusedWindowHandleLocked(int displayId) const { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4758 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(displayId); |
| 4759 | return getWindowHandleLocked(focusedToken, displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4760 | } |
| 4761 | |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 4762 | bool InputDispatcher::canWindowReceiveMotionLocked(const sp<WindowInfoHandle>& window, |
| 4763 | const MotionEntry& motionEntry) const { |
| 4764 | const WindowInfo& info = *window->getInfo(); |
| 4765 | |
| 4766 | // Skip spy window targets that are not valid for targeted injection. |
| 4767 | if (const auto err = verifyTargetedInjection(window, motionEntry); err) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4768 | return false; |
| 4769 | } |
| 4770 | |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 4771 | if (info.inputConfig.test(WindowInfo::InputConfig::PAUSE_DISPATCHING)) { |
| 4772 | ALOGI("Not sending touch event to %s because it is paused", window->getName().c_str()); |
| 4773 | return false; |
| 4774 | } |
| 4775 | |
| 4776 | if (info.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) { |
| 4777 | ALOGW("Not sending touch gesture to %s because it has config NO_INPUT_CHANNEL", |
| 4778 | window->getName().c_str()); |
| 4779 | return false; |
| 4780 | } |
| 4781 | |
| 4782 | sp<Connection> connection = getConnectionLocked(window->getToken()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4783 | if (connection == nullptr) { |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 4784 | ALOGW("Not sending touch to %s because there's no corresponding connection", |
| 4785 | window->getName().c_str()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4786 | return false; |
| 4787 | } |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 4788 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4789 | if (!connection->responsive) { |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 4790 | 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] | 4791 | return false; |
| 4792 | } |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 4793 | |
| 4794 | // Drop events that can't be trusted due to occlusion |
| 4795 | const auto [x, y] = resolveTouchedPosition(motionEntry); |
| 4796 | TouchOcclusionInfo occlusionInfo = computeTouchOcclusionInfoLocked(window, x, y); |
| 4797 | if (!isTouchTrustedLocked(occlusionInfo)) { |
| 4798 | if (DEBUG_TOUCH_OCCLUSION) { |
| 4799 | ALOGD("Stack of obscuring windows during untrusted touch (%d, %d):", x, y); |
| 4800 | for (const auto& log : occlusionInfo.debugInfo) { |
| 4801 | ALOGD("%s", log.c_str()); |
| 4802 | } |
| 4803 | } |
| 4804 | ALOGW("Dropping untrusted touch event due to %s/%d", occlusionInfo.obscuringPackage.c_str(), |
| 4805 | occlusionInfo.obscuringUid); |
| 4806 | return false; |
| 4807 | } |
| 4808 | |
| 4809 | // Drop touch events if requested by input feature |
| 4810 | if (shouldDropInput(motionEntry, window)) { |
| 4811 | return false; |
| 4812 | } |
| 4813 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4814 | return true; |
| 4815 | } |
| 4816 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4817 | std::shared_ptr<InputChannel> InputDispatcher::getInputChannelLocked( |
| 4818 | const sp<IBinder>& token) const { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4819 | auto connectionIt = mConnectionsByToken.find(token); |
| 4820 | if (connectionIt == mConnectionsByToken.end()) { |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4821 | return nullptr; |
| 4822 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4823 | return connectionIt->second->inputChannel; |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4824 | } |
| 4825 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4826 | void InputDispatcher::updateWindowHandlesForDisplayLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4827 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
| 4828 | if (windowInfoHandles.empty()) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4829 | // Remove all handles on a display if there are no windows left. |
| 4830 | mWindowHandlesByDisplay.erase(displayId); |
| 4831 | return; |
| 4832 | } |
| 4833 | |
| 4834 | // Since we compare the pointer of input window handles across window updates, we need |
| 4835 | // 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] | 4836 | const std::vector<sp<WindowInfoHandle>>& oldHandles = getWindowHandlesLocked(displayId); |
| 4837 | std::unordered_map<int32_t /*id*/, sp<WindowInfoHandle>> oldHandlesById; |
| 4838 | for (const sp<WindowInfoHandle>& handle : oldHandles) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4839 | oldHandlesById[handle->getId()] = handle; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4840 | } |
| 4841 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4842 | std::vector<sp<WindowInfoHandle>> newHandles; |
| 4843 | for (const sp<WindowInfoHandle>& handle : windowInfoHandles) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4844 | const WindowInfo* info = handle->getInfo(); |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 4845 | if (getInputChannelLocked(handle->getToken()) == nullptr) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4846 | const bool noInputChannel = |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 4847 | info->inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4848 | const bool canReceiveInput = |
| 4849 | !info->inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE) || |
| 4850 | !info->inputConfig.test(WindowInfo::InputConfig::NOT_FOCUSABLE); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4851 | if (canReceiveInput && !noInputChannel) { |
John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 4852 | ALOGV("Window handle %s has no registered input channel", |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4853 | handle->getName().c_str()); |
Robert Carr | 2984b7a | 2020-04-13 17:06:45 -0700 | [diff] [blame] | 4854 | continue; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4855 | } |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4856 | } |
| 4857 | |
| 4858 | if (info->displayId != displayId) { |
| 4859 | ALOGE("Window %s updated by wrong display %d, should belong to display %d", |
| 4860 | handle->getName().c_str(), displayId, info->displayId); |
| 4861 | continue; |
| 4862 | } |
| 4863 | |
Robert Carr | edd1360 | 2020-04-13 17:24:34 -0700 | [diff] [blame] | 4864 | if ((oldHandlesById.find(handle->getId()) != oldHandlesById.end()) && |
| 4865 | (oldHandlesById.at(handle->getId())->getToken() == handle->getToken())) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4866 | const sp<WindowInfoHandle>& oldHandle = oldHandlesById.at(handle->getId()); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4867 | oldHandle->updateFrom(handle); |
| 4868 | newHandles.push_back(oldHandle); |
| 4869 | } else { |
| 4870 | newHandles.push_back(handle); |
| 4871 | } |
| 4872 | } |
| 4873 | |
| 4874 | // Insert or replace |
| 4875 | mWindowHandlesByDisplay[displayId] = newHandles; |
| 4876 | } |
| 4877 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4878 | void InputDispatcher::setInputWindows( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4879 | const std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>>& handlesPerDisplay) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 4880 | // TODO(b/198444055): Remove setInputWindows from InputDispatcher. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4881 | { // acquire lock |
| 4882 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 4883 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 4884 | setInputWindowsLocked(handles, displayId); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4885 | } |
| 4886 | } |
| 4887 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4888 | mLooper->wake(); |
| 4889 | } |
| 4890 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4891 | /** |
| 4892 | * Called from InputManagerService, update window handle list by displayId that can receive input. |
| 4893 | * A window handle contains information about InputChannel, Touch Region, Types, Focused,... |
| 4894 | * If set an empty list, remove all handles from the specific display. |
| 4895 | * For focused handle, check if need to change and send a cancel event to previous one. |
| 4896 | * For removed handle, check if need to send a cancel event if already in touch. |
| 4897 | */ |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4898 | void InputDispatcher::setInputWindowsLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4899 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4900 | if (DEBUG_FOCUS) { |
| 4901 | std::string windowList; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4902 | for (const sp<WindowInfoHandle>& iwh : windowInfoHandles) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4903 | windowList += iwh->getName() + " "; |
| 4904 | } |
| 4905 | ALOGD("setInputWindows displayId=%" PRId32 " %s", displayId, windowList.c_str()); |
| 4906 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4907 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4908 | // Check preconditions for new input windows |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4909 | for (const sp<WindowInfoHandle>& window : windowInfoHandles) { |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4910 | const WindowInfo& info = *window->getInfo(); |
| 4911 | |
| 4912 | // 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] | 4913 | const bool noInputWindow = info.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4914 | if (noInputWindow && window->getToken() != nullptr) { |
| 4915 | ALOGE("%s has feature NO_INPUT_WINDOW, but a non-null token. Clearing", |
| 4916 | window->getName().c_str()); |
| 4917 | window->releaseChannel(); |
| 4918 | } |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4919 | |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 4920 | // Ensure all spy windows are trusted overlays |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4921 | LOG_ALWAYS_FATAL_IF(info.isSpy() && |
| 4922 | !info.inputConfig.test( |
| 4923 | WindowInfo::InputConfig::TRUSTED_OVERLAY), |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 4924 | "%s has feature SPY, but is not a trusted overlay.", |
| 4925 | window->getName().c_str()); |
| 4926 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4927 | // Ensure all stylus interceptors are trusted overlays |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4928 | LOG_ALWAYS_FATAL_IF(info.interceptsStylus() && |
| 4929 | !info.inputConfig.test( |
| 4930 | WindowInfo::InputConfig::TRUSTED_OVERLAY), |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4931 | "%s has feature INTERCEPTS_STYLUS, but is not a trusted overlay.", |
| 4932 | window->getName().c_str()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4933 | } |
| 4934 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4935 | // Copy old handles for release if they are no longer present. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4936 | const std::vector<sp<WindowInfoHandle>> oldWindowHandles = getWindowHandlesLocked(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4937 | |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4938 | // Save the old windows' orientation by ID before it gets updated. |
| 4939 | std::unordered_map<int32_t, uint32_t> oldWindowOrientations; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4940 | for (const sp<WindowInfoHandle>& handle : oldWindowHandles) { |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4941 | oldWindowOrientations.emplace(handle->getId(), |
| 4942 | handle->getInfo()->transform.getOrientation()); |
| 4943 | } |
| 4944 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4945 | updateWindowHandlesForDisplayLocked(windowInfoHandles, displayId); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4946 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4947 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4948 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4949 | std::optional<FocusResolver::FocusChanges> changes = |
| 4950 | mFocusResolver.setInputWindows(displayId, windowHandles); |
| 4951 | if (changes) { |
| 4952 | onFocusChangedLocked(*changes); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4953 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4954 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4955 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 4956 | mTouchStatesByDisplay.find(displayId); |
| 4957 | if (stateIt != mTouchStatesByDisplay.end()) { |
| 4958 | TouchState& state = stateIt->second; |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4959 | for (size_t i = 0; i < state.windows.size();) { |
| 4960 | TouchedWindow& touchedWindow = state.windows[i]; |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4961 | if (getWindowHandleLocked(touchedWindow.windowHandle) == nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4962 | if (DEBUG_FOCUS) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4963 | ALOGD("Touched window was removed: %s in display %" PRId32, |
| 4964 | touchedWindow.windowHandle->getName().c_str(), displayId); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4965 | } |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4966 | std::shared_ptr<InputChannel> touchedInputChannel = |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4967 | getInputChannelLocked(touchedWindow.windowHandle->getToken()); |
| 4968 | if (touchedInputChannel != nullptr) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 4969 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4970 | "touched window was removed"); |
| 4971 | synthesizeCancelationEventsForInputChannelLocked(touchedInputChannel, options); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4972 | // Since we are about to drop the touch, cancel the events for the wallpaper as |
| 4973 | // well. |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 4974 | if (touchedWindow.targetFlags.test(InputTarget::Flags::FOREGROUND) && |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4975 | touchedWindow.windowHandle->getInfo()->inputConfig.test( |
| 4976 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER)) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4977 | sp<WindowInfoHandle> wallpaper = state.getWallpaperWindow(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4978 | synthesizeCancelationEventsForWindowLocked(wallpaper, options); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4979 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4980 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4981 | state.windows.erase(state.windows.begin() + i); |
| 4982 | } else { |
| 4983 | ++i; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4984 | } |
| 4985 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4986 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4987 | // 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] | 4988 | // could just clear the state here. |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 4989 | if (mDragState && mDragState->dragWindow->getInfo()->displayId == displayId && |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4990 | std::find(windowHandles.begin(), windowHandles.end(), mDragState->dragWindow) == |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4991 | windowHandles.end()) { |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 4992 | ALOGI("Drag window went away: %s", mDragState->dragWindow->getName().c_str()); |
| 4993 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4994 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4995 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4996 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4997 | |
Prabir Pradhan | 8b89c2f | 2021-07-29 16:30:14 +0000 | [diff] [blame] | 4998 | // Determine if the orientation of any of the input windows have changed, and cancel all |
| 4999 | // pointer events if necessary. |
| 5000 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
| 5001 | const sp<WindowInfoHandle> newWindowHandle = getWindowHandleLocked(oldWindowHandle); |
| 5002 | if (newWindowHandle != nullptr && |
| 5003 | newWindowHandle->getInfo()->transform.getOrientation() != |
| 5004 | oldWindowOrientations[oldWindowHandle->getId()]) { |
| 5005 | std::shared_ptr<InputChannel> inputChannel = |
| 5006 | getInputChannelLocked(newWindowHandle->getToken()); |
| 5007 | if (inputChannel != nullptr) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 5008 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
Prabir Pradhan | 8b89c2f | 2021-07-29 16:30:14 +0000 | [diff] [blame] | 5009 | "touched window's orientation changed"); |
| 5010 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 5011 | } |
| 5012 | } |
| 5013 | } |
| 5014 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5015 | // Release information for windows that are no longer present. |
| 5016 | // This ensures that unused input channels are released promptly. |
| 5017 | // Otherwise, they might stick around until the window handle is destroyed |
| 5018 | // which might not happen until the next GC. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5019 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 5020 | if (getWindowHandleLocked(oldWindowHandle) == nullptr) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5021 | if (DEBUG_FOCUS) { |
| 5022 | ALOGD("Window went away: %s", oldWindowHandle->getName().c_str()); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 5023 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5024 | oldWindowHandle->releaseChannel(); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 5025 | } |
chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 5026 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5027 | } |
| 5028 | |
| 5029 | void InputDispatcher::setFocusedApplication( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5030 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5031 | if (DEBUG_FOCUS) { |
| 5032 | ALOGD("setFocusedApplication displayId=%" PRId32 " %s", displayId, |
| 5033 | inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>"); |
| 5034 | } |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 5035 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5036 | std::scoped_lock _l(mLock); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 5037 | setFocusedApplicationLocked(displayId, inputApplicationHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5038 | } // release lock |
| 5039 | |
| 5040 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5041 | mLooper->wake(); |
| 5042 | } |
| 5043 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 5044 | void InputDispatcher::setFocusedApplicationLocked( |
| 5045 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
| 5046 | std::shared_ptr<InputApplicationHandle> oldFocusedApplicationHandle = |
| 5047 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 5048 | |
| 5049 | if (sharedPointersEqual(oldFocusedApplicationHandle, inputApplicationHandle)) { |
| 5050 | return; // This application is already focused. No need to wake up or change anything. |
| 5051 | } |
| 5052 | |
| 5053 | // Set the new application handle. |
| 5054 | if (inputApplicationHandle != nullptr) { |
| 5055 | mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle; |
| 5056 | } else { |
| 5057 | mFocusedApplicationHandlesByDisplay.erase(displayId); |
| 5058 | } |
| 5059 | |
| 5060 | // No matter what the old focused application was, stop waiting on it because it is |
| 5061 | // no longer focused. |
| 5062 | resetNoFocusedWindowTimeoutLocked(); |
| 5063 | } |
| 5064 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5065 | /** |
| 5066 | * Sets the focused display, which is responsible for receiving focus-dispatched input events where |
| 5067 | * the display not specified. |
| 5068 | * |
| 5069 | * We track any unreleased events for each window. If a window loses the ability to receive the |
| 5070 | * released event, we will send a cancel event to it. So when the focused display is changed, we |
| 5071 | * cancel all the unreleased display-unspecified events for the focused window on the old focused |
| 5072 | * display. The display-specified events won't be affected. |
| 5073 | */ |
| 5074 | void InputDispatcher::setFocusedDisplay(int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5075 | if (DEBUG_FOCUS) { |
| 5076 | ALOGD("setFocusedDisplay displayId=%" PRId32, displayId); |
| 5077 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5078 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5079 | std::scoped_lock _l(mLock); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5080 | |
| 5081 | if (mFocusedDisplayId != displayId) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5082 | sp<IBinder> oldFocusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5083 | mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5084 | if (oldFocusedWindowToken != nullptr) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5085 | std::shared_ptr<InputChannel> inputChannel = |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5086 | getInputChannelLocked(oldFocusedWindowToken); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5087 | if (inputChannel != nullptr) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5088 | CancelationOptions |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 5089 | options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5090 | "The display which contains this window no longer has focus."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5091 | options.displayId = ADISPLAY_ID_NONE; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5092 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
| 5093 | } |
| 5094 | } |
| 5095 | mFocusedDisplayId = displayId; |
| 5096 | |
Chris Ye | 3c2d6f5 | 2020-08-09 10:39:48 -0700 | [diff] [blame] | 5097 | // Find new focused window and validate |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5098 | sp<IBinder> newFocusedWindowToken = mFocusResolver.getFocusedWindowToken(displayId); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5099 | sendFocusChangedCommandLocked(oldFocusedWindowToken, newFocusedWindowToken); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5100 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5101 | if (newFocusedWindowToken == nullptr) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5102 | ALOGW("Focused display #%" PRId32 " does not have a focused window.", displayId); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5103 | if (mFocusResolver.hasFocusedWindowTokens()) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5104 | ALOGE("But another display has a focused window\n%s", |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5105 | mFocusResolver.dumpFocusedWindows().c_str()); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5106 | } |
| 5107 | } |
| 5108 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5109 | } // release lock |
| 5110 | |
| 5111 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5112 | mLooper->wake(); |
| 5113 | } |
| 5114 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5115 | void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5116 | if (DEBUG_FOCUS) { |
| 5117 | ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); |
| 5118 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5119 | |
| 5120 | bool changed; |
| 5121 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5122 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5123 | |
| 5124 | if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { |
| 5125 | if (mDispatchFrozen && !frozen) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5126 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5127 | } |
| 5128 | |
| 5129 | if (mDispatchEnabled && !enabled) { |
| 5130 | resetAndDropEverythingLocked("dispatcher is being disabled"); |
| 5131 | } |
| 5132 | |
| 5133 | mDispatchEnabled = enabled; |
| 5134 | mDispatchFrozen = frozen; |
| 5135 | changed = true; |
| 5136 | } else { |
| 5137 | changed = false; |
| 5138 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5139 | } // release lock |
| 5140 | |
| 5141 | if (changed) { |
| 5142 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5143 | mLooper->wake(); |
| 5144 | } |
| 5145 | } |
| 5146 | |
| 5147 | void InputDispatcher::setInputFilterEnabled(bool enabled) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5148 | if (DEBUG_FOCUS) { |
| 5149 | ALOGD("setInputFilterEnabled: enabled=%d", enabled); |
| 5150 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5151 | |
| 5152 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5153 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5154 | |
| 5155 | if (mInputFilterEnabled == enabled) { |
| 5156 | return; |
| 5157 | } |
| 5158 | |
| 5159 | mInputFilterEnabled = enabled; |
| 5160 | resetAndDropEverythingLocked("input filter is being enabled or disabled"); |
| 5161 | } // release lock |
| 5162 | |
| 5163 | // Wake up poll loop since there might be work to do to drop everything. |
| 5164 | mLooper->wake(); |
| 5165 | } |
| 5166 | |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 5167 | bool InputDispatcher::setInTouchMode(bool inTouchMode, int32_t pid, int32_t uid, bool hasPermission, |
| 5168 | int32_t displayId) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5169 | bool needWake = false; |
| 5170 | { |
| 5171 | std::scoped_lock lock(mLock); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5172 | ALOGD_IF(DEBUG_TOUCH_MODE, |
| 5173 | "Request to change touch mode to %s (calling pid=%d, uid=%d, " |
| 5174 | "hasPermission=%s, target displayId=%d, mTouchModePerDisplay[displayId]=%s)", |
| 5175 | toString(inTouchMode), pid, uid, toString(hasPermission), displayId, |
| 5176 | mTouchModePerDisplay.count(displayId) == 0 |
| 5177 | ? "not set" |
| 5178 | : std::to_string(mTouchModePerDisplay[displayId]).c_str()); |
| 5179 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5180 | auto touchModeIt = mTouchModePerDisplay.find(displayId); |
| 5181 | if (touchModeIt != mTouchModePerDisplay.end() && touchModeIt->second == inTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5182 | return false; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5183 | } |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5184 | if (!hasPermission) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 5185 | if (!focusedWindowIsOwnedByLocked(pid, uid) && |
| 5186 | !recentWindowsAreOwnedByLocked(pid, uid)) { |
| 5187 | ALOGD("Touch mode switch rejected, caller (pid=%d, uid=%d) doesn't own the focused " |
| 5188 | "window nor none of the previously interacted window", |
| 5189 | pid, uid); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5190 | return false; |
| 5191 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5192 | } |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5193 | mTouchModePerDisplay[displayId] = inTouchMode; |
| 5194 | auto entry = std::make_unique<TouchModeEntry>(mIdGenerator.nextId(), now(), inTouchMode, |
| 5195 | displayId); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5196 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 5197 | } // release lock |
| 5198 | |
| 5199 | if (needWake) { |
| 5200 | mLooper->wake(); |
| 5201 | } |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5202 | return true; |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 5203 | } |
| 5204 | |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 5205 | bool InputDispatcher::focusedWindowIsOwnedByLocked(int32_t pid, int32_t uid) { |
| 5206 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
| 5207 | if (focusedToken == nullptr) { |
| 5208 | return false; |
| 5209 | } |
| 5210 | sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(focusedToken); |
| 5211 | return isWindowOwnedBy(windowHandle, pid, uid); |
| 5212 | } |
| 5213 | |
| 5214 | bool InputDispatcher::recentWindowsAreOwnedByLocked(int32_t pid, int32_t uid) { |
| 5215 | return std::find_if(mInteractionConnectionTokens.begin(), mInteractionConnectionTokens.end(), |
| 5216 | [&](const sp<IBinder>& connectionToken) REQUIRES(mLock) { |
| 5217 | const sp<WindowInfoHandle> windowHandle = |
| 5218 | getWindowHandleLocked(connectionToken); |
| 5219 | return isWindowOwnedBy(windowHandle, pid, uid); |
| 5220 | }) != mInteractionConnectionTokens.end(); |
| 5221 | } |
| 5222 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 5223 | void InputDispatcher::setMaximumObscuringOpacityForTouch(float opacity) { |
| 5224 | if (opacity < 0 || opacity > 1) { |
| 5225 | LOG_ALWAYS_FATAL("Maximum obscuring opacity for touch should be >= 0 and <= 1"); |
| 5226 | return; |
| 5227 | } |
| 5228 | |
| 5229 | std::scoped_lock lock(mLock); |
| 5230 | mMaximumObscuringOpacityForTouch = opacity; |
| 5231 | } |
| 5232 | |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5233 | std::tuple<TouchState*, TouchedWindow*, int32_t /*displayId*/> |
| 5234 | InputDispatcher::findTouchStateWindowAndDisplayLocked(const sp<IBinder>& token) { |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5235 | for (auto& [displayId, state] : mTouchStatesByDisplay) { |
| 5236 | for (TouchedWindow& w : state.windows) { |
| 5237 | if (w.windowHandle->getToken() == token) { |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5238 | return std::make_tuple(&state, &w, displayId); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5239 | } |
| 5240 | } |
| 5241 | } |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5242 | return std::make_tuple(nullptr, nullptr, ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5243 | } |
| 5244 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5245 | bool InputDispatcher::transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken, |
| 5246 | bool isDragDrop) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 5247 | if (fromToken == toToken) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5248 | if (DEBUG_FOCUS) { |
| 5249 | ALOGD("Trivial transfer to same window."); |
| 5250 | } |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 5251 | return true; |
| 5252 | } |
| 5253 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5254 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5255 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5256 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5257 | // Find the target touch state and touched window by fromToken. |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5258 | auto [state, touchedWindow, displayId] = findTouchStateWindowAndDisplayLocked(fromToken); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5259 | if (state == nullptr || touchedWindow == nullptr) { |
| 5260 | ALOGD("Focus transfer failed because from window is not being touched."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5261 | return false; |
| 5262 | } |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5263 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5264 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(toToken, displayId); |
| 5265 | if (toWindowHandle == nullptr) { |
| 5266 | ALOGW("Cannot transfer focus because to window not found."); |
| 5267 | return false; |
| 5268 | } |
| 5269 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5270 | if (DEBUG_FOCUS) { |
| 5271 | ALOGD("transferTouchFocus: fromWindowHandle=%s, toWindowHandle=%s", |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5272 | touchedWindow->windowHandle->getName().c_str(), |
| 5273 | toWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5274 | } |
| 5275 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5276 | // Erase old window. |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5277 | ftl::Flags<InputTarget::Flags> oldTargetFlags = touchedWindow->targetFlags; |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 5278 | std::bitset<MAX_POINTER_ID + 1> pointerIds = touchedWindow->pointerIds; |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5279 | sp<WindowInfoHandle> fromWindowHandle = touchedWindow->windowHandle; |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5280 | state->removeWindowByToken(fromToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5281 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5282 | // Add new window. |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 5283 | nsecs_t downTimeInTarget = now(); |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5284 | ftl::Flags<InputTarget::Flags> newTargetFlags = |
| 5285 | oldTargetFlags & (InputTarget::Flags::SPLIT | InputTarget::Flags::DISPATCH_AS_IS); |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 5286 | if (canReceiveForegroundTouches(*toWindowHandle->getInfo())) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5287 | newTargetFlags |= InputTarget::Flags::FOREGROUND; |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 5288 | } |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 5289 | state->addOrUpdateWindow(toWindowHandle, newTargetFlags, pointerIds, downTimeInTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5290 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5291 | // Store the dragging window. |
| 5292 | if (isDragDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 5293 | if (pointerIds.count() != 1) { |
| 5294 | ALOGW("The drag and drop cannot be started when there is no pointer or more than 1" |
| 5295 | " pointer on the window."); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 5296 | return false; |
| 5297 | } |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 5298 | // Track the pointer id for drag window and generate the drag state. |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 5299 | const size_t id = firstMarkedBit(pointerIds); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 5300 | mDragState = std::make_unique<DragState>(toWindowHandle, id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5301 | } |
| 5302 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5303 | // Synthesize cancel for old window and down for new window. |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 5304 | sp<Connection> fromConnection = getConnectionLocked(fromToken); |
| 5305 | sp<Connection> toConnection = getConnectionLocked(toToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5306 | if (fromConnection != nullptr && toConnection != nullptr) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5307 | fromConnection->inputState.mergePointerStateTo(toConnection->inputState); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5308 | CancelationOptions |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 5309 | options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5310 | "transferring touch focus from this window to another window"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5311 | synthesizeCancelationEventsForConnectionLocked(fromConnection, options); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5312 | synthesizePointerDownEventsForConnectionLocked(downTimeInTarget, toConnection, |
| 5313 | newTargetFlags); |
| 5314 | |
| 5315 | // Check if the wallpaper window should deliver the corresponding event. |
| 5316 | transferWallpaperTouch(oldTargetFlags, newTargetFlags, fromWindowHandle, toWindowHandle, |
| 5317 | *state, pointerIds); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5318 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5319 | } // release lock |
| 5320 | |
| 5321 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5322 | mLooper->wake(); |
| 5323 | return true; |
| 5324 | } |
| 5325 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5326 | /** |
| 5327 | * Get the touched foreground window on the given display. |
| 5328 | * Return null if there are no windows touched on that display, or if more than one foreground |
| 5329 | * window is being touched. |
| 5330 | */ |
| 5331 | sp<WindowInfoHandle> InputDispatcher::findTouchedForegroundWindowLocked(int32_t displayId) const { |
| 5332 | auto stateIt = mTouchStatesByDisplay.find(displayId); |
| 5333 | if (stateIt == mTouchStatesByDisplay.end()) { |
| 5334 | ALOGI("No touch state on display %" PRId32, displayId); |
| 5335 | return nullptr; |
| 5336 | } |
| 5337 | |
| 5338 | const TouchState& state = stateIt->second; |
| 5339 | sp<WindowInfoHandle> touchedForegroundWindow; |
| 5340 | // If multiple foreground windows are touched, return nullptr |
| 5341 | for (const TouchedWindow& window : state.windows) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 5342 | if (window.targetFlags.test(InputTarget::Flags::FOREGROUND)) { |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5343 | if (touchedForegroundWindow != nullptr) { |
| 5344 | ALOGI("Two or more foreground windows: %s and %s", |
| 5345 | touchedForegroundWindow->getName().c_str(), |
| 5346 | window.windowHandle->getName().c_str()); |
| 5347 | return nullptr; |
| 5348 | } |
| 5349 | touchedForegroundWindow = window.windowHandle; |
| 5350 | } |
| 5351 | } |
| 5352 | return touchedForegroundWindow; |
| 5353 | } |
| 5354 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5355 | // Binder call |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5356 | bool InputDispatcher::transferTouch(const sp<IBinder>& destChannelToken, int32_t displayId) { |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5357 | sp<IBinder> fromToken; |
| 5358 | { // acquire lock |
| 5359 | std::scoped_lock _l(mLock); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5360 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(destChannelToken, displayId); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5361 | if (toWindowHandle == nullptr) { |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5362 | ALOGW("Could not find window associated with token=%p on display %" PRId32, |
| 5363 | destChannelToken.get(), displayId); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5364 | return false; |
| 5365 | } |
| 5366 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5367 | sp<WindowInfoHandle> from = findTouchedForegroundWindowLocked(displayId); |
| 5368 | if (from == nullptr) { |
| 5369 | ALOGE("Could not find a source window in %s for %p", __func__, destChannelToken.get()); |
| 5370 | return false; |
| 5371 | } |
| 5372 | |
| 5373 | fromToken = from->getToken(); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5374 | } // release lock |
| 5375 | |
| 5376 | return transferTouchFocus(fromToken, destChannelToken); |
| 5377 | } |
| 5378 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5379 | void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5380 | if (DEBUG_FOCUS) { |
| 5381 | ALOGD("Resetting and dropping all events (%s).", reason); |
| 5382 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5383 | |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 5384 | CancelationOptions options(CancelationOptions::Mode::CANCEL_ALL_EVENTS, reason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5385 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 5386 | |
| 5387 | resetKeyRepeatLocked(); |
| 5388 | releasePendingEventLocked(); |
| 5389 | drainInboundQueueLocked(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5390 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5391 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5392 | mAnrTracker.clear(); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5393 | mTouchStatesByDisplay.clear(); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5394 | mReplacedKeys.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5395 | } |
| 5396 | |
| 5397 | void InputDispatcher::logDispatchStateLocked() { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5398 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5399 | dumpDispatchStateLocked(dump); |
| 5400 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5401 | std::istringstream stream(dump); |
| 5402 | std::string line; |
| 5403 | |
| 5404 | while (std::getline(stream, line, '\n')) { |
| 5405 | ALOGD("%s", line.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5406 | } |
| 5407 | } |
| 5408 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5409 | std::string InputDispatcher::dumpPointerCaptureStateLocked() { |
| 5410 | std::string dump; |
| 5411 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5412 | dump += StringPrintf(INDENT "Pointer Capture Requested: %s\n", |
| 5413 | toString(mCurrentPointerCaptureRequest.enable)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5414 | |
| 5415 | std::string windowName = "None"; |
| 5416 | if (mWindowTokenWithPointerCapture) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5417 | const sp<WindowInfoHandle> captureWindowHandle = |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5418 | getWindowHandleLocked(mWindowTokenWithPointerCapture); |
| 5419 | windowName = captureWindowHandle ? captureWindowHandle->getName().c_str() |
| 5420 | : "token has capture without window"; |
| 5421 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5422 | 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] | 5423 | |
| 5424 | return dump; |
| 5425 | } |
| 5426 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5427 | void InputDispatcher::dumpDispatchStateLocked(std::string& dump) { |
Siarhei Vishniakou | 043a3ec | 2019-05-01 11:30:46 -0700 | [diff] [blame] | 5428 | dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled)); |
| 5429 | dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen)); |
| 5430 | dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5431 | dump += StringPrintf(INDENT "FocusedDisplayId: %" PRId32 "\n", mFocusedDisplayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5432 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5433 | if (!mFocusedApplicationHandlesByDisplay.empty()) { |
| 5434 | dump += StringPrintf(INDENT "FocusedApplications:\n"); |
| 5435 | for (auto& it : mFocusedApplicationHandlesByDisplay) { |
| 5436 | const int32_t displayId = it.first; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5437 | const std::shared_ptr<InputApplicationHandle>& applicationHandle = it.second; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5438 | const std::chrono::duration timeout = |
| 5439 | applicationHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5440 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5441 | ", name='%s', dispatchingTimeout=%" PRId64 "ms\n", |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5442 | displayId, applicationHandle->getName().c_str(), millis(timeout)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5443 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5444 | } else { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5445 | dump += StringPrintf(INDENT "FocusedApplications: <none>\n"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5446 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5447 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5448 | dump += mFocusResolver.dump(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5449 | dump += dumpPointerCaptureStateLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5450 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5451 | if (!mTouchStatesByDisplay.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5452 | dump += StringPrintf(INDENT "TouchStatesByDisplay:\n"); |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5453 | for (const auto& [displayId, state] : mTouchStatesByDisplay) { |
Siarhei Vishniakou | 6e1e987 | 2022-11-08 17:51:35 -0800 | [diff] [blame] | 5454 | std::string touchStateDump = addLinePrefix(state.dump(), INDENT2); |
| 5455 | dump += INDENT2 + std::to_string(displayId) + " : " + touchStateDump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5456 | } |
| 5457 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5458 | dump += INDENT "TouchStates: <no displays touched>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5459 | } |
| 5460 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5461 | if (mDragState) { |
| 5462 | dump += StringPrintf(INDENT "DragState:\n"); |
| 5463 | mDragState->dump(dump, INDENT2); |
| 5464 | } |
| 5465 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5466 | if (!mWindowHandlesByDisplay.empty()) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5467 | for (const auto& [displayId, windowHandles] : mWindowHandlesByDisplay) { |
| 5468 | dump += StringPrintf(INDENT "Display: %" PRId32 "\n", displayId); |
| 5469 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
| 5470 | const auto& displayInfo = it->second; |
| 5471 | dump += StringPrintf(INDENT2 "logicalSize=%dx%d\n", displayInfo.logicalWidth, |
| 5472 | displayInfo.logicalHeight); |
| 5473 | displayInfo.transform.dump(dump, "transform", INDENT4); |
| 5474 | } else { |
| 5475 | dump += INDENT2 "No DisplayInfo found!\n"; |
| 5476 | } |
| 5477 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5478 | if (!windowHandles.empty()) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5479 | dump += INDENT2 "Windows:\n"; |
| 5480 | for (size_t i = 0; i < windowHandles.size(); i++) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5481 | const sp<WindowInfoHandle>& windowHandle = windowHandles[i]; |
| 5482 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5483 | |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5484 | dump += StringPrintf(INDENT3 "%zu: name='%s', id=%" PRId32 ", displayId=%d, " |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5485 | "inputConfig=%s, alpha=%.2f, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5486 | "frame=[%d,%d][%d,%d], globalScale=%f, " |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5487 | "applicationInfo.name=%s, " |
| 5488 | "applicationInfo.token=%s, " |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 5489 | "touchableRegion=", |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5490 | i, windowInfo->name.c_str(), windowInfo->id, |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5491 | windowInfo->displayId, |
| 5492 | windowInfo->inputConfig.string().c_str(), |
| 5493 | windowInfo->alpha, windowInfo->frameLeft, |
| 5494 | windowInfo->frameTop, windowInfo->frameRight, |
| 5495 | windowInfo->frameBottom, windowInfo->globalScaleFactor, |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5496 | windowInfo->applicationInfo.name.c_str(), |
| 5497 | toString(windowInfo->applicationInfo.token).c_str()); |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 5498 | dump += dumpRegion(windowInfo->touchableRegion); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5499 | dump += StringPrintf(", ownerPid=%d, ownerUid=%d, dispatchingTimeout=%" PRId64 |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5500 | "ms, hasToken=%s, " |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5501 | "touchOcclusionMode=%s\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5502 | windowInfo->ownerPid, windowInfo->ownerUid, |
Bernardo Rufino | c2f1fad | 2020-11-04 17:30:57 +0000 | [diff] [blame] | 5503 | millis(windowInfo->dispatchingTimeout), |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 5504 | toString(windowInfo->token != nullptr), |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5505 | toString(windowInfo->touchOcclusionMode).c_str()); |
chaviw | 85b4420 | 2020-07-24 11:46:21 -0700 | [diff] [blame] | 5506 | windowInfo->transform.dump(dump, "transform", INDENT4); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5507 | } |
| 5508 | } else { |
| 5509 | dump += INDENT2 "Windows: <none>\n"; |
| 5510 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5511 | } |
| 5512 | } else { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5513 | dump += INDENT "Displays: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5514 | } |
| 5515 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5516 | if (!mGlobalMonitorsByDisplay.empty()) { |
| 5517 | for (const auto& [displayId, monitors] : mGlobalMonitorsByDisplay) { |
| 5518 | dump += StringPrintf(INDENT "Global monitors on display %d:\n", displayId); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5519 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5520 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5521 | } else { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5522 | dump += INDENT "Global Monitors: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5523 | } |
| 5524 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5525 | const nsecs_t currentTime = now(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5526 | |
| 5527 | // Dump recently dispatched or dropped events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5528 | if (!mRecentQueue.empty()) { |
| 5529 | dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5530 | for (std::shared_ptr<EventEntry>& entry : mRecentQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5531 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5532 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5533 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5534 | } |
| 5535 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5536 | dump += INDENT "RecentQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5537 | } |
| 5538 | |
| 5539 | // Dump event currently being dispatched. |
| 5540 | if (mPendingEvent) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5541 | dump += INDENT "PendingEvent:\n"; |
| 5542 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5543 | dump += mPendingEvent->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5544 | dump += StringPrintf(", age=%" PRId64 "ms\n", |
| 5545 | ns2ms(currentTime - mPendingEvent->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5546 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5547 | dump += INDENT "PendingEvent: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5548 | } |
| 5549 | |
| 5550 | // Dump inbound events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5551 | if (!mInboundQueue.empty()) { |
| 5552 | dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5553 | for (std::shared_ptr<EventEntry>& entry : mInboundQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5554 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5555 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5556 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5557 | } |
| 5558 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5559 | dump += INDENT "InboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5560 | } |
| 5561 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5562 | if (!mReplacedKeys.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5563 | dump += INDENT "ReplacedKeys:\n"; |
Michael Wright | 3cec446 | 2022-11-24 22:05:46 +0000 | [diff] [blame] | 5564 | for (const auto& [replacement, newKeyCode] : mReplacedKeys) { |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5565 | dump += StringPrintf(INDENT2 "originalKeyCode=%d, deviceId=%d -> newKeyCode=%d\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5566 | replacement.keyCode, replacement.deviceId, newKeyCode); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5567 | } |
| 5568 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5569 | dump += INDENT "ReplacedKeys: <empty>\n"; |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5570 | } |
| 5571 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5572 | if (!mCommandQueue.empty()) { |
| 5573 | dump += StringPrintf(INDENT "CommandQueue: size=%zu\n", mCommandQueue.size()); |
| 5574 | } else { |
| 5575 | dump += INDENT "CommandQueue: <empty>\n"; |
| 5576 | } |
| 5577 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5578 | if (!mConnectionsByToken.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5579 | dump += INDENT "Connections:\n"; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5580 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5581 | dump += StringPrintf(INDENT2 "%i: channelName='%s', windowName='%s', " |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5582 | "status=%s, monitor=%s, responsive=%s\n", |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5583 | connection->inputChannel->getFd().get(), |
| 5584 | connection->getInputChannelName().c_str(), |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5585 | connection->getWindowName().c_str(), |
| 5586 | ftl::enum_string(connection->status).c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5587 | toString(connection->monitor), toString(connection->responsive)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5588 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5589 | if (!connection->outboundQueue.empty()) { |
| 5590 | dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n", |
| 5591 | connection->outboundQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5592 | dump += dumpQueue(connection->outboundQueue, currentTime); |
| 5593 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5594 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5595 | dump += INDENT3 "OutboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5596 | } |
| 5597 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5598 | if (!connection->waitQueue.empty()) { |
| 5599 | dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n", |
| 5600 | connection->waitQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5601 | dump += dumpQueue(connection->waitQueue, currentTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5602 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5603 | dump += INDENT3 "WaitQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5604 | } |
| 5605 | } |
| 5606 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5607 | dump += INDENT "Connections: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5608 | } |
| 5609 | |
| 5610 | if (isAppSwitchPendingLocked()) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5611 | dump += StringPrintf(INDENT "AppSwitch: pending, due in %" PRId64 "ms\n", |
| 5612 | ns2ms(mAppSwitchDueTime - now())); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5613 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5614 | dump += INDENT "AppSwitch: not pending\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5615 | } |
| 5616 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5617 | if (!mTouchModePerDisplay.empty()) { |
| 5618 | dump += INDENT "TouchModePerDisplay:\n"; |
| 5619 | for (const auto& [displayId, touchMode] : mTouchModePerDisplay) { |
| 5620 | dump += StringPrintf(INDENT2 "Display: %" PRId32 " TouchMode: %s\n", displayId, |
| 5621 | std::to_string(touchMode).c_str()); |
| 5622 | } |
| 5623 | } else { |
| 5624 | dump += INDENT "TouchModePerDisplay: <none>\n"; |
| 5625 | } |
| 5626 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5627 | dump += INDENT "Configuration:\n"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5628 | dump += StringPrintf(INDENT2 "KeyRepeatDelay: %" PRId64 "ms\n", ns2ms(mConfig.keyRepeatDelay)); |
| 5629 | dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n", |
| 5630 | ns2ms(mConfig.keyRepeatTimeout)); |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 5631 | dump += mLatencyTracker.dump(INDENT2); |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 5632 | dump += mLatencyAggregator.dump(INDENT2); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5633 | } |
| 5634 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5635 | void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) { |
| 5636 | const size_t numMonitors = monitors.size(); |
| 5637 | for (size_t i = 0; i < numMonitors; i++) { |
| 5638 | const Monitor& monitor = monitors[i]; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5639 | const std::shared_ptr<InputChannel>& channel = monitor.inputChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5640 | dump += StringPrintf(INDENT2 "%zu: '%s', ", i, channel->getName().c_str()); |
| 5641 | dump += "\n"; |
| 5642 | } |
| 5643 | } |
| 5644 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5645 | class LooperEventCallback : public LooperCallback { |
| 5646 | public: |
| 5647 | LooperEventCallback(std::function<int(int events)> callback) : mCallback(callback) {} |
| 5648 | int handleEvent(int /*fd*/, int events, void* /*data*/) override { return mCallback(events); } |
| 5649 | |
| 5650 | private: |
| 5651 | std::function<int(int events)> mCallback; |
| 5652 | }; |
| 5653 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5654 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputChannel(const std::string& name) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5655 | if (DEBUG_CHANNEL_CREATION) { |
| 5656 | ALOGD("channel '%s' ~ createInputChannel", name.c_str()); |
| 5657 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5658 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5659 | std::unique_ptr<InputChannel> serverChannel; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5660 | std::unique_ptr<InputChannel> clientChannel; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5661 | status_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5662 | |
| 5663 | if (result) { |
| 5664 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5665 | } |
| 5666 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5667 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5668 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5669 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5670 | int fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5671 | sp<Connection> connection = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5672 | sp<Connection>::make(std::move(serverChannel), /*monitor=*/false, mIdGenerator); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5673 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5674 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5675 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5676 | } |
| 5677 | mConnectionsByToken.emplace(token, connection); |
| 5678 | |
| 5679 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5680 | this, std::placeholders::_1, token); |
| 5681 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5682 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, sp<LooperEventCallback>::make(callback), |
| 5683 | nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5684 | } // release lock |
| 5685 | |
| 5686 | // Wake the looper because some connections have changed. |
| 5687 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5688 | return clientChannel; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5689 | } |
| 5690 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5691 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputMonitor(int32_t displayId, |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5692 | const std::string& name, |
| 5693 | int32_t pid) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5694 | std::shared_ptr<InputChannel> serverChannel; |
| 5695 | std::unique_ptr<InputChannel> clientChannel; |
| 5696 | status_t result = openInputChannelPair(name, serverChannel, clientChannel); |
| 5697 | if (result) { |
| 5698 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5699 | } |
| 5700 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5701 | { // acquire lock |
| 5702 | std::scoped_lock _l(mLock); |
| 5703 | |
| 5704 | if (displayId < 0) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5705 | return base::Error(BAD_VALUE) << "Attempted to create input monitor with name " << name |
| 5706 | << " without a specified display."; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5707 | } |
| 5708 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5709 | sp<Connection> connection = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5710 | sp<Connection>::make(serverChannel, /*monitor=*/true, mIdGenerator); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5711 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5712 | const int fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5713 | |
| 5714 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5715 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5716 | } |
| 5717 | mConnectionsByToken.emplace(token, connection); |
| 5718 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5719 | this, std::placeholders::_1, token); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5720 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5721 | mGlobalMonitorsByDisplay[displayId].emplace_back(serverChannel, pid); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5722 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5723 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, sp<LooperEventCallback>::make(callback), |
| 5724 | nullptr); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5725 | } |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5726 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5727 | // Wake the looper because some connections have changed. |
| 5728 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5729 | return clientChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5730 | } |
| 5731 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5732 | status_t InputDispatcher::removeInputChannel(const sp<IBinder>& connectionToken) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5733 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5734 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5735 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5736 | status_t status = removeInputChannelLocked(connectionToken, /*notify=*/false); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5737 | if (status) { |
| 5738 | return status; |
| 5739 | } |
| 5740 | } // release lock |
| 5741 | |
| 5742 | // Wake the poll loop because removing the connection may have changed the current |
| 5743 | // synchronization state. |
| 5744 | mLooper->wake(); |
| 5745 | return OK; |
| 5746 | } |
| 5747 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5748 | status_t InputDispatcher::removeInputChannelLocked(const sp<IBinder>& connectionToken, |
| 5749 | bool notify) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5750 | sp<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5751 | if (connection == nullptr) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5752 | // 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] | 5753 | return BAD_VALUE; |
| 5754 | } |
| 5755 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5756 | removeConnectionLocked(connection); |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 5757 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5758 | if (connection->monitor) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5759 | removeMonitorChannelLocked(connectionToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5760 | } |
| 5761 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5762 | mLooper->removeFd(connection->inputChannel->getFd()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5763 | |
| 5764 | nsecs_t currentTime = now(); |
| 5765 | abortBrokenDispatchCycleLocked(currentTime, connection, notify); |
| 5766 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5767 | connection->status = Connection::Status::ZOMBIE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5768 | return OK; |
| 5769 | } |
| 5770 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5771 | void InputDispatcher::removeMonitorChannelLocked(const sp<IBinder>& connectionToken) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5772 | for (auto it = mGlobalMonitorsByDisplay.begin(); it != mGlobalMonitorsByDisplay.end();) { |
| 5773 | auto& [displayId, monitors] = *it; |
| 5774 | std::erase_if(monitors, [connectionToken](const Monitor& monitor) { |
| 5775 | return monitor.inputChannel->getConnectionToken() == connectionToken; |
| 5776 | }); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5777 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5778 | if (monitors.empty()) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5779 | it = mGlobalMonitorsByDisplay.erase(it); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5780 | } else { |
| 5781 | ++it; |
| 5782 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5783 | } |
| 5784 | } |
| 5785 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5786 | status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5787 | std::scoped_lock _l(mLock); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 5788 | return pilferPointersLocked(token); |
| 5789 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5790 | |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 5791 | status_t InputDispatcher::pilferPointersLocked(const sp<IBinder>& token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5792 | const std::shared_ptr<InputChannel> requestingChannel = getInputChannelLocked(token); |
| 5793 | if (!requestingChannel) { |
| 5794 | ALOGW("Attempted to pilfer pointers from an un-registered channel or invalid token"); |
| 5795 | return BAD_VALUE; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5796 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5797 | |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5798 | auto [statePtr, windowPtr, displayId] = findTouchStateWindowAndDisplayLocked(token); |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 5799 | if (statePtr == nullptr || windowPtr == nullptr || windowPtr->pointerIds.none()) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5800 | ALOGW("Attempted to pilfer points from a channel without any on-going pointer streams." |
| 5801 | " Ignoring."); |
| 5802 | return BAD_VALUE; |
| 5803 | } |
| 5804 | |
| 5805 | TouchState& state = *statePtr; |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 5806 | TouchedWindow& window = *windowPtr; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5807 | // Send cancel events to all the input channels we're stealing from. |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 5808 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5809 | "input channel stole pointer stream"); |
| 5810 | options.deviceId = state.deviceId; |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5811 | options.displayId = displayId; |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 5812 | options.pointerIds = window.pointerIds; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5813 | std::string canceledWindows; |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 5814 | for (const TouchedWindow& w : state.windows) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5815 | const std::shared_ptr<InputChannel> channel = |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 5816 | getInputChannelLocked(w.windowHandle->getToken()); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5817 | if (channel != nullptr && channel->getConnectionToken() != token) { |
| 5818 | synthesizeCancelationEventsForInputChannelLocked(channel, options); |
| 5819 | canceledWindows += canceledWindows.empty() ? "[" : ", "; |
| 5820 | canceledWindows += channel->getName(); |
| 5821 | } |
| 5822 | } |
| 5823 | canceledWindows += canceledWindows.empty() ? "[]" : "]"; |
| 5824 | ALOGI("Channel %s is stealing touch from %s", requestingChannel->getName().c_str(), |
| 5825 | canceledWindows.c_str()); |
| 5826 | |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 5827 | // Prevent the gesture from being sent to any other windows. |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 5828 | // This only blocks relevant pointers to be sent to other windows |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 5829 | window.pilferedPointerIds |= window.pointerIds; |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 5830 | |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 5831 | state.cancelPointersForWindowsExcept(window.pointerIds, token); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5832 | return OK; |
| 5833 | } |
| 5834 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5835 | void InputDispatcher::requestPointerCapture(const sp<IBinder>& windowToken, bool enabled) { |
| 5836 | { // acquire lock |
| 5837 | std::scoped_lock _l(mLock); |
| 5838 | if (DEBUG_FOCUS) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5839 | const sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(windowToken); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5840 | ALOGI("Request to %s Pointer Capture from: %s.", enabled ? "enable" : "disable", |
| 5841 | windowHandle != nullptr ? windowHandle->getName().c_str() |
| 5842 | : "token without window"); |
| 5843 | } |
| 5844 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5845 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5846 | if (focusedToken != windowToken) { |
| 5847 | ALOGW("Ignoring request to %s Pointer Capture: window does not have focus.", |
| 5848 | enabled ? "enable" : "disable"); |
| 5849 | return; |
| 5850 | } |
| 5851 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5852 | if (enabled == mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5853 | ALOGW("Ignoring request to %s Pointer Capture: " |
| 5854 | "window has %s requested pointer capture.", |
| 5855 | enabled ? "enable" : "disable", enabled ? "already" : "not"); |
| 5856 | return; |
| 5857 | } |
| 5858 | |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 5859 | if (enabled) { |
| 5860 | if (std::find(mIneligibleDisplaysForPointerCapture.begin(), |
| 5861 | mIneligibleDisplaysForPointerCapture.end(), |
| 5862 | mFocusedDisplayId) != mIneligibleDisplaysForPointerCapture.end()) { |
| 5863 | ALOGW("Ignoring request to enable Pointer Capture: display is not eligible"); |
| 5864 | return; |
| 5865 | } |
| 5866 | } |
| 5867 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5868 | setPointerCaptureLocked(enabled); |
| 5869 | } // release lock |
| 5870 | |
| 5871 | // Wake the thread to process command entries. |
| 5872 | mLooper->wake(); |
| 5873 | } |
| 5874 | |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 5875 | void InputDispatcher::setDisplayEligibilityForPointerCapture(int32_t displayId, bool isEligible) { |
| 5876 | { // acquire lock |
| 5877 | std::scoped_lock _l(mLock); |
| 5878 | std::erase(mIneligibleDisplaysForPointerCapture, displayId); |
| 5879 | if (!isEligible) { |
| 5880 | mIneligibleDisplaysForPointerCapture.push_back(displayId); |
| 5881 | } |
| 5882 | } // release lock |
| 5883 | } |
| 5884 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5885 | std::optional<int32_t> InputDispatcher::findMonitorPidByTokenLocked(const sp<IBinder>& token) { |
| 5886 | for (const auto& [_, monitors] : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5887 | for (const Monitor& monitor : monitors) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5888 | if (monitor.inputChannel->getConnectionToken() == token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5889 | return monitor.pid; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5890 | } |
| 5891 | } |
| 5892 | } |
| 5893 | return std::nullopt; |
| 5894 | } |
| 5895 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5896 | sp<Connection> InputDispatcher::getConnectionLocked(const sp<IBinder>& inputConnectionToken) const { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 5897 | if (inputConnectionToken == nullptr) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5898 | return nullptr; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 5899 | } |
| 5900 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5901 | for (const auto& [token, connection] : mConnectionsByToken) { |
| 5902 | if (token == inputConnectionToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5903 | return connection; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5904 | } |
| 5905 | } |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 5906 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5907 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5908 | } |
| 5909 | |
Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 5910 | std::string InputDispatcher::getConnectionNameLocked(const sp<IBinder>& connectionToken) const { |
| 5911 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 5912 | if (connection == nullptr) { |
| 5913 | return "<nullptr>"; |
| 5914 | } |
| 5915 | return connection->getInputChannelName(); |
| 5916 | } |
| 5917 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5918 | void InputDispatcher::removeConnectionLocked(const sp<Connection>& connection) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5919 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5920 | mConnectionsByToken.erase(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5921 | } |
| 5922 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5923 | void InputDispatcher::doDispatchCycleFinishedCommand(nsecs_t finishTime, |
| 5924 | const sp<Connection>& connection, uint32_t seq, |
| 5925 | bool handled, nsecs_t consumeTime) { |
| 5926 | // Handle post-event policy actions. |
| 5927 | std::deque<DispatchEntry*>::iterator dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 5928 | if (dispatchEntryIt == connection->waitQueue.end()) { |
| 5929 | return; |
| 5930 | } |
| 5931 | DispatchEntry* dispatchEntry = *dispatchEntryIt; |
| 5932 | const nsecs_t eventDuration = finishTime - dispatchEntry->deliveryTime; |
| 5933 | if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) { |
| 5934 | ALOGI("%s spent %" PRId64 "ms processing %s", connection->getWindowName().c_str(), |
| 5935 | ns2ms(eventDuration), dispatchEntry->eventEntry->getDescription().c_str()); |
| 5936 | } |
| 5937 | if (shouldReportFinishedEvent(*dispatchEntry, *connection)) { |
| 5938 | mLatencyTracker.trackFinishedEvent(dispatchEntry->eventEntry->id, |
| 5939 | connection->inputChannel->getConnectionToken(), |
| 5940 | dispatchEntry->deliveryTime, consumeTime, finishTime); |
| 5941 | } |
| 5942 | |
| 5943 | bool restartEvent; |
| 5944 | if (dispatchEntry->eventEntry->type == EventEntry::Type::KEY) { |
| 5945 | KeyEntry& keyEntry = static_cast<KeyEntry&>(*(dispatchEntry->eventEntry)); |
| 5946 | restartEvent = |
| 5947 | afterKeyEventLockedInterruptable(connection, dispatchEntry, keyEntry, handled); |
| 5948 | } else if (dispatchEntry->eventEntry->type == EventEntry::Type::MOTION) { |
| 5949 | MotionEntry& motionEntry = static_cast<MotionEntry&>(*(dispatchEntry->eventEntry)); |
| 5950 | restartEvent = afterMotionEventLockedInterruptable(connection, dispatchEntry, motionEntry, |
| 5951 | handled); |
| 5952 | } else { |
| 5953 | restartEvent = false; |
| 5954 | } |
| 5955 | |
| 5956 | // Dequeue the event and start the next cycle. |
| 5957 | // Because the lock might have been released, it is possible that the |
| 5958 | // contents of the wait queue to have been drained, so we need to double-check |
| 5959 | // a few things. |
| 5960 | dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 5961 | if (dispatchEntryIt != connection->waitQueue.end()) { |
| 5962 | dispatchEntry = *dispatchEntryIt; |
| 5963 | connection->waitQueue.erase(dispatchEntryIt); |
| 5964 | const sp<IBinder>& connectionToken = connection->inputChannel->getConnectionToken(); |
| 5965 | mAnrTracker.erase(dispatchEntry->timeoutTime, connectionToken); |
| 5966 | if (!connection->responsive) { |
| 5967 | connection->responsive = isConnectionResponsive(*connection); |
| 5968 | if (connection->responsive) { |
| 5969 | // The connection was unresponsive, and now it's responsive. |
| 5970 | processConnectionResponsiveLocked(*connection); |
| 5971 | } |
| 5972 | } |
| 5973 | traceWaitQueueLength(*connection); |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5974 | if (restartEvent && connection->status == Connection::Status::NORMAL) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5975 | connection->outboundQueue.push_front(dispatchEntry); |
| 5976 | traceOutboundQueueLength(*connection); |
| 5977 | } else { |
| 5978 | releaseDispatchEntry(dispatchEntry); |
| 5979 | } |
| 5980 | } |
| 5981 | |
| 5982 | // Start the next dispatch cycle for this connection. |
| 5983 | startDispatchCycleLocked(now(), connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5984 | } |
| 5985 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5986 | void InputDispatcher::sendFocusChangedCommandLocked(const sp<IBinder>& oldToken, |
| 5987 | const sp<IBinder>& newToken) { |
| 5988 | auto command = [this, oldToken, newToken]() REQUIRES(mLock) { |
| 5989 | scoped_unlock unlock(mLock); |
| 5990 | mPolicy->notifyFocusChanged(oldToken, newToken); |
| 5991 | }; |
| 5992 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5993 | } |
| 5994 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5995 | void InputDispatcher::sendDropWindowCommandLocked(const sp<IBinder>& token, float x, float y) { |
| 5996 | auto command = [this, token, x, y]() REQUIRES(mLock) { |
| 5997 | scoped_unlock unlock(mLock); |
| 5998 | mPolicy->notifyDropWindow(token, x, y); |
| 5999 | }; |
| 6000 | postCommandLocked(std::move(command)); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 6001 | } |
| 6002 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6003 | void InputDispatcher::onAnrLocked(const sp<Connection>& connection) { |
| 6004 | if (connection == nullptr) { |
| 6005 | LOG_ALWAYS_FATAL("Caller must check for nullness"); |
| 6006 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6007 | // Since we are allowing the policy to extend the timeout, maybe the waitQueue |
| 6008 | // is already healthy again. Don't raise ANR in this situation |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6009 | if (connection->waitQueue.empty()) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6010 | ALOGI("Not raising ANR because the connection %s has recovered", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6011 | connection->inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6012 | return; |
| 6013 | } |
| 6014 | /** |
| 6015 | * The "oldestEntry" is the entry that was first sent to the application. That entry, however, |
| 6016 | * may not be the one that caused the timeout to occur. One possibility is that window timeout |
| 6017 | * has changed. This could cause newer entries to time out before the already dispatched |
| 6018 | * entries. In that situation, the newest entries caused ANR. But in all likelihood, the app |
| 6019 | * processes the events linearly. So providing information about the oldest entry seems to be |
| 6020 | * most useful. |
| 6021 | */ |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6022 | DispatchEntry* oldestEntry = *connection->waitQueue.begin(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6023 | const nsecs_t currentWait = now() - oldestEntry->deliveryTime; |
| 6024 | std::string reason = |
| 6025 | android::base::StringPrintf("%s is not responding. Waited %" PRId64 "ms for %s", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6026 | connection->inputChannel->getName().c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6027 | ns2ms(currentWait), |
| 6028 | oldestEntry->eventEntry->getDescription().c_str()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6029 | sp<IBinder> connectionToken = connection->inputChannel->getConnectionToken(); |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 6030 | updateLastAnrStateLocked(getWindowHandleLocked(connectionToken), reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6031 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6032 | processConnectionUnresponsiveLocked(*connection, std::move(reason)); |
| 6033 | |
| 6034 | // Stop waking up for events on this connection, it is already unresponsive |
| 6035 | cancelEventsForAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6036 | } |
| 6037 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6038 | void InputDispatcher::onAnrLocked(std::shared_ptr<InputApplicationHandle> application) { |
| 6039 | std::string reason = |
| 6040 | StringPrintf("%s does not have a focused window", application->getName().c_str()); |
| 6041 | updateLastAnrStateLocked(*application, reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6042 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6043 | auto command = [this, application = std::move(application)]() REQUIRES(mLock) { |
| 6044 | scoped_unlock unlock(mLock); |
| 6045 | mPolicy->notifyNoFocusedWindowAnr(application); |
| 6046 | }; |
| 6047 | postCommandLocked(std::move(command)); |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 6048 | } |
| 6049 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 6050 | void InputDispatcher::updateLastAnrStateLocked(const sp<WindowInfoHandle>& window, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6051 | const std::string& reason) { |
| 6052 | const std::string windowLabel = getApplicationWindowLabel(nullptr, window); |
| 6053 | updateLastAnrStateLocked(windowLabel, reason); |
| 6054 | } |
| 6055 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6056 | void InputDispatcher::updateLastAnrStateLocked(const InputApplicationHandle& application, |
| 6057 | const std::string& reason) { |
| 6058 | const std::string windowLabel = getApplicationWindowLabel(&application, nullptr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6059 | updateLastAnrStateLocked(windowLabel, reason); |
| 6060 | } |
| 6061 | |
| 6062 | void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel, |
| 6063 | const std::string& reason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6064 | // 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] | 6065 | time_t t = time(nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6066 | struct tm tm; |
| 6067 | localtime_r(&t, &tm); |
| 6068 | char timestr[64]; |
| 6069 | strftime(timestr, sizeof(timestr), "%F %T", &tm); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6070 | mLastAnrState.clear(); |
| 6071 | mLastAnrState += INDENT "ANR:\n"; |
| 6072 | mLastAnrState += StringPrintf(INDENT2 "Time: %s\n", timestr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6073 | mLastAnrState += StringPrintf(INDENT2 "Reason: %s\n", reason.c_str()); |
| 6074 | mLastAnrState += StringPrintf(INDENT2 "Window: %s\n", windowLabel.c_str()); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6075 | dumpDispatchStateLocked(mLastAnrState); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6076 | } |
| 6077 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6078 | void InputDispatcher::doInterceptKeyBeforeDispatchingCommand(const sp<IBinder>& focusedWindowToken, |
| 6079 | KeyEntry& entry) { |
| 6080 | const KeyEvent event = createKeyEvent(entry); |
| 6081 | nsecs_t delay = 0; |
| 6082 | { // release lock |
| 6083 | scoped_unlock unlock(mLock); |
| 6084 | android::base::Timer t; |
| 6085 | delay = mPolicy->interceptKeyBeforeDispatching(focusedWindowToken, &event, |
| 6086 | entry.policyFlags); |
| 6087 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 6088 | ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms", |
| 6089 | std::to_string(t.duration().count()).c_str()); |
| 6090 | } |
| 6091 | } // acquire lock |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6092 | |
| 6093 | if (delay < 0) { |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 6094 | entry.interceptKeyResult = KeyEntry::InterceptKeyResult::SKIP; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6095 | } else if (delay == 0) { |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 6096 | entry.interceptKeyResult = KeyEntry::InterceptKeyResult::CONTINUE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6097 | } else { |
Michael Wright | 5caf55a | 2022-11-24 22:31:42 +0000 | [diff] [blame] | 6098 | entry.interceptKeyResult = KeyEntry::InterceptKeyResult::TRY_AGAIN_LATER; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6099 | entry.interceptKeyWakeupTime = now() + delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6100 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6101 | } |
| 6102 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6103 | void InputDispatcher::sendWindowUnresponsiveCommandLocked(const sp<IBinder>& token, |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6104 | std::optional<int32_t> pid, |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6105 | std::string reason) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6106 | auto command = [this, token, pid, reason = std::move(reason)]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6107 | scoped_unlock unlock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6108 | mPolicy->notifyWindowUnresponsive(token, pid, reason); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6109 | }; |
| 6110 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6111 | } |
| 6112 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6113 | void InputDispatcher::sendWindowResponsiveCommandLocked(const sp<IBinder>& token, |
| 6114 | std::optional<int32_t> pid) { |
| 6115 | auto command = [this, token, pid]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6116 | scoped_unlock unlock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6117 | mPolicy->notifyWindowResponsive(token, pid); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6118 | }; |
| 6119 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6120 | } |
| 6121 | |
| 6122 | /** |
| 6123 | * Tell the policy that a connection has become unresponsive so that it can start ANR. |
| 6124 | * Check whether the connection of interest is a monitor or a window, and add the corresponding |
| 6125 | * command entry to the command queue. |
| 6126 | */ |
| 6127 | void InputDispatcher::processConnectionUnresponsiveLocked(const Connection& connection, |
| 6128 | std::string reason) { |
| 6129 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6130 | std::optional<int32_t> pid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6131 | if (connection.monitor) { |
| 6132 | ALOGW("Monitor %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 6133 | reason.c_str()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6134 | pid = findMonitorPidByTokenLocked(connectionToken); |
| 6135 | } else { |
| 6136 | // The connection is a window |
| 6137 | ALOGW("Window %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 6138 | reason.c_str()); |
| 6139 | const sp<WindowInfoHandle> handle = getWindowHandleLocked(connectionToken); |
| 6140 | if (handle != nullptr) { |
| 6141 | pid = handle->getInfo()->ownerPid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6142 | } |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6143 | } |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6144 | sendWindowUnresponsiveCommandLocked(connectionToken, pid, std::move(reason)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6145 | } |
| 6146 | |
| 6147 | /** |
| 6148 | * Tell the policy that a connection has become responsive so that it can stop ANR. |
| 6149 | */ |
| 6150 | void InputDispatcher::processConnectionResponsiveLocked(const Connection& connection) { |
| 6151 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6152 | std::optional<int32_t> pid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6153 | if (connection.monitor) { |
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 | const sp<WindowInfoHandle> handle = getWindowHandleLocked(connectionToken); |
| 6158 | if (handle != nullptr) { |
| 6159 | pid = handle->getInfo()->ownerPid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6160 | } |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6161 | } |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6162 | sendWindowResponsiveCommandLocked(connectionToken, pid); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6163 | } |
| 6164 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6165 | bool InputDispatcher::afterKeyEventLockedInterruptable(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6166 | DispatchEntry* dispatchEntry, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6167 | KeyEntry& keyEntry, bool handled) { |
| 6168 | if (keyEntry.flags & AKEY_EVENT_FLAG_FALLBACK) { |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6169 | if (!handled) { |
| 6170 | // Report the key as unhandled, since the fallback was not handled. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6171 | mReporter->reportUnhandledKey(keyEntry.id); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6172 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6173 | return false; |
| 6174 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6175 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6176 | // Get the fallback key state. |
| 6177 | // Clear it out after dispatching the UP. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6178 | int32_t originalKeyCode = keyEntry.keyCode; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6179 | int32_t fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6180 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6181 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6182 | } |
| 6183 | |
| 6184 | if (handled || !dispatchEntry->hasForegroundTarget()) { |
| 6185 | // If the application handles the original key for which we previously |
| 6186 | // generated a fallback or if the window is not a foreground window, |
| 6187 | // then cancel the associated fallback key, if any. |
| 6188 | if (fallbackKeyCode != -1) { |
| 6189 | // Dispatch the unhandled key to the policy with the cancel flag. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6190 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6191 | ALOGD("Unhandled key event: Asking policy to cancel fallback action. " |
| 6192 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6193 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, |
| 6194 | keyEntry.policyFlags); |
| 6195 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6196 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6197 | event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6198 | |
| 6199 | mLock.unlock(); |
| 6200 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 6201 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), &event, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6202 | keyEntry.policyFlags, &event); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6203 | |
| 6204 | mLock.lock(); |
| 6205 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6206 | // Cancel the fallback key. |
| 6207 | if (fallbackKeyCode != AKEYCODE_UNKNOWN) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 6208 | CancelationOptions options(CancelationOptions::Mode::CANCEL_FALLBACK_EVENTS, |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6209 | "application handled the original non-fallback key " |
| 6210 | "or is no longer a foreground target, " |
| 6211 | "canceling previously dispatched fallback key"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6212 | options.keyCode = fallbackKeyCode; |
| 6213 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6214 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6215 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6216 | } |
| 6217 | } else { |
| 6218 | // If the application did not handle a non-fallback key, first check |
| 6219 | // that we are in a good state to perform unhandled key event processing |
| 6220 | // Then ask the policy what to do with it. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6221 | bool initialDown = keyEntry.action == AKEY_EVENT_ACTION_DOWN && keyEntry.repeatCount == 0; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6222 | if (fallbackKeyCode == -1 && !initialDown) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6223 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6224 | ALOGD("Unhandled key event: Skipping unhandled key event processing " |
| 6225 | "since this is not an initial down. " |
| 6226 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6227 | originalKeyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 6228 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6229 | return false; |
| 6230 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6231 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6232 | // Dispatch the unhandled key to the policy. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6233 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6234 | ALOGD("Unhandled key event: Asking policy to perform fallback action. " |
| 6235 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6236 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 6237 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6238 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6239 | |
| 6240 | mLock.unlock(); |
| 6241 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 6242 | bool fallback = |
| 6243 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6244 | &event, keyEntry.policyFlags, &event); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6245 | |
| 6246 | mLock.lock(); |
| 6247 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 6248 | if (connection->status != Connection::Status::NORMAL) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6249 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6250 | return false; |
| 6251 | } |
| 6252 | |
| 6253 | // Latch the fallback keycode for this key on an initial down. |
| 6254 | // The fallback keycode cannot change at any other point in the lifecycle. |
| 6255 | if (initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6256 | if (fallback) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6257 | fallbackKeyCode = event.getKeyCode(); |
| 6258 | } else { |
| 6259 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
| 6260 | } |
| 6261 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
| 6262 | } |
| 6263 | |
| 6264 | ALOG_ASSERT(fallbackKeyCode != -1); |
| 6265 | |
| 6266 | // Cancel the fallback key if the policy decides not to send it anymore. |
| 6267 | // We will continue to dispatch the key to the policy but we will no |
| 6268 | // longer dispatch a fallback key to the application. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6269 | if (fallbackKeyCode != AKEYCODE_UNKNOWN && |
| 6270 | (!fallback || fallbackKeyCode != event.getKeyCode())) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6271 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6272 | if (fallback) { |
| 6273 | ALOGD("Unhandled key event: Policy requested to send key %d" |
| 6274 | "as a fallback for %d, but on the DOWN it had requested " |
| 6275 | "to send %d instead. Fallback canceled.", |
| 6276 | event.getKeyCode(), originalKeyCode, fallbackKeyCode); |
| 6277 | } else { |
| 6278 | ALOGD("Unhandled key event: Policy did not request fallback for %d, " |
| 6279 | "but on the DOWN it had requested to send %d. " |
| 6280 | "Fallback canceled.", |
| 6281 | originalKeyCode, fallbackKeyCode); |
| 6282 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6283 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6284 | |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 6285 | CancelationOptions options(CancelationOptions::Mode::CANCEL_FALLBACK_EVENTS, |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6286 | "canceling fallback, policy no longer desires it"); |
| 6287 | options.keyCode = fallbackKeyCode; |
| 6288 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
| 6289 | |
| 6290 | fallback = false; |
| 6291 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6292 | if (keyEntry.action != AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6293 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6294 | } |
| 6295 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6296 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6297 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6298 | { |
| 6299 | std::string msg; |
| 6300 | const KeyedVector<int32_t, int32_t>& fallbackKeys = |
| 6301 | connection->inputState.getFallbackKeys(); |
| 6302 | for (size_t i = 0; i < fallbackKeys.size(); i++) { |
| 6303 | msg += StringPrintf(", %d->%d", fallbackKeys.keyAt(i), fallbackKeys.valueAt(i)); |
| 6304 | } |
| 6305 | ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.", |
| 6306 | fallbackKeys.size(), msg.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6307 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6308 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6309 | |
| 6310 | if (fallback) { |
| 6311 | // Restart the dispatch cycle using the fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6312 | keyEntry.eventTime = event.getEventTime(); |
| 6313 | keyEntry.deviceId = event.getDeviceId(); |
| 6314 | keyEntry.source = event.getSource(); |
| 6315 | keyEntry.displayId = event.getDisplayId(); |
| 6316 | keyEntry.flags = event.getFlags() | AKEY_EVENT_FLAG_FALLBACK; |
| 6317 | keyEntry.keyCode = fallbackKeyCode; |
| 6318 | keyEntry.scanCode = event.getScanCode(); |
| 6319 | keyEntry.metaState = event.getMetaState(); |
| 6320 | keyEntry.repeatCount = event.getRepeatCount(); |
| 6321 | keyEntry.downTime = event.getDownTime(); |
| 6322 | keyEntry.syntheticRepeat = false; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6323 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6324 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6325 | ALOGD("Unhandled key event: Dispatching fallback key. " |
| 6326 | "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x", |
| 6327 | originalKeyCode, fallbackKeyCode, keyEntry.metaState); |
| 6328 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6329 | return true; // restart the event |
| 6330 | } else { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6331 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6332 | ALOGD("Unhandled key event: No fallback key."); |
| 6333 | } |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6334 | |
| 6335 | // Report the key as unhandled, since there is no fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6336 | mReporter->reportUnhandledKey(keyEntry.id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6337 | } |
| 6338 | } |
| 6339 | return false; |
| 6340 | } |
| 6341 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6342 | bool InputDispatcher::afterMotionEventLockedInterruptable(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6343 | DispatchEntry* dispatchEntry, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6344 | MotionEntry& motionEntry, bool handled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6345 | return false; |
| 6346 | } |
| 6347 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6348 | void InputDispatcher::traceInboundQueueLengthLocked() { |
| 6349 | if (ATRACE_ENABLED()) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 6350 | ATRACE_INT("iq", mInboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6351 | } |
| 6352 | } |
| 6353 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6354 | void InputDispatcher::traceOutboundQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6355 | if (ATRACE_ENABLED()) { |
| 6356 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6357 | snprintf(counterName, sizeof(counterName), "oq:%s", connection.getWindowName().c_str()); |
| 6358 | ATRACE_INT(counterName, connection.outboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6359 | } |
| 6360 | } |
| 6361 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6362 | void InputDispatcher::traceWaitQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6363 | if (ATRACE_ENABLED()) { |
| 6364 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6365 | snprintf(counterName, sizeof(counterName), "wq:%s", connection.getWindowName().c_str()); |
| 6366 | ATRACE_INT(counterName, connection.waitQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6367 | } |
| 6368 | } |
| 6369 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6370 | void InputDispatcher::dump(std::string& dump) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6371 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6372 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6373 | dump += "Input Dispatcher State:\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6374 | dumpDispatchStateLocked(dump); |
| 6375 | |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6376 | if (!mLastAnrState.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6377 | dump += "\nInput Dispatcher State at time of last ANR:\n"; |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6378 | dump += mLastAnrState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6379 | } |
| 6380 | } |
| 6381 | |
| 6382 | void InputDispatcher::monitor() { |
| 6383 | // 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] | 6384 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6385 | mLooper->wake(); |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6386 | mDispatcherIsAlive.wait(_l); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6387 | } |
| 6388 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6389 | /** |
| 6390 | * Wake up the dispatcher and wait until it processes all events and commands. |
| 6391 | * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so |
| 6392 | * this method can be safely called from any thread, as long as you've ensured that |
| 6393 | * the work you are interested in completing has already been queued. |
| 6394 | */ |
| 6395 | bool InputDispatcher::waitForIdle() { |
| 6396 | /** |
| 6397 | * Timeout should represent the longest possible time that a device might spend processing |
| 6398 | * events and commands. |
| 6399 | */ |
| 6400 | constexpr std::chrono::duration TIMEOUT = 100ms; |
| 6401 | std::unique_lock lock(mLock); |
| 6402 | mLooper->wake(); |
| 6403 | std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT); |
| 6404 | return result == std::cv_status::no_timeout; |
| 6405 | } |
| 6406 | |
Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 6407 | /** |
| 6408 | * Sets focus to the window identified by the token. This must be called |
| 6409 | * after updating any input window handles. |
| 6410 | * |
| 6411 | * Params: |
| 6412 | * request.token - input channel token used to identify the window that should gain focus. |
| 6413 | * request.focusedToken - the token that the caller expects currently to be focused. If the |
| 6414 | * specified token does not match the currently focused window, this request will be dropped. |
| 6415 | * If the specified focused token matches the currently focused window, the call will succeed. |
| 6416 | * Set this to "null" if this call should succeed no matter what the currently focused token is. |
| 6417 | * request.timestamp - SYSTEM_TIME_MONOTONIC timestamp in nanos set by the client (wm) |
| 6418 | * when requesting the focus change. This determines which request gets |
| 6419 | * precedence if there is a focus change request from another source such as pointer down. |
| 6420 | */ |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6421 | void InputDispatcher::setFocusedWindow(const FocusRequest& request) { |
| 6422 | { // acquire lock |
| 6423 | std::scoped_lock _l(mLock); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6424 | std::optional<FocusResolver::FocusChanges> changes = |
| 6425 | mFocusResolver.setFocusedWindow(request, getWindowHandlesLocked(request.displayId)); |
| 6426 | if (changes) { |
| 6427 | onFocusChangedLocked(*changes); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6428 | } |
| 6429 | } // release lock |
| 6430 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6431 | mLooper->wake(); |
| 6432 | } |
| 6433 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6434 | void InputDispatcher::onFocusChangedLocked(const FocusResolver::FocusChanges& changes) { |
| 6435 | if (changes.oldFocus) { |
| 6436 | std::shared_ptr<InputChannel> focusedInputChannel = getInputChannelLocked(changes.oldFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6437 | if (focusedInputChannel) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 6438 | CancelationOptions options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6439 | "focus left window"); |
| 6440 | synthesizeCancelationEventsForInputChannelLocked(focusedInputChannel, options); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6441 | enqueueFocusEventLocked(changes.oldFocus, /*hasFocus=*/false, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6442 | } |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6443 | } |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6444 | if (changes.newFocus) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6445 | enqueueFocusEventLocked(changes.newFocus, /*hasFocus=*/true, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6446 | } |
| 6447 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6448 | // If a window has pointer capture, then it must have focus. We need to ensure that this |
| 6449 | // contract is upheld when pointer capture is being disabled due to a loss of window focus. |
| 6450 | // If the window loses focus before it loses pointer capture, then the window can be in a state |
| 6451 | // where it has pointer capture but not focus, violating the contract. Therefore we must |
| 6452 | // dispatch the pointer capture event before the focus event. Since focus events are added to |
| 6453 | // the front of the queue (above), we add the pointer capture event to the front of the queue |
| 6454 | // after the focus events are added. This ensures the pointer capture event ends up at the |
| 6455 | // front. |
| 6456 | disablePointerCaptureForcedLocked(); |
| 6457 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6458 | if (mFocusedDisplayId == changes.displayId) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6459 | sendFocusChangedCommandLocked(changes.oldFocus, changes.newFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6460 | } |
| 6461 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6462 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6463 | void InputDispatcher::disablePointerCaptureForcedLocked() { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6464 | if (!mCurrentPointerCaptureRequest.enable && !mWindowTokenWithPointerCapture) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6465 | return; |
| 6466 | } |
| 6467 | |
| 6468 | ALOGD_IF(DEBUG_FOCUS, "Disabling Pointer Capture because the window lost focus."); |
| 6469 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6470 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6471 | setPointerCaptureLocked(false); |
| 6472 | } |
| 6473 | |
| 6474 | if (!mWindowTokenWithPointerCapture) { |
| 6475 | // No need to send capture changes because no window has capture. |
| 6476 | return; |
| 6477 | } |
| 6478 | |
| 6479 | if (mPendingEvent != nullptr) { |
| 6480 | // Move the pending event to the front of the queue. This will give the chance |
| 6481 | // for the pending event to be dropped if it is a captured event. |
| 6482 | mInboundQueue.push_front(mPendingEvent); |
| 6483 | mPendingEvent = nullptr; |
| 6484 | } |
| 6485 | |
| 6486 | auto entry = std::make_unique<PointerCaptureChangedEntry>(mIdGenerator.nextId(), now(), |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6487 | mCurrentPointerCaptureRequest); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6488 | mInboundQueue.push_front(std::move(entry)); |
| 6489 | } |
| 6490 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6491 | void InputDispatcher::setPointerCaptureLocked(bool enable) { |
| 6492 | mCurrentPointerCaptureRequest.enable = enable; |
| 6493 | mCurrentPointerCaptureRequest.seq++; |
| 6494 | auto command = [this, request = mCurrentPointerCaptureRequest]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6495 | scoped_unlock unlock(mLock); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6496 | mPolicy->setPointerCapture(request); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6497 | }; |
| 6498 | postCommandLocked(std::move(command)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6499 | } |
| 6500 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6501 | void InputDispatcher::displayRemoved(int32_t displayId) { |
| 6502 | { // acquire lock |
| 6503 | std::scoped_lock _l(mLock); |
| 6504 | // Set an empty list to remove all handles from the specific display. |
| 6505 | setInputWindowsLocked(/* window handles */ {}, displayId); |
| 6506 | setFocusedApplicationLocked(displayId, nullptr); |
| 6507 | // Call focus resolver to clean up stale requests. This must be called after input windows |
| 6508 | // have been removed for the removed display. |
| 6509 | mFocusResolver.displayRemoved(displayId); |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 6510 | // Reset pointer capture eligibility, regardless of previous state. |
| 6511 | std::erase(mIneligibleDisplaysForPointerCapture, displayId); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 6512 | // Remove the associated touch mode state. |
| 6513 | mTouchModePerDisplay.erase(displayId); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6514 | } // release lock |
| 6515 | |
| 6516 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6517 | mLooper->wake(); |
| 6518 | } |
| 6519 | |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6520 | void InputDispatcher::onWindowInfosChanged(const std::vector<WindowInfo>& windowInfos, |
| 6521 | const std::vector<DisplayInfo>& displayInfos) { |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6522 | // The listener sends the windows as a flattened array. Separate the windows by display for |
| 6523 | // more convenient parsing. |
| 6524 | std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>> handlesPerDisplay; |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6525 | for (const auto& info : windowInfos) { |
| 6526 | handlesPerDisplay.emplace(info.displayId, std::vector<sp<WindowInfoHandle>>()); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6527 | handlesPerDisplay[info.displayId].push_back(sp<WindowInfoHandle>::make(info)); |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6528 | } |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6529 | |
| 6530 | { // acquire lock |
| 6531 | std::scoped_lock _l(mLock); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 6532 | |
| 6533 | // Ensure that we have an entry created for all existing displays so that if a displayId has |
| 6534 | // no windows, we can tell that the windows were removed from the display. |
| 6535 | for (const auto& [displayId, _] : mWindowHandlesByDisplay) { |
| 6536 | handlesPerDisplay[displayId]; |
| 6537 | } |
| 6538 | |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6539 | mDisplayInfos.clear(); |
| 6540 | for (const auto& displayInfo : displayInfos) { |
| 6541 | mDisplayInfos.emplace(displayInfo.displayId, displayInfo); |
| 6542 | } |
| 6543 | |
| 6544 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 6545 | setInputWindowsLocked(handles, displayId); |
| 6546 | } |
| 6547 | } |
| 6548 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6549 | mLooper->wake(); |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6550 | } |
| 6551 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6552 | bool InputDispatcher::shouldDropInput( |
| 6553 | const EventEntry& entry, const sp<android::gui::WindowInfoHandle>& windowHandle) const { |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6554 | if (windowHandle->getInfo()->inputConfig.test(WindowInfo::InputConfig::DROP_INPUT) || |
| 6555 | (windowHandle->getInfo()->inputConfig.test( |
| 6556 | WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED) && |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6557 | isWindowObscuredLocked(windowHandle))) { |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6558 | ALOGW("Dropping %s event targeting %s as requested by the input configuration {%s} on " |
| 6559 | "display %" PRId32 ".", |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6560 | ftl::enum_string(entry.type).c_str(), windowHandle->getName().c_str(), |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6561 | windowHandle->getInfo()->inputConfig.string().c_str(), |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6562 | windowHandle->getInfo()->displayId); |
| 6563 | return true; |
| 6564 | } |
| 6565 | return false; |
| 6566 | } |
| 6567 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 6568 | void InputDispatcher::DispatcherWindowListener::onWindowInfosChanged( |
| 6569 | const std::vector<gui::WindowInfo>& windowInfos, |
| 6570 | const std::vector<DisplayInfo>& displayInfos) { |
| 6571 | mDispatcher.onWindowInfosChanged(windowInfos, displayInfos); |
| 6572 | } |
| 6573 | |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6574 | void InputDispatcher::cancelCurrentTouch() { |
| 6575 | { |
| 6576 | std::scoped_lock _l(mLock); |
| 6577 | ALOGD("Canceling all ongoing pointer gestures on all displays."); |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 6578 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6579 | "cancel current touch"); |
| 6580 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 6581 | |
| 6582 | mTouchStatesByDisplay.clear(); |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6583 | } |
| 6584 | // Wake up poll loop since there might be work to do. |
| 6585 | mLooper->wake(); |
| 6586 | } |
| 6587 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6588 | void InputDispatcher::setMonitorDispatchingTimeoutForTest(std::chrono::nanoseconds timeout) { |
| 6589 | std::scoped_lock _l(mLock); |
| 6590 | mMonitorDispatchingTimeout = timeout; |
| 6591 | } |
| 6592 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6593 | void InputDispatcher::slipWallpaperTouch(ftl::Flags<InputTarget::Flags> targetFlags, |
| 6594 | const sp<WindowInfoHandle>& oldWindowHandle, |
| 6595 | const sp<WindowInfoHandle>& newWindowHandle, |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 6596 | TouchState& state, int32_t pointerId, |
| 6597 | std::vector<InputTarget>& targets) { |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 6598 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
| 6599 | pointerIds.set(pointerId); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6600 | const bool oldHasWallpaper = oldWindowHandle->getInfo()->inputConfig.test( |
| 6601 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER); |
| 6602 | const bool newHasWallpaper = targetFlags.test(InputTarget::Flags::FOREGROUND) && |
| 6603 | newWindowHandle->getInfo()->inputConfig.test( |
| 6604 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER); |
| 6605 | const sp<WindowInfoHandle> oldWallpaper = |
| 6606 | oldHasWallpaper ? state.getWallpaperWindow() : nullptr; |
| 6607 | const sp<WindowInfoHandle> newWallpaper = |
| 6608 | newHasWallpaper ? findWallpaperWindowBelow(newWindowHandle) : nullptr; |
| 6609 | if (oldWallpaper == newWallpaper) { |
| 6610 | return; |
| 6611 | } |
| 6612 | |
| 6613 | if (oldWallpaper != nullptr) { |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 6614 | const TouchedWindow& oldTouchedWindow = state.getTouchedWindow(oldWallpaper); |
| 6615 | addWindowTargetLocked(oldWallpaper, |
| 6616 | oldTouchedWindow.targetFlags | |
| 6617 | InputTarget::Flags::DISPATCH_AS_SLIPPERY_EXIT, |
| 6618 | pointerIds, oldTouchedWindow.firstDownTimeInTarget, targets); |
| 6619 | state.removeTouchedPointerFromWindow(pointerId, oldWallpaper); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6620 | } |
| 6621 | |
| 6622 | if (newWallpaper != nullptr) { |
| 6623 | state.addOrUpdateWindow(newWallpaper, |
| 6624 | InputTarget::Flags::DISPATCH_AS_SLIPPERY_ENTER | |
| 6625 | InputTarget::Flags::WINDOW_IS_OBSCURED | |
| 6626 | InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED, |
| 6627 | pointerIds); |
| 6628 | } |
| 6629 | } |
| 6630 | |
| 6631 | void InputDispatcher::transferWallpaperTouch(ftl::Flags<InputTarget::Flags> oldTargetFlags, |
| 6632 | ftl::Flags<InputTarget::Flags> newTargetFlags, |
| 6633 | const sp<WindowInfoHandle> fromWindowHandle, |
| 6634 | const sp<WindowInfoHandle> toWindowHandle, |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 6635 | TouchState& state, |
| 6636 | std::bitset<MAX_POINTER_ID + 1> pointerIds) { |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6637 | const bool oldHasWallpaper = oldTargetFlags.test(InputTarget::Flags::FOREGROUND) && |
| 6638 | fromWindowHandle->getInfo()->inputConfig.test( |
| 6639 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER); |
| 6640 | const bool newHasWallpaper = newTargetFlags.test(InputTarget::Flags::FOREGROUND) && |
| 6641 | toWindowHandle->getInfo()->inputConfig.test( |
| 6642 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER); |
| 6643 | |
| 6644 | const sp<WindowInfoHandle> oldWallpaper = |
| 6645 | oldHasWallpaper ? state.getWallpaperWindow() : nullptr; |
| 6646 | const sp<WindowInfoHandle> newWallpaper = |
| 6647 | newHasWallpaper ? findWallpaperWindowBelow(toWindowHandle) : nullptr; |
| 6648 | if (oldWallpaper == newWallpaper) { |
| 6649 | return; |
| 6650 | } |
| 6651 | |
| 6652 | if (oldWallpaper != nullptr) { |
| 6653 | CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, |
| 6654 | "transferring touch focus to another window"); |
| 6655 | state.removeWindowByToken(oldWallpaper->getToken()); |
| 6656 | synthesizeCancelationEventsForWindowLocked(oldWallpaper, options); |
| 6657 | } |
| 6658 | |
| 6659 | if (newWallpaper != nullptr) { |
| 6660 | nsecs_t downTimeInTarget = now(); |
| 6661 | ftl::Flags<InputTarget::Flags> wallpaperFlags = |
| 6662 | oldTargetFlags & (InputTarget::Flags::SPLIT | InputTarget::Flags::DISPATCH_AS_IS); |
| 6663 | wallpaperFlags |= InputTarget::Flags::WINDOW_IS_OBSCURED | |
| 6664 | InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED; |
| 6665 | state.addOrUpdateWindow(newWallpaper, wallpaperFlags, pointerIds, downTimeInTarget); |
| 6666 | sp<Connection> wallpaperConnection = getConnectionLocked(newWallpaper->getToken()); |
| 6667 | if (wallpaperConnection != nullptr) { |
| 6668 | sp<Connection> toConnection = getConnectionLocked(toWindowHandle->getToken()); |
| 6669 | toConnection->inputState.mergePointerStateTo(wallpaperConnection->inputState); |
| 6670 | synthesizePointerDownEventsForConnectionLocked(downTimeInTarget, wallpaperConnection, |
| 6671 | wallpaperFlags); |
| 6672 | } |
| 6673 | } |
| 6674 | } |
| 6675 | |
| 6676 | sp<WindowInfoHandle> InputDispatcher::findWallpaperWindowBelow( |
| 6677 | const sp<WindowInfoHandle>& windowHandle) const { |
| 6678 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
| 6679 | getWindowHandlesLocked(windowHandle->getInfo()->displayId); |
| 6680 | bool foundWindow = false; |
| 6681 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
| 6682 | if (!foundWindow && otherHandle != windowHandle) { |
| 6683 | continue; |
| 6684 | } |
| 6685 | if (windowHandle == otherHandle) { |
| 6686 | foundWindow = true; |
| 6687 | continue; |
| 6688 | } |
| 6689 | |
| 6690 | if (otherHandle->getInfo()->inputConfig.test(WindowInfo::InputConfig::IS_WALLPAPER)) { |
| 6691 | return otherHandle; |
| 6692 | } |
| 6693 | } |
| 6694 | return nullptr; |
| 6695 | } |
| 6696 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 6697 | } // namespace android::inputdispatcher |