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> |
Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 23 | #include <android-base/properties.h> |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 24 | #include <android-base/stringprintf.h> |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 25 | #include <android/os/IInputConstants.h> |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 26 | #include <binder/Binder.h> |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 27 | #include <ftl/enum.h> |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 28 | #if defined(__ANDROID__) |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 29 | #include <gui/SurfaceComposerClient.h> |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 30 | #endif |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 31 | #include <input/InputDevice.h> |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 32 | #include <powermanager/PowerManager.h> |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 33 | #include <unistd.h> |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 34 | #include <utils/Trace.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 35 | |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 36 | #include <cerrno> |
| 37 | #include <cinttypes> |
| 38 | #include <climits> |
| 39 | #include <cstddef> |
| 40 | #include <ctime> |
| 41 | #include <queue> |
| 42 | #include <sstream> |
| 43 | |
| 44 | #include "Connection.h" |
Arthur Hung | 1a1007b | 2022-05-11 07:15:01 +0000 | [diff] [blame] | 45 | #include "DebugConfig.h" |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 46 | #include "InputDispatcher.h" |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 47 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 48 | #define INDENT " " |
| 49 | #define INDENT2 " " |
| 50 | #define INDENT3 " " |
| 51 | #define INDENT4 " " |
| 52 | |
Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 53 | using android::base::HwTimeoutMultiplier; |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 54 | using android::base::Result; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 55 | using android::base::StringPrintf; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 56 | using android::gui::DisplayInfo; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 57 | using android::gui::FocusRequest; |
| 58 | using android::gui::TouchOcclusionMode; |
| 59 | using android::gui::WindowInfo; |
| 60 | using android::gui::WindowInfoHandle; |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 61 | using android::os::IInputConstants; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 62 | using android::os::InputEventInjectionResult; |
| 63 | using android::os::InputEventInjectionSync; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 64 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 65 | namespace android::inputdispatcher { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 66 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 67 | namespace { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 68 | // Temporarily releases a held mutex for the lifetime of the instance. |
| 69 | // Named to match std::scoped_lock |
| 70 | class scoped_unlock { |
| 71 | public: |
| 72 | explicit scoped_unlock(std::mutex& mutex) : mMutex(mutex) { mMutex.unlock(); } |
| 73 | ~scoped_unlock() { mMutex.lock(); } |
| 74 | |
| 75 | private: |
| 76 | std::mutex& mMutex; |
| 77 | }; |
| 78 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 79 | // Default input dispatching timeout if there is no focused application or paused window |
| 80 | // from which to determine an appropriate dispatching timeout. |
Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 81 | const std::chrono::duration DEFAULT_INPUT_DISPATCHING_TIMEOUT = std::chrono::milliseconds( |
| 82 | android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS * |
| 83 | HwTimeoutMultiplier()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 84 | |
| 85 | // Amount of time to allow for all pending events to be processed when an app switch |
| 86 | // key is on the way. This is used to preempt input dispatch and drop input events |
| 87 | // 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] | 88 | constexpr nsecs_t APP_SWITCH_TIMEOUT = 500 * 1000000LL; // 0.5sec |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 89 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 90 | const std::chrono::duration STALE_EVENT_TIMEOUT = std::chrono::seconds(10) * HwTimeoutMultiplier(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 91 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 92 | // 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] | 93 | constexpr nsecs_t SLOW_EVENT_PROCESSING_WARNING_TIMEOUT = 2000 * 1000000LL; // 2sec |
| 94 | |
| 95 | // Log a warning when an interception call takes longer than this to process. |
| 96 | constexpr std::chrono::milliseconds SLOW_INTERCEPTION_THRESHOLD = 50ms; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 97 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 98 | // Additional key latency in case a connection is still processing some motion events. |
| 99 | // This will help with the case when a user touched a button that opens a new window, |
| 100 | // and gives us the chance to dispatch the key to this new window. |
| 101 | constexpr std::chrono::nanoseconds KEY_WAITING_FOR_EVENTS_TIMEOUT = 500ms; |
| 102 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 103 | // Number of recent events to keep for debugging purposes. |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 104 | constexpr size_t RECENT_QUEUE_MAX_SIZE = 10; |
| 105 | |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 106 | // Event log tags. See EventLogTags.logtags for reference. |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 107 | constexpr int LOGTAG_INPUT_INTERACTION = 62000; |
| 108 | constexpr int LOGTAG_INPUT_FOCUS = 62001; |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 109 | constexpr int LOGTAG_INPUT_CANCEL = 62003; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 110 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 111 | inline nsecs_t now() { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 112 | return systemTime(SYSTEM_TIME_MONOTONIC); |
| 113 | } |
| 114 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 115 | inline const char* toString(bool value) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 116 | return value ? "true" : "false"; |
| 117 | } |
| 118 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 119 | inline const std::string toString(const sp<IBinder>& binder) { |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 120 | if (binder == nullptr) { |
| 121 | return "<null>"; |
| 122 | } |
| 123 | return StringPrintf("%p", binder.get()); |
| 124 | } |
| 125 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 126 | inline int32_t getMotionEventActionPointerIndex(int32_t action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 127 | return (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> |
| 128 | AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 131 | bool isValidKeyAction(int32_t action) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 132 | switch (action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 133 | case AKEY_EVENT_ACTION_DOWN: |
| 134 | case AKEY_EVENT_ACTION_UP: |
| 135 | return true; |
| 136 | default: |
| 137 | return false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 141 | bool validateKeyEvent(int32_t action) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 142 | if (!isValidKeyAction(action)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 143 | ALOGE("Key event has invalid action code 0x%x", action); |
| 144 | return false; |
| 145 | } |
| 146 | return true; |
| 147 | } |
| 148 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 149 | bool isValidMotionAction(int32_t action, int32_t actionButton, int32_t pointerCount) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 150 | switch (action & AMOTION_EVENT_ACTION_MASK) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 151 | case AMOTION_EVENT_ACTION_DOWN: |
| 152 | case AMOTION_EVENT_ACTION_UP: |
| 153 | case AMOTION_EVENT_ACTION_CANCEL: |
| 154 | case AMOTION_EVENT_ACTION_MOVE: |
| 155 | case AMOTION_EVENT_ACTION_OUTSIDE: |
| 156 | case AMOTION_EVENT_ACTION_HOVER_ENTER: |
| 157 | case AMOTION_EVENT_ACTION_HOVER_MOVE: |
| 158 | case AMOTION_EVENT_ACTION_HOVER_EXIT: |
| 159 | case AMOTION_EVENT_ACTION_SCROLL: |
| 160 | return true; |
| 161 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 162 | case AMOTION_EVENT_ACTION_POINTER_UP: { |
| 163 | int32_t index = getMotionEventActionPointerIndex(action); |
| 164 | return index >= 0 && index < pointerCount; |
| 165 | } |
| 166 | case AMOTION_EVENT_ACTION_BUTTON_PRESS: |
| 167 | case AMOTION_EVENT_ACTION_BUTTON_RELEASE: |
| 168 | return actionButton != 0; |
| 169 | default: |
| 170 | return false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 174 | int64_t millis(std::chrono::nanoseconds t) { |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 175 | return std::chrono::duration_cast<std::chrono::milliseconds>(t).count(); |
| 176 | } |
| 177 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 178 | bool validateMotionEvent(int32_t action, int32_t actionButton, size_t pointerCount, |
| 179 | const PointerProperties* pointerProperties) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 180 | if (!isValidMotionAction(action, actionButton, pointerCount)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 181 | ALOGE("Motion event has invalid action code 0x%x", action); |
| 182 | return false; |
| 183 | } |
| 184 | if (pointerCount < 1 || pointerCount > MAX_POINTERS) { |
Siarhei Vishniakou | 0174738 | 2022-01-20 13:23:27 -0800 | [diff] [blame] | 185 | 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] | 186 | pointerCount, MAX_POINTERS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 187 | return false; |
| 188 | } |
| 189 | BitSet32 pointerIdBits; |
| 190 | for (size_t i = 0; i < pointerCount; i++) { |
| 191 | int32_t id = pointerProperties[i].id; |
| 192 | if (id < 0 || id > MAX_POINTER_ID) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 193 | ALOGE("Motion event has invalid pointer id %d; value must be between 0 and %d", id, |
| 194 | MAX_POINTER_ID); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 195 | return false; |
| 196 | } |
| 197 | if (pointerIdBits.hasBit(id)) { |
| 198 | ALOGE("Motion event has duplicate pointer id %d", id); |
| 199 | return false; |
| 200 | } |
| 201 | pointerIdBits.markBit(id); |
| 202 | } |
| 203 | return true; |
| 204 | } |
| 205 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 206 | std::string dumpRegion(const Region& region) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 207 | if (region.isEmpty()) { |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 208 | return "<empty>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 209 | } |
| 210 | |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 211 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 212 | bool first = true; |
| 213 | Region::const_iterator cur = region.begin(); |
| 214 | Region::const_iterator const tail = region.end(); |
| 215 | while (cur != tail) { |
| 216 | if (first) { |
| 217 | first = false; |
| 218 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 219 | dump += "|"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 220 | } |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 221 | 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] | 222 | cur++; |
| 223 | } |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 224 | return dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 225 | } |
| 226 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 227 | std::string dumpQueue(const std::deque<DispatchEntry*>& queue, nsecs_t currentTime) { |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 228 | constexpr size_t maxEntries = 50; // max events to print |
| 229 | constexpr size_t skipBegin = maxEntries / 2; |
| 230 | const size_t skipEnd = queue.size() - maxEntries / 2; |
| 231 | // skip from maxEntries / 2 ... size() - maxEntries/2 |
| 232 | // only print from 0 .. skipBegin and then from skipEnd .. size() |
| 233 | |
| 234 | std::string dump; |
| 235 | for (size_t i = 0; i < queue.size(); i++) { |
| 236 | const DispatchEntry& entry = *queue[i]; |
| 237 | if (i >= skipBegin && i < skipEnd) { |
| 238 | dump += StringPrintf(INDENT4 "<skipped %zu entries>\n", skipEnd - skipBegin); |
| 239 | i = skipEnd - 1; // it will be incremented to "skipEnd" by 'continue' |
| 240 | continue; |
| 241 | } |
| 242 | dump.append(INDENT4); |
| 243 | dump += entry.eventEntry->getDescription(); |
| 244 | dump += StringPrintf(", seq=%" PRIu32 |
| 245 | ", targetFlags=0x%08x, resolvedAction=%d, age=%" PRId64 "ms", |
| 246 | entry.seq, entry.targetFlags, entry.resolvedAction, |
| 247 | ns2ms(currentTime - entry.eventEntry->eventTime)); |
| 248 | if (entry.deliveryTime != 0) { |
| 249 | // This entry was delivered, so add information on how long we've been waiting |
| 250 | dump += StringPrintf(", wait=%" PRId64 "ms", ns2ms(currentTime - entry.deliveryTime)); |
| 251 | } |
| 252 | dump.append("\n"); |
| 253 | } |
| 254 | return dump; |
| 255 | } |
| 256 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 257 | /** |
| 258 | * Find the entry in std::unordered_map by key, and return it. |
| 259 | * If the entry is not found, return a default constructed entry. |
| 260 | * |
| 261 | * Useful when the entries are vectors, since an empty vector will be returned |
| 262 | * if the entry is not found. |
| 263 | * Also useful when the entries are sp<>. If an entry is not found, nullptr is returned. |
| 264 | */ |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 265 | template <typename K, typename V> |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 266 | V getValueByKey(const std::unordered_map<K, V>& map, K key) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 267 | auto it = map.find(key); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 268 | return it != map.end() ? it->second : V{}; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 269 | } |
| 270 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 271 | bool haveSameToken(const sp<WindowInfoHandle>& first, const sp<WindowInfoHandle>& second) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 272 | if (first == second) { |
| 273 | return true; |
| 274 | } |
| 275 | |
| 276 | if (first == nullptr || second == nullptr) { |
| 277 | return false; |
| 278 | } |
| 279 | |
| 280 | return first->getToken() == second->getToken(); |
| 281 | } |
| 282 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 283 | bool haveSameApplicationToken(const WindowInfo* first, const WindowInfo* second) { |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 284 | if (first == nullptr || second == nullptr) { |
| 285 | return false; |
| 286 | } |
| 287 | return first->applicationInfo.token != nullptr && |
| 288 | first->applicationInfo.token == second->applicationInfo.token; |
| 289 | } |
| 290 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 291 | std::unique_ptr<DispatchEntry> createDispatchEntry(const InputTarget& inputTarget, |
| 292 | std::shared_ptr<EventEntry> eventEntry, |
| 293 | int32_t inputTargetFlags) { |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 294 | if (inputTarget.useDefaultPointerTransform()) { |
| 295 | const ui::Transform& transform = inputTarget.getDefaultPointerTransform(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 296 | return std::make_unique<DispatchEntry>(eventEntry, inputTargetFlags, transform, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 297 | inputTarget.displayTransform, |
| 298 | inputTarget.globalScaleFactor); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | ALOG_ASSERT(eventEntry->type == EventEntry::Type::MOTION); |
| 302 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); |
| 303 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 304 | std::vector<PointerCoords> pointerCoords; |
| 305 | pointerCoords.resize(motionEntry.pointerCount); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 306 | |
| 307 | // Use the first pointer information to normalize all other pointers. This could be any pointer |
| 308 | // 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] | 309 | // uses the transform for the normalized pointer. |
| 310 | const ui::Transform& firstPointerTransform = |
| 311 | inputTarget.pointerTransforms[inputTarget.pointerIds.firstMarkedBit()]; |
| 312 | ui::Transform inverseFirstTransform = firstPointerTransform.inverse(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 313 | |
| 314 | // Iterate through all pointers in the event to normalize against the first. |
| 315 | for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.pointerCount; pointerIndex++) { |
| 316 | const PointerProperties& pointerProperties = motionEntry.pointerProperties[pointerIndex]; |
| 317 | uint32_t pointerId = uint32_t(pointerProperties.id); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 318 | const ui::Transform& currTransform = inputTarget.pointerTransforms[pointerId]; |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 319 | |
| 320 | pointerCoords[pointerIndex].copyFrom(motionEntry.pointerCoords[pointerIndex]); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 321 | // First, apply the current pointer's transform to update the coordinates into |
| 322 | // window space. |
| 323 | pointerCoords[pointerIndex].transform(currTransform); |
| 324 | // Next, apply the inverse transform of the normalized coordinates so the |
| 325 | // current coordinates are transformed into the normalized coordinate space. |
| 326 | pointerCoords[pointerIndex].transform(inverseFirstTransform); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 329 | std::unique_ptr<MotionEntry> combinedMotionEntry = |
| 330 | std::make_unique<MotionEntry>(motionEntry.id, motionEntry.eventTime, |
| 331 | motionEntry.deviceId, motionEntry.source, |
| 332 | motionEntry.displayId, motionEntry.policyFlags, |
| 333 | motionEntry.action, motionEntry.actionButton, |
| 334 | motionEntry.flags, motionEntry.metaState, |
| 335 | motionEntry.buttonState, motionEntry.classification, |
| 336 | motionEntry.edgeFlags, motionEntry.xPrecision, |
| 337 | motionEntry.yPrecision, motionEntry.xCursorPosition, |
| 338 | motionEntry.yCursorPosition, motionEntry.downTime, |
| 339 | motionEntry.pointerCount, motionEntry.pointerProperties, |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 340 | pointerCoords.data()); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 341 | |
| 342 | if (motionEntry.injectionState) { |
| 343 | combinedMotionEntry->injectionState = motionEntry.injectionState; |
| 344 | combinedMotionEntry->injectionState->refCount += 1; |
| 345 | } |
| 346 | |
| 347 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 348 | std::make_unique<DispatchEntry>(std::move(combinedMotionEntry), inputTargetFlags, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 349 | firstPointerTransform, inputTarget.displayTransform, |
| 350 | inputTarget.globalScaleFactor); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 351 | return dispatchEntry; |
| 352 | } |
| 353 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 354 | status_t openInputChannelPair(const std::string& name, std::shared_ptr<InputChannel>& serverChannel, |
| 355 | std::unique_ptr<InputChannel>& clientChannel) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 356 | std::unique_ptr<InputChannel> uniqueServerChannel; |
| 357 | status_t result = InputChannel::openInputChannelPair(name, uniqueServerChannel, clientChannel); |
| 358 | |
| 359 | serverChannel = std::move(uniqueServerChannel); |
| 360 | return result; |
| 361 | } |
| 362 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 363 | template <typename T> |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 364 | 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] | 365 | if (lhs == nullptr && rhs == nullptr) { |
| 366 | return true; |
| 367 | } |
| 368 | if (lhs == nullptr || rhs == nullptr) { |
| 369 | return false; |
| 370 | } |
| 371 | return *lhs == *rhs; |
| 372 | } |
| 373 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 374 | KeyEvent createKeyEvent(const KeyEntry& entry) { |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 375 | KeyEvent event; |
| 376 | event.initialize(entry.id, entry.deviceId, entry.source, entry.displayId, INVALID_HMAC, |
| 377 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, |
| 378 | entry.repeatCount, entry.downTime, entry.eventTime); |
| 379 | return event; |
| 380 | } |
| 381 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 382 | bool shouldReportMetricsForConnection(const Connection& connection) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 383 | // Do not keep track of gesture monitors. They receive every event and would disproportionately |
| 384 | // affect the statistics. |
| 385 | if (connection.monitor) { |
| 386 | return false; |
| 387 | } |
| 388 | // If the connection is experiencing ANR, let's skip it. We have separate ANR metrics |
| 389 | if (!connection.responsive) { |
| 390 | return false; |
| 391 | } |
| 392 | return true; |
| 393 | } |
| 394 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 395 | bool shouldReportFinishedEvent(const DispatchEntry& dispatchEntry, const Connection& connection) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 396 | const EventEntry& eventEntry = *dispatchEntry.eventEntry; |
| 397 | const int32_t& inputEventId = eventEntry.id; |
| 398 | if (inputEventId != dispatchEntry.resolvedEventId) { |
| 399 | // Event was transmuted |
| 400 | return false; |
| 401 | } |
| 402 | if (inputEventId == android::os::IInputConstants::INVALID_INPUT_EVENT_ID) { |
| 403 | return false; |
| 404 | } |
| 405 | // Only track latency for events that originated from hardware |
| 406 | if (eventEntry.isSynthesized()) { |
| 407 | return false; |
| 408 | } |
| 409 | const EventEntry::Type& inputEventEntryType = eventEntry.type; |
| 410 | if (inputEventEntryType == EventEntry::Type::KEY) { |
| 411 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 412 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
| 413 | return false; |
| 414 | } |
| 415 | } else if (inputEventEntryType == EventEntry::Type::MOTION) { |
| 416 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 417 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL || |
| 418 | motionEntry.action == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 419 | return false; |
| 420 | } |
| 421 | } else { |
| 422 | // Not a key or a motion |
| 423 | return false; |
| 424 | } |
| 425 | if (!shouldReportMetricsForConnection(connection)) { |
| 426 | return false; |
| 427 | } |
| 428 | return true; |
| 429 | } |
| 430 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 431 | /** |
| 432 | * Connection is responsive if it has no events in the waitQueue that are older than the |
| 433 | * current time. |
| 434 | */ |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 435 | bool isConnectionResponsive(const Connection& connection) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 436 | const nsecs_t currentTime = now(); |
| 437 | for (const DispatchEntry* entry : connection.waitQueue) { |
| 438 | if (entry->timeoutTime < currentTime) { |
| 439 | return false; |
| 440 | } |
| 441 | } |
| 442 | return true; |
| 443 | } |
| 444 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 445 | // Returns true if the event type passed as argument represents a user activity. |
| 446 | bool isUserActivityEvent(const EventEntry& eventEntry) { |
| 447 | switch (eventEntry.type) { |
| 448 | case EventEntry::Type::FOCUS: |
| 449 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 450 | case EventEntry::Type::DRAG: |
| 451 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
| 452 | case EventEntry::Type::SENSOR: |
| 453 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 454 | return false; |
| 455 | case EventEntry::Type::DEVICE_RESET: |
| 456 | case EventEntry::Type::KEY: |
| 457 | case EventEntry::Type::MOTION: |
| 458 | return true; |
| 459 | } |
| 460 | } |
| 461 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 462 | // 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] | 463 | bool windowAcceptsTouchAt(const WindowInfo& windowInfo, int32_t displayId, int32_t x, int32_t y, |
| 464 | bool isStylus) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 465 | const auto inputConfig = windowInfo.inputConfig; |
| 466 | if (windowInfo.displayId != displayId || |
| 467 | inputConfig.test(WindowInfo::InputConfig::NOT_VISIBLE)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 468 | return false; |
| 469 | } |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 470 | const bool windowCanInterceptTouch = isStylus && windowInfo.interceptsStylus(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 471 | if (inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE) && !windowCanInterceptTouch) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 472 | return false; |
| 473 | } |
Prabir Pradhan | 0634904 | 2022-02-04 09:19:17 -0800 | [diff] [blame] | 474 | if (!windowInfo.touchableRegionContainsPoint(x, y)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 475 | return false; |
| 476 | } |
| 477 | return true; |
| 478 | } |
| 479 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 480 | bool isPointerFromStylus(const MotionEntry& entry, int32_t pointerIndex) { |
| 481 | return isFromSource(entry.source, AINPUT_SOURCE_STYLUS) && |
Prabir Pradhan | e562696 | 2022-10-27 20:30:53 +0000 | [diff] [blame^] | 482 | isStylusToolType(entry.pointerProperties[pointerIndex].toolType); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 483 | } |
| 484 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 485 | // Determines if the given window can be targeted as InputTarget::FLAG_FOREGROUND. |
| 486 | // Foreground events are only sent to "foreground targetable" windows, but not all gestures sent to |
| 487 | // such window are necessarily targeted with the flag. For example, an event with ACTION_OUTSIDE can |
| 488 | // be sent to such a window, but it is not a foreground event and doesn't use |
| 489 | // InputTarget::FLAG_FOREGROUND. |
| 490 | bool canReceiveForegroundTouches(const WindowInfo& info) { |
| 491 | // A non-touchable window can still receive touch events (e.g. in the case of |
| 492 | // STYLUS_INTERCEPTOR), so prevent such windows from receiving foreground events for touches. |
| 493 | return !info.inputConfig.test(gui::WindowInfo::InputConfig::NOT_TOUCHABLE) && !info.isSpy(); |
| 494 | } |
| 495 | |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 496 | bool isWindowOwnedBy(const sp<WindowInfoHandle>& windowHandle, int32_t pid, int32_t uid) { |
| 497 | if (windowHandle == nullptr) { |
| 498 | return false; |
| 499 | } |
| 500 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 501 | if (pid == windowInfo->ownerPid && uid == windowInfo->ownerUid) { |
| 502 | return true; |
| 503 | } |
| 504 | return false; |
| 505 | } |
| 506 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 507 | // Checks targeted injection using the window's owner's uid. |
| 508 | // Returns an empty string if an entry can be sent to the given window, or an error message if the |
| 509 | // entry is a targeted injection whose uid target doesn't match the window owner. |
| 510 | std::optional<std::string> verifyTargetedInjection(const sp<WindowInfoHandle>& window, |
| 511 | const EventEntry& entry) { |
| 512 | if (entry.injectionState == nullptr || !entry.injectionState->targetUid) { |
| 513 | // The event was not injected, or the injected event does not target a window. |
| 514 | return {}; |
| 515 | } |
| 516 | const int32_t uid = *entry.injectionState->targetUid; |
| 517 | if (window == nullptr) { |
| 518 | return StringPrintf("No valid window target for injection into uid %d.", uid); |
| 519 | } |
| 520 | if (entry.injectionState->targetUid != window->getInfo()->ownerUid) { |
| 521 | return StringPrintf("Injected event targeted at uid %d would be dispatched to window '%s' " |
| 522 | "owned by uid %d.", |
| 523 | uid, window->getName().c_str(), window->getInfo()->ownerUid); |
| 524 | } |
| 525 | return {}; |
| 526 | } |
| 527 | |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 528 | Point resolveTouchedPosition(const MotionEntry& entry) { |
| 529 | const bool isFromMouse = isFromSource(entry.source, AINPUT_SOURCE_MOUSE); |
| 530 | // Always dispatch mouse events to cursor position. |
| 531 | if (isFromMouse) { |
| 532 | return Point(static_cast<int32_t>(entry.xCursorPosition), |
| 533 | static_cast<int32_t>(entry.yCursorPosition)); |
| 534 | } |
| 535 | |
| 536 | const int32_t pointerIndex = getMotionEventActionPointerIndex(entry.action); |
| 537 | return Point(static_cast<int32_t>( |
| 538 | entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_X)), |
| 539 | static_cast<int32_t>( |
| 540 | entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_Y))); |
| 541 | } |
| 542 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 543 | std::optional<nsecs_t> getDownTime(const EventEntry& eventEntry) { |
| 544 | if (eventEntry.type == EventEntry::Type::KEY) { |
| 545 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 546 | return keyEntry.downTime; |
| 547 | } else if (eventEntry.type == EventEntry::Type::MOTION) { |
| 548 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 549 | return motionEntry.downTime; |
| 550 | } |
| 551 | return std::nullopt; |
| 552 | } |
| 553 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 554 | } // namespace |
| 555 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 556 | // --- InputDispatcher --- |
| 557 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 558 | InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy) |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 559 | : InputDispatcher(policy, STALE_EVENT_TIMEOUT) {} |
| 560 | |
| 561 | InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy, |
| 562 | std::chrono::nanoseconds staleEventTimeout) |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 563 | : mPolicy(policy), |
| 564 | mPendingEvent(nullptr), |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 565 | mLastDropReason(DropReason::NOT_DROPPED), |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 566 | mIdGenerator(IdGenerator::Source::INPUT_DISPATCHER), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 567 | mAppSwitchSawKeyDown(false), |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 568 | mAppSwitchDueTime(LLONG_MAX), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 569 | mNextUnblockedEvent(nullptr), |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 570 | mMonitorDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT), |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 571 | mDispatchEnabled(false), |
| 572 | mDispatchFrozen(false), |
| 573 | mInputFilterEnabled(false), |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 574 | mMaximumObscuringOpacityForTouch(1.0f), |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 575 | mFocusedDisplayId(ADISPLAY_ID_DEFAULT), |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 576 | mWindowTokenWithPointerCapture(nullptr), |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 577 | mStaleEventTimeout(staleEventTimeout), |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 578 | mLatencyAggregator(), |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 579 | mLatencyTracker(&mLatencyAggregator) { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 580 | mLooper = sp<Looper>::make(false); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 581 | mReporter = createInputReporter(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 582 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 583 | mWindowInfoListener = sp<DispatcherWindowListener>::make(*this); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 584 | #if defined(__ANDROID__) |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 585 | SurfaceComposerClient::getDefault()->addWindowInfosListener(mWindowInfoListener); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 586 | #endif |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 587 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 588 | policy->getDispatcherConfiguration(&mConfig); |
| 589 | } |
| 590 | |
| 591 | InputDispatcher::~InputDispatcher() { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 592 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 593 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 594 | resetKeyRepeatLocked(); |
| 595 | releasePendingEventLocked(); |
| 596 | drainInboundQueueLocked(); |
| 597 | mCommandQueue.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 598 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 599 | while (!mConnectionsByToken.empty()) { |
| 600 | sp<Connection> connection = mConnectionsByToken.begin()->second; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 601 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), |
| 602 | false /* notify */); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 603 | } |
| 604 | } |
| 605 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 606 | status_t InputDispatcher::start() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 607 | if (mThread) { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 608 | return ALREADY_EXISTS; |
| 609 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 610 | mThread = std::make_unique<InputThread>( |
| 611 | "InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); }); |
| 612 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | status_t InputDispatcher::stop() { |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 616 | if (mThread && mThread->isCallingThread()) { |
| 617 | ALOGE("InputDispatcher cannot be stopped from its own thread!"); |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 618 | return INVALID_OPERATION; |
| 619 | } |
Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 620 | mThread.reset(); |
| 621 | return OK; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 622 | } |
| 623 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 624 | void InputDispatcher::dispatchOnce() { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 625 | nsecs_t nextWakeupTime = LLONG_MAX; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 626 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 627 | std::scoped_lock _l(mLock); |
| 628 | mDispatcherIsAlive.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 629 | |
| 630 | // Run a dispatch loop if there are no pending commands. |
| 631 | // The dispatch loop might enqueue commands to run afterwards. |
| 632 | if (!haveCommandsLocked()) { |
| 633 | dispatchOnceInnerLocked(&nextWakeupTime); |
| 634 | } |
| 635 | |
| 636 | // Run all pending commands if there are any. |
| 637 | // 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] | 638 | if (runCommandsLockedInterruptable()) { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 639 | nextWakeupTime = LLONG_MIN; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 640 | } |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 641 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 642 | // If we are still waiting for ack on some events, |
| 643 | // we might have to wake up earlier to check if an app is anr'ing. |
| 644 | const nsecs_t nextAnrCheck = processAnrsLocked(); |
| 645 | nextWakeupTime = std::min(nextWakeupTime, nextAnrCheck); |
| 646 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 647 | // We are about to enter an infinitely long sleep, because we have no commands or |
| 648 | // pending or queued events |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 649 | if (nextWakeupTime == LLONG_MAX) { |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 650 | mDispatcherEnteredIdle.notify_all(); |
| 651 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 652 | } // release lock |
| 653 | |
| 654 | // Wait for callback or timeout or wake. (make sure we round up, not down) |
| 655 | nsecs_t currentTime = now(); |
| 656 | int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime); |
| 657 | mLooper->pollOnce(timeoutMillis); |
| 658 | } |
| 659 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 660 | /** |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 661 | * Raise ANR if there is no focused window. |
| 662 | * Before the ANR is raised, do a final state check: |
| 663 | * 1. The currently focused application must be the same one we are waiting for. |
| 664 | * 2. Ensure we still don't have a focused window. |
| 665 | */ |
| 666 | void InputDispatcher::processNoFocusedWindowAnrLocked() { |
| 667 | // Check if the application that we are waiting for is still focused. |
| 668 | std::shared_ptr<InputApplicationHandle> focusedApplication = |
| 669 | getValueByKey(mFocusedApplicationHandlesByDisplay, mAwaitedApplicationDisplayId); |
| 670 | if (focusedApplication == nullptr || |
| 671 | focusedApplication->getApplicationToken() != |
| 672 | mAwaitedFocusedApplication->getApplicationToken()) { |
| 673 | // Unexpected because we should have reset the ANR timer when focused application changed |
| 674 | ALOGE("Waited for a focused window, but focused application has already changed to %s", |
| 675 | focusedApplication->getName().c_str()); |
| 676 | return; // The focused application has changed. |
| 677 | } |
| 678 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 679 | const sp<WindowInfoHandle>& focusedWindowHandle = |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 680 | getFocusedWindowHandleLocked(mAwaitedApplicationDisplayId); |
| 681 | if (focusedWindowHandle != nullptr) { |
| 682 | return; // We now have a focused window. No need for ANR. |
| 683 | } |
| 684 | onAnrLocked(mAwaitedFocusedApplication); |
| 685 | } |
| 686 | |
| 687 | /** |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 688 | * Check if any of the connections' wait queues have events that are too old. |
| 689 | * If we waited for events to be ack'ed for more than the window timeout, raise an ANR. |
| 690 | * Return the time at which we should wake up next. |
| 691 | */ |
| 692 | nsecs_t InputDispatcher::processAnrsLocked() { |
| 693 | const nsecs_t currentTime = now(); |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 694 | nsecs_t nextAnrCheck = LLONG_MAX; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 695 | // Check if we are waiting for a focused window to appear. Raise ANR if waited too long |
| 696 | if (mNoFocusedWindowTimeoutTime.has_value() && mAwaitedFocusedApplication != nullptr) { |
| 697 | if (currentTime >= *mNoFocusedWindowTimeoutTime) { |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 698 | processNoFocusedWindowAnrLocked(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 699 | mAwaitedFocusedApplication.reset(); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 700 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 701 | return LLONG_MIN; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 702 | } else { |
Siarhei Vishniakou | 38a6d27 | 2020-10-20 20:29:33 -0500 | [diff] [blame] | 703 | // Keep waiting. We will drop the event when mNoFocusedWindowTimeoutTime comes. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 704 | nextAnrCheck = *mNoFocusedWindowTimeoutTime; |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | // Check if any connection ANRs are due |
| 709 | nextAnrCheck = std::min(nextAnrCheck, mAnrTracker.firstTimeout()); |
| 710 | if (currentTime < nextAnrCheck) { // most likely scenario |
| 711 | return nextAnrCheck; // everything is normal. Let's check again at nextAnrCheck |
| 712 | } |
| 713 | |
| 714 | // If we reached here, we have an unresponsive connection. |
| 715 | sp<Connection> connection = getConnectionLocked(mAnrTracker.firstToken()); |
| 716 | if (connection == nullptr) { |
| 717 | ALOGE("Could not find connection for entry %" PRId64, mAnrTracker.firstTimeout()); |
| 718 | return nextAnrCheck; |
| 719 | } |
| 720 | connection->responsive = false; |
| 721 | // Stop waking up for this unresponsive connection |
| 722 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 723 | onAnrLocked(connection); |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 724 | return LLONG_MIN; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 725 | } |
| 726 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 727 | std::chrono::nanoseconds InputDispatcher::getDispatchingTimeoutLocked( |
| 728 | const sp<Connection>& connection) { |
| 729 | if (connection->monitor) { |
| 730 | return mMonitorDispatchingTimeout; |
| 731 | } |
| 732 | const sp<WindowInfoHandle> window = |
| 733 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 734 | if (window != nullptr) { |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 735 | return window->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 736 | } |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 737 | return DEFAULT_INPUT_DISPATCHING_TIMEOUT; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 738 | } |
| 739 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 740 | void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) { |
| 741 | nsecs_t currentTime = now(); |
| 742 | |
Jeff Brown | dc5992e | 2014-04-11 01:27:26 -0700 | [diff] [blame] | 743 | // Reset the key repeat timer whenever normal dispatch is suspended while the |
| 744 | // device is in a non-interactive state. This is to ensure that we abort a key |
| 745 | // repeat if the device is just coming out of sleep. |
| 746 | if (!mDispatchEnabled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 747 | resetKeyRepeatLocked(); |
| 748 | } |
| 749 | |
| 750 | // If dispatching is frozen, do not process timeouts or try to deliver any new events. |
| 751 | if (mDispatchFrozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 752 | if (DEBUG_FOCUS) { |
| 753 | ALOGD("Dispatch frozen. Waiting some more."); |
| 754 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 755 | return; |
| 756 | } |
| 757 | |
| 758 | // Optimize latency of app switches. |
| 759 | // Essentially we start a short timeout when an app switch key (HOME / ENDCALL) has |
| 760 | // been pressed. When it expires, we preempt dispatch and drop all other pending events. |
| 761 | bool isAppSwitchDue = mAppSwitchDueTime <= currentTime; |
| 762 | if (mAppSwitchDueTime < *nextWakeupTime) { |
| 763 | *nextWakeupTime = mAppSwitchDueTime; |
| 764 | } |
| 765 | |
| 766 | // Ready to start a new event. |
| 767 | // If we don't already have a pending event, go grab one. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 768 | if (!mPendingEvent) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 769 | if (mInboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 770 | if (isAppSwitchDue) { |
| 771 | // The inbound queue is empty so the app switch key we were waiting |
| 772 | // for will never arrive. Stop waiting for it. |
| 773 | resetPendingAppSwitchLocked(false); |
| 774 | isAppSwitchDue = false; |
| 775 | } |
| 776 | |
| 777 | // Synthesize a key repeat if appropriate. |
| 778 | if (mKeyRepeatState.lastKeyEntry) { |
| 779 | if (currentTime >= mKeyRepeatState.nextRepeatTime) { |
| 780 | mPendingEvent = synthesizeKeyRepeatLocked(currentTime); |
| 781 | } else { |
| 782 | if (mKeyRepeatState.nextRepeatTime < *nextWakeupTime) { |
| 783 | *nextWakeupTime = mKeyRepeatState.nextRepeatTime; |
| 784 | } |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | // Nothing to do if there is no pending event. |
| 789 | if (!mPendingEvent) { |
| 790 | return; |
| 791 | } |
| 792 | } else { |
| 793 | // Inbound queue has at least one entry. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 794 | mPendingEvent = mInboundQueue.front(); |
| 795 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 796 | traceInboundQueueLengthLocked(); |
| 797 | } |
| 798 | |
| 799 | // Poke user activity for this event. |
| 800 | if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 801 | pokeUserActivityLocked(*mPendingEvent); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 802 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | // Now we have an event to dispatch. |
| 806 | // 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] | 807 | ALOG_ASSERT(mPendingEvent != nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 808 | bool done = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 809 | DropReason dropReason = DropReason::NOT_DROPPED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 810 | if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 811 | dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 812 | } else if (!mDispatchEnabled) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 813 | dropReason = DropReason::DISABLED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | if (mNextUnblockedEvent == mPendingEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 817 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | switch (mPendingEvent->type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 821 | case EventEntry::Type::CONFIGURATION_CHANGED: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 822 | const ConfigurationChangedEntry& typedEntry = |
| 823 | static_cast<const ConfigurationChangedEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 824 | done = dispatchConfigurationChangedLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 825 | dropReason = DropReason::NOT_DROPPED; // configuration changes are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 826 | break; |
| 827 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 828 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 829 | case EventEntry::Type::DEVICE_RESET: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 830 | const DeviceResetEntry& typedEntry = |
| 831 | static_cast<const DeviceResetEntry&>(*mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 832 | done = dispatchDeviceResetLocked(currentTime, typedEntry); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 833 | dropReason = DropReason::NOT_DROPPED; // device resets are never dropped |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 834 | break; |
| 835 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 836 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 837 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 838 | std::shared_ptr<FocusEntry> typedEntry = |
| 839 | std::static_pointer_cast<FocusEntry>(mPendingEvent); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 840 | dispatchFocusLocked(currentTime, typedEntry); |
| 841 | done = true; |
| 842 | dropReason = DropReason::NOT_DROPPED; // focus events are never dropped |
| 843 | break; |
| 844 | } |
| 845 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 846 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 847 | const auto typedEntry = std::static_pointer_cast<TouchModeEntry>(mPendingEvent); |
| 848 | dispatchTouchModeChangeLocked(currentTime, typedEntry); |
| 849 | done = true; |
| 850 | dropReason = DropReason::NOT_DROPPED; // touch mode events are never dropped |
| 851 | break; |
| 852 | } |
| 853 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 854 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 855 | const auto typedEntry = |
| 856 | std::static_pointer_cast<PointerCaptureChangedEntry>(mPendingEvent); |
| 857 | dispatchPointerCaptureChangedLocked(currentTime, typedEntry, dropReason); |
| 858 | done = true; |
| 859 | break; |
| 860 | } |
| 861 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 862 | case EventEntry::Type::DRAG: { |
| 863 | std::shared_ptr<DragEntry> typedEntry = |
| 864 | std::static_pointer_cast<DragEntry>(mPendingEvent); |
| 865 | dispatchDragLocked(currentTime, typedEntry); |
| 866 | done = true; |
| 867 | break; |
| 868 | } |
| 869 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 870 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 871 | std::shared_ptr<KeyEntry> keyEntry = std::static_pointer_cast<KeyEntry>(mPendingEvent); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 872 | if (isAppSwitchDue) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 873 | if (isAppSwitchKeyEvent(*keyEntry)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 874 | resetPendingAppSwitchLocked(true); |
| 875 | isAppSwitchDue = false; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 876 | } else if (dropReason == DropReason::NOT_DROPPED) { |
| 877 | dropReason = DropReason::APP_SWITCH; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 878 | } |
| 879 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 880 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *keyEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 881 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 882 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 883 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 884 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 885 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 886 | done = dispatchKeyLocked(currentTime, keyEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 887 | break; |
| 888 | } |
| 889 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 890 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 891 | std::shared_ptr<MotionEntry> motionEntry = |
| 892 | std::static_pointer_cast<MotionEntry>(mPendingEvent); |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 893 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 894 | dropReason = DropReason::APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 895 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 896 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *motionEntry)) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 897 | dropReason = DropReason::STALE; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 898 | } |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 899 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { |
| 900 | dropReason = DropReason::BLOCKED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 901 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 902 | done = dispatchMotionLocked(currentTime, motionEntry, &dropReason, nextWakeupTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 903 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 904 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 905 | |
| 906 | case EventEntry::Type::SENSOR: { |
| 907 | std::shared_ptr<SensorEntry> sensorEntry = |
| 908 | std::static_pointer_cast<SensorEntry>(mPendingEvent); |
| 909 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { |
| 910 | dropReason = DropReason::APP_SWITCH; |
| 911 | } |
| 912 | // Sensor timestamps use SYSTEM_TIME_BOOTTIME time base, so we can't use |
| 913 | // 'currentTime' here, get SYSTEM_TIME_BOOTTIME instead. |
| 914 | nsecs_t bootTime = systemTime(SYSTEM_TIME_BOOTTIME); |
| 915 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(bootTime, *sensorEntry)) { |
| 916 | dropReason = DropReason::STALE; |
| 917 | } |
| 918 | dispatchSensorLocked(currentTime, sensorEntry, &dropReason, nextWakeupTime); |
| 919 | done = true; |
| 920 | break; |
| 921 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | if (done) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 925 | if (dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 926 | dropInboundEventLocked(*mPendingEvent, dropReason); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 927 | } |
Michael Wright | 3a98172 | 2015-06-10 15:26:13 +0100 | [diff] [blame] | 928 | mLastDropReason = dropReason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 929 | |
| 930 | releasePendingEventLocked(); |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 931 | *nextWakeupTime = LLONG_MIN; // force next poll to wake up immediately |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 932 | } |
| 933 | } |
| 934 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 935 | bool InputDispatcher::isStaleEvent(nsecs_t currentTime, const EventEntry& entry) { |
| 936 | return std::chrono::nanoseconds(currentTime - entry.eventTime) >= mStaleEventTimeout; |
| 937 | } |
| 938 | |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 939 | /** |
| 940 | * Return true if the events preceding this incoming motion event should be dropped |
| 941 | * Return false otherwise (the default behaviour) |
| 942 | */ |
| 943 | bool InputDispatcher::shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 944 | const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN && |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 945 | isFromSource(motionEntry.source, AINPUT_SOURCE_CLASS_POINTER); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 946 | |
| 947 | // Optimize case where the current application is unresponsive and the user |
| 948 | // decides to touch a window in a different application. |
| 949 | // If the application takes too long to catch up then we drop all events preceding |
| 950 | // the touch into the other window. |
| 951 | if (isPointerDownEvent && mAwaitedFocusedApplication != nullptr) { |
Siarhei Vishniakou | 9306c38 | 2022-09-30 15:30:31 -0700 | [diff] [blame] | 952 | const int32_t displayId = motionEntry.displayId; |
| 953 | const auto [x, y] = resolveTouchedPosition(motionEntry); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 954 | const bool isStylus = isPointerFromStylus(motionEntry, 0 /*pointerIndex*/); |
Siarhei Vishniakou | 9306c38 | 2022-09-30 15:30:31 -0700 | [diff] [blame] | 955 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 956 | sp<WindowInfoHandle> touchedWindowHandle = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 957 | findTouchedWindowAtLocked(displayId, x, y, nullptr, isStylus); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 958 | if (touchedWindowHandle != nullptr && |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 959 | touchedWindowHandle->getApplicationToken() != |
| 960 | mAwaitedFocusedApplication->getApplicationToken()) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 961 | // User touched a different application than the one we are waiting on. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 962 | ALOGI("Pruning input queue because user touched a different application while waiting " |
| 963 | "for %s", |
| 964 | mAwaitedFocusedApplication->getName().c_str()); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 965 | return true; |
| 966 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 967 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 968 | // Alternatively, maybe there's a spy window that could handle this event. |
| 969 | const std::vector<sp<WindowInfoHandle>> touchedSpies = |
| 970 | findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus); |
| 971 | for (const auto& windowHandle : touchedSpies) { |
| 972 | const sp<Connection> connection = getConnectionLocked(windowHandle->getToken()); |
Siarhei Vishniakou | 34ed4d4 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 973 | if (connection != nullptr && connection->responsive) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 974 | // This spy window could take more input. Drop all events preceding this |
| 975 | // 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] | 976 | 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] | 977 | "responsive spy window that may handle the event.", |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 978 | mAwaitedFocusedApplication->getName().c_str()); |
| 979 | return true; |
| 980 | } |
| 981 | } |
| 982 | } |
| 983 | |
| 984 | // Prevent getting stuck: if we have a pending key event, and some motion events that have not |
| 985 | // yet been processed by some connections, the dispatcher will wait for these motion |
| 986 | // events to be processed before dispatching the key event. This is because these motion events |
| 987 | // may cause a new window to be launched, which the user might expect to receive focus. |
| 988 | // To prevent waiting forever for such events, just send the key to the currently focused window |
| 989 | if (isPointerDownEvent && mKeyIsWaitingForEventsTimeout) { |
| 990 | ALOGD("Received a new pointer down event, stop waiting for events to process and " |
| 991 | "just send the pending key event to the focused window."); |
| 992 | mKeyIsWaitingForEventsTimeout = now(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 993 | } |
| 994 | return false; |
| 995 | } |
| 996 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 997 | bool InputDispatcher::enqueueInboundEventLocked(std::unique_ptr<EventEntry> newEntry) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 998 | bool needWake = mInboundQueue.empty(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 999 | mInboundQueue.push_back(std::move(newEntry)); |
| 1000 | EventEntry& entry = *(mInboundQueue.back()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1001 | traceInboundQueueLengthLocked(); |
| 1002 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1003 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1004 | case EventEntry::Type::KEY: { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1005 | LOG_ALWAYS_FATAL_IF((entry.policyFlags & POLICY_FLAG_TRUSTED) == 0, |
| 1006 | "Unexpected untrusted event."); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1007 | // Optimize app switch latency. |
| 1008 | // If the application takes too long to catch up then we drop all events preceding |
| 1009 | // the app switch key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1010 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1011 | if (isAppSwitchKeyEvent(keyEntry)) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1012 | if (keyEntry.action == AKEY_EVENT_ACTION_DOWN) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1013 | mAppSwitchSawKeyDown = true; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1014 | } else if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1015 | if (mAppSwitchSawKeyDown) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1016 | if (DEBUG_APP_SWITCH) { |
| 1017 | ALOGD("App switch is pending!"); |
| 1018 | } |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1019 | mAppSwitchDueTime = keyEntry.eventTime + APP_SWITCH_TIMEOUT; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1020 | mAppSwitchSawKeyDown = false; |
| 1021 | needWake = true; |
| 1022 | } |
| 1023 | } |
| 1024 | } |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 1025 | |
| 1026 | // If a new up event comes in, and the pending event with same key code has been asked |
| 1027 | // to try again later because of the policy. We have to reset the intercept key wake up |
| 1028 | // time for it may have been handled in the policy and could be dropped. |
| 1029 | if (keyEntry.action == AKEY_EVENT_ACTION_UP && mPendingEvent && |
| 1030 | mPendingEvent->type == EventEntry::Type::KEY) { |
| 1031 | KeyEntry& pendingKey = static_cast<KeyEntry&>(*mPendingEvent); |
| 1032 | if (pendingKey.keyCode == keyEntry.keyCode && |
| 1033 | pendingKey.interceptKeyResult == |
| 1034 | KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER) { |
| 1035 | pendingKey.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; |
| 1036 | pendingKey.interceptKeyWakeupTime = 0; |
| 1037 | needWake = true; |
| 1038 | } |
| 1039 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1040 | break; |
| 1041 | } |
| 1042 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1043 | case EventEntry::Type::MOTION: { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1044 | LOG_ALWAYS_FATAL_IF((entry.policyFlags & POLICY_FLAG_TRUSTED) == 0, |
| 1045 | "Unexpected untrusted event."); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1046 | if (shouldPruneInboundQueueLocked(static_cast<MotionEntry&>(entry))) { |
| 1047 | mNextUnblockedEvent = mInboundQueue.back(); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1048 | needWake = true; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1049 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1050 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1051 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1052 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1053 | LOG_ALWAYS_FATAL("Focus events should be inserted using enqueueFocusEventLocked"); |
| 1054 | break; |
| 1055 | } |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1056 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1057 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1058 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1059 | case EventEntry::Type::SENSOR: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1060 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1061 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1062 | // nothing to do |
| 1063 | break; |
| 1064 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | return needWake; |
| 1068 | } |
| 1069 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1070 | void InputDispatcher::addRecentEventLocked(std::shared_ptr<EventEntry> entry) { |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1071 | // Do not store sensor event in recent queue to avoid flooding the queue. |
| 1072 | if (entry->type != EventEntry::Type::SENSOR) { |
| 1073 | mRecentQueue.push_back(entry); |
| 1074 | } |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1075 | if (mRecentQueue.size() > RECENT_QUEUE_MAX_SIZE) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1076 | mRecentQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1077 | } |
| 1078 | } |
| 1079 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1080 | sp<WindowInfoHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, int32_t x, |
| 1081 | int32_t y, TouchState* touchState, |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1082 | bool isStylus, |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1083 | bool addOutsideTargets, |
Siarhei Vishniakou | 6ebd069 | 2022-10-20 15:05:45 -0700 | [diff] [blame] | 1084 | bool ignoreDragWindow) const { |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 1085 | if (addOutsideTargets && touchState == nullptr) { |
| 1086 | LOG_ALWAYS_FATAL("Must provide a valid touch state if adding outside targets"); |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1087 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1088 | // Traverse windows from front to back to find touched window. |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1089 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1090 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 1091 | if (ignoreDragWindow && haveSameToken(windowHandle, mDragState->dragWindow)) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1092 | continue; |
| 1093 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1094 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1095 | const WindowInfo& info = *windowHandle->getInfo(); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1096 | if (!info.isSpy() && windowAcceptsTouchAt(info, displayId, x, y, isStylus)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1097 | return windowHandle; |
| 1098 | } |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1099 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1100 | if (addOutsideTargets && |
| 1101 | info.inputConfig.test(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1102 | touchState->addOrUpdateWindow(windowHandle, InputTarget::FLAG_DISPATCH_AS_OUTSIDE, |
| 1103 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1104 | } |
| 1105 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1106 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1107 | } |
| 1108 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1109 | std::vector<sp<WindowInfoHandle>> InputDispatcher::findTouchedSpyWindowsAtLocked( |
| 1110 | int32_t displayId, int32_t x, int32_t y, bool isStylus) const { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1111 | // Traverse windows from front to back and gather the touched spy windows. |
| 1112 | std::vector<sp<WindowInfoHandle>> spyWindows; |
| 1113 | const auto& windowHandles = getWindowHandlesLocked(displayId); |
| 1114 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 1115 | const WindowInfo& info = *windowHandle->getInfo(); |
| 1116 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1117 | if (!windowAcceptsTouchAt(info, displayId, x, y, isStylus)) { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1118 | continue; |
| 1119 | } |
| 1120 | if (!info.isSpy()) { |
| 1121 | // The first touched non-spy window was found, so return the spy windows touched so far. |
| 1122 | return spyWindows; |
| 1123 | } |
| 1124 | spyWindows.push_back(windowHandle); |
| 1125 | } |
| 1126 | return spyWindows; |
| 1127 | } |
| 1128 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1129 | void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1130 | const char* reason; |
| 1131 | switch (dropReason) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1132 | case DropReason::POLICY: |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1133 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 1134 | ALOGD("Dropped event because policy consumed it."); |
| 1135 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1136 | reason = "inbound event was dropped because the policy consumed it"; |
| 1137 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1138 | case DropReason::DISABLED: |
| 1139 | if (mLastDropReason != DropReason::DISABLED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1140 | ALOGI("Dropped event because input dispatch is disabled."); |
| 1141 | } |
| 1142 | reason = "inbound event was dropped because input dispatch is disabled"; |
| 1143 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1144 | case DropReason::APP_SWITCH: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1145 | ALOGI("Dropped event because of pending overdue app switch."); |
| 1146 | reason = "inbound event was dropped because of pending overdue app switch"; |
| 1147 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1148 | case DropReason::BLOCKED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1149 | ALOGI("Dropped event because the current application is not responding and the user " |
| 1150 | "has started interacting with a different application."); |
| 1151 | reason = "inbound event was dropped because the current application is not responding " |
| 1152 | "and the user has started interacting with a different application"; |
| 1153 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1154 | case DropReason::STALE: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1155 | ALOGI("Dropped event because it is stale."); |
| 1156 | reason = "inbound event was dropped because it is stale"; |
| 1157 | break; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1158 | case DropReason::NO_POINTER_CAPTURE: |
| 1159 | ALOGI("Dropped event because there is no window with Pointer Capture."); |
| 1160 | reason = "inbound event was dropped because there is no window with Pointer Capture"; |
| 1161 | break; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1162 | case DropReason::NOT_DROPPED: { |
| 1163 | LOG_ALWAYS_FATAL("Should not be dropping a NOT_DROPPED event"); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1164 | return; |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1165 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1166 | } |
| 1167 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1168 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1169 | case EventEntry::Type::KEY: { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1170 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 1171 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1172 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1173 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1174 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1175 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1176 | if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1177 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, reason); |
| 1178 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1179 | } else { |
| 1180 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 1181 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1182 | } |
| 1183 | break; |
| 1184 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1185 | case EventEntry::Type::SENSOR: { |
| 1186 | break; |
| 1187 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1188 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 1189 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1190 | break; |
| 1191 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1192 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1193 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1194 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 1195 | case EventEntry::Type::DEVICE_RESET: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1196 | 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] | 1197 | break; |
| 1198 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1199 | } |
| 1200 | } |
| 1201 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1202 | static bool isAppSwitchKeyCode(int32_t keyCode) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1203 | return keyCode == AKEYCODE_HOME || keyCode == AKEYCODE_ENDCALL || |
| 1204 | keyCode == AKEYCODE_APP_SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1205 | } |
| 1206 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1207 | bool InputDispatcher::isAppSwitchKeyEvent(const KeyEntry& keyEntry) { |
| 1208 | return !(keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) && isAppSwitchKeyCode(keyEntry.keyCode) && |
| 1209 | (keyEntry.policyFlags & POLICY_FLAG_TRUSTED) && |
| 1210 | (keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1211 | } |
| 1212 | |
| 1213 | bool InputDispatcher::isAppSwitchPendingLocked() { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 1214 | return mAppSwitchDueTime != LLONG_MAX; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1215 | } |
| 1216 | |
| 1217 | void InputDispatcher::resetPendingAppSwitchLocked(bool handled) { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 1218 | mAppSwitchDueTime = LLONG_MAX; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1219 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1220 | if (DEBUG_APP_SWITCH) { |
| 1221 | if (handled) { |
| 1222 | ALOGD("App switch has arrived."); |
| 1223 | } else { |
| 1224 | ALOGD("App switch was abandoned."); |
| 1225 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1226 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1227 | } |
| 1228 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1229 | bool InputDispatcher::haveCommandsLocked() const { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1230 | return !mCommandQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1231 | } |
| 1232 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1233 | bool InputDispatcher::runCommandsLockedInterruptable() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1234 | if (mCommandQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1235 | return false; |
| 1236 | } |
| 1237 | |
| 1238 | do { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1239 | auto command = std::move(mCommandQueue.front()); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1240 | mCommandQueue.pop_front(); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1241 | // Commands are run with the lock held, but may release and re-acquire the lock from within. |
| 1242 | command(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1243 | } while (!mCommandQueue.empty()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1244 | return true; |
| 1245 | } |
| 1246 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1247 | void InputDispatcher::postCommandLocked(Command&& command) { |
| 1248 | mCommandQueue.push_back(command); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1249 | } |
| 1250 | |
| 1251 | void InputDispatcher::drainInboundQueueLocked() { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1252 | while (!mInboundQueue.empty()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1253 | std::shared_ptr<EventEntry> entry = mInboundQueue.front(); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1254 | mInboundQueue.pop_front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1255 | releaseInboundEventLocked(entry); |
| 1256 | } |
| 1257 | traceInboundQueueLengthLocked(); |
| 1258 | } |
| 1259 | |
| 1260 | void InputDispatcher::releasePendingEventLocked() { |
| 1261 | if (mPendingEvent) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1262 | releaseInboundEventLocked(mPendingEvent); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1263 | mPendingEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1264 | } |
| 1265 | } |
| 1266 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1267 | void InputDispatcher::releaseInboundEventLocked(std::shared_ptr<EventEntry> entry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1268 | InjectionState* injectionState = entry->injectionState; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1269 | if (injectionState && injectionState->injectionResult == InputEventInjectionResult::PENDING) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1270 | if (DEBUG_DISPATCH_CYCLE) { |
| 1271 | ALOGD("Injected inbound event was dropped."); |
| 1272 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1273 | setInjectionResult(*entry, InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1274 | } |
| 1275 | if (entry == mNextUnblockedEvent) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1276 | mNextUnblockedEvent = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1277 | } |
| 1278 | addRecentEventLocked(entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1279 | } |
| 1280 | |
| 1281 | void InputDispatcher::resetKeyRepeatLocked() { |
| 1282 | if (mKeyRepeatState.lastKeyEntry) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1283 | mKeyRepeatState.lastKeyEntry = nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1284 | } |
| 1285 | } |
| 1286 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1287 | std::shared_ptr<KeyEntry> InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) { |
| 1288 | std::shared_ptr<KeyEntry> entry = mKeyRepeatState.lastKeyEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1289 | |
Michael Wright | 2e73295 | 2014-09-24 13:26:59 -0700 | [diff] [blame] | 1290 | uint32_t policyFlags = entry->policyFlags & |
| 1291 | (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1292 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1293 | std::shared_ptr<KeyEntry> newEntry = |
| 1294 | std::make_unique<KeyEntry>(mIdGenerator.nextId(), currentTime, entry->deviceId, |
| 1295 | entry->source, entry->displayId, policyFlags, entry->action, |
| 1296 | entry->flags, entry->keyCode, entry->scanCode, |
| 1297 | entry->metaState, entry->repeatCount + 1, entry->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1298 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1299 | newEntry->syntheticRepeat = true; |
| 1300 | mKeyRepeatState.lastKeyEntry = newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1301 | mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1302 | return newEntry; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1303 | } |
| 1304 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1305 | bool InputDispatcher::dispatchConfigurationChangedLocked(nsecs_t currentTime, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1306 | const ConfigurationChangedEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1307 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1308 | ALOGD("dispatchConfigurationChanged - eventTime=%" PRId64, entry.eventTime); |
| 1309 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1310 | |
| 1311 | // Reset key repeating in case a keyboard device was added or removed or something. |
| 1312 | resetKeyRepeatLocked(); |
| 1313 | |
| 1314 | // 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] | 1315 | auto command = [this, eventTime = entry.eventTime]() REQUIRES(mLock) { |
| 1316 | scoped_unlock unlock(mLock); |
| 1317 | mPolicy->notifyConfigurationChanged(eventTime); |
| 1318 | }; |
| 1319 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1320 | return true; |
| 1321 | } |
| 1322 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1323 | bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime, |
| 1324 | const DeviceResetEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1325 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1326 | ALOGD("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry.eventTime, |
| 1327 | entry.deviceId); |
| 1328 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1329 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 1330 | // Reset key repeating in case a keyboard device was disabled or enabled. |
| 1331 | if (mKeyRepeatState.lastKeyEntry && mKeyRepeatState.lastKeyEntry->deviceId == entry.deviceId) { |
| 1332 | resetKeyRepeatLocked(); |
| 1333 | } |
| 1334 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1335 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, "device was reset"); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1336 | options.deviceId = entry.deviceId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1337 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1338 | return true; |
| 1339 | } |
| 1340 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1341 | void InputDispatcher::enqueueFocusEventLocked(const sp<IBinder>& windowToken, bool hasFocus, |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1342 | const std::string& reason) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1343 | if (mPendingEvent != nullptr) { |
| 1344 | // Move the pending event to the front of the queue. This will give the chance |
| 1345 | // for the pending event to get dispatched to the newly focused window |
| 1346 | mInboundQueue.push_front(mPendingEvent); |
| 1347 | mPendingEvent = nullptr; |
| 1348 | } |
| 1349 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1350 | std::unique_ptr<FocusEntry> focusEntry = |
| 1351 | std::make_unique<FocusEntry>(mIdGenerator.nextId(), now(), windowToken, hasFocus, |
| 1352 | reason); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1353 | |
| 1354 | // This event should go to the front of the queue, but behind all other focus events |
| 1355 | // Find the last focus event, and insert right after it |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1356 | std::deque<std::shared_ptr<EventEntry>>::reverse_iterator it = |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1357 | std::find_if(mInboundQueue.rbegin(), mInboundQueue.rend(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1358 | [](const std::shared_ptr<EventEntry>& event) { |
| 1359 | return event->type == EventEntry::Type::FOCUS; |
| 1360 | }); |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1361 | |
| 1362 | // 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] | 1363 | mInboundQueue.insert(it.base(), std::move(focusEntry)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1364 | } |
| 1365 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1366 | void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime, std::shared_ptr<FocusEntry> entry) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 1367 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1368 | if (channel == nullptr) { |
| 1369 | return; // Window has gone away |
| 1370 | } |
| 1371 | InputTarget target; |
| 1372 | target.inputChannel = channel; |
| 1373 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1374 | entry->dispatchInProgress = true; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1375 | std::string message = std::string("Focus ") + (entry->hasFocus ? "entering " : "leaving ") + |
| 1376 | channel->getName(); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 1377 | std::string reason = std::string("reason=").append(entry->reason); |
| 1378 | android_log_event_list(LOGTAG_INPUT_FOCUS) << message << reason << LOG_ID_EVENTS; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1379 | dispatchEventLocked(currentTime, entry, {target}); |
| 1380 | } |
| 1381 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1382 | void InputDispatcher::dispatchPointerCaptureChangedLocked( |
| 1383 | nsecs_t currentTime, const std::shared_ptr<PointerCaptureChangedEntry>& entry, |
| 1384 | DropReason& dropReason) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1385 | dropReason = DropReason::NOT_DROPPED; |
| 1386 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1387 | const bool haveWindowWithPointerCapture = mWindowTokenWithPointerCapture != nullptr; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1388 | sp<IBinder> token; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1389 | |
| 1390 | if (entry->pointerCaptureRequest.enable) { |
| 1391 | // Enable Pointer Capture. |
| 1392 | if (haveWindowWithPointerCapture && |
| 1393 | (entry->pointerCaptureRequest == mCurrentPointerCaptureRequest)) { |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 1394 | // This can happen if pointer capture is disabled and re-enabled before we notify the |
| 1395 | // app of the state change, so there is no need to notify the app. |
| 1396 | ALOGI("Skipping dispatch of Pointer Capture being enabled: no state change."); |
| 1397 | return; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1398 | } |
| 1399 | if (!mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1400 | // This can happen if a window requests capture and immediately releases capture. |
| 1401 | ALOGW("No window requested Pointer Capture."); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1402 | dropReason = DropReason::NO_POINTER_CAPTURE; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1403 | return; |
| 1404 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1405 | if (entry->pointerCaptureRequest.seq != mCurrentPointerCaptureRequest.seq) { |
| 1406 | ALOGI("Skipping dispatch of Pointer Capture being enabled: sequence number mismatch."); |
| 1407 | return; |
| 1408 | } |
| 1409 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1410 | token = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1411 | LOG_ALWAYS_FATAL_IF(!token, "Cannot find focused window for Pointer Capture."); |
| 1412 | mWindowTokenWithPointerCapture = token; |
| 1413 | } else { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1414 | // Disable Pointer Capture. |
| 1415 | // We do not check if the sequence number matches for requests to disable Pointer Capture |
| 1416 | // for two reasons: |
| 1417 | // 1. Pointer Capture can be disabled by a focus change, which means we can get two entries |
| 1418 | // to disable capture with the same sequence number: one generated by |
| 1419 | // disablePointerCaptureForcedLocked() and another as an acknowledgement of Pointer |
| 1420 | // Capture being disabled in InputReader. |
| 1421 | // 2. We respect any request to disable Pointer Capture generated by InputReader, since the |
| 1422 | // actual Pointer Capture state that affects events being generated by input devices is |
| 1423 | // in InputReader. |
| 1424 | if (!haveWindowWithPointerCapture) { |
| 1425 | // Pointer capture was already forcefully disabled because of focus change. |
| 1426 | dropReason = DropReason::NOT_DROPPED; |
| 1427 | return; |
| 1428 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1429 | token = mWindowTokenWithPointerCapture; |
| 1430 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1431 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1432 | setPointerCaptureLocked(false); |
| 1433 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1434 | } |
| 1435 | |
| 1436 | auto channel = getInputChannelLocked(token); |
| 1437 | if (channel == nullptr) { |
| 1438 | // Window has gone away, clean up Pointer Capture state. |
| 1439 | mWindowTokenWithPointerCapture = nullptr; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1440 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1441 | setPointerCaptureLocked(false); |
| 1442 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1443 | return; |
| 1444 | } |
| 1445 | InputTarget target; |
| 1446 | target.inputChannel = channel; |
| 1447 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1448 | entry->dispatchInProgress = true; |
| 1449 | dispatchEventLocked(currentTime, entry, {target}); |
| 1450 | |
| 1451 | dropReason = DropReason::NOT_DROPPED; |
| 1452 | } |
| 1453 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1454 | void InputDispatcher::dispatchTouchModeChangeLocked(nsecs_t currentTime, |
| 1455 | const std::shared_ptr<TouchModeEntry>& entry) { |
| 1456 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 1457 | getWindowHandlesLocked(entry->displayId); |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1458 | if (windowHandles.empty()) { |
| 1459 | return; |
| 1460 | } |
| 1461 | const std::vector<InputTarget> inputTargets = |
| 1462 | getInputTargetsFromWindowHandlesLocked(windowHandles); |
| 1463 | if (inputTargets.empty()) { |
| 1464 | return; |
| 1465 | } |
| 1466 | entry->dispatchInProgress = true; |
| 1467 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1468 | } |
| 1469 | |
| 1470 | std::vector<InputTarget> InputDispatcher::getInputTargetsFromWindowHandlesLocked( |
| 1471 | const std::vector<sp<WindowInfoHandle>>& windowHandles) const { |
| 1472 | std::vector<InputTarget> inputTargets; |
| 1473 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1474 | const sp<IBinder>& token = handle->getToken(); |
| 1475 | if (token == nullptr) { |
| 1476 | continue; |
| 1477 | } |
| 1478 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(token); |
| 1479 | if (channel == nullptr) { |
| 1480 | continue; // Window has gone away |
| 1481 | } |
| 1482 | InputTarget target; |
| 1483 | target.inputChannel = channel; |
| 1484 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1485 | inputTargets.push_back(target); |
| 1486 | } |
| 1487 | return inputTargets; |
| 1488 | } |
| 1489 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1490 | bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, std::shared_ptr<KeyEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1491 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1492 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1493 | if (!entry->dispatchInProgress) { |
| 1494 | if (entry->repeatCount == 0 && entry->action == AKEY_EVENT_ACTION_DOWN && |
| 1495 | (entry->policyFlags & POLICY_FLAG_TRUSTED) && |
| 1496 | (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { |
| 1497 | if (mKeyRepeatState.lastKeyEntry && |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1498 | mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode && |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1499 | // We have seen two identical key downs in a row which indicates that the device |
| 1500 | // driver is automatically generating key repeats itself. We take note of the |
| 1501 | // repeat here, but we disable our own next key repeat timer since it is clear that |
| 1502 | // we will not need to synthesize key repeats ourselves. |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1503 | mKeyRepeatState.lastKeyEntry->deviceId == entry->deviceId) { |
| 1504 | // Make sure we don't get key down from a different device. If a different |
| 1505 | // device Id has same key pressed down, the new device Id will replace the |
| 1506 | // current one to hold the key repeat with repeat count reset. |
| 1507 | // In the future when got a KEY_UP on the device id, drop it and do not |
| 1508 | // stop the key repeat on current device. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1509 | entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; |
| 1510 | resetKeyRepeatLocked(); |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 1511 | mKeyRepeatState.nextRepeatTime = LLONG_MAX; // don't generate repeats ourselves |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1512 | } else { |
| 1513 | // Not a repeat. Save key down state in case we do see a repeat later. |
| 1514 | resetKeyRepeatLocked(); |
| 1515 | mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; |
| 1516 | } |
| 1517 | mKeyRepeatState.lastKeyEntry = entry; |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1518 | } else if (entry->action == AKEY_EVENT_ACTION_UP && mKeyRepeatState.lastKeyEntry && |
| 1519 | mKeyRepeatState.lastKeyEntry->deviceId != entry->deviceId) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1520 | // The key on device 'deviceId' is still down, do not stop key repeat |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1521 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 1522 | ALOGD("deviceId=%d got KEY_UP as stale", entry->deviceId); |
| 1523 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1524 | } else if (!entry->syntheticRepeat) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1525 | resetKeyRepeatLocked(); |
| 1526 | } |
| 1527 | |
| 1528 | if (entry->repeatCount == 1) { |
| 1529 | entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; |
| 1530 | } else { |
| 1531 | entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; |
| 1532 | } |
| 1533 | |
| 1534 | entry->dispatchInProgress = true; |
| 1535 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1536 | logOutboundKeyDetails("dispatchKey - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1537 | } |
| 1538 | |
| 1539 | // Handle case where the policy asked us to try again later last time. |
| 1540 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER) { |
| 1541 | if (currentTime < entry->interceptKeyWakeupTime) { |
| 1542 | if (entry->interceptKeyWakeupTime < *nextWakeupTime) { |
| 1543 | *nextWakeupTime = entry->interceptKeyWakeupTime; |
| 1544 | } |
| 1545 | return false; // wait until next wakeup |
| 1546 | } |
| 1547 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; |
| 1548 | entry->interceptKeyWakeupTime = 0; |
| 1549 | } |
| 1550 | |
| 1551 | // Give the policy a chance to intercept the key. |
| 1552 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN) { |
| 1553 | if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1554 | sp<IBinder> focusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1555 | mFocusResolver.getFocusedWindowToken(getTargetDisplayId(*entry)); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1556 | |
| 1557 | auto command = [this, focusedWindowToken, entry]() REQUIRES(mLock) { |
| 1558 | doInterceptKeyBeforeDispatchingCommand(focusedWindowToken, *entry); |
| 1559 | }; |
| 1560 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1561 | return false; // wait for the command to run |
| 1562 | } else { |
| 1563 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
| 1564 | } |
| 1565 | } else if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_SKIP) { |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1566 | if (*dropReason == DropReason::NOT_DROPPED) { |
| 1567 | *dropReason = DropReason::POLICY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1568 | } |
| 1569 | } |
| 1570 | |
| 1571 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1572 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1573 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1574 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1575 | : InputEventInjectionResult::FAILED); |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1576 | mReporter->reportDroppedKey(entry->id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1577 | return true; |
| 1578 | } |
| 1579 | |
| 1580 | // Identify targets. |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1581 | InputEventInjectionResult injectionResult; |
| 1582 | sp<WindowInfoHandle> focusedWindow = |
| 1583 | findFocusedWindowTargetLocked(currentTime, *entry, nextWakeupTime, |
| 1584 | /*byref*/ injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1585 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1586 | return false; |
| 1587 | } |
| 1588 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1589 | setInjectionResult(*entry, injectionResult); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1590 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1591 | return true; |
| 1592 | } |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1593 | LOG_ALWAYS_FATAL_IF(focusedWindow == nullptr); |
| 1594 | |
| 1595 | std::vector<InputTarget> inputTargets; |
| 1596 | addWindowTargetLocked(focusedWindow, |
| 1597 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS, |
| 1598 | BitSet32(0), getDownTime(*entry), inputTargets); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1599 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1600 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1601 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1602 | |
| 1603 | // Dispatch the key. |
| 1604 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1605 | return true; |
| 1606 | } |
| 1607 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1608 | void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1609 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1610 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 ", " |
| 1611 | "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, " |
| 1612 | "metaState=0x%x, repeatCount=%d, downTime=%" PRId64, |
| 1613 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, |
| 1614 | entry.policyFlags, entry.action, entry.flags, entry.keyCode, entry.scanCode, |
| 1615 | entry.metaState, entry.repeatCount, entry.downTime); |
| 1616 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1617 | } |
| 1618 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1619 | void InputDispatcher::dispatchSensorLocked(nsecs_t currentTime, |
| 1620 | const std::shared_ptr<SensorEntry>& entry, |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1621 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1622 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1623 | ALOGD("notifySensorEvent eventTime=%" PRId64 ", hwTimestamp=%" PRId64 ", deviceId=%d, " |
| 1624 | "source=0x%x, sensorType=%s", |
| 1625 | entry->eventTime, entry->hwTimestamp, entry->deviceId, entry->source, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1626 | ftl::enum_string(entry->sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1627 | } |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1628 | auto command = [this, entry]() REQUIRES(mLock) { |
| 1629 | scoped_unlock unlock(mLock); |
| 1630 | |
| 1631 | if (entry->accuracyChanged) { |
| 1632 | mPolicy->notifySensorAccuracy(entry->deviceId, entry->sensorType, entry->accuracy); |
| 1633 | } |
| 1634 | mPolicy->notifySensorEvent(entry->deviceId, entry->sensorType, entry->accuracy, |
| 1635 | entry->hwTimestamp, entry->values); |
| 1636 | }; |
| 1637 | postCommandLocked(std::move(command)); |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1638 | } |
| 1639 | |
| 1640 | bool InputDispatcher::flushSensor(int deviceId, InputDeviceSensorType sensorType) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1641 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1642 | ALOGD("flushSensor deviceId=%d, sensorType=%s", deviceId, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1643 | ftl::enum_string(sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1644 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1645 | { // acquire lock |
| 1646 | std::scoped_lock _l(mLock); |
| 1647 | |
| 1648 | for (auto it = mInboundQueue.begin(); it != mInboundQueue.end(); it++) { |
| 1649 | std::shared_ptr<EventEntry> entry = *it; |
| 1650 | if (entry->type == EventEntry::Type::SENSOR) { |
| 1651 | it = mInboundQueue.erase(it); |
| 1652 | releaseInboundEventLocked(entry); |
| 1653 | } |
| 1654 | } |
| 1655 | } |
| 1656 | return true; |
| 1657 | } |
| 1658 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1659 | bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, std::shared_ptr<MotionEntry> entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1660 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1661 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1662 | // Preprocessing. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1663 | if (!entry->dispatchInProgress) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1664 | entry->dispatchInProgress = true; |
| 1665 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1666 | logOutboundMotionDetails("dispatchMotion - ", *entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1667 | } |
| 1668 | |
| 1669 | // Clean up if dropping the event. |
Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1670 | if (*dropReason != DropReason::NOT_DROPPED) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1671 | setInjectionResult(*entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1672 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED |
| 1673 | : InputEventInjectionResult::FAILED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1674 | return true; |
| 1675 | } |
| 1676 | |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 1677 | const bool isPointerEvent = isFromSource(entry->source, AINPUT_SOURCE_CLASS_POINTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1678 | |
| 1679 | // Identify targets. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1680 | std::vector<InputTarget> inputTargets; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1681 | |
| 1682 | bool conflictingPointerActions = false; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1683 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1684 | if (isPointerEvent) { |
| 1685 | // Pointer event. (eg. touchscreen) |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 1686 | |
| 1687 | if (mDragState && |
| 1688 | (entry->action & AMOTION_EVENT_ACTION_MASK) == AMOTION_EVENT_ACTION_POINTER_DOWN) { |
| 1689 | // If drag and drop ongoing and pointer down occur: pilfer drag window pointers |
| 1690 | pilferPointersLocked(mDragState->dragWindow->getToken()); |
| 1691 | } |
| 1692 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1693 | std::vector<TouchedWindow> touchedWindows = |
Siarhei Vishniakou | 4fe5739 | 2022-10-25 13:44:30 -0700 | [diff] [blame] | 1694 | findTouchedWindowTargetsLocked(currentTime, *entry, &conflictingPointerActions, |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1695 | /*byref*/ injectionResult); |
| 1696 | for (const TouchedWindow& touchedWindow : touchedWindows) { |
| 1697 | LOG_ALWAYS_FATAL_IF(injectionResult != InputEventInjectionResult::SUCCEEDED, |
| 1698 | "Shouldn't be adding window if the injection didn't succeed."); |
| 1699 | addWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags, |
| 1700 | touchedWindow.pointerIds, touchedWindow.firstDownTimeInTarget, |
| 1701 | inputTargets); |
| 1702 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1703 | } else { |
| 1704 | // Non touch event. (eg. trackball) |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1705 | sp<WindowInfoHandle> focusedWindow = |
| 1706 | findFocusedWindowTargetLocked(currentTime, *entry, nextWakeupTime, injectionResult); |
| 1707 | if (injectionResult == InputEventInjectionResult::SUCCEEDED) { |
| 1708 | LOG_ALWAYS_FATAL_IF(focusedWindow == nullptr); |
| 1709 | addWindowTargetLocked(focusedWindow, |
| 1710 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS, |
| 1711 | BitSet32(0), getDownTime(*entry), inputTargets); |
| 1712 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1713 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1714 | if (injectionResult == InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1715 | return false; |
| 1716 | } |
| 1717 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1718 | setInjectionResult(*entry, injectionResult); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1719 | if (injectionResult == InputEventInjectionResult::TARGET_MISMATCH) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1720 | return true; |
| 1721 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1722 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1723 | CancelationOptions::Mode mode(isPointerEvent |
| 1724 | ? CancelationOptions::CANCEL_POINTER_EVENTS |
| 1725 | : CancelationOptions::CANCEL_NON_POINTER_EVENTS); |
| 1726 | CancelationOptions options(mode, "input event injection failed"); |
| 1727 | synthesizeCancelationEventsForMonitorsLocked(options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1728 | return true; |
| 1729 | } |
| 1730 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1731 | // Add monitor channels from event's or focused display. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1732 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1733 | |
| 1734 | // Dispatch the motion. |
| 1735 | if (conflictingPointerActions) { |
| 1736 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1737 | "conflicting pointer actions"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1738 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 1739 | } |
| 1740 | dispatchEventLocked(currentTime, entry, inputTargets); |
| 1741 | return true; |
| 1742 | } |
| 1743 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1744 | void InputDispatcher::enqueueDragEventLocked(const sp<WindowInfoHandle>& windowHandle, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 1745 | bool isExiting, const int32_t rawX, |
| 1746 | const int32_t rawY) { |
| 1747 | const vec2 xy = windowHandle->getInfo()->transform.transform(vec2(rawX, rawY)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1748 | std::unique_ptr<DragEntry> dragEntry = |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 1749 | std::make_unique<DragEntry>(mIdGenerator.nextId(), now(), windowHandle->getToken(), |
| 1750 | isExiting, xy.x, xy.y); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1751 | |
| 1752 | enqueueInboundEventLocked(std::move(dragEntry)); |
| 1753 | } |
| 1754 | |
| 1755 | void InputDispatcher::dispatchDragLocked(nsecs_t currentTime, std::shared_ptr<DragEntry> entry) { |
| 1756 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); |
| 1757 | if (channel == nullptr) { |
| 1758 | return; // Window has gone away |
| 1759 | } |
| 1760 | InputTarget target; |
| 1761 | target.inputChannel = channel; |
| 1762 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 1763 | entry->dispatchInProgress = true; |
| 1764 | dispatchEventLocked(currentTime, entry, {target}); |
| 1765 | } |
| 1766 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1767 | void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry& entry) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1768 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 1769 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
| 1770 | ", policyFlags=0x%x, " |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1771 | "action=%s, actionButton=0x%x, flags=0x%x, " |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1772 | "metaState=0x%x, buttonState=0x%x," |
| 1773 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64, |
| 1774 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1775 | entry.policyFlags, MotionEvent::actionToString(entry.action).c_str(), |
| 1776 | entry.actionButton, entry.flags, entry.metaState, entry.buttonState, entry.edgeFlags, |
| 1777 | entry.xPrecision, entry.yPrecision, entry.downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1778 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1779 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
| 1780 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
| 1781 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 1782 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 1783 | "orientation=%f", |
| 1784 | i, entry.pointerProperties[i].id, entry.pointerProperties[i].toolType, |
| 1785 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 1786 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 1787 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 1788 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 1789 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 1790 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 1791 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 1792 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 1793 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| 1794 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1795 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1796 | } |
| 1797 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1798 | void InputDispatcher::dispatchEventLocked(nsecs_t currentTime, |
| 1799 | std::shared_ptr<EventEntry> eventEntry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1800 | const std::vector<InputTarget>& inputTargets) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1801 | ATRACE_CALL(); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1802 | if (DEBUG_DISPATCH_CYCLE) { |
| 1803 | ALOGD("dispatchEventToCurrentInputTargets"); |
| 1804 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1805 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1806 | updateInteractionTokensLocked(*eventEntry, inputTargets); |
| 1807 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1808 | ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true |
| 1809 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1810 | pokeUserActivityLocked(*eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1811 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1812 | for (const InputTarget& inputTarget : inputTargets) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 1813 | sp<Connection> connection = |
| 1814 | getConnectionLocked(inputTarget.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 1815 | if (connection != nullptr) { |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 1816 | prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1817 | } else { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1818 | if (DEBUG_FOCUS) { |
| 1819 | ALOGD("Dropping event delivery to target with channel '%s' because it " |
| 1820 | "is no longer registered with the input dispatcher.", |
| 1821 | inputTarget.inputChannel->getName().c_str()); |
| 1822 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1823 | } |
| 1824 | } |
| 1825 | } |
| 1826 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1827 | void InputDispatcher::cancelEventsForAnrLocked(const sp<Connection>& connection) { |
| 1828 | // We will not be breaking any connections here, even if the policy wants us to abort dispatch. |
| 1829 | // If the policy decides to close the app, we will get a channel removal event via |
| 1830 | // unregisterInputChannel, and will clean up the connection that way. We are already not |
| 1831 | // sending new pointers to the connection when it blocked, but focused events will continue to |
| 1832 | // pile up. |
| 1833 | ALOGW("Canceling events for %s because it is unresponsive", |
| 1834 | connection->inputChannel->getName().c_str()); |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 1835 | if (connection->status == Connection::Status::NORMAL) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1836 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, |
| 1837 | "application not responding"); |
| 1838 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1839 | } |
| 1840 | } |
| 1841 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1842 | void InputDispatcher::resetNoFocusedWindowTimeoutLocked() { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1843 | if (DEBUG_FOCUS) { |
| 1844 | ALOGD("Resetting ANR timeouts."); |
| 1845 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1846 | |
| 1847 | // Reset input target wait timeout. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1848 | mNoFocusedWindowTimeoutTime = std::nullopt; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1849 | mAwaitedFocusedApplication.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1850 | } |
| 1851 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1852 | /** |
| 1853 | * Get the display id that the given event should go to. If this event specifies a valid display id, |
| 1854 | * then it should be dispatched to that display. Otherwise, the event goes to the focused display. |
| 1855 | * Focused display is the display that the user most recently interacted with. |
| 1856 | */ |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1857 | int32_t InputDispatcher::getTargetDisplayId(const EventEntry& entry) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1858 | int32_t displayId; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1859 | switch (entry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1860 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1861 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 1862 | displayId = keyEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1863 | break; |
| 1864 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1865 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1866 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 1867 | displayId = motionEntry.displayId; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1868 | break; |
| 1869 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1870 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1871 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1872 | case EventEntry::Type::FOCUS: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1873 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1874 | case EventEntry::Type::DEVICE_RESET: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1875 | case EventEntry::Type::SENSOR: |
| 1876 | case EventEntry::Type::DRAG: { |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1877 | 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] | 1878 | return ADISPLAY_ID_NONE; |
| 1879 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1880 | } |
| 1881 | return displayId == ADISPLAY_ID_NONE ? mFocusedDisplayId : displayId; |
| 1882 | } |
| 1883 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1884 | bool InputDispatcher::shouldWaitToSendKeyLocked(nsecs_t currentTime, |
| 1885 | const char* focusedWindowName) { |
| 1886 | if (mAnrTracker.empty()) { |
| 1887 | // already processed all events that we waited for |
| 1888 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1889 | return false; |
| 1890 | } |
| 1891 | |
| 1892 | if (!mKeyIsWaitingForEventsTimeout.has_value()) { |
| 1893 | // Start the timer |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 1894 | // 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] | 1895 | mKeyIsWaitingForEventsTimeout = currentTime + |
| 1896 | std::chrono::duration_cast<std::chrono::nanoseconds>(KEY_WAITING_FOR_EVENTS_TIMEOUT) |
| 1897 | .count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1898 | return true; |
| 1899 | } |
| 1900 | |
| 1901 | // We still have pending events, and already started the timer |
| 1902 | if (currentTime < *mKeyIsWaitingForEventsTimeout) { |
| 1903 | return true; // Still waiting |
| 1904 | } |
| 1905 | |
| 1906 | // Waited too long, and some connection still hasn't processed all motions |
| 1907 | // Just send the key to the focused window |
| 1908 | ALOGW("Dispatching key to %s even though there are other unprocessed events", |
| 1909 | focusedWindowName); |
| 1910 | mKeyIsWaitingForEventsTimeout = std::nullopt; |
| 1911 | return false; |
| 1912 | } |
| 1913 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1914 | sp<WindowInfoHandle> InputDispatcher::findFocusedWindowTargetLocked( |
| 1915 | nsecs_t currentTime, const EventEntry& entry, nsecs_t* nextWakeupTime, |
| 1916 | InputEventInjectionResult& outInjectionResult) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1917 | std::string reason; |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1918 | outInjectionResult = InputEventInjectionResult::FAILED; // Default result |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1919 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1920 | int32_t displayId = getTargetDisplayId(entry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1921 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1922 | std::shared_ptr<InputApplicationHandle> focusedApplicationHandle = |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1923 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 1924 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1925 | // If there is no currently focused window and no focused application |
| 1926 | // then drop the event. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1927 | if (focusedWindowHandle == nullptr && focusedApplicationHandle == nullptr) { |
| 1928 | ALOGI("Dropping %s event because there is no focused window or focused application in " |
| 1929 | "display %" PRId32 ".", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1930 | ftl::enum_string(entry.type).c_str(), displayId); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1931 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1932 | } |
| 1933 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 1934 | // Drop key events if requested by input feature |
| 1935 | if (focusedWindowHandle != nullptr && shouldDropInput(entry, focusedWindowHandle)) { |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1936 | return nullptr; |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 1937 | } |
| 1938 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1939 | // Compatibility behavior: raise ANR if there is a focused application, but no focused window. |
| 1940 | // Only start counting when we have a focused event to dispatch. The ANR is canceled if we |
| 1941 | // start interacting with another application via touch (app switch). This code can be removed |
| 1942 | // if the "no focused window ANR" is moved to the policy. Input doesn't know whether |
| 1943 | // an app is expected to have a focused window. |
| 1944 | if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) { |
| 1945 | if (!mNoFocusedWindowTimeoutTime.has_value()) { |
| 1946 | // 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] | 1947 | std::chrono::nanoseconds timeout = focusedApplicationHandle->getDispatchingTimeout( |
| 1948 | DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
| 1949 | mNoFocusedWindowTimeoutTime = currentTime + timeout.count(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1950 | mAwaitedFocusedApplication = focusedApplicationHandle; |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 1951 | mAwaitedApplicationDisplayId = displayId; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1952 | ALOGW("Waiting because no window has focus but %s may eventually add a " |
| 1953 | "window when it finishes starting up. Will wait for %" PRId64 "ms", |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1954 | mAwaitedFocusedApplication->getName().c_str(), millis(timeout)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1955 | *nextWakeupTime = *mNoFocusedWindowTimeoutTime; |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1956 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 1957 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1958 | } else if (currentTime > *mNoFocusedWindowTimeoutTime) { |
| 1959 | // Already raised ANR. Drop the event |
| 1960 | ALOGE("Dropping %s event because there is no focused window", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1961 | ftl::enum_string(entry.type).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1962 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1963 | } else { |
| 1964 | // Still waiting for the focused window |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1965 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 1966 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1967 | } |
| 1968 | } |
| 1969 | |
| 1970 | // we have a valid, non-null focused window |
| 1971 | resetNoFocusedWindowTimeoutLocked(); |
| 1972 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1973 | // Verify targeted injection. |
| 1974 | if (const auto err = verifyTargetedInjection(focusedWindowHandle, entry); err) { |
| 1975 | ALOGW("Dropping injected event: %s", (*err).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1976 | outInjectionResult = InputEventInjectionResult::TARGET_MISMATCH; |
| 1977 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1978 | } |
| 1979 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1980 | if (focusedWindowHandle->getInfo()->inputConfig.test( |
| 1981 | WindowInfo::InputConfig::PAUSE_DISPATCHING)) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1982 | ALOGI("Waiting because %s is paused", focusedWindowHandle->getName().c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 1983 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 1984 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1985 | } |
| 1986 | |
| 1987 | // If the event is a key event, then we must wait for all previous events to |
| 1988 | // complete before delivering it because previous events may have the |
| 1989 | // side-effect of transferring focus to a different window and we want to |
| 1990 | // ensure that the following keys are sent to the new window. |
| 1991 | // |
| 1992 | // Suppose the user touches a button in a window then immediately presses "A". |
| 1993 | // If the button causes a pop-up window to appear then we want to ensure that |
| 1994 | // the "A" key is delivered to the new pop-up window. This is because users |
| 1995 | // often anticipate pending UI changes when typing on a keyboard. |
| 1996 | // To obtain this behavior, we must serialize key events with respect to all |
| 1997 | // prior input events. |
| 1998 | if (entry.type == EventEntry::Type::KEY) { |
| 1999 | if (shouldWaitToSendKeyLocked(currentTime, focusedWindowHandle->getName().c_str())) { |
| 2000 | *nextWakeupTime = *mKeyIsWaitingForEventsTimeout; |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2001 | outInjectionResult = InputEventInjectionResult::PENDING; |
| 2002 | return nullptr; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2003 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2004 | } |
| 2005 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2006 | outInjectionResult = InputEventInjectionResult::SUCCEEDED; |
| 2007 | return focusedWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2008 | } |
| 2009 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2010 | /** |
| 2011 | * Given a list of monitors, remove the ones we cannot find a connection for, and the ones |
| 2012 | * that are currently unresponsive. |
| 2013 | */ |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2014 | std::vector<Monitor> InputDispatcher::selectResponsiveMonitorsLocked( |
| 2015 | const std::vector<Monitor>& monitors) const { |
| 2016 | std::vector<Monitor> responsiveMonitors; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2017 | std::copy_if(monitors.begin(), monitors.end(), std::back_inserter(responsiveMonitors), |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2018 | [this](const Monitor& monitor) REQUIRES(mLock) { |
| 2019 | sp<Connection> connection = |
| 2020 | getConnectionLocked(monitor.inputChannel->getConnectionToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2021 | if (connection == nullptr) { |
| 2022 | ALOGE("Could not find connection for monitor %s", |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2023 | monitor.inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2024 | return false; |
| 2025 | } |
| 2026 | if (!connection->responsive) { |
| 2027 | ALOGW("Unresponsive monitor %s will not get the new gesture", |
| 2028 | connection->inputChannel->getName().c_str()); |
| 2029 | return false; |
| 2030 | } |
| 2031 | return true; |
| 2032 | }); |
| 2033 | return responsiveMonitors; |
| 2034 | } |
| 2035 | |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 2036 | /** |
| 2037 | * In general, touch should be always split between windows. Some exceptions: |
| 2038 | * 1. Don't split touch is if we have an active pointer down, and a new pointer is going down that's |
| 2039 | * from the same device, *and* the window that's receiving the current pointer does not support |
| 2040 | * split touch. |
| 2041 | * 2. Don't split mouse events |
| 2042 | */ |
| 2043 | bool InputDispatcher::shouldSplitTouch(const TouchState& touchState, |
| 2044 | const MotionEntry& entry) const { |
| 2045 | if (isFromSource(entry.source, AINPUT_SOURCE_MOUSE)) { |
| 2046 | // We should never split mouse events |
| 2047 | return false; |
| 2048 | } |
| 2049 | for (const TouchedWindow& touchedWindow : touchState.windows) { |
| 2050 | if (touchedWindow.windowHandle->getInfo()->isSpy()) { |
| 2051 | // Spy windows should not affect whether or not touch is split. |
| 2052 | continue; |
| 2053 | } |
| 2054 | if (touchedWindow.windowHandle->getInfo()->supportsSplitTouch()) { |
| 2055 | continue; |
| 2056 | } |
| 2057 | // Eventually, touchedWindow will contain the deviceId of each pointer that's currently |
| 2058 | // being sent there. For now, use deviceId from touch state. |
| 2059 | if (entry.deviceId == touchState.deviceId && !touchedWindow.pointerIds.isEmpty()) { |
| 2060 | return false; |
| 2061 | } |
| 2062 | } |
| 2063 | return true; |
| 2064 | } |
| 2065 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2066 | std::vector<TouchedWindow> InputDispatcher::findTouchedWindowTargetsLocked( |
Siarhei Vishniakou | 4fe5739 | 2022-10-25 13:44:30 -0700 | [diff] [blame] | 2067 | nsecs_t currentTime, const MotionEntry& entry, bool* outConflictingPointerActions, |
| 2068 | InputEventInjectionResult& outInjectionResult) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2069 | ATRACE_CALL(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2070 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2071 | std::vector<TouchedWindow> touchedWindows; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2072 | // For security reasons, we defer updating the touch state until we are sure that |
| 2073 | // event injection will be allowed. |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2074 | const int32_t displayId = entry.displayId; |
| 2075 | const int32_t action = entry.action; |
| 2076 | const int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2077 | |
| 2078 | // 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] | 2079 | outInjectionResult = InputEventInjectionResult::PENDING; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2080 | sp<WindowInfoHandle> newHoverWindowHandle(mLastHoverWindowHandle); |
| 2081 | sp<WindowInfoHandle> newTouchedWindowHandle; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2082 | |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2083 | // Copy current touch state into tempTouchState. |
| 2084 | // This state will be used to update mTouchStatesByDisplay at the end of this function. |
| 2085 | // 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] | 2086 | const TouchState* oldState = nullptr; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2087 | TouchState tempTouchState; |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2088 | if (const auto it = mTouchStatesByDisplay.find(displayId); it != mTouchStatesByDisplay.end()) { |
| 2089 | oldState = &(it->second); |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 2090 | tempTouchState = *oldState; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2091 | } |
| 2092 | |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 2093 | bool isSplit = shouldSplitTouch(tempTouchState, entry); |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 2094 | const bool switchedDevice = (oldState != nullptr) && |
| 2095 | (tempTouchState.deviceId != entry.deviceId || tempTouchState.source != entry.source); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2096 | |
| 2097 | const bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE || |
| 2098 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2099 | maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); |
| 2100 | const bool newGesture = (maskedAction == AMOTION_EVENT_ACTION_DOWN || |
| 2101 | maskedAction == AMOTION_EVENT_ACTION_SCROLL || isHoverAction); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 2102 | const bool isFromMouse = isFromSource(entry.source, AINPUT_SOURCE_MOUSE); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2103 | if (newGesture) { |
| 2104 | bool down = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Siarhei Vishniakou | 3ad385b | 2022-11-04 10:09:53 -0700 | [diff] [blame] | 2105 | if (switchedDevice && tempTouchState.isDown() && !down && !isHoverAction) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2106 | ALOGI("Dropping event because a pointer for a different device is already down " |
| 2107 | "in display %" PRId32, |
| 2108 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2109 | // TODO: test multiple simultaneous input streams. |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2110 | outInjectionResult = InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | f0ab2c8 | 2022-10-25 18:15:28 -0700 | [diff] [blame] | 2111 | return touchedWindows; // wrong device |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2112 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2113 | tempTouchState.reset(); |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2114 | tempTouchState.deviceId = entry.deviceId; |
| 2115 | tempTouchState.source = entry.source; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2116 | isSplit = false; |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2117 | } else if (switchedDevice && maskedAction == AMOTION_EVENT_ACTION_MOVE) { |
Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2118 | ALOGI("Dropping move event because a pointer for a different device is already active " |
| 2119 | "in display %" PRId32, |
| 2120 | displayId); |
Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2121 | // TODO: test multiple simultaneous input streams. |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2122 | outInjectionResult = InputEventInjectionResult::FAILED; |
Siarhei Vishniakou | f0ab2c8 | 2022-10-25 18:15:28 -0700 | [diff] [blame] | 2123 | return touchedWindows; // wrong device |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2124 | } |
| 2125 | |
| 2126 | if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { |
| 2127 | /* 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] | 2128 | const auto [x, y] = resolveTouchedPosition(entry); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2129 | const int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2130 | const bool isDown = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2131 | const bool isStylus = isPointerFromStylus(entry, pointerIndex); |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 2132 | newTouchedWindowHandle = findTouchedWindowAtLocked(displayId, x, y, &tempTouchState, |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2133 | isStylus, isDown /*addOutsideTargets*/); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2134 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2135 | // Handle the case where we did not find a window. |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2136 | if (newTouchedWindowHandle == nullptr) { |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 2137 | ALOGD("No new touched window at (%" PRId32 ", %" PRId32 ") in display %" PRId32, x, y, |
| 2138 | displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2139 | // 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] | 2140 | newTouchedWindowHandle = tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2141 | } |
| 2142 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2143 | // Verify targeted injection. |
| 2144 | if (const auto err = verifyTargetedInjection(newTouchedWindowHandle, entry); err) { |
| 2145 | ALOGW("Dropping injected touch event: %s", (*err).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2146 | outInjectionResult = os::InputEventInjectionResult::TARGET_MISMATCH; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2147 | newTouchedWindowHandle = nullptr; |
| 2148 | goto Failed; |
| 2149 | } |
| 2150 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2151 | // Figure out whether splitting will be allowed for this window. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2152 | if (newTouchedWindowHandle != nullptr) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2153 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
| 2154 | // New window supports splitting, but we should never split mouse events. |
| 2155 | isSplit = !isFromMouse; |
| 2156 | } else if (isSplit) { |
| 2157 | // New window does not support splitting but we have already split events. |
| 2158 | // Ignore the new window. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2159 | newTouchedWindowHandle = nullptr; |
| 2160 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2161 | } else { |
| 2162 | // 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] | 2163 | // be delivered to a new window which supports split touch. Pointers from a mouse device |
| 2164 | // should never be split. |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 2165 | isSplit = !isFromMouse; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2166 | } |
| 2167 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2168 | // Update hover state. |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2169 | if (newTouchedWindowHandle != nullptr) { |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2170 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT) { |
| 2171 | newHoverWindowHandle = nullptr; |
| 2172 | } else if (isHoverAction) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2173 | newHoverWindowHandle = newTouchedWindowHandle; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2174 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2175 | } |
| 2176 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2177 | std::vector<sp<WindowInfoHandle>> newTouchedWindows = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2178 | findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2179 | if (newTouchedWindowHandle != nullptr) { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2180 | // Process the foreground window first so that it is the first to receive the event. |
| 2181 | newTouchedWindows.insert(newTouchedWindows.begin(), newTouchedWindowHandle); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2182 | } |
| 2183 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2184 | if (newTouchedWindows.empty()) { |
| 2185 | ALOGI("Dropping event because there is no touchable window at (%d, %d) on display %d.", |
| 2186 | x, y, displayId); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2187 | outInjectionResult = InputEventInjectionResult::FAILED; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2188 | goto Failed; |
| 2189 | } |
| 2190 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2191 | for (const sp<WindowInfoHandle>& windowHandle : newTouchedWindows) { |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 2192 | if (!canWindowReceiveMotionLocked(windowHandle, entry)) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2193 | continue; |
| 2194 | } |
| 2195 | |
| 2196 | // Set target flags. |
| 2197 | int32_t targetFlags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 2198 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2199 | if (canReceiveForegroundTouches(*windowHandle->getInfo())) { |
| 2200 | // There should only be one touched window that can be "foreground" for the pointer. |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2201 | targetFlags |= InputTarget::FLAG_FOREGROUND; |
| 2202 | } |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2203 | |
| 2204 | if (isSplit) { |
| 2205 | targetFlags |= InputTarget::FLAG_SPLIT; |
| 2206 | } |
| 2207 | if (isWindowObscuredAtPointLocked(windowHandle, x, y)) { |
| 2208 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
| 2209 | } else if (isWindowObscuredLocked(windowHandle)) { |
| 2210 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 2211 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2212 | |
| 2213 | // Update the temporary touch state. |
| 2214 | BitSet32 pointerIds; |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 2215 | pointerIds.markBit(entry.pointerProperties[pointerIndex].id); |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2216 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2217 | tempTouchState.addOrUpdateWindow(windowHandle, targetFlags, pointerIds, |
| 2218 | entry.eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2219 | } |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 2220 | |
| 2221 | // If any existing window is pilfering pointers from newly added window, remove it |
| 2222 | BitSet32 canceledPointers = BitSet32(0); |
| 2223 | for (const TouchedWindow& window : tempTouchState.windows) { |
| 2224 | if (window.isPilferingPointers) { |
| 2225 | canceledPointers |= window.pointerIds; |
| 2226 | } |
| 2227 | } |
| 2228 | tempTouchState.cancelPointersForNonPilferingWindows(canceledPointers); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2229 | } else { |
| 2230 | /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ |
| 2231 | |
| 2232 | // If the pointer is not currently down, then ignore the event. |
Siarhei Vishniakou | 3ad385b | 2022-11-04 10:09:53 -0700 | [diff] [blame] | 2233 | if (!tempTouchState.isDown()) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2234 | if (DEBUG_FOCUS) { |
| 2235 | ALOGD("Dropping event because the pointer is not down or we previously " |
| 2236 | "dropped the pointer down event in display %" PRId32, |
| 2237 | displayId); |
| 2238 | } |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2239 | outInjectionResult = InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2240 | goto Failed; |
| 2241 | } |
| 2242 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2243 | addDragEventLocked(entry); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2244 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2245 | // Check whether touches should slip outside of the current foreground window. |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2246 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.pointerCount == 1 && |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2247 | tempTouchState.isSlippery()) { |
Siarhei Vishniakou | 9306c38 | 2022-09-30 15:30:31 -0700 | [diff] [blame] | 2248 | const auto [x, y] = resolveTouchedPosition(entry); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2249 | const bool isStylus = isPointerFromStylus(entry, 0 /*pointerIndex*/); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2250 | sp<WindowInfoHandle> oldTouchedWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2251 | tempTouchState.getFirstForegroundWindowHandle(); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2252 | newTouchedWindowHandle = |
| 2253 | findTouchedWindowAtLocked(displayId, x, y, &tempTouchState, isStylus); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2254 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2255 | // Verify targeted injection. |
| 2256 | if (const auto err = verifyTargetedInjection(newTouchedWindowHandle, entry); err) { |
| 2257 | ALOGW("Dropping injected event: %s", (*err).c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2258 | outInjectionResult = os::InputEventInjectionResult::TARGET_MISMATCH; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2259 | newTouchedWindowHandle = nullptr; |
| 2260 | goto Failed; |
| 2261 | } |
| 2262 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2263 | // Drop touch events if requested by input feature |
| 2264 | if (newTouchedWindowHandle != nullptr && |
| 2265 | shouldDropInput(entry, newTouchedWindowHandle)) { |
| 2266 | newTouchedWindowHandle = nullptr; |
| 2267 | } |
| 2268 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2269 | if (oldTouchedWindowHandle != newTouchedWindowHandle && |
| 2270 | oldTouchedWindowHandle != nullptr && newTouchedWindowHandle != nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2271 | if (DEBUG_FOCUS) { |
| 2272 | ALOGD("Touch is slipping out of window %s into window %s in display %" PRId32, |
| 2273 | oldTouchedWindowHandle->getName().c_str(), |
| 2274 | newTouchedWindowHandle->getName().c_str(), displayId); |
| 2275 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2276 | // Make a slippery exit from the old window. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2277 | tempTouchState.addOrUpdateWindow(oldTouchedWindowHandle, |
| 2278 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT, |
| 2279 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2280 | |
| 2281 | // Make a slippery entrance into the new window. |
| 2282 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
Prabir Pradhan | 713bb3e | 2021-12-20 02:07:40 -0800 | [diff] [blame] | 2283 | isSplit = !isFromMouse; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2284 | } |
| 2285 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2286 | int32_t targetFlags = InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER; |
| 2287 | if (canReceiveForegroundTouches(*newTouchedWindowHandle->getInfo())) { |
| 2288 | targetFlags |= InputTarget::FLAG_FOREGROUND; |
| 2289 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2290 | if (isSplit) { |
| 2291 | targetFlags |= InputTarget::FLAG_SPLIT; |
| 2292 | } |
| 2293 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
| 2294 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2295 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { |
| 2296 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2297 | } |
| 2298 | |
| 2299 | BitSet32 pointerIds; |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 2300 | pointerIds.markBit(entry.pointerProperties[0].id); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2301 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds, |
| 2302 | entry.eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2303 | } |
| 2304 | } |
| 2305 | } |
| 2306 | |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2307 | // Update dispatching for hover enter and exit. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2308 | if (newHoverWindowHandle != mLastHoverWindowHandle) { |
Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2309 | // Let the previous window know that the hover sequence is over, unless we already did |
| 2310 | // it when dispatching it as is to newTouchedWindowHandle. |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2311 | if (mLastHoverWindowHandle != nullptr && |
| 2312 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT || |
| 2313 | mLastHoverWindowHandle != newTouchedWindowHandle)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2314 | if (DEBUG_HOVER) { |
| 2315 | ALOGD("Sending hover exit event to window %s.", |
| 2316 | mLastHoverWindowHandle->getName().c_str()); |
| 2317 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2318 | tempTouchState.addOrUpdateWindow(mLastHoverWindowHandle, |
| 2319 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT, BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2320 | } |
| 2321 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2322 | // Let the new window know that the hover sequence is starting, unless we already did it |
| 2323 | // when dispatching it as is to newTouchedWindowHandle. |
| 2324 | if (newHoverWindowHandle != nullptr && |
| 2325 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2326 | newHoverWindowHandle != newTouchedWindowHandle)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2327 | if (DEBUG_HOVER) { |
| 2328 | ALOGD("Sending hover enter event to window %s.", |
| 2329 | newHoverWindowHandle->getName().c_str()); |
| 2330 | } |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2331 | tempTouchState.addOrUpdateWindow(newHoverWindowHandle, |
| 2332 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER, |
| 2333 | BitSet32(0)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2334 | } |
| 2335 | } |
| 2336 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2337 | // Ensure that we have at least one foreground window or at least one window that cannot be a |
| 2338 | // foreground target. If we only have windows that are not receiving foreground touches (e.g. we |
| 2339 | // only have windows getting ACTION_OUTSIDE), then drop the event, because there is no window |
| 2340 | // that is actually receiving the entire gesture. |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2341 | if (std::none_of(tempTouchState.windows.begin(), tempTouchState.windows.end(), |
| 2342 | [](const TouchedWindow& touchedWindow) { |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2343 | return !canReceiveForegroundTouches( |
| 2344 | *touchedWindow.windowHandle->getInfo()) || |
| 2345 | (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) != 0; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2346 | })) { |
Siarhei Vishniakou | 1fb1891 | 2022-03-08 10:31:39 -0800 | [diff] [blame] | 2347 | ALOGI("Dropping event because there is no touched window on display %d to receive it: %s", |
| 2348 | displayId, entry.getDescription().c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2349 | outInjectionResult = InputEventInjectionResult::FAILED; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2350 | goto Failed; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2351 | } |
| 2352 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2353 | // Ensure that all touched windows are valid for injection. |
| 2354 | if (entry.injectionState != nullptr) { |
| 2355 | std::string errs; |
| 2356 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
| 2357 | if (touchedWindow.targetFlags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 2358 | // Allow ACTION_OUTSIDE events generated by targeted injection to be |
| 2359 | // dispatched to any uid, since the coords will be zeroed out later. |
| 2360 | continue; |
| 2361 | } |
| 2362 | const auto err = verifyTargetedInjection(touchedWindow.windowHandle, entry); |
| 2363 | if (err) errs += "\n - " + *err; |
| 2364 | } |
| 2365 | if (!errs.empty()) { |
| 2366 | ALOGW("Dropping targeted injection: At least one touched window is not owned by uid " |
| 2367 | "%d:%s", |
| 2368 | *entry.injectionState->targetUid, errs.c_str()); |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2369 | outInjectionResult = InputEventInjectionResult::TARGET_MISMATCH; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2370 | goto Failed; |
| 2371 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2372 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2373 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2374 | // Check whether windows listening for outside touches are owned by the same UID. If it is |
| 2375 | // set the policy flag that we will not reveal coordinate information to this window. |
| 2376 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2377 | sp<WindowInfoHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2378 | tempTouchState.getFirstForegroundWindowHandle(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2379 | if (foregroundWindowHandle) { |
| 2380 | const int32_t foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2381 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2382 | if (touchedWindow.targetFlags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2383 | sp<WindowInfoHandle> windowInfoHandle = touchedWindow.windowHandle; |
| 2384 | if (windowInfoHandle->getInfo()->ownerUid != foregroundWindowUid) { |
| 2385 | tempTouchState.addOrUpdateWindow(windowInfoHandle, |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2386 | InputTarget::FLAG_ZERO_COORDS, |
| 2387 | BitSet32(0)); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2388 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2389 | } |
| 2390 | } |
| 2391 | } |
| 2392 | } |
| 2393 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2394 | // If this is the first pointer going down and the touched window has a wallpaper |
| 2395 | // then also add the touched wallpaper windows so they are locked in for the duration |
| 2396 | // of the touch gesture. |
| 2397 | // We do not collect wallpapers during HOVER_MOVE or SCROLL because the wallpaper |
| 2398 | // engine only supports touch events. We would need to add a mechanism similar |
| 2399 | // to View.onGenericMotionEvent to enable wallpapers to handle these events. |
| 2400 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2401 | sp<WindowInfoHandle> foregroundWindowHandle = |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2402 | tempTouchState.getFirstForegroundWindowHandle(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2403 | if (foregroundWindowHandle && |
| 2404 | foregroundWindowHandle->getInfo()->inputConfig.test( |
| 2405 | WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER)) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2406 | const std::vector<sp<WindowInfoHandle>>& windowHandles = |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2407 | getWindowHandlesLocked(displayId); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2408 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
| 2409 | const WindowInfo* info = windowHandle->getInfo(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2410 | if (info->displayId == displayId && |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2411 | windowHandle->getInfo()->inputConfig.test( |
| 2412 | WindowInfo::InputConfig::IS_WALLPAPER)) { |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2413 | tempTouchState |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2414 | .addOrUpdateWindow(windowHandle, |
| 2415 | InputTarget::FLAG_WINDOW_IS_OBSCURED | |
| 2416 | InputTarget:: |
| 2417 | FLAG_WINDOW_IS_PARTIALLY_OBSCURED | |
| 2418 | InputTarget::FLAG_DISPATCH_AS_IS, |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2419 | BitSet32(0), entry.eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2420 | } |
| 2421 | } |
| 2422 | } |
| 2423 | } |
| 2424 | |
| 2425 | // Success! Output targets. |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2426 | touchedWindows = tempTouchState.windows; |
| 2427 | outInjectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2428 | |
| 2429 | // Drop the outside or hover touch windows since we will not care about them |
| 2430 | // in the next iteration. |
Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2431 | tempTouchState.filterNonAsIsTouchWindows(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2432 | |
| 2433 | Failed: |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2434 | // Update final pieces of touch state if the injector had permission. |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2435 | if (switchedDevice) { |
| 2436 | if (DEBUG_FOCUS) { |
| 2437 | ALOGD("Conflicting pointer actions: Switched to a different device."); |
| 2438 | } |
| 2439 | *outConflictingPointerActions = true; |
| 2440 | } |
| 2441 | |
| 2442 | if (isHoverAction) { |
| 2443 | // Started hovering, therefore no longer down. |
Siarhei Vishniakou | 3ad385b | 2022-11-04 10:09:53 -0700 | [diff] [blame] | 2444 | if (oldState && oldState->isDown()) { |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2445 | if (DEBUG_FOCUS) { |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2446 | ALOGD("Conflicting pointer actions: Hover received while pointer was " |
| 2447 | "down."); |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2448 | } |
| 2449 | *outConflictingPointerActions = true; |
| 2450 | } |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2451 | tempTouchState.reset(); |
| 2452 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 2453 | maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) { |
| 2454 | tempTouchState.deviceId = entry.deviceId; |
| 2455 | tempTouchState.source = entry.source; |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2456 | } |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2457 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP || |
| 2458 | maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
| 2459 | // All pointers up or canceled. |
| 2460 | tempTouchState.reset(); |
| 2461 | } else if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 2462 | // First pointer went down. |
Siarhei Vishniakou | 3ad385b | 2022-11-04 10:09:53 -0700 | [diff] [blame] | 2463 | if (oldState && oldState->isDown()) { |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2464 | if (DEBUG_FOCUS) { |
| 2465 | ALOGD("Conflicting pointer actions: Down received while already down."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2466 | } |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2467 | *outConflictingPointerActions = true; |
Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2468 | } |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2469 | } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 2470 | // One pointer went up. |
| 2471 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
| 2472 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2473 | |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2474 | for (size_t i = 0; i < tempTouchState.windows.size();) { |
| 2475 | TouchedWindow& touchedWindow = tempTouchState.windows[i]; |
| 2476 | touchedWindow.pointerIds.clearBit(pointerId); |
| 2477 | if (touchedWindow.pointerIds.isEmpty()) { |
| 2478 | tempTouchState.windows.erase(tempTouchState.windows.begin() + i); |
| 2479 | continue; |
| 2480 | } |
| 2481 | i += 1; |
| 2482 | } |
| 2483 | } else if (!isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN) { |
| 2484 | // If no split, we suppose all touched windows should receive pointer down. |
| 2485 | const int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
| 2486 | for (size_t i = 0; i < tempTouchState.windows.size(); i++) { |
| 2487 | TouchedWindow& touchedWindow = tempTouchState.windows[i]; |
| 2488 | // Ignore drag window for it should just track one pointer. |
| 2489 | if (mDragState && mDragState->dragWindow == touchedWindow.windowHandle) { |
| 2490 | continue; |
| 2491 | } |
| 2492 | touchedWindow.pointerIds.markBit(entry.pointerProperties[pointerIndex].id); |
| 2493 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2494 | } |
| 2495 | |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2496 | // Save changes unless the action was scroll in which case the temporary touch |
| 2497 | // state was only valid for this one action. |
| 2498 | if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) { |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 2499 | if (displayId >= 0) { |
Siarhei Vishniakou | 79c3266 | 2022-10-25 17:56:25 -0700 | [diff] [blame] | 2500 | mTouchStatesByDisplay[displayId] = tempTouchState; |
| 2501 | } else { |
| 2502 | mTouchStatesByDisplay.erase(displayId); |
| 2503 | } |
| 2504 | } |
| 2505 | |
| 2506 | // Update hover state. |
| 2507 | mLastHoverWindowHandle = newHoverWindowHandle; |
| 2508 | |
Siarhei Vishniakou | 6278ca2 | 2022-10-25 11:19:19 -0700 | [diff] [blame] | 2509 | return touchedWindows; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2510 | } |
| 2511 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2512 | void InputDispatcher::finishDragAndDrop(int32_t displayId, float x, float y) { |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2513 | // Prevent stylus interceptor windows from affecting drag and drop behavior for now, until we |
| 2514 | // have an explicit reason to support it. |
| 2515 | constexpr bool isStylus = false; |
| 2516 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2517 | const sp<WindowInfoHandle> dropWindow = |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2518 | findTouchedWindowAtLocked(displayId, x, y, nullptr /*touchState*/, isStylus, |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 2519 | false /*addOutsideTargets*/, true /*ignoreDragWindow*/); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2520 | if (dropWindow) { |
| 2521 | vec2 local = dropWindow->getInfo()->transform.transform(x, y); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2522 | sendDropWindowCommandLocked(dropWindow->getToken(), local.x, local.y); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 2523 | } else { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2524 | ALOGW("No window found when drop."); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2525 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2526 | } |
| 2527 | mDragState.reset(); |
| 2528 | } |
| 2529 | |
| 2530 | void InputDispatcher::addDragEventLocked(const MotionEntry& entry) { |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 2531 | if (!mDragState || mDragState->dragWindow->getInfo()->displayId != entry.displayId) { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2532 | return; |
| 2533 | } |
| 2534 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2535 | if (!mDragState->isStartDrag) { |
| 2536 | mDragState->isStartDrag = true; |
| 2537 | mDragState->isStylusButtonDownAtStart = |
| 2538 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2539 | } |
| 2540 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2541 | // Find the pointer index by id. |
| 2542 | int32_t pointerIndex = 0; |
| 2543 | for (; static_cast<uint32_t>(pointerIndex) < entry.pointerCount; pointerIndex++) { |
| 2544 | const PointerProperties& pointerProperties = entry.pointerProperties[pointerIndex]; |
| 2545 | if (pointerProperties.id == mDragState->pointerId) { |
| 2546 | break; |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2547 | } |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2548 | } |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2549 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2550 | if (uint32_t(pointerIndex) == entry.pointerCount) { |
| 2551 | 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] | 2552 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2553 | mDragState.reset(); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2554 | return; |
| 2555 | } |
| 2556 | |
| 2557 | const int32_t maskedAction = entry.action & AMOTION_EVENT_ACTION_MASK; |
| 2558 | const int32_t x = entry.pointerCoords[pointerIndex].getX(); |
| 2559 | const int32_t y = entry.pointerCoords[pointerIndex].getY(); |
| 2560 | |
| 2561 | switch (maskedAction) { |
| 2562 | case AMOTION_EVENT_ACTION_MOVE: { |
| 2563 | // Handle the special case : stylus button no longer pressed. |
| 2564 | bool isStylusButtonDown = |
| 2565 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; |
| 2566 | if (mDragState->isStylusButtonDownAtStart && !isStylusButtonDown) { |
| 2567 | finishDragAndDrop(entry.displayId, x, y); |
| 2568 | return; |
| 2569 | } |
| 2570 | |
| 2571 | // Prevent stylus interceptor windows from affecting drag and drop behavior for now, |
| 2572 | // until we have an explicit reason to support it. |
| 2573 | constexpr bool isStylus = false; |
| 2574 | |
| 2575 | const sp<WindowInfoHandle> hoverWindowHandle = |
| 2576 | findTouchedWindowAtLocked(entry.displayId, x, y, nullptr /*touchState*/, |
| 2577 | isStylus, false /*addOutsideTargets*/, |
| 2578 | true /*ignoreDragWindow*/); |
| 2579 | // enqueue drag exit if needed. |
| 2580 | if (hoverWindowHandle != mDragState->dragHoverWindowHandle && |
| 2581 | !haveSameToken(hoverWindowHandle, mDragState->dragHoverWindowHandle)) { |
| 2582 | if (mDragState->dragHoverWindowHandle != nullptr) { |
| 2583 | enqueueDragEventLocked(mDragState->dragHoverWindowHandle, true /*isExiting*/, x, |
| 2584 | y); |
| 2585 | } |
| 2586 | mDragState->dragHoverWindowHandle = hoverWindowHandle; |
| 2587 | } |
| 2588 | // enqueue drag location if needed. |
| 2589 | if (hoverWindowHandle != nullptr) { |
| 2590 | enqueueDragEventLocked(hoverWindowHandle, false /*isExiting*/, x, y); |
| 2591 | } |
| 2592 | break; |
| 2593 | } |
| 2594 | |
| 2595 | case AMOTION_EVENT_ACTION_POINTER_UP: |
| 2596 | if (getMotionEventActionPointerIndex(entry.action) != pointerIndex) { |
| 2597 | break; |
| 2598 | } |
| 2599 | // The drag pointer is up. |
| 2600 | [[fallthrough]]; |
| 2601 | case AMOTION_EVENT_ACTION_UP: |
| 2602 | finishDragAndDrop(entry.displayId, x, y); |
| 2603 | break; |
| 2604 | case AMOTION_EVENT_ACTION_CANCEL: { |
| 2605 | ALOGD("Receiving cancel when drag and drop."); |
| 2606 | sendDropWindowCommandLocked(nullptr, 0, 0); |
| 2607 | mDragState.reset(); |
| 2608 | break; |
| 2609 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2610 | } |
| 2611 | } |
| 2612 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2613 | void InputDispatcher::addWindowTargetLocked(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2614 | int32_t targetFlags, BitSet32 pointerIds, |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2615 | std::optional<nsecs_t> firstDownTimeInTarget, |
Siarhei Vishniakou | f75cddb | 2022-10-25 10:42:16 -0700 | [diff] [blame] | 2616 | std::vector<InputTarget>& inputTargets) const { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2617 | std::vector<InputTarget>::iterator it = |
| 2618 | std::find_if(inputTargets.begin(), inputTargets.end(), |
| 2619 | [&windowHandle](const InputTarget& inputTarget) { |
| 2620 | return inputTarget.inputChannel->getConnectionToken() == |
| 2621 | windowHandle->getToken(); |
| 2622 | }); |
Chavi Weingarten | 97b8eec | 2020-01-09 18:09:08 +0000 | [diff] [blame] | 2623 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2624 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2625 | |
| 2626 | if (it == inputTargets.end()) { |
| 2627 | InputTarget inputTarget; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 2628 | std::shared_ptr<InputChannel> inputChannel = |
| 2629 | getInputChannelLocked(windowHandle->getToken()); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2630 | if (inputChannel == nullptr) { |
| 2631 | ALOGW("Window %s already unregistered input channel", windowHandle->getName().c_str()); |
| 2632 | return; |
| 2633 | } |
| 2634 | inputTarget.inputChannel = inputChannel; |
| 2635 | inputTarget.flags = targetFlags; |
| 2636 | inputTarget.globalScaleFactor = windowInfo->globalScaleFactor; |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2637 | inputTarget.firstDownTimeInTarget = firstDownTimeInTarget; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2638 | const auto& displayInfoIt = mDisplayInfos.find(windowInfo->displayId); |
| 2639 | if (displayInfoIt != mDisplayInfos.end()) { |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 2640 | inputTarget.displayTransform = displayInfoIt->second.transform; |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2641 | } else { |
Prabir Pradhan | 8b89c2f | 2021-07-29 16:30:14 +0000 | [diff] [blame] | 2642 | ALOGE("DisplayInfo not found for window on display: %d", windowInfo->displayId); |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2643 | } |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2644 | inputTargets.push_back(inputTarget); |
| 2645 | it = inputTargets.end() - 1; |
| 2646 | } |
| 2647 | |
| 2648 | ALOG_ASSERT(it->flags == targetFlags); |
| 2649 | ALOG_ASSERT(it->globalScaleFactor == windowInfo->globalScaleFactor); |
| 2650 | |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2651 | it->addPointers(pointerIds, windowInfo->transform); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2652 | } |
| 2653 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2654 | void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, |
Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2655 | int32_t displayId) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2656 | auto monitorsIt = mGlobalMonitorsByDisplay.find(displayId); |
| 2657 | if (monitorsIt == mGlobalMonitorsByDisplay.end()) return; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2658 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2659 | for (const Monitor& monitor : selectResponsiveMonitorsLocked(monitorsIt->second)) { |
| 2660 | InputTarget target; |
| 2661 | target.inputChannel = monitor.inputChannel; |
| 2662 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2663 | // target.firstDownTimeInTarget is not set for global monitors. It is only required in split |
| 2664 | // touch and global monitoring works as intended even without setting firstDownTimeInTarget |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2665 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
| 2666 | target.displayTransform = it->second.transform; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2667 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2668 | target.setDefaultPointerTransform(target.displayTransform); |
| 2669 | inputTargets.push_back(target); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2670 | } |
| 2671 | } |
| 2672 | |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2673 | /** |
| 2674 | * Indicate whether one window handle should be considered as obscuring |
| 2675 | * another window handle. We only check a few preconditions. Actually |
| 2676 | * checking the bounds is left to the caller. |
| 2677 | */ |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2678 | static bool canBeObscuredBy(const sp<WindowInfoHandle>& windowHandle, |
| 2679 | const sp<WindowInfoHandle>& otherHandle) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2680 | // Compare by token so cloned layers aren't counted |
| 2681 | if (haveSameToken(windowHandle, otherHandle)) { |
| 2682 | return false; |
| 2683 | } |
| 2684 | auto info = windowHandle->getInfo(); |
| 2685 | auto otherInfo = otherHandle->getInfo(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2686 | if (otherInfo->inputConfig.test(WindowInfo::InputConfig::NOT_VISIBLE)) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2687 | return false; |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2688 | } else if (otherInfo->alpha == 0 && |
| 2689 | otherInfo->inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE)) { |
Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame] | 2690 | // Those act as if they were invisible, so we don't need to flag them. |
| 2691 | // We do want to potentially flag touchable windows even if they have 0 |
| 2692 | // opacity, since they can consume touches and alter the effects of the |
| 2693 | // user interaction (eg. apps that rely on |
| 2694 | // FLAG_WINDOW_IS_PARTIALLY_OBSCURED should still be told about those |
| 2695 | // windows), hence we also check for FLAG_NOT_TOUCHABLE. |
| 2696 | return false; |
Bernardo Rufino | 8007daf | 2020-09-22 09:40:01 +0000 | [diff] [blame] | 2697 | } else if (info->ownerUid == otherInfo->ownerUid) { |
| 2698 | // If ownerUid is the same we don't generate occlusion events as there |
| 2699 | // is no security boundary within an uid. |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2700 | return false; |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2701 | } else if (otherInfo->inputConfig.test(gui::WindowInfo::InputConfig::TRUSTED_OVERLAY)) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2702 | return false; |
| 2703 | } else if (otherInfo->displayId != info->displayId) { |
| 2704 | return false; |
| 2705 | } |
| 2706 | return true; |
| 2707 | } |
| 2708 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2709 | /** |
| 2710 | * Returns touch occlusion information in the form of TouchOcclusionInfo. To check if the touch is |
| 2711 | * untrusted, one should check: |
| 2712 | * |
| 2713 | * 1. If result.hasBlockingOcclusion is true. |
| 2714 | * If it's, it means the touch should be blocked due to a window with occlusion mode of |
| 2715 | * BLOCK_UNTRUSTED. |
| 2716 | * |
| 2717 | * 2. If result.obscuringOpacity > mMaximumObscuringOpacityForTouch. |
| 2718 | * If it is (and 1 is false), then the touch should be blocked because a stack of windows |
| 2719 | * (possibly only one) with occlusion mode of USE_OPACITY from one UID resulted in a composed |
| 2720 | * obscuring opacity above the threshold. Note that if there was no window of occlusion mode |
| 2721 | * USE_OPACITY, result.obscuringOpacity would've been 0 and since |
| 2722 | * mMaximumObscuringOpacityForTouch >= 0, the condition above would never be true. |
| 2723 | * |
| 2724 | * If neither of those is true, then it means the touch can be allowed. |
| 2725 | */ |
| 2726 | InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2727 | const sp<WindowInfoHandle>& windowHandle, int32_t x, int32_t y) const { |
| 2728 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2729 | int32_t displayId = windowInfo->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2730 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2731 | TouchOcclusionInfo info; |
| 2732 | info.hasBlockingOcclusion = false; |
| 2733 | info.obscuringOpacity = 0; |
| 2734 | info.obscuringUid = -1; |
| 2735 | std::map<int32_t, float> opacityByUid; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2736 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2737 | if (windowHandle == otherHandle) { |
| 2738 | break; // All future windows are below us. Exit early. |
| 2739 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2740 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 2741 | if (canBeObscuredBy(windowHandle, otherHandle) && otherInfo->frameContainsPoint(x, y) && |
| 2742 | !haveSameApplicationToken(windowInfo, otherInfo)) { |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2743 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2744 | info.debugInfo.push_back( |
| 2745 | dumpWindowForTouchOcclusion(otherInfo, /* isTouchedWindow */ false)); |
| 2746 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2747 | // canBeObscuredBy() has returned true above, which means this window is untrusted, so |
| 2748 | // we perform the checks below to see if the touch can be propagated or not based on the |
| 2749 | // window's touch occlusion mode |
| 2750 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::BLOCK_UNTRUSTED) { |
| 2751 | info.hasBlockingOcclusion = true; |
| 2752 | info.obscuringUid = otherInfo->ownerUid; |
| 2753 | info.obscuringPackage = otherInfo->packageName; |
| 2754 | break; |
| 2755 | } |
| 2756 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::USE_OPACITY) { |
| 2757 | uint32_t uid = otherInfo->ownerUid; |
| 2758 | float opacity = |
| 2759 | (opacityByUid.find(uid) == opacityByUid.end()) ? 0 : opacityByUid[uid]; |
| 2760 | // Given windows A and B: |
| 2761 | // opacity(A, B) = 1 - [1 - opacity(A)] * [1 - opacity(B)] |
| 2762 | opacity = 1 - (1 - opacity) * (1 - otherInfo->alpha); |
| 2763 | opacityByUid[uid] = opacity; |
| 2764 | if (opacity > info.obscuringOpacity) { |
| 2765 | info.obscuringOpacity = opacity; |
| 2766 | info.obscuringUid = uid; |
| 2767 | info.obscuringPackage = otherInfo->packageName; |
| 2768 | } |
| 2769 | } |
| 2770 | } |
| 2771 | } |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2772 | if (DEBUG_TOUCH_OCCLUSION) { |
| 2773 | info.debugInfo.push_back( |
| 2774 | dumpWindowForTouchOcclusion(windowInfo, /* isTouchedWindow */ true)); |
| 2775 | } |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2776 | return info; |
| 2777 | } |
| 2778 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2779 | std::string InputDispatcher::dumpWindowForTouchOcclusion(const WindowInfo* info, |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2780 | bool isTouchedWindow) const { |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 2781 | return StringPrintf(INDENT2 "* %spackage=%s/%" PRId32 ", id=%" PRId32 ", mode=%s, alpha=%.2f, " |
| 2782 | "frame=[%" PRId32 ",%" PRId32 "][%" PRId32 ",%" PRId32 |
| 2783 | "], touchableRegion=%s, window={%s}, inputConfig={%s}, " |
| 2784 | "hasToken=%s, applicationInfo.name=%s, applicationInfo.token=%s\n", |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2785 | isTouchedWindow ? "[TOUCHED] " : "", info->packageName.c_str(), |
| 2786 | info->ownerUid, info->id, toString(info->touchOcclusionMode).c_str(), |
| 2787 | info->alpha, info->frameLeft, info->frameTop, info->frameRight, |
| 2788 | info->frameBottom, dumpRegion(info->touchableRegion).c_str(), |
| 2789 | info->name.c_str(), info->inputConfig.string().c_str(), |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 2790 | toString(info->token != nullptr), info->applicationInfo.name.c_str(), |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 2791 | toString(info->applicationInfo.token).c_str()); |
Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2792 | } |
| 2793 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2794 | bool InputDispatcher::isTouchTrustedLocked(const TouchOcclusionInfo& occlusionInfo) const { |
| 2795 | if (occlusionInfo.hasBlockingOcclusion) { |
| 2796 | ALOGW("Untrusted touch due to occlusion by %s/%d", occlusionInfo.obscuringPackage.c_str(), |
| 2797 | occlusionInfo.obscuringUid); |
| 2798 | return false; |
| 2799 | } |
| 2800 | if (occlusionInfo.obscuringOpacity > mMaximumObscuringOpacityForTouch) { |
| 2801 | ALOGW("Untrusted touch due to occlusion by %s/%d (obscuring opacity = " |
| 2802 | "%.2f, maximum allowed = %.2f)", |
| 2803 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid, |
| 2804 | occlusionInfo.obscuringOpacity, mMaximumObscuringOpacityForTouch); |
| 2805 | return false; |
| 2806 | } |
| 2807 | return true; |
| 2808 | } |
| 2809 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2810 | bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<WindowInfoHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2811 | int32_t x, int32_t y) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2812 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2813 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 2814 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2815 | if (windowHandle == otherHandle) { |
| 2816 | break; // All future windows are below us. Exit early. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2817 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2818 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2819 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2820 | otherInfo->frameContainsPoint(x, y)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2821 | return true; |
| 2822 | } |
| 2823 | } |
| 2824 | return false; |
| 2825 | } |
| 2826 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2827 | bool InputDispatcher::isWindowObscuredLocked(const sp<WindowInfoHandle>& windowHandle) const { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2828 | int32_t displayId = windowHandle->getInfo()->displayId; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2829 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
| 2830 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
| 2831 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2832 | if (windowHandle == otherHandle) { |
| 2833 | break; // All future windows are below us. Exit early. |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2834 | } |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2835 | const WindowInfo* otherInfo = otherHandle->getInfo(); |
Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2836 | if (canBeObscuredBy(windowHandle, otherHandle) && |
mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2837 | otherInfo->overlaps(windowInfo)) { |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2838 | return true; |
| 2839 | } |
| 2840 | } |
| 2841 | return false; |
| 2842 | } |
| 2843 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2844 | std::string InputDispatcher::getApplicationWindowLabel( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2845 | const InputApplicationHandle* applicationHandle, const sp<WindowInfoHandle>& windowHandle) { |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2846 | if (applicationHandle != nullptr) { |
| 2847 | if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2848 | return applicationHandle->getName() + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2849 | } else { |
| 2850 | return applicationHandle->getName(); |
| 2851 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2852 | } else if (windowHandle != nullptr) { |
Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2853 | return windowHandle->getInfo()->applicationInfo.name + " - " + windowHandle->getName(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2854 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2855 | return "<unknown application or window>"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2856 | } |
| 2857 | } |
| 2858 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2859 | void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 2860 | if (!isUserActivityEvent(eventEntry)) { |
| 2861 | // 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] | 2862 | return; |
| 2863 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2864 | int32_t displayId = getTargetDisplayId(eventEntry); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2865 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2866 | if (focusedWindowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2867 | const WindowInfo* info = focusedWindowHandle->getInfo(); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 2868 | if (info->inputConfig.test(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2869 | if (DEBUG_DISPATCH_CYCLE) { |
| 2870 | ALOGD("Not poking user activity: disabled by window '%s'.", info->name.c_str()); |
| 2871 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2872 | return; |
| 2873 | } |
| 2874 | } |
| 2875 | |
| 2876 | int32_t eventType = USER_ACTIVITY_EVENT_OTHER; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2877 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2878 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2879 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 2880 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2881 | return; |
| 2882 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2883 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2884 | if (MotionEvent::isTouchEvent(motionEntry.source, motionEntry.action)) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2885 | eventType = USER_ACTIVITY_EVENT_TOUCH; |
| 2886 | } |
| 2887 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2888 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2889 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2890 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 2891 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2892 | return; |
| 2893 | } |
| 2894 | eventType = USER_ACTIVITY_EVENT_BUTTON; |
| 2895 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2896 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 2897 | default: { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2898 | LOG_ALWAYS_FATAL("%s events are not user activity", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2899 | ftl::enum_string(eventEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2900 | break; |
| 2901 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2902 | } |
| 2903 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2904 | auto command = [this, eventTime = eventEntry.eventTime, eventType, displayId]() |
| 2905 | REQUIRES(mLock) { |
| 2906 | scoped_unlock unlock(mLock); |
| 2907 | mPolicy->pokeUserActivity(eventTime, eventType, displayId); |
| 2908 | }; |
| 2909 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2910 | } |
| 2911 | |
| 2912 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2913 | const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2914 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2915 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2916 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2917 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2918 | StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2919 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2920 | ATRACE_NAME(message.c_str()); |
| 2921 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2922 | if (DEBUG_DISPATCH_CYCLE) { |
| 2923 | ALOGD("channel '%s' ~ prepareDispatchCycle - flags=0x%08x, " |
| 2924 | "globalScaleFactor=%f, pointerIds=0x%x %s", |
| 2925 | connection->getInputChannelName().c_str(), inputTarget.flags, |
| 2926 | inputTarget.globalScaleFactor, inputTarget.pointerIds.value, |
| 2927 | inputTarget.getPointerInfoString().c_str()); |
| 2928 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2929 | |
| 2930 | // Skip this event if the connection status is not normal. |
| 2931 | // 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] | 2932 | if (connection->status != Connection::Status::NORMAL) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2933 | if (DEBUG_DISPATCH_CYCLE) { |
| 2934 | ALOGD("channel '%s' ~ Dropping event because the channel status is %s", |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 2935 | connection->getInputChannelName().c_str(), |
| 2936 | ftl::enum_string(connection->status).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2937 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2938 | return; |
| 2939 | } |
| 2940 | |
| 2941 | // Split a motion event if needed. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2942 | if (inputTarget.flags & InputTarget::FLAG_SPLIT) { |
| 2943 | LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION, |
| 2944 | "Entry type %s should not have FLAG_SPLIT", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2945 | ftl::enum_string(eventEntry->type).c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2946 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2947 | const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry); |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2948 | if (inputTarget.pointerIds.count() != originalMotionEntry.pointerCount) { |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2949 | LOG_ALWAYS_FATAL_IF(!inputTarget.firstDownTimeInTarget.has_value(), |
| 2950 | "Splitting motion events requires a down time to be set for the " |
| 2951 | "target"); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2952 | std::unique_ptr<MotionEntry> splitMotionEntry = |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2953 | splitMotionEvent(originalMotionEntry, inputTarget.pointerIds, |
| 2954 | inputTarget.firstDownTimeInTarget.value()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2955 | if (!splitMotionEntry) { |
| 2956 | return; // split event was dropped |
| 2957 | } |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 2958 | if (splitMotionEntry->action == AMOTION_EVENT_ACTION_CANCEL) { |
| 2959 | std::string reason = std::string("reason=pointer cancel on split window"); |
| 2960 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 2961 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 2962 | } |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2963 | if (DEBUG_FOCUS) { |
| 2964 | ALOGD("channel '%s' ~ Split motion event.", |
| 2965 | connection->getInputChannelName().c_str()); |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2966 | logOutboundMotionDetails(" ", *splitMotionEntry); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2967 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2968 | enqueueDispatchEntriesLocked(currentTime, connection, std::move(splitMotionEntry), |
| 2969 | inputTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2970 | return; |
| 2971 | } |
| 2972 | } |
| 2973 | |
| 2974 | // Not splitting. Enqueue dispatch entries for the event as is. |
| 2975 | enqueueDispatchEntriesLocked(currentTime, connection, eventEntry, inputTarget); |
| 2976 | } |
| 2977 | |
| 2978 | void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2979 | const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2980 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2981 | const InputTarget& inputTarget) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2982 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2983 | std::string message = |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2984 | StringPrintf("enqueueDispatchEntriesLocked(inputChannel=%s, id=0x%" PRIx32 ")", |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2985 | connection->getInputChannelName().c_str(), eventEntry->id); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2986 | ATRACE_NAME(message.c_str()); |
| 2987 | } |
| 2988 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 2989 | const bool wasEmpty = connection->outboundQueue.empty(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2990 | |
| 2991 | // Enqueue dispatch entries for the requested modes. |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2992 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2993 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2994 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2995 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2996 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2997 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2998 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2999 | InputTarget::FLAG_DISPATCH_AS_IS); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3000 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3001 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3002 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3003 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3004 | |
| 3005 | // If the outbound queue was previously empty, start the dispatch cycle going. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3006 | if (wasEmpty && !connection->outboundQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3007 | startDispatchCycleLocked(currentTime, connection); |
| 3008 | } |
| 3009 | } |
| 3010 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3011 | void InputDispatcher::enqueueDispatchEntryLocked(const sp<Connection>& connection, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3012 | std::shared_ptr<EventEntry> eventEntry, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3013 | const InputTarget& inputTarget, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3014 | int32_t dispatchMode) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3015 | if (ATRACE_ENABLED()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3016 | std::string message = StringPrintf("enqueueDispatchEntry(inputChannel=%s, dispatchMode=%s)", |
| 3017 | connection->getInputChannelName().c_str(), |
| 3018 | dispatchModeToString(dispatchMode).c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3019 | ATRACE_NAME(message.c_str()); |
| 3020 | } |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3021 | int32_t inputTargetFlags = inputTarget.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3022 | if (!(inputTargetFlags & dispatchMode)) { |
| 3023 | return; |
| 3024 | } |
| 3025 | inputTargetFlags = (inputTargetFlags & ~InputTarget::FLAG_DISPATCH_MASK) | dispatchMode; |
| 3026 | |
| 3027 | // This is a new event. |
| 3028 | // Enqueue a new dispatch entry onto the outbound queue for this connection. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3029 | std::unique_ptr<DispatchEntry> dispatchEntry = |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3030 | createDispatchEntry(inputTarget, eventEntry, inputTargetFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3031 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3032 | // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a |
| 3033 | // different EventEntry than what was passed in. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3034 | EventEntry& newEntry = *(dispatchEntry->eventEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3035 | // Apply target flags and update the connection's input state. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3036 | switch (newEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3037 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3038 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3039 | dispatchEntry->resolvedEventId = keyEntry.id; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3040 | dispatchEntry->resolvedAction = keyEntry.action; |
| 3041 | dispatchEntry->resolvedFlags = keyEntry.flags; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3042 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3043 | if (!connection->inputState.trackKey(keyEntry, dispatchEntry->resolvedAction, |
| 3044 | dispatchEntry->resolvedFlags)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3045 | if (DEBUG_DISPATCH_CYCLE) { |
| 3046 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent key " |
| 3047 | "event", |
| 3048 | connection->getInputChannelName().c_str()); |
| 3049 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3050 | return; // skip the inconsistent event |
| 3051 | } |
| 3052 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3053 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3054 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3055 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3056 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(newEntry); |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3057 | // Assign a default value to dispatchEntry that will never be generated by InputReader, |
| 3058 | // and assign a InputDispatcher value if it doesn't change in the if-else chain below. |
| 3059 | constexpr int32_t DEFAULT_RESOLVED_EVENT_ID = |
| 3060 | static_cast<int32_t>(IdGenerator::Source::OTHER); |
| 3061 | dispatchEntry->resolvedEventId = DEFAULT_RESOLVED_EVENT_ID; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3062 | if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 3063 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; |
| 3064 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT) { |
| 3065 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; |
| 3066 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER) { |
| 3067 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 3068 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT) { |
| 3069 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_CANCEL; |
| 3070 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER) { |
| 3071 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_DOWN; |
| 3072 | } else { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3073 | dispatchEntry->resolvedAction = motionEntry.action; |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3074 | dispatchEntry->resolvedEventId = motionEntry.id; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3075 | } |
| 3076 | if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE && |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3077 | !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source, |
| 3078 | motionEntry.displayId)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3079 | if (DEBUG_DISPATCH_CYCLE) { |
| 3080 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: filling in missing hover " |
| 3081 | "enter event", |
| 3082 | connection->getInputChannelName().c_str()); |
| 3083 | } |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3084 | // We keep the 'resolvedEventId' here equal to the original 'motionEntry.id' because |
| 3085 | // this is a one-to-one event conversion. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3086 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 3087 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3088 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3089 | dispatchEntry->resolvedFlags = motionEntry.flags; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3090 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_OBSCURED) { |
| 3091 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; |
| 3092 | } |
| 3093 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED) { |
| 3094 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 3095 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3096 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3097 | if (!connection->inputState.trackMotion(motionEntry, dispatchEntry->resolvedAction, |
| 3098 | dispatchEntry->resolvedFlags)) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3099 | if (DEBUG_DISPATCH_CYCLE) { |
| 3100 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent motion " |
| 3101 | "event", |
| 3102 | connection->getInputChannelName().c_str()); |
| 3103 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3104 | return; // skip the inconsistent event |
| 3105 | } |
| 3106 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3107 | dispatchEntry->resolvedEventId = |
| 3108 | dispatchEntry->resolvedEventId == DEFAULT_RESOLVED_EVENT_ID |
| 3109 | ? mIdGenerator.nextId() |
| 3110 | : motionEntry.id; |
| 3111 | if (ATRACE_ENABLED() && dispatchEntry->resolvedEventId != motionEntry.id) { |
| 3112 | std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32 |
| 3113 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3114 | motionEntry.id, dispatchEntry->resolvedEventId); |
| 3115 | ATRACE_NAME(message.c_str()); |
| 3116 | } |
| 3117 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 3118 | if ((motionEntry.flags & AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) && |
| 3119 | (motionEntry.policyFlags & POLICY_FLAG_TRUSTED)) { |
| 3120 | // Skip reporting pointer down outside focus to the policy. |
| 3121 | break; |
| 3122 | } |
| 3123 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3124 | dispatchPointerDownOutsideFocus(motionEntry.source, dispatchEntry->resolvedAction, |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3125 | inputTarget.inputChannel->getConnectionToken()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3126 | |
| 3127 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3128 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3129 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3130 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3131 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3132 | case EventEntry::Type::DRAG: { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3133 | break; |
| 3134 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3135 | case EventEntry::Type::SENSOR: { |
| 3136 | LOG_ALWAYS_FATAL("SENSOR events should not go to apps via input channel"); |
| 3137 | break; |
| 3138 | } |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3139 | case EventEntry::Type::CONFIGURATION_CHANGED: |
| 3140 | case EventEntry::Type::DEVICE_RESET: { |
| 3141 | LOG_ALWAYS_FATAL("%s events should not go to apps", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3142 | ftl::enum_string(newEntry.type).c_str()); |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3143 | break; |
| 3144 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3145 | } |
| 3146 | |
| 3147 | // Remember that we are waiting for this dispatch to complete. |
| 3148 | if (dispatchEntry->hasForegroundTarget()) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3149 | incrementPendingForegroundDispatches(newEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3150 | } |
| 3151 | |
| 3152 | // Enqueue the dispatch entry. |
Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3153 | connection->outboundQueue.push_back(dispatchEntry.release()); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3154 | traceOutboundQueueLength(*connection); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3155 | } |
| 3156 | |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3157 | /** |
| 3158 | * This function is purely for debugging. It helps us understand where the user interaction |
| 3159 | * was taking place. For example, if user is touching launcher, we will see a log that user |
| 3160 | * started interacting with launcher. In that example, the event would go to the wallpaper as well. |
| 3161 | * We will see both launcher and wallpaper in that list. |
| 3162 | * Once the interaction with a particular set of connections starts, no new logs will be printed |
| 3163 | * until the set of interacted connections changes. |
| 3164 | * |
| 3165 | * The following items are skipped, to reduce the logspam: |
| 3166 | * ACTION_OUTSIDE: any windows that are receiving ACTION_OUTSIDE are not logged |
| 3167 | * ACTION_UP: any windows that receive ACTION_UP are not logged (for both keys and motions). |
| 3168 | * This includes situations like the soft BACK button key. When the user releases (lifts up the |
| 3169 | * finger) the back button, then navigation bar will inject KEYCODE_BACK with ACTION_UP. |
| 3170 | * Both of those ACTION_UP events would not be logged |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3171 | */ |
| 3172 | void InputDispatcher::updateInteractionTokensLocked(const EventEntry& entry, |
| 3173 | const std::vector<InputTarget>& targets) { |
| 3174 | // Skip ACTION_UP events, and all events other than keys and motions |
| 3175 | if (entry.type == EventEntry::Type::KEY) { |
| 3176 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); |
| 3177 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
| 3178 | return; |
| 3179 | } |
| 3180 | } else if (entry.type == EventEntry::Type::MOTION) { |
| 3181 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); |
| 3182 | if (motionEntry.action == AMOTION_EVENT_ACTION_UP || |
| 3183 | motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { |
| 3184 | return; |
| 3185 | } |
| 3186 | } else { |
| 3187 | return; // Not a key or a motion |
| 3188 | } |
| 3189 | |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 3190 | std::unordered_set<sp<IBinder>, StrongPointerHash<IBinder>> newConnectionTokens; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3191 | std::vector<sp<Connection>> newConnections; |
| 3192 | for (const InputTarget& target : targets) { |
| 3193 | if ((target.flags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) == |
| 3194 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 3195 | continue; // Skip windows that receive ACTION_OUTSIDE |
| 3196 | } |
| 3197 | |
| 3198 | sp<IBinder> token = target.inputChannel->getConnectionToken(); |
| 3199 | sp<Connection> connection = getConnectionLocked(token); |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3200 | if (connection == nullptr) { |
| 3201 | continue; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3202 | } |
| 3203 | newConnectionTokens.insert(std::move(token)); |
| 3204 | newConnections.emplace_back(connection); |
| 3205 | } |
| 3206 | if (newConnectionTokens == mInteractionConnectionTokens) { |
| 3207 | return; // no change |
| 3208 | } |
| 3209 | mInteractionConnectionTokens = newConnectionTokens; |
| 3210 | |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3211 | std::string targetList; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3212 | for (const sp<Connection>& connection : newConnections) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3213 | targetList += connection->getWindowName() + ", "; |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3214 | } |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3215 | std::string message = "Interaction with: " + targetList; |
| 3216 | if (targetList.empty()) { |
Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3217 | message += "<none>"; |
| 3218 | } |
| 3219 | android_log_event_list(LOGTAG_INPUT_INTERACTION) << message << LOG_ID_EVENTS; |
| 3220 | } |
| 3221 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3222 | void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3223 | const sp<IBinder>& token) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3224 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3225 | uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK; |
| 3226 | if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) { |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3227 | return; |
| 3228 | } |
| 3229 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 3230 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3231 | if (focusedToken == token) { |
| 3232 | // ignore since token is focused |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3233 | return; |
| 3234 | } |
| 3235 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3236 | auto command = [this, token]() REQUIRES(mLock) { |
| 3237 | scoped_unlock unlock(mLock); |
| 3238 | mPolicy->onPointerDownOutsideFocus(token); |
| 3239 | }; |
| 3240 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3241 | } |
| 3242 | |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3243 | status_t InputDispatcher::publishMotionEvent(Connection& connection, |
| 3244 | DispatchEntry& dispatchEntry) const { |
| 3245 | const EventEntry& eventEntry = *(dispatchEntry.eventEntry); |
| 3246 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); |
| 3247 | |
| 3248 | PointerCoords scaledCoords[MAX_POINTERS]; |
| 3249 | const PointerCoords* usingCoords = motionEntry.pointerCoords; |
| 3250 | |
| 3251 | // Set the X and Y offset and X and Y scale depending on the input source. |
| 3252 | if ((motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) && |
| 3253 | !(dispatchEntry.targetFlags & InputTarget::FLAG_ZERO_COORDS)) { |
| 3254 | float globalScaleFactor = dispatchEntry.globalScaleFactor; |
| 3255 | if (globalScaleFactor != 1.0f) { |
| 3256 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
| 3257 | scaledCoords[i] = motionEntry.pointerCoords[i]; |
| 3258 | // Don't apply window scale here since we don't want scale to affect raw |
| 3259 | // coordinates. The scale will be sent back to the client and applied |
| 3260 | // later when requesting relative coordinates. |
| 3261 | scaledCoords[i].scale(globalScaleFactor, 1 /* windowXScale */, |
| 3262 | 1 /* windowYScale */); |
| 3263 | } |
| 3264 | usingCoords = scaledCoords; |
| 3265 | } |
| 3266 | } else if (dispatchEntry.targetFlags & InputTarget::FLAG_ZERO_COORDS) { |
| 3267 | // We don't want the dispatch target to know the coordinates |
| 3268 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { |
| 3269 | scaledCoords[i].clear(); |
| 3270 | } |
| 3271 | usingCoords = scaledCoords; |
| 3272 | } |
| 3273 | |
| 3274 | std::array<uint8_t, 32> hmac = getSignature(motionEntry, dispatchEntry); |
| 3275 | |
| 3276 | // Publish the motion event. |
| 3277 | return connection.inputPublisher |
| 3278 | .publishMotionEvent(dispatchEntry.seq, dispatchEntry.resolvedEventId, |
| 3279 | motionEntry.deviceId, motionEntry.source, motionEntry.displayId, |
| 3280 | std::move(hmac), dispatchEntry.resolvedAction, |
| 3281 | motionEntry.actionButton, dispatchEntry.resolvedFlags, |
| 3282 | motionEntry.edgeFlags, motionEntry.metaState, |
| 3283 | motionEntry.buttonState, motionEntry.classification, |
| 3284 | dispatchEntry.transform, motionEntry.xPrecision, |
| 3285 | motionEntry.yPrecision, motionEntry.xCursorPosition, |
| 3286 | motionEntry.yCursorPosition, dispatchEntry.rawTransform, |
| 3287 | motionEntry.downTime, motionEntry.eventTime, |
| 3288 | motionEntry.pointerCount, motionEntry.pointerProperties, |
| 3289 | usingCoords); |
| 3290 | } |
| 3291 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3292 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3293 | const sp<Connection>& connection) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3294 | if (ATRACE_ENABLED()) { |
| 3295 | std::string message = StringPrintf("startDispatchCycleLocked(inputChannel=%s)", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3296 | connection->getInputChannelName().c_str()); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3297 | ATRACE_NAME(message.c_str()); |
| 3298 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3299 | if (DEBUG_DISPATCH_CYCLE) { |
| 3300 | ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str()); |
| 3301 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3302 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3303 | while (connection->status == Connection::Status::NORMAL && !connection->outboundQueue.empty()) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3304 | DispatchEntry* dispatchEntry = connection->outboundQueue.front(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3305 | dispatchEntry->deliveryTime = currentTime; |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3306 | const std::chrono::nanoseconds timeout = getDispatchingTimeoutLocked(connection); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3307 | dispatchEntry->timeoutTime = currentTime + timeout.count(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3308 | |
| 3309 | // Publish the event. |
| 3310 | status_t status; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3311 | const EventEntry& eventEntry = *(dispatchEntry->eventEntry); |
| 3312 | switch (eventEntry.type) { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3313 | case EventEntry::Type::KEY: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3314 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); |
| 3315 | std::array<uint8_t, 32> hmac = getSignature(keyEntry, *dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3316 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3317 | // Publish the key event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3318 | status = connection->inputPublisher |
| 3319 | .publishKeyEvent(dispatchEntry->seq, |
| 3320 | dispatchEntry->resolvedEventId, keyEntry.deviceId, |
| 3321 | keyEntry.source, keyEntry.displayId, |
| 3322 | std::move(hmac), dispatchEntry->resolvedAction, |
| 3323 | dispatchEntry->resolvedFlags, keyEntry.keyCode, |
| 3324 | keyEntry.scanCode, keyEntry.metaState, |
| 3325 | keyEntry.repeatCount, keyEntry.downTime, |
| 3326 | keyEntry.eventTime); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3327 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3328 | } |
| 3329 | |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3330 | case EventEntry::Type::MOTION: { |
Siarhei Vishniakou | cce7e11 | 2022-10-25 13:31:17 -0700 | [diff] [blame] | 3331 | status = publishMotionEvent(*connection, *dispatchEntry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3332 | break; |
| 3333 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3334 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3335 | case EventEntry::Type::FOCUS: { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3336 | const FocusEntry& focusEntry = static_cast<const FocusEntry&>(eventEntry); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3337 | status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3338 | focusEntry.id, |
Antonio Kantek | 3cfec7b | 2021-11-05 18:26:17 -0700 | [diff] [blame] | 3339 | focusEntry.hasFocus); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3340 | break; |
| 3341 | } |
| 3342 | |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3343 | case EventEntry::Type::TOUCH_MODE_CHANGED: { |
| 3344 | const TouchModeEntry& touchModeEntry = |
| 3345 | static_cast<const TouchModeEntry&>(eventEntry); |
| 3346 | status = connection->inputPublisher |
| 3347 | .publishTouchModeEvent(dispatchEntry->seq, touchModeEntry.id, |
| 3348 | touchModeEntry.inTouchMode); |
| 3349 | |
| 3350 | break; |
| 3351 | } |
| 3352 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3353 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { |
| 3354 | const auto& captureEntry = |
| 3355 | static_cast<const PointerCaptureChangedEntry&>(eventEntry); |
| 3356 | status = connection->inputPublisher |
| 3357 | .publishCaptureEvent(dispatchEntry->seq, captureEntry.id, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 3358 | captureEntry.pointerCaptureRequest.enable); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3359 | break; |
| 3360 | } |
| 3361 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3362 | case EventEntry::Type::DRAG: { |
| 3363 | const DragEntry& dragEntry = static_cast<const DragEntry&>(eventEntry); |
| 3364 | status = connection->inputPublisher.publishDragEvent(dispatchEntry->seq, |
| 3365 | dragEntry.id, dragEntry.x, |
| 3366 | dragEntry.y, |
| 3367 | dragEntry.isExiting); |
| 3368 | break; |
| 3369 | } |
| 3370 | |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3371 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3372 | case EventEntry::Type::DEVICE_RESET: |
| 3373 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3374 | LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3375 | ftl::enum_string(eventEntry.type).c_str()); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3376 | return; |
Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3377 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3378 | } |
| 3379 | |
| 3380 | // Check the result. |
| 3381 | if (status) { |
| 3382 | if (status == WOULD_BLOCK) { |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3383 | if (connection->waitQueue.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3384 | ALOGE("channel '%s' ~ Could not publish event because the pipe is full. " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3385 | "This is unexpected because the wait queue is empty, so the pipe " |
| 3386 | "should be empty and we shouldn't have any problems writing an " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3387 | "event to it, status=%s(%d)", |
| 3388 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3389 | status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3390 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 3391 | } else { |
| 3392 | // Pipe is full and we are waiting for the app to finish process some events |
| 3393 | // before sending more events to it. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3394 | if (DEBUG_DISPATCH_CYCLE) { |
| 3395 | ALOGD("channel '%s' ~ Could not publish event because the pipe is full, " |
| 3396 | "waiting for the application to catch up", |
| 3397 | connection->getInputChannelName().c_str()); |
| 3398 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3399 | } |
| 3400 | } else { |
| 3401 | ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, " |
Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3402 | "status=%s(%d)", |
| 3403 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3404 | status); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3405 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
| 3406 | } |
| 3407 | return; |
| 3408 | } |
| 3409 | |
| 3410 | // Re-enqueue the event on the wait queue. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3411 | connection->outboundQueue.erase(std::remove(connection->outboundQueue.begin(), |
| 3412 | connection->outboundQueue.end(), |
| 3413 | dispatchEntry)); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3414 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3415 | connection->waitQueue.push_back(dispatchEntry); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3416 | if (connection->responsive) { |
| 3417 | mAnrTracker.insert(dispatchEntry->timeoutTime, |
| 3418 | connection->inputChannel->getConnectionToken()); |
| 3419 | } |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3420 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3421 | } |
| 3422 | } |
| 3423 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3424 | std::array<uint8_t, 32> InputDispatcher::sign(const VerifiedInputEvent& event) const { |
| 3425 | size_t size; |
| 3426 | switch (event.type) { |
| 3427 | case VerifiedInputEvent::Type::KEY: { |
| 3428 | size = sizeof(VerifiedKeyEvent); |
| 3429 | break; |
| 3430 | } |
| 3431 | case VerifiedInputEvent::Type::MOTION: { |
| 3432 | size = sizeof(VerifiedMotionEvent); |
| 3433 | break; |
| 3434 | } |
| 3435 | } |
| 3436 | const uint8_t* start = reinterpret_cast<const uint8_t*>(&event); |
| 3437 | return mHmacKeyManager.sign(start, size); |
| 3438 | } |
| 3439 | |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3440 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3441 | const MotionEntry& motionEntry, const DispatchEntry& dispatchEntry) const { |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3442 | const int32_t actionMasked = dispatchEntry.resolvedAction & AMOTION_EVENT_ACTION_MASK; |
| 3443 | if (actionMasked != AMOTION_EVENT_ACTION_UP && actionMasked != AMOTION_EVENT_ACTION_DOWN) { |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3444 | // Only sign events up and down events as the purely move events |
| 3445 | // are tied to their up/down counterparts so signing would be redundant. |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3446 | return INVALID_HMAC; |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3447 | } |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3448 | |
| 3449 | VerifiedMotionEvent verifiedEvent = |
| 3450 | verifiedMotionEventFromMotionEntry(motionEntry, dispatchEntry.rawTransform); |
| 3451 | verifiedEvent.actionMasked = actionMasked; |
| 3452 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS; |
| 3453 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3454 | } |
| 3455 | |
| 3456 | const std::array<uint8_t, 32> InputDispatcher::getSignature( |
| 3457 | const KeyEntry& keyEntry, const DispatchEntry& dispatchEntry) const { |
| 3458 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(keyEntry); |
| 3459 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_KEY_EVENT_FLAGS; |
| 3460 | verifiedEvent.action = dispatchEntry.resolvedAction; |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3461 | return sign(verifiedEvent); |
Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3462 | } |
| 3463 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3464 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3465 | const sp<Connection>& connection, uint32_t seq, |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 3466 | bool handled, nsecs_t consumeTime) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3467 | if (DEBUG_DISPATCH_CYCLE) { |
| 3468 | ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s", |
| 3469 | connection->getInputChannelName().c_str(), seq, toString(handled)); |
| 3470 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3471 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3472 | if (connection->status == Connection::Status::BROKEN || |
| 3473 | connection->status == Connection::Status::ZOMBIE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3474 | return; |
| 3475 | } |
| 3476 | |
| 3477 | // Notify other system components and prepare to start the next dispatch cycle. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3478 | auto command = [this, currentTime, connection, seq, handled, consumeTime]() REQUIRES(mLock) { |
| 3479 | doDispatchCycleFinishedCommand(currentTime, connection, seq, handled, consumeTime); |
| 3480 | }; |
| 3481 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3482 | } |
| 3483 | |
| 3484 | void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3485 | const sp<Connection>& connection, |
| 3486 | bool notify) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3487 | if (DEBUG_DISPATCH_CYCLE) { |
| 3488 | ALOGD("channel '%s' ~ abortBrokenDispatchCycle - notify=%s", |
| 3489 | connection->getInputChannelName().c_str(), toString(notify)); |
| 3490 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3491 | |
| 3492 | // Clear the dispatch queues. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3493 | drainDispatchQueue(connection->outboundQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3494 | traceOutboundQueueLength(*connection); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3495 | drainDispatchQueue(connection->waitQueue); |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3496 | traceWaitQueueLength(*connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3497 | |
| 3498 | // The connection appears to be unrecoverably broken. |
| 3499 | // Ignore already broken or zombie connections. |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3500 | if (connection->status == Connection::Status::NORMAL) { |
| 3501 | connection->status = Connection::Status::BROKEN; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3502 | |
| 3503 | if (notify) { |
| 3504 | // Notify other system components. |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3505 | ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", |
| 3506 | connection->getInputChannelName().c_str()); |
| 3507 | |
| 3508 | auto command = [this, connection]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3509 | scoped_unlock unlock(mLock); |
| 3510 | mPolicy->notifyInputChannelBroken(connection->inputChannel->getConnectionToken()); |
| 3511 | }; |
| 3512 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3513 | } |
| 3514 | } |
| 3515 | } |
| 3516 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3517 | void InputDispatcher::drainDispatchQueue(std::deque<DispatchEntry*>& queue) { |
| 3518 | while (!queue.empty()) { |
| 3519 | DispatchEntry* dispatchEntry = queue.front(); |
| 3520 | queue.pop_front(); |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3521 | releaseDispatchEntry(dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3522 | } |
| 3523 | } |
| 3524 | |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3525 | void InputDispatcher::releaseDispatchEntry(DispatchEntry* dispatchEntry) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3526 | if (dispatchEntry->hasForegroundTarget()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3527 | decrementPendingForegroundDispatches(*(dispatchEntry->eventEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3528 | } |
| 3529 | delete dispatchEntry; |
| 3530 | } |
| 3531 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3532 | int InputDispatcher::handleReceiveCallback(int events, sp<IBinder> connectionToken) { |
| 3533 | std::scoped_lock _l(mLock); |
| 3534 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 3535 | if (connection == nullptr) { |
| 3536 | ALOGW("Received looper callback for unknown input channel token %p. events=0x%x", |
| 3537 | connectionToken.get(), events); |
| 3538 | return 0; // remove the callback |
| 3539 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3540 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3541 | bool notify; |
| 3542 | if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { |
| 3543 | if (!(events & ALOOPER_EVENT_INPUT)) { |
| 3544 | ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. " |
| 3545 | "events=0x%x", |
| 3546 | connection->getInputChannelName().c_str(), events); |
| 3547 | return 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3548 | } |
| 3549 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3550 | nsecs_t currentTime = now(); |
| 3551 | bool gotOne = false; |
| 3552 | status_t status = OK; |
| 3553 | for (;;) { |
| 3554 | Result<InputPublisher::ConsumerResponse> result = |
| 3555 | connection->inputPublisher.receiveConsumerResponse(); |
| 3556 | if (!result.ok()) { |
| 3557 | status = result.error().code(); |
| 3558 | break; |
| 3559 | } |
| 3560 | |
| 3561 | if (std::holds_alternative<InputPublisher::Finished>(*result)) { |
| 3562 | const InputPublisher::Finished& finish = |
| 3563 | std::get<InputPublisher::Finished>(*result); |
| 3564 | finishDispatchCycleLocked(currentTime, connection, finish.seq, finish.handled, |
| 3565 | finish.consumeTime); |
| 3566 | } else if (std::holds_alternative<InputPublisher::Timeline>(*result)) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3567 | if (shouldReportMetricsForConnection(*connection)) { |
| 3568 | const InputPublisher::Timeline& timeline = |
| 3569 | std::get<InputPublisher::Timeline>(*result); |
| 3570 | mLatencyTracker |
| 3571 | .trackGraphicsLatency(timeline.inputEventId, |
| 3572 | connection->inputChannel->getConnectionToken(), |
| 3573 | std::move(timeline.graphicsTimeline)); |
| 3574 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3575 | } |
| 3576 | gotOne = true; |
| 3577 | } |
| 3578 | if (gotOne) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3579 | runCommandsLockedInterruptable(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3580 | if (status == WOULD_BLOCK) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3581 | return 1; |
| 3582 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3583 | } |
| 3584 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3585 | notify = status != DEAD_OBJECT || !connection->monitor; |
| 3586 | if (notify) { |
| 3587 | ALOGE("channel '%s' ~ Failed to receive finished signal. status=%s(%d)", |
| 3588 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), |
| 3589 | status); |
| 3590 | } |
| 3591 | } else { |
| 3592 | // Monitor channels are never explicitly unregistered. |
| 3593 | // We do it automatically when the remote endpoint is closed so don't warn about them. |
| 3594 | const bool stillHaveWindowHandle = |
| 3595 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()) != nullptr; |
| 3596 | notify = !connection->monitor && stillHaveWindowHandle; |
| 3597 | if (notify) { |
| 3598 | ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred. events=0x%x", |
| 3599 | connection->getInputChannelName().c_str(), events); |
| 3600 | } |
| 3601 | } |
| 3602 | |
| 3603 | // Remove the channel. |
| 3604 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), notify); |
| 3605 | return 0; // remove the callback |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3606 | } |
| 3607 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3608 | void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3609 | const CancelationOptions& options) { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3610 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 3611 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3612 | } |
| 3613 | } |
| 3614 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3615 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3616 | const CancelationOptions& options) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 3617 | for (const auto& [_, monitors] : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3618 | for (const Monitor& monitor : monitors) { |
| 3619 | synthesizeCancelationEventsForInputChannelLocked(monitor.inputChannel, options); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3620 | } |
Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3621 | } |
| 3622 | } |
| 3623 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3624 | void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked( |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 3625 | const std::shared_ptr<InputChannel>& channel, const CancelationOptions& options) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 3626 | sp<Connection> connection = getConnectionLocked(channel->getConnectionToken()); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3627 | if (connection == nullptr) { |
| 3628 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3629 | } |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3630 | |
| 3631 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3632 | } |
| 3633 | |
| 3634 | void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( |
| 3635 | const sp<Connection>& connection, const CancelationOptions& options) { |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3636 | if (connection->status == Connection::Status::BROKEN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3637 | return; |
| 3638 | } |
| 3639 | |
| 3640 | nsecs_t currentTime = now(); |
| 3641 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3642 | std::vector<std::unique_ptr<EventEntry>> cancelationEvents = |
Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 3643 | connection->inputState.synthesizeCancelationEvents(currentTime, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3644 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3645 | if (cancelationEvents.empty()) { |
| 3646 | return; |
| 3647 | } |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3648 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 3649 | ALOGD("channel '%s' ~ Synthesized %zu cancelation events to bring channel back in sync " |
| 3650 | "with reality: %s, mode=%d.", |
| 3651 | connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason, |
| 3652 | options.mode); |
| 3653 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3654 | |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3655 | std::string reason = std::string("reason=").append(options.reason); |
| 3656 | android_log_event_list(LOGTAG_INPUT_CANCEL) |
| 3657 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; |
| 3658 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3659 | InputTarget target; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3660 | sp<WindowInfoHandle> windowHandle = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3661 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3662 | if (windowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3663 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3664 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3665 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3666 | } |
| 3667 | target.inputChannel = connection->inputChannel; |
| 3668 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 3669 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 3670 | const bool wasEmpty = connection->outboundQueue.empty(); |
| 3671 | |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3672 | for (size_t i = 0; i < cancelationEvents.size(); i++) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3673 | std::unique_ptr<EventEntry> cancelationEventEntry = std::move(cancelationEvents[i]); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3674 | switch (cancelationEventEntry->type) { |
| 3675 | case EventEntry::Type::KEY: { |
| 3676 | logOutboundKeyDetails("cancel - ", |
| 3677 | static_cast<const KeyEntry&>(*cancelationEventEntry)); |
| 3678 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3679 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3680 | case EventEntry::Type::MOTION: { |
| 3681 | logOutboundMotionDetails("cancel - ", |
| 3682 | static_cast<const MotionEntry&>(*cancelationEventEntry)); |
| 3683 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3684 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3685 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3686 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3687 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
| 3688 | case EventEntry::Type::DRAG: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3689 | LOG_ALWAYS_FATAL("Canceling %s events is not supported", |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3690 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3691 | break; |
| 3692 | } |
| 3693 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3694 | case EventEntry::Type::DEVICE_RESET: |
| 3695 | case EventEntry::Type::SENSOR: { |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3696 | 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] | 3697 | ftl::enum_string(cancelationEventEntry->type).c_str()); |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3698 | break; |
| 3699 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3700 | } |
| 3701 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3702 | enqueueDispatchEntryLocked(connection, std::move(cancelationEventEntry), target, |
| 3703 | InputTarget::FLAG_DISPATCH_AS_IS); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3704 | } |
Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3705 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 3706 | // If the outbound queue was previously empty, start the dispatch cycle going. |
| 3707 | if (wasEmpty && !connection->outboundQueue.empty()) { |
| 3708 | startDispatchCycleLocked(currentTime, connection); |
| 3709 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3710 | } |
| 3711 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3712 | void InputDispatcher::synthesizePointerDownEventsForConnectionLocked( |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3713 | const nsecs_t downTime, const sp<Connection>& connection) { |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3714 | if (connection->status == Connection::Status::BROKEN) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3715 | return; |
| 3716 | } |
| 3717 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3718 | std::vector<std::unique_ptr<EventEntry>> downEvents = |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3719 | connection->inputState.synthesizePointerDownEvents(downTime); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3720 | |
| 3721 | if (downEvents.empty()) { |
| 3722 | return; |
| 3723 | } |
| 3724 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3725 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3726 | ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.", |
| 3727 | connection->getInputChannelName().c_str(), downEvents.size()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3728 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3729 | |
| 3730 | InputTarget target; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3731 | sp<WindowInfoHandle> windowHandle = |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3732 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); |
| 3733 | if (windowHandle != nullptr) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3734 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3735 | target.setDefaultPointerTransform(windowInfo->transform); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3736 | target.globalScaleFactor = windowInfo->globalScaleFactor; |
| 3737 | } |
| 3738 | target.inputChannel = connection->inputChannel; |
| 3739 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
| 3740 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 3741 | const bool wasEmpty = connection->outboundQueue.empty(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3742 | for (std::unique_ptr<EventEntry>& downEventEntry : downEvents) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3743 | switch (downEventEntry->type) { |
| 3744 | case EventEntry::Type::MOTION: { |
| 3745 | logOutboundMotionDetails("down - ", |
| 3746 | static_cast<const MotionEntry&>(*downEventEntry)); |
| 3747 | break; |
| 3748 | } |
| 3749 | |
| 3750 | case EventEntry::Type::KEY: |
| 3751 | case EventEntry::Type::FOCUS: |
Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3752 | case EventEntry::Type::TOUCH_MODE_CHANGED: |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3753 | case EventEntry::Type::CONFIGURATION_CHANGED: |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3754 | case EventEntry::Type::DEVICE_RESET: |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3755 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3756 | case EventEntry::Type::SENSOR: |
| 3757 | case EventEntry::Type::DRAG: { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3758 | 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] | 3759 | ftl::enum_string(downEventEntry->type).c_str()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3760 | break; |
| 3761 | } |
| 3762 | } |
| 3763 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3764 | enqueueDispatchEntryLocked(connection, std::move(downEventEntry), target, |
| 3765 | InputTarget::FLAG_DISPATCH_AS_IS); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3766 | } |
| 3767 | |
hongzuo liu | 95785e2 | 2022-09-06 02:51:35 +0000 | [diff] [blame] | 3768 | // If the outbound queue was previously empty, start the dispatch cycle going. |
| 3769 | if (wasEmpty && !connection->outboundQueue.empty()) { |
| 3770 | startDispatchCycleLocked(downTime, connection); |
| 3771 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3772 | } |
| 3773 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3774 | std::unique_ptr<MotionEntry> InputDispatcher::splitMotionEvent( |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3775 | const MotionEntry& originalMotionEntry, BitSet32 pointerIds, nsecs_t splitDownTime) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3776 | ALOG_ASSERT(pointerIds.value != 0); |
| 3777 | |
| 3778 | uint32_t splitPointerIndexMap[MAX_POINTERS]; |
| 3779 | PointerProperties splitPointerProperties[MAX_POINTERS]; |
| 3780 | PointerCoords splitPointerCoords[MAX_POINTERS]; |
| 3781 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3782 | uint32_t originalPointerCount = originalMotionEntry.pointerCount; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3783 | uint32_t splitPointerCount = 0; |
| 3784 | |
| 3785 | for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3786 | originalPointerIndex++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3787 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3788 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3789 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3790 | if (pointerIds.hasBit(pointerId)) { |
| 3791 | splitPointerIndexMap[splitPointerCount] = originalPointerIndex; |
| 3792 | splitPointerProperties[splitPointerCount].copyFrom(pointerProperties); |
| 3793 | splitPointerCoords[splitPointerCount].copyFrom( |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3794 | originalMotionEntry.pointerCoords[originalPointerIndex]); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3795 | splitPointerCount += 1; |
| 3796 | } |
| 3797 | } |
| 3798 | |
| 3799 | if (splitPointerCount != pointerIds.count()) { |
| 3800 | // This is bad. We are missing some of the pointers that we expected to deliver. |
| 3801 | // Most likely this indicates that we received an ACTION_MOVE events that has |
| 3802 | // different pointer ids than we expected based on the previous ACTION_DOWN |
| 3803 | // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers |
| 3804 | // in this way. |
| 3805 | ALOGW("Dropping split motion event because the pointer count is %d but " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3806 | "we expected there to be %d pointers. This probably means we received " |
| 3807 | "a broken sequence of pointer ids from the input device.", |
| 3808 | splitPointerCount, pointerIds.count()); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3809 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3810 | } |
| 3811 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3812 | int32_t action = originalMotionEntry.action; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3813 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3814 | if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN || |
| 3815 | maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3816 | int32_t originalPointerIndex = getMotionEventActionPointerIndex(action); |
| 3817 | const PointerProperties& pointerProperties = |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3818 | originalMotionEntry.pointerProperties[originalPointerIndex]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3819 | uint32_t pointerId = uint32_t(pointerProperties.id); |
| 3820 | if (pointerIds.hasBit(pointerId)) { |
| 3821 | if (pointerIds.count() == 1) { |
| 3822 | // The first/last pointer went down/up. |
| 3823 | action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3824 | ? AMOTION_EVENT_ACTION_DOWN |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3825 | : (originalMotionEntry.flags & AMOTION_EVENT_FLAG_CANCELED) != 0 |
| 3826 | ? AMOTION_EVENT_ACTION_CANCEL |
| 3827 | : AMOTION_EVENT_ACTION_UP; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3828 | } else { |
| 3829 | // A secondary pointer went down/up. |
| 3830 | uint32_t splitPointerIndex = 0; |
| 3831 | while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { |
| 3832 | splitPointerIndex += 1; |
| 3833 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3834 | action = maskedAction | |
| 3835 | (splitPointerIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3836 | } |
| 3837 | } else { |
| 3838 | // An unrelated pointer changed. |
| 3839 | action = AMOTION_EVENT_ACTION_MOVE; |
| 3840 | } |
| 3841 | } |
| 3842 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3843 | if (action == AMOTION_EVENT_ACTION_DOWN) { |
| 3844 | LOG_ALWAYS_FATAL_IF(splitDownTime != originalMotionEntry.eventTime, |
| 3845 | "Split motion event has mismatching downTime and eventTime for " |
| 3846 | "ACTION_DOWN, motionEntry=%s, splitDownTime=%" PRId64 "ms", |
| 3847 | originalMotionEntry.getDescription().c_str(), ns2ms(splitDownTime)); |
| 3848 | } |
| 3849 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3850 | int32_t newId = mIdGenerator.nextId(); |
| 3851 | if (ATRACE_ENABLED()) { |
| 3852 | std::string message = StringPrintf("Split MotionEvent(id=0x%" PRIx32 |
| 3853 | ") to MotionEvent(id=0x%" PRIx32 ").", |
| 3854 | originalMotionEntry.id, newId); |
| 3855 | ATRACE_NAME(message.c_str()); |
| 3856 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3857 | std::unique_ptr<MotionEntry> splitMotionEntry = |
| 3858 | std::make_unique<MotionEntry>(newId, originalMotionEntry.eventTime, |
| 3859 | originalMotionEntry.deviceId, originalMotionEntry.source, |
| 3860 | originalMotionEntry.displayId, |
| 3861 | originalMotionEntry.policyFlags, action, |
| 3862 | originalMotionEntry.actionButton, |
| 3863 | originalMotionEntry.flags, originalMotionEntry.metaState, |
| 3864 | originalMotionEntry.buttonState, |
| 3865 | originalMotionEntry.classification, |
| 3866 | originalMotionEntry.edgeFlags, |
| 3867 | originalMotionEntry.xPrecision, |
| 3868 | originalMotionEntry.yPrecision, |
| 3869 | originalMotionEntry.xCursorPosition, |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3870 | originalMotionEntry.yCursorPosition, splitDownTime, |
| 3871 | splitPointerCount, splitPointerProperties, |
| 3872 | splitPointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3873 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3874 | if (originalMotionEntry.injectionState) { |
| 3875 | splitMotionEntry->injectionState = originalMotionEntry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3876 | splitMotionEntry->injectionState->refCount += 1; |
| 3877 | } |
| 3878 | |
| 3879 | return splitMotionEntry; |
| 3880 | } |
| 3881 | |
| 3882 | void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3883 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 3884 | ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args->eventTime); |
| 3885 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3886 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3887 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3888 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3889 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3890 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3891 | std::unique_ptr<ConfigurationChangedEntry> newEntry = |
| 3892 | std::make_unique<ConfigurationChangedEntry>(args->id, args->eventTime); |
| 3893 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3894 | } // release lock |
| 3895 | |
| 3896 | if (needWake) { |
| 3897 | mLooper->wake(); |
| 3898 | } |
| 3899 | } |
| 3900 | |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3901 | /** |
| 3902 | * If one of the meta shortcuts is detected, process them here: |
| 3903 | * Meta + Backspace -> generate BACK |
| 3904 | * Meta + Enter -> generate HOME |
| 3905 | * This will potentially overwrite keyCode and metaState. |
| 3906 | */ |
| 3907 | void InputDispatcher::accelerateMetaShortcuts(const int32_t deviceId, const int32_t action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3908 | int32_t& keyCode, int32_t& metaState) { |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3909 | if (metaState & AMETA_META_ON && action == AKEY_EVENT_ACTION_DOWN) { |
| 3910 | int32_t newKeyCode = AKEYCODE_UNKNOWN; |
| 3911 | if (keyCode == AKEYCODE_DEL) { |
| 3912 | newKeyCode = AKEYCODE_BACK; |
| 3913 | } else if (keyCode == AKEYCODE_ENTER) { |
| 3914 | newKeyCode = AKEYCODE_HOME; |
| 3915 | } |
| 3916 | if (newKeyCode != AKEYCODE_UNKNOWN) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3917 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3918 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3919 | mReplacedKeys[replacement] = newKeyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3920 | keyCode = newKeyCode; |
| 3921 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3922 | } |
| 3923 | } else if (action == AKEY_EVENT_ACTION_UP) { |
| 3924 | // In order to maintain a consistent stream of up and down events, check to see if the key |
| 3925 | // going up is one we've replaced in a down event and haven't yet replaced in an up event, |
| 3926 | // 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] | 3927 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3928 | struct KeyReplacement replacement = {keyCode, deviceId}; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3929 | auto replacementIt = mReplacedKeys.find(replacement); |
| 3930 | if (replacementIt != mReplacedKeys.end()) { |
| 3931 | keyCode = replacementIt->second; |
| 3932 | mReplacedKeys.erase(replacementIt); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3933 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); |
| 3934 | } |
| 3935 | } |
| 3936 | } |
| 3937 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3938 | void InputDispatcher::notifyKey(const NotifyKeyArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3939 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 3940 | ALOGD("notifyKey - eventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 |
| 3941 | "policyFlags=0x%x, action=0x%x, " |
| 3942 | "flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, downTime=%" PRId64, |
| 3943 | args->eventTime, args->deviceId, args->source, args->displayId, args->policyFlags, |
| 3944 | args->action, args->flags, args->keyCode, args->scanCode, args->metaState, |
| 3945 | args->downTime); |
| 3946 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3947 | if (!validateKeyEvent(args->action)) { |
| 3948 | return; |
| 3949 | } |
| 3950 | |
| 3951 | uint32_t policyFlags = args->policyFlags; |
| 3952 | int32_t flags = args->flags; |
| 3953 | int32_t metaState = args->metaState; |
Siarhei Vishniakou | 622bd32 | 2018-10-29 18:02:27 -0700 | [diff] [blame] | 3954 | // InputDispatcher tracks and generates key repeats on behalf of |
| 3955 | // whatever notifies it, so repeatCount should always be set to 0 |
| 3956 | constexpr int32_t repeatCount = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3957 | if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { |
| 3958 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 3959 | flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
| 3960 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3961 | if (policyFlags & POLICY_FLAG_FUNCTION) { |
| 3962 | metaState |= AMETA_FUNCTION_ON; |
| 3963 | } |
| 3964 | |
| 3965 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 3966 | |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3967 | int32_t keyCode = args->keyCode; |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3968 | accelerateMetaShortcuts(args->deviceId, args->action, keyCode, metaState); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3969 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3970 | KeyEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3971 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3972 | args->action, flags, keyCode, args->scanCode, metaState, repeatCount, |
| 3973 | args->downTime, args->eventTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3974 | |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3975 | android::base::Timer t; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3976 | mPolicy->interceptKeyBeforeQueueing(&event, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3977 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 3978 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3979 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3980 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3981 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3982 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3983 | { // acquire lock |
| 3984 | mLock.lock(); |
| 3985 | |
| 3986 | if (shouldSendKeyToInputFilterLocked(args)) { |
| 3987 | mLock.unlock(); |
| 3988 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 3989 | policyFlags |= POLICY_FLAG_FILTERED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3990 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 3991 | return; // event was consumed by the filter |
| 3992 | } |
| 3993 | |
| 3994 | mLock.lock(); |
| 3995 | } |
| 3996 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3997 | std::unique_ptr<KeyEntry> newEntry = |
| 3998 | std::make_unique<KeyEntry>(args->id, args->eventTime, args->deviceId, args->source, |
| 3999 | args->displayId, policyFlags, args->action, flags, |
| 4000 | keyCode, args->scanCode, metaState, repeatCount, |
| 4001 | args->downTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4002 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4003 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4004 | mLock.unlock(); |
| 4005 | } // release lock |
| 4006 | |
| 4007 | if (needWake) { |
| 4008 | mLooper->wake(); |
| 4009 | } |
| 4010 | } |
| 4011 | |
| 4012 | bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) { |
| 4013 | return mInputFilterEnabled; |
| 4014 | } |
| 4015 | |
| 4016 | void InputDispatcher::notifyMotion(const NotifyMotionArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4017 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4018 | ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 4019 | "displayId=%" PRId32 ", policyFlags=0x%x, " |
Siarhei Vishniakou | 6ebd069 | 2022-10-20 15:05:45 -0700 | [diff] [blame] | 4020 | "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] | 4021 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, xCursorPosition=%f, " |
| 4022 | "yCursorPosition=%f, downTime=%" PRId64, |
| 4023 | args->id, args->eventTime, args->deviceId, args->source, args->displayId, |
Siarhei Vishniakou | 6ebd069 | 2022-10-20 15:05:45 -0700 | [diff] [blame] | 4024 | args->policyFlags, MotionEvent::actionToString(args->action).c_str(), |
| 4025 | args->actionButton, args->flags, args->metaState, args->buttonState, args->edgeFlags, |
| 4026 | args->xPrecision, args->yPrecision, args->xCursorPosition, args->yCursorPosition, |
| 4027 | args->downTime); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4028 | for (uint32_t i = 0; i < args->pointerCount; i++) { |
| 4029 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
| 4030 | "x=%f, y=%f, pressure=%f, size=%f, " |
| 4031 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
| 4032 | "orientation=%f", |
| 4033 | i, args->pointerProperties[i].id, args->pointerProperties[i].toolType, |
| 4034 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 4035 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 4036 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 4037 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 4038 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 4039 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 4040 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 4041 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 4042 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| 4043 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4044 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4045 | if (!validateMotionEvent(args->action, args->actionButton, args->pointerCount, |
| 4046 | args->pointerProperties)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4047 | return; |
| 4048 | } |
| 4049 | |
| 4050 | uint32_t policyFlags = args->policyFlags; |
| 4051 | policyFlags |= POLICY_FLAG_TRUSTED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4052 | |
| 4053 | android::base::Timer t; |
Charles Chen | 3611f1f | 2019-01-29 17:26:18 +0800 | [diff] [blame] | 4054 | mPolicy->interceptMotionBeforeQueueing(args->displayId, args->eventTime, /*byref*/ policyFlags); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4055 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4056 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4057 | std::to_string(t.duration().count()).c_str()); |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4058 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4059 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4060 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4061 | { // acquire lock |
| 4062 | mLock.lock(); |
| 4063 | |
| 4064 | if (shouldSendMotionToInputFilterLocked(args)) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4065 | ui::Transform displayTransform; |
| 4066 | if (const auto it = mDisplayInfos.find(args->displayId); it != mDisplayInfos.end()) { |
| 4067 | displayTransform = it->second.transform; |
| 4068 | } |
| 4069 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4070 | mLock.unlock(); |
| 4071 | |
| 4072 | MotionEvent event; |
Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4073 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, |
| 4074 | args->action, args->actionButton, args->flags, args->edgeFlags, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 4075 | args->metaState, args->buttonState, args->classification, |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4076 | displayTransform, args->xPrecision, args->yPrecision, |
| 4077 | args->xCursorPosition, args->yCursorPosition, displayTransform, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 4078 | args->downTime, args->eventTime, args->pointerCount, |
| 4079 | args->pointerProperties, args->pointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4080 | |
| 4081 | policyFlags |= POLICY_FLAG_FILTERED; |
| 4082 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 4083 | return; // event was consumed by the filter |
| 4084 | } |
| 4085 | |
| 4086 | mLock.lock(); |
| 4087 | } |
| 4088 | |
| 4089 | // Just enqueue a new motion event. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4090 | std::unique_ptr<MotionEntry> newEntry = |
| 4091 | std::make_unique<MotionEntry>(args->id, args->eventTime, args->deviceId, |
| 4092 | args->source, args->displayId, policyFlags, |
| 4093 | args->action, args->actionButton, args->flags, |
| 4094 | args->metaState, args->buttonState, |
| 4095 | args->classification, args->edgeFlags, |
| 4096 | args->xPrecision, args->yPrecision, |
| 4097 | args->xCursorPosition, args->yCursorPosition, |
| 4098 | args->downTime, args->pointerCount, |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4099 | args->pointerProperties, args->pointerCoords); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4100 | |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 4101 | if (args->id != android::os::IInputConstants::INVALID_INPUT_EVENT_ID && |
| 4102 | IdGenerator::getSource(args->id) == IdGenerator::Source::INPUT_READER && |
| 4103 | !mInputFilterEnabled) { |
| 4104 | const bool isDown = args->action == AMOTION_EVENT_ACTION_DOWN; |
| 4105 | mLatencyTracker.trackListener(args->id, isDown, args->eventTime, args->readTime); |
| 4106 | } |
| 4107 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4108 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4109 | mLock.unlock(); |
| 4110 | } // release lock |
| 4111 | |
| 4112 | if (needWake) { |
| 4113 | mLooper->wake(); |
| 4114 | } |
| 4115 | } |
| 4116 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4117 | void InputDispatcher::notifySensor(const NotifySensorArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4118 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4119 | ALOGD("notifySensor - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " |
| 4120 | " sensorType=%s", |
| 4121 | args->id, args->eventTime, args->deviceId, args->source, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 4122 | ftl::enum_string(args->sensorType).c_str()); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4123 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4124 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4125 | bool needWake = false; |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4126 | { // acquire lock |
| 4127 | mLock.lock(); |
| 4128 | |
| 4129 | // Just enqueue a new sensor event. |
| 4130 | std::unique_ptr<SensorEntry> newEntry = |
| 4131 | std::make_unique<SensorEntry>(args->id, args->eventTime, args->deviceId, |
| 4132 | args->source, 0 /* policyFlags*/, args->hwTimestamp, |
| 4133 | args->sensorType, args->accuracy, |
| 4134 | args->accuracyChanged, args->values); |
| 4135 | |
| 4136 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
| 4137 | mLock.unlock(); |
| 4138 | } // release lock |
| 4139 | |
| 4140 | if (needWake) { |
| 4141 | mLooper->wake(); |
| 4142 | } |
| 4143 | } |
| 4144 | |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 4145 | void InputDispatcher::notifyVibratorState(const NotifyVibratorStateArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4146 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4147 | ALOGD("notifyVibratorState - eventTime=%" PRId64 ", device=%d, isOn=%d", args->eventTime, |
| 4148 | args->deviceId, args->isOn); |
| 4149 | } |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 4150 | mPolicy->notifyVibratorState(args->deviceId, args->isOn); |
| 4151 | } |
| 4152 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4153 | bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4154 | return mInputFilterEnabled; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4155 | } |
| 4156 | |
| 4157 | void InputDispatcher::notifySwitch(const NotifySwitchArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4158 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4159 | ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, " |
| 4160 | "switchMask=0x%08x", |
| 4161 | args->eventTime, args->policyFlags, args->switchValues, args->switchMask); |
| 4162 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4163 | |
| 4164 | uint32_t policyFlags = args->policyFlags; |
| 4165 | policyFlags |= POLICY_FLAG_TRUSTED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4166 | mPolicy->notifySwitch(args->eventTime, args->switchValues, args->switchMask, policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4167 | } |
| 4168 | |
| 4169 | void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4170 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4171 | ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args->eventTime, |
| 4172 | args->deviceId); |
| 4173 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4174 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4175 | bool needWake = false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4176 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4177 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4178 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4179 | std::unique_ptr<DeviceResetEntry> newEntry = |
| 4180 | std::make_unique<DeviceResetEntry>(args->id, args->eventTime, args->deviceId); |
| 4181 | needWake = enqueueInboundEventLocked(std::move(newEntry)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4182 | } // release lock |
| 4183 | |
| 4184 | if (needWake) { |
| 4185 | mLooper->wake(); |
| 4186 | } |
| 4187 | } |
| 4188 | |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4189 | void InputDispatcher::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs* args) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4190 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
| 4191 | ALOGD("notifyPointerCaptureChanged - eventTime=%" PRId64 ", enabled=%s", args->eventTime, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4192 | args->request.enable ? "true" : "false"); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4193 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4194 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4195 | bool needWake = false; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4196 | { // acquire lock |
| 4197 | std::scoped_lock _l(mLock); |
| 4198 | auto entry = std::make_unique<PointerCaptureChangedEntry>(args->id, args->eventTime, |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4199 | args->request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4200 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 4201 | } // release lock |
| 4202 | |
| 4203 | if (needWake) { |
| 4204 | mLooper->wake(); |
| 4205 | } |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4206 | } |
| 4207 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4208 | InputEventInjectionResult InputDispatcher::injectInputEvent(const InputEvent* event, |
| 4209 | std::optional<int32_t> targetUid, |
| 4210 | InputEventInjectionSync syncMode, |
| 4211 | std::chrono::milliseconds timeout, |
| 4212 | uint32_t policyFlags) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4213 | if (DEBUG_INBOUND_EVENT_DETAILS) { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4214 | ALOGD("injectInputEvent - eventType=%d, targetUid=%s, syncMode=%d, timeout=%lld, " |
| 4215 | "policyFlags=0x%08x", |
| 4216 | event->getType(), targetUid ? std::to_string(*targetUid).c_str() : "none", syncMode, |
| 4217 | timeout.count(), policyFlags); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4218 | } |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 4219 | 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] | 4220 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4221 | policyFlags |= POLICY_FLAG_INJECTED | POLICY_FLAG_TRUSTED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4222 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4223 | // 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] | 4224 | // that have gone through the InputFilter. If the event passed through the InputFilter, assign |
| 4225 | // the provided device id. If the InputFilter is accessibility, and it modifies or synthesizes |
| 4226 | // the injected event, it is responsible for setting POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY. |
| 4227 | // For those events, we will set FLAG_IS_ACCESSIBILITY_EVENT to allow apps to distinguish them |
| 4228 | // from events that originate from actual hardware. |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4229 | int32_t resolvedDeviceId = VIRTUAL_KEYBOARD_ID; |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4230 | if (policyFlags & POLICY_FLAG_FILTERED) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4231 | resolvedDeviceId = event->getDeviceId(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4232 | } |
| 4233 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4234 | std::queue<std::unique_ptr<EventEntry>> injectedEntries; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4235 | switch (event->getType()) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4236 | case AINPUT_EVENT_TYPE_KEY: { |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4237 | const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event); |
| 4238 | int32_t action = incomingKey.getAction(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4239 | if (!validateKeyEvent(action)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4240 | return InputEventInjectionResult::FAILED; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4241 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4242 | |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4243 | int32_t flags = incomingKey.getFlags(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4244 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4245 | flags |= AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4246 | } |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4247 | int32_t keyCode = incomingKey.getKeyCode(); |
| 4248 | int32_t metaState = incomingKey.getMetaState(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4249 | accelerateMetaShortcuts(resolvedDeviceId, action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4250 | /*byref*/ keyCode, /*byref*/ metaState); |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4251 | KeyEvent keyEvent; |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4252 | keyEvent.initialize(incomingKey.getId(), resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4253 | incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode, |
| 4254 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), |
| 4255 | incomingKey.getDownTime(), incomingKey.getEventTime()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4256 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4257 | if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) { |
| 4258 | policyFlags |= POLICY_FLAG_VIRTUAL; |
Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4259 | } |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4260 | |
| 4261 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 4262 | android::base::Timer t; |
| 4263 | mPolicy->interceptKeyBeforeQueueing(&keyEvent, /*byref*/ policyFlags); |
| 4264 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4265 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", |
| 4266 | std::to_string(t.duration().count()).c_str()); |
| 4267 | } |
| 4268 | } |
| 4269 | |
| 4270 | mLock.lock(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4271 | std::unique_ptr<KeyEntry> injectedEntry = |
| 4272 | std::make_unique<KeyEntry>(incomingKey.getId(), incomingKey.getEventTime(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4273 | resolvedDeviceId, incomingKey.getSource(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4274 | incomingKey.getDisplayId(), policyFlags, action, |
| 4275 | flags, keyCode, incomingKey.getScanCode(), metaState, |
| 4276 | incomingKey.getRepeatCount(), |
| 4277 | incomingKey.getDownTime()); |
| 4278 | injectedEntries.push(std::move(injectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4279 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4280 | } |
| 4281 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4282 | case AINPUT_EVENT_TYPE_MOTION: { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4283 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4284 | const int32_t action = motionEvent.getAction(); |
| 4285 | const bool isPointerEvent = |
| 4286 | isFromSource(event->getSource(), AINPUT_SOURCE_CLASS_POINTER); |
| 4287 | // If a pointer event has no displayId specified, inject it to the default display. |
| 4288 | const uint32_t displayId = isPointerEvent && (event->getDisplayId() == ADISPLAY_ID_NONE) |
| 4289 | ? ADISPLAY_ID_DEFAULT |
| 4290 | : event->getDisplayId(); |
| 4291 | const size_t pointerCount = motionEvent.getPointerCount(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4292 | const PointerProperties* pointerProperties = motionEvent.getPointerProperties(); |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4293 | const int32_t actionButton = motionEvent.getActionButton(); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4294 | int32_t flags = motionEvent.getFlags(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4295 | if (!validateMotionEvent(action, actionButton, pointerCount, pointerProperties)) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4296 | return InputEventInjectionResult::FAILED; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4297 | } |
| 4298 | |
| 4299 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4300 | nsecs_t eventTime = motionEvent.getEventTime(); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4301 | android::base::Timer t; |
| 4302 | mPolicy->interceptMotionBeforeQueueing(displayId, eventTime, /*byref*/ policyFlags); |
| 4303 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 4304 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", |
| 4305 | std::to_string(t.duration().count()).c_str()); |
| 4306 | } |
| 4307 | } |
| 4308 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4309 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { |
| 4310 | flags |= AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; |
| 4311 | } |
| 4312 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4313 | mLock.lock(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4314 | const nsecs_t* sampleEventTimes = motionEvent.getSampleEventTimes(); |
| 4315 | const PointerCoords* samplePointerCoords = motionEvent.getSamplePointerCoords(); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4316 | std::unique_ptr<MotionEntry> injectedEntry = |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4317 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, |
| 4318 | resolvedDeviceId, motionEvent.getSource(), |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4319 | displayId, policyFlags, action, actionButton, |
| 4320 | flags, motionEvent.getMetaState(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4321 | motionEvent.getButtonState(), |
| 4322 | motionEvent.getClassification(), |
| 4323 | motionEvent.getEdgeFlags(), |
| 4324 | motionEvent.getXPrecision(), |
| 4325 | motionEvent.getYPrecision(), |
| 4326 | motionEvent.getRawXCursorPosition(), |
| 4327 | motionEvent.getRawYCursorPosition(), |
| 4328 | motionEvent.getDownTime(), uint32_t(pointerCount), |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4329 | pointerProperties, samplePointerCoords); |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4330 | transformMotionEntryForInjectionLocked(*injectedEntry, motionEvent.getTransform()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4331 | injectedEntries.push(std::move(injectedEntry)); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4332 | for (size_t i = motionEvent.getHistorySize(); i > 0; i--) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4333 | sampleEventTimes += 1; |
| 4334 | samplePointerCoords += pointerCount; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4335 | std::unique_ptr<MotionEntry> nextInjectedEntry = |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4336 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, |
| 4337 | resolvedDeviceId, motionEvent.getSource(), |
Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4338 | displayId, policyFlags, action, actionButton, |
| 4339 | flags, motionEvent.getMetaState(), |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4340 | motionEvent.getButtonState(), |
| 4341 | motionEvent.getClassification(), |
| 4342 | motionEvent.getEdgeFlags(), |
| 4343 | motionEvent.getXPrecision(), |
| 4344 | motionEvent.getYPrecision(), |
| 4345 | motionEvent.getRawXCursorPosition(), |
| 4346 | motionEvent.getRawYCursorPosition(), |
| 4347 | motionEvent.getDownTime(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4348 | uint32_t(pointerCount), pointerProperties, |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4349 | samplePointerCoords); |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4350 | transformMotionEntryForInjectionLocked(*nextInjectedEntry, |
| 4351 | motionEvent.getTransform()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4352 | injectedEntries.push(std::move(nextInjectedEntry)); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4353 | } |
| 4354 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4355 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4356 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4357 | default: |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 4358 | ALOGW("Cannot inject %s events", inputEventTypeToString(event->getType())); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4359 | return InputEventInjectionResult::FAILED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4360 | } |
| 4361 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4362 | InjectionState* injectionState = new InjectionState(targetUid); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4363 | if (syncMode == InputEventInjectionSync::NONE) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4364 | injectionState->injectionIsAsync = true; |
| 4365 | } |
| 4366 | |
| 4367 | injectionState->refCount += 1; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4368 | injectedEntries.back()->injectionState = injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4369 | |
| 4370 | bool needWake = false; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4371 | while (!injectedEntries.empty()) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4372 | needWake |= enqueueInboundEventLocked(std::move(injectedEntries.front())); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4373 | injectedEntries.pop(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4374 | } |
| 4375 | |
| 4376 | mLock.unlock(); |
| 4377 | |
| 4378 | if (needWake) { |
| 4379 | mLooper->wake(); |
| 4380 | } |
| 4381 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4382 | InputEventInjectionResult injectionResult; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4383 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4384 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4385 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4386 | if (syncMode == InputEventInjectionSync::NONE) { |
| 4387 | injectionResult = InputEventInjectionResult::SUCCEEDED; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4388 | } else { |
| 4389 | for (;;) { |
| 4390 | injectionResult = injectionState->injectionResult; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4391 | if (injectionResult != InputEventInjectionResult::PENDING) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4392 | break; |
| 4393 | } |
| 4394 | |
| 4395 | nsecs_t remainingTimeout = endTime - now(); |
| 4396 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4397 | if (DEBUG_INJECTION) { |
| 4398 | ALOGD("injectInputEvent - Timed out waiting for injection result " |
| 4399 | "to become available."); |
| 4400 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4401 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4402 | break; |
| 4403 | } |
| 4404 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4405 | mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4406 | } |
| 4407 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4408 | if (injectionResult == InputEventInjectionResult::SUCCEEDED && |
| 4409 | syncMode == InputEventInjectionSync::WAIT_FOR_FINISHED) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4410 | while (injectionState->pendingForegroundDispatches != 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4411 | if (DEBUG_INJECTION) { |
| 4412 | ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", |
| 4413 | injectionState->pendingForegroundDispatches); |
| 4414 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4415 | nsecs_t remainingTimeout = endTime - now(); |
| 4416 | if (remainingTimeout <= 0) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4417 | if (DEBUG_INJECTION) { |
| 4418 | ALOGD("injectInputEvent - Timed out waiting for pending foreground " |
| 4419 | "dispatches to finish."); |
| 4420 | } |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4421 | injectionResult = InputEventInjectionResult::TIMED_OUT; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4422 | break; |
| 4423 | } |
| 4424 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4425 | mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4426 | } |
| 4427 | } |
| 4428 | } |
| 4429 | |
| 4430 | injectionState->release(); |
| 4431 | } // release lock |
| 4432 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4433 | if (DEBUG_INJECTION) { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4434 | ALOGD("injectInputEvent - Finished with result %d.", injectionResult); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4435 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4436 | |
| 4437 | return injectionResult; |
| 4438 | } |
| 4439 | |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4440 | std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) { |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4441 | std::array<uint8_t, 32> calculatedHmac; |
| 4442 | std::unique_ptr<VerifiedInputEvent> result; |
| 4443 | switch (event.getType()) { |
| 4444 | case AINPUT_EVENT_TYPE_KEY: { |
| 4445 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event); |
| 4446 | VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent); |
| 4447 | result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4448 | calculatedHmac = sign(verifiedKeyEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4449 | break; |
| 4450 | } |
| 4451 | case AINPUT_EVENT_TYPE_MOTION: { |
| 4452 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event); |
| 4453 | VerifiedMotionEvent verifiedMotionEvent = |
| 4454 | verifiedMotionEventFromMotionEvent(motionEvent); |
| 4455 | result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent); |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4456 | calculatedHmac = sign(verifiedMotionEvent); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4457 | break; |
| 4458 | } |
| 4459 | default: { |
| 4460 | ALOGE("Cannot verify events of type %" PRId32, event.getType()); |
| 4461 | return nullptr; |
| 4462 | } |
| 4463 | } |
| 4464 | if (calculatedHmac == INVALID_HMAC) { |
| 4465 | return nullptr; |
| 4466 | } |
| 4467 | if (calculatedHmac != event.getHmac()) { |
| 4468 | return nullptr; |
| 4469 | } |
| 4470 | return result; |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4471 | } |
| 4472 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4473 | void InputDispatcher::setInjectionResult(EventEntry& entry, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4474 | InputEventInjectionResult injectionResult) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4475 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4476 | if (injectionState) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4477 | if (DEBUG_INJECTION) { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4478 | ALOGD("Setting input event injection result to %d.", injectionResult); |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4479 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4480 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4481 | if (injectionState->injectionIsAsync && !(entry.policyFlags & POLICY_FLAG_FILTERED)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4482 | // Log the outcome since the injector did not wait for the injection result. |
| 4483 | switch (injectionResult) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4484 | case InputEventInjectionResult::SUCCEEDED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4485 | ALOGV("Asynchronous input event injection succeeded."); |
| 4486 | break; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4487 | case InputEventInjectionResult::TARGET_MISMATCH: |
| 4488 | ALOGV("Asynchronous input event injection target mismatch."); |
| 4489 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4490 | case InputEventInjectionResult::FAILED: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4491 | ALOGW("Asynchronous input event injection failed."); |
| 4492 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4493 | case InputEventInjectionResult::TIMED_OUT: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4494 | ALOGW("Asynchronous input event injection timed out."); |
| 4495 | break; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4496 | case InputEventInjectionResult::PENDING: |
| 4497 | ALOGE("Setting result to 'PENDING' for asynchronous injection"); |
| 4498 | break; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4499 | } |
| 4500 | } |
| 4501 | |
| 4502 | injectionState->injectionResult = injectionResult; |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4503 | mInjectionResultAvailable.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4504 | } |
| 4505 | } |
| 4506 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4507 | void InputDispatcher::transformMotionEntryForInjectionLocked( |
| 4508 | MotionEntry& entry, const ui::Transform& injectedTransform) const { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4509 | // Input injection works in the logical display coordinate space, but the input pipeline works |
| 4510 | // display space, so we need to transform the injected events accordingly. |
| 4511 | const auto it = mDisplayInfos.find(entry.displayId); |
| 4512 | if (it == mDisplayInfos.end()) return; |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4513 | const auto& transformToDisplay = it->second.transform.inverse() * injectedTransform; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4514 | |
Prabir Pradhan | d9a2ebe | 2022-07-20 19:25:13 +0000 | [diff] [blame] | 4515 | if (entry.xCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION && |
| 4516 | entry.yCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) { |
| 4517 | const vec2 cursor = |
| 4518 | MotionEvent::calculateTransformedXY(entry.source, transformToDisplay, |
| 4519 | {entry.xCursorPosition, entry.yCursorPosition}); |
| 4520 | entry.xCursorPosition = cursor.x; |
| 4521 | entry.yCursorPosition = cursor.y; |
| 4522 | } |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4523 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
Prabir Pradhan | 8e6ce22 | 2022-02-24 09:08:54 -0800 | [diff] [blame] | 4524 | entry.pointerCoords[i] = |
| 4525 | MotionEvent::calculateTransformedCoords(entry.source, transformToDisplay, |
| 4526 | entry.pointerCoords[i]); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4527 | } |
| 4528 | } |
| 4529 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4530 | void InputDispatcher::incrementPendingForegroundDispatches(EventEntry& entry) { |
| 4531 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4532 | if (injectionState) { |
| 4533 | injectionState->pendingForegroundDispatches += 1; |
| 4534 | } |
| 4535 | } |
| 4536 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4537 | void InputDispatcher::decrementPendingForegroundDispatches(EventEntry& entry) { |
| 4538 | InjectionState* injectionState = entry.injectionState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4539 | if (injectionState) { |
| 4540 | injectionState->pendingForegroundDispatches -= 1; |
| 4541 | |
| 4542 | if (injectionState->pendingForegroundDispatches == 0) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4543 | mInjectionSyncFinished.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4544 | } |
| 4545 | } |
| 4546 | } |
| 4547 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4548 | const std::vector<sp<WindowInfoHandle>>& InputDispatcher::getWindowHandlesLocked( |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4549 | int32_t displayId) const { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4550 | static const std::vector<sp<WindowInfoHandle>> EMPTY_WINDOW_HANDLES; |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4551 | auto it = mWindowHandlesByDisplay.find(displayId); |
| 4552 | return it != mWindowHandlesByDisplay.end() ? it->second : EMPTY_WINDOW_HANDLES; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4553 | } |
| 4554 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4555 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4556 | const sp<IBinder>& windowHandleToken) const { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4557 | if (windowHandleToken == nullptr) { |
| 4558 | return nullptr; |
| 4559 | } |
| 4560 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4561 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4562 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 4563 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4564 | if (windowHandle->getToken() == windowHandleToken) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4565 | return windowHandle; |
| 4566 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4567 | } |
| 4568 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4569 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4570 | } |
| 4571 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4572 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked(const sp<IBinder>& windowHandleToken, |
| 4573 | int displayId) const { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4574 | if (windowHandleToken == nullptr) { |
| 4575 | return nullptr; |
| 4576 | } |
| 4577 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4578 | for (const sp<WindowInfoHandle>& windowHandle : getWindowHandlesLocked(displayId)) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4579 | if (windowHandle->getToken() == windowHandleToken) { |
| 4580 | return windowHandle; |
| 4581 | } |
| 4582 | } |
| 4583 | return nullptr; |
| 4584 | } |
| 4585 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4586 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( |
| 4587 | const sp<WindowInfoHandle>& windowHandle) const { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4588 | for (auto& it : mWindowHandlesByDisplay) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4589 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; |
| 4590 | for (const sp<WindowInfoHandle>& handle : windowHandles) { |
arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4591 | if (handle->getId() == windowHandle->getId() && |
| 4592 | handle->getToken() == windowHandle->getToken()) { |
Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4593 | if (windowHandle->getInfo()->displayId != it.first) { |
| 4594 | ALOGE("Found window %s in display %" PRId32 |
| 4595 | ", but it should belong to display %" PRId32, |
| 4596 | windowHandle->getName().c_str(), it.first, |
| 4597 | windowHandle->getInfo()->displayId); |
| 4598 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4599 | return handle; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4600 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4601 | } |
| 4602 | } |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4603 | return nullptr; |
| 4604 | } |
| 4605 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4606 | sp<WindowInfoHandle> InputDispatcher::getFocusedWindowHandleLocked(int displayId) const { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4607 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(displayId); |
| 4608 | return getWindowHandleLocked(focusedToken, displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4609 | } |
| 4610 | |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 4611 | bool InputDispatcher::canWindowReceiveMotionLocked(const sp<WindowInfoHandle>& window, |
| 4612 | const MotionEntry& motionEntry) const { |
| 4613 | const WindowInfo& info = *window->getInfo(); |
| 4614 | |
| 4615 | // Skip spy window targets that are not valid for targeted injection. |
| 4616 | if (const auto err = verifyTargetedInjection(window, motionEntry); err) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4617 | return false; |
| 4618 | } |
| 4619 | |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 4620 | if (info.inputConfig.test(WindowInfo::InputConfig::PAUSE_DISPATCHING)) { |
| 4621 | ALOGI("Not sending touch event to %s because it is paused", window->getName().c_str()); |
| 4622 | return false; |
| 4623 | } |
| 4624 | |
| 4625 | if (info.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) { |
| 4626 | ALOGW("Not sending touch gesture to %s because it has config NO_INPUT_CHANNEL", |
| 4627 | window->getName().c_str()); |
| 4628 | return false; |
| 4629 | } |
| 4630 | |
| 4631 | sp<Connection> connection = getConnectionLocked(window->getToken()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4632 | if (connection == nullptr) { |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 4633 | ALOGW("Not sending touch to %s because there's no corresponding connection", |
| 4634 | window->getName().c_str()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4635 | return false; |
| 4636 | } |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 4637 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4638 | if (!connection->responsive) { |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 4639 | 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] | 4640 | return false; |
| 4641 | } |
Siarhei Vishniakou | d4e3f3a | 2022-09-27 14:31:05 -0700 | [diff] [blame] | 4642 | |
| 4643 | // Drop events that can't be trusted due to occlusion |
| 4644 | const auto [x, y] = resolveTouchedPosition(motionEntry); |
| 4645 | TouchOcclusionInfo occlusionInfo = computeTouchOcclusionInfoLocked(window, x, y); |
| 4646 | if (!isTouchTrustedLocked(occlusionInfo)) { |
| 4647 | if (DEBUG_TOUCH_OCCLUSION) { |
| 4648 | ALOGD("Stack of obscuring windows during untrusted touch (%d, %d):", x, y); |
| 4649 | for (const auto& log : occlusionInfo.debugInfo) { |
| 4650 | ALOGD("%s", log.c_str()); |
| 4651 | } |
| 4652 | } |
| 4653 | ALOGW("Dropping untrusted touch event due to %s/%d", occlusionInfo.obscuringPackage.c_str(), |
| 4654 | occlusionInfo.obscuringUid); |
| 4655 | return false; |
| 4656 | } |
| 4657 | |
| 4658 | // Drop touch events if requested by input feature |
| 4659 | if (shouldDropInput(motionEntry, window)) { |
| 4660 | return false; |
| 4661 | } |
| 4662 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4663 | return true; |
| 4664 | } |
| 4665 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4666 | std::shared_ptr<InputChannel> InputDispatcher::getInputChannelLocked( |
| 4667 | const sp<IBinder>& token) const { |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4668 | auto connectionIt = mConnectionsByToken.find(token); |
| 4669 | if (connectionIt == mConnectionsByToken.end()) { |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4670 | return nullptr; |
| 4671 | } |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4672 | return connectionIt->second->inputChannel; |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4673 | } |
| 4674 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4675 | void InputDispatcher::updateWindowHandlesForDisplayLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4676 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
| 4677 | if (windowInfoHandles.empty()) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4678 | // Remove all handles on a display if there are no windows left. |
| 4679 | mWindowHandlesByDisplay.erase(displayId); |
| 4680 | return; |
| 4681 | } |
| 4682 | |
| 4683 | // Since we compare the pointer of input window handles across window updates, we need |
| 4684 | // 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] | 4685 | const std::vector<sp<WindowInfoHandle>>& oldHandles = getWindowHandlesLocked(displayId); |
| 4686 | std::unordered_map<int32_t /*id*/, sp<WindowInfoHandle>> oldHandlesById; |
| 4687 | for (const sp<WindowInfoHandle>& handle : oldHandles) { |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4688 | oldHandlesById[handle->getId()] = handle; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4689 | } |
| 4690 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4691 | std::vector<sp<WindowInfoHandle>> newHandles; |
| 4692 | for (const sp<WindowInfoHandle>& handle : windowInfoHandles) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4693 | const WindowInfo* info = handle->getInfo(); |
Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 4694 | if (getInputChannelLocked(handle->getToken()) == nullptr) { |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4695 | const bool noInputChannel = |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 4696 | info->inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4697 | const bool canReceiveInput = |
| 4698 | !info->inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE) || |
| 4699 | !info->inputConfig.test(WindowInfo::InputConfig::NOT_FOCUSABLE); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4700 | if (canReceiveInput && !noInputChannel) { |
John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 4701 | ALOGV("Window handle %s has no registered input channel", |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4702 | handle->getName().c_str()); |
Robert Carr | 2984b7a | 2020-04-13 17:06:45 -0700 | [diff] [blame] | 4703 | continue; |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4704 | } |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4705 | } |
| 4706 | |
| 4707 | if (info->displayId != displayId) { |
| 4708 | ALOGE("Window %s updated by wrong display %d, should belong to display %d", |
| 4709 | handle->getName().c_str(), displayId, info->displayId); |
| 4710 | continue; |
| 4711 | } |
| 4712 | |
Robert Carr | edd1360 | 2020-04-13 17:24:34 -0700 | [diff] [blame] | 4713 | if ((oldHandlesById.find(handle->getId()) != oldHandlesById.end()) && |
| 4714 | (oldHandlesById.at(handle->getId())->getToken() == handle->getToken())) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4715 | const sp<WindowInfoHandle>& oldHandle = oldHandlesById.at(handle->getId()); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4716 | oldHandle->updateFrom(handle); |
| 4717 | newHandles.push_back(oldHandle); |
| 4718 | } else { |
| 4719 | newHandles.push_back(handle); |
| 4720 | } |
| 4721 | } |
| 4722 | |
| 4723 | // Insert or replace |
| 4724 | mWindowHandlesByDisplay[displayId] = newHandles; |
| 4725 | } |
| 4726 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4727 | void InputDispatcher::setInputWindows( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4728 | const std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>>& handlesPerDisplay) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 4729 | // TODO(b/198444055): Remove setInputWindows from InputDispatcher. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4730 | { // acquire lock |
| 4731 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 4732 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 4733 | setInputWindowsLocked(handles, displayId); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4734 | } |
| 4735 | } |
| 4736 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4737 | mLooper->wake(); |
| 4738 | } |
| 4739 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4740 | /** |
| 4741 | * Called from InputManagerService, update window handle list by displayId that can receive input. |
| 4742 | * A window handle contains information about InputChannel, Touch Region, Types, Focused,... |
| 4743 | * If set an empty list, remove all handles from the specific display. |
| 4744 | * For focused handle, check if need to change and send a cancel event to previous one. |
| 4745 | * For removed handle, check if need to send a cancel event if already in touch. |
| 4746 | */ |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4747 | void InputDispatcher::setInputWindowsLocked( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4748 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4749 | if (DEBUG_FOCUS) { |
| 4750 | std::string windowList; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4751 | for (const sp<WindowInfoHandle>& iwh : windowInfoHandles) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4752 | windowList += iwh->getName() + " "; |
| 4753 | } |
| 4754 | ALOGD("setInputWindows displayId=%" PRId32 " %s", displayId, windowList.c_str()); |
| 4755 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4756 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4757 | // Check preconditions for new input windows |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4758 | for (const sp<WindowInfoHandle>& window : windowInfoHandles) { |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4759 | const WindowInfo& info = *window->getInfo(); |
| 4760 | |
| 4761 | // 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] | 4762 | const bool noInputWindow = info.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4763 | if (noInputWindow && window->getToken() != nullptr) { |
| 4764 | ALOGE("%s has feature NO_INPUT_WINDOW, but a non-null token. Clearing", |
| 4765 | window->getName().c_str()); |
| 4766 | window->releaseChannel(); |
| 4767 | } |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4768 | |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 4769 | // Ensure all spy windows are trusted overlays |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4770 | LOG_ALWAYS_FATAL_IF(info.isSpy() && |
| 4771 | !info.inputConfig.test( |
| 4772 | WindowInfo::InputConfig::TRUSTED_OVERLAY), |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 4773 | "%s has feature SPY, but is not a trusted overlay.", |
| 4774 | window->getName().c_str()); |
| 4775 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4776 | // Ensure all stylus interceptors are trusted overlays |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4777 | LOG_ALWAYS_FATAL_IF(info.interceptsStylus() && |
| 4778 | !info.inputConfig.test( |
| 4779 | WindowInfo::InputConfig::TRUSTED_OVERLAY), |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4780 | "%s has feature INTERCEPTS_STYLUS, but is not a trusted overlay.", |
| 4781 | window->getName().c_str()); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4782 | } |
| 4783 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4784 | // Copy old handles for release if they are no longer present. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4785 | const std::vector<sp<WindowInfoHandle>> oldWindowHandles = getWindowHandlesLocked(displayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4786 | |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4787 | // Save the old windows' orientation by ID before it gets updated. |
| 4788 | std::unordered_map<int32_t, uint32_t> oldWindowOrientations; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4789 | for (const sp<WindowInfoHandle>& handle : oldWindowHandles) { |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4790 | oldWindowOrientations.emplace(handle->getId(), |
| 4791 | handle->getInfo()->transform.getOrientation()); |
| 4792 | } |
| 4793 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4794 | updateWindowHandlesForDisplayLocked(windowInfoHandles, displayId); |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4795 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4796 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4797 | if (mLastHoverWindowHandle && |
| 4798 | std::find(windowHandles.begin(), windowHandles.end(), mLastHoverWindowHandle) == |
| 4799 | windowHandles.end()) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4800 | mLastHoverWindowHandle = nullptr; |
| 4801 | } |
| 4802 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4803 | std::optional<FocusResolver::FocusChanges> changes = |
| 4804 | mFocusResolver.setInputWindows(displayId, windowHandles); |
| 4805 | if (changes) { |
| 4806 | onFocusChangedLocked(*changes); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4807 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4808 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4809 | std::unordered_map<int32_t, TouchState>::iterator stateIt = |
| 4810 | mTouchStatesByDisplay.find(displayId); |
| 4811 | if (stateIt != mTouchStatesByDisplay.end()) { |
| 4812 | TouchState& state = stateIt->second; |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4813 | for (size_t i = 0; i < state.windows.size();) { |
| 4814 | TouchedWindow& touchedWindow = state.windows[i]; |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4815 | if (getWindowHandleLocked(touchedWindow.windowHandle) == nullptr) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4816 | if (DEBUG_FOCUS) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4817 | ALOGD("Touched window was removed: %s in display %" PRId32, |
| 4818 | touchedWindow.windowHandle->getName().c_str(), displayId); |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4819 | } |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4820 | std::shared_ptr<InputChannel> touchedInputChannel = |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4821 | getInputChannelLocked(touchedWindow.windowHandle->getToken()); |
| 4822 | if (touchedInputChannel != nullptr) { |
| 4823 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4824 | "touched window was removed"); |
| 4825 | synthesizeCancelationEventsForInputChannelLocked(touchedInputChannel, options); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4826 | // Since we are about to drop the touch, cancel the events for the wallpaper as |
| 4827 | // well. |
| 4828 | if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND && |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4829 | touchedWindow.windowHandle->getInfo()->inputConfig.test( |
| 4830 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER)) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4831 | sp<WindowInfoHandle> wallpaper = state.getWallpaperWindow(); |
| 4832 | if (wallpaper != nullptr) { |
| 4833 | sp<Connection> wallpaperConnection = |
| 4834 | getConnectionLocked(wallpaper->getToken()); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 4835 | if (wallpaperConnection != nullptr) { |
| 4836 | synthesizeCancelationEventsForConnectionLocked(wallpaperConnection, |
| 4837 | options); |
| 4838 | } |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4839 | } |
| 4840 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4841 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4842 | state.windows.erase(state.windows.begin() + i); |
| 4843 | } else { |
| 4844 | ++i; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4845 | } |
| 4846 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4847 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4848 | // 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] | 4849 | // could just clear the state here. |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 4850 | if (mDragState && mDragState->dragWindow->getInfo()->displayId == displayId && |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4851 | std::find(windowHandles.begin(), windowHandles.end(), mDragState->dragWindow) == |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4852 | windowHandles.end()) { |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 4853 | ALOGI("Drag window went away: %s", mDragState->dragWindow->getName().c_str()); |
| 4854 | sendDropWindowCommandLocked(nullptr, 0, 0); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4855 | mDragState.reset(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4856 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4857 | } |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4858 | |
Prabir Pradhan | 8b89c2f | 2021-07-29 16:30:14 +0000 | [diff] [blame] | 4859 | // Determine if the orientation of any of the input windows have changed, and cancel all |
| 4860 | // pointer events if necessary. |
| 4861 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
| 4862 | const sp<WindowInfoHandle> newWindowHandle = getWindowHandleLocked(oldWindowHandle); |
| 4863 | if (newWindowHandle != nullptr && |
| 4864 | newWindowHandle->getInfo()->transform.getOrientation() != |
| 4865 | oldWindowOrientations[oldWindowHandle->getId()]) { |
| 4866 | std::shared_ptr<InputChannel> inputChannel = |
| 4867 | getInputChannelLocked(newWindowHandle->getToken()); |
| 4868 | if (inputChannel != nullptr) { |
| 4869 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 4870 | "touched window's orientation changed"); |
| 4871 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4872 | } |
| 4873 | } |
| 4874 | } |
| 4875 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4876 | // Release information for windows that are no longer present. |
| 4877 | // This ensures that unused input channels are released promptly. |
| 4878 | // Otherwise, they might stick around until the window handle is destroyed |
| 4879 | // which might not happen until the next GC. |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4880 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { |
Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4881 | if (getWindowHandleLocked(oldWindowHandle) == nullptr) { |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4882 | if (DEBUG_FOCUS) { |
| 4883 | ALOGD("Window went away: %s", oldWindowHandle->getName().c_str()); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4884 | } |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4885 | oldWindowHandle->releaseChannel(); |
Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4886 | } |
chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 4887 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4888 | } |
| 4889 | |
| 4890 | void InputDispatcher::setFocusedApplication( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4891 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4892 | if (DEBUG_FOCUS) { |
| 4893 | ALOGD("setFocusedApplication displayId=%" PRId32 " %s", displayId, |
| 4894 | inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>"); |
| 4895 | } |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4896 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4897 | std::scoped_lock _l(mLock); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 4898 | setFocusedApplicationLocked(displayId, inputApplicationHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4899 | } // release lock |
| 4900 | |
| 4901 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4902 | mLooper->wake(); |
| 4903 | } |
| 4904 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 4905 | void InputDispatcher::setFocusedApplicationLocked( |
| 4906 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { |
| 4907 | std::shared_ptr<InputApplicationHandle> oldFocusedApplicationHandle = |
| 4908 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); |
| 4909 | |
| 4910 | if (sharedPointersEqual(oldFocusedApplicationHandle, inputApplicationHandle)) { |
| 4911 | return; // This application is already focused. No need to wake up or change anything. |
| 4912 | } |
| 4913 | |
| 4914 | // Set the new application handle. |
| 4915 | if (inputApplicationHandle != nullptr) { |
| 4916 | mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle; |
| 4917 | } else { |
| 4918 | mFocusedApplicationHandlesByDisplay.erase(displayId); |
| 4919 | } |
| 4920 | |
| 4921 | // No matter what the old focused application was, stop waiting on it because it is |
| 4922 | // no longer focused. |
| 4923 | resetNoFocusedWindowTimeoutLocked(); |
| 4924 | } |
| 4925 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4926 | /** |
| 4927 | * Sets the focused display, which is responsible for receiving focus-dispatched input events where |
| 4928 | * the display not specified. |
| 4929 | * |
| 4930 | * We track any unreleased events for each window. If a window loses the ability to receive the |
| 4931 | * released event, we will send a cancel event to it. So when the focused display is changed, we |
| 4932 | * cancel all the unreleased display-unspecified events for the focused window on the old focused |
| 4933 | * display. The display-specified events won't be affected. |
| 4934 | */ |
| 4935 | void InputDispatcher::setFocusedDisplay(int32_t displayId) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4936 | if (DEBUG_FOCUS) { |
| 4937 | ALOGD("setFocusedDisplay displayId=%" PRId32, displayId); |
| 4938 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4939 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4940 | std::scoped_lock _l(mLock); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4941 | |
| 4942 | if (mFocusedDisplayId != displayId) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4943 | sp<IBinder> oldFocusedWindowToken = |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4944 | mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4945 | if (oldFocusedWindowToken != nullptr) { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4946 | std::shared_ptr<InputChannel> inputChannel = |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4947 | getInputChannelLocked(oldFocusedWindowToken); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4948 | if (inputChannel != nullptr) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4949 | CancelationOptions |
| 4950 | options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 4951 | "The display which contains this window no longer has focus."); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4952 | options.displayId = ADISPLAY_ID_NONE; |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4953 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); |
| 4954 | } |
| 4955 | } |
| 4956 | mFocusedDisplayId = displayId; |
| 4957 | |
Chris Ye | 3c2d6f5 | 2020-08-09 10:39:48 -0700 | [diff] [blame] | 4958 | // Find new focused window and validate |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4959 | sp<IBinder> newFocusedWindowToken = mFocusResolver.getFocusedWindowToken(displayId); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 4960 | sendFocusChangedCommandLocked(oldFocusedWindowToken, newFocusedWindowToken); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4961 | |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4962 | if (newFocusedWindowToken == nullptr) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4963 | ALOGW("Focused display #%" PRId32 " does not have a focused window.", displayId); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4964 | if (mFocusResolver.hasFocusedWindowTokens()) { |
Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4965 | ALOGE("But another display has a focused window\n%s", |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4966 | mFocusResolver.dumpFocusedWindows().c_str()); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4967 | } |
| 4968 | } |
| 4969 | } |
| 4970 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4971 | if (DEBUG_FOCUS) { |
| 4972 | logDispatchStateLocked(); |
| 4973 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4974 | } // release lock |
| 4975 | |
| 4976 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 4977 | mLooper->wake(); |
| 4978 | } |
| 4979 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4980 | void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4981 | if (DEBUG_FOCUS) { |
| 4982 | ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); |
| 4983 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4984 | |
| 4985 | bool changed; |
| 4986 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4987 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4988 | |
| 4989 | if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { |
| 4990 | if (mDispatchFrozen && !frozen) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4991 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4992 | } |
| 4993 | |
| 4994 | if (mDispatchEnabled && !enabled) { |
| 4995 | resetAndDropEverythingLocked("dispatcher is being disabled"); |
| 4996 | } |
| 4997 | |
| 4998 | mDispatchEnabled = enabled; |
| 4999 | mDispatchFrozen = frozen; |
| 5000 | changed = true; |
| 5001 | } else { |
| 5002 | changed = false; |
| 5003 | } |
| 5004 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5005 | if (DEBUG_FOCUS) { |
| 5006 | logDispatchStateLocked(); |
| 5007 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5008 | } // release lock |
| 5009 | |
| 5010 | if (changed) { |
| 5011 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5012 | mLooper->wake(); |
| 5013 | } |
| 5014 | } |
| 5015 | |
| 5016 | void InputDispatcher::setInputFilterEnabled(bool enabled) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5017 | if (DEBUG_FOCUS) { |
| 5018 | ALOGD("setInputFilterEnabled: enabled=%d", enabled); |
| 5019 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5020 | |
| 5021 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5022 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5023 | |
| 5024 | if (mInputFilterEnabled == enabled) { |
| 5025 | return; |
| 5026 | } |
| 5027 | |
| 5028 | mInputFilterEnabled = enabled; |
| 5029 | resetAndDropEverythingLocked("input filter is being enabled or disabled"); |
| 5030 | } // release lock |
| 5031 | |
| 5032 | // Wake up poll loop since there might be work to do to drop everything. |
| 5033 | mLooper->wake(); |
| 5034 | } |
| 5035 | |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 5036 | bool InputDispatcher::setInTouchMode(bool inTouchMode, int32_t pid, int32_t uid, bool hasPermission, |
| 5037 | int32_t displayId) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5038 | bool needWake = false; |
| 5039 | { |
| 5040 | std::scoped_lock lock(mLock); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5041 | ALOGD_IF(DEBUG_TOUCH_MODE, |
| 5042 | "Request to change touch mode to %s (calling pid=%d, uid=%d, " |
| 5043 | "hasPermission=%s, target displayId=%d, mTouchModePerDisplay[displayId]=%s)", |
| 5044 | toString(inTouchMode), pid, uid, toString(hasPermission), displayId, |
| 5045 | mTouchModePerDisplay.count(displayId) == 0 |
| 5046 | ? "not set" |
| 5047 | : std::to_string(mTouchModePerDisplay[displayId]).c_str()); |
| 5048 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5049 | auto touchModeIt = mTouchModePerDisplay.find(displayId); |
| 5050 | if (touchModeIt != mTouchModePerDisplay.end() && touchModeIt->second == inTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5051 | return false; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5052 | } |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5053 | if (!hasPermission) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 5054 | if (!focusedWindowIsOwnedByLocked(pid, uid) && |
| 5055 | !recentWindowsAreOwnedByLocked(pid, uid)) { |
| 5056 | ALOGD("Touch mode switch rejected, caller (pid=%d, uid=%d) doesn't own the focused " |
| 5057 | "window nor none of the previously interacted window", |
| 5058 | pid, uid); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5059 | return false; |
| 5060 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5061 | } |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5062 | mTouchModePerDisplay[displayId] = inTouchMode; |
| 5063 | auto entry = std::make_unique<TouchModeEntry>(mIdGenerator.nextId(), now(), inTouchMode, |
| 5064 | displayId); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5065 | needWake = enqueueInboundEventLocked(std::move(entry)); |
| 5066 | } // release lock |
| 5067 | |
| 5068 | if (needWake) { |
| 5069 | mLooper->wake(); |
| 5070 | } |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5071 | return true; |
Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 5072 | } |
| 5073 | |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 5074 | bool InputDispatcher::focusedWindowIsOwnedByLocked(int32_t pid, int32_t uid) { |
| 5075 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
| 5076 | if (focusedToken == nullptr) { |
| 5077 | return false; |
| 5078 | } |
| 5079 | sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(focusedToken); |
| 5080 | return isWindowOwnedBy(windowHandle, pid, uid); |
| 5081 | } |
| 5082 | |
| 5083 | bool InputDispatcher::recentWindowsAreOwnedByLocked(int32_t pid, int32_t uid) { |
| 5084 | return std::find_if(mInteractionConnectionTokens.begin(), mInteractionConnectionTokens.end(), |
| 5085 | [&](const sp<IBinder>& connectionToken) REQUIRES(mLock) { |
| 5086 | const sp<WindowInfoHandle> windowHandle = |
| 5087 | getWindowHandleLocked(connectionToken); |
| 5088 | return isWindowOwnedBy(windowHandle, pid, uid); |
| 5089 | }) != mInteractionConnectionTokens.end(); |
| 5090 | } |
| 5091 | |
Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 5092 | void InputDispatcher::setMaximumObscuringOpacityForTouch(float opacity) { |
| 5093 | if (opacity < 0 || opacity > 1) { |
| 5094 | LOG_ALWAYS_FATAL("Maximum obscuring opacity for touch should be >= 0 and <= 1"); |
| 5095 | return; |
| 5096 | } |
| 5097 | |
| 5098 | std::scoped_lock lock(mLock); |
| 5099 | mMaximumObscuringOpacityForTouch = opacity; |
| 5100 | } |
| 5101 | |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5102 | std::tuple<TouchState*, TouchedWindow*, int32_t /*displayId*/> |
| 5103 | InputDispatcher::findTouchStateWindowAndDisplayLocked(const sp<IBinder>& token) { |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5104 | for (auto& [displayId, state] : mTouchStatesByDisplay) { |
| 5105 | for (TouchedWindow& w : state.windows) { |
| 5106 | if (w.windowHandle->getToken() == token) { |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5107 | return std::make_tuple(&state, &w, displayId); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5108 | } |
| 5109 | } |
| 5110 | } |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5111 | return std::make_tuple(nullptr, nullptr, ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5112 | } |
| 5113 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5114 | bool InputDispatcher::transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken, |
| 5115 | bool isDragDrop) { |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 5116 | if (fromToken == toToken) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5117 | if (DEBUG_FOCUS) { |
| 5118 | ALOGD("Trivial transfer to same window."); |
| 5119 | } |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 5120 | return true; |
| 5121 | } |
| 5122 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5123 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5124 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5125 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5126 | // Find the target touch state and touched window by fromToken. |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5127 | auto [state, touchedWindow, displayId] = findTouchStateWindowAndDisplayLocked(fromToken); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5128 | if (state == nullptr || touchedWindow == nullptr) { |
| 5129 | ALOGD("Focus transfer failed because from window is not being touched."); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5130 | return false; |
| 5131 | } |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5132 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5133 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(toToken, displayId); |
| 5134 | if (toWindowHandle == nullptr) { |
| 5135 | ALOGW("Cannot transfer focus because to window not found."); |
| 5136 | return false; |
| 5137 | } |
| 5138 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5139 | if (DEBUG_FOCUS) { |
| 5140 | ALOGD("transferTouchFocus: fromWindowHandle=%s, toWindowHandle=%s", |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5141 | touchedWindow->windowHandle->getName().c_str(), |
| 5142 | toWindowHandle->getName().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5143 | } |
| 5144 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5145 | // Erase old window. |
| 5146 | int32_t oldTargetFlags = touchedWindow->targetFlags; |
| 5147 | BitSet32 pointerIds = touchedWindow->pointerIds; |
| 5148 | state->removeWindowByToken(fromToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5149 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5150 | // Add new window. |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 5151 | nsecs_t downTimeInTarget = now(); |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 5152 | int32_t newTargetFlags = |
| 5153 | oldTargetFlags & (InputTarget::FLAG_SPLIT | InputTarget::FLAG_DISPATCH_AS_IS); |
| 5154 | if (canReceiveForegroundTouches(*toWindowHandle->getInfo())) { |
| 5155 | newTargetFlags |= InputTarget::FLAG_FOREGROUND; |
| 5156 | } |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 5157 | state->addOrUpdateWindow(toWindowHandle, newTargetFlags, pointerIds, downTimeInTarget); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5158 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5159 | // Store the dragging window. |
| 5160 | if (isDragDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 5161 | if (pointerIds.count() != 1) { |
| 5162 | ALOGW("The drag and drop cannot be started when there is no pointer or more than 1" |
| 5163 | " pointer on the window."); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 5164 | return false; |
| 5165 | } |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 5166 | // Track the pointer id for drag window and generate the drag state. |
| 5167 | const int32_t id = pointerIds.firstMarkedBit(); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 5168 | mDragState = std::make_unique<DragState>(toWindowHandle, id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5169 | } |
| 5170 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5171 | // Synthesize cancel for old window and down for new window. |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 5172 | sp<Connection> fromConnection = getConnectionLocked(fromToken); |
| 5173 | sp<Connection> toConnection = getConnectionLocked(toToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5174 | if (fromConnection != nullptr && toConnection != nullptr) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5175 | fromConnection->inputState.mergePointerStateTo(toConnection->inputState); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5176 | CancelationOptions |
| 5177 | options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 5178 | "transferring touch focus from this window to another window"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5179 | synthesizeCancelationEventsForConnectionLocked(fromConnection, options); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 5180 | synthesizePointerDownEventsForConnectionLocked(downTimeInTarget, toConnection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5181 | } |
| 5182 | |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5183 | if (DEBUG_FOCUS) { |
| 5184 | logDispatchStateLocked(); |
| 5185 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5186 | } // release lock |
| 5187 | |
| 5188 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 5189 | mLooper->wake(); |
| 5190 | return true; |
| 5191 | } |
| 5192 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5193 | /** |
| 5194 | * Get the touched foreground window on the given display. |
| 5195 | * Return null if there are no windows touched on that display, or if more than one foreground |
| 5196 | * window is being touched. |
| 5197 | */ |
| 5198 | sp<WindowInfoHandle> InputDispatcher::findTouchedForegroundWindowLocked(int32_t displayId) const { |
| 5199 | auto stateIt = mTouchStatesByDisplay.find(displayId); |
| 5200 | if (stateIt == mTouchStatesByDisplay.end()) { |
| 5201 | ALOGI("No touch state on display %" PRId32, displayId); |
| 5202 | return nullptr; |
| 5203 | } |
| 5204 | |
| 5205 | const TouchState& state = stateIt->second; |
| 5206 | sp<WindowInfoHandle> touchedForegroundWindow; |
| 5207 | // If multiple foreground windows are touched, return nullptr |
| 5208 | for (const TouchedWindow& window : state.windows) { |
| 5209 | if (window.targetFlags & InputTarget::FLAG_FOREGROUND) { |
| 5210 | if (touchedForegroundWindow != nullptr) { |
| 5211 | ALOGI("Two or more foreground windows: %s and %s", |
| 5212 | touchedForegroundWindow->getName().c_str(), |
| 5213 | window.windowHandle->getName().c_str()); |
| 5214 | return nullptr; |
| 5215 | } |
| 5216 | touchedForegroundWindow = window.windowHandle; |
| 5217 | } |
| 5218 | } |
| 5219 | return touchedForegroundWindow; |
| 5220 | } |
| 5221 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5222 | // Binder call |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5223 | bool InputDispatcher::transferTouch(const sp<IBinder>& destChannelToken, int32_t displayId) { |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5224 | sp<IBinder> fromToken; |
| 5225 | { // acquire lock |
| 5226 | std::scoped_lock _l(mLock); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5227 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(destChannelToken, displayId); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5228 | if (toWindowHandle == nullptr) { |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5229 | ALOGW("Could not find window associated with token=%p on display %" PRId32, |
| 5230 | destChannelToken.get(), displayId); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5231 | return false; |
| 5232 | } |
| 5233 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5234 | sp<WindowInfoHandle> from = findTouchedForegroundWindowLocked(displayId); |
| 5235 | if (from == nullptr) { |
| 5236 | ALOGE("Could not find a source window in %s for %p", __func__, destChannelToken.get()); |
| 5237 | return false; |
| 5238 | } |
| 5239 | |
| 5240 | fromToken = from->getToken(); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5241 | } // release lock |
| 5242 | |
| 5243 | return transferTouchFocus(fromToken, destChannelToken); |
| 5244 | } |
| 5245 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5246 | void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { |
Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5247 | if (DEBUG_FOCUS) { |
| 5248 | ALOGD("Resetting and dropping all events (%s).", reason); |
| 5249 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5250 | |
| 5251 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, reason); |
| 5252 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 5253 | |
| 5254 | resetKeyRepeatLocked(); |
| 5255 | releasePendingEventLocked(); |
| 5256 | drainInboundQueueLocked(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5257 | resetNoFocusedWindowTimeoutLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5258 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5259 | mAnrTracker.clear(); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5260 | mTouchStatesByDisplay.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5261 | mLastHoverWindowHandle.clear(); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5262 | mReplacedKeys.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5263 | } |
| 5264 | |
| 5265 | void InputDispatcher::logDispatchStateLocked() { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5266 | std::string dump; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5267 | dumpDispatchStateLocked(dump); |
| 5268 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5269 | std::istringstream stream(dump); |
| 5270 | std::string line; |
| 5271 | |
| 5272 | while (std::getline(stream, line, '\n')) { |
| 5273 | ALOGD("%s", line.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5274 | } |
| 5275 | } |
| 5276 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5277 | std::string InputDispatcher::dumpPointerCaptureStateLocked() { |
| 5278 | std::string dump; |
| 5279 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5280 | dump += StringPrintf(INDENT "Pointer Capture Requested: %s\n", |
| 5281 | toString(mCurrentPointerCaptureRequest.enable)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5282 | |
| 5283 | std::string windowName = "None"; |
| 5284 | if (mWindowTokenWithPointerCapture) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5285 | const sp<WindowInfoHandle> captureWindowHandle = |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5286 | getWindowHandleLocked(mWindowTokenWithPointerCapture); |
| 5287 | windowName = captureWindowHandle ? captureWindowHandle->getName().c_str() |
| 5288 | : "token has capture without window"; |
| 5289 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5290 | 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] | 5291 | |
| 5292 | return dump; |
| 5293 | } |
| 5294 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5295 | void InputDispatcher::dumpDispatchStateLocked(std::string& dump) { |
Siarhei Vishniakou | 043a3ec | 2019-05-01 11:30:46 -0700 | [diff] [blame] | 5296 | dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled)); |
| 5297 | dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen)); |
| 5298 | dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5299 | dump += StringPrintf(INDENT "FocusedDisplayId: %" PRId32 "\n", mFocusedDisplayId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5300 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5301 | if (!mFocusedApplicationHandlesByDisplay.empty()) { |
| 5302 | dump += StringPrintf(INDENT "FocusedApplications:\n"); |
| 5303 | for (auto& it : mFocusedApplicationHandlesByDisplay) { |
| 5304 | const int32_t displayId = it.first; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5305 | const std::shared_ptr<InputApplicationHandle>& applicationHandle = it.second; |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5306 | const std::chrono::duration timeout = |
| 5307 | applicationHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5308 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5309 | ", name='%s', dispatchingTimeout=%" PRId64 "ms\n", |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5310 | displayId, applicationHandle->getName().c_str(), millis(timeout)); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5311 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5312 | } else { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5313 | dump += StringPrintf(INDENT "FocusedApplications: <none>\n"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5314 | } |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5315 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5316 | dump += mFocusResolver.dump(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5317 | dump += dumpPointerCaptureStateLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5318 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5319 | if (!mTouchStatesByDisplay.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5320 | dump += StringPrintf(INDENT "TouchStatesByDisplay:\n"); |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5321 | for (const auto& [displayId, state] : mTouchStatesByDisplay) { |
| 5322 | dump += StringPrintf(INDENT2 "%d: deviceId=%d, source=0x%08x\n", displayId, |
Siarhei Vishniakou | 3ad385b | 2022-11-04 10:09:53 -0700 | [diff] [blame] | 5323 | state.deviceId, state.source); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5324 | if (!state.windows.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5325 | dump += INDENT3 "Windows:\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5326 | for (size_t i = 0; i < state.windows.size(); i++) { |
| 5327 | const TouchedWindow& touchedWindow = state.windows[i]; |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 5328 | dump += StringPrintf(INDENT4 "%zu: name='%s', pointerIds=0x%0x, " |
| 5329 | "targetFlags=0x%x, firstDownTimeInTarget=%" PRId64 |
| 5330 | "ms\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5331 | i, touchedWindow.windowHandle->getName().c_str(), |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 5332 | touchedWindow.pointerIds.value, touchedWindow.targetFlags, |
| 5333 | ns2ms(touchedWindow.firstDownTimeInTarget.value_or(0))); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5334 | } |
| 5335 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5336 | dump += INDENT3 "Windows: <none>\n"; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5337 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5338 | } |
| 5339 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5340 | dump += INDENT "TouchStates: <no displays touched>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5341 | } |
| 5342 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5343 | if (mDragState) { |
| 5344 | dump += StringPrintf(INDENT "DragState:\n"); |
| 5345 | mDragState->dump(dump, INDENT2); |
| 5346 | } |
| 5347 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5348 | if (!mWindowHandlesByDisplay.empty()) { |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5349 | for (const auto& [displayId, windowHandles] : mWindowHandlesByDisplay) { |
| 5350 | dump += StringPrintf(INDENT "Display: %" PRId32 "\n", displayId); |
| 5351 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { |
| 5352 | const auto& displayInfo = it->second; |
| 5353 | dump += StringPrintf(INDENT2 "logicalSize=%dx%d\n", displayInfo.logicalWidth, |
| 5354 | displayInfo.logicalHeight); |
| 5355 | displayInfo.transform.dump(dump, "transform", INDENT4); |
| 5356 | } else { |
| 5357 | dump += INDENT2 "No DisplayInfo found!\n"; |
| 5358 | } |
| 5359 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5360 | if (!windowHandles.empty()) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5361 | dump += INDENT2 "Windows:\n"; |
| 5362 | for (size_t i = 0; i < windowHandles.size(); i++) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5363 | const sp<WindowInfoHandle>& windowHandle = windowHandles[i]; |
| 5364 | const WindowInfo* windowInfo = windowHandle->getInfo(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5365 | |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5366 | dump += StringPrintf(INDENT3 "%zu: name='%s', id=%" PRId32 ", displayId=%d, " |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5367 | "inputConfig=%s, alpha=%.2f, " |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5368 | "frame=[%d,%d][%d,%d], globalScale=%f, " |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5369 | "applicationInfo.name=%s, " |
| 5370 | "applicationInfo.token=%s, " |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 5371 | "touchableRegion=", |
Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5372 | i, windowInfo->name.c_str(), windowInfo->id, |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5373 | windowInfo->displayId, |
| 5374 | windowInfo->inputConfig.string().c_str(), |
| 5375 | windowInfo->alpha, windowInfo->frameLeft, |
| 5376 | windowInfo->frameTop, windowInfo->frameRight, |
| 5377 | windowInfo->frameBottom, windowInfo->globalScaleFactor, |
Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5378 | windowInfo->applicationInfo.name.c_str(), |
| 5379 | toString(windowInfo->applicationInfo.token).c_str()); |
Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 5380 | dump += dumpRegion(windowInfo->touchableRegion); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5381 | dump += StringPrintf(", ownerPid=%d, ownerUid=%d, dispatchingTimeout=%" PRId64 |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5382 | "ms, hasToken=%s, " |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5383 | "touchOcclusionMode=%s\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5384 | windowInfo->ownerPid, windowInfo->ownerUid, |
Bernardo Rufino | c2f1fad | 2020-11-04 17:30:57 +0000 | [diff] [blame] | 5385 | millis(windowInfo->dispatchingTimeout), |
Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 5386 | toString(windowInfo->token != nullptr), |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5387 | toString(windowInfo->touchOcclusionMode).c_str()); |
chaviw | 85b4420 | 2020-07-24 11:46:21 -0700 | [diff] [blame] | 5388 | windowInfo->transform.dump(dump, "transform", INDENT4); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5389 | } |
| 5390 | } else { |
| 5391 | dump += INDENT2 "Windows: <none>\n"; |
| 5392 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5393 | } |
| 5394 | } else { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5395 | dump += INDENT "Displays: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5396 | } |
| 5397 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5398 | if (!mGlobalMonitorsByDisplay.empty()) { |
| 5399 | for (const auto& [displayId, monitors] : mGlobalMonitorsByDisplay) { |
| 5400 | dump += StringPrintf(INDENT "Global monitors on display %d:\n", displayId); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5401 | dumpMonitors(dump, monitors); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5402 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5403 | } else { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5404 | dump += INDENT "Global Monitors: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5405 | } |
| 5406 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5407 | const nsecs_t currentTime = now(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5408 | |
| 5409 | // Dump recently dispatched or dropped events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5410 | if (!mRecentQueue.empty()) { |
| 5411 | dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5412 | for (std::shared_ptr<EventEntry>& entry : mRecentQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5413 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5414 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5415 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5416 | } |
| 5417 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5418 | dump += INDENT "RecentQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5419 | } |
| 5420 | |
| 5421 | // Dump event currently being dispatched. |
| 5422 | if (mPendingEvent) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5423 | dump += INDENT "PendingEvent:\n"; |
| 5424 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5425 | dump += mPendingEvent->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5426 | dump += StringPrintf(", age=%" PRId64 "ms\n", |
| 5427 | ns2ms(currentTime - mPendingEvent->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5428 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5429 | dump += INDENT "PendingEvent: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5430 | } |
| 5431 | |
| 5432 | // Dump inbound events from oldest to newest. |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5433 | if (!mInboundQueue.empty()) { |
| 5434 | dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size()); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5435 | for (std::shared_ptr<EventEntry>& entry : mInboundQueue) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5436 | dump += INDENT2; |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5437 | dump += entry->getDescription(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5438 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5439 | } |
| 5440 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5441 | dump += INDENT "InboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5442 | } |
| 5443 | |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5444 | if (!mReplacedKeys.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5445 | dump += INDENT "ReplacedKeys:\n"; |
Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5446 | for (const std::pair<KeyReplacement, int32_t>& pair : mReplacedKeys) { |
| 5447 | const KeyReplacement& replacement = pair.first; |
| 5448 | int32_t newKeyCode = pair.second; |
| 5449 | dump += StringPrintf(INDENT2 "originalKeyCode=%d, deviceId=%d -> newKeyCode=%d\n", |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5450 | replacement.keyCode, replacement.deviceId, newKeyCode); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5451 | } |
| 5452 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5453 | dump += INDENT "ReplacedKeys: <empty>\n"; |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5454 | } |
| 5455 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5456 | if (!mCommandQueue.empty()) { |
| 5457 | dump += StringPrintf(INDENT "CommandQueue: size=%zu\n", mCommandQueue.size()); |
| 5458 | } else { |
| 5459 | dump += INDENT "CommandQueue: <empty>\n"; |
| 5460 | } |
| 5461 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5462 | if (!mConnectionsByToken.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5463 | dump += INDENT "Connections:\n"; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5464 | for (const auto& [token, connection] : mConnectionsByToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5465 | dump += StringPrintf(INDENT2 "%i: channelName='%s', windowName='%s', " |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5466 | "status=%s, monitor=%s, responsive=%s\n", |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5467 | connection->inputChannel->getFd().get(), |
| 5468 | connection->getInputChannelName().c_str(), |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5469 | connection->getWindowName().c_str(), |
| 5470 | ftl::enum_string(connection->status).c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5471 | toString(connection->monitor), toString(connection->responsive)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5472 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5473 | if (!connection->outboundQueue.empty()) { |
| 5474 | dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n", |
| 5475 | connection->outboundQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5476 | dump += dumpQueue(connection->outboundQueue, currentTime); |
| 5477 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5478 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5479 | dump += INDENT3 "OutboundQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5480 | } |
| 5481 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5482 | if (!connection->waitQueue.empty()) { |
| 5483 | dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n", |
| 5484 | connection->waitQueue.size()); |
Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5485 | dump += dumpQueue(connection->waitQueue, currentTime); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5486 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5487 | dump += INDENT3 "WaitQueue: <empty>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5488 | } |
| 5489 | } |
| 5490 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5491 | dump += INDENT "Connections: <none>\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5492 | } |
| 5493 | |
| 5494 | if (isAppSwitchPendingLocked()) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5495 | dump += StringPrintf(INDENT "AppSwitch: pending, due in %" PRId64 "ms\n", |
| 5496 | ns2ms(mAppSwitchDueTime - now())); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5497 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5498 | dump += INDENT "AppSwitch: not pending\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5499 | } |
| 5500 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 5501 | if (!mTouchModePerDisplay.empty()) { |
| 5502 | dump += INDENT "TouchModePerDisplay:\n"; |
| 5503 | for (const auto& [displayId, touchMode] : mTouchModePerDisplay) { |
| 5504 | dump += StringPrintf(INDENT2 "Display: %" PRId32 " TouchMode: %s\n", displayId, |
| 5505 | std::to_string(touchMode).c_str()); |
| 5506 | } |
| 5507 | } else { |
| 5508 | dump += INDENT "TouchModePerDisplay: <none>\n"; |
| 5509 | } |
| 5510 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5511 | dump += INDENT "Configuration:\n"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5512 | dump += StringPrintf(INDENT2 "KeyRepeatDelay: %" PRId64 "ms\n", ns2ms(mConfig.keyRepeatDelay)); |
| 5513 | dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n", |
| 5514 | ns2ms(mConfig.keyRepeatTimeout)); |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 5515 | dump += mLatencyTracker.dump(INDENT2); |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 5516 | dump += mLatencyAggregator.dump(INDENT2); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5517 | } |
| 5518 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5519 | void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) { |
| 5520 | const size_t numMonitors = monitors.size(); |
| 5521 | for (size_t i = 0; i < numMonitors; i++) { |
| 5522 | const Monitor& monitor = monitors[i]; |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5523 | const std::shared_ptr<InputChannel>& channel = monitor.inputChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5524 | dump += StringPrintf(INDENT2 "%zu: '%s', ", i, channel->getName().c_str()); |
| 5525 | dump += "\n"; |
| 5526 | } |
| 5527 | } |
| 5528 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5529 | class LooperEventCallback : public LooperCallback { |
| 5530 | public: |
| 5531 | LooperEventCallback(std::function<int(int events)> callback) : mCallback(callback) {} |
| 5532 | int handleEvent(int /*fd*/, int events, void* /*data*/) override { return mCallback(events); } |
| 5533 | |
| 5534 | private: |
| 5535 | std::function<int(int events)> mCallback; |
| 5536 | }; |
| 5537 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5538 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputChannel(const std::string& name) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5539 | if (DEBUG_CHANNEL_CREATION) { |
| 5540 | ALOGD("channel '%s' ~ createInputChannel", name.c_str()); |
| 5541 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5542 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5543 | std::unique_ptr<InputChannel> serverChannel; |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5544 | std::unique_ptr<InputChannel> clientChannel; |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5545 | status_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5546 | |
| 5547 | if (result) { |
| 5548 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5549 | } |
| 5550 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5551 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5552 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5553 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5554 | int fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5555 | sp<Connection> connection = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5556 | sp<Connection>::make(std::move(serverChannel), false /*monitor*/, mIdGenerator); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5557 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5558 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5559 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5560 | } |
| 5561 | mConnectionsByToken.emplace(token, connection); |
| 5562 | |
| 5563 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5564 | this, std::placeholders::_1, token); |
| 5565 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5566 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, sp<LooperEventCallback>::make(callback), |
| 5567 | nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5568 | } // release lock |
| 5569 | |
| 5570 | // Wake the looper because some connections have changed. |
| 5571 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5572 | return clientChannel; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5573 | } |
| 5574 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5575 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputMonitor(int32_t displayId, |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5576 | const std::string& name, |
| 5577 | int32_t pid) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5578 | std::shared_ptr<InputChannel> serverChannel; |
| 5579 | std::unique_ptr<InputChannel> clientChannel; |
| 5580 | status_t result = openInputChannelPair(name, serverChannel, clientChannel); |
| 5581 | if (result) { |
| 5582 | return base::Error(result) << "Failed to open input channel pair with name " << name; |
| 5583 | } |
| 5584 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5585 | { // acquire lock |
| 5586 | std::scoped_lock _l(mLock); |
| 5587 | |
| 5588 | if (displayId < 0) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5589 | return base::Error(BAD_VALUE) << "Attempted to create input monitor with name " << name |
| 5590 | << " without a specified display."; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5591 | } |
| 5592 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5593 | sp<Connection> connection = |
| 5594 | sp<Connection>::make(serverChannel, true /*monitor*/, mIdGenerator); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5595 | const sp<IBinder>& token = serverChannel->getConnectionToken(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5596 | const int fd = serverChannel->getFd(); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5597 | |
| 5598 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { |
| 5599 | ALOGE("Created a new connection, but the token %p is already known", token.get()); |
| 5600 | } |
| 5601 | mConnectionsByToken.emplace(token, connection); |
| 5602 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, |
| 5603 | this, std::placeholders::_1, token); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5604 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5605 | mGlobalMonitorsByDisplay[displayId].emplace_back(serverChannel, pid); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5606 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5607 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, sp<LooperEventCallback>::make(callback), |
| 5608 | nullptr); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5609 | } |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5610 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5611 | // Wake the looper because some connections have changed. |
| 5612 | mLooper->wake(); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5613 | return clientChannel; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5614 | } |
| 5615 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5616 | status_t InputDispatcher::removeInputChannel(const sp<IBinder>& connectionToken) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5617 | { // acquire lock |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5618 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5619 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5620 | status_t status = removeInputChannelLocked(connectionToken, false /*notify*/); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5621 | if (status) { |
| 5622 | return status; |
| 5623 | } |
| 5624 | } // release lock |
| 5625 | |
| 5626 | // Wake the poll loop because removing the connection may have changed the current |
| 5627 | // synchronization state. |
| 5628 | mLooper->wake(); |
| 5629 | return OK; |
| 5630 | } |
| 5631 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5632 | status_t InputDispatcher::removeInputChannelLocked(const sp<IBinder>& connectionToken, |
| 5633 | bool notify) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5634 | sp<Connection> connection = getConnectionLocked(connectionToken); |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5635 | if (connection == nullptr) { |
Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5636 | // 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] | 5637 | return BAD_VALUE; |
| 5638 | } |
| 5639 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5640 | removeConnectionLocked(connection); |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 5641 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5642 | if (connection->monitor) { |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5643 | removeMonitorChannelLocked(connectionToken); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5644 | } |
| 5645 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5646 | mLooper->removeFd(connection->inputChannel->getFd()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5647 | |
| 5648 | nsecs_t currentTime = now(); |
| 5649 | abortBrokenDispatchCycleLocked(currentTime, connection, notify); |
| 5650 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5651 | connection->status = Connection::Status::ZOMBIE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5652 | return OK; |
| 5653 | } |
| 5654 | |
Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5655 | void InputDispatcher::removeMonitorChannelLocked(const sp<IBinder>& connectionToken) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5656 | for (auto it = mGlobalMonitorsByDisplay.begin(); it != mGlobalMonitorsByDisplay.end();) { |
| 5657 | auto& [displayId, monitors] = *it; |
| 5658 | std::erase_if(monitors, [connectionToken](const Monitor& monitor) { |
| 5659 | return monitor.inputChannel->getConnectionToken() == connectionToken; |
| 5660 | }); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5661 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5662 | if (monitors.empty()) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5663 | it = mGlobalMonitorsByDisplay.erase(it); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5664 | } else { |
| 5665 | ++it; |
| 5666 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5667 | } |
| 5668 | } |
| 5669 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5670 | status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5671 | std::scoped_lock _l(mLock); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 5672 | return pilferPointersLocked(token); |
| 5673 | } |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5674 | |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 5675 | status_t InputDispatcher::pilferPointersLocked(const sp<IBinder>& token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5676 | const std::shared_ptr<InputChannel> requestingChannel = getInputChannelLocked(token); |
| 5677 | if (!requestingChannel) { |
| 5678 | ALOGW("Attempted to pilfer pointers from an un-registered channel or invalid token"); |
| 5679 | return BAD_VALUE; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5680 | } |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5681 | |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5682 | auto [statePtr, windowPtr, displayId] = findTouchStateWindowAndDisplayLocked(token); |
Siarhei Vishniakou | 3ad385b | 2022-11-04 10:09:53 -0700 | [diff] [blame] | 5683 | if (statePtr == nullptr || windowPtr == nullptr || windowPtr->pointerIds.isEmpty()) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5684 | ALOGW("Attempted to pilfer points from a channel without any on-going pointer streams." |
| 5685 | " Ignoring."); |
| 5686 | return BAD_VALUE; |
| 5687 | } |
| 5688 | |
| 5689 | TouchState& state = *statePtr; |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 5690 | TouchedWindow& window = *windowPtr; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5691 | // Send cancel events to all the input channels we're stealing from. |
| 5692 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 5693 | "input channel stole pointer stream"); |
| 5694 | options.deviceId = state.deviceId; |
Siarhei Vishniakou | 40b8fbd | 2022-11-04 10:50:26 -0700 | [diff] [blame] | 5695 | options.displayId = displayId; |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 5696 | options.pointerIds = window.pointerIds; |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5697 | std::string canceledWindows; |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 5698 | for (const TouchedWindow& w : state.windows) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5699 | const std::shared_ptr<InputChannel> channel = |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 5700 | getInputChannelLocked(w.windowHandle->getToken()); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5701 | if (channel != nullptr && channel->getConnectionToken() != token) { |
| 5702 | synthesizeCancelationEventsForInputChannelLocked(channel, options); |
| 5703 | canceledWindows += canceledWindows.empty() ? "[" : ", "; |
| 5704 | canceledWindows += channel->getName(); |
| 5705 | } |
| 5706 | } |
| 5707 | canceledWindows += canceledWindows.empty() ? "[]" : "]"; |
| 5708 | ALOGI("Channel %s is stealing touch from %s", requestingChannel->getName().c_str(), |
| 5709 | canceledWindows.c_str()); |
| 5710 | |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 5711 | // Prevent the gesture from being sent to any other windows. |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 5712 | // This only blocks relevant pointers to be sent to other windows |
| 5713 | window.isPilferingPointers = true; |
| 5714 | |
Siarhei Vishniakou | 64a9853 | 2022-10-25 15:20:24 -0700 | [diff] [blame] | 5715 | state.cancelPointersForWindowsExcept(window.pointerIds, token); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5716 | return OK; |
| 5717 | } |
| 5718 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5719 | void InputDispatcher::requestPointerCapture(const sp<IBinder>& windowToken, bool enabled) { |
| 5720 | { // acquire lock |
| 5721 | std::scoped_lock _l(mLock); |
| 5722 | if (DEBUG_FOCUS) { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5723 | const sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(windowToken); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5724 | ALOGI("Request to %s Pointer Capture from: %s.", enabled ? "enable" : "disable", |
| 5725 | windowHandle != nullptr ? windowHandle->getName().c_str() |
| 5726 | : "token without window"); |
| 5727 | } |
| 5728 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5729 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5730 | if (focusedToken != windowToken) { |
| 5731 | ALOGW("Ignoring request to %s Pointer Capture: window does not have focus.", |
| 5732 | enabled ? "enable" : "disable"); |
| 5733 | return; |
| 5734 | } |
| 5735 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5736 | if (enabled == mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5737 | ALOGW("Ignoring request to %s Pointer Capture: " |
| 5738 | "window has %s requested pointer capture.", |
| 5739 | enabled ? "enable" : "disable", enabled ? "already" : "not"); |
| 5740 | return; |
| 5741 | } |
| 5742 | |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 5743 | if (enabled) { |
| 5744 | if (std::find(mIneligibleDisplaysForPointerCapture.begin(), |
| 5745 | mIneligibleDisplaysForPointerCapture.end(), |
| 5746 | mFocusedDisplayId) != mIneligibleDisplaysForPointerCapture.end()) { |
| 5747 | ALOGW("Ignoring request to enable Pointer Capture: display is not eligible"); |
| 5748 | return; |
| 5749 | } |
| 5750 | } |
| 5751 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5752 | setPointerCaptureLocked(enabled); |
| 5753 | } // release lock |
| 5754 | |
| 5755 | // Wake the thread to process command entries. |
| 5756 | mLooper->wake(); |
| 5757 | } |
| 5758 | |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 5759 | void InputDispatcher::setDisplayEligibilityForPointerCapture(int32_t displayId, bool isEligible) { |
| 5760 | { // acquire lock |
| 5761 | std::scoped_lock _l(mLock); |
| 5762 | std::erase(mIneligibleDisplaysForPointerCapture, displayId); |
| 5763 | if (!isEligible) { |
| 5764 | mIneligibleDisplaysForPointerCapture.push_back(displayId); |
| 5765 | } |
| 5766 | } // release lock |
| 5767 | } |
| 5768 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5769 | std::optional<int32_t> InputDispatcher::findMonitorPidByTokenLocked(const sp<IBinder>& token) { |
| 5770 | for (const auto& [_, monitors] : mGlobalMonitorsByDisplay) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5771 | for (const Monitor& monitor : monitors) { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5772 | if (monitor.inputChannel->getConnectionToken() == token) { |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5773 | return monitor.pid; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5774 | } |
| 5775 | } |
| 5776 | } |
| 5777 | return std::nullopt; |
| 5778 | } |
| 5779 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5780 | sp<Connection> InputDispatcher::getConnectionLocked(const sp<IBinder>& inputConnectionToken) const { |
Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 5781 | if (inputConnectionToken == nullptr) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5782 | return nullptr; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 5783 | } |
| 5784 | |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5785 | for (const auto& [token, connection] : mConnectionsByToken) { |
| 5786 | if (token == inputConnectionToken) { |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5787 | return connection; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5788 | } |
| 5789 | } |
Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 5790 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5791 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5792 | } |
| 5793 | |
Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 5794 | std::string InputDispatcher::getConnectionNameLocked(const sp<IBinder>& connectionToken) const { |
| 5795 | sp<Connection> connection = getConnectionLocked(connectionToken); |
| 5796 | if (connection == nullptr) { |
| 5797 | return "<nullptr>"; |
| 5798 | } |
| 5799 | return connection->getInputChannelName(); |
| 5800 | } |
| 5801 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5802 | void InputDispatcher::removeConnectionLocked(const sp<Connection>& connection) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5803 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5804 | mConnectionsByToken.erase(connection->inputChannel->getConnectionToken()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5805 | } |
| 5806 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5807 | void InputDispatcher::doDispatchCycleFinishedCommand(nsecs_t finishTime, |
| 5808 | const sp<Connection>& connection, uint32_t seq, |
| 5809 | bool handled, nsecs_t consumeTime) { |
| 5810 | // Handle post-event policy actions. |
| 5811 | std::deque<DispatchEntry*>::iterator dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 5812 | if (dispatchEntryIt == connection->waitQueue.end()) { |
| 5813 | return; |
| 5814 | } |
| 5815 | DispatchEntry* dispatchEntry = *dispatchEntryIt; |
| 5816 | const nsecs_t eventDuration = finishTime - dispatchEntry->deliveryTime; |
| 5817 | if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) { |
| 5818 | ALOGI("%s spent %" PRId64 "ms processing %s", connection->getWindowName().c_str(), |
| 5819 | ns2ms(eventDuration), dispatchEntry->eventEntry->getDescription().c_str()); |
| 5820 | } |
| 5821 | if (shouldReportFinishedEvent(*dispatchEntry, *connection)) { |
| 5822 | mLatencyTracker.trackFinishedEvent(dispatchEntry->eventEntry->id, |
| 5823 | connection->inputChannel->getConnectionToken(), |
| 5824 | dispatchEntry->deliveryTime, consumeTime, finishTime); |
| 5825 | } |
| 5826 | |
| 5827 | bool restartEvent; |
| 5828 | if (dispatchEntry->eventEntry->type == EventEntry::Type::KEY) { |
| 5829 | KeyEntry& keyEntry = static_cast<KeyEntry&>(*(dispatchEntry->eventEntry)); |
| 5830 | restartEvent = |
| 5831 | afterKeyEventLockedInterruptable(connection, dispatchEntry, keyEntry, handled); |
| 5832 | } else if (dispatchEntry->eventEntry->type == EventEntry::Type::MOTION) { |
| 5833 | MotionEntry& motionEntry = static_cast<MotionEntry&>(*(dispatchEntry->eventEntry)); |
| 5834 | restartEvent = afterMotionEventLockedInterruptable(connection, dispatchEntry, motionEntry, |
| 5835 | handled); |
| 5836 | } else { |
| 5837 | restartEvent = false; |
| 5838 | } |
| 5839 | |
| 5840 | // Dequeue the event and start the next cycle. |
| 5841 | // Because the lock might have been released, it is possible that the |
| 5842 | // contents of the wait queue to have been drained, so we need to double-check |
| 5843 | // a few things. |
| 5844 | dispatchEntryIt = connection->findWaitQueueEntry(seq); |
| 5845 | if (dispatchEntryIt != connection->waitQueue.end()) { |
| 5846 | dispatchEntry = *dispatchEntryIt; |
| 5847 | connection->waitQueue.erase(dispatchEntryIt); |
| 5848 | const sp<IBinder>& connectionToken = connection->inputChannel->getConnectionToken(); |
| 5849 | mAnrTracker.erase(dispatchEntry->timeoutTime, connectionToken); |
| 5850 | if (!connection->responsive) { |
| 5851 | connection->responsive = isConnectionResponsive(*connection); |
| 5852 | if (connection->responsive) { |
| 5853 | // The connection was unresponsive, and now it's responsive. |
| 5854 | processConnectionResponsiveLocked(*connection); |
| 5855 | } |
| 5856 | } |
| 5857 | traceWaitQueueLength(*connection); |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5858 | if (restartEvent && connection->status == Connection::Status::NORMAL) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5859 | connection->outboundQueue.push_front(dispatchEntry); |
| 5860 | traceOutboundQueueLength(*connection); |
| 5861 | } else { |
| 5862 | releaseDispatchEntry(dispatchEntry); |
| 5863 | } |
| 5864 | } |
| 5865 | |
| 5866 | // Start the next dispatch cycle for this connection. |
| 5867 | startDispatchCycleLocked(now(), connection); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5868 | } |
| 5869 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5870 | void InputDispatcher::sendFocusChangedCommandLocked(const sp<IBinder>& oldToken, |
| 5871 | const sp<IBinder>& newToken) { |
| 5872 | auto command = [this, oldToken, newToken]() REQUIRES(mLock) { |
| 5873 | scoped_unlock unlock(mLock); |
| 5874 | mPolicy->notifyFocusChanged(oldToken, newToken); |
| 5875 | }; |
| 5876 | postCommandLocked(std::move(command)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5877 | } |
| 5878 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5879 | void InputDispatcher::sendDropWindowCommandLocked(const sp<IBinder>& token, float x, float y) { |
| 5880 | auto command = [this, token, x, y]() REQUIRES(mLock) { |
| 5881 | scoped_unlock unlock(mLock); |
| 5882 | mPolicy->notifyDropWindow(token, x, y); |
| 5883 | }; |
| 5884 | postCommandLocked(std::move(command)); |
Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5885 | } |
| 5886 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5887 | void InputDispatcher::onAnrLocked(const sp<Connection>& connection) { |
| 5888 | if (connection == nullptr) { |
| 5889 | LOG_ALWAYS_FATAL("Caller must check for nullness"); |
| 5890 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5891 | // Since we are allowing the policy to extend the timeout, maybe the waitQueue |
| 5892 | // is already healthy again. Don't raise ANR in this situation |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5893 | if (connection->waitQueue.empty()) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5894 | ALOGI("Not raising ANR because the connection %s has recovered", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5895 | connection->inputChannel->getName().c_str()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5896 | return; |
| 5897 | } |
| 5898 | /** |
| 5899 | * The "oldestEntry" is the entry that was first sent to the application. That entry, however, |
| 5900 | * may not be the one that caused the timeout to occur. One possibility is that window timeout |
| 5901 | * has changed. This could cause newer entries to time out before the already dispatched |
| 5902 | * entries. In that situation, the newest entries caused ANR. But in all likelihood, the app |
| 5903 | * processes the events linearly. So providing information about the oldest entry seems to be |
| 5904 | * most useful. |
| 5905 | */ |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5906 | DispatchEntry* oldestEntry = *connection->waitQueue.begin(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5907 | const nsecs_t currentWait = now() - oldestEntry->deliveryTime; |
| 5908 | std::string reason = |
| 5909 | android::base::StringPrintf("%s is not responding. Waited %" PRId64 "ms for %s", |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5910 | connection->inputChannel->getName().c_str(), |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5911 | ns2ms(currentWait), |
| 5912 | oldestEntry->eventEntry->getDescription().c_str()); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5913 | sp<IBinder> connectionToken = connection->inputChannel->getConnectionToken(); |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 5914 | updateLastAnrStateLocked(getWindowHandleLocked(connectionToken), reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5915 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5916 | processConnectionUnresponsiveLocked(*connection, std::move(reason)); |
| 5917 | |
| 5918 | // Stop waking up for events on this connection, it is already unresponsive |
| 5919 | cancelEventsForAnrLocked(connection); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5920 | } |
| 5921 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5922 | void InputDispatcher::onAnrLocked(std::shared_ptr<InputApplicationHandle> application) { |
| 5923 | std::string reason = |
| 5924 | StringPrintf("%s does not have a focused window", application->getName().c_str()); |
| 5925 | updateLastAnrStateLocked(*application, reason); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5926 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5927 | auto command = [this, application = std::move(application)]() REQUIRES(mLock) { |
| 5928 | scoped_unlock unlock(mLock); |
| 5929 | mPolicy->notifyNoFocusedWindowAnr(application); |
| 5930 | }; |
| 5931 | postCommandLocked(std::move(command)); |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 5932 | } |
| 5933 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5934 | void InputDispatcher::updateLastAnrStateLocked(const sp<WindowInfoHandle>& window, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5935 | const std::string& reason) { |
| 5936 | const std::string windowLabel = getApplicationWindowLabel(nullptr, window); |
| 5937 | updateLastAnrStateLocked(windowLabel, reason); |
| 5938 | } |
| 5939 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5940 | void InputDispatcher::updateLastAnrStateLocked(const InputApplicationHandle& application, |
| 5941 | const std::string& reason) { |
| 5942 | const std::string windowLabel = getApplicationWindowLabel(&application, nullptr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5943 | updateLastAnrStateLocked(windowLabel, reason); |
| 5944 | } |
| 5945 | |
| 5946 | void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel, |
| 5947 | const std::string& reason) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5948 | // 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] | 5949 | time_t t = time(nullptr); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5950 | struct tm tm; |
| 5951 | localtime_r(&t, &tm); |
| 5952 | char timestr[64]; |
| 5953 | strftime(timestr, sizeof(timestr), "%F %T", &tm); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5954 | mLastAnrState.clear(); |
| 5955 | mLastAnrState += INDENT "ANR:\n"; |
| 5956 | mLastAnrState += StringPrintf(INDENT2 "Time: %s\n", timestr); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5957 | mLastAnrState += StringPrintf(INDENT2 "Reason: %s\n", reason.c_str()); |
| 5958 | mLastAnrState += StringPrintf(INDENT2 "Window: %s\n", windowLabel.c_str()); |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5959 | dumpDispatchStateLocked(mLastAnrState); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5960 | } |
| 5961 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5962 | void InputDispatcher::doInterceptKeyBeforeDispatchingCommand(const sp<IBinder>& focusedWindowToken, |
| 5963 | KeyEntry& entry) { |
| 5964 | const KeyEvent event = createKeyEvent(entry); |
| 5965 | nsecs_t delay = 0; |
| 5966 | { // release lock |
| 5967 | scoped_unlock unlock(mLock); |
| 5968 | android::base::Timer t; |
| 5969 | delay = mPolicy->interceptKeyBeforeDispatching(focusedWindowToken, &event, |
| 5970 | entry.policyFlags); |
| 5971 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { |
| 5972 | ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms", |
| 5973 | std::to_string(t.duration().count()).c_str()); |
| 5974 | } |
| 5975 | } // acquire lock |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5976 | |
| 5977 | if (delay < 0) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5978 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_SKIP; |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5979 | } else if (delay == 0) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5980 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5981 | } else { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5982 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER; |
| 5983 | entry.interceptKeyWakeupTime = now() + delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5984 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5985 | } |
| 5986 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5987 | void InputDispatcher::sendWindowUnresponsiveCommandLocked(const sp<IBinder>& token, |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5988 | std::optional<int32_t> pid, |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5989 | std::string reason) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5990 | auto command = [this, token, pid, reason = std::move(reason)]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5991 | scoped_unlock unlock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5992 | mPolicy->notifyWindowUnresponsive(token, pid, reason); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5993 | }; |
| 5994 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5995 | } |
| 5996 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5997 | void InputDispatcher::sendWindowResponsiveCommandLocked(const sp<IBinder>& token, |
| 5998 | std::optional<int32_t> pid) { |
| 5999 | auto command = [this, token, pid]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6000 | scoped_unlock unlock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6001 | mPolicy->notifyWindowResponsive(token, pid); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6002 | }; |
| 6003 | postCommandLocked(std::move(command)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6004 | } |
| 6005 | |
| 6006 | /** |
| 6007 | * Tell the policy that a connection has become unresponsive so that it can start ANR. |
| 6008 | * Check whether the connection of interest is a monitor or a window, and add the corresponding |
| 6009 | * command entry to the command queue. |
| 6010 | */ |
| 6011 | void InputDispatcher::processConnectionUnresponsiveLocked(const Connection& connection, |
| 6012 | std::string reason) { |
| 6013 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6014 | std::optional<int32_t> pid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6015 | if (connection.monitor) { |
| 6016 | ALOGW("Monitor %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 6017 | reason.c_str()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6018 | pid = findMonitorPidByTokenLocked(connectionToken); |
| 6019 | } else { |
| 6020 | // The connection is a window |
| 6021 | ALOGW("Window %s is unresponsive: %s", connection.inputChannel->getName().c_str(), |
| 6022 | reason.c_str()); |
| 6023 | const sp<WindowInfoHandle> handle = getWindowHandleLocked(connectionToken); |
| 6024 | if (handle != nullptr) { |
| 6025 | pid = handle->getInfo()->ownerPid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6026 | } |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6027 | } |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6028 | sendWindowUnresponsiveCommandLocked(connectionToken, pid, std::move(reason)); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6029 | } |
| 6030 | |
| 6031 | /** |
| 6032 | * Tell the policy that a connection has become responsive so that it can stop ANR. |
| 6033 | */ |
| 6034 | void InputDispatcher::processConnectionResponsiveLocked(const Connection& connection) { |
| 6035 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6036 | std::optional<int32_t> pid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6037 | if (connection.monitor) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6038 | pid = findMonitorPidByTokenLocked(connectionToken); |
| 6039 | } else { |
| 6040 | // The connection is a window |
| 6041 | const sp<WindowInfoHandle> handle = getWindowHandleLocked(connectionToken); |
| 6042 | if (handle != nullptr) { |
| 6043 | pid = handle->getInfo()->ownerPid; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6044 | } |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6045 | } |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6046 | sendWindowResponsiveCommandLocked(connectionToken, pid); |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 6047 | } |
| 6048 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6049 | bool InputDispatcher::afterKeyEventLockedInterruptable(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6050 | DispatchEntry* dispatchEntry, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6051 | KeyEntry& keyEntry, bool handled) { |
| 6052 | if (keyEntry.flags & AKEY_EVENT_FLAG_FALLBACK) { |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6053 | if (!handled) { |
| 6054 | // Report the key as unhandled, since the fallback was not handled. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6055 | mReporter->reportUnhandledKey(keyEntry.id); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6056 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6057 | return false; |
| 6058 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6059 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6060 | // Get the fallback key state. |
| 6061 | // Clear it out after dispatching the UP. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6062 | int32_t originalKeyCode = keyEntry.keyCode; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6063 | int32_t fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode); |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6064 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6065 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6066 | } |
| 6067 | |
| 6068 | if (handled || !dispatchEntry->hasForegroundTarget()) { |
| 6069 | // If the application handles the original key for which we previously |
| 6070 | // generated a fallback or if the window is not a foreground window, |
| 6071 | // then cancel the associated fallback key, if any. |
| 6072 | if (fallbackKeyCode != -1) { |
| 6073 | // Dispatch the unhandled key to the policy with the cancel flag. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6074 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6075 | ALOGD("Unhandled key event: Asking policy to cancel fallback action. " |
| 6076 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6077 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, |
| 6078 | keyEntry.policyFlags); |
| 6079 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6080 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6081 | event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6082 | |
| 6083 | mLock.unlock(); |
| 6084 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 6085 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), &event, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6086 | keyEntry.policyFlags, &event); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6087 | |
| 6088 | mLock.lock(); |
| 6089 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6090 | // Cancel the fallback key. |
| 6091 | if (fallbackKeyCode != AKEYCODE_UNKNOWN) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6092 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6093 | "application handled the original non-fallback key " |
| 6094 | "or is no longer a foreground target, " |
| 6095 | "canceling previously dispatched fallback key"); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6096 | options.keyCode = fallbackKeyCode; |
| 6097 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6098 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6099 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6100 | } |
| 6101 | } else { |
| 6102 | // If the application did not handle a non-fallback key, first check |
| 6103 | // that we are in a good state to perform unhandled key event processing |
| 6104 | // Then ask the policy what to do with it. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6105 | bool initialDown = keyEntry.action == AKEY_EVENT_ACTION_DOWN && keyEntry.repeatCount == 0; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6106 | if (fallbackKeyCode == -1 && !initialDown) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6107 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6108 | ALOGD("Unhandled key event: Skipping unhandled key event processing " |
| 6109 | "since this is not an initial down. " |
| 6110 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6111 | originalKeyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 6112 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6113 | return false; |
| 6114 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6115 | |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6116 | // Dispatch the unhandled key to the policy. |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6117 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6118 | ALOGD("Unhandled key event: Asking policy to perform fallback action. " |
| 6119 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
| 6120 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); |
| 6121 | } |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6122 | KeyEvent event = createKeyEvent(keyEntry); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6123 | |
| 6124 | mLock.unlock(); |
| 6125 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 6126 | bool fallback = |
| 6127 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6128 | &event, keyEntry.policyFlags, &event); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6129 | |
| 6130 | mLock.lock(); |
| 6131 | |
Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 6132 | if (connection->status != Connection::Status::NORMAL) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6133 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 6134 | return false; |
| 6135 | } |
| 6136 | |
| 6137 | // Latch the fallback keycode for this key on an initial down. |
| 6138 | // The fallback keycode cannot change at any other point in the lifecycle. |
| 6139 | if (initialDown) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6140 | if (fallback) { |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6141 | fallbackKeyCode = event.getKeyCode(); |
| 6142 | } else { |
| 6143 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
| 6144 | } |
| 6145 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
| 6146 | } |
| 6147 | |
| 6148 | ALOG_ASSERT(fallbackKeyCode != -1); |
| 6149 | |
| 6150 | // Cancel the fallback key if the policy decides not to send it anymore. |
| 6151 | // We will continue to dispatch the key to the policy but we will no |
| 6152 | // longer dispatch a fallback key to the application. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6153 | if (fallbackKeyCode != AKEYCODE_UNKNOWN && |
| 6154 | (!fallback || fallbackKeyCode != event.getKeyCode())) { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6155 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6156 | if (fallback) { |
| 6157 | ALOGD("Unhandled key event: Policy requested to send key %d" |
| 6158 | "as a fallback for %d, but on the DOWN it had requested " |
| 6159 | "to send %d instead. Fallback canceled.", |
| 6160 | event.getKeyCode(), originalKeyCode, fallbackKeyCode); |
| 6161 | } else { |
| 6162 | ALOGD("Unhandled key event: Policy did not request fallback for %d, " |
| 6163 | "but on the DOWN it had requested to send %d. " |
| 6164 | "Fallback canceled.", |
| 6165 | originalKeyCode, fallbackKeyCode); |
| 6166 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6167 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6168 | |
| 6169 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
| 6170 | "canceling fallback, policy no longer desires it"); |
| 6171 | options.keyCode = fallbackKeyCode; |
| 6172 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
| 6173 | |
| 6174 | fallback = false; |
| 6175 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6176 | if (keyEntry.action != AKEY_EVENT_ACTION_UP) { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6177 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6178 | } |
| 6179 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6180 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6181 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6182 | { |
| 6183 | std::string msg; |
| 6184 | const KeyedVector<int32_t, int32_t>& fallbackKeys = |
| 6185 | connection->inputState.getFallbackKeys(); |
| 6186 | for (size_t i = 0; i < fallbackKeys.size(); i++) { |
| 6187 | msg += StringPrintf(", %d->%d", fallbackKeys.keyAt(i), fallbackKeys.valueAt(i)); |
| 6188 | } |
| 6189 | ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.", |
| 6190 | fallbackKeys.size(), msg.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6191 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6192 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6193 | |
| 6194 | if (fallback) { |
| 6195 | // Restart the dispatch cycle using the fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6196 | keyEntry.eventTime = event.getEventTime(); |
| 6197 | keyEntry.deviceId = event.getDeviceId(); |
| 6198 | keyEntry.source = event.getSource(); |
| 6199 | keyEntry.displayId = event.getDisplayId(); |
| 6200 | keyEntry.flags = event.getFlags() | AKEY_EVENT_FLAG_FALLBACK; |
| 6201 | keyEntry.keyCode = fallbackKeyCode; |
| 6202 | keyEntry.scanCode = event.getScanCode(); |
| 6203 | keyEntry.metaState = event.getMetaState(); |
| 6204 | keyEntry.repeatCount = event.getRepeatCount(); |
| 6205 | keyEntry.downTime = event.getDownTime(); |
| 6206 | keyEntry.syntheticRepeat = false; |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6207 | |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6208 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6209 | ALOGD("Unhandled key event: Dispatching fallback key. " |
| 6210 | "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x", |
| 6211 | originalKeyCode, fallbackKeyCode, keyEntry.metaState); |
| 6212 | } |
Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6213 | return true; // restart the event |
| 6214 | } else { |
Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6215 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 6216 | ALOGD("Unhandled key event: No fallback key."); |
| 6217 | } |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6218 | |
| 6219 | // Report the key as unhandled, since there is no fallback key. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6220 | mReporter->reportUnhandledKey(keyEntry.id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6221 | } |
| 6222 | } |
| 6223 | return false; |
| 6224 | } |
| 6225 | |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6226 | bool InputDispatcher::afterMotionEventLockedInterruptable(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6227 | DispatchEntry* dispatchEntry, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6228 | MotionEntry& motionEntry, bool handled) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6229 | return false; |
| 6230 | } |
| 6231 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6232 | void InputDispatcher::traceInboundQueueLengthLocked() { |
| 6233 | if (ATRACE_ENABLED()) { |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 6234 | ATRACE_INT("iq", mInboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6235 | } |
| 6236 | } |
| 6237 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6238 | void InputDispatcher::traceOutboundQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6239 | if (ATRACE_ENABLED()) { |
| 6240 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6241 | snprintf(counterName, sizeof(counterName), "oq:%s", connection.getWindowName().c_str()); |
| 6242 | ATRACE_INT(counterName, connection.outboundQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6243 | } |
| 6244 | } |
| 6245 | |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6246 | void InputDispatcher::traceWaitQueueLength(const Connection& connection) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6247 | if (ATRACE_ENABLED()) { |
| 6248 | char counterName[40]; |
Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6249 | snprintf(counterName, sizeof(counterName), "wq:%s", connection.getWindowName().c_str()); |
| 6250 | ATRACE_INT(counterName, connection.waitQueue.size()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6251 | } |
| 6252 | } |
| 6253 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6254 | void InputDispatcher::dump(std::string& dump) { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6255 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6256 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6257 | dump += "Input Dispatcher State:\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6258 | dumpDispatchStateLocked(dump); |
| 6259 | |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6260 | if (!mLastAnrState.empty()) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6261 | dump += "\nInput Dispatcher State at time of last ANR:\n"; |
Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6262 | dump += mLastAnrState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6263 | } |
| 6264 | } |
| 6265 | |
| 6266 | void InputDispatcher::monitor() { |
| 6267 | // 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] | 6268 | std::unique_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6269 | mLooper->wake(); |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6270 | mDispatcherIsAlive.wait(_l); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6271 | } |
| 6272 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6273 | /** |
| 6274 | * Wake up the dispatcher and wait until it processes all events and commands. |
| 6275 | * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so |
| 6276 | * this method can be safely called from any thread, as long as you've ensured that |
| 6277 | * the work you are interested in completing has already been queued. |
| 6278 | */ |
| 6279 | bool InputDispatcher::waitForIdle() { |
| 6280 | /** |
| 6281 | * Timeout should represent the longest possible time that a device might spend processing |
| 6282 | * events and commands. |
| 6283 | */ |
| 6284 | constexpr std::chrono::duration TIMEOUT = 100ms; |
| 6285 | std::unique_lock lock(mLock); |
| 6286 | mLooper->wake(); |
| 6287 | std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT); |
| 6288 | return result == std::cv_status::no_timeout; |
| 6289 | } |
| 6290 | |
Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 6291 | /** |
| 6292 | * Sets focus to the window identified by the token. This must be called |
| 6293 | * after updating any input window handles. |
| 6294 | * |
| 6295 | * Params: |
| 6296 | * request.token - input channel token used to identify the window that should gain focus. |
| 6297 | * request.focusedToken - the token that the caller expects currently to be focused. If the |
| 6298 | * specified token does not match the currently focused window, this request will be dropped. |
| 6299 | * If the specified focused token matches the currently focused window, the call will succeed. |
| 6300 | * Set this to "null" if this call should succeed no matter what the currently focused token is. |
| 6301 | * request.timestamp - SYSTEM_TIME_MONOTONIC timestamp in nanos set by the client (wm) |
| 6302 | * when requesting the focus change. This determines which request gets |
| 6303 | * precedence if there is a focus change request from another source such as pointer down. |
| 6304 | */ |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6305 | void InputDispatcher::setFocusedWindow(const FocusRequest& request) { |
| 6306 | { // acquire lock |
| 6307 | std::scoped_lock _l(mLock); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6308 | std::optional<FocusResolver::FocusChanges> changes = |
| 6309 | mFocusResolver.setFocusedWindow(request, getWindowHandlesLocked(request.displayId)); |
| 6310 | if (changes) { |
| 6311 | onFocusChangedLocked(*changes); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6312 | } |
| 6313 | } // release lock |
| 6314 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6315 | mLooper->wake(); |
| 6316 | } |
| 6317 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6318 | void InputDispatcher::onFocusChangedLocked(const FocusResolver::FocusChanges& changes) { |
| 6319 | if (changes.oldFocus) { |
| 6320 | std::shared_ptr<InputChannel> focusedInputChannel = getInputChannelLocked(changes.oldFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6321 | if (focusedInputChannel) { |
| 6322 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 6323 | "focus left window"); |
| 6324 | synthesizeCancelationEventsForInputChannelLocked(focusedInputChannel, options); |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6325 | enqueueFocusEventLocked(changes.oldFocus, false /*hasFocus*/, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6326 | } |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6327 | } |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6328 | if (changes.newFocus) { |
| 6329 | enqueueFocusEventLocked(changes.newFocus, true /*hasFocus*/, changes.reason); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6330 | } |
| 6331 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6332 | // If a window has pointer capture, then it must have focus. We need to ensure that this |
| 6333 | // contract is upheld when pointer capture is being disabled due to a loss of window focus. |
| 6334 | // If the window loses focus before it loses pointer capture, then the window can be in a state |
| 6335 | // where it has pointer capture but not focus, violating the contract. Therefore we must |
| 6336 | // dispatch the pointer capture event before the focus event. Since focus events are added to |
| 6337 | // the front of the queue (above), we add the pointer capture event to the front of the queue |
| 6338 | // after the focus events are added. This ensures the pointer capture event ends up at the |
| 6339 | // front. |
| 6340 | disablePointerCaptureForcedLocked(); |
| 6341 | |
Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6342 | if (mFocusedDisplayId == changes.displayId) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6343 | sendFocusChangedCommandLocked(changes.oldFocus, changes.newFocus); |
Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6344 | } |
| 6345 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6346 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6347 | void InputDispatcher::disablePointerCaptureForcedLocked() { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6348 | if (!mCurrentPointerCaptureRequest.enable && !mWindowTokenWithPointerCapture) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6349 | return; |
| 6350 | } |
| 6351 | |
| 6352 | ALOGD_IF(DEBUG_FOCUS, "Disabling Pointer Capture because the window lost focus."); |
| 6353 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6354 | if (mCurrentPointerCaptureRequest.enable) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6355 | setPointerCaptureLocked(false); |
| 6356 | } |
| 6357 | |
| 6358 | if (!mWindowTokenWithPointerCapture) { |
| 6359 | // No need to send capture changes because no window has capture. |
| 6360 | return; |
| 6361 | } |
| 6362 | |
| 6363 | if (mPendingEvent != nullptr) { |
| 6364 | // Move the pending event to the front of the queue. This will give the chance |
| 6365 | // for the pending event to be dropped if it is a captured event. |
| 6366 | mInboundQueue.push_front(mPendingEvent); |
| 6367 | mPendingEvent = nullptr; |
| 6368 | } |
| 6369 | |
| 6370 | auto entry = std::make_unique<PointerCaptureChangedEntry>(mIdGenerator.nextId(), now(), |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6371 | mCurrentPointerCaptureRequest); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6372 | mInboundQueue.push_front(std::move(entry)); |
| 6373 | } |
| 6374 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6375 | void InputDispatcher::setPointerCaptureLocked(bool enable) { |
| 6376 | mCurrentPointerCaptureRequest.enable = enable; |
| 6377 | mCurrentPointerCaptureRequest.seq++; |
| 6378 | auto command = [this, request = mCurrentPointerCaptureRequest]() REQUIRES(mLock) { |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6379 | scoped_unlock unlock(mLock); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6380 | mPolicy->setPointerCapture(request); |
Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6381 | }; |
| 6382 | postCommandLocked(std::move(command)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6383 | } |
| 6384 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6385 | void InputDispatcher::displayRemoved(int32_t displayId) { |
| 6386 | { // acquire lock |
| 6387 | std::scoped_lock _l(mLock); |
| 6388 | // Set an empty list to remove all handles from the specific display. |
| 6389 | setInputWindowsLocked(/* window handles */ {}, displayId); |
| 6390 | setFocusedApplicationLocked(displayId, nullptr); |
| 6391 | // Call focus resolver to clean up stale requests. This must be called after input windows |
| 6392 | // have been removed for the removed display. |
| 6393 | mFocusResolver.displayRemoved(displayId); |
Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 6394 | // Reset pointer capture eligibility, regardless of previous state. |
| 6395 | std::erase(mIneligibleDisplaysForPointerCapture, displayId); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 6396 | // Remove the associated touch mode state. |
| 6397 | mTouchModePerDisplay.erase(displayId); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6398 | } // release lock |
| 6399 | |
| 6400 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6401 | mLooper->wake(); |
| 6402 | } |
| 6403 | |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6404 | void InputDispatcher::onWindowInfosChanged(const std::vector<WindowInfo>& windowInfos, |
| 6405 | const std::vector<DisplayInfo>& displayInfos) { |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6406 | // The listener sends the windows as a flattened array. Separate the windows by display for |
| 6407 | // more convenient parsing. |
| 6408 | std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>> handlesPerDisplay; |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6409 | for (const auto& info : windowInfos) { |
| 6410 | handlesPerDisplay.emplace(info.displayId, std::vector<sp<WindowInfoHandle>>()); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6411 | handlesPerDisplay[info.displayId].push_back(sp<WindowInfoHandle>::make(info)); |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6412 | } |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6413 | |
| 6414 | { // acquire lock |
| 6415 | std::scoped_lock _l(mLock); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 6416 | |
| 6417 | // Ensure that we have an entry created for all existing displays so that if a displayId has |
| 6418 | // no windows, we can tell that the windows were removed from the display. |
| 6419 | for (const auto& [displayId, _] : mWindowHandlesByDisplay) { |
| 6420 | handlesPerDisplay[displayId]; |
| 6421 | } |
| 6422 | |
Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6423 | mDisplayInfos.clear(); |
| 6424 | for (const auto& displayInfo : displayInfos) { |
| 6425 | mDisplayInfos.emplace(displayInfo.displayId, displayInfo); |
| 6426 | } |
| 6427 | |
| 6428 | for (const auto& [displayId, handles] : handlesPerDisplay) { |
| 6429 | setInputWindowsLocked(handles, displayId); |
| 6430 | } |
| 6431 | } |
| 6432 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 6433 | mLooper->wake(); |
chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6434 | } |
| 6435 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6436 | bool InputDispatcher::shouldDropInput( |
| 6437 | const EventEntry& entry, const sp<android::gui::WindowInfoHandle>& windowHandle) const { |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6438 | if (windowHandle->getInfo()->inputConfig.test(WindowInfo::InputConfig::DROP_INPUT) || |
| 6439 | (windowHandle->getInfo()->inputConfig.test( |
| 6440 | WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED) && |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6441 | isWindowObscuredLocked(windowHandle))) { |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6442 | ALOGW("Dropping %s event targeting %s as requested by the input configuration {%s} on " |
| 6443 | "display %" PRId32 ".", |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6444 | ftl::enum_string(entry.type).c_str(), windowHandle->getName().c_str(), |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6445 | windowHandle->getInfo()->inputConfig.string().c_str(), |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6446 | windowHandle->getInfo()->displayId); |
| 6447 | return true; |
| 6448 | } |
| 6449 | return false; |
| 6450 | } |
| 6451 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 6452 | void InputDispatcher::DispatcherWindowListener::onWindowInfosChanged( |
| 6453 | const std::vector<gui::WindowInfo>& windowInfos, |
| 6454 | const std::vector<DisplayInfo>& displayInfos) { |
| 6455 | mDispatcher.onWindowInfosChanged(windowInfos, displayInfos); |
| 6456 | } |
| 6457 | |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6458 | void InputDispatcher::cancelCurrentTouch() { |
| 6459 | { |
| 6460 | std::scoped_lock _l(mLock); |
| 6461 | ALOGD("Canceling all ongoing pointer gestures on all displays."); |
| 6462 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 6463 | "cancel current touch"); |
| 6464 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 6465 | |
| 6466 | mTouchStatesByDisplay.clear(); |
| 6467 | mLastHoverWindowHandle.clear(); |
| 6468 | } |
| 6469 | // Wake up poll loop since there might be work to do. |
| 6470 | mLooper->wake(); |
| 6471 | } |
| 6472 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6473 | void InputDispatcher::setMonitorDispatchingTimeoutForTest(std::chrono::nanoseconds timeout) { |
| 6474 | std::scoped_lock _l(mLock); |
| 6475 | mMonitorDispatchingTimeout = timeout; |
| 6476 | } |
| 6477 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 6478 | } // namespace android::inputdispatcher |